4 * Output of the PHP parser.
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.
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.
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
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 $mJsConfigVars = array(), # JavaScript config variable for mw.config combined with this page
45 $mOutputHooks = array(), # Hook tags as per $wgParserOutputHooks
46 $mWarnings = array(), # Warning text to be returned to the user. Wikitext formatted, in the key only
47 $mSections = array(), # Table of contents
48 $mEditSectionTokens = false, # prefix/suffix markers if edit sections were output as tokens
49 $mProperties = array(), # Name/value pairs to be cached in the DB
50 $mTOCHTML = '', # HTML of the TOC
51 $mTimestamp, # Timestamp of the revision
52 $mTOCEnabled = true; # Whether TOC should be shown, can't override __NOTOC__
53 private $mIndexPolicy = ''; # 'index' or 'noindex'? Any other value will result in no change.
54 private $mAccessedOptions = array(); # List of ParserOptions (stored in the keys)
55 private $mSecondaryDataUpdates = array(); # List of DataUpdate, used to save info from the page somewhere else.
56 private $mExtensionData = array(); # extra data used by extensions
57 private $mLimitReportData = array(); # Parser limit report data
58 private $mParseStartTime = array(); # Timestamps for getTimeSinceStart()
60 const EDITSECTION_REGEX
= '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
62 function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
63 $containsOldMagic = false, $titletext = ''
66 $this->mLanguageLinks
= $languageLinks;
67 $this->mCategories
= $categoryLinks;
68 $this->mContainsOldMagic
= $containsOldMagic;
69 $this->mTitleText
= $titletext;
73 wfProfileIn( __METHOD__
);
75 if ( $this->mEditSectionTokens
) {
76 $text = preg_replace_callback( ParserOutput
::EDITSECTION_REGEX
,
77 array( &$this, 'replaceEditSectionLinksCallback' ), $text );
79 $text = preg_replace( ParserOutput
::EDITSECTION_REGEX
, '', $text );
82 // If you have an old cached version of this class - sorry, you can't disable the TOC
83 if ( isset( $this->mTOCEnabled
) && $this->mTOCEnabled
) {
84 $text = str_replace( array( Parser
::TOC_START
, Parser
::TOC_END
), '', $text );
87 '#' . preg_quote( Parser
::TOC_START
) . '.*?' . preg_quote( Parser
::TOC_END
) . '#s',
92 wfProfileOut( __METHOD__
);
97 * callback used by getText to replace editsection tokens
100 * @throws MWException
103 function replaceEditSectionLinksCallback( $m ) {
104 global $wgOut, $wgLang;
106 htmlspecialchars_decode( $m[1] ),
107 htmlspecialchars_decode( $m[2] ),
108 isset( $m[4] ) ?
$m[3] : null,
110 $args[0] = Title
::newFromText( $args[0] );
111 if ( !is_object( $args[0] ) ) {
112 throw new MWException( "Bad parser output text." );
114 $args[] = $wgLang->getCode();
115 $skin = $wgOut->getSkin();
116 return call_user_func_array( array( $skin, 'doEditSectionLink' ), $args );
119 function &getLanguageLinks() { return $this->mLanguageLinks
; }
120 function getInterwikiLinks() { return $this->mInterwikiLinks
; }
121 function getCategoryLinks() { return array_keys( $this->mCategories
); }
122 function &getCategories() { return $this->mCategories
; }
123 function getTitleText() { return $this->mTitleText
; }
124 function getSections() { return $this->mSections
; }
125 function getEditSectionTokens() { return $this->mEditSectionTokens
; }
126 function &getLinks() { return $this->mLinks
; }
127 function &getTemplates() { return $this->mTemplates
; }
128 function &getTemplateIds() { return $this->mTemplateIds
; }
129 function &getImages() { return $this->mImages
; }
130 function &getFileSearchOptions() { return $this->mFileSearchOptions
; }
131 function &getExternalLinks() { return $this->mExternalLinks
; }
132 function getNoGallery() { return $this->mNoGallery
; }
133 function getHeadItems() { return $this->mHeadItems
; }
134 function getModules() { return $this->mModules
; }
135 function getModuleScripts() { return $this->mModuleScripts
; }
136 function getModuleStyles() { return $this->mModuleStyles
; }
137 function getModuleMessages() { return $this->mModuleMessages
; }
139 function getJsConfigVars() { return $this->mJsConfigVars
; }
140 function getOutputHooks() { return (array)$this->mOutputHooks
; }
141 function getWarnings() { return array_keys( $this->mWarnings
); }
142 function getIndexPolicy() { return $this->mIndexPolicy
; }
143 function getTOCHTML() { return $this->mTOCHTML
; }
144 function getTimestamp() { return $this->mTimestamp
; }
145 function getLimitReportData() { return $this->mLimitReportData
; }
146 function getTOCEnabled() { return $this->mTOCEnabled
; }
148 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
149 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
150 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories
, $cl ); }
152 function setTitleText( $t ) { return wfSetVar( $this->mTitleText
, $t ); }
153 function setSections( $toc ) { return wfSetVar( $this->mSections
, $toc ); }
154 function setEditSectionTokens( $t ) { return wfSetVar( $this->mEditSectionTokens
, $t ); }
155 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy
, $policy ); }
156 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML
, $tochtml ); }
157 function setTimestamp( $timestamp ) { return wfSetVar( $this->mTimestamp
, $timestamp ); }
158 function setTOCEnabled( $flag ) { return wfSetVar( $this->mTOCEnabled
, $flag ); }
160 function addCategory( $c, $sort ) { $this->mCategories
[$c] = $sort; }
161 function addLanguageLink( $t ) { $this->mLanguageLinks
[] = $t; }
162 function addWarning( $s ) { $this->mWarnings
[$s] = 1; }
164 function addOutputHook( $hook, $data = false ) {
165 $this->mOutputHooks
[] = array( $hook, $data );
168 function setNewSection( $value ) {
169 $this->mNewSection
= (bool)$value;
171 function hideNewSection( $value ) {
172 $this->mHideNewSection
= (bool)$value;
174 function getHideNewSection() {
175 return (bool)$this->mHideNewSection
;
177 function getNewSection() {
178 return (bool)$this->mNewSection
;
182 * Checks, if a url is pointing to the own server
184 * @param string $internal the server to check against
185 * @param string $url the url to check
188 static function isLinkInternal( $internal, $url ) {
189 return (bool)preg_match( '/^' .
190 # If server is proto relative, check also for http/https links
191 ( substr( $internal, 0, 2 ) === '//' ?
'(?:https?:)?' : '' ) .
192 preg_quote( $internal, '/' ) .
193 # check for query/path/anchor or end of link in each case
199 function addExternalLink( $url ) {
200 # We don't register links pointing to our own server, unless... :-)
201 global $wgServer, $wgRegisterInternalExternals;
203 $registerExternalLink = true;
204 if ( !$wgRegisterInternalExternals ) {
205 $registerExternalLink = !self
::isLinkInternal( $wgServer, $url );
207 if ( $registerExternalLink ) {
208 $this->mExternalLinks
[$url] = 1;
213 * Record a local or interwiki inline link for saving in future link tables.
215 * @param $title Title object
216 * @param $id Mixed: optional known page_id so we can skip the lookup
218 function addLink( Title
$title, $id = null ) {
219 if ( $title->isExternal() ) {
220 // Don't record interwikis in pagelinks
221 $this->addInterwikiLink( $title );
224 $ns = $title->getNamespace();
225 $dbk = $title->getDBkey();
226 if ( $ns == NS_MEDIA
) {
227 // Normalize this pseudo-alias if it makes it down here...
229 } elseif ( $ns == NS_SPECIAL
) {
230 // We don't record Special: links currently
231 // It might actually be wise to, but we'd need to do some normalization.
233 } elseif ( $dbk === '' ) {
234 // Don't record self links - [[#Foo]]
237 if ( !isset( $this->mLinks
[$ns] ) ) {
238 $this->mLinks
[$ns] = array();
240 if ( is_null( $id ) ) {
241 $id = $title->getArticleID();
243 $this->mLinks
[$ns][$dbk] = $id;
247 * Register a file dependency for this output
248 * @param string $name Title dbKey
249 * @param string $timestamp MW timestamp of file creation (or false if non-existing)
250 * @param string $sha1 base 36 SHA-1 of file (or false if non-existing)
253 function addImage( $name, $timestamp = null, $sha1 = null ) {
254 $this->mImages
[$name] = 1;
255 if ( $timestamp !== null && $sha1 !== null ) {
256 $this->mFileSearchOptions
[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
261 * Register a template dependency for this output
262 * @param $title Title
267 function addTemplate( $title, $page_id, $rev_id ) {
268 $ns = $title->getNamespace();
269 $dbk = $title->getDBkey();
270 if ( !isset( $this->mTemplates
[$ns] ) ) {
271 $this->mTemplates
[$ns] = array();
273 $this->mTemplates
[$ns][$dbk] = $page_id;
274 if ( !isset( $this->mTemplateIds
[$ns] ) ) {
275 $this->mTemplateIds
[$ns] = array();
277 $this->mTemplateIds
[$ns][$dbk] = $rev_id; // For versioning
281 * @param $title Title object, must be an interwiki link
282 * @throws MWException if given invalid input
284 function addInterwikiLink( $title ) {
285 if ( !$title->isExternal() ) {
286 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
288 $prefix = $title->getInterwiki();
289 if ( !isset( $this->mInterwikiLinks
[$prefix] ) ) {
290 $this->mInterwikiLinks
[$prefix] = array();
292 $this->mInterwikiLinks
[$prefix][$title->getDBkey()] = 1;
296 * Add some text to the "<head>".
297 * If $tag is set, the section with that tag will only be included once
300 function addHeadItem( $section, $tag = false ) {
301 if ( $tag !== false ) {
302 $this->mHeadItems
[$tag] = $section;
304 $this->mHeadItems
[] = $section;
308 public function addModules( $modules ) {
309 $this->mModules
= array_merge( $this->mModules
, (array)$modules );
312 public function addModuleScripts( $modules ) {
313 $this->mModuleScripts
= array_merge( $this->mModuleScripts
, (array)$modules );
316 public function addModuleStyles( $modules ) {
317 $this->mModuleStyles
= array_merge( $this->mModuleStyles
, (array)$modules );
320 public function addModuleMessages( $modules ) {
321 $this->mModuleMessages
= array_merge( $this->mModuleMessages
, (array)$modules );
325 * Add one or more variables to be set in mw.config in JavaScript.
327 * @param $keys {String|Array} Key or array of key/value pairs.
328 * @param $value {Mixed} [optional] Value of the configuration variable.
331 public function addJsConfigVars( $keys, $value = null ) {
332 if ( is_array( $keys ) ) {
333 foreach ( $keys as $key => $value ) {
334 $this->mJsConfigVars
[$key] = $value;
339 $this->mJsConfigVars
[$keys] = $value;
343 * Copy items from the OutputPage object into this one
345 * @param $out OutputPage object
347 public function addOutputPageMetadata( OutputPage
$out ) {
348 $this->addModules( $out->getModules() );
349 $this->addModuleScripts( $out->getModuleScripts() );
350 $this->addModuleStyles( $out->getModuleStyles() );
351 $this->addModuleMessages( $out->getModuleMessages() );
352 $this->addJsConfigVars( $out->getJsConfigVars() );
354 $this->mHeadItems
= array_merge( $this->mHeadItems
, $out->getHeadItemsArray() );
358 * Override the title to be used for display
359 * -- this is assumed to have been validated
360 * (check equal normalisation, etc.)
362 * @param string $text desired title text
364 public function setDisplayTitle( $text ) {
365 $this->setTitleText( $text );
366 $this->setProperty( 'displaytitle', $text );
370 * Get the title to be used for display
374 public function getDisplayTitle() {
375 $t = $this->getTitleText();
383 * Fairly generic flag setter thingy.
385 public function setFlag( $flag ) {
386 $this->mFlags
[$flag] = true;
389 public function getFlag( $flag ) {
390 return isset( $this->mFlags
[$flag] );
394 * Set a property to be stored in the page_props database table.
396 * page_props is a key value store indexed by the page ID. This allows
397 * the parser to set a property on a page which can then be quickly
398 * retrieved given the page ID or via a DB join when given the page
401 * setProperty() is thus used to propagate properties from the parsed
402 * page to request contexts other than a page view of the currently parsed
405 * Some applications examples:
407 * * To implement hidden categories, hiding pages from category listings
408 * by storing a property.
410 * * Overriding the displayed article title.
411 * @see ParserOutput::setDisplayTitle()
413 * * To implement image tagging, for example displaying an icon on an
414 * image thumbnail to indicate that it is listed for deletion on
416 * This is not actually implemented, yet but would be pretty cool.
418 * @note: Do not use setProperty() to set a property which is only used
419 * in a context where the ParserOutput object itself is already available,
420 * for example a normal page view. There is no need to save such a property
421 * in the database since the text is already parsed. You can just hook
422 * OutputPageParserOutput and get your data out of the ParserOutput object.
424 * If you are writing an extension where you want to set a property in the
425 * parser which is used by an OutputPageParserOutput hook, you have to
426 * associate the extension data directly with the ParserOutput object.
427 * Since MediaWiki 1.21, you can use setExtensionData() to do this:
431 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
434 * And then later, in OutputPageParserOutput or similar:
438 * $output->getExtensionData( 'my_ext_foo' );
441 * In MediaWiki 1.20 and older, you have to use a custom member variable
442 * within the ParserOutput object:
446 * $parser->getOutput()->my_ext_foo = '...';
450 public function setProperty( $name, $value ) {
451 $this->mProperties
[$name] = $value;
454 public function getProperty( $name ) {
455 return isset( $this->mProperties
[$name] ) ?
$this->mProperties
[$name] : false;
458 public function getProperties() {
459 if ( !isset( $this->mProperties
) ) {
460 $this->mProperties
= array();
462 return $this->mProperties
;
466 * Returns the options from its ParserOptions which have been taken
467 * into account to produce this output or false if not available.
468 * @return mixed Array
470 public function getUsedOptions() {
471 if ( !isset( $this->mAccessedOptions
) ) {
474 return array_keys( $this->mAccessedOptions
);
478 * Tags a parser option for use in the cache key for this parser output.
479 * Registered as a watcher at ParserOptions::registerWatcher() by Parser::clearState().
481 * @see ParserCache::getKey
482 * @see ParserCache::save
483 * @see ParserOptions::addExtraKey
484 * @see ParserOptions::optionsHash
486 public function recordOption( $option ) {
487 $this->mAccessedOptions
[$option] = true;
491 * Adds an update job to the output. Any update jobs added to the output will
492 * eventually be executed in order to store any secondary information extracted
493 * from the page's content. This is triggered by calling getSecondaryDataUpdates()
494 * and is used for forward links updates on edit and backlink updates by jobs.
498 * @param DataUpdate $update
500 public function addSecondaryDataUpdate( DataUpdate
$update ) {
501 $this->mSecondaryDataUpdates
[] = $update;
505 * Returns any DataUpdate jobs to be executed in order to store secondary information
506 * extracted from the page's content, including a LinksUpdate object for all links stored in
507 * this ParserOutput object.
509 * @note: Avoid using this method directly, use ContentHandler::getSecondaryDataUpdates() instead! The content
510 * handler may provide additional update objects.
514 * @param $title Title The title of the page we're updating. If not given, a title object will be created
515 * based on $this->getTitleText()
516 * @param $recursive Boolean: queue jobs for recursive updates?
518 * @return Array. An array of instances of DataUpdate
520 public function getSecondaryDataUpdates( Title
$title = null, $recursive = true ) {
521 if ( is_null( $title ) ) {
522 $title = Title
::newFromText( $this->getTitleText() );
525 $linksUpdate = new LinksUpdate( $title, $this, $recursive );
527 return array_merge( $this->mSecondaryDataUpdates
, array( $linksUpdate ) );
531 * Attaches arbitrary data to this ParserObject. This can be used to store some information in
532 * the ParserOutput object for later use during page output. The data will be cached along with
533 * the ParserOutput object, but unlike data set using setProperty(), it is not recorded in the
536 * This method is provided to overcome the unsafe practice of attaching extra information to a
537 * ParserObject by directly assigning member variables.
539 * To use setExtensionData() to pass extension information from a hook inside the parser to a
540 * hook in the page output, use this in the parser hook:
544 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
547 * And then later, in OutputPageParserOutput or similar:
551 * $output->getExtensionData( 'my_ext_foo' );
554 * In MediaWiki 1.20 and older, you have to use a custom member variable
555 * within the ParserOutput object:
559 * $parser->getOutput()->my_ext_foo = '...';
564 * @param string $key The key for accessing the data. Extensions should take care to avoid
565 * conflicts in naming keys. It is suggested to use the extension's name as a
568 * @param mixed $value The value to set. Setting a value to null is equivalent to removing
571 public function setExtensionData( $key, $value ) {
572 if ( $value === null ) {
573 unset( $this->mExtensionData
[$key] );
575 $this->mExtensionData
[$key] = $value;
580 * Gets extensions data previously attached to this ParserOutput using setExtensionData().
581 * Typically, such data would be set while parsing the page, e.g. by a parser function.
585 * @param string $key The key to look up.
587 * @return mixed The value previously set for the given key using setExtensionData( $key ),
588 * or null if no value was set for this key.
590 public function getExtensionData( $key ) {
591 if ( isset( $this->mExtensionData
[$key] ) ) {
592 return $this->mExtensionData
[$key];
598 private static function getTimes( $clock = null ) {
600 if ( !$clock ||
$clock === 'wall' ) {
601 $ret['wall'] = microtime( true );
603 if ( ( !$clock ||
$clock === 'cpu' ) && function_exists( 'getrusage' ) ) {
605 $ret['cpu'] = $ru['ru_utime.tv_sec'] +
$ru['ru_utime.tv_usec'] / 1e6
;
606 $ret['cpu'] +
= $ru['ru_stime.tv_sec'] +
$ru['ru_stime.tv_usec'] / 1e6
;
612 * Resets the parse start timestamps for future calls to getTimeSinceStart()
615 function resetParseStartTime() {
616 $this->mParseStartTime
= self
::getTimes();
620 * Returns the time since resetParseStartTime() was last called
622 * Clocks available are:
623 * - wall: Wall clock time
624 * - cpu: CPU time (requires getrusage)
627 * @param string $clock
630 function getTimeSinceStart( $clock ) {
631 if ( !isset( $this->mParseStartTime
[$clock] ) ) {
635 $end = self
::getTimes( $clock );
636 return $end[$clock] - $this->mParseStartTime
[$clock];
640 * Sets parser limit report data for a key
642 * The key is used as the prefix for various messages used for formatting:
643 * - $key: The label for the field in the limit report
644 * - $key-value-text: Message used to format the value in the "NewPP limit
645 * report" HTML comment. If missing, uses $key-format.
646 * - $key-value-html: Message used to format the value in the preview
647 * limit report table. If missing, uses $key-format.
648 * - $key-value: Message used to format the value. If missing, uses "$1".
650 * Note that all values are interpreted as wikitext, and so should be
651 * encoded with htmlspecialchars() as necessary, but should avoid complex
652 * HTML for sanity of display in the "NewPP limit report" comment.
655 * @param string $key Message key
656 * @param mixed $value Appropriate for Message::params()
658 function setLimitReportData( $key, $value ) {
659 $this->mLimitReportData
[$key] = $value;
663 * Save space for for serialization by removing useless values
667 array_keys( get_object_vars( $this ) ),
668 array( 'mSecondaryDataUpdates', 'mParseStartTime' )