MediaWiki  master
ParserOutput.php
Go to the documentation of this file.
1 <?php
2 
25 class ParserOutput extends CacheTime {
33 
38 
42  public $mText = null;
43 
49 
53  public $mCategories;
54 
58  public $mIndicators = [];
59 
63  public $mTitleText;
64 
70  public $mLinks = [];
71 
76  public $mTemplates = [];
77 
82  public $mTemplateIds = [];
83 
87  public $mImages = [];
88 
92  public $mFileSearchOptions = [];
93 
97  public $mExternalLinks = [];
98 
103  public $mInterwikiLinks = [];
104 
108  public $mNewSection = false;
109 
113  public $mHideNewSection = false;
114 
118  public $mNoGallery = false;
119 
123  public $mHeadItems = [];
124 
128  public $mModules = [];
129 
133  public $mModuleStyles = [];
134 
138  public $mJsConfigVars = [];
139 
143  public $mOutputHooks = [];
144 
149  public $mWarnings = [];
150 
154  public $mSections = [];
155 
159  public $mProperties = [];
160 
164  public $mTOCHTML = '';
165 
169  public $mTimestamp;
170 
174  public $mEnableOOUI = false;
175 
179  private $mIndexPolicy = '';
180 
184  private $mAccessedOptions = [];
185 
189  private $mExtensionData = [];
190 
194  private $mLimitReportData = [];
195 
197  private $mLimitReportJSData = [];
198 
202  private $mParseStartTime = [];
203 
207  private $mPreventClickjacking = false;
208 
212  private $mFlags = [];
213 
215  private const SPECULATIVE_FIELDS = [
216  'speculativePageIdUsed',
217  'mSpeculativeRevId',
218  'revisionTimestampUsed'
219  ];
220 
222  private $mSpeculativeRevId;
227 
230 
234  private $mWrapperDivClasses = [];
235 
237  private $mMaxAdaptiveExpiry = INF;
238 
240  '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#s';
241 
242  // finalizeAdaptiveCacheExpiry() uses TTL = MAX( m * PARSE_TIME + b, MIN_AR_TTL)
243  // Current values imply that m=3933.333333 and b=-333.333333
244  // See https://www.nngroup.com/articles/website-response-times/
245  const PARSE_FAST_SEC = 0.100; // perceived "fast" page parse
246  const PARSE_SLOW_SEC = 1.0; // perceived "slow" page parse
247  const FAST_AR_TTL = 60; // adaptive TTL for "fast" pages
248  const SLOW_AR_TTL = 3600; // adaptive TTL for "slow" pages
249  const MIN_AR_TTL = 15; // min adaptive TTL (for sanity, pool counter, and edit stashing)
250 
260  public function __construct( $text = '', $languageLinks = [], $categoryLinks = [],
261  $unused = false, $titletext = ''
262  ) {
263  $this->mText = $text;
264  $this->mLanguageLinks = $languageLinks;
265  $this->mCategories = $categoryLinks;
266  $this->mTitleText = $titletext;
267  }
268 
279  public function hasText() {
280  return ( $this->mText !== null );
281  }
282 
291  public function getRawText() {
292  if ( $this->mText === null ) {
293  throw new LogicException( 'This ParserOutput contains no text!' );
294  }
295 
296  return $this->mText;
297  }
298 
324  public function getText( $options = [] ) {
325  $options += [
326  'allowTOC' => true,
327  'enableSectionEditLinks' => true,
328  'unwrap' => false,
329  'deduplicateStyles' => true,
330  'wrapperDivClass' => $this->getWrapperDivClass(),
331  ];
332  $text = $this->getRawText();
333 
334  Hooks::runWithoutAbort( 'ParserOutputPostCacheTransform', [ $this, &$text, &$options ] );
335 
336  if ( $options['wrapperDivClass'] !== '' && !$options['unwrap'] ) {
337  $text = Html::rawElement( 'div', [ 'class' => $options['wrapperDivClass'] ], $text );
338  }
339 
340  if ( $options['enableSectionEditLinks'] ) {
341  $text = preg_replace_callback(
342  self::EDITSECTION_REGEX,
343  function ( $m ) {
344  $editsectionPage = Title::newFromText( htmlspecialchars_decode( $m[1] ) );
345  $editsectionSection = htmlspecialchars_decode( $m[2] );
346  $editsectionContent = isset( $m[4] ) ? Sanitizer::decodeCharReferences( $m[3] ) : null;
347 
348  if ( !is_object( $editsectionPage ) ) {
349  throw new MWException( "Bad parser output text." );
350  }
351 
353  return $context->getSkin()->doEditSectionLink(
354  $editsectionPage,
355  $editsectionSection,
356  $editsectionContent,
357  $context->getLanguage()
358  );
359  },
360  $text
361  );
362  } else {
363  $text = preg_replace( self::EDITSECTION_REGEX, '', $text );
364  }
365 
366  if ( $options['allowTOC'] ) {
367  $text = str_replace( [ Parser::TOC_START, Parser::TOC_END ], '', $text );
368  } else {
369  $text = preg_replace(
370  '#' . preg_quote( Parser::TOC_START, '#' ) . '.*?' . preg_quote( Parser::TOC_END, '#' ) . '#s',
371  '',
372  $text
373  );
374  }
375 
376  if ( $options['deduplicateStyles'] ) {
377  $seen = [];
378  $text = preg_replace_callback(
379  '#<style\s+([^>]*data-mw-deduplicate\s*=[^>]*)>.*?</style>#s',
380  function ( $m ) use ( &$seen ) {
381  $attr = Sanitizer::decodeTagAttributes( $m[1] );
382  if ( !isset( $attr['data-mw-deduplicate'] ) ) {
383  return $m[0];
384  }
385 
386  $key = $attr['data-mw-deduplicate'];
387  if ( !isset( $seen[$key] ) ) {
388  $seen[$key] = true;
389  return $m[0];
390  }
391 
392  // We were going to use an empty <style> here, but there
393  // was concern that would be too much overhead for browsers.
394  // So let's hope a <link> with a non-standard rel and href isn't
395  // going to be misinterpreted or mangled by any subsequent processing.
396  return Html::element( 'link', [
397  'rel' => 'mw-deduplicated-inline-style',
398  'href' => "mw-data:" . wfUrlencode( $key ),
399  ] );
400  },
401  $text
402  );
403  }
404 
405  // Hydrate slot section header placeholders generated by RevisionRenderer.
406  $text = preg_replace_callback(
407  '#<mw:slotheader>(.*?)</mw:slotheader>#',
408  function ( $m ) {
409  $role = htmlspecialchars_decode( $m[1] );
410  // TODO: map to message, using the interface language. Set lang="xyz" accordingly.
411  $headerText = $role;
412  return $headerText;
413  },
414  $text
415  );
416  return $text;
417  }
418 
424  public function addWrapperDivClass( $class ) {
425  $this->mWrapperDivClasses[$class] = true;
426  }
427 
432  public function clearWrapperDivClass() {
433  $this->mWrapperDivClasses = [];
434  }
435 
443  public function getWrapperDivClass() {
444  return implode( ' ', array_keys( $this->mWrapperDivClasses ) );
445  }
446 
451  public function setSpeculativeRevIdUsed( $id ) {
452  $this->mSpeculativeRevId = $id;
453  }
454 
459  public function getSpeculativeRevIdUsed() {
461  }
462 
467  public function setSpeculativePageIdUsed( $id ) {
468  $this->speculativePageIdUsed = $id;
469  }
470 
475  public function getSpeculativePageIdUsed() {
477  }
478 
483  public function setRevisionTimestampUsed( $timestamp ) {
484  $this->revisionTimestampUsed = $timestamp;
485  }
486 
491  public function getRevisionTimestampUsed() {
493  }
494 
499  public function setRevisionUsedSha1Base36( $hash ) {
500  if ( $hash === null ) {
501  return; // e.g. RevisionRecord::getSha1() returned null
502  }
503 
504  if (
505  $this->revisionUsedSha1Base36 !== null &&
506  $this->revisionUsedSha1Base36 !== $hash
507  ) {
508  $this->revisionUsedSha1Base36 = ''; // mismatched
509  } else {
510  $this->revisionUsedSha1Base36 = $hash;
511  }
512  }
513 
518  public function getRevisionUsedSha1Base36() {
520  }
521 
522  public function &getLanguageLinks() {
523  return $this->mLanguageLinks;
524  }
525 
526  public function getInterwikiLinks() {
527  return $this->mInterwikiLinks;
528  }
529 
530  public function getCategoryLinks() {
531  return array_keys( $this->mCategories );
532  }
533 
534  public function &getCategories() {
535  return $this->mCategories;
536  }
537 
542  public function getIndicators() {
543  return $this->mIndicators;
544  }
545 
546  public function getTitleText() {
547  return $this->mTitleText;
548  }
549 
550  public function getSections() {
551  return $this->mSections;
552  }
553 
554  public function &getLinks() {
555  return $this->mLinks;
556  }
557 
558  public function &getTemplates() {
559  return $this->mTemplates;
560  }
561 
562  public function &getTemplateIds() {
563  return $this->mTemplateIds;
564  }
565 
566  public function &getImages() {
567  return $this->mImages;
568  }
569 
570  public function &getFileSearchOptions() {
572  }
573 
574  public function &getExternalLinks() {
575  return $this->mExternalLinks;
576  }
577 
578  public function setNoGallery( $value ) {
579  $this->mNoGallery = (bool)$value;
580  }
581 
582  public function getNoGallery() {
583  return $this->mNoGallery;
584  }
585 
586  public function getHeadItems() {
587  return $this->mHeadItems;
588  }
589 
590  public function getModules() {
591  return $this->mModules;
592  }
593 
594  public function getModuleStyles() {
595  return $this->mModuleStyles;
596  }
597 
602  public function getJsConfigVars() {
603  return $this->mJsConfigVars;
604  }
605 
606  public function getOutputHooks() {
607  return (array)$this->mOutputHooks;
608  }
609 
610  public function getWarnings() {
611  return array_keys( $this->mWarnings );
612  }
613 
614  public function getIndexPolicy() {
615  return $this->mIndexPolicy;
616  }
617 
618  public function getTOCHTML() {
619  return $this->mTOCHTML;
620  }
621 
625  public function getTimestamp() {
626  return $this->mTimestamp;
627  }
628 
629  public function getLimitReportData() {
631  }
632 
633  public function getLimitReportJSData() {
635  }
636 
637  public function getEnableOOUI() {
638  return $this->mEnableOOUI;
639  }
640 
641  public function setText( $text ) {
642  return wfSetVar( $this->mText, $text );
643  }
644 
645  public function setLanguageLinks( $ll ) {
646  return wfSetVar( $this->mLanguageLinks, $ll );
647  }
648 
649  public function setCategoryLinks( $cl ) {
650  return wfSetVar( $this->mCategories, $cl );
651  }
652 
653  public function setTitleText( $t ) {
654  return wfSetVar( $this->mTitleText, $t );
655  }
656 
657  public function setSections( $toc ) {
658  return wfSetVar( $this->mSections, $toc );
659  }
660 
661  public function setIndexPolicy( $policy ) {
662  return wfSetVar( $this->mIndexPolicy, $policy );
663  }
664 
665  public function setTOCHTML( $tochtml ) {
666  return wfSetVar( $this->mTOCHTML, $tochtml );
667  }
668 
669  public function setTimestamp( $timestamp ) {
670  return wfSetVar( $this->mTimestamp, $timestamp );
671  }
672 
673  public function addCategory( $c, $sort ) {
674  $this->mCategories[$c] = $sort;
675  }
676 
682  public function setIndicator( $id, $content ) {
683  $this->mIndicators[$id] = $content;
684  }
685 
693  public function setEnableOOUI( $enable = false ) {
694  $this->mEnableOOUI = $enable;
695  }
696 
697  public function addLanguageLink( $t ) {
698  $this->mLanguageLinks[] = $t;
699  }
700 
701  public function addWarning( $s ) {
702  $this->mWarnings[$s] = 1;
703  }
704 
705  public function addOutputHook( $hook, $data = false ) {
706  $this->mOutputHooks[] = [ $hook, $data ];
707  }
708 
709  public function setNewSection( $value ) {
710  $this->mNewSection = (bool)$value;
711  }
712 
713  public function hideNewSection( $value ) {
714  $this->mHideNewSection = (bool)$value;
715  }
716 
717  public function getHideNewSection() {
718  return (bool)$this->mHideNewSection;
719  }
720 
721  public function getNewSection() {
722  return (bool)$this->mNewSection;
723  }
724 
732  public static function isLinkInternal( $internal, $url ) {
733  return (bool)preg_match( '/^' .
734  # If server is proto relative, check also for http/https links
735  ( substr( $internal, 0, 2 ) === '//' ? '(?:https?:)?' : '' ) .
736  preg_quote( $internal, '/' ) .
737  # check for query/path/anchor or end of link in each case
738  '(?:[\?\/\#]|$)/i',
739  $url
740  );
741  }
742 
743  public function addExternalLink( $url ) {
744  # We don't register links pointing to our own server, unless... :-)
746 
747  # Replace unnecessary URL escape codes with the referenced character
748  # This prevents spammers from hiding links from the filters
749  $url = Parser::normalizeLinkUrl( $url );
750 
751  $registerExternalLink = true;
752  if ( !$wgRegisterInternalExternals ) {
753  $registerExternalLink = !self::isLinkInternal( $wgServer, $url );
754  }
755  if ( $registerExternalLink ) {
756  $this->mExternalLinks[$url] = 1;
757  }
758  }
759 
766  public function addLink( Title $title, $id = null ) {
767  if ( $title->isExternal() ) {
768  // Don't record interwikis in pagelinks
769  $this->addInterwikiLink( $title );
770  return;
771  }
772  $ns = $title->getNamespace();
773  $dbk = $title->getDBkey();
774  if ( $ns == NS_MEDIA ) {
775  // Normalize this pseudo-alias if it makes it down here...
776  $ns = NS_FILE;
777  } elseif ( $ns == NS_SPECIAL ) {
778  // We don't record Special: links currently
779  // It might actually be wise to, but we'd need to do some normalization.
780  return;
781  } elseif ( $dbk === '' ) {
782  // Don't record self links - [[#Foo]]
783  return;
784  }
785  if ( !isset( $this->mLinks[$ns] ) ) {
786  $this->mLinks[$ns] = [];
787  }
788  if ( is_null( $id ) ) {
789  $id = $title->getArticleID();
790  }
791  $this->mLinks[$ns][$dbk] = $id;
792  }
793 
800  public function addImage( $name, $timestamp = null, $sha1 = null ) {
801  $this->mImages[$name] = 1;
802  if ( $timestamp !== null && $sha1 !== null ) {
803  $this->mFileSearchOptions[$name] = [ 'time' => $timestamp, 'sha1' => $sha1 ];
804  }
805  }
806 
813  public function addTemplate( $title, $page_id, $rev_id ) {
814  $ns = $title->getNamespace();
815  $dbk = $title->getDBkey();
816  if ( !isset( $this->mTemplates[$ns] ) ) {
817  $this->mTemplates[$ns] = [];
818  }
819  $this->mTemplates[$ns][$dbk] = $page_id;
820  if ( !isset( $this->mTemplateIds[$ns] ) ) {
821  $this->mTemplateIds[$ns] = [];
822  }
823  $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
824  }
825 
830  public function addInterwikiLink( $title ) {
831  if ( !$title->isExternal() ) {
832  throw new MWException( 'Non-interwiki link passed, internal parser error.' );
833  }
834  $prefix = $title->getInterwiki();
835  if ( !isset( $this->mInterwikiLinks[$prefix] ) ) {
836  $this->mInterwikiLinks[$prefix] = [];
837  }
838  $this->mInterwikiLinks[$prefix][$title->getDBkey()] = 1;
839  }
840 
848  public function addHeadItem( $section, $tag = false ) {
849  if ( $tag !== false ) {
850  $this->mHeadItems[$tag] = $section;
851  } else {
852  $this->mHeadItems[] = $section;
853  }
854  }
855 
859  public function addModules( $modules ) {
860  $this->mModules = array_merge( $this->mModules, (array)$modules );
861  }
862 
866  public function addModuleStyles( $modules ) {
867  $this->mModuleStyles = array_merge( $this->mModuleStyles, (array)$modules );
868  }
869 
877  public function addJsConfigVars( $keys, $value = null ) {
878  if ( is_array( $keys ) ) {
879  foreach ( $keys as $key => $value ) {
880  $this->mJsConfigVars[$key] = $value;
881  }
882  return;
883  }
884 
885  $this->mJsConfigVars[$keys] = $value;
886  }
887 
893  public function addOutputPageMetadata( OutputPage $out ) {
894  $this->addModules( $out->getModules() );
895  $this->addModuleStyles( $out->getModuleStyles() );
896  $this->addJsConfigVars( $out->getJsConfigVars() );
897 
898  $this->mHeadItems = array_merge( $this->mHeadItems, $out->getHeadItemsArray() );
899  $this->mPreventClickjacking = $this->mPreventClickjacking || $out->getPreventClickjacking();
900  }
901 
918  public function addTrackingCategory( $msg, $title ) {
919  if ( $title->isSpecialPage() ) {
920  wfDebug( __METHOD__ . ": Not adding tracking category $msg to special page!\n" );
921  return false;
922  }
923 
924  // Important to parse with correct title (T33469)
925  $cat = wfMessage( $msg )
926  ->title( $title )
927  ->inContentLanguage()
928  ->text();
929 
930  # Allow tracking categories to be disabled by setting them to "-"
931  if ( $cat === '-' ) {
932  return false;
933  }
934 
935  $containerCategory = Title::makeTitleSafe( NS_CATEGORY, $cat );
936  if ( $containerCategory ) {
937  $this->addCategory( $containerCategory->getDBkey(), $this->getProperty( 'defaultsort' ) ?: '' );
938  return true;
939  } else {
940  wfDebug( __METHOD__ . ": [[MediaWiki:$msg]] is not a valid title!\n" );
941  return false;
942  }
943  }
944 
956  public function setDisplayTitle( $text ) {
957  $this->setTitleText( $text );
958  $this->setProperty( 'displaytitle', $text );
959  }
960 
969  public function getDisplayTitle() {
970  $t = $this->getTitleText();
971  if ( $t === '' ) {
972  return false;
973  }
974  return $t;
975  }
976 
982  public function setFlag( $flag ) {
983  $this->mFlags[$flag] = true;
984  }
985 
990  public function getFlag( $flag ) {
991  return isset( $this->mFlags[$flag] );
992  }
993 
998  public function getAllFlags() {
999  return array_keys( $this->mFlags );
1000  }
1001 
1062  public function setProperty( $name, $value ) {
1063  $this->mProperties[$name] = $value;
1064  }
1065 
1074  public function getProperty( $name ) {
1075  return $this->mProperties[$name] ?? false;
1076  }
1077 
1078  public function unsetProperty( $name ) {
1079  unset( $this->mProperties[$name] );
1080  }
1081 
1082  public function getProperties() {
1083  if ( !isset( $this->mProperties ) ) {
1084  $this->mProperties = [];
1085  }
1086  return $this->mProperties;
1087  }
1088 
1094  public function getUsedOptions() {
1095  if ( !isset( $this->mAccessedOptions ) ) {
1096  return [];
1097  }
1098  return array_keys( $this->mAccessedOptions );
1099  }
1100 
1113  public function recordOption( $option ) {
1114  $this->mAccessedOptions[$option] = true;
1115  }
1116 
1157  public function setExtensionData( $key, $value ) {
1158  if ( $value === null ) {
1159  unset( $this->mExtensionData[$key] );
1160  } else {
1161  $this->mExtensionData[$key] = $value;
1162  }
1163  }
1164 
1176  public function getExtensionData( $key ) {
1177  return $this->mExtensionData[$key] ?? null;
1178  }
1179 
1180  private static function getTimes( $clock = null ) {
1181  $ret = [];
1182  if ( !$clock || $clock === 'wall' ) {
1183  $ret['wall'] = microtime( true );
1184  }
1185  if ( !$clock || $clock === 'cpu' ) {
1186  $ru = wfGetRusage();
1187  if ( $ru ) {
1188  $ret['cpu'] = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
1189  $ret['cpu'] += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
1190  }
1191  }
1192  return $ret;
1193  }
1194 
1199  public function resetParseStartTime() {
1200  $this->mParseStartTime = self::getTimes();
1201  }
1202 
1214  public function getTimeSinceStart( $clock ) {
1215  if ( !isset( $this->mParseStartTime[$clock] ) ) {
1216  return null;
1217  }
1218 
1219  $end = self::getTimes( $clock );
1220  return $end[$clock] - $this->mParseStartTime[$clock];
1221  }
1222 
1242  public function setLimitReportData( $key, $value ) {
1243  $this->mLimitReportData[$key] = $value;
1244 
1245  if ( is_array( $value ) ) {
1246  if ( array_keys( $value ) === [ 0, 1 ]
1247  && is_numeric( $value[0] )
1248  && is_numeric( $value[1] )
1249  ) {
1250  $data = [ 'value' => $value[0], 'limit' => $value[1] ];
1251  } else {
1252  $data = $value;
1253  }
1254  } else {
1255  $data = $value;
1256  }
1257 
1258  if ( strpos( $key, '-' ) ) {
1259  list( $ns, $name ) = explode( '-', $key, 2 );
1260  $this->mLimitReportJSData[$ns][$name] = $data;
1261  } else {
1262  $this->mLimitReportJSData[$key] = $data;
1263  }
1264  }
1265 
1276  public function hasDynamicContent() {
1277  global $wgParserCacheExpireTime;
1278 
1279  return $this->getCacheExpiry() < $wgParserCacheExpireTime;
1280  }
1281 
1289  public function preventClickjacking( $flag = null ) {
1290  return wfSetVar( $this->mPreventClickjacking, $flag );
1291  }
1292 
1299  public function updateRuntimeAdaptiveExpiry( $ttl ) {
1300  $this->mMaxAdaptiveExpiry = min( $ttl, $this->mMaxAdaptiveExpiry );
1301  $this->updateCacheExpiry( $ttl );
1302  }
1303 
1309  public function finalizeAdaptiveCacheExpiry() {
1310  if ( is_infinite( $this->mMaxAdaptiveExpiry ) ) {
1311  return; // not set
1312  }
1313 
1314  $runtime = $this->getTimeSinceStart( 'wall' );
1315  if ( is_float( $runtime ) ) {
1316  $slope = ( self::SLOW_AR_TTL - self::FAST_AR_TTL )
1317  / ( self::PARSE_SLOW_SEC - self::PARSE_FAST_SEC );
1318  // SLOW_AR_TTL = PARSE_SLOW_SEC * $slope + $point
1319  $point = self::SLOW_AR_TTL - self::PARSE_SLOW_SEC * $slope;
1320 
1321  $adaptiveTTL = min(
1322  max( $slope * $runtime + $point, self::MIN_AR_TTL ),
1323  $this->mMaxAdaptiveExpiry
1324  );
1325  $this->updateCacheExpiry( $adaptiveTTL );
1326  }
1327  }
1328 
1329  public function __sleep() {
1330  return array_filter( array_keys( get_object_vars( $this ) ),
1331  function ( $field ) {
1332  if ( $field === 'mParseStartTime' ) {
1333  return false;
1334  } elseif ( strpos( $field, "\0" ) !== false ) {
1335  // Unserializing unknown private fields in HHVM causes
1336  // member variables with nulls in their names (T229366)
1337  return false;
1338  } else {
1339  return true;
1340  }
1341  }
1342  );
1343  }
1344 
1353  $this->mOutputHooks = self::mergeList( $this->mOutputHooks, $source->getOutputHooks() );
1354  $this->mWarnings = self::mergeMap( $this->mWarnings, $source->mWarnings ); // don't use getter
1355  $this->mTimestamp = $this->useMaxValue( $this->mTimestamp, $source->getTimestamp() );
1356 
1357  foreach ( self::SPECULATIVE_FIELDS as $field ) {
1358  if ( $this->$field && $source->$field && $this->$field !== $source->$field ) {
1359  wfLogWarning( __METHOD__ . ": inconsistent '$field' properties!" );
1360  }
1361  $this->$field = $this->useMaxValue( $this->$field, $source->$field );
1362  }
1363 
1364  $this->mParseStartTime = $this->useEachMinValue(
1365  $this->mParseStartTime,
1366  $source->mParseStartTime
1367  );
1368 
1369  $this->mFlags = self::mergeMap( $this->mFlags, $source->mFlags );
1370  $this->mAccessedOptions = self::mergeMap( $this->mAccessedOptions, $source->mAccessedOptions );
1371 
1372  // TODO: maintain per-slot limit reports!
1373  if ( empty( $this->mLimitReportData ) ) {
1374  $this->mLimitReportData = $source->mLimitReportData;
1375  }
1376  if ( empty( $this->mLimitReportJSData ) ) {
1377  $this->mLimitReportJSData = $source->mLimitReportJSData;
1378  }
1379  }
1380 
1389  // HTML and HTTP
1390  $this->mHeadItems = self::mergeMixedList( $this->mHeadItems, $source->getHeadItems() );
1391  $this->mModules = self::mergeList( $this->mModules, $source->getModules() );
1392  $this->mModuleStyles = self::mergeList( $this->mModuleStyles, $source->getModuleStyles() );
1393  $this->mJsConfigVars = self::mergeMap( $this->mJsConfigVars, $source->getJsConfigVars() );
1394  $this->mMaxAdaptiveExpiry = min( $this->mMaxAdaptiveExpiry, $source->mMaxAdaptiveExpiry );
1395 
1396  // "noindex" always wins!
1397  if ( $this->mIndexPolicy === 'noindex' || $source->mIndexPolicy === 'noindex' ) {
1398  $this->mIndexPolicy = 'noindex';
1399  } elseif ( $this->mIndexPolicy !== 'index' ) {
1400  $this->mIndexPolicy = $source->mIndexPolicy;
1401  }
1402 
1403  // Skin control
1404  $this->mNewSection = $this->mNewSection || $source->getNewSection();
1405  $this->mHideNewSection = $this->mHideNewSection || $source->getHideNewSection();
1406  $this->mNoGallery = $this->mNoGallery || $source->getNoGallery();
1407  $this->mEnableOOUI = $this->mEnableOOUI || $source->getEnableOOUI();
1408  $this->mPreventClickjacking = $this->mPreventClickjacking || $source->preventClickjacking();
1409 
1410  // TODO: we'll have to be smarter about this!
1411  $this->mSections = array_merge( $this->mSections, $source->getSections() );
1412  $this->mTOCHTML .= $source->mTOCHTML;
1413 
1414  // XXX: we don't want to concatenate title text, so first write wins.
1415  // We should use the first *modified* title text, but we don't have the original to check.
1416  if ( $this->mTitleText === null || $this->mTitleText === '' ) {
1417  $this->mTitleText = $source->mTitleText;
1418  }
1419 
1420  // class names are stored in array keys
1421  $this->mWrapperDivClasses = self::mergeMap(
1422  $this->mWrapperDivClasses,
1423  $source->mWrapperDivClasses
1424  );
1425 
1426  // NOTE: last write wins, same as within one ParserOutput
1427  $this->mIndicators = self::mergeMap( $this->mIndicators, $source->getIndicators() );
1428 
1429  // NOTE: include extension data in "tracking meta data" as well as "html meta data"!
1430  // TODO: add a $mergeStrategy parameter to setExtensionData to allow different
1431  // kinds of extension data to be merged in different ways.
1432  $this->mExtensionData = self::mergeMap(
1433  $this->mExtensionData,
1434  $source->mExtensionData
1435  );
1436  }
1437 
1446  $this->mLanguageLinks = self::mergeList( $this->mLanguageLinks, $source->getLanguageLinks() );
1447  $this->mCategories = self::mergeMap( $this->mCategories, $source->getCategories() );
1448  $this->mLinks = self::merge2D( $this->mLinks, $source->getLinks() );
1449  $this->mTemplates = self::merge2D( $this->mTemplates, $source->getTemplates() );
1450  $this->mTemplateIds = self::merge2D( $this->mTemplateIds, $source->getTemplateIds() );
1451  $this->mImages = self::mergeMap( $this->mImages, $source->getImages() );
1452  $this->mFileSearchOptions = self::mergeMap(
1453  $this->mFileSearchOptions,
1454  $source->getFileSearchOptions()
1455  );
1456  $this->mExternalLinks = self::mergeMap( $this->mExternalLinks, $source->getExternalLinks() );
1457  $this->mInterwikiLinks = self::merge2D(
1458  $this->mInterwikiLinks,
1459  $source->getInterwikiLinks()
1460  );
1461 
1462  // TODO: add a $mergeStrategy parameter to setProperty to allow different
1463  // kinds of properties to be merged in different ways.
1464  $this->mProperties = self::mergeMap( $this->mProperties, $source->getProperties() );
1465 
1466  // NOTE: include extension data in "tracking meta data" as well as "html meta data"!
1467  // TODO: add a $mergeStrategy parameter to setExtensionData to allow different
1468  // kinds of extension data to be merged in different ways.
1469  $this->mExtensionData = self::mergeMap(
1470  $this->mExtensionData,
1471  $source->mExtensionData
1472  );
1473  }
1474 
1475  private static function mergeMixedList( array $a, array $b ) {
1476  return array_unique( array_merge( $a, $b ), SORT_REGULAR );
1477  }
1478 
1479  private static function mergeList( array $a, array $b ) {
1480  return array_values( array_unique( array_merge( $a, $b ), SORT_REGULAR ) );
1481  }
1482 
1483  private static function mergeMap( array $a, array $b ) {
1484  return array_replace( $a, $b );
1485  }
1486 
1487  private static function merge2D( array $a, array $b ) {
1488  $values = [];
1489  $keys = array_merge( array_keys( $a ), array_keys( $b ) );
1490 
1491  foreach ( $keys as $k ) {
1492  if ( empty( $a[$k] ) ) {
1493  $values[$k] = $b[$k];
1494  } elseif ( empty( $b[$k] ) ) {
1495  $values[$k] = $a[$k];
1496  } elseif ( is_array( $a[$k] ) && is_array( $b[$k] ) ) {
1497  $values[$k] = array_replace( $a[$k], $b[$k] );
1498  } else {
1499  $values[$k] = $b[$k];
1500  }
1501  }
1502 
1503  return $values;
1504  }
1505 
1506  private static function useEachMinValue( array $a, array $b ) {
1507  $values = [];
1508  $keys = array_merge( array_keys( $a ), array_keys( $b ) );
1509 
1510  foreach ( $keys as $k ) {
1511  if ( is_array( $a[$k] ?? null ) && is_array( $b[$k] ?? null ) ) {
1512  $values[$k] = self::useEachMinValue( $a[$k], $b[$k] );
1513  } else {
1514  $values[$k] = self::useMinValue( $a[$k] ?? null, $b[$k] ?? null );
1515  }
1516  }
1517 
1518  return $values;
1519  }
1520 
1521  private static function useMinValue( $a, $b ) {
1522  if ( $a === null ) {
1523  return $b;
1524  }
1525 
1526  if ( $b === null ) {
1527  return $a;
1528  }
1529 
1530  return min( $a, $b );
1531  }
1532 
1533  private static function useMaxValue( $a, $b ) {
1534  if ( $a === null ) {
1535  return $b;
1536  }
1537 
1538  if ( $b === null ) {
1539  return $a;
1540  }
1541 
1542  return max( $a, $b );
1543  }
1544 
1545 }
static useEachMinValue(array $a, array $b)
getPreventClickjacking()
Get the prevent-clickjacking flag.
addCategory( $c, $sort)
static useMaxValue( $a, $b)
getModules( $filter=false, $position=null, $param='mModules', $type=ResourceLoaderModule::TYPE_COMBINED)
Get the list of modules to include on this page.
Definition: OutputPage.php:531
setProperty( $name, $value)
Set a property to be stored in the page_props database table.
addTrackingCategory( $msg, $title)
Add a tracking category, getting the title from a system message, or print a debug message if the tit...
getHeadItemsArray()
Get an array of head items.
Definition: OutputPage.php:627
$wgParserCacheExpireTime
The expiry time for the parser cache, in seconds.
getArticleID( $flags=0)
Get the article ID for this Title from the link cache, adding it if necessary.
Definition: Title.php:3166
setTOCHTML( $tochtml)
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
int null $speculativePageIdUsed
Assumed page ID for {{PAGEID}} if no revision is set.
int null $mSpeculativeRevId
Assumed rev ID for {{REVISIONID}} if no revision is set.
hideNewSection( $value)
$context
Definition: load.php:45
setRevisionTimestampUsed( $timestamp)
addModuleStyles( $modules)
const PARSE_FAST_SEC
setText( $text)
__construct( $text='', $languageLinks=[], $categoryLinks=[], $unused=false, $titletext='')
const EDITSECTION_REGEX
setSpeculativePageIdUsed( $id)
getRevisionUsedSha1Base36()
preventClickjacking( $flag=null)
Get or set the prevent-clickjacking flag.
wfGetRusage()
Get system resource usage of current request context.
setCategoryLinks( $cl)
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
$wgRegisterInternalExternals
By default MediaWiki does not register links pointing to same server in externallinks dataset...
const TOC_START
Definition: Parser.php:142
& getFileSearchOptions()
getWrapperDivClass()
Returns the class (or classes) to be used with the wrapper div for this otuput.
$sort
getProperty( $name)
$source
const NS_SPECIAL
Definition: Defines.php:49
static isLinkInternal( $internal, $url)
Checks, if a url is pointing to the own server.
setSpeculativeRevIdUsed( $id)
Parser cache specific expiry check.
Definition: CacheTime.php:29
wfLogWarning( $msg, $callerOffset=1, $level=E_USER_WARNING)
Send a warning as a PHP error and the debug log.
setDisplayTitle( $text)
Override the title to be used for display.
addImage( $name, $timestamp=null, $sha1=null)
Register a file dependency for this output.
addExternalLink( $url)
static getTimes( $clock=null)
addWrapperDivClass( $class)
Add a CSS class to use for the wrapping div.
addModules( $modules)
getSpeculativePageIdUsed()
getJsConfigVars()
Get the javascript config vars to include on this page.
static useMinValue( $a, $b)
setEnableOOUI( $enable=false)
Enables OOUI, if true, in any OutputPage instance this ParserOutput object is added to...
addOutputHook( $hook, $data=false)
getDisplayTitle()
Get the title to be used for display.
hasText()
Returns true if text was passed to the constructor, or set using setText().
$modules
setExtensionData( $key, $value)
Attaches arbitrary data to this ParserObject.
getCacheExpiry()
Returns the number of seconds after which this object should expire.
Definition: CacheTime.php:129
isExternal()
Is this Title interwiki?
Definition: Title.php:915
wfUrlencode( $s)
We want some things to be included as literal characters in our title URLs for prettiness, which urlencode encodes by default.
int null $revisionTimestampUsed
Assumed rev timestamp for {{REVISIONTIMESTAMP}} if no revision is set.
static getMain()
Get the RequestContext object associated with the main request.
recordOption( $option)
Tags a parser option for use in the cache key for this parser output.
mergeTrackingMetaDataFrom(ParserOutput $source)
Merges dependency tracking metadata such as backlinks, images used, and extension data from $source i...
getExtensionData( $key)
Gets extensions data previously attached to this ParserOutput using setExtensionData().
setIndexPolicy( $policy)
setFlag( $flag)
Attach a flag to the output so that it can be checked later to handle special cases.
getDBkey()
Get the main part with underscores.
Definition: Title.php:1016
addInterwikiLink( $title)
const NS_MEDIA
Definition: Defines.php:48
static mergeList(array $a, array $b)
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
getTimeSinceStart( $clock)
Returns the time since resetParseStartTime() was last called.
const NS_CATEGORY
Definition: Defines.php:74
addOutputPageMetadata(OutputPage $out)
Copy items from the OutputPage object into this one.
static mergeMap(array $a, array $b)
setNoGallery( $value)
static mergeMixedList(array $a, array $b)
static runWithoutAbort( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:231
static decodeTagAttributes( $text)
Return an associative array of attribute names and values from a partial tag string.
Definition: Sanitizer.php:1450
getNamespace()
Get the namespace index, i.e.
Definition: Title.php:1040
const NS_FILE
Definition: Defines.php:66
getModuleStyles( $filter=false, $position=null)
Get the list of style-only modules to load on this page.
Definition: OutputPage.php:556
getRawText()
Get the cacheable text with <mw:editsection> markers still in it.
resetParseStartTime()
Resets the parse start timestamps for future calls to getTimeSinceStart()
const SUPPORTS_UNWRAP_TRANSFORM
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:612
static merge2D(array $a, array $b)
addLanguageLink( $t)
mergeInternalMetaDataFrom(ParserOutput $source)
Merges internal metadata such as flags, accessed options, and profiling info from $source into this P...
wfSetVar(&$dest, $source, $force=false)
Sets dest to source and returns the original value of dest If source is NULL, it just returns the val...
setLimitReportData( $key, $value)
Sets parser limit report data for a key.
addTemplate( $title, $page_id, $rev_id)
Register a template dependency for this output.
array $mLimitReportJSData
Parser limit report data for JSON.
hasDynamicContent()
Check whether the cache TTL was lowered due to dynamic content.
const SUPPORTS_STATELESS_TRANSFORMS
Feature flags to indicate to extensions that MediaWiki core supports and uses getText() stateless tra...
addHeadItem( $section, $tag=false)
Add some text to the "<head>".
getText( $options=[])
Get the output HTML.
const TOC_END
Definition: Parser.php:143
finalizeAdaptiveCacheExpiry()
Call this when parsing is done to lower the TTL based on low parse times.
unsetProperty( $name)
setLanguageLinks( $ll)
setIndicator( $id, $content)
static decodeCharReferences( $text)
Decode any character references, numeric or named entities, in the text and return a UTF-8 string...
Definition: Sanitizer.php:1669
clearWrapperDivClass()
Clears the CSS class to use for the wrapping div, effectively disabling the wrapper div until addWrap...
addLink(Title $title, $id=null)
Record a local or interwiki inline link for saving in future link tables.
int $mMaxAdaptiveExpiry
Upper bound of expiry based on parse duration.
$wgServer
URL of the server.
$mWrapperDivClasses
string CSS classes to use for the wrapping div, stored in the array keys.
const PARSE_SLOW_SEC
$content
Definition: router.php:78
static normalizeLinkUrl( $url)
Replace unusual escape codes in a URL with their equivalent characters.
Definition: Parser.php:2143
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
string null $revisionUsedSha1Base36
SHA-1 base 36 hash of any self-transclusion.
setSections( $toc)
setRevisionUsedSha1Base36( $hash)
getFlag( $flag)
getRevisionTimestampUsed()
setTimestamp( $timestamp)
updateCacheExpiry( $seconds)
Sets the number of seconds after which this object should expire.
Definition: CacheTime.php:112
updateRuntimeAdaptiveExpiry( $ttl)
Lower the runtime adaptive TTL to at most this value.
setNewSection( $value)
mergeHtmlMetaDataFrom(ParserOutput $source)
Merges HTML metadata such as head items, JS config vars, and HTTP cache control info from $source int...
getUsedOptions()
Returns the options from its ParserOptions which have been taken into account to produce this output...
addJsConfigVars( $keys, $value=null)
Add one or more variables to be set in mw.config in JavaScript.
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:319