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
{
26 * @var string $mText The output text
31 * @var array $mLanguageLinks List of the full text of language links,
32 * in the order they appear.
34 public $mLanguageLinks;
37 * @var array $mCategoriesMap of category names to sort keys
42 * @var array $mIndicators Page status indicators, usually displayed in top-right corner.
44 public $mIndicators = [];
47 * @var string $mTitleText Title text of the chosen language variant, as HTML.
52 * @var array $mLinks 2-D map of NS/DBK to ID for the links in the document.
58 * @var array $mTemplates 2-D map of NS/DBK to ID for the template references.
61 public $mTemplates = [];
64 * @var array $mTemplateIds 2-D map of NS/DBK to rev ID for the template references.
67 public $mTemplateIds = [];
70 * @var array $mImages DB keys of the images used, in the array key only
75 * @var array $mFileSearchOptions DB keys of the images used mapped to sha1 and MW timestamp.
77 public $mFileSearchOptions = [];
80 * @var array $mExternalLinks External link URLs, in the key only.
82 public $mExternalLinks = [];
85 * @var array $mInterwikiLinks 2-D map of prefix/DBK (in keys only)
86 * for the inline interwiki links in the document.
88 public $mInterwikiLinks = [];
91 * @var bool $mNewSection Show a new section link?
93 public $mNewSection = false;
96 * @var bool $mHideNewSection Hide the new section link?
98 public $mHideNewSection = false;
101 * @var bool $mNoGallery No gallery on category page? (__NOGALLERY__).
103 public $mNoGallery = false;
106 * @var array $mHeadItems Items to put in the <head> section
108 public $mHeadItems = [];
111 * @var array $mModules Modules to be loaded by ResourceLoader
113 public $mModules = [];
116 * @var array $mModuleScripts Modules of which only the JS will be loaded by ResourceLoader.
118 public $mModuleScripts = [];
121 * @var array $mModuleStyles Modules of which only the CSSS will be loaded by ResourceLoader.
123 public $mModuleStyles = [];
126 * @var array $mJsConfigVars JavaScript config variable for mw.config combined with this page.
128 public $mJsConfigVars = [];
131 * @var array $mOutputHooks Hook tags as per $wgParserOutputHooks.
133 public $mOutputHooks = [];
136 * @var array $mWarnings Warning text to be returned to the user.
137 * Wikitext formatted, in the key only.
139 public $mWarnings = [];
142 * @var array $mSections Table of contents
144 public $mSections = [];
147 * @var bool $mEditSectionTokens prefix/suffix markers if edit sections were output as tokens.
149 public $mEditSectionTokens = false;
152 * @var array $mProperties Name/value pairs to be cached in the DB.
154 public $mProperties = [];
157 * @var string $mTOCHTML HTML of the TOC.
159 public $mTOCHTML = '';
162 * @var string $mTimestamp Timestamp of the revision.
167 * @var bool $mTOCEnabled Whether TOC should be shown, can't override __NOTOC__.
169 public $mTOCEnabled = true;
172 * @var bool $mEnableOOUI Whether OOUI should be enabled.
174 public $mEnableOOUI = false;
177 * @var string $mIndexPolicy 'index' or 'noindex'? Any other value will result in no change.
179 private $mIndexPolicy = '';
182 * @var array $mAccessedOptions List of ParserOptions (stored in the keys).
184 private $mAccessedOptions = [];
187 * @var array $mExtensionData extra data used by extensions.
189 private $mExtensionData = [];
191 /** @var array $mLimitReportData Parser limit report data. */
192 private $mLimitReportData = [];
195 * @var array $mParseStartTime Timestamps for getTimeSinceStart().
197 private $mParseStartTime = [];
200 * @var bool $mPreventClickjacking Whether to emit X-Frame-Options: DENY.
202 private $mPreventClickjacking = false;
205 * @var array $mFlags Generic flags.
207 private $mFlags = [];
209 /** @var integer|null Assumed rev ID for {{REVISIONID}} if no revision is set */
210 private $mSpeculativeRevId;
212 /** @var integer Upper bound of expiry based on parse duration */
213 private $mMaxAdaptiveExpiry = INF
;
215 const EDITSECTION_REGEX
=
216 '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
218 // finalizeAdaptiveCacheExpiry() uses TTL = MAX( m * PARSE_TIME + b, MIN_AR_TTL)
219 // Current values imply that m=3933.333333 and b=-333.333333
220 // See https://www.nngroup.com/articles/website-response-times/
221 const PARSE_FAST_SEC
= .100; // perceived "fast" page parse
222 const PARSE_SLOW_SEC
= 1.0; // perceived "slow" page parse
223 const FAST_AR_TTL
= 60; // adaptive TTL for "fast" pages
224 const SLOW_AR_TTL
= 3600; // adaptive TTL for "slow" pages
225 const MIN_AR_TTL
= 15; // min adaptive TTL (for sanity, pool counter, and edit stashing)
227 public function __construct( $text = '', $languageLinks = [], $categoryLinks = [],
228 $unused = false, $titletext = ''
230 $this->mText
= $text;
231 $this->mLanguageLinks
= $languageLinks;
232 $this->mCategories
= $categoryLinks;
233 $this->mTitleText
= $titletext;
237 * Get the cacheable text with <mw:editsection> markers still in it. The
238 * return value is suitable for writing back via setText() but is not valid
239 * for display to the user.
243 public function getRawText() {
247 public function getText() {
248 $text = $this->mText
;
249 if ( $this->mEditSectionTokens
) {
250 $text = preg_replace_callback(
251 ParserOutput
::EDITSECTION_REGEX
,
253 global $wgOut, $wgLang;
254 $editsectionPage = Title
::newFromText( htmlspecialchars_decode( $m[1] ) );
255 $editsectionSection = htmlspecialchars_decode( $m[2] );
256 $editsectionContent = isset( $m[4] ) ?
$m[3] : null;
258 if ( !is_object( $editsectionPage ) ) {
259 throw new MWException( "Bad parser output text." );
262 $skin = $wgOut->getSkin();
263 return call_user_func_array(
264 [ $skin, 'doEditSectionLink' ],
265 [ $editsectionPage, $editsectionSection,
266 $editsectionContent, $wgLang->getCode() ]
272 $text = preg_replace( ParserOutput
::EDITSECTION_REGEX
, '', $text );
275 // If you have an old cached version of this class - sorry, you can't disable the TOC
276 if ( isset( $this->mTOCEnabled
) && $this->mTOCEnabled
) {
277 $text = str_replace( [ Parser
::TOC_START
, Parser
::TOC_END
], '', $text );
279 $text = preg_replace(
280 '#' . preg_quote( Parser
::TOC_START
, '#' ) . '.*?' . preg_quote( Parser
::TOC_END
, '#' ) . '#s',
292 public function setSpeculativeRevIdUsed( $id ) {
293 $this->mSpeculativeRevId
= $id;
297 public function getSpeculativeRevIdUsed() {
298 return $this->mSpeculativeRevId
;
301 public function &getLanguageLinks() {
302 return $this->mLanguageLinks
;
305 public function getInterwikiLinks() {
306 return $this->mInterwikiLinks
;
309 public function getCategoryLinks() {
310 return array_keys( $this->mCategories
);
313 public function &getCategories() {
314 return $this->mCategories
;
320 public function getIndicators() {
321 return $this->mIndicators
;
324 public function getTitleText() {
325 return $this->mTitleText
;
328 public function getSections() {
329 return $this->mSections
;
332 public function getEditSectionTokens() {
333 return $this->mEditSectionTokens
;
336 public function &getLinks() {
337 return $this->mLinks
;
340 public function &getTemplates() {
341 return $this->mTemplates
;
344 public function &getTemplateIds() {
345 return $this->mTemplateIds
;
348 public function &getImages() {
349 return $this->mImages
;
352 public function &getFileSearchOptions() {
353 return $this->mFileSearchOptions
;
356 public function &getExternalLinks() {
357 return $this->mExternalLinks
;
360 public function getNoGallery() {
361 return $this->mNoGallery
;
364 public function getHeadItems() {
365 return $this->mHeadItems
;
368 public function getModules() {
369 return $this->mModules
;
372 public function getModuleScripts() {
373 return $this->mModuleScripts
;
376 public function getModuleStyles() {
377 return $this->mModuleStyles
;
381 public function getJsConfigVars() {
382 return $this->mJsConfigVars
;
385 public function getOutputHooks() {
386 return (array)$this->mOutputHooks
;
389 public function getWarnings() {
390 return array_keys( $this->mWarnings
);
393 public function getIndexPolicy() {
394 return $this->mIndexPolicy
;
397 public function getTOCHTML() {
398 return $this->mTOCHTML
;
402 * @return string|null TS_MW timestamp of the revision content
404 public function getTimestamp() {
405 return $this->mTimestamp
;
408 public function getLimitReportData() {
409 return $this->mLimitReportData
;
412 public function getTOCEnabled() {
413 return $this->mTOCEnabled
;
416 public function getEnableOOUI() {
417 return $this->mEnableOOUI
;
420 public function setText( $text ) {
421 return wfSetVar( $this->mText
, $text );
424 public function setLanguageLinks( $ll ) {
425 return wfSetVar( $this->mLanguageLinks
, $ll );
428 public function setCategoryLinks( $cl ) {
429 return wfSetVar( $this->mCategories
, $cl );
432 public function setTitleText( $t ) {
433 return wfSetVar( $this->mTitleText
, $t );
436 public function setSections( $toc ) {
437 return wfSetVar( $this->mSections
, $toc );
440 public function setEditSectionTokens( $t ) {
441 return wfSetVar( $this->mEditSectionTokens
, $t );
444 public function setIndexPolicy( $policy ) {
445 return wfSetVar( $this->mIndexPolicy
, $policy );
448 public function setTOCHTML( $tochtml ) {
449 return wfSetVar( $this->mTOCHTML
, $tochtml );
452 public function setTimestamp( $timestamp ) {
453 return wfSetVar( $this->mTimestamp
, $timestamp );
456 public function setTOCEnabled( $flag ) {
457 return wfSetVar( $this->mTOCEnabled
, $flag );
460 public function addCategory( $c, $sort ) {
461 $this->mCategories
[$c] = $sort;
467 public function setIndicator( $id, $content ) {
468 $this->mIndicators
[$id] = $content;
472 * Enables OOUI, if true, in any OutputPage instance this ParserOutput
473 * object is added to.
476 * @param bool $enable If OOUI should be enabled or not
478 public function setEnableOOUI( $enable = false ) {
479 $this->mEnableOOUI
= $enable;
482 public function addLanguageLink( $t ) {
483 $this->mLanguageLinks
[] = $t;
486 public function addWarning( $s ) {
487 $this->mWarnings
[$s] = 1;
490 public function addOutputHook( $hook, $data = false ) {
491 $this->mOutputHooks
[] = [ $hook, $data ];
494 public function setNewSection( $value ) {
495 $this->mNewSection
= (bool)$value;
497 public function hideNewSection( $value ) {
498 $this->mHideNewSection
= (bool)$value;
500 public function getHideNewSection() {
501 return (bool)$this->mHideNewSection
;
503 public function getNewSection() {
504 return (bool)$this->mNewSection
;
508 * Checks, if a url is pointing to the own server
510 * @param string $internal The server to check against
511 * @param string $url The url to check
514 public static function isLinkInternal( $internal, $url ) {
515 return (bool)preg_match( '/^' .
516 # If server is proto relative, check also for http/https links
517 ( substr( $internal, 0, 2 ) === '//' ?
'(?:https?:)?' : '' ) .
518 preg_quote( $internal, '/' ) .
519 # check for query/path/anchor or end of link in each case
525 public function addExternalLink( $url ) {
526 # We don't register links pointing to our own server, unless... :-)
527 global $wgServer, $wgRegisterInternalExternals;
529 $registerExternalLink = true;
530 if ( !$wgRegisterInternalExternals ) {
531 $registerExternalLink = !self
::isLinkInternal( $wgServer, $url );
533 if ( $registerExternalLink ) {
534 $this->mExternalLinks
[$url] = 1;
539 * Record a local or interwiki inline link for saving in future link tables.
541 * @param Title $title
542 * @param int|null $id Optional known page_id so we can skip the lookup
544 public function addLink( Title
$title, $id = null ) {
545 if ( $title->isExternal() ) {
546 // Don't record interwikis in pagelinks
547 $this->addInterwikiLink( $title );
550 $ns = $title->getNamespace();
551 $dbk = $title->getDBkey();
552 if ( $ns == NS_MEDIA
) {
553 // Normalize this pseudo-alias if it makes it down here...
555 } elseif ( $ns == NS_SPECIAL
) {
556 // We don't record Special: links currently
557 // It might actually be wise to, but we'd need to do some normalization.
559 } elseif ( $dbk === '' ) {
560 // Don't record self links - [[#Foo]]
563 if ( !isset( $this->mLinks
[$ns] ) ) {
564 $this->mLinks
[$ns] = [];
566 if ( is_null( $id ) ) {
567 $id = $title->getArticleID();
569 $this->mLinks
[$ns][$dbk] = $id;
573 * Register a file dependency for this output
574 * @param string $name Title dbKey
575 * @param string $timestamp MW timestamp of file creation (or false if non-existing)
576 * @param string $sha1 Base 36 SHA-1 of file (or false if non-existing)
579 public function addImage( $name, $timestamp = null, $sha1 = null ) {
580 $this->mImages
[$name] = 1;
581 if ( $timestamp !== null && $sha1 !== null ) {
582 $this->mFileSearchOptions
[$name] = [ 'time' => $timestamp, 'sha1' => $sha1 ];
587 * Register a template dependency for this output
588 * @param Title $title
589 * @param int $page_id
593 public function addTemplate( $title, $page_id, $rev_id ) {
594 $ns = $title->getNamespace();
595 $dbk = $title->getDBkey();
596 if ( !isset( $this->mTemplates
[$ns] ) ) {
597 $this->mTemplates
[$ns] = [];
599 $this->mTemplates
[$ns][$dbk] = $page_id;
600 if ( !isset( $this->mTemplateIds
[$ns] ) ) {
601 $this->mTemplateIds
[$ns] = [];
603 $this->mTemplateIds
[$ns][$dbk] = $rev_id; // For versioning
607 * @param Title $title Title object, must be an interwiki link
608 * @throws MWException If given invalid input
610 public function addInterwikiLink( $title ) {
611 if ( !$title->isExternal() ) {
612 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
614 $prefix = $title->getInterwiki();
615 if ( !isset( $this->mInterwikiLinks
[$prefix] ) ) {
616 $this->mInterwikiLinks
[$prefix] = [];
618 $this->mInterwikiLinks
[$prefix][$title->getDBkey()] = 1;
622 * Add some text to the "<head>".
623 * If $tag is set, the section with that tag will only be included once
625 * @param string $section
626 * @param string|bool $tag
628 public function addHeadItem( $section, $tag = false ) {
629 if ( $tag !== false ) {
630 $this->mHeadItems
[$tag] = $section;
632 $this->mHeadItems
[] = $section;
636 public function addModules( $modules ) {
637 $this->mModules
= array_merge( $this->mModules
, (array)$modules );
640 public function addModuleScripts( $modules ) {
641 $this->mModuleScripts
= array_merge( $this->mModuleScripts
, (array)$modules );
644 public function addModuleStyles( $modules ) {
645 $this->mModuleStyles
= array_merge( $this->mModuleStyles
, (array)$modules );
649 * Add one or more variables to be set in mw.config in JavaScript.
651 * @param string|array $keys Key or array of key/value pairs.
652 * @param mixed $value [optional] Value of the configuration variable.
655 public function addJsConfigVars( $keys, $value = null ) {
656 if ( is_array( $keys ) ) {
657 foreach ( $keys as $key => $value ) {
658 $this->mJsConfigVars
[$key] = $value;
663 $this->mJsConfigVars
[$keys] = $value;
667 * Copy items from the OutputPage object into this one
669 * @param OutputPage $out
671 public function addOutputPageMetadata( OutputPage
$out ) {
672 $this->addModules( $out->getModules() );
673 $this->addModuleScripts( $out->getModuleScripts() );
674 $this->addModuleStyles( $out->getModuleStyles() );
675 $this->addJsConfigVars( $out->getJsConfigVars() );
677 $this->mHeadItems
= array_merge( $this->mHeadItems
, $out->getHeadItemsArray() );
678 $this->mPreventClickjacking
= $this->mPreventClickjacking ||
$out->getPreventClickjacking();
682 * Add a tracking category, getting the title from a system message,
683 * or print a debug message if the title is invalid.
685 * Any message used with this function should be registered so it will
686 * show up on Special:TrackingCategories. Core messages should be added
687 * to SpecialTrackingCategories::$coreTrackingCategories, and extensions
688 * should add to "TrackingCategories" in their extension.json.
690 * @param string $msg Message key
691 * @param Title $title title of the page which is being tracked
692 * @return bool Whether the addition was successful
695 public function addTrackingCategory( $msg, $title ) {
696 if ( $title->getNamespace() === NS_SPECIAL
) {
697 wfDebug( __METHOD__
. ": Not adding tracking category $msg to special page!\n" );
701 // Important to parse with correct title (bug 31469)
702 $cat = wfMessage( $msg )
704 ->inContentLanguage()
707 # Allow tracking categories to be disabled by setting them to "-"
708 if ( $cat === '-' ) {
712 $containerCategory = Title
::makeTitleSafe( NS_CATEGORY
, $cat );
713 if ( $containerCategory ) {
714 $this->addCategory( $containerCategory->getDBkey(), $this->getProperty( 'defaultsort' ) ?
: '' );
717 wfDebug( __METHOD__
. ": [[MediaWiki:$msg]] is not a valid title!\n" );
723 * Override the title to be used for display
725 * @note this is assumed to have been validated
726 * (check equal normalisation, etc.)
728 * @note this is expected to be safe HTML,
729 * ready to be served to the client.
731 * @param string $text Desired title text
733 public function setDisplayTitle( $text ) {
734 $this->setTitleText( $text );
735 $this->setProperty( 'displaytitle', $text );
739 * Get the title to be used for display.
741 * As per the contract of setDisplayTitle(), this is safe HTML,
742 * ready to be served to the client.
744 * @return string HTML
746 public function getDisplayTitle() {
747 $t = $this->getTitleText();
755 * Fairly generic flag setter thingy.
756 * @param string $flag
758 public function setFlag( $flag ) {
759 $this->mFlags
[$flag] = true;
762 public function getFlag( $flag ) {
763 return isset( $this->mFlags
[$flag] );
767 * Set a property to be stored in the page_props database table.
769 * page_props is a key value store indexed by the page ID. This allows
770 * the parser to set a property on a page which can then be quickly
771 * retrieved given the page ID or via a DB join when given the page
774 * Since 1.23, page_props are also indexed by numeric value, to allow
775 * for efficient "top k" queries of pages wrt a given property.
777 * setProperty() is thus used to propagate properties from the parsed
778 * page to request contexts other than a page view of the currently parsed
781 * Some applications examples:
783 * * To implement hidden categories, hiding pages from category listings
784 * by storing a property.
786 * * Overriding the displayed article title.
787 * @see ParserOutput::setDisplayTitle()
789 * * To implement image tagging, for example displaying an icon on an
790 * image thumbnail to indicate that it is listed for deletion on
792 * This is not actually implemented, yet but would be pretty cool.
794 * @note Do not use setProperty() to set a property which is only used
795 * in a context where the ParserOutput object itself is already available,
796 * for example a normal page view. There is no need to save such a property
797 * in the database since the text is already parsed. You can just hook
798 * OutputPageParserOutput and get your data out of the ParserOutput object.
800 * If you are writing an extension where you want to set a property in the
801 * parser which is used by an OutputPageParserOutput hook, you have to
802 * associate the extension data directly with the ParserOutput object.
803 * Since MediaWiki 1.21, you can use setExtensionData() to do this:
807 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
810 * And then later, in OutputPageParserOutput or similar:
814 * $output->getExtensionData( 'my_ext_foo' );
817 * In MediaWiki 1.20 and older, you have to use a custom member variable
818 * within the ParserOutput object:
822 * $parser->getOutput()->my_ext_foo = '...';
826 public function setProperty( $name, $value ) {
827 $this->mProperties
[$name] = $value;
831 * @param string $name The property name to look up.
833 * @return mixed|bool The value previously set using setProperty(). False if null or no value
834 * was set for the given property name.
836 * @note You need to use getProperties() to check for boolean and null properties.
838 public function getProperty( $name ) {
839 return isset( $this->mProperties
[$name] ) ?
$this->mProperties
[$name] : false;
842 public function unsetProperty( $name ) {
843 unset( $this->mProperties
[$name] );
846 public function getProperties() {
847 if ( !isset( $this->mProperties
) ) {
848 $this->mProperties
= [];
850 return $this->mProperties
;
854 * Returns the options from its ParserOptions which have been taken
855 * into account to produce this output or false if not available.
858 public function getUsedOptions() {
859 if ( !isset( $this->mAccessedOptions
) ) {
862 return array_keys( $this->mAccessedOptions
);
866 * Tags a parser option for use in the cache key for this parser output.
867 * Registered as a watcher at ParserOptions::registerWatcher() by Parser::clearState().
868 * The information gathered here is available via getUsedOptions(),
869 * and is used by ParserCache::save().
871 * @see ParserCache::getKey
872 * @see ParserCache::save
873 * @see ParserOptions::addExtraKey
874 * @see ParserOptions::optionsHash
875 * @param string $option
877 public function recordOption( $option ) {
878 $this->mAccessedOptions
[$option] = true;
882 * Attaches arbitrary data to this ParserObject. This can be used to store some information in
883 * the ParserOutput object for later use during page output. The data will be cached along with
884 * the ParserOutput object, but unlike data set using setProperty(), it is not recorded in the
887 * This method is provided to overcome the unsafe practice of attaching extra information to a
888 * ParserObject by directly assigning member variables.
890 * To use setExtensionData() to pass extension information from a hook inside the parser to a
891 * hook in the page output, use this in the parser hook:
895 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
898 * And then later, in OutputPageParserOutput or similar:
902 * $output->getExtensionData( 'my_ext_foo' );
905 * In MediaWiki 1.20 and older, you have to use a custom member variable
906 * within the ParserOutput object:
910 * $parser->getOutput()->my_ext_foo = '...';
915 * @param string $key The key for accessing the data. Extensions should take care to avoid
916 * conflicts in naming keys. It is suggested to use the extension's name as a prefix.
918 * @param mixed $value The value to set. Setting a value to null is equivalent to removing
921 public function setExtensionData( $key, $value ) {
922 if ( $value === null ) {
923 unset( $this->mExtensionData
[$key] );
925 $this->mExtensionData
[$key] = $value;
930 * Gets extensions data previously attached to this ParserOutput using setExtensionData().
931 * Typically, such data would be set while parsing the page, e.g. by a parser function.
935 * @param string $key The key to look up.
937 * @return mixed|null The value previously set for the given key using setExtensionData()
938 * or null if no value was set for this key.
940 public function getExtensionData( $key ) {
941 if ( isset( $this->mExtensionData
[$key] ) ) {
942 return $this->mExtensionData
[$key];
948 private static function getTimes( $clock = null ) {
950 if ( !$clock ||
$clock === 'wall' ) {
951 $ret['wall'] = microtime( true );
953 if ( !$clock ||
$clock === 'cpu' ) {
956 $ret['cpu'] = $ru['ru_utime.tv_sec'] +
$ru['ru_utime.tv_usec'] / 1e6
;
957 $ret['cpu'] +
= $ru['ru_stime.tv_sec'] +
$ru['ru_stime.tv_usec'] / 1e6
;
964 * Resets the parse start timestamps for future calls to getTimeSinceStart()
967 public function resetParseStartTime() {
968 $this->mParseStartTime
= self
::getTimes();
972 * Returns the time since resetParseStartTime() was last called
974 * Clocks available are:
975 * - wall: Wall clock time
976 * - cpu: CPU time (requires getrusage)
979 * @param string $clock
982 public function getTimeSinceStart( $clock ) {
983 if ( !isset( $this->mParseStartTime
[$clock] ) ) {
987 $end = self
::getTimes( $clock );
988 return $end[$clock] - $this->mParseStartTime
[$clock];
992 * Sets parser limit report data for a key
994 * If $value consist of a list of two floats, it will be interpreted as
995 * (actual value, maximum allowed value). The presence of a "-" in $key will cause
996 * the first part of the key to be interpreted as a namespace.
999 * @param string $key Data key
1000 * @param mixed $value Data value One of (float, string, bool, JSON serializable array)
1002 public function setLimitReportData( $key, $value ) {
1003 if ( is_array( $value ) ) {
1004 if ( array_keys( $value ) === [ 0, 1 ]
1005 && is_numeric( $value[0] )
1006 && is_numeric( $value[1] )
1008 $data = [ 'value' => $value[0], 'limit' => $value[1] ];
1016 if ( strpos( $key, '-' ) ) {
1017 list( $ns, $name ) = explode( '-', $key, 2 );
1018 $this->mLimitReportData
[$ns][$name] = $data;
1020 $this->mLimitReportData
[$key] = $data;
1025 * Check whether the cache TTL was lowered due to dynamic content
1027 * When content is determined by more than hard state (e.g. page edits),
1028 * such as template/file transclusions based on the current timestamp or
1029 * extension tags that generate lists based on queries, this return true.
1034 public function hasDynamicContent() {
1035 global $wgParserCacheExpireTime;
1037 return $this->getCacheExpiry() < $wgParserCacheExpireTime;
1041 * Get or set the prevent-clickjacking flag
1044 * @param bool|null $flag New flag value, or null to leave it unchanged
1045 * @return bool Old flag value
1047 public function preventClickjacking( $flag = null ) {
1048 return wfSetVar( $this->mPreventClickjacking
, $flag );
1052 * Lower the runtime adaptive TTL to at most this value
1054 * @param integer $ttl
1057 public function updateRuntimeAdaptiveExpiry( $ttl ) {
1058 $this->mMaxAdaptiveExpiry
= min( $ttl, $this->mMaxAdaptiveExpiry
);
1059 $this->updateCacheExpiry( $ttl );
1063 * Call this when parsing is done to lower the TTL based on low parse times
1067 public function finalizeAdaptiveCacheExpiry() {
1068 if ( is_infinite( $this->mMaxAdaptiveExpiry
) ) {
1072 $runtime = $this->getTimeSinceStart( 'wall' );
1073 if ( is_float( $runtime ) ) {
1074 $slope = ( self
::SLOW_AR_TTL
- self
::FAST_AR_TTL
)
1075 / ( self
::PARSE_SLOW_SEC
- self
::PARSE_FAST_SEC
);
1076 // SLOW_AR_TTL = PARSE_SLOW_SEC * $slope + $point
1077 $point = self
::SLOW_AR_TTL
- self
::PARSE_SLOW_SEC
* $slope;
1080 max( $slope * $runtime +
$point, self
::MIN_AR_TTL
),
1081 $this->mMaxAdaptiveExpiry
1083 $this->updateCacheExpiry( $adaptiveTTL );
1087 public function __sleep() {
1089 array_keys( get_object_vars( $this ) ),
1090 [ 'mParseStartTime' ]