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 public $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()
59 private $mPreventClickjacking = false; # Whether to emit X-Frame-Options: DENY
61 const EDITSECTION_REGEX
=
62 '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
64 public function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
65 $containsOldMagic = false, $titletext = ''
68 $this->mLanguageLinks
= $languageLinks;
69 $this->mCategories
= $categoryLinks;
70 $this->mContainsOldMagic
= $containsOldMagic;
71 $this->mTitleText
= $titletext;
74 public function getText() {
75 wfProfileIn( __METHOD__
);
77 if ( $this->mEditSectionTokens
) {
78 $text = preg_replace_callback(
79 ParserOutput
::EDITSECTION_REGEX
,
81 global $wgOut, $wgLang;
82 $editsectionPage = Title
::newFromText( htmlspecialchars_decode( $m[1] ) );
83 $editsectionSection = htmlspecialchars_decode( $m[2] );
84 $editsectionContent = isset( $m[4] ) ?
$m[3] : null;
86 if ( !is_object( $editsectionPage ) ) {
87 throw new MWException( "Bad parser output text." );
90 $skin = $wgOut->getSkin();
91 return call_user_func_array(
92 array( $skin, 'doEditSectionLink' ),
93 array( $editsectionPage, $editsectionSection,
94 $editsectionContent, $wgLang->getCode() )
100 $text = preg_replace( ParserOutput
::EDITSECTION_REGEX
, '', $text );
103 // If you have an old cached version of this class - sorry, you can't disable the TOC
104 if ( isset( $this->mTOCEnabled
) && $this->mTOCEnabled
) {
105 $text = str_replace( array( Parser
::TOC_START
, Parser
::TOC_END
), '', $text );
107 $text = preg_replace(
108 '#' . preg_quote( Parser
::TOC_START
) . '.*?' . preg_quote( Parser
::TOC_END
) . '#s',
113 wfProfileOut( __METHOD__
);
117 public function &getLanguageLinks() {
118 return $this->mLanguageLinks
;
121 public function getInterwikiLinks() {
122 return $this->mInterwikiLinks
;
125 public function getCategoryLinks() {
126 return array_keys( $this->mCategories
);
129 public function &getCategories() {
130 return $this->mCategories
;
133 public function getTitleText() {
134 return $this->mTitleText
;
137 public function getSections() {
138 return $this->mSections
;
141 public function getEditSectionTokens() {
142 return $this->mEditSectionTokens
;
145 public function &getLinks() {
146 return $this->mLinks
;
149 public function &getTemplates() {
150 return $this->mTemplates
;
153 public function &getTemplateIds() {
154 return $this->mTemplateIds
;
157 public function &getImages() {
158 return $this->mImages
;
161 public function &getFileSearchOptions() {
162 return $this->mFileSearchOptions
;
165 public function &getExternalLinks() {
166 return $this->mExternalLinks
;
169 public function getNoGallery() {
170 return $this->mNoGallery
;
173 public function getHeadItems() {
174 return $this->mHeadItems
;
177 public function getModules() {
178 return $this->mModules
;
181 public function getModuleScripts() {
182 return $this->mModuleScripts
;
185 public function getModuleStyles() {
186 return $this->mModuleStyles
;
189 public function getModuleMessages() {
190 return $this->mModuleMessages
;
194 public function getJsConfigVars() {
195 return $this->mJsConfigVars
;
198 public function getOutputHooks() {
199 return (array)$this->mOutputHooks
;
202 public function getWarnings() {
203 return array_keys( $this->mWarnings
);
206 public function getIndexPolicy() {
207 return $this->mIndexPolicy
;
210 public function getTOCHTML() {
211 return $this->mTOCHTML
;
214 public function getTimestamp() {
215 return $this->mTimestamp
;
218 public function getLimitReportData() {
219 return $this->mLimitReportData
;
222 public function getTOCEnabled() {
223 return $this->mTOCEnabled
;
226 public function setText( $text ) {
227 return wfSetVar( $this->mText
, $text );
230 public function setLanguageLinks( $ll ) {
231 return wfSetVar( $this->mLanguageLinks
, $ll );
234 public function setCategoryLinks( $cl ) {
235 return wfSetVar( $this->mCategories
, $cl );
238 public function setTitleText( $t ) {
239 return wfSetVar( $this->mTitleText
, $t );
242 public function setSections( $toc ) {
243 return wfSetVar( $this->mSections
, $toc );
246 public function setEditSectionTokens( $t ) {
247 return wfSetVar( $this->mEditSectionTokens
, $t );
250 public function setIndexPolicy( $policy ) {
251 return wfSetVar( $this->mIndexPolicy
, $policy );
254 public function setTOCHTML( $tochtml ) {
255 return wfSetVar( $this->mTOCHTML
, $tochtml );
258 public function setTimestamp( $timestamp ) {
259 return wfSetVar( $this->mTimestamp
, $timestamp );
262 public function setTOCEnabled( $flag ) {
263 return wfSetVar( $this->mTOCEnabled
, $flag );
266 public function addCategory( $c, $sort ) {
267 $this->mCategories
[$c] = $sort;
270 public function addLanguageLink( $t ) {
271 $this->mLanguageLinks
[] = $t;
274 public function addWarning( $s ) {
275 $this->mWarnings
[$s] = 1;
278 public function addOutputHook( $hook, $data = false ) {
279 $this->mOutputHooks
[] = array( $hook, $data );
282 public function setNewSection( $value ) {
283 $this->mNewSection
= (bool)$value;
285 public function hideNewSection( $value ) {
286 $this->mHideNewSection
= (bool)$value;
288 public function getHideNewSection() {
289 return (bool)$this->mHideNewSection
;
291 public function getNewSection() {
292 return (bool)$this->mNewSection
;
296 * Checks, if a url is pointing to the own server
298 * @param string $internal The server to check against
299 * @param string $url The url to check
302 public static function isLinkInternal( $internal, $url ) {
303 return (bool)preg_match( '/^' .
304 # If server is proto relative, check also for http/https links
305 ( substr( $internal, 0, 2 ) === '//' ?
'(?:https?:)?' : '' ) .
306 preg_quote( $internal, '/' ) .
307 # check for query/path/anchor or end of link in each case
313 public function addExternalLink( $url ) {
314 # We don't register links pointing to our own server, unless... :-)
315 global $wgServer, $wgRegisterInternalExternals;
317 $registerExternalLink = true;
318 if ( !$wgRegisterInternalExternals ) {
319 $registerExternalLink = !self
::isLinkInternal( $wgServer, $url );
321 if ( $registerExternalLink ) {
322 $this->mExternalLinks
[$url] = 1;
327 * Record a local or interwiki inline link for saving in future link tables.
329 * @param Title $title
330 * @param int|null $id Optional known page_id so we can skip the lookup
332 public function addLink( Title
$title, $id = null ) {
333 if ( $title->isExternal() ) {
334 // Don't record interwikis in pagelinks
335 $this->addInterwikiLink( $title );
338 $ns = $title->getNamespace();
339 $dbk = $title->getDBkey();
340 if ( $ns == NS_MEDIA
) {
341 // Normalize this pseudo-alias if it makes it down here...
343 } elseif ( $ns == NS_SPECIAL
) {
344 // We don't record Special: links currently
345 // It might actually be wise to, but we'd need to do some normalization.
347 } elseif ( $dbk === '' ) {
348 // Don't record self links - [[#Foo]]
351 if ( !isset( $this->mLinks
[$ns] ) ) {
352 $this->mLinks
[$ns] = array();
354 if ( is_null( $id ) ) {
355 $id = $title->getArticleID();
357 $this->mLinks
[$ns][$dbk] = $id;
361 * Register a file dependency for this output
362 * @param string $name Title dbKey
363 * @param string $timestamp MW timestamp of file creation (or false if non-existing)
364 * @param string $sha1 Base 36 SHA-1 of file (or false if non-existing)
367 public function addImage( $name, $timestamp = null, $sha1 = null ) {
368 $this->mImages
[$name] = 1;
369 if ( $timestamp !== null && $sha1 !== null ) {
370 $this->mFileSearchOptions
[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
375 * Register a template dependency for this output
376 * @param Title $title
377 * @param int $page_id
381 public function addTemplate( $title, $page_id, $rev_id ) {
382 $ns = $title->getNamespace();
383 $dbk = $title->getDBkey();
384 if ( !isset( $this->mTemplates
[$ns] ) ) {
385 $this->mTemplates
[$ns] = array();
387 $this->mTemplates
[$ns][$dbk] = $page_id;
388 if ( !isset( $this->mTemplateIds
[$ns] ) ) {
389 $this->mTemplateIds
[$ns] = array();
391 $this->mTemplateIds
[$ns][$dbk] = $rev_id; // For versioning
395 * @param Title $title Title object, must be an interwiki link
396 * @throws MWException If given invalid input
398 public function addInterwikiLink( $title ) {
399 if ( !$title->isExternal() ) {
400 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
402 $prefix = $title->getInterwiki();
403 if ( !isset( $this->mInterwikiLinks
[$prefix] ) ) {
404 $this->mInterwikiLinks
[$prefix] = array();
406 $this->mInterwikiLinks
[$prefix][$title->getDBkey()] = 1;
410 * Add some text to the "<head>".
411 * If $tag is set, the section with that tag will only be included once
413 * @param string $section
414 * @param string|bool $tag
416 public function addHeadItem( $section, $tag = false ) {
417 if ( $tag !== false ) {
418 $this->mHeadItems
[$tag] = $section;
420 $this->mHeadItems
[] = $section;
424 public function addModules( $modules ) {
425 $this->mModules
= array_merge( $this->mModules
, (array)$modules );
428 public function addModuleScripts( $modules ) {
429 $this->mModuleScripts
= array_merge( $this->mModuleScripts
, (array)$modules );
432 public function addModuleStyles( $modules ) {
433 $this->mModuleStyles
= array_merge( $this->mModuleStyles
, (array)$modules );
436 public function addModuleMessages( $modules ) {
437 $this->mModuleMessages
= array_merge( $this->mModuleMessages
, (array)$modules );
441 * Add one or more variables to be set in mw.config in JavaScript.
443 * @param string|array $keys Key or array of key/value pairs.
444 * @param mixed $value [optional] Value of the configuration variable.
447 public function addJsConfigVars( $keys, $value = null ) {
448 if ( is_array( $keys ) ) {
449 foreach ( $keys as $key => $value ) {
450 $this->mJsConfigVars
[$key] = $value;
455 $this->mJsConfigVars
[$keys] = $value;
459 * Copy items from the OutputPage object into this one
461 * @param OutputPage $out
463 public function addOutputPageMetadata( OutputPage
$out ) {
464 $this->addModules( $out->getModules() );
465 $this->addModuleScripts( $out->getModuleScripts() );
466 $this->addModuleStyles( $out->getModuleStyles() );
467 $this->addModuleMessages( $out->getModuleMessages() );
468 $this->addJsConfigVars( $out->getJsConfigVars() );
470 $this->mHeadItems
= array_merge( $this->mHeadItems
, $out->getHeadItemsArray() );
471 $this->mPreventClickjacking
= $this->mPreventClickjacking ||
$out->getPreventClickjacking();
475 * Override the title to be used for display
476 * -- this is assumed to have been validated
477 * (check equal normalisation, etc.)
479 * @param string $text Desired title text
481 public function setDisplayTitle( $text ) {
482 $this->setTitleText( $text );
483 $this->setProperty( 'displaytitle', $text );
487 * Get the title to be used for display
491 public function getDisplayTitle() {
492 $t = $this->getTitleText();
500 * Fairly generic flag setter thingy.
501 * @param string $flag
503 public function setFlag( $flag ) {
504 $this->mFlags
[$flag] = true;
507 public function getFlag( $flag ) {
508 return isset( $this->mFlags
[$flag] );
512 * Set a property to be stored in the page_props database table.
514 * page_props is a key value store indexed by the page ID. This allows
515 * the parser to set a property on a page which can then be quickly
516 * retrieved given the page ID or via a DB join when given the page
519 * Since 1.23, page_props are also indexed by numeric value, to allow
520 * for efficient "top k" queries of pages wrt a given property.
522 * setProperty() is thus used to propagate properties from the parsed
523 * page to request contexts other than a page view of the currently parsed
526 * Some applications examples:
528 * * To implement hidden categories, hiding pages from category listings
529 * by storing a property.
531 * * Overriding the displayed article title.
532 * @see ParserOutput::setDisplayTitle()
534 * * To implement image tagging, for example displaying an icon on an
535 * image thumbnail to indicate that it is listed for deletion on
537 * This is not actually implemented, yet but would be pretty cool.
539 * @note Do not use setProperty() to set a property which is only used
540 * in a context where the ParserOutput object itself is already available,
541 * for example a normal page view. There is no need to save such a property
542 * in the database since the text is already parsed. You can just hook
543 * OutputPageParserOutput and get your data out of the ParserOutput object.
545 * If you are writing an extension where you want to set a property in the
546 * parser which is used by an OutputPageParserOutput hook, you have to
547 * associate the extension data directly with the ParserOutput object.
548 * Since MediaWiki 1.21, you can use setExtensionData() to do this:
552 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
555 * And then later, in OutputPageParserOutput or similar:
559 * $output->getExtensionData( 'my_ext_foo' );
562 * In MediaWiki 1.20 and older, you have to use a custom member variable
563 * within the ParserOutput object:
567 * $parser->getOutput()->my_ext_foo = '...';
571 public function setProperty( $name, $value ) {
572 $this->mProperties
[$name] = $value;
576 * @param string $name The property name to look up.
578 * @return mixed|bool The value previously set using setProperty(). False if null or no value
579 * was set for the given property name.
581 * @note You need to use getProperties() to check for boolean and null properties.
583 public function getProperty( $name ) {
584 return isset( $this->mProperties
[$name] ) ?
$this->mProperties
[$name] : false;
587 public function unsetProperty( $name ) {
588 unset( $this->mProperties
[$name] );
591 public function getProperties() {
592 if ( !isset( $this->mProperties
) ) {
593 $this->mProperties
= array();
595 return $this->mProperties
;
599 * Returns the options from its ParserOptions which have been taken
600 * into account to produce this output or false if not available.
603 public function getUsedOptions() {
604 if ( !isset( $this->mAccessedOptions
) ) {
607 return array_keys( $this->mAccessedOptions
);
611 * Tags a parser option for use in the cache key for this parser output.
612 * Registered as a watcher at ParserOptions::registerWatcher() by Parser::clearState().
614 * @see ParserCache::getKey
615 * @see ParserCache::save
616 * @see ParserOptions::addExtraKey
617 * @see ParserOptions::optionsHash
618 * @param string $option
620 public function recordOption( $option ) {
621 $this->mAccessedOptions
[$option] = true;
625 * Adds an update job to the output. Any update jobs added to the output will
626 * eventually be executed in order to store any secondary information extracted
627 * from the page's content. This is triggered by calling getSecondaryDataUpdates()
628 * and is used for forward links updates on edit and backlink updates by jobs.
632 * @param DataUpdate $update
634 public function addSecondaryDataUpdate( DataUpdate
$update ) {
635 $this->mSecondaryDataUpdates
[] = $update;
639 * Returns any DataUpdate jobs to be executed in order to store secondary information
640 * extracted from the page's content, including a LinksUpdate object for all links stored in
641 * this ParserOutput object.
643 * @note Avoid using this method directly, use ContentHandler::getSecondaryDataUpdates()
644 * instead! The content handler may provide additional update objects.
648 * @param Title $title The title of the page we're updating. If not given, a title object will
649 * be created based on $this->getTitleText()
650 * @param bool $recursive Queue jobs for recursive updates?
652 * @return array An array of instances of DataUpdate
654 public function getSecondaryDataUpdates( Title
$title = null, $recursive = true ) {
655 if ( is_null( $title ) ) {
656 $title = Title
::newFromText( $this->getTitleText() );
659 $linksUpdate = new LinksUpdate( $title, $this, $recursive );
661 return array_merge( $this->mSecondaryDataUpdates
, array( $linksUpdate ) );
665 * Attaches arbitrary data to this ParserObject. This can be used to store some information in
666 * the ParserOutput object for later use during page output. The data will be cached along with
667 * the ParserOutput object, but unlike data set using setProperty(), it is not recorded in the
670 * This method is provided to overcome the unsafe practice of attaching extra information to a
671 * ParserObject by directly assigning member variables.
673 * To use setExtensionData() to pass extension information from a hook inside the parser to a
674 * hook in the page output, use this in the parser hook:
678 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
681 * And then later, in OutputPageParserOutput or similar:
685 * $output->getExtensionData( 'my_ext_foo' );
688 * In MediaWiki 1.20 and older, you have to use a custom member variable
689 * within the ParserOutput object:
693 * $parser->getOutput()->my_ext_foo = '...';
698 * @param string $key The key for accessing the data. Extensions should take care to avoid
699 * conflicts in naming keys. It is suggested to use the extension's name as a prefix.
701 * @param mixed $value The value to set. Setting a value to null is equivalent to removing
704 public function setExtensionData( $key, $value ) {
705 if ( $value === null ) {
706 unset( $this->mExtensionData
[$key] );
708 $this->mExtensionData
[$key] = $value;
713 * Gets extensions data previously attached to this ParserOutput using setExtensionData().
714 * Typically, such data would be set while parsing the page, e.g. by a parser function.
718 * @param string $key The key to look up.
720 * @return mixed|null The value previously set for the given key using setExtensionData()
721 * or null if no value was set for this key.
723 public function getExtensionData( $key ) {
724 if ( isset( $this->mExtensionData
[$key] ) ) {
725 return $this->mExtensionData
[$key];
731 private static function getTimes( $clock = null ) {
733 if ( !$clock ||
$clock === 'wall' ) {
734 $ret['wall'] = microtime( true );
736 if ( ( !$clock ||
$clock === 'cpu' ) && function_exists( 'getrusage' ) ) {
738 $ret['cpu'] = $ru['ru_utime.tv_sec'] +
$ru['ru_utime.tv_usec'] / 1e6
;
739 $ret['cpu'] +
= $ru['ru_stime.tv_sec'] +
$ru['ru_stime.tv_usec'] / 1e6
;
745 * Resets the parse start timestamps for future calls to getTimeSinceStart()
748 public function resetParseStartTime() {
749 $this->mParseStartTime
= self
::getTimes();
753 * Returns the time since resetParseStartTime() was last called
755 * Clocks available are:
756 * - wall: Wall clock time
757 * - cpu: CPU time (requires getrusage)
760 * @param string $clock
763 public function getTimeSinceStart( $clock ) {
764 if ( !isset( $this->mParseStartTime
[$clock] ) ) {
768 $end = self
::getTimes( $clock );
769 return $end[$clock] - $this->mParseStartTime
[$clock];
773 * Sets parser limit report data for a key
775 * The key is used as the prefix for various messages used for formatting:
776 * - $key: The label for the field in the limit report
777 * - $key-value-text: Message used to format the value in the "NewPP limit
778 * report" HTML comment. If missing, uses $key-format.
779 * - $key-value-html: Message used to format the value in the preview
780 * limit report table. If missing, uses $key-format.
781 * - $key-value: Message used to format the value. If missing, uses "$1".
783 * Note that all values are interpreted as wikitext, and so should be
784 * encoded with htmlspecialchars() as necessary, but should avoid complex
785 * HTML for sanity of display in the "NewPP limit report" comment.
788 * @param string $key Message key
789 * @param mixed $value Appropriate for Message::params()
791 public function setLimitReportData( $key, $value ) {
792 $this->mLimitReportData
[$key] = $value;
796 * Get or set the prevent-clickjacking flag
799 * @param bool|null $flag New flag value, or null to leave it unchanged
800 * @return bool Old flag value
802 public function preventClickjacking( $flag = null ) {
803 return wfSetVar( $this->mPreventClickjacking
, $flag );
807 * Save space for for serialization by removing useless values
810 public function __sleep() {
812 array_keys( get_object_vars( $this ) ),
813 array( 'mSecondaryDataUpdates', 'mParseStartTime' )