Merge "Rename $usableSkins to $allowedSkins"
[lhc/web/wiklou.git] / includes / parser / ParserOutput.php
1 <?php
2
3 /**
4 * Output of the PHP parser.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Parser
23 */
24 class ParserOutput extends CacheTime {
25 var $mText, # The output text
26 $mLanguageLinks, # List of the full text of language links, in the order they appear
27 $mCategories, # Map of category names to sort keys
28 $mTitleText, # title text of the chosen language variant
29 $mLinks = array(), # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
30 $mTemplates = array(), # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
31 $mTemplateIds = array(), # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
32 $mImages = array(), # DB keys of the images used, in the array key only
33 $mFileSearchOptions = array(), # DB keys of the images used mapped to sha1 and MW timestamp
34 $mExternalLinks = array(), # External link URLs, in the key only
35 $mInterwikiLinks = array(), # 2-D map of prefix/DBK (in keys only) for the inline interwiki links in the document.
36 $mNewSection = false, # Show a new section link?
37 $mHideNewSection = false, # Hide the new section link?
38 $mNoGallery = false, # No gallery on category page? (__NOGALLERY__)
39 $mHeadItems = array(), # Items to put in the <head> section
40 $mModules = array(), # Modules to be loaded by the resource loader
41 $mModuleScripts = array(), # Modules of which only the JS will be loaded by the resource loader
42 $mModuleStyles = array(), # Modules of which only the CSSS will be loaded by the resource loader
43 $mModuleMessages = array(), # Modules of which only the messages will be loaded by the resource loader
44 $mOutputHooks = array(), # Hook tags as per $wgParserOutputHooks
45 $mWarnings = array(), # Warning text to be returned to the user. Wikitext formatted, in the key only
46 $mSections = array(), # Table of contents
47 $mEditSectionTokens = false, # prefix/suffix markers if edit sections were output as tokens
48 $mProperties = array(), # Name/value pairs to be cached in the DB
49 $mTOCHTML = '', # HTML of the TOC
50 $mTimestamp; # Timestamp of the revision
51 private $mIndexPolicy = ''; # 'index' or 'noindex'? Any other value will result in no change.
52 private $mAccessedOptions = array(); # List of ParserOptions (stored in the keys)
53 private $mSecondaryDataUpdates = array(); # List of DataUpdate, used to save info from the page somewhere else.
54 private $mExtensionData = array(); # extra data used by extensions
55
56 const EDITSECTION_REGEX = '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
57
58 function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
59 $containsOldMagic = false, $titletext = '' )
60 {
61 $this->mText = $text;
62 $this->mLanguageLinks = $languageLinks;
63 $this->mCategories = $categoryLinks;
64 $this->mContainsOldMagic = $containsOldMagic;
65 $this->mTitleText = $titletext;
66 }
67
68 function getText() {
69 if ( $this->mEditSectionTokens ) {
70 $text = $this->mText;
71
72 // If there's old output with misplaced editsections links cached, mangle it to put them in
73 // the right position. We can assume that there is no '</hN>' inside header tags, making this
74 // possible to do with a regex.
75 $text = preg_replace(
76 // [ this part is like EDITSECTION_REGEX, but with non-capturing groups ]
77 // note the space here ------v
78 '#(<[hH](\d)>)(<(?:mw:)?editsection page="(?:.*?)" section="(?:.*?)"(?:/>|>(?:.*?)(?:</(?:mw:)?editsection>))) ([\s\S]*?)(</[hH]\2>)#',
79 // swap the order of content and editsection link - $2 is ignored since it's the number in hN's tag name
80 '$1$4 $3$5',
81 $text
82 );
83
84 return preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
85 array( &$this, 'replaceEditSectionLinksCallback' ), $text );
86 }
87 return preg_replace( ParserOutput::EDITSECTION_REGEX, '', $this->mText );
88 }
89
90 /**
91 * callback used by getText to replace editsection tokens
92 * @private
93 * @param $m
94 * @throws MWException
95 * @return mixed
96 */
97 function replaceEditSectionLinksCallback( $m ) {
98 global $wgOut, $wgLang;
99 $args = array(
100 htmlspecialchars_decode( $m[1] ),
101 htmlspecialchars_decode( $m[2] ),
102 isset( $m[4] ) ? $m[3] : null,
103 );
104 $args[0] = Title::newFromText( $args[0] );
105 if ( !is_object( $args[0] ) ) {
106 throw new MWException( "Bad parser output text." );
107 }
108 $args[] = $wgLang->getCode();
109 $skin = $wgOut->getSkin();
110 return call_user_func_array( array( $skin, 'doEditSectionLink' ), $args );
111 }
112
113 function &getLanguageLinks() { return $this->mLanguageLinks; }
114 function getInterwikiLinks() { return $this->mInterwikiLinks; }
115 function getCategoryLinks() { return array_keys( $this->mCategories ); }
116 function &getCategories() { return $this->mCategories; }
117 function getTitleText() { return $this->mTitleText; }
118 function getSections() { return $this->mSections; }
119 function getEditSectionTokens() { return $this->mEditSectionTokens; }
120 function &getLinks() { return $this->mLinks; }
121 function &getTemplates() { return $this->mTemplates; }
122 function &getTemplateIds() { return $this->mTemplateIds; }
123 function &getImages() { return $this->mImages; }
124 function &getFileSearchOptions() { return $this->mFileSearchOptions; }
125 function &getExternalLinks() { return $this->mExternalLinks; }
126 function getNoGallery() { return $this->mNoGallery; }
127 function getHeadItems() { return $this->mHeadItems; }
128 function getModules() { return $this->mModules; }
129 function getModuleScripts() { return $this->mModuleScripts; }
130 function getModuleStyles() { return $this->mModuleStyles; }
131 function getModuleMessages() { return $this->mModuleMessages; }
132 function getOutputHooks() { return (array)$this->mOutputHooks; }
133 function getWarnings() { return array_keys( $this->mWarnings ); }
134 function getIndexPolicy() { return $this->mIndexPolicy; }
135 function getTOCHTML() { return $this->mTOCHTML; }
136 function getTimestamp() { return $this->mTimestamp; }
137
138 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
139 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
140 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
141
142 function setTitleText( $t ) { return wfSetVar( $this->mTitleText, $t ); }
143 function setSections( $toc ) { return wfSetVar( $this->mSections, $toc ); }
144 function setEditSectionTokens( $t ) { return wfSetVar( $this->mEditSectionTokens, $t ); }
145 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy, $policy ); }
146 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML, $tochtml ); }
147 function setTimestamp( $timestamp ) { return wfSetVar( $this->mTimestamp, $timestamp ); }
148
149 function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
150 function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
151 function addWarning( $s ) { $this->mWarnings[$s] = 1; }
152
153 function addOutputHook( $hook, $data = false ) {
154 $this->mOutputHooks[] = array( $hook, $data );
155 }
156
157 function setNewSection( $value ) {
158 $this->mNewSection = (bool)$value;
159 }
160 function hideNewSection( $value ) {
161 $this->mHideNewSection = (bool)$value;
162 }
163 function getHideNewSection() {
164 return (bool)$this->mHideNewSection;
165 }
166 function getNewSection() {
167 return (bool)$this->mNewSection;
168 }
169
170 /**
171 * Checks, if a url is pointing to the own server
172 *
173 * @param string $internal the server to check against
174 * @param string $url the url to check
175 * @return bool
176 */
177 static function isLinkInternal( $internal, $url ) {
178 return (bool)preg_match( '/^' .
179 # If server is proto relative, check also for http/https links
180 ( substr( $internal, 0, 2 ) === '//' ? '(?:https?:)?' : '' ) .
181 preg_quote( $internal, '/' ) .
182 # check for query/path/anchor or end of link in each case
183 '(?:[\?\/\#]|$)/i',
184 $url
185 );
186 }
187
188 function addExternalLink( $url ) {
189 # We don't register links pointing to our own server, unless... :-)
190 global $wgServer, $wgRegisterInternalExternals;
191
192 $registerExternalLink = true;
193 if ( !$wgRegisterInternalExternals ) {
194 $registerExternalLink = !self::isLinkInternal( $wgServer, $url );
195 }
196 if ( $registerExternalLink ) {
197 $this->mExternalLinks[$url] = 1;
198 }
199 }
200
201 /**
202 * Record a local or interwiki inline link for saving in future link tables.
203 *
204 * @param $title Title object
205 * @param $id Mixed: optional known page_id so we can skip the lookup
206 */
207 function addLink( Title $title, $id = null ) {
208 if ( $title->isExternal() ) {
209 // Don't record interwikis in pagelinks
210 $this->addInterwikiLink( $title );
211 return;
212 }
213 $ns = $title->getNamespace();
214 $dbk = $title->getDBkey();
215 if ( $ns == NS_MEDIA ) {
216 // Normalize this pseudo-alias if it makes it down here...
217 $ns = NS_FILE;
218 } elseif ( $ns == NS_SPECIAL ) {
219 // We don't record Special: links currently
220 // It might actually be wise to, but we'd need to do some normalization.
221 return;
222 } elseif ( $dbk === '' ) {
223 // Don't record self links - [[#Foo]]
224 return;
225 }
226 if ( !isset( $this->mLinks[$ns] ) ) {
227 $this->mLinks[$ns] = array();
228 }
229 if ( is_null( $id ) ) {
230 $id = $title->getArticleID();
231 }
232 $this->mLinks[$ns][$dbk] = $id;
233 }
234
235 /**
236 * Register a file dependency for this output
237 * @param string $name Title dbKey
238 * @param string $timestamp MW timestamp of file creation (or false if non-existing)
239 * @param string $sha1 base 36 SHA-1 of file (or false if non-existing)
240 * @return void
241 */
242 function addImage( $name, $timestamp = null, $sha1 = null ) {
243 $this->mImages[$name] = 1;
244 if ( $timestamp !== null && $sha1 !== null ) {
245 $this->mFileSearchOptions[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
246 }
247 }
248
249 /**
250 * Register a template dependency for this output
251 * @param $title Title
252 * @param $page_id
253 * @param $rev_id
254 * @return void
255 */
256 function addTemplate( $title, $page_id, $rev_id ) {
257 $ns = $title->getNamespace();
258 $dbk = $title->getDBkey();
259 if ( !isset( $this->mTemplates[$ns] ) ) {
260 $this->mTemplates[$ns] = array();
261 }
262 $this->mTemplates[$ns][$dbk] = $page_id;
263 if ( !isset( $this->mTemplateIds[$ns] ) ) {
264 $this->mTemplateIds[$ns] = array();
265 }
266 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
267 }
268
269 /**
270 * @param $title Title object, must be an interwiki link
271 * @throws MWException if given invalid input
272 */
273 function addInterwikiLink( $title ) {
274 $prefix = $title->getInterwiki();
275 if ( $prefix == '' ) {
276 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
277 }
278 if ( !isset( $this->mInterwikiLinks[$prefix] ) ) {
279 $this->mInterwikiLinks[$prefix] = array();
280 }
281 $this->mInterwikiLinks[$prefix][$title->getDBkey()] = 1;
282 }
283
284 /**
285 * Add some text to the "<head>".
286 * If $tag is set, the section with that tag will only be included once
287 * in a given page.
288 */
289 function addHeadItem( $section, $tag = false ) {
290 if ( $tag !== false ) {
291 $this->mHeadItems[$tag] = $section;
292 } else {
293 $this->mHeadItems[] = $section;
294 }
295 }
296
297 public function addModules( $modules ) {
298 $this->mModules = array_merge( $this->mModules, (array) $modules );
299 }
300
301 public function addModuleScripts( $modules ) {
302 $this->mModuleScripts = array_merge( $this->mModuleScripts, (array)$modules );
303 }
304
305 public function addModuleStyles( $modules ) {
306 $this->mModuleStyles = array_merge( $this->mModuleStyles, (array)$modules );
307 }
308
309 public function addModuleMessages( $modules ) {
310 $this->mModuleMessages = array_merge( $this->mModuleMessages, (array)$modules );
311 }
312
313 /**
314 * Copy items from the OutputPage object into this one
315 *
316 * @param $out OutputPage object
317 */
318 public function addOutputPageMetadata( OutputPage $out ) {
319 $this->addModules( $out->getModules() );
320 $this->addModuleScripts( $out->getModuleScripts() );
321 $this->addModuleStyles( $out->getModuleStyles() );
322 $this->addModuleMessages( $out->getModuleMessages() );
323
324 $this->mHeadItems = array_merge( $this->mHeadItems, $out->getHeadItemsArray() );
325 }
326
327 /**
328 * Override the title to be used for display
329 * -- this is assumed to have been validated
330 * (check equal normalisation, etc.)
331 *
332 * @param string $text desired title text
333 */
334 public function setDisplayTitle( $text ) {
335 $this->setTitleText( $text );
336 $this->setProperty( 'displaytitle', $text );
337 }
338
339 /**
340 * Get the title to be used for display
341 *
342 * @return String
343 */
344 public function getDisplayTitle() {
345 $t = $this->getTitleText();
346 if ( $t === '' ) {
347 return false;
348 }
349 return $t;
350 }
351
352 /**
353 * Fairly generic flag setter thingy.
354 */
355 public function setFlag( $flag ) {
356 $this->mFlags[$flag] = true;
357 }
358
359 public function getFlag( $flag ) {
360 return isset( $this->mFlags[$flag] );
361 }
362
363 /**
364 * Set a property to be stored in the page_props database table.
365 *
366 * page_props is a key value store indexed by the page ID. This allows
367 * the parser to set a property on a page which can then be quickly
368 * retrieved given the page ID or via a DB join when given the page
369 * title.
370 *
371 * setProperty() is thus used to propagate properties from the parsed
372 * page to request contexts other than a page view of the currently parsed
373 * article.
374 *
375 * Some applications examples:
376 *
377 * * To implement hidden categories, hiding pages from category listings
378 * by storing a property.
379 *
380 * * Overriding the displayed article title.
381 * @see ParserOutput::setDisplayTitle()
382 *
383 * * To implement image tagging, for example displaying an icon on an
384 * image thumbnail to indicate that it is listed for deletion on
385 * Wikimedia Commons.
386 * This is not actually implemented, yet but would be pretty cool.
387 *
388 * @note: Do not use setProperty() to set a property which is only used
389 * in a context where the ParserOutput object itself is already available,
390 * for example a normal page view. There is no need to save such a property
391 * in the database since it the text is already parsed. You can just hook
392 * OutputPageParserOutput and get your data out of the ParserOutput object.
393 *
394 * If you are writing an extension where you want to set a property in the
395 * parser which is used by an OutputPageParserOutput hook, you have to
396 * associate the extension data directly with the ParserOutput object.
397 * Since MediaWiki 1.21, you can use setExtensionData() to do this:
398 *
399 * @par Example:
400 * @code
401 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
402 * @endcode
403 *
404 * And then later, in OutputPageParserOutput or similar:
405 *
406 * @par Example:
407 * @code
408 * $output->getExtensionData( 'my_ext_foo' );
409 * @endcode
410 *
411 * In MediaWiki 1.20 and older, you have to use a custom member variable
412 * within the ParserOutput object:
413 *
414 * @par Example:
415 * @code
416 * $parser->getOutput()->my_ext_foo = '...';
417 * @endcode
418 *
419 */
420 public function setProperty( $name, $value ) {
421 $this->mProperties[$name] = $value;
422 }
423
424 public function getProperty( $name ) {
425 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
426 }
427
428 public function getProperties() {
429 if ( !isset( $this->mProperties ) ) {
430 $this->mProperties = array();
431 }
432 return $this->mProperties;
433 }
434
435 /**
436 * Returns the options from its ParserOptions which have been taken
437 * into account to produce this output or false if not available.
438 * @return mixed Array
439 */
440 public function getUsedOptions() {
441 if ( !isset( $this->mAccessedOptions ) ) {
442 return array();
443 }
444 return array_keys( $this->mAccessedOptions );
445 }
446
447 /**
448 * Callback passed by the Parser to the ParserOptions to keep track of which options are used.
449 * @access private
450 */
451 function recordOption( $option ) {
452 $this->mAccessedOptions[$option] = true;
453 }
454
455 /**
456 * Adds an update job to the output. Any update jobs added to the output will eventually bexecuted in order to
457 * store any secondary information extracted from the page's content.
458 *
459 * @since 1.20
460 *
461 * @param DataUpdate $update
462 */
463 public function addSecondaryDataUpdate( DataUpdate $update ) {
464 $this->mSecondaryDataUpdates[] = $update;
465 }
466
467 /**
468 * Returns any DataUpdate jobs to be executed in order to store secondary information
469 * extracted from the page's content, including a LinksUpdate object for all links stored in
470 * this ParserOutput object.
471 *
472 * @note: Avoid using this method directly, use ContentHandler::getSecondaryDataUpdates() instead! The content
473 * handler may provide additional update objects.
474 *
475 * @since 1.20
476 *
477 * @param $title Title The title of the page we're updating. If not given, a title object will be created
478 * based on $this->getTitleText()
479 * @param $recursive Boolean: queue jobs for recursive updates?
480 *
481 * @return Array. An array of instances of DataUpdate
482 */
483 public function getSecondaryDataUpdates( Title $title = null, $recursive = true ) {
484 if ( is_null( $title ) ) {
485 $title = Title::newFromText( $this->getTitleText() );
486 }
487
488 $linksUpdate = new LinksUpdate( $title, $this, $recursive );
489
490 return array_merge( $this->mSecondaryDataUpdates, array( $linksUpdate ) );
491 }
492
493 /**
494 * Attaches arbitrary data to this ParserObject. This can be used to store some information in
495 * the ParserOutput object for later use during page output. The data will be cached along with
496 * the ParserOutput object, but unlike data set using setProperty(), it is not recorded in the
497 * database.
498 *
499 * This method is provided to overcome the unsafe practice of attaching extra information to a
500 * ParserObject by directly assigning member variables.
501 *
502 * To use setExtensionData() to pass extension information from a hook inside the parser to a
503 * hook in the page output, use this in the parser hook:
504 *
505 * @par Example:
506 * @code
507 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
508 * @endcode
509 *
510 * And then later, in OutputPageParserOutput or similar:
511 *
512 * @par Example:
513 * @code
514 * $output->getExtensionData( 'my_ext_foo' );
515 * @endcode
516 *
517 * In MediaWiki 1.20 and older, you have to use a custom member variable
518 * within the ParserOutput object:
519 *
520 * @par Example:
521 * @code
522 * $parser->getOutput()->my_ext_foo = '...';
523 * @endcode
524 *
525 * @since 1.21
526 *
527 * @param string $key The key for accessing the data. Extensions should take care to avoid
528 * conflicts in naming keys. It is suggested to use the extension's name as a
529 * prefix.
530 *
531 * @param mixed $value The value to set. Setting a value to null is equivalent to removing
532 * the value.
533 */
534 public function setExtensionData( $key, $value ) {
535 if ( $value === null ) {
536 unset( $this->mExtensionData[$key] );
537 } else {
538 $this->mExtensionData[$key] = $value;
539 }
540 }
541
542 /**
543 * Gets extensions data previously attached to this ParserOutput using setExtensionData().
544 * Typically, such data would be set while parsing the page, e.g. by a parser function.
545 *
546 * @since 1.21
547 *
548 * @param string $key The key to look up.
549 *
550 * @return mixed The value previously set for the given key using setExtensionData( $key ),
551 * or null if no value was set for this key.
552 */
553 public function getExtensionData( $key ) {
554 if ( isset( $this->mExtensionData[$key] ) ) {
555 return $this->mExtensionData[$key];
556 }
557
558 return null;
559 }
560
561 }