Fixed a bunch of silly instances of [^!=]==\s*(true|false)
[lhc/web/wiklou.git] / includes / parser / ParserOutput.php
1 <?php
2 /**
3 * @todo document
4 * @ingroup Parser
5 */
6 class ParserOutput
7 {
8 var $mText, # The output text
9 $mLanguageLinks, # List of the full text of language links, in the order they appear
10 $mCategories, # Map of category names to sort keys
11 $mContainsOldMagic, # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
12 $mTitleText, # title text of the chosen language variant
13 $mCacheTime = '', # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
14 $mCacheExpiry = null, # Seconds after which the object should expire, use 0 for uncachable. Used in ParserCache.
15 $mVersion = Parser::VERSION, # Compatibility check
16 $mLinks = array(), # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
17 $mTemplates = array(), # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
18 $mTemplateIds = array(), # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
19 $mImages = array(), # DB keys of the images used, in the array key only
20 $mExternalLinks = array(), # External link URLs, in the key only
21 $mInterwikiLinks = array(), # 2-D map of prefix/DBK (in keys only) for the inline interwiki links in the document.
22 $mNewSection = false, # Show a new section link?
23 $mHideNewSection = false, # Hide the new section link?
24 $mNoGallery = false, # No gallery on category page? (__NOGALLERY__)
25 $mHeadItems = array(), # Items to put in the <head> section
26 $mOutputHooks = array(), # Hook tags as per $wgParserOutputHooks
27 $mWarnings = array(), # Warning text to be returned to the user. Wikitext formatted, in the key only
28 $mSections = array(), # Table of contents
29 $mProperties = array(), # Name/value pairs to be cached in the DB
30 $mTOCHTML = ''; # HTML of the TOC
31 private $mIndexPolicy = '', # 'index' or 'noindex'? Any other value will result in no change.
32 $mPageIcons = array(); # Array of icons to show for the page (like Protect, Featured, etc)
33
34 function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
35 $containsOldMagic = false, $titletext = '' )
36 {
37 $this->mText = $text;
38 $this->mLanguageLinks = $languageLinks;
39 $this->mCategories = $categoryLinks;
40 $this->mContainsOldMagic = $containsOldMagic;
41 $this->mTitleText = $titletext;
42 }
43
44 function getText() { return $this->mText; }
45 function &getLanguageLinks() { return $this->mLanguageLinks; }
46 function getInterwikiLinks() { return $this->mInterwikiLinks; }
47 function getCategoryLinks() { return array_keys( $this->mCategories ); }
48 function &getCategories() { return $this->mCategories; }
49 function getCacheTime() { return $this->mCacheTime; }
50 function getTitleText() { return $this->mTitleText; }
51 function getSections() { return $this->mSections; }
52 function &getLinks() { return $this->mLinks; }
53 function &getTemplates() { return $this->mTemplates; }
54 function &getImages() { return $this->mImages; }
55 function &getExternalLinks() { return $this->mExternalLinks; }
56 function getNoGallery() { return $this->mNoGallery; }
57 function getHeadItems() { return $this->mHeadItems; }
58 function getSubtitle() { return $this->mSubtitle; }
59 function getOutputHooks() { return (array)$this->mOutputHooks; }
60 function getWarnings() { return array_keys( $this->mWarnings ); }
61 function getIndexPolicy() { return $this->mIndexPolicy; }
62 function getTOCHTML() { return $this->mTOCHTML; }
63 function getPageIcons() { return $this->mPageIcons; }
64
65 function containsOldMagic() { return $this->mContainsOldMagic; }
66 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
67 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
68 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
69 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
70
71 /** setCacheTime() sets the timestamp expressing when the page has been rendered.
72 * This doesn not control expiry, see updateCacheExpiry() for that!
73 */
74 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
75 function setTitleText( $t ) { return wfSetVar( $this->mTitleText, $t ); }
76 function setSections( $toc ) { return wfSetVar( $this->mSections, $toc ); }
77 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy, $policy ); }
78 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML, $tochtml ); }
79
80
81 /** Sets the number of seconds after which this object should expire.
82 * This value is used with the ParserCache.
83 * If called with a value greater than the value provided at any previous call,
84 * the new call has no effect. The value returned by getCacheExpiry is smaller
85 * or equal to the smallest number that was provided as an argument to
86 * updateCacheExpiry().
87 */
88 function updateCacheExpiry( $seconds ) {
89 $seconds = (int)$seconds;
90
91 if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds )
92 $this->mCacheExpiry = $seconds;
93
94 // hack: set old-style marker for uncacheable entries.
95 if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 )
96 $this->mCacheTime = -1;
97 }
98
99 /** Returns the number of seconds after which this object should expire.
100 * This method is used by ParserCache to determine how long the ParserOutput can be cached.
101 * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
102 * The value returned by getCacheExpiry is smaller or equal to the smallest number
103 * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
104 * value of $wgParserCacheExpireTime.
105 */
106 function getCacheExpiry() {
107 global $wgParserCacheExpireTime;
108
109 if ( $this->mCacheTime < 0 ) return 0; // old-style marker for "not cachable"
110
111 $expire = $this->mCacheExpiry;
112
113 if ( $expire === null )
114 $expire = $wgParserCacheExpireTime;
115 else
116 $expire = min( $expire, $wgParserCacheExpireTime );
117
118 if( $this->containsOldMagic() ) { //compatibility hack
119 $expire = min( $expire, 3600 ); # 1 hour
120 }
121
122 if ( $expire <= 0 ) return 0; // not cachable
123 else return $expire;
124 }
125
126 function isCacheable() {
127 return $this->getCacheExpiry() > 0;
128 }
129
130 function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
131 function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
132 function addWarning( $s ) { $this->mWarnings[$s] = 1; }
133
134 function addOutputHook( $hook, $data = false ) {
135 $this->mOutputHooks[] = array( $hook, $data );
136 }
137
138 function setNewSection( $value ) {
139 $this->mNewSection = (bool)$value;
140 }
141 function hideNewSection ( $value ) {
142 $this->mHideNewSection = (bool)$value;
143 }
144 function getHideNewSection () {
145 return (bool)$this->mHideNewSection;
146 }
147 function getNewSection() {
148 return (bool)$this->mNewSection;
149 }
150
151 function addExternalLink( $url ) {
152 # We don't register links pointing to our own server, unless... :-)
153 global $wgServer, $wgRegisterInternalExternals;
154 if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
155 $this->mExternalLinks[$url] = 1;
156 }
157
158 /**
159 * Record a local or interwiki inline link for saving in future link tables.
160 *
161 * @param $title Title object
162 * @param $id Mixed: optional known page_id so we can skip the lookup
163 */
164 function addLink( $title, $id = null ) {
165 if ( $title->isExternal() ) {
166 // Don't record interwikis in pagelinks
167 $this->addInterwikiLink( $title );
168 return;
169 }
170 $ns = $title->getNamespace();
171 $dbk = $title->getDBkey();
172 if ( $ns == NS_MEDIA ) {
173 // Normalize this pseudo-alias if it makes it down here...
174 $ns = NS_FILE;
175 } elseif( $ns == NS_SPECIAL ) {
176 // We don't record Special: links currently
177 // It might actually be wise to, but we'd need to do some normalization.
178 return;
179 } elseif( $dbk === '' ) {
180 // Don't record self links - [[#Foo]]
181 return;
182 }
183 if ( !isset( $this->mLinks[$ns] ) ) {
184 $this->mLinks[$ns] = array();
185 }
186 if ( is_null( $id ) ) {
187 $id = $title->getArticleID();
188 }
189 $this->mLinks[$ns][$dbk] = $id;
190 }
191
192 function addImage( $name ) {
193 $this->mImages[$name] = 1;
194 }
195
196 function addTemplate( $title, $page_id, $rev_id ) {
197 $ns = $title->getNamespace();
198 $dbk = $title->getDBkey();
199 if ( !isset( $this->mTemplates[$ns] ) ) {
200 $this->mTemplates[$ns] = array();
201 }
202 $this->mTemplates[$ns][$dbk] = $page_id;
203 if ( !isset( $this->mTemplateIds[$ns] ) ) {
204 $this->mTemplateIds[$ns] = array();
205 }
206 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
207 }
208
209 /**
210 * @param $title Title object, must be an interwiki link
211 * @throws MWException if given invalid input
212 */
213 function addInterwikiLink( $title ) {
214 $prefix = $title->getInterwiki();
215 if( $prefix == '' ) {
216 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
217 }
218 if (!isset($this->mInterwikiLinks[$prefix])) {
219 $this->mInterwikiLinks[$prefix] = array();
220 }
221 $this->mInterwikiLinks[$prefix][$title->getDBkey()] = 1;
222 }
223
224 /**
225 * Return true if this cached output object predates the global or
226 * per-article cache invalidation timestamps, or if it comes from
227 * an incompatible older version.
228 *
229 * @param $touched String: the affected article's last touched timestamp
230 * @return Boolean
231 */
232 public function expired( $touched ) {
233 global $wgCacheEpoch;
234 return !$this->isCacheable() || // parser says it's uncacheable
235 $this->getCacheTime() < $touched ||
236 $this->getCacheTime() <= $wgCacheEpoch ||
237 $this->getCacheTime() < wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) || // expiry period has passed
238 !isset( $this->mVersion ) ||
239 version_compare( $this->mVersion, Parser::VERSION, "lt" );
240 }
241
242 /**
243 * Add some text to the <head>.
244 * If $tag is set, the section with that tag will only be included once
245 * in a given page.
246 */
247 function addHeadItem( $section, $tag = false ) {
248 if ( $tag !== false ) {
249 $this->mHeadItems[$tag] = $section;
250 } else {
251 $this->mHeadItems[] = $section;
252 }
253 }
254
255 /**
256 * Add page icons to the parser output.
257 * @param File $file A valid file
258 * @param String $alt Alt text, if any
259 */
260 function addPageIcon( $file, $alt = '' ) {
261 $this->mPageIcons[] = array( $file, $alt );
262 }
263
264 /**
265 * Override the title to be used for display
266 * -- this is assumed to have been validated
267 * (check equal normalisation, etc.)
268 *
269 * @param $text String: desired title text
270 */
271 public function setDisplayTitle( $text ) {
272 $this->setTitleText( $text );
273 }
274
275 /**
276 * Get the title to be used for display
277 *
278 * @return String
279 */
280 public function getDisplayTitle() {
281 $t = $this->getTitleText( );
282 if( $t === '' ) {
283 return false;
284 }
285 return $t;
286 }
287
288 /**
289 * Fairly generic flag setter thingy.
290 */
291 public function setFlag( $flag ) {
292 $this->mFlags[$flag] = true;
293 }
294
295 public function getFlag( $flag ) {
296 return isset( $this->mFlags[$flag] );
297 }
298
299 /**
300 * Set a property to be cached in the DB
301 */
302 public function setProperty( $name, $value ) {
303 $this->mProperties[$name] = $value;
304 }
305
306 public function getProperty( $name ){
307 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
308 }
309
310 public function getProperties() {
311 if ( !isset( $this->mProperties ) ) {
312 $this->mProperties = array();
313 }
314 return $this->mProperties;
315 }
316 }