MediaWiki  master
ParserOptions.php
Go to the documentation of this file.
1 <?php
28 use Wikimedia\ScopedCallback;
29 
46 
52  private static $defaults = null;
53 
58  private static $lazyOptions = null;
59 
64  private static $initialLazyOptions = [
65  'dateformat' => [ __CLASS__, 'initDateFormat' ],
66  'speculativeRevId' => [ __CLASS__, 'initSpeculativeRevId' ],
67  'speculativePageId' => [ __CLASS__, 'initSpeculativePageId' ],
68  ];
69 
74  private static $cacheVaryingOptionsHash = null;
75 
81  'dateformat' => true,
82  'thumbsize' => true,
83  'printable' => true,
84  'userlang' => true,
85  ];
86 
92  private static $callbacks = [
93  'currentRevisionRecordCallback' => true,
94  'templateCallback' => true,
95  'speculativeRevIdCallback' => true,
96  'speculativePageIdCallback' => true,
97  ];
98 
103  private $options;
104 
110  private $mTimestamp;
111 
117  private $mUser;
118 
124  private $onAccessCallback = null;
125 
132  private $redirectTarget = null;
133 
137  private $mExtraKey = '';
138 
145  public function getOption( $name ) {
146  if ( !array_key_exists( $name, $this->options ) ) {
147  throw new InvalidArgumentException( "Unknown parser option $name" );
148  }
149 
150  $this->lazyLoadOption( $name );
151  $this->optionUsed( $name );
152  return $this->options[$name];
153  }
154 
158  private function lazyLoadOption( $name ) {
160  if ( isset( $lazyOptions[$name] ) && $this->options[$name] === null ) {
161  $this->options[$name] = call_user_func( $lazyOptions[$name], $this, $name );
162  }
163  }
164 
170  private function nullifyLazyOption( array $options ): array {
171  return array_fill_keys( array_keys( self::getLazyOptions() ), null ) + $options;
172  }
173 
185  public static function getLazyOptions(): array {
186  // Trigger a call to the 'ParserOptionsRegister' hook if it hasn't
187  // already been called.
188  if ( self::$lazyOptions === null ) {
190  }
191  return self::$lazyOptions;
192  }
193 
202  private static function getCacheVaryingOptionsHash(): array {
203  // Trigger a call to the 'ParserOptionsRegister' hook if it hasn't
204  // already been called.
205  if ( self::$cacheVaryingOptionsHash === null ) {
207  }
209  }
210 
219  public function setOption( $name, $value ) {
220  if ( !array_key_exists( $name, $this->options ) ) {
221  throw new InvalidArgumentException( "Unknown parser option $name" );
222  }
223  $old = $this->options[$name];
224  $this->options[$name] = $value;
225  return $old;
226  }
227 
236  protected function setOptionLegacy( $name, $value ) {
237  if ( !array_key_exists( $name, $this->options ) ) {
238  throw new InvalidArgumentException( "Unknown parser option $name" );
239  }
240  return wfSetVar( $this->options[$name], $value );
241  }
242 
251  public function getInterwikiMagic() {
252  return $this->getOption( 'interwikiMagic' );
253  }
254 
260  public function setInterwikiMagic( $x ) {
261  return $this->setOptionLegacy( 'interwikiMagic', $x );
262  }
263 
268  public function getAllowExternalImages() {
269  return $this->getOption( 'allowExternalImages' );
270  }
271 
279  public function setAllowExternalImages( $x ) {
280  wfDeprecated( __METHOD__, '1.35' );
281  return $this->setOptionLegacy( 'allowExternalImages', $x );
282  }
283 
291  public function getAllowExternalImagesFrom() {
292  return $this->getOption( 'allowExternalImagesFrom' );
293  }
294 
305  public function setAllowExternalImagesFrom( $x ) {
306  wfDeprecated( __METHOD__, '1.35' );
307  return $this->setOptionLegacy( 'allowExternalImagesFrom', $x );
308  }
309 
314  public function getEnableImageWhitelist() {
315  return $this->getOption( 'enableImageWhitelist' );
316  }
317 
325  public function setEnableImageWhitelist( $x ) {
326  wfDeprecated( __METHOD__, '1.35' );
327  return $this->setOptionLegacy( 'enableImageWhitelist', $x );
328  }
329 
334  public function getAllowSpecialInclusion() {
335  return $this->getOption( 'allowSpecialInclusion' );
336  }
337 
343  public function setAllowSpecialInclusion( $x ) {
344  return $this->setOptionLegacy( 'allowSpecialInclusion', $x );
345  }
346 
351  public function getInterfaceMessage() {
352  return $this->getOption( 'interfaceMessage' );
353  }
354 
360  public function setInterfaceMessage( $x ) {
361  return $this->setOptionLegacy( 'interfaceMessage', $x );
362  }
363 
368  public function getTargetLanguage() {
369  return $this->getOption( 'targetLanguage' );
370  }
371 
377  public function setTargetLanguage( $x ) {
378  return $this->setOption( 'targetLanguage', $x );
379  }
380 
385  public function getMaxIncludeSize() {
386  return $this->getOption( 'maxIncludeSize' );
387  }
388 
394  public function setMaxIncludeSize( $x ) {
395  return $this->setOptionLegacy( 'maxIncludeSize', $x );
396  }
397 
402  public function getMaxPPNodeCount() {
403  return $this->getOption( 'maxPPNodeCount' );
404  }
405 
411  public function setMaxPPNodeCount( $x ) {
412  return $this->setOptionLegacy( 'maxPPNodeCount', $x );
413  }
414 
419  public function getMaxPPExpandDepth() {
420  return $this->getOption( 'maxPPExpandDepth' );
421  }
422 
427  public function getMaxTemplateDepth() {
428  return $this->getOption( 'maxTemplateDepth' );
429  }
430 
436  public function setMaxTemplateDepth( $x ) {
437  return $this->setOptionLegacy( 'maxTemplateDepth', $x );
438  }
439 
446  return $this->getOption( 'expensiveParserFunctionLimit' );
447  }
448 
455  public function setExpensiveParserFunctionLimit( $x ) {
456  return $this->setOptionLegacy( 'expensiveParserFunctionLimit', $x );
457  }
458 
464  public function getRemoveComments() {
465  return $this->getOption( 'removeComments' );
466  }
467 
474  public function setRemoveComments( $x ) {
475  return $this->setOptionLegacy( 'removeComments', $x );
476  }
477 
482  public function getEnableLimitReport() {
483  return $this->getOption( 'enableLimitReport' );
484  }
485 
491  public function enableLimitReport( $x = true ) {
492  return $this->setOptionLegacy( 'enableLimitReport', $x );
493  }
494 
500  public function getCleanSignatures() {
501  return $this->getOption( 'cleanSignatures' );
502  }
503 
510  public function setCleanSignatures( $x ) {
511  return $this->setOptionLegacy( 'cleanSignatures', $x );
512  }
513 
518  public function getExternalLinkTarget() {
519  return $this->getOption( 'externalLinkTarget' );
520  }
521 
527  public function setExternalLinkTarget( $x ) {
528  return $this->setOptionLegacy( 'externalLinkTarget', $x );
529  }
530 
535  public function getDisableContentConversion() {
536  return $this->getOption( 'disableContentConversion' );
537  }
538 
544  public function disableContentConversion( $x = true ) {
545  return $this->setOptionLegacy( 'disableContentConversion', $x );
546  }
547 
552  public function getDisableTitleConversion() {
553  return $this->getOption( 'disableTitleConversion' );
554  }
555 
561  public function disableTitleConversion( $x = true ) {
562  return $this->setOptionLegacy( 'disableTitleConversion', $x );
563  }
564 
569  public function getThumbSize() {
570  return $this->getOption( 'thumbsize' );
571  }
572 
578  public function setThumbSize( $x ) {
579  return $this->setOptionLegacy( 'thumbsize', $x );
580  }
581 
587  public function getStubThreshold() {
588  wfDeprecated( __METHOD__, '1.37' );
589  return 0;
590  }
591 
598  public function setStubThreshold( $x ) {
599  wfDeprecated( __METHOD__, '1.37' );
600  return 0;
601  }
602 
607  public function getIsPreview() {
608  return $this->getOption( 'isPreview' );
609  }
610 
616  public function setIsPreview( $x ) {
617  return $this->setOptionLegacy( 'isPreview', $x );
618  }
619 
624  public function getIsSectionPreview() {
625  return $this->getOption( 'isSectionPreview' );
626  }
627 
633  public function setIsSectionPreview( $x ) {
634  return $this->setOptionLegacy( 'isSectionPreview', $x );
635  }
636 
641  public function getIsPrintable() {
642  return $this->getOption( 'printable' );
643  }
644 
650  public function setIsPrintable( $x ) {
651  return $this->setOptionLegacy( 'printable', $x );
652  }
653 
658  public function getPreSaveTransform() {
659  return $this->getOption( 'preSaveTransform' );
660  }
661 
667  public function setPreSaveTransform( $x ) {
668  return $this->setOptionLegacy( 'preSaveTransform', $x );
669  }
670 
675  public function getDateFormat() {
676  return $this->getOption( 'dateformat' );
677  }
678 
684  private static function initDateFormat( ParserOptions $popt ) {
685  $userFactory = MediaWikiServices::getInstance()->getUserFactory();
686  return $userFactory->newFromUserIdentity( $popt->getUserIdentity() )->getDatePreference();
687  }
688 
694  public function setDateFormat( $x ) {
695  return $this->setOptionLegacy( 'dateformat', $x );
696  }
697 
715  public function getUserLangObj() {
716  return $this->getOption( 'userlang' );
717  }
718 
731  public function getUserLang() {
732  return $this->getUserLangObj()->getCode();
733  }
734 
740  public function setUserLang( $x ) {
741  if ( is_string( $x ) ) {
742  $x = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( $x );
743  }
744 
745  return $this->setOptionLegacy( 'userlang', $x );
746  }
747 
753  public function getMagicISBNLinks() {
754  return $this->getOption( 'magicISBNLinks' );
755  }
756 
762  public function getMagicPMIDLinks() {
763  return $this->getOption( 'magicPMIDLinks' );
764  }
765 
771  public function getMagicRFCLinks() {
772  return $this->getOption( 'magicRFCLinks' );
773  }
774 
789  public function getAllowUnsafeRawHtml() {
790  return $this->getOption( 'allowUnsafeRawHtml' );
791  }
792 
801  public function setAllowUnsafeRawHtml( $x ) {
802  return $this->setOptionLegacy( 'allowUnsafeRawHtml', $x );
803  }
804 
810  public function getWrapOutputClass() {
811  return $this->getOption( 'wrapclass' );
812  }
813 
821  public function setWrapOutputClass( $className ) {
822  if ( $className === true ) { // DWIM, they probably want the default class name
823  $className = 'mw-parser-output';
824  }
825  if ( $className === false ) {
826  wfDeprecated( __METHOD__ . '( false )', '1.31' );
827  }
828  return $this->setOption( 'wrapclass', $className );
829  }
830 
838  return $this->getOption( 'currentRevisionRecordCallback' );
839  }
840 
848  public function setCurrentRevisionRecordCallback( $x ) {
849  return $this->setOption( 'currentRevisionRecordCallback', $x );
850  }
851 
856  public function getTemplateCallback() {
857  return $this->getOption( 'templateCallback' );
858  }
859 
865  public function setTemplateCallback( $x ) {
866  return $this->setOptionLegacy( 'templateCallback', $x );
867  }
868 
879  public function getSpeculativeRevId() {
880  return $this->getOption( 'speculativeRevId' );
881  }
882 
893  public function getSpeculativePageId() {
894  return $this->getOption( 'speculativePageId' );
895  }
896 
903  private static function initSpeculativeRevId( ParserOptions $popt ) {
904  $cb = $popt->getOption( 'speculativeRevIdCallback' );
905  $id = $cb ? $cb() : null;
906 
907  // returning null would result in this being re-called every access
908  return $id ?? false;
909  }
910 
917  private static function initSpeculativePageId( ParserOptions $popt ) {
918  $cb = $popt->getOption( 'speculativePageIdCallback' );
919  $id = $cb ? $cb() : null;
920 
921  // returning null would result in this being re-called every access
922  return $id ?? false;
923  }
924 
931  public function setSpeculativeRevIdCallback( $x ) {
932  $this->setOption( 'speculativeRevId', null ); // reset
933  return $this->setOption( 'speculativeRevIdCallback', $x );
934  }
935 
942  public function setSpeculativePageIdCallback( $x ) {
943  $this->setOption( 'speculativePageId', null ); // reset
944  return $this->setOption( 'speculativePageIdCallback', $x );
945  }
946 
951  public function getTimestamp() {
952  if ( !isset( $this->mTimestamp ) ) {
953  $this->mTimestamp = wfTimestampNow();
954  }
955  return $this->mTimestamp;
956  }
957 
963  public function setTimestamp( $x ) {
964  return wfSetVar( $this->mTimestamp, $x );
965  }
966 
975  public function setRedirectTarget( $title ) {
976  $this->redirectTarget = $title;
977  }
978 
985  public function getRedirectTarget() {
986  return $this->redirectTarget;
987  }
988 
995  public function addExtraKey( $key ) {
996  $this->mExtraKey .= '!' . $key;
997  }
998 
1004  public function getUserIdentity(): UserIdentity {
1005  return $this->mUser;
1006  }
1007 
1014  public function __construct( UserIdentity $user, $lang = null ) {
1015  if ( $lang === null ) {
1016  global $wgLang;
1018  $lang = $wgLang;
1019  }
1020  $this->initialiseFromUser( $user, $lang );
1021  }
1022 
1030  public static function newFromAnon() {
1031  return new ParserOptions( new User,
1032  MediaWikiServices::getInstance()->getContentLanguage() );
1033  }
1034 
1044  public static function newFromUser( $user ) {
1045  return new ParserOptions( $user );
1046  }
1047 
1057  public static function newFromUserAndLang( UserIdentity $user, Language $lang ) {
1058  return new ParserOptions( $user, $lang );
1059  }
1060 
1069  public static function newFromContext( IContextSource $context ) {
1070  return new ParserOptions( $context->getUser(), $context->getLanguage() );
1071  }
1072 
1091  public static function newCanonical( $context, $userLang = null ) {
1092  if ( $context instanceof IContextSource ) {
1093  $ret = self::newFromContext( $context );
1094  } elseif ( $context === 'canonical' ) {
1095  $ret = self::newFromAnon();
1096  } elseif ( $context instanceof UserIdentity ) {
1097  $ret = new self( $context, $userLang );
1098  } else {
1099  throw new InvalidArgumentException(
1100  '$context must be an IContextSource, the string "canonical", or a UserIdentity'
1101  );
1102  }
1103 
1104  foreach ( self::getCanonicalOverrides() as $k => $v ) {
1105  $ret->setOption( $k, $v );
1106  }
1107  return $ret;
1108  }
1109 
1114  public static function clearStaticCache() {
1115  if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
1116  throw new RuntimeException( __METHOD__ . ' is just for testing' );
1117  }
1118  self::$defaults = null;
1119  self::$lazyOptions = null;
1120  self::$cacheVaryingOptionsHash = null;
1121  }
1122 
1132  private static function getDefaults() {
1138  $services = MediaWikiServices::getInstance();
1139  $languageConverterFactory = $services->getLanguageConverterFactory();
1140  $userOptionsLookup = $services->getUserOptionsLookup();
1141  $contentLanguage = $services->getContentLanguage();
1142 
1143  if ( self::$defaults === null ) {
1144  // *UPDATE* ParserOptions::matches() if any of this changes as needed
1145  self::$defaults = [
1146  'dateformat' => null,
1147  'interfaceMessage' => false,
1148  'targetLanguage' => null,
1149  'removeComments' => true,
1150  'enableLimitReport' => false,
1151  'preSaveTransform' => true,
1152  'isPreview' => false,
1153  'isSectionPreview' => false,
1154  'printable' => false,
1155  'allowUnsafeRawHtml' => true,
1156  'wrapclass' => 'mw-parser-output',
1157  'currentRevisionRecordCallback' => [ Parser::class, 'statelessFetchRevisionRecord' ],
1158  'templateCallback' => [ Parser::class, 'statelessFetchTemplate' ],
1159  'speculativeRevIdCallback' => null,
1160  'speculativeRevId' => null,
1161  'speculativePageIdCallback' => null,
1162  'speculativePageId' => null,
1163  ];
1164 
1165  self::$cacheVaryingOptionsHash = self::$initialCacheVaryingOptionsHash;
1166  self::$lazyOptions = self::$initialLazyOptions;
1167 
1168  Hooks::runner()->onParserOptionsRegister(
1169  self::$defaults,
1170  self::$cacheVaryingOptionsHash,
1171  self::$lazyOptions
1172  );
1173 
1174  ksort( self::$cacheVaryingOptionsHash );
1175  }
1176 
1177  // Unit tests depend on being able to modify the globals at will
1178  return self::$defaults + [
1179  'interwikiMagic' => $wgInterwikiMagic,
1180  'allowExternalImages' => $wgAllowExternalImages,
1181  'allowExternalImagesFrom' => $wgAllowExternalImagesFrom,
1182  'enableImageWhitelist' => $wgEnableImageWhitelist,
1183  'allowSpecialInclusion' => $wgAllowSpecialInclusion,
1184  'maxIncludeSize' => $wgMaxArticleSize * 1024,
1185  'maxPPNodeCount' => $wgMaxPPNodeCount,
1186  'maxPPExpandDepth' => $wgMaxPPExpandDepth,
1187  'maxTemplateDepth' => $wgMaxTemplateDepth,
1188  'expensiveParserFunctionLimit' => $wgExpensiveParserFunctionLimit,
1189  'externalLinkTarget' => $wgExternalLinkTarget,
1190  'cleanSignatures' => $wgCleanSignatures,
1191  'disableContentConversion' => $languageConverterFactory->isConversionDisabled(),
1192  'disableTitleConversion' => $languageConverterFactory->isLinkConversionDisabled(),
1193  'magicISBNLinks' => $wgEnableMagicLinks['ISBN'],
1194  'magicPMIDLinks' => $wgEnableMagicLinks['PMID'],
1195  'magicRFCLinks' => $wgEnableMagicLinks['RFC'],
1196  'thumbsize' => $userOptionsLookup->getDefaultOption( 'thumbsize' ),
1197  'userlang' => $contentLanguage,
1198  ];
1199  }
1200 
1210  private static function getCanonicalOverrides() {
1212 
1213  return [
1214  'enableLimitReport' => $wgEnableParserLimitReporting,
1215  ];
1216  }
1217 
1224  private function initialiseFromUser( UserIdentity $user, Language $lang ) {
1225  // Initially lazy loaded option defaults must not be taken into account,
1226  // otherwise lazy loading does not work. Setting a default for lazy option
1227  // is useful for matching with canonical options.
1228  $this->options = $this->nullifyLazyOption( self::getDefaults() );
1229 
1230  $this->mUser = $user;
1231  $services = MediaWikiServices::getInstance();
1232  $optionsLookup = $services->getUserOptionsLookup();
1233  $this->options['thumbsize'] = $optionsLookup->getOption( $user, 'thumbsize' );
1234  $this->options['userlang'] = $lang;
1235  }
1236 
1246  public function matches( ParserOptions $other ) {
1247  // Compare most options
1248  $options = array_keys( $this->options );
1249  $options = array_diff( $options, [
1250  'enableLimitReport', // only affects HTML comments
1251  'tidy', // Has no effect since 1.35; removed in 1.36
1252  ] );
1253  foreach ( $options as $option ) {
1254  // Resolve any lazy options
1255  $this->lazyLoadOption( $option );
1256  $other->lazyLoadOption( $option );
1257 
1258  $o1 = $this->optionToString( $this->options[$option] );
1259  $o2 = $this->optionToString( $other->options[$option] );
1260  if ( $o1 !== $o2 ) {
1261  return false;
1262  }
1263  }
1264 
1265  // Compare most other fields
1266  foreach ( ( new ReflectionClass( $this ) )->getProperties() as $property ) {
1267  $field = $property->getName();
1268  if ( $property->isStatic() ) {
1269  continue;
1270  }
1271  if ( in_array( $field, [
1272  'options', // Already checked above
1273  'onAccessCallback', // only used for ParserOutput option tracking
1274  ] ) ) {
1275  continue;
1276  }
1277 
1278  if ( !is_object( $this->$field ) && $this->$field !== $other->$field ) {
1279  return false;
1280  }
1281  }
1282 
1283  return true;
1284  }
1285 
1291  public function matchesForCacheKey( ParserOptions $other ) {
1292  foreach ( self::allCacheVaryingOptions() as $option ) {
1293  // Populate any lazy options
1294  $this->lazyLoadOption( $option );
1295  $other->lazyLoadOption( $option );
1296 
1297  $o1 = $this->optionToString( $this->options[$option] );
1298  $o2 = $this->optionToString( $other->options[$option] );
1299  if ( $o1 !== $o2 ) {
1300  return false;
1301  }
1302  }
1303 
1304  return true;
1305  }
1306 
1312  public function registerWatcher( $callback ) {
1313  $this->onAccessCallback = $callback;
1314  }
1315 
1324  public function optionUsed( $optionName ) {
1325  if ( $this->onAccessCallback ) {
1326  call_user_func( $this->onAccessCallback, $optionName );
1327  }
1328  }
1329 
1335  public static function allCacheVaryingOptions() {
1336  return array_keys( array_filter( self::getCacheVaryingOptionsHash() ) );
1337  }
1338 
1344  private function optionToString( $value ) {
1345  if ( $value === true ) {
1346  return '1';
1347  } elseif ( $value === false ) {
1348  return '0';
1349  } elseif ( $value === null ) {
1350  return '';
1351  } elseif ( $value instanceof Language ) {
1352  return $value->getCode();
1353  } elseif ( is_array( $value ) ) {
1354  return '[' . implode( ',', array_map( [ $this, 'optionToString' ], $value ) ) . ']';
1355  } else {
1356  return (string)$value;
1357  }
1358  }
1359 
1372  public function optionsHash( $forOptions, $title = null ) {
1373  global $wgRenderHashAppend;
1374 
1375  $inCacheKey = self::allCacheVaryingOptions();
1376 
1377  // Resolve any lazy options
1378  $lazyOpts = array_intersect( $forOptions,
1379  $inCacheKey, array_keys( self::getLazyOptions() ) );
1380  foreach ( $lazyOpts as $k ) {
1381  $this->lazyLoadOption( $k );
1382  }
1383 
1386 
1387  // We only include used options with non-canonical values in the key
1388  // so adding a new option doesn't invalidate the entire parser cache.
1389  // The drawback to this is that changing the default value of an option
1390  // requires manual invalidation of existing cache entries, as mentioned
1391  // in the docs on the relevant methods and hooks.
1392  $values = [];
1393  foreach ( array_intersect( $inCacheKey, $forOptions ) as $option ) {
1394  $v = $this->optionToString( $options[$option] );
1395  $d = $this->optionToString( $defaults[$option] );
1396  if ( $v !== $d ) {
1397  $values[] = "$option=$v";
1398  }
1399  }
1400 
1401  $confstr = $values ? implode( '!', $values ) : 'canonical';
1402 
1403  // add in language specific options, if any
1404  // @todo FIXME: This is just a way of retrieving the url/user preferred variant
1405  $services = MediaWikiServices::getInstance();
1406  $lang = $title ? $title->getPageLanguage() : $services->getContentLanguage();
1407  $converter = $services->getLanguageConverterFactory()->getLanguageConverter( $lang );
1408  $confstr .= $converter->getExtraHashOptions();
1409 
1410  $confstr .= $wgRenderHashAppend;
1411 
1412  if ( $this->mExtraKey != '' ) {
1413  $confstr .= $this->mExtraKey;
1414  }
1415 
1416  $user = $services->getUserFactory()->newFromUserIdentity( $this->getUserIdentity() );
1417  // Give a chance for extensions to modify the hash, if they have
1418  // extra options or other effects on the parser cache.
1419  Hooks::runner()->onPageRenderingHash(
1420  $confstr,
1421  $user,
1422  $forOptions
1423  );
1424 
1425  // Make it a valid memcached key fragment
1426  $confstr = str_replace( ' ', '_', $confstr );
1427 
1428  return $confstr;
1429  }
1430 
1437  public function isSafeToCache( array $usedOptions = null ) {
1439  $inCacheKey = self::getCacheVaryingOptionsHash();
1440  $usedOptions = $usedOptions ?? array_keys( $this->options );
1441  foreach ( $usedOptions as $option ) {
1442  if ( empty( $inCacheKey[$option] ) && empty( self::$callbacks[$option] ) ) {
1443  $v = $this->optionToString( $this->options[$option] ?? null );
1444  $d = $this->optionToString( $defaults[$option] ?? null );
1445  if ( $v !== $d ) {
1446  return false;
1447  }
1448  }
1449  }
1450  return true;
1451  }
1452 
1463  public function setupFakeRevision( $title, $content, $user ) {
1464  $oldCallback = $this->setCurrentRevisionRecordCallback(
1465  static function (
1466  $titleToCheck, $parser = null ) use ( $title, $content, $user, &$oldCallback
1467  ) {
1468  if ( $titleToCheck->equals( $title ) ) {
1469  $revRecord = new MutableRevisionRecord( $title );
1470  $revRecord->setContent( SlotRecord::MAIN, $content )
1471  ->setUser( $user )
1472  ->setTimestamp( MWTimestamp::now( TS_MW ) )
1473  ->setPageId( $title->getArticleID() )
1474  ->setParentId( $title->getLatestRevID() );
1475  return $revRecord;
1476  } else {
1477  return call_user_func( $oldCallback, $titleToCheck, $parser );
1478  }
1479  }
1480  );
1481 
1482  global $wgHooks;
1483  $wgHooks['TitleExists'][] =
1484  static function ( $titleToCheck, &$exists ) use ( $title ) {
1485  if ( $titleToCheck->equals( $title ) ) {
1486  $exists = true;
1487  }
1488  };
1489  end( $wgHooks['TitleExists'] );
1490  $key = key( $wgHooks['TitleExists'] );
1491  $linkCache = MediaWikiServices::getInstance()->getLinkCache();
1492  $linkCache->clearBadLink( $title->getPrefixedDBkey() );
1493  return new ScopedCallback( static function () use ( $title, $key, $linkCache ) {
1494  global $wgHooks;
1495  unset( $wgHooks['TitleExists'][$key] );
1496  $linkCache->clearLink( $title );
1497  } );
1498  }
1499 }
1500 
ParserOptions\setMaxIncludeSize
setMaxIncludeSize( $x)
Maximum size of template expansions, in bytes.
Definition: ParserOptions.php:394
ParserOptions\getMagicPMIDLinks
getMagicPMIDLinks()
Are magic PMID links enabled?
Definition: ParserOptions.php:762
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:45
ParserOptions\setOptionLegacy
setOptionLegacy( $name, $value)
Legacy implementation.
Definition: ParserOptions.php:236
ParserOptions\getAllowExternalImagesFrom
getAllowExternalImagesFrom()
External images to allow.
Definition: ParserOptions.php:291
ParserOptions\getWrapOutputClass
getWrapOutputClass()
Class to use to wrap output from Parser::parse()
Definition: ParserOptions.php:810
ParserOptions\getExpensiveParserFunctionLimit
getExpensiveParserFunctionLimit()
Maximum number of calls per parse to expensive parser functions.
Definition: ParserOptions.php:445
ParserOptions\getIsSectionPreview
getIsSectionPreview()
Parsing the page for a "preview" operation on a single section?
Definition: ParserOptions.php:624
$wgMaxArticleSize
$wgMaxArticleSize
Maximum article size in kibibytes.
Definition: DefaultSettings.php:2654
ParserOptions\getRemoveComments
getRemoveComments()
Remove HTML comments.
Definition: ParserOptions.php:464
ParserOptions\setAllowUnsafeRawHtml
setAllowUnsafeRawHtml( $x)
If the wiki is configured to allow raw html ($wgRawHtml = true) is it allowed in the specific case of...
Definition: ParserOptions.php:801
ParserOptions\disableContentConversion
disableContentConversion( $x=true)
Whether content conversion should be disabled.
Definition: ParserOptions.php:544
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:200
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
wfSetVar
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...
Definition: GlobalFunctions.php:1504
$wgMaxTemplateDepth
$wgMaxTemplateDepth
Maximum recursion depth for templates within templates.
Definition: DefaultSettings.php:4873
ParserOptions\nullifyLazyOption
nullifyLazyOption(array $options)
Resets lazy loaded options to null in the provided $options array.
Definition: ParserOptions.php:170
ParserOptions\getIsPreview
getIsPreview()
Parsing the page for a "preview" operation?
Definition: ParserOptions.php:607
ParserOptions\getDisableTitleConversion
getDisableTitleConversion()
Whether title conversion should be disabled.
Definition: ParserOptions.php:552
ParserOptions\setAllowExternalImages
setAllowExternalImages( $x)
Allow all external images inline?
Definition: ParserOptions.php:279
ParserOptions\setTemplateCallback
setTemplateCallback( $x)
Callback for template fetching; first argument to call_user_func().
Definition: ParserOptions.php:865
ParserOptions\setIsPreview
setIsPreview( $x)
Parsing the page for a "preview" operation?
Definition: ParserOptions.php:616
ParserOptions\__construct
__construct(UserIdentity $user, $lang=null)
Definition: ParserOptions.php:1014
ParserOptions\setTargetLanguage
setTargetLanguage( $x)
Target language for the parse.
Definition: ParserOptions.php:377
ParserOptions\getInterfaceMessage
getInterfaceMessage()
Parsing an interface message?
Definition: ParserOptions.php:351
ParserOptions\setIsSectionPreview
setIsSectionPreview( $x)
Parsing the page for a "preview" operation on a single section?
Definition: ParserOptions.php:633
ParserOptions\getSpeculativeRevId
getSpeculativeRevId()
A guess for {{REVISIONID}}, calculated using the callback provided via setSpeculativeRevIdCallback().
Definition: ParserOptions.php:879
ParserOptions\newFromAnon
static newFromAnon()
Get a ParserOptions object for an anonymous user.
Definition: ParserOptions.php:1030
ParserOptions\setEnableImageWhitelist
setEnableImageWhitelist( $x)
Use the on-wiki external image whitelist?
Definition: ParserOptions.php:325
ParserOptions\newFromUserAndLang
static newFromUserAndLang(UserIdentity $user, Language $lang)
Get a ParserOptions object from a given user and language.
Definition: ParserOptions.php:1057
ParserOptions\setWrapOutputClass
setWrapOutputClass( $className)
CSS class to use to wrap output from Parser::parse()
Definition: ParserOptions.php:821
ParserOptions\setSpeculativeRevIdCallback
setSpeculativeRevIdCallback( $x)
Callback to generate a guess for {{REVISIONID}}.
Definition: ParserOptions.php:931
ParserOptions\disableTitleConversion
disableTitleConversion( $x=true)
Whether title conversion should be disabled.
Definition: ParserOptions.php:561
ParserOptions\initDateFormat
static initDateFormat(ParserOptions $popt)
Lazy initializer for dateFormat.
Definition: ParserOptions.php:684
ParserOptions\getIsPrintable
getIsPrintable()
Parsing the printable version of the page?
Definition: ParserOptions.php:641
ParserOptions\setMaxTemplateDepth
setMaxTemplateDepth( $x)
Maximum recursion depth for templates within templates.
Definition: ParserOptions.php:436
$wgLang
$wgLang
Definition: Setup.php:834
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
ParserOptions\getMagicRFCLinks
getMagicRFCLinks()
Are magic RFC links enabled?
Definition: ParserOptions.php:771
ParserOptions\getUserLangObj
getUserLangObj()
Get the user language used by the parser for this page and split the parser cache.
Definition: ParserOptions.php:715
ParserOptions\registerWatcher
registerWatcher( $callback)
Registers a callback for tracking which ParserOptions which are used.
Definition: ParserOptions.php:1312
ParserOptions\setupFakeRevision
setupFakeRevision( $title, $content, $user)
Sets a hook to force that a page exists, and sets a current revision callback to return a revision wi...
Definition: ParserOptions.php:1463
ParserOptions\getDateFormat
getDateFormat()
Date format index.
Definition: ParserOptions.php:675
ParserOptions\setOption
setOption( $name, $value)
Set an option, generically.
Definition: ParserOptions.php:219
ParserOptions\initSpeculativeRevId
static initSpeculativeRevId(ParserOptions $popt)
Callback registered with ParserOptions::$lazyOptions, triggered by getSpeculativeRevId().
Definition: ParserOptions.php:903
$wgHooks
$wgHooks
Global list of hooks.
Definition: DefaultSettings.php:8716
ParserOptions\getCanonicalOverrides
static getCanonicalOverrides()
Get "canonical" non-default option values.
Definition: ParserOptions.php:1210
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:989
ParserOptions\getRedirectTarget
getRedirectTarget()
Get the previously-set redirect target.
Definition: ParserOptions.php:985
ParserOptions\$mUser
UserIdentity $mUser
Stored user object.
Definition: ParserOptions.php:117
ParserOptions\setUserLang
setUserLang( $x)
Set the user language used by the parser for this page and split the parser cache.
Definition: ParserOptions.php:740
ParserOptions\getAllowSpecialInclusion
getAllowSpecialInclusion()
Allow inclusion of special pages?
Definition: ParserOptions.php:334
$wgAllowExternalImagesFrom
$wgAllowExternalImagesFrom
If the above is false, you can specify an exception here.
Definition: DefaultSettings.php:4919
ParserOptions\getMaxIncludeSize
getMaxIncludeSize()
Maximum size of template expansions, in bytes.
Definition: ParserOptions.php:385
ParserOptions\$mTimestamp
string null $mTimestamp
Timestamp used for {{CURRENTDAY}} etc.
Definition: ParserOptions.php:110
$wgExpensiveParserFunctionLimit
$wgExpensiveParserFunctionLimit
Maximum number of calls per parse to expensive parser functions such as PAGESINCATEGORY.
Definition: DefaultSettings.php:5051
ParserOptions\setThumbSize
setThumbSize( $x)
Thumb size preferred by the user.
Definition: ParserOptions.php:578
ParserOptions\getEnableImageWhitelist
getEnableImageWhitelist()
Use the on-wiki external image whitelist?
Definition: ParserOptions.php:314
$wgInterwikiMagic
$wgInterwikiMagic
Treat language links as magic connectors, not inline links.
Definition: DefaultSettings.php:3459
ParserOptions\setExternalLinkTarget
setExternalLinkTarget( $x)
Target attribute for external links.
Definition: ParserOptions.php:527
ParserOptions\setDateFormat
setDateFormat( $x)
Date format index.
Definition: ParserOptions.php:694
ParserOptions\newCanonical
static newCanonical( $context, $userLang=null)
Creates a "canonical" ParserOptions object.
Definition: ParserOptions.php:1091
ParserOptions\getTargetLanguage
getTargetLanguage()
Target language for the parse.
Definition: ParserOptions.php:368
$title
$title
Definition: testCompression.php:38
ParserOptions\initSpeculativePageId
static initSpeculativePageId(ParserOptions $popt)
Callback registered with ParserOptions::$lazyOptions, triggered by getSpeculativePageId().
Definition: ParserOptions.php:917
ParserOptions\setAllowSpecialInclusion
setAllowSpecialInclusion( $x)
Allow inclusion of special pages?
Definition: ParserOptions.php:343
$wgMaxPPExpandDepth
$wgMaxPPExpandDepth
Definition: DefaultSettings.php:4878
ParserOptions\getDefaults
static getDefaults()
Get default option values.
Definition: ParserOptions.php:1132
ParserOptions\getExternalLinkTarget
getExternalLinkTarget()
Target attribute for external links.
Definition: ParserOptions.php:518
ParserOptions\$onAccessCallback
callable null $onAccessCallback
Function to be called when an option is accessed.
Definition: ParserOptions.php:124
$wgEnableMagicLinks
$wgEnableMagicLinks
Enable the magic links feature of automatically turning ISBN xxx, PMID xxx, RFC xxx into links.
Definition: DefaultSettings.php:5076
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1686
ParserOptions\getStubThreshold
getStubThreshold()
Thumb size preferred by the user.
Definition: ParserOptions.php:587
$wgExternalLinkTarget
$wgExternalLinkTarget
Set a default target for external links, e.g.
Definition: DefaultSettings.php:4998
ParserOptions\$options
array $options
Current values for all options that are relevant for caching.
Definition: ParserOptions.php:103
ParserOptions\getUserLang
getUserLang()
Same as getUserLangObj() but returns a string instead.
Definition: ParserOptions.php:731
ParserOptions\getPreSaveTransform
getPreSaveTransform()
Transform wiki markup when saving the page?
Definition: ParserOptions.php:658
ParserOptions\setStubThreshold
setStubThreshold( $x)
Thumb size preferred by the user.
Definition: ParserOptions.php:598
ParserOptions\$lazyOptions
static callable[] null $lazyOptions
Lazy-loaded options.
Definition: ParserOptions.php:58
ParserOptions\getOption
getOption( $name)
Fetch an option and track that is was accessed.
Definition: ParserOptions.php:145
ParserOptions\$mExtraKey
$mExtraKey
Appended to the options hash.
Definition: ParserOptions.php:137
ParserOptions\getAllowUnsafeRawHtml
getAllowUnsafeRawHtml()
If the wiki is configured to allow raw html ($wgRawHtml = true) is it allowed in the specific case of...
Definition: ParserOptions.php:789
ParserOptions\getDisableContentConversion
getDisableContentConversion()
Whether content conversion should be disabled.
Definition: ParserOptions.php:535
ParserOptions\optionToString
optionToString( $value)
Convert an option to a string value.
Definition: ParserOptions.php:1344
ParserOptions\matchesForCacheKey
matchesForCacheKey(ParserOptions $other)
Definition: ParserOptions.php:1291
ParserOptions\optionUsed
optionUsed( $optionName)
Called when an option is accessed.
Definition: ParserOptions.php:1324
$content
$content
Definition: router.php:76
ParserOptions\getSpeculativePageId
getSpeculativePageId()
A guess for {{PAGEID}}, calculated using the callback provided via setSpeculativeRevPageCallback().
Definition: ParserOptions.php:893
ParserOptions\setTimestamp
setTimestamp( $x)
Timestamp used for {{CURRENTDAY}} etc.
Definition: ParserOptions.php:963
ParserOptions\setSpeculativePageIdCallback
setSpeculativePageIdCallback( $x)
Callback to generate a guess for {{PAGEID}}.
Definition: ParserOptions.php:942
ParserOptions\setCleanSignatures
setCleanSignatures( $x)
Clean up signature texts?
Definition: ParserOptions.php:510
ParserOptions\newFromContext
static newFromContext(IContextSource $context)
Get a ParserOptions object from a IContextSource object.
Definition: ParserOptions.php:1069
MediaWiki\Revision\MutableRevisionRecord
Definition: MutableRevisionRecord.php:44
Hooks\runner
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
$wgEnableParserLimitReporting
$wgEnableParserLimitReporting
Whether to include the NewPP limit report as a HTML comment.
Definition: DefaultSettings.php:8571
ParserOptions\getTemplateCallback
getTemplateCallback()
Callback for template fetching; first argument to call_user_func().
Definition: ParserOptions.php:856
ParserOptions\$cacheVaryingOptionsHash
static array null $cacheVaryingOptionsHash
Specify options that are included in the cache key.
Definition: ParserOptions.php:74
ParserOptions\setRemoveComments
setRemoveComments( $x)
Remove HTML comments.
Definition: ParserOptions.php:474
ParserOptions\setInterwikiMagic
setInterwikiMagic( $x)
Specify whether to extract interlanguage links.
Definition: ParserOptions.php:260
IContextSource\getUser
getUser()
ParserOptions\lazyLoadOption
lazyLoadOption( $name)
Definition: ParserOptions.php:158
ParserOptions\setRedirectTarget
setRedirectTarget( $title)
Note that setting or changing this does not make the page a redirect or change its target,...
Definition: ParserOptions.php:975
ParserOptions\setExpensiveParserFunctionLimit
setExpensiveParserFunctionLimit( $x)
Maximum number of calls per parse to expensive parser functions.
Definition: ParserOptions.php:455
ParserOptions\getTimestamp
getTimestamp()
Timestamp used for {{CURRENTDAY}} etc.
Definition: ParserOptions.php:951
ParserOptions\setMaxPPNodeCount
setMaxPPNodeCount( $x)
Maximum number of nodes touched by PPFrame::expand()
Definition: ParserOptions.php:411
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
ParserOptions\getCleanSignatures
getCleanSignatures()
Clean up signature texts?
Definition: ParserOptions.php:500
ParserOptions\getLazyOptions
static getLazyOptions()
Get lazy-loaded options.
Definition: ParserOptions.php:185
$userOptionsLookup
UserOptionsLookup $userOptionsLookup
Definition: ApiWatchlistTrait.php:33
ParserOptions\$redirectTarget
Title null $redirectTarget
If the page being parsed is a redirect, this should hold the redirect target.
Definition: ParserOptions.php:132
ParserOptions\setAllowExternalImagesFrom
setAllowExternalImagesFrom( $x)
External images to allow.
Definition: ParserOptions.php:305
ParserOptions\matches
matches(ParserOptions $other)
Check if these options match that of another options set.
Definition: ParserOptions.php:1246
ParserOptions\enableLimitReport
enableLimitReport( $x=true)
Enable limit report in an HTML comment on output.
Definition: ParserOptions.php:491
Title
Represents a title within MediaWiki.
Definition: Title.php:47
ParserOptions\getInterwikiMagic
getInterwikiMagic()
Whether to extract interlanguage links.
Definition: ParserOptions.php:251
ParserOptions\getUserIdentity
getUserIdentity()
Get the identity of the user for whom the parse is made.
Definition: ParserOptions.php:1004
ParserOptions\setInterfaceMessage
setInterfaceMessage( $x)
Parsing an interface message?
Definition: ParserOptions.php:360
ParserOptions\$defaults
static array null $defaults
Default values for all options that are relevant for caching.
Definition: ParserOptions.php:52
ParserOptions\allCacheVaryingOptions
static allCacheVaryingOptions()
Return all option keys that vary the options hash.
Definition: ParserOptions.php:1335
$wgCleanSignatures
$wgCleanSignatures
If true, removes (by substituting) templates in signatures.
Definition: DefaultSettings.php:4900
ParserOptions\getThumbSize
getThumbSize()
Thumb size preferred by the user.
Definition: ParserOptions.php:569
ParserOptions\getMagicISBNLinks
getMagicISBNLinks()
Are magic ISBN links enabled?
Definition: ParserOptions.php:753
ParserOptions\getCacheVaryingOptionsHash
static getCacheVaryingOptionsHash()
Get cache varying options, with the name of the option in the key, and a boolean in the value which i...
Definition: ParserOptions.php:202
ParserOptions\setIsPrintable
setIsPrintable( $x)
Parsing the printable version of the page?
Definition: ParserOptions.php:650
ParserOptions\getMaxPPNodeCount
getMaxPPNodeCount()
Maximum number of nodes touched by PPFrame::expand()
Definition: ParserOptions.php:402
$wgRenderHashAppend
$wgRenderHashAppend
Append a configured value to the parser cache and the sitenotice key so that they can be kept separat...
Definition: DefaultSettings.php:3131
ParserOptions\getCurrentRevisionRecordCallback
getCurrentRevisionRecordCallback()
Callback for current revision fetching; first argument to call_user_func().
Definition: ParserOptions.php:837
ParserOptions\getMaxPPExpandDepth
getMaxPPExpandDepth()
Maximum recursion depth in PPFrame::expand()
Definition: ParserOptions.php:419
ParserOptions\clearStaticCache
static clearStaticCache()
Reset static caches.
Definition: ParserOptions.php:1114
ParserOptions\getEnableLimitReport
getEnableLimitReport()
Enable limit report in an HTML comment on output.
Definition: ParserOptions.php:482
ParserOptions\isSafeToCache
isSafeToCache(array $usedOptions=null)
Test whether these options are safe to cache.
Definition: ParserOptions.php:1437
ParserOptions\getMaxTemplateDepth
getMaxTemplateDepth()
Maximum recursion depth for templates within templates.
Definition: ParserOptions.php:427
ParserOptions\setPreSaveTransform
setPreSaveTransform( $x)
Transform wiki markup when saving the page?
Definition: ParserOptions.php:667
ParserOptions\$initialCacheVaryingOptionsHash
static array $initialCacheVaryingOptionsHash
Initial inCacheKey options (before hook)
Definition: ParserOptions.php:80
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:68
ParserOptions\$callbacks
static array $callbacks
Specify pseudo-options that are actually callbacks.
Definition: ParserOptions.php:92
StubObject\unstub
static unstub(&$obj)
Unstubs an object, if it is a stub object.
Definition: StubObject.php:101
ParserOptions\$initialLazyOptions
static callable[] $initialLazyOptions
Initial lazy-loaded options (before hook)
Definition: ParserOptions.php:64
$wgMaxPPNodeCount
$wgMaxPPNodeCount
A complexity limit on template expansion: the maximum number of nodes visited by PPFrame::expand()
Definition: DefaultSettings.php:4865
ParserOptions\setCurrentRevisionRecordCallback
setCurrentRevisionRecordCallback( $x)
Callback for current revision fetching; first argument to call_user_func().
Definition: ParserOptions.php:848
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:42
ParserOptions\optionsHash
optionsHash( $forOptions, $title=null)
Generate a hash string with the values set on these ParserOptions for the keys given in the array.
Definition: ParserOptions.php:1372
ParserOptions\initialiseFromUser
initialiseFromUser(UserIdentity $user, Language $lang)
Get user options.
Definition: ParserOptions.php:1224
IContextSource\getLanguage
getLanguage()
MediaWiki\Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
ParserOptions\getAllowExternalImages
getAllowExternalImages()
Allow all external images inline?
Definition: ParserOptions.php:268
ParserOptions\newFromUser
static newFromUser( $user)
Get a ParserOptions object from a given user.
Definition: ParserOptions.php:1044
$wgAllowExternalImages
$wgAllowExternalImages
Whether to allow inline image pointing to other websites.
Definition: DefaultSettings.php:4905
$wgEnableImageWhitelist
$wgEnableImageWhitelist
If $wgAllowExternalImages is false, you can allow an on-wiki allow list of regular expression fragmen...
Definition: DefaultSettings.php:4932
ParserOptions\addExtraKey
addExtraKey( $key)
Extra key that should be present in the parser cache key.
Definition: ParserOptions.php:995
$wgAllowSpecialInclusion
$wgAllowSpecialInclusion
Allow special page inclusions such as {{Special:Allpages}}.
Definition: DefaultSettings.php:9172