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
25 class ParserOutput
extends CacheTime
{
27 * Feature flags to indicate to extensions that MediaWiki core supports and
28 * uses getText() stateless transforms.
30 const SUPPORTS_STATELESS_TRANSFORMS
= 1;
31 const SUPPORTS_UNWRAP_TRANSFORM
= 1;
34 * @var string|null $mText The output text
39 * @var array $mLanguageLinks List of the full text of language links,
40 * in the order they appear.
42 public $mLanguageLinks;
45 * @var array $mCategories Map of category names to sort keys
50 * @var array $mIndicators Page status indicators, usually displayed in top-right corner.
52 public $mIndicators = [];
55 * @var string $mTitleText Title text of the chosen language variant, as HTML.
60 * @var array $mLinks 2-D map of NS/DBK to ID for the links in the document.
66 * @var array $mTemplates 2-D map of NS/DBK to ID for the template references.
69 public $mTemplates = [];
72 * @var array $mTemplateIds 2-D map of NS/DBK to rev ID for the template references.
75 public $mTemplateIds = [];
78 * @var array $mImages DB keys of the images used, in the array key only
83 * @var array $mFileSearchOptions DB keys of the images used mapped to sha1 and MW timestamp.
85 public $mFileSearchOptions = [];
88 * @var array $mExternalLinks External link URLs, in the key only.
90 public $mExternalLinks = [];
93 * @var array $mInterwikiLinks 2-D map of prefix/DBK (in keys only)
94 * for the inline interwiki links in the document.
96 public $mInterwikiLinks = [];
99 * @var bool $mNewSection Show a new section link?
101 public $mNewSection = false;
104 * @var bool $mHideNewSection Hide the new section link?
106 public $mHideNewSection = false;
109 * @var bool $mNoGallery No gallery on category page? (__NOGALLERY__).
111 public $mNoGallery = false;
114 * @var array $mHeadItems Items to put in the <head> section
116 public $mHeadItems = [];
119 * @var array $mModules Modules to be loaded by ResourceLoader
121 public $mModules = [];
124 * @var array $mModuleStyles Modules of which only the CSSS will be loaded by ResourceLoader.
126 public $mModuleStyles = [];
129 * @var array $mJsConfigVars JavaScript config variable for mw.config combined with this page.
131 public $mJsConfigVars = [];
134 * @var array $mOutputHooks Hook tags as per $wgParserOutputHooks.
136 public $mOutputHooks = [];
139 * @var array $mWarnings Warning text to be returned to the user.
140 * Wikitext formatted, in the key only.
142 public $mWarnings = [];
145 * @var array $mSections Table of contents
147 public $mSections = [];
150 * @var array $mProperties Name/value pairs to be cached in the DB.
152 public $mProperties = [];
155 * @var string $mTOCHTML HTML of the TOC.
157 public $mTOCHTML = '';
160 * @var string $mTimestamp Timestamp of the revision.
165 * @var bool $mEnableOOUI Whether OOUI should be enabled.
167 public $mEnableOOUI = false;
170 * @var string $mIndexPolicy 'index' or 'noindex'? Any other value will result in no change.
172 private $mIndexPolicy = '';
175 * @var true[] $mAccessedOptions List of ParserOptions (stored in the keys).
177 private $mAccessedOptions = [];
180 * @var array $mExtensionData extra data used by extensions.
182 private $mExtensionData = [];
185 * @var array $mLimitReportData Parser limit report data.
187 private $mLimitReportData = [];
189 /** @var array Parser limit report data for JSON */
190 private $mLimitReportJSData = [];
193 * @var array $mParseStartTime Timestamps for getTimeSinceStart().
195 private $mParseStartTime = [];
198 * @var bool $mPreventClickjacking Whether to emit X-Frame-Options: DENY.
200 private $mPreventClickjacking = false;
203 * @var array $mFlags Generic flags.
205 private $mFlags = [];
207 /** @var int|null Assumed rev ID for {{REVISIONID}} if no revision is set */
208 private $mSpeculativeRevId;
210 /** string CSS classes to use for the wrapping div, stored in the array keys.
211 * If no class is given, no wrapper is added.
213 private $mWrapperDivClasses = [];
215 /** @var int Upper bound of expiry based on parse duration */
216 private $mMaxAdaptiveExpiry = INF
;
218 const EDITSECTION_REGEX
=
219 '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#s';
221 // finalizeAdaptiveCacheExpiry() uses TTL = MAX( m * PARSE_TIME + b, MIN_AR_TTL)
222 // Current values imply that m=3933.333333 and b=-333.333333
223 // See https://www.nngroup.com/articles/website-response-times/
224 const PARSE_FAST_SEC
= 0.100; // perceived "fast" page parse
225 const PARSE_SLOW_SEC
= 1.0; // perceived "slow" page parse
226 const FAST_AR_TTL
= 60; // adaptive TTL for "fast" pages
227 const SLOW_AR_TTL
= 3600; // adaptive TTL for "slow" pages
228 const MIN_AR_TTL
= 15; // min adaptive TTL (for sanity, pool counter, and edit stashing)
231 * @param string|null $text HTML. Use null to indicate that this ParserOutput contains only
232 * meta-data, and the HTML output is undetermined, as opposed to empty. Passing null
233 * here causes hasText() to return false.
234 * @param array $languageLinks
235 * @param array $categoryLinks
236 * @param bool $unused
237 * @param string $titletext
239 public function __construct( $text = '', $languageLinks = [], $categoryLinks = [],
240 $unused = false, $titletext = ''
242 $this->mText
= $text;
243 $this->mLanguageLinks
= $languageLinks;
244 $this->mCategories
= $categoryLinks;
245 $this->mTitleText
= $titletext;
249 * Returns true if text was passed to the constructor, or set using setText(). Returns false
250 * if null was passed to the $text parameter of the constructor to indicate that this
251 * ParserOutput only contains meta-data, and the HTML output is undetermined.
255 * @return bool Whether this ParserOutput contains rendered text. If this returns false, the
256 * ParserOutput contains meta-data only.
258 public function hasText() {
259 return ( $this->mText
!== null );
263 * Get the cacheable text with <mw:editsection> markers still in it. The
264 * return value is suitable for writing back via setText() but is not valid
265 * for display to the user.
270 public function getRawText() {
271 if ( $this->mText
=== null ) {
272 throw new LogicException( 'This ParserOutput contains no text!' );
279 * Get the output HTML
281 * @param array $options (since 1.31) Transformations to apply to the HTML
282 * - allowTOC: (bool) Show the TOC, assuming there were enough headings
283 * to generate one and `__NOTOC__` wasn't used. Default is true,
284 * but might be statefully overridden.
285 * - enableSectionEditLinks: (bool) Include section edit links, assuming
286 * section edit link tokens are present in the HTML. Default is true,
287 * but might be statefully overridden.
288 * - unwrap: (bool) Return text without a wrapper div. Default is false,
289 * meaning a wrapper div will be added if getWrapperDivClass() returns
290 * a non-empty string.
291 * - wrapperDivClass: (string) Wrap the output in a div and apply the given
292 * CSS class to that div. This overrides the output of getWrapperDivClass().
293 * Setting this to an empty string has the same effect as 'unwrap' => true.
294 * - deduplicateStyles: (bool) When true, which is the default, `<style>`
295 * tags with the `data-mw-deduplicate` attribute set are deduplicated by
296 * value of the attribute: all but the first will be replaced by `<link
297 * rel="mw-deduplicated-inline-style" href="mw-data:..."/>` tags, where
298 * the scheme-specific-part of the href is the (percent-encoded) value
299 * of the `data-mw-deduplicate` attribute.
300 * @return string HTML
301 * @return-taint escaped
303 public function getText( $options = [] ) {
306 'enableSectionEditLinks' => true,
308 'deduplicateStyles' => true,
309 'wrapperDivClass' => $this->getWrapperDivClass(),
311 $text = $this->getRawText();
313 Hooks
::runWithoutAbort( 'ParserOutputPostCacheTransform', [ $this, &$text, &$options ] );
315 if ( $options['wrapperDivClass'] !== '' && !$options['unwrap'] ) {
316 $text = Html
::rawElement( 'div', [ 'class' => $options['wrapperDivClass'] ], $text );
319 if ( $options['enableSectionEditLinks'] ) {
320 $text = preg_replace_callback(
321 self
::EDITSECTION_REGEX
,
323 $editsectionPage = Title
::newFromText( htmlspecialchars_decode( $m[1] ) );
324 $editsectionSection = htmlspecialchars_decode( $m[2] );
325 $editsectionContent = isset( $m[4] ) ? Sanitizer
::decodeCharReferences( $m[3] ) : null;
327 if ( !is_object( $editsectionPage ) ) {
328 throw new MWException( "Bad parser output text." );
331 $context = RequestContext
::getMain();
332 return $context->getSkin()->doEditSectionLink(
336 $context->getLanguage()
342 $text = preg_replace( self
::EDITSECTION_REGEX
, '', $text );
345 if ( $options['allowTOC'] ) {
346 $text = str_replace( [ Parser
::TOC_START
, Parser
::TOC_END
], '', $text );
348 $text = preg_replace(
349 '#' . preg_quote( Parser
::TOC_START
, '#' ) . '.*?' . preg_quote( Parser
::TOC_END
, '#' ) . '#s',
355 if ( $options['deduplicateStyles'] ) {
357 $text = preg_replace_callback(
358 '#<style\s+([^>]*data-mw-deduplicate\s*=[^>]*)>.*?</style>#s',
359 function ( $m ) use ( &$seen ) {
360 $attr = Sanitizer
::decodeTagAttributes( $m[1] );
361 if ( !isset( $attr['data-mw-deduplicate'] ) ) {
365 $key = $attr['data-mw-deduplicate'];
366 if ( !isset( $seen[$key] ) ) {
371 // We were going to use an empty <style> here, but there
372 // was concern that would be too much overhead for browsers.
373 // So let's hope a <link> with a non-standard rel and href isn't
374 // going to be misinterpreted or mangled by any subsequent processing.
375 return Html
::element( 'link', [
376 'rel' => 'mw-deduplicated-inline-style',
377 'href' => "mw-data:" . wfUrlencode( $key ),
384 // Hydrate slot section header placeholders generated by RevisionRenderer.
385 $text = preg_replace_callback(
386 '#<mw:slotheader>(.*?)</mw:slotheader>#',
388 $role = htmlspecialchars_decode( $m[1] );
389 // TODO: map to message, using the interface language. Set lang="xyz" accordingly.
399 * Add a CSS class to use for the wrapping div. If no class is given, no wrapper is added.
401 * @param string $class
403 public function addWrapperDivClass( $class ) {
404 $this->mWrapperDivClasses
[$class] = true;
408 * Clears the CSS class to use for the wrapping div, effectively disabling the wrapper div
409 * until addWrapperDivClass() is called.
411 public function clearWrapperDivClass() {
412 $this->mWrapperDivClasses
= [];
416 * Returns the class (or classes) to be used with the wrapper div for this otuput.
417 * If there is no wrapper class given, no wrapper div should be added.
418 * The wrapper div is added automatically by getText().
422 public function getWrapperDivClass() {
423 return implode( ' ', array_keys( $this->mWrapperDivClasses
) );
430 public function setSpeculativeRevIdUsed( $id ) {
431 $this->mSpeculativeRevId
= $id;
438 public function getSpeculativeRevIdUsed() {
439 return $this->mSpeculativeRevId
;
442 public function &getLanguageLinks() {
443 return $this->mLanguageLinks
;
446 public function getInterwikiLinks() {
447 return $this->mInterwikiLinks
;
450 public function getCategoryLinks() {
451 return array_keys( $this->mCategories
);
454 public function &getCategories() {
455 return $this->mCategories
;
462 public function getIndicators() {
463 return $this->mIndicators
;
466 public function getTitleText() {
467 return $this->mTitleText
;
470 public function getSections() {
471 return $this->mSections
;
474 public function &getLinks() {
475 return $this->mLinks
;
478 public function &getTemplates() {
479 return $this->mTemplates
;
482 public function &getTemplateIds() {
483 return $this->mTemplateIds
;
486 public function &getImages() {
487 return $this->mImages
;
490 public function &getFileSearchOptions() {
491 return $this->mFileSearchOptions
;
494 public function &getExternalLinks() {
495 return $this->mExternalLinks
;
498 public function setNoGallery( $value ) {
499 $this->mNoGallery
= (bool)$value;
502 public function getNoGallery() {
503 return $this->mNoGallery
;
506 public function getHeadItems() {
507 return $this->mHeadItems
;
510 public function getModules() {
511 return $this->mModules
;
514 public function getModuleStyles() {
515 return $this->mModuleStyles
;
522 public function getJsConfigVars() {
523 return $this->mJsConfigVars
;
526 public function getOutputHooks() {
527 return (array)$this->mOutputHooks
;
530 public function getWarnings() {
531 return array_keys( $this->mWarnings
);
534 public function getIndexPolicy() {
535 return $this->mIndexPolicy
;
538 public function getTOCHTML() {
539 return $this->mTOCHTML
;
543 * @return string|null TS_MW timestamp of the revision content
545 public function getTimestamp() {
546 return $this->mTimestamp
;
549 public function getLimitReportData() {
550 return $this->mLimitReportData
;
553 public function getLimitReportJSData() {
554 return $this->mLimitReportJSData
;
557 public function getEnableOOUI() {
558 return $this->mEnableOOUI
;
561 public function setText( $text ) {
562 return wfSetVar( $this->mText
, $text );
565 public function setLanguageLinks( $ll ) {
566 return wfSetVar( $this->mLanguageLinks
, $ll );
569 public function setCategoryLinks( $cl ) {
570 return wfSetVar( $this->mCategories
, $cl );
573 public function setTitleText( $t ) {
574 return wfSetVar( $this->mTitleText
, $t );
577 public function setSections( $toc ) {
578 return wfSetVar( $this->mSections
, $toc );
581 public function setIndexPolicy( $policy ) {
582 return wfSetVar( $this->mIndexPolicy
, $policy );
585 public function setTOCHTML( $tochtml ) {
586 return wfSetVar( $this->mTOCHTML
, $tochtml );
589 public function setTimestamp( $timestamp ) {
590 return wfSetVar( $this->mTimestamp
, $timestamp );
593 public function addCategory( $c, $sort ) {
594 $this->mCategories
[$c] = $sort;
599 * @param string $content
602 public function setIndicator( $id, $content ) {
603 $this->mIndicators
[$id] = $content;
607 * Enables OOUI, if true, in any OutputPage instance this ParserOutput
608 * object is added to.
611 * @param bool $enable If OOUI should be enabled or not
613 public function setEnableOOUI( $enable = false ) {
614 $this->mEnableOOUI
= $enable;
617 public function addLanguageLink( $t ) {
618 $this->mLanguageLinks
[] = $t;
621 public function addWarning( $s ) {
622 $this->mWarnings
[$s] = 1;
625 public function addOutputHook( $hook, $data = false ) {
626 $this->mOutputHooks
[] = [ $hook, $data ];
629 public function setNewSection( $value ) {
630 $this->mNewSection
= (bool)$value;
633 public function hideNewSection( $value ) {
634 $this->mHideNewSection
= (bool)$value;
637 public function getHideNewSection() {
638 return (bool)$this->mHideNewSection
;
641 public function getNewSection() {
642 return (bool)$this->mNewSection
;
646 * Checks, if a url is pointing to the own server
648 * @param string $internal The server to check against
649 * @param string $url The url to check
652 public static function isLinkInternal( $internal, $url ) {
653 return (bool)preg_match( '/^' .
654 # If server is proto relative, check also for http/https links
655 ( substr( $internal, 0, 2 ) === '//' ?
'(?:https?:)?' : '' ) .
656 preg_quote( $internal, '/' ) .
657 # check for query/path/anchor or end of link in each case
663 public function addExternalLink( $url ) {
664 # We don't register links pointing to our own server, unless... :-)
665 global $wgServer, $wgRegisterInternalExternals;
667 # Replace unnecessary URL escape codes with the referenced character
668 # This prevents spammers from hiding links from the filters
669 $url = Parser
::normalizeLinkUrl( $url );
671 $registerExternalLink = true;
672 if ( !$wgRegisterInternalExternals ) {
673 $registerExternalLink = !self
::isLinkInternal( $wgServer, $url );
675 if ( $registerExternalLink ) {
676 $this->mExternalLinks
[$url] = 1;
681 * Record a local or interwiki inline link for saving in future link tables.
683 * @param Title $title
684 * @param int|null $id Optional known page_id so we can skip the lookup
686 public function addLink( Title
$title, $id = null ) {
687 if ( $title->isExternal() ) {
688 // Don't record interwikis in pagelinks
689 $this->addInterwikiLink( $title );
692 $ns = $title->getNamespace();
693 $dbk = $title->getDBkey();
694 if ( $ns == NS_MEDIA
) {
695 // Normalize this pseudo-alias if it makes it down here...
697 } elseif ( $ns == NS_SPECIAL
) {
698 // We don't record Special: links currently
699 // It might actually be wise to, but we'd need to do some normalization.
701 } elseif ( $dbk === '' ) {
702 // Don't record self links - [[#Foo]]
705 if ( !isset( $this->mLinks
[$ns] ) ) {
706 $this->mLinks
[$ns] = [];
708 if ( is_null( $id ) ) {
709 $id = $title->getArticleID();
711 $this->mLinks
[$ns][$dbk] = $id;
715 * Register a file dependency for this output
716 * @param string $name Title dbKey
717 * @param string|false|null $timestamp MW timestamp of file creation (or false if non-existing)
718 * @param string|false|null $sha1 Base 36 SHA-1 of file (or false if non-existing)
720 public function addImage( $name, $timestamp = null, $sha1 = null ) {
721 $this->mImages
[$name] = 1;
722 if ( $timestamp !== null && $sha1 !== null ) {
723 $this->mFileSearchOptions
[$name] = [ 'time' => $timestamp, 'sha1' => $sha1 ];
728 * Register a template dependency for this output
729 * @param Title $title
730 * @param int $page_id
733 public function addTemplate( $title, $page_id, $rev_id ) {
734 $ns = $title->getNamespace();
735 $dbk = $title->getDBkey();
736 if ( !isset( $this->mTemplates
[$ns] ) ) {
737 $this->mTemplates
[$ns] = [];
739 $this->mTemplates
[$ns][$dbk] = $page_id;
740 if ( !isset( $this->mTemplateIds
[$ns] ) ) {
741 $this->mTemplateIds
[$ns] = [];
743 $this->mTemplateIds
[$ns][$dbk] = $rev_id; // For versioning
747 * @param Title $title Title object, must be an interwiki link
748 * @throws MWException If given invalid input
750 public function addInterwikiLink( $title ) {
751 if ( !$title->isExternal() ) {
752 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
754 $prefix = $title->getInterwiki();
755 if ( !isset( $this->mInterwikiLinks
[$prefix] ) ) {
756 $this->mInterwikiLinks
[$prefix] = [];
758 $this->mInterwikiLinks
[$prefix][$title->getDBkey()] = 1;
762 * Add some text to the "<head>".
763 * If $tag is set, the section with that tag will only be included once
765 * @param string $section
766 * @param string|bool $tag
768 public function addHeadItem( $section, $tag = false ) {
769 if ( $tag !== false ) {
770 $this->mHeadItems
[$tag] = $section;
772 $this->mHeadItems
[] = $section;
777 * @see OutputPage::addModules
779 public function addModules( $modules ) {
780 $this->mModules
= array_merge( $this->mModules
, (array)$modules );
784 * @see OutputPage::addModuleStyles
786 public function addModuleStyles( $modules ) {
787 $this->mModuleStyles
= array_merge( $this->mModuleStyles
, (array)$modules );
791 * Add one or more variables to be set in mw.config in JavaScript.
793 * @param string|array $keys Key or array of key/value pairs.
794 * @param mixed|null $value [optional] Value of the configuration variable.
797 public function addJsConfigVars( $keys, $value = null ) {
798 if ( is_array( $keys ) ) {
799 foreach ( $keys as $key => $value ) {
800 $this->mJsConfigVars
[$key] = $value;
805 $this->mJsConfigVars
[$keys] = $value;
809 * Copy items from the OutputPage object into this one
811 * @param OutputPage $out
813 public function addOutputPageMetadata( OutputPage
$out ) {
814 $this->addModules( $out->getModules() );
815 $this->addModuleStyles( $out->getModuleStyles() );
816 $this->addJsConfigVars( $out->getJsConfigVars() );
818 $this->mHeadItems
= array_merge( $this->mHeadItems
, $out->getHeadItemsArray() );
819 $this->mPreventClickjacking
= $this->mPreventClickjacking ||
$out->getPreventClickjacking();
823 * Add a tracking category, getting the title from a system message,
824 * or print a debug message if the title is invalid.
826 * Any message used with this function should be registered so it will
827 * show up on Special:TrackingCategories. Core messages should be added
828 * to SpecialTrackingCategories::$coreTrackingCategories, and extensions
829 * should add to "TrackingCategories" in their extension.json.
831 * @todo Migrate some code to TrackingCategories
833 * @param string $msg Message key
834 * @param Title $title title of the page which is being tracked
835 * @return bool Whether the addition was successful
838 public function addTrackingCategory( $msg, $title ) {
839 if ( $title->isSpecialPage() ) {
840 wfDebug( __METHOD__
. ": Not adding tracking category $msg to special page!\n" );
844 // Important to parse with correct title (T33469)
845 $cat = wfMessage( $msg )
847 ->inContentLanguage()
850 # Allow tracking categories to be disabled by setting them to "-"
851 if ( $cat === '-' ) {
855 $containerCategory = Title
::makeTitleSafe( NS_CATEGORY
, $cat );
856 if ( $containerCategory ) {
857 $this->addCategory( $containerCategory->getDBkey(), $this->getProperty( 'defaultsort' ) ?
: '' );
860 wfDebug( __METHOD__
. ": [[MediaWiki:$msg]] is not a valid title!\n" );
866 * Override the title to be used for display
868 * @note this is assumed to have been validated
869 * (check equal normalisation, etc.)
871 * @note this is expected to be safe HTML,
872 * ready to be served to the client.
874 * @param string $text Desired title text
876 public function setDisplayTitle( $text ) {
877 $this->setTitleText( $text );
878 $this->setProperty( 'displaytitle', $text );
882 * Get the title to be used for display.
884 * As per the contract of setDisplayTitle(), this is safe HTML,
885 * ready to be served to the client.
887 * @return string HTML
889 public function getDisplayTitle() {
890 $t = $this->getTitleText();
898 * Attach a flag to the output so that it can be checked later to handle special cases
900 * @param string $flag
902 public function setFlag( $flag ) {
903 $this->mFlags
[$flag] = true;
907 * @param string $flag
908 * @return bool Whether the given flag was set to signify a special case
910 public function getFlag( $flag ) {
911 return isset( $this->mFlags
[$flag] );
915 * @return string[] List of flags signifying special cases
918 public function getAllFlags() {
919 return array_keys( $this->mFlags
);
923 * Set a property to be stored in the page_props database table.
925 * page_props is a key value store indexed by the page ID. This allows
926 * the parser to set a property on a page which can then be quickly
927 * retrieved given the page ID or via a DB join when given the page
930 * Since 1.23, page_props are also indexed by numeric value, to allow
931 * for efficient "top k" queries of pages wrt a given property.
933 * setProperty() is thus used to propagate properties from the parsed
934 * page to request contexts other than a page view of the currently parsed
937 * Some applications examples:
939 * * To implement hidden categories, hiding pages from category listings
940 * by storing a property.
942 * * Overriding the displayed article title (ParserOutput::setDisplayTitle()).
944 * * To implement image tagging, for example displaying an icon on an
945 * image thumbnail to indicate that it is listed for deletion on
947 * This is not actually implemented, yet but would be pretty cool.
949 * @note Do not use setProperty() to set a property which is only used
950 * in a context where the ParserOutput object itself is already available,
951 * for example a normal page view. There is no need to save such a property
952 * in the database since the text is already parsed. You can just hook
953 * OutputPageParserOutput and get your data out of the ParserOutput object.
955 * If you are writing an extension where you want to set a property in the
956 * parser which is used by an OutputPageParserOutput hook, you have to
957 * associate the extension data directly with the ParserOutput object.
958 * Since MediaWiki 1.21, you can use setExtensionData() to do this:
962 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
965 * And then later, in OutputPageParserOutput or similar:
969 * $output->getExtensionData( 'my_ext_foo' );
972 * In MediaWiki 1.20 and older, you have to use a custom member variable
973 * within the ParserOutput object:
977 * $parser->getOutput()->my_ext_foo = '...';
979 * @param string $name
980 * @param mixed $value
982 public function setProperty( $name, $value ) {
983 $this->mProperties
[$name] = $value;
987 * @param string $name The property name to look up.
989 * @return mixed|bool The value previously set using setProperty(). False if null or no value
990 * was set for the given property name.
992 * @note You need to use getProperties() to check for boolean and null properties.
994 public function getProperty( $name ) {
995 return $this->mProperties
[$name] ??
false;
998 public function unsetProperty( $name ) {
999 unset( $this->mProperties
[$name] );
1002 public function getProperties() {
1003 if ( !isset( $this->mProperties
) ) {
1004 $this->mProperties
= [];
1006 return $this->mProperties
;
1010 * Returns the options from its ParserOptions which have been taken
1011 * into account to produce this output.
1014 public function getUsedOptions() {
1015 if ( !isset( $this->mAccessedOptions
) ) {
1018 return array_keys( $this->mAccessedOptions
);
1022 * Tags a parser option for use in the cache key for this parser output.
1023 * Registered as a watcher at ParserOptions::registerWatcher() by Parser::clearState().
1024 * The information gathered here is available via getUsedOptions(),
1025 * and is used by ParserCache::save().
1027 * @see ParserCache::getKey
1028 * @see ParserCache::save
1029 * @see ParserOptions::addExtraKey
1030 * @see ParserOptions::optionsHash
1031 * @param string $option
1033 public function recordOption( $option ) {
1034 $this->mAccessedOptions
[$option] = true;
1038 * Attaches arbitrary data to this ParserObject. This can be used to store some information in
1039 * the ParserOutput object for later use during page output. The data will be cached along with
1040 * the ParserOutput object, but unlike data set using setProperty(), it is not recorded in the
1043 * This method is provided to overcome the unsafe practice of attaching extra information to a
1044 * ParserObject by directly assigning member variables.
1046 * To use setExtensionData() to pass extension information from a hook inside the parser to a
1047 * hook in the page output, use this in the parser hook:
1051 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
1054 * And then later, in OutputPageParserOutput or similar:
1058 * $output->getExtensionData( 'my_ext_foo' );
1061 * In MediaWiki 1.20 and older, you have to use a custom member variable
1062 * within the ParserOutput object:
1066 * $parser->getOutput()->my_ext_foo = '...';
1071 * @param string $key The key for accessing the data. Extensions should take care to avoid
1072 * conflicts in naming keys. It is suggested to use the extension's name as a prefix.
1074 * @param mixed $value The value to set. Setting a value to null is equivalent to removing
1077 public function setExtensionData( $key, $value ) {
1078 if ( $value === null ) {
1079 unset( $this->mExtensionData
[$key] );
1081 $this->mExtensionData
[$key] = $value;
1086 * Gets extensions data previously attached to this ParserOutput using setExtensionData().
1087 * Typically, such data would be set while parsing the page, e.g. by a parser function.
1091 * @param string $key The key to look up.
1093 * @return mixed|null The value previously set for the given key using setExtensionData()
1094 * or null if no value was set for this key.
1096 public function getExtensionData( $key ) {
1097 return $this->mExtensionData
[$key] ??
null;
1100 private static function getTimes( $clock = null ) {
1102 if ( !$clock ||
$clock === 'wall' ) {
1103 $ret['wall'] = microtime( true );
1105 if ( !$clock ||
$clock === 'cpu' ) {
1106 $ru = wfGetRusage();
1108 $ret['cpu'] = $ru['ru_utime.tv_sec'] +
$ru['ru_utime.tv_usec'] / 1e6
;
1109 $ret['cpu'] +
= $ru['ru_stime.tv_sec'] +
$ru['ru_stime.tv_usec'] / 1e6
;
1116 * Resets the parse start timestamps for future calls to getTimeSinceStart()
1119 public function resetParseStartTime() {
1120 $this->mParseStartTime
= self
::getTimes();
1124 * Returns the time since resetParseStartTime() was last called
1126 * Clocks available are:
1127 * - wall: Wall clock time
1128 * - cpu: CPU time (requires getrusage)
1131 * @param string $clock
1132 * @return float|null
1134 public function getTimeSinceStart( $clock ) {
1135 if ( !isset( $this->mParseStartTime
[$clock] ) ) {
1139 $end = self
::getTimes( $clock );
1140 return $end[$clock] - $this->mParseStartTime
[$clock];
1144 * Sets parser limit report data for a key
1146 * The key is used as the prefix for various messages used for formatting:
1147 * - $key: The label for the field in the limit report
1148 * - $key-value-text: Message used to format the value in the "NewPP limit
1149 * report" HTML comment. If missing, uses $key-format.
1150 * - $key-value-html: Message used to format the value in the preview
1151 * limit report table. If missing, uses $key-format.
1152 * - $key-value: Message used to format the value. If missing, uses "$1".
1154 * Note that all values are interpreted as wikitext, and so should be
1155 * encoded with htmlspecialchars() as necessary, but should avoid complex
1156 * HTML for sanity of display in the "NewPP limit report" comment.
1159 * @param string $key Message key
1160 * @param mixed $value Appropriate for Message::params()
1162 public function setLimitReportData( $key, $value ) {
1163 $this->mLimitReportData
[$key] = $value;
1165 if ( is_array( $value ) ) {
1166 if ( array_keys( $value ) === [ 0, 1 ]
1167 && is_numeric( $value[0] )
1168 && is_numeric( $value[1] )
1170 $data = [ 'value' => $value[0], 'limit' => $value[1] ];
1178 if ( strpos( $key, '-' ) ) {
1179 list( $ns, $name ) = explode( '-', $key, 2 );
1180 $this->mLimitReportJSData
[$ns][$name] = $data;
1182 $this->mLimitReportJSData
[$key] = $data;
1187 * Check whether the cache TTL was lowered due to dynamic content
1189 * When content is determined by more than hard state (e.g. page edits),
1190 * such as template/file transclusions based on the current timestamp or
1191 * extension tags that generate lists based on queries, this return true.
1196 public function hasDynamicContent() {
1197 global $wgParserCacheExpireTime;
1199 return $this->getCacheExpiry() < $wgParserCacheExpireTime;
1203 * Get or set the prevent-clickjacking flag
1206 * @param bool|null $flag New flag value, or null to leave it unchanged
1207 * @return bool Old flag value
1209 public function preventClickjacking( $flag = null ) {
1210 return wfSetVar( $this->mPreventClickjacking
, $flag );
1214 * Lower the runtime adaptive TTL to at most this value
1219 public function updateRuntimeAdaptiveExpiry( $ttl ) {
1220 $this->mMaxAdaptiveExpiry
= min( $ttl, $this->mMaxAdaptiveExpiry
);
1221 $this->updateCacheExpiry( $ttl );
1225 * Call this when parsing is done to lower the TTL based on low parse times
1229 public function finalizeAdaptiveCacheExpiry() {
1230 if ( is_infinite( $this->mMaxAdaptiveExpiry
) ) {
1234 $runtime = $this->getTimeSinceStart( 'wall' );
1235 if ( is_float( $runtime ) ) {
1236 $slope = ( self
::SLOW_AR_TTL
- self
::FAST_AR_TTL
)
1237 / ( self
::PARSE_SLOW_SEC
- self
::PARSE_FAST_SEC
);
1238 // SLOW_AR_TTL = PARSE_SLOW_SEC * $slope + $point
1239 $point = self
::SLOW_AR_TTL
- self
::PARSE_SLOW_SEC
* $slope;
1242 max( $slope * $runtime +
$point, self
::MIN_AR_TTL
),
1243 $this->mMaxAdaptiveExpiry
1245 $this->updateCacheExpiry( $adaptiveTTL );
1249 public function __sleep() {
1251 array_keys( get_object_vars( $this ) ),
1252 [ 'mParseStartTime' ]
1257 * Merges internal metadata such as flags, accessed options, and profiling info
1258 * from $source into this ParserOutput. This should be used whenever the state of $source
1259 * has any impact on the state of this ParserOutput.
1261 * @param ParserOutput $source
1263 public function mergeInternalMetaDataFrom( ParserOutput
$source ) {
1264 $this->mOutputHooks
= self
::mergeList( $this->mOutputHooks
, $source->getOutputHooks() );
1265 $this->mWarnings
= self
::mergeMap( $this->mWarnings
, $source->mWarnings
); // don't use getter
1266 $this->mTimestamp
= $this->useMaxValue( $this->mTimestamp
, $source->getTimestamp() );
1268 if ( $this->mSpeculativeRevId
&& $source->mSpeculativeRevId
1269 && $this->mSpeculativeRevId
!== $source->mSpeculativeRevId
1272 'Inconsistent speculative revision ID encountered while merging parser output!'
1276 $this->mSpeculativeRevId
= $this->useMaxValue(
1277 $this->mSpeculativeRevId
,
1278 $source->getSpeculativeRevIdUsed()
1280 $this->mParseStartTime
= $this->useEachMinValue(
1281 $this->mParseStartTime
,
1282 $source->mParseStartTime
1285 $this->mFlags
= self
::mergeMap( $this->mFlags
, $source->mFlags
);
1286 $this->mAccessedOptions
= self
::mergeMap( $this->mAccessedOptions
, $source->mAccessedOptions
);
1288 // TODO: maintain per-slot limit reports!
1289 if ( empty( $this->mLimitReportData
) ) {
1290 $this->mLimitReportData
= $source->mLimitReportData
;
1292 if ( empty( $this->mLimitReportJSData
) ) {
1293 $this->mLimitReportJSData
= $source->mLimitReportJSData
;
1298 * Merges HTML metadata such as head items, JS config vars, and HTTP cache control info
1299 * from $source into this ParserOutput. This should be used whenever the HTML in $source
1300 * has been somehow mered into the HTML of this ParserOutput.
1302 * @param ParserOutput $source
1304 public function mergeHtmlMetaDataFrom( ParserOutput
$source ) {
1306 $this->mHeadItems
= self
::mergeMixedList( $this->mHeadItems
, $source->getHeadItems() );
1307 $this->mModules
= self
::mergeList( $this->mModules
, $source->getModules() );
1308 $this->mModuleStyles
= self
::mergeList( $this->mModuleStyles
, $source->getModuleStyles() );
1309 $this->mJsConfigVars
= self
::mergeMap( $this->mJsConfigVars
, $source->getJsConfigVars() );
1310 $this->mMaxAdaptiveExpiry
= min( $this->mMaxAdaptiveExpiry
, $source->mMaxAdaptiveExpiry
);
1312 // "noindex" always wins!
1313 if ( $this->mIndexPolicy
=== 'noindex' ||
$source->mIndexPolicy
=== 'noindex' ) {
1314 $this->mIndexPolicy
= 'noindex';
1315 } elseif ( $this->mIndexPolicy
!== 'index' ) {
1316 $this->mIndexPolicy
= $source->mIndexPolicy
;
1320 $this->mNewSection
= $this->mNewSection ||
$source->getNewSection();
1321 $this->mHideNewSection
= $this->mHideNewSection ||
$source->getHideNewSection();
1322 $this->mNoGallery
= $this->mNoGallery ||
$source->getNoGallery();
1323 $this->mEnableOOUI
= $this->mEnableOOUI ||
$source->getEnableOOUI();
1324 $this->mPreventClickjacking
= $this->mPreventClickjacking ||
$source->preventClickjacking();
1326 // TODO: we'll have to be smarter about this!
1327 $this->mSections
= array_merge( $this->mSections
, $source->getSections() );
1328 $this->mTOCHTML
.= $source->mTOCHTML
;
1330 // XXX: we don't want to concatenate title text, so first write wins.
1331 // We should use the first *modified* title text, but we don't have the original to check.
1332 if ( $this->mTitleText
=== null ||
$this->mTitleText
=== '' ) {
1333 $this->mTitleText
= $source->mTitleText
;
1336 // class names are stored in array keys
1337 $this->mWrapperDivClasses
= self
::mergeMap(
1338 $this->mWrapperDivClasses
,
1339 $source->mWrapperDivClasses
1342 // NOTE: last write wins, same as within one ParserOutput
1343 $this->mIndicators
= self
::mergeMap( $this->mIndicators
, $source->getIndicators() );
1345 // NOTE: include extension data in "tracking meta data" as well as "html meta data"!
1346 // TODO: add a $mergeStrategy parameter to setExtensionData to allow different
1347 // kinds of extension data to be merged in different ways.
1348 $this->mExtensionData
= self
::mergeMap(
1349 $this->mExtensionData
,
1350 $source->mExtensionData
1355 * Merges dependency tracking metadata such as backlinks, images used, and extension data
1356 * from $source into this ParserOutput. This allows dependency tracking to be done for the
1357 * combined output of multiple content slots.
1359 * @param ParserOutput $source
1361 public function mergeTrackingMetaDataFrom( ParserOutput
$source ) {
1362 $this->mLanguageLinks
= self
::mergeList( $this->mLanguageLinks
, $source->getLanguageLinks() );
1363 $this->mCategories
= self
::mergeMap( $this->mCategories
, $source->getCategories() );
1364 $this->mLinks
= self
::merge2D( $this->mLinks
, $source->getLinks() );
1365 $this->mTemplates
= self
::merge2D( $this->mTemplates
, $source->getTemplates() );
1366 $this->mTemplateIds
= self
::merge2D( $this->mTemplateIds
, $source->getTemplateIds() );
1367 $this->mImages
= self
::mergeMap( $this->mImages
, $source->getImages() );
1368 $this->mFileSearchOptions
= self
::mergeMap(
1369 $this->mFileSearchOptions
,
1370 $source->getFileSearchOptions()
1372 $this->mExternalLinks
= self
::mergeMap( $this->mExternalLinks
, $source->getExternalLinks() );
1373 $this->mInterwikiLinks
= self
::merge2D(
1374 $this->mInterwikiLinks
,
1375 $source->getInterwikiLinks()
1378 // TODO: add a $mergeStrategy parameter to setProperty to allow different
1379 // kinds of properties to be merged in different ways.
1380 $this->mProperties
= self
::mergeMap( $this->mProperties
, $source->getProperties() );
1382 // NOTE: include extension data in "tracking meta data" as well as "html meta data"!
1383 // TODO: add a $mergeStrategy parameter to setExtensionData to allow different
1384 // kinds of extension data to be merged in different ways.
1385 $this->mExtensionData
= self
::mergeMap(
1386 $this->mExtensionData
,
1387 $source->mExtensionData
1391 private static function mergeMixedList( array $a, array $b ) {
1392 return array_unique( array_merge( $a, $b ), SORT_REGULAR
);
1395 private static function mergeList( array $a, array $b ) {
1396 return array_values( array_unique( array_merge( $a, $b ), SORT_REGULAR
) );
1399 private static function mergeMap( array $a, array $b ) {
1400 return array_replace( $a, $b );
1403 private static function merge2D( array $a, array $b ) {
1405 $keys = array_merge( array_keys( $a ), array_keys( $b ) );
1407 foreach ( $keys as $k ) {
1408 if ( empty( $a[$k] ) ) {
1409 $values[$k] = $b[$k];
1410 } elseif ( empty( $b[$k] ) ) {
1411 $values[$k] = $a[$k];
1412 } elseif ( is_array( $a[$k] ) && is_array( $b[$k] ) ) {
1413 $values[$k] = array_replace( $a[$k], $b[$k] );
1415 $values[$k] = $b[$k];
1422 private static function useEachMinValue( array $a, array $b ) {
1424 $keys = array_merge( array_keys( $a ), array_keys( $b ) );
1426 foreach ( $keys as $k ) {
1427 if ( is_array( $a[$k] ??
null ) && is_array( $b[$k] ??
null ) ) {
1428 $values[$k] = self
::useEachMinValue( $a[$k], $b[$k] );
1430 $values[$k] = self
::useMinValue( $a[$k] ??
null, $b[$k] ??
null );
1437 private static function useMinValue( $a, $b ) {
1438 if ( $a === null ) {
1442 if ( $b === null ) {
1446 return min( $a, $b );
1449 private static function useMaxValue( $a, $b ) {
1450 if ( $a === null ) {
1454 if ( $b === null ) {
1458 return max( $a, $b );