fixes Bug #27543 - “Warning: in_array() [function.in-array]: Wrong datatype for secon...
[lhc/web/wiklou.git] / includes / parser / ParserOutput.php
1 <?php
2 /**
3 * Output of the PHP parser
4 *
5 * @file
6 * @ingroup Parser
7 */
8
9 /**
10 * @todo document
11 * @ingroup Parser
12 */
13
14 class CacheTime {
15 var $mVersion = Parser::VERSION, # Compatibility check
16 $mCacheTime = '', # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
17 $mCacheExpiry = null, # Seconds after which the object should expire, use 0 for uncachable. Used in ParserCache.
18 $mContainsOldMagic; # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
19
20 function getCacheTime() { return $this->mCacheTime; }
21
22 function containsOldMagic() { return $this->mContainsOldMagic; }
23 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
24
25 /**
26 * setCacheTime() sets the timestamp expressing when the page has been rendered.
27 * This doesn not control expiry, see updateCacheExpiry() for that!
28 * @param $t string
29 * @return string
30 */
31 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
32
33 /**
34 * Sets the number of seconds after which this object should expire.
35 * This value is used with the ParserCache.
36 * If called with a value greater than the value provided at any previous call,
37 * the new call has no effect. The value returned by getCacheExpiry is smaller
38 * or equal to the smallest number that was provided as an argument to
39 * updateCacheExpiry().
40 *
41 * @param $seconds number
42 */
43 function updateCacheExpiry( $seconds ) {
44 $seconds = (int)$seconds;
45
46 if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds ) {
47 $this->mCacheExpiry = $seconds;
48 }
49
50 // hack: set old-style marker for uncacheable entries.
51 if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 ) {
52 $this->mCacheTime = -1;
53 }
54 }
55
56 /**
57 * Returns the number of seconds after which this object should expire.
58 * This method is used by ParserCache to determine how long the ParserOutput can be cached.
59 * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
60 * The value returned by getCacheExpiry is smaller or equal to the smallest number
61 * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
62 * value of $wgParserCacheExpireTime.
63 */
64 function getCacheExpiry() {
65 global $wgParserCacheExpireTime;
66
67 if ( $this->mCacheTime < 0 ) {
68 return 0;
69 } // old-style marker for "not cachable"
70
71 $expire = $this->mCacheExpiry;
72
73 if ( $expire === null ) {
74 $expire = $wgParserCacheExpireTime;
75 } else {
76 $expire = min( $expire, $wgParserCacheExpireTime );
77 }
78
79 if( $this->containsOldMagic() ) { //compatibility hack
80 $expire = min( $expire, 3600 ); # 1 hour
81 }
82
83 if ( $expire <= 0 ) {
84 return 0; // not cachable
85 } else {
86 return $expire;
87 }
88 }
89
90 /**
91 * @return bool
92 */
93 function isCacheable() {
94 return $this->getCacheExpiry() > 0;
95 }
96
97 /**
98 * Return true if this cached output object predates the global or
99 * per-article cache invalidation timestamps, or if it comes from
100 * an incompatible older version.
101 *
102 * @param $touched String: the affected article's last touched timestamp
103 * @return Boolean
104 */
105 public function expired( $touched ) {
106 global $wgCacheEpoch;
107 return !$this->isCacheable() || // parser says it's uncacheable
108 $this->getCacheTime() < $touched ||
109 $this->getCacheTime() <= $wgCacheEpoch ||
110 $this->getCacheTime() < wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) || // expiry period has passed
111 !isset( $this->mVersion ) ||
112 version_compare( $this->mVersion, Parser::VERSION, "lt" );
113 }
114 }
115
116 class ParserOutput extends CacheTime {
117 var $mText, # The output text
118 $mLanguageLinks, # List of the full text of language links, in the order they appear
119 $mCategories, # Map of category names to sort keys
120 $mTitleText, # title text of the chosen language variant
121 $mLinks = array(), # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
122 $mTemplates = array(), # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
123 $mTemplateIds = array(), # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
124 $mImages = array(), # DB keys of the images used, in the array key only
125 $mImageTimeKeys = array(), # DB keys of the images used mapped to sha1 and MW timestamp
126 $mExternalLinks = array(), # External link URLs, in the key only
127 $mInterwikiLinks = array(), # 2-D map of prefix/DBK (in keys only) for the inline interwiki links in the document.
128 $mNewSection = false, # Show a new section link?
129 $mHideNewSection = false, # Hide the new section link?
130 $mNoGallery = false, # No gallery on category page? (__NOGALLERY__)
131 $mHeadItems = array(), # Items to put in the <head> section
132 $mModules = array(), # Modules to be loaded by the resource loader
133 $mOutputHooks = array(), # Hook tags as per $wgParserOutputHooks
134 $mWarnings = array(), # Warning text to be returned to the user. Wikitext formatted, in the key only
135 $mSections = array(), # Table of contents
136 $mEditSectionTokens = false, # prefix/suffix markers if edit sections were output as tokens
137 $mProperties = array(), # Name/value pairs to be cached in the DB
138 $mTOCHTML = ''; # HTML of the TOC
139 private $mIndexPolicy = ''; # 'index' or 'noindex'? Any other value will result in no change.
140 private $mAccessedOptions = array(); # List of ParserOptions (stored in the keys)
141
142 const EDITSECTION_REGEX = '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
143
144 function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
145 $containsOldMagic = false, $titletext = '' )
146 {
147 $this->mText = $text;
148 $this->mLanguageLinks = $languageLinks;
149 $this->mCategories = $categoryLinks;
150 $this->mContainsOldMagic = $containsOldMagic;
151 $this->mTitleText = $titletext;
152 }
153
154 function getText() {
155 if ( $this->mEditSectionTokens ) {
156 return preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
157 array( &$this, 'replaceEditSectionLinksCallback' ), $this->mText );
158 }
159 return $this->mText;
160 }
161
162 /**
163 * callback used by getText to replace editsection tokens
164 * @private
165 */
166 function replaceEditSectionLinksCallback( $m ) {
167 global $wgOut, $wgLang;
168 $args = array(
169 htmlspecialchars_decode($m[1]),
170 htmlspecialchars_decode($m[2]),
171 isset($m[4]) ? $m[3] : null,
172 );
173 $args[0] = Title::newFromText( $args[0] );
174 if ( !is_object($args[0]) ) {
175 throw new MWException("Bad parser output text.");
176 }
177 $args[] = $wgLang->getCode();
178 $skin = $wgOut->getSkin();
179 return call_user_func_array( array( $skin, 'doEditSectionLink' ), $args );
180 }
181
182 function &getLanguageLinks() { return $this->mLanguageLinks; }
183 function getInterwikiLinks() { return $this->mInterwikiLinks; }
184 function getCategoryLinks() { return array_keys( $this->mCategories ); }
185 function &getCategories() { return $this->mCategories; }
186 function getTitleText() { return $this->mTitleText; }
187 function getSections() { return $this->mSections; }
188 function getEditSectionTokens() { return $this->mEditSectionTokens; }
189 function &getLinks() { return $this->mLinks; }
190 function &getTemplates() { return $this->mTemplates; }
191 function &getTemplateIds() { return $this->mTemplateIds; }
192 function &getImages() { return $this->mImages; }
193 function &getImageTimeKeys() { return $this->mImageTimeKeys; }
194 function &getExternalLinks() { return $this->mExternalLinks; }
195 function getNoGallery() { return $this->mNoGallery; }
196 function getHeadItems() { return $this->mHeadItems; }
197 function getModules() { return $this->mModules; }
198 function getOutputHooks() { return (array)$this->mOutputHooks; }
199 function getWarnings() { return array_keys( $this->mWarnings ); }
200 function getIndexPolicy() { return $this->mIndexPolicy; }
201 function getTOCHTML() { return $this->mTOCHTML; }
202
203 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
204 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
205 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
206
207 function setTitleText( $t ) { return wfSetVar( $this->mTitleText, $t ); }
208 function setSections( $toc ) { return wfSetVar( $this->mSections, $toc ); }
209 function setEditSectionTokens( $t ) { return wfSetVar( $this->mEditSectionTokens, $t ); }
210 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy, $policy ); }
211 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML, $tochtml ); }
212
213 function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
214 function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
215 function addWarning( $s ) { $this->mWarnings[$s] = 1; }
216
217 function addOutputHook( $hook, $data = false ) {
218 $this->mOutputHooks[] = array( $hook, $data );
219 }
220
221 function setNewSection( $value ) {
222 $this->mNewSection = (bool)$value;
223 }
224 function hideNewSection ( $value ) {
225 $this->mHideNewSection = (bool)$value;
226 }
227 function getHideNewSection () {
228 return (bool)$this->mHideNewSection;
229 }
230 function getNewSection() {
231 return (bool)$this->mNewSection;
232 }
233
234 function addExternalLink( $url ) {
235 # We don't register links pointing to our own server, unless... :-)
236 global $wgServer, $wgRegisterInternalExternals;
237 if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
238 $this->mExternalLinks[$url] = 1;
239 }
240
241 /**
242 * Record a local or interwiki inline link for saving in future link tables.
243 *
244 * @param $title Title object
245 * @param $id Mixed: optional known page_id so we can skip the lookup
246 */
247 function addLink( $title, $id = null ) {
248 if ( $title->isExternal() ) {
249 // Don't record interwikis in pagelinks
250 $this->addInterwikiLink( $title );
251 return;
252 }
253 $ns = $title->getNamespace();
254 $dbk = $title->getDBkey();
255 if ( $ns == NS_MEDIA ) {
256 // Normalize this pseudo-alias if it makes it down here...
257 $ns = NS_FILE;
258 } elseif( $ns == NS_SPECIAL ) {
259 // We don't record Special: links currently
260 // It might actually be wise to, but we'd need to do some normalization.
261 return;
262 } elseif( $dbk === '' ) {
263 // Don't record self links - [[#Foo]]
264 return;
265 }
266 if ( !isset( $this->mLinks[$ns] ) ) {
267 $this->mLinks[$ns] = array();
268 }
269 if ( is_null( $id ) ) {
270 $id = $title->getArticleID();
271 }
272 $this->mLinks[$ns][$dbk] = $id;
273 }
274
275 /**
276 * Register a file dependency for this output
277 * @param $name string Title dbKey
278 * @param $timestamp string MW timestamp of file creation (or false if non-existing)
279 * @param $sha string base 36 SHA-1 of file (or false if non-existing)
280 * @return void
281 */
282 function addImage( $name, $timestamp = null, $sha1 = null ) {
283 $this->mImages[$name] = 1;
284 if ( $timestamp !== null && $sha1 !== null ) {
285 $this->mImageTimeKeys[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
286 }
287 }
288
289 /**
290 * Register a template dependency for this output
291 * @param $title Title
292 * @param $page_id
293 * @param $rev_id
294 * @return void
295 */
296 function addTemplate( $title, $page_id, $rev_id ) {
297 $ns = $title->getNamespace();
298 $dbk = $title->getDBkey();
299 if ( !isset( $this->mTemplates[$ns] ) ) {
300 $this->mTemplates[$ns] = array();
301 }
302 $this->mTemplates[$ns][$dbk] = $page_id;
303 if ( !isset( $this->mTemplateIds[$ns] ) ) {
304 $this->mTemplateIds[$ns] = array();
305 }
306 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
307 }
308
309 /**
310 * @param $title Title object, must be an interwiki link
311 * @throws MWException if given invalid input
312 */
313 function addInterwikiLink( $title ) {
314 $prefix = $title->getInterwiki();
315 if( $prefix == '' ) {
316 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
317 }
318 if (!isset($this->mInterwikiLinks[$prefix])) {
319 $this->mInterwikiLinks[$prefix] = array();
320 }
321 $this->mInterwikiLinks[$prefix][$title->getDBkey()] = 1;
322 }
323
324 /**
325 * Add some text to the <head>.
326 * If $tag is set, the section with that tag will only be included once
327 * in a given page.
328 */
329 function addHeadItem( $section, $tag = false ) {
330 if ( $tag !== false ) {
331 $this->mHeadItems[$tag] = $section;
332 } else {
333 $this->mHeadItems[] = $section;
334 }
335 }
336
337 function addModules( $modules ) {
338 $this->mModules = array_merge( $this->mModules, (array) $modules );
339 }
340
341 /**
342 * Override the title to be used for display
343 * -- this is assumed to have been validated
344 * (check equal normalisation, etc.)
345 *
346 * @param $text String: desired title text
347 */
348 public function setDisplayTitle( $text ) {
349 $this->setTitleText( $text );
350 $this->setProperty( 'displaytitle', $text );
351 }
352
353 /**
354 * Get the title to be used for display
355 *
356 * @return String
357 */
358 public function getDisplayTitle() {
359 $t = $this->getTitleText();
360 if( $t === '' ) {
361 return false;
362 }
363 return $t;
364 }
365
366 /**
367 * Fairly generic flag setter thingy.
368 */
369 public function setFlag( $flag ) {
370 $this->mFlags[$flag] = true;
371 }
372
373 public function getFlag( $flag ) {
374 return isset( $this->mFlags[$flag] );
375 }
376
377 /**
378 * Set a property to be cached in the DB
379 */
380 public function setProperty( $name, $value ) {
381 $this->mProperties[$name] = $value;
382 }
383
384 public function getProperty( $name ){
385 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
386 }
387
388 public function getProperties() {
389 if ( !isset( $this->mProperties ) ) {
390 $this->mProperties = array();
391 }
392 return $this->mProperties;
393 }
394
395
396 /**
397 * Returns the options from its ParserOptions which have been taken
398 * into account to produce this output or false if not available.
399 * @return mixed Array
400 */
401 public function getUsedOptions() {
402 if ( !isset( $this->mAccessedOptions ) ) {
403 return array();
404 }
405 return array_keys( $this->mAccessedOptions );
406 }
407
408 /**
409 * Callback passed by the Parser to the ParserOptions to keep track of which options are used.
410 * @access private
411 */
412 function recordOption( $option ) {
413 $this->mAccessedOptions[$option] = true;
414 }
415 }