39 use Psr\Log\LoggerInterface;
40 use Psr\Log\NullLogger;
41 use Wikimedia\IPUtils;
42 use Wikimedia\ScopedCallback;
92 # Flags for Parser::setFunctionHook
96 # Constants needed for external link processing
97 # Everything except bracket, space, or control characters
98 # \p{Zs} is unicode 'separator, space' category. It covers the space 0x20
99 # as well as U+3000 is IDEOGRAPHIC SPACE for T21052
100 # \x{FFFD} is the Unicode replacement character, which the HTML5 spec
101 # uses to replace invalid HTML characters.
103 # Simplified expression to match an IPv4 or IPv6 address, or
104 # at least one character of a host name (embeds EXT_LINK_URL_CLASS)
106 private const EXT_LINK_ADDR =
'(?:[0-9.]+|\\[(?i:[0-9a-f:.]+)\\]|[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}])';
107 # RegExp to make image URLs (embeds IPv6 part of EXT_LINK_ADDR)
109 private const EXT_IMAGE_REGEX =
'/^(http:\/\/|https:\/\/)((?:\\[(?i:[0-9a-f:.]+)\\])?[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}]+)
110 \\/([A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]+)\\.((?i)gif|png|jpg|jpeg)$/Sxu';
112 # Regular expression for a non-newline space
113 private const SPACE_NOT_NL =
'(?:\t| |&\#0*160;|&\#[Xx]0*[Aa]0;|\p{Zs})';
121 # Allowed values for $this->mOutputType
122 # Parameter to startExternalParse().
124 public const
OT_WIKI = 2;
# like preSaveTransform()
127 # like extractSections() - portions of the original are returned unchanged.
147 public const MARKER_SUFFIX =
"-QINU`\"'\x7f";
150 # Markers used for wrapping the table of contents
172 # Initialised by initializeVariables()
190 # Initialised in constructor
193 # Initialized in getPreprocessor()
200 # Cleared with clearState():
253 public $mUser; #
User object; only used when doing pre-save transform
256 # These are variables reset at least once per parse regardless of $clearState
271 public $mTitle; #
Title context, used
for self-link rendering and similar things
372 'EnableScaryTranscluding',
373 'ExtraInterlanguageLinkPrefixes',
383 'SignatureValidation',
386 'TranscludeCacheExpiry',
387 'PreprocessorCacheThreshold',
388 'DisableLangConversion'
413 $urlProtocols =
null,
427 wfDeprecated(
'old calling convention for ' . __METHOD__,
'1.34' );
431 if ( empty( $this->mConf[
'class'] ) ) {
432 $this->mConf[
'class'] = self::class;
434 $this->svcOptions =
new ServiceOptions( self::CONSTRUCTOR_OPTIONS,
435 $this->mConf, func_num_args() > 6
436 ? func_get_arg( 6 ) : MediaWikiServices::getInstance()->getMainConfig()
439 $nsInfo = func_num_args() > 8 ? func_get_arg( 8 ) :
null;
452 $this->mExtLinkBracketedRegex =
'/\[(((?i)' . $this->mUrlProtocols .
')' .
453 self::EXT_LINK_ADDR .
454 self::EXT_LINK_URL_CLASS .
'*)\p{Zs}*([^\]\\x00-\\x08\\x0a-\\x1F\\x{FFFD}]*?)\]/Su';
457 MediaWikiServices::getInstance()->getMagicWordFactory();
459 $this->contLang =
$contLang ?? MediaWikiServices::getInstance()->getContentLanguage();
461 $this->factory =
$factory ?? MediaWikiServices::getInstance()->getParserFactory();
462 $this->specialPageFactory = $spFactory ??
463 MediaWikiServices::getInstance()->getSpecialPageFactory();
465 MediaWikiServices::getInstance()->getLinkRendererFactory();
466 $this->nsInfo =
$nsInfo ?? MediaWikiServices::getInstance()->getNamespaceInfo();
467 $this->logger =
$logger ?:
new NullLogger();
469 MediaWikiServices::getInstance()->getBadFileLookup();
472 MediaWikiServices::getInstance()->getLanguageConverterFactory();
475 MediaWikiServices::getInstance()->getHookContainer();
476 $this->hookRunner =
new HookRunner( $this->hookContainer );
487 if ( isset( $this->mLinkHolders ) ) {
489 unset( $this->mLinkHolders );
492 foreach ( $this as $name => $value ) {
493 unset( $this->$name );
501 $this->mInParse =
false;
509 foreach ( [
'mStripState',
'mVarCache' ] as $k ) {
517 $this->hookRunner->onParserCloned( $this );
526 if ( !$this->mFirstCall ) {
529 $this->mFirstCall =
false;
535 $this->hookRunner->onParserFirstCallInit( $this );
546 $this->mAutonumber = 0;
553 $this->mRevisionObject = $this->mRevisionTimestamp =
554 $this->mRevisionId = $this->mRevisionUser = $this->mRevisionSize =
null;
555 $this->mRevisionRecordObject =
null;
556 $this->mVarCache = [];
558 $this->mLangLinkLanguages = [];
559 $this->currentRevisionCache =
null;
563 # Clear these on every parse, T6549
564 $this->mTplRedirCache = $this->mTplDomCache = [];
566 $this->mShowToc =
true;
567 $this->mForceTocPosition =
false;
568 $this->mIncludeSizes = [
572 $this->mPPNodeCount = 0;
573 $this->mGeneratedPPNodeCount = 0;
574 $this->mHighestExpansionDepth = 0;
575 $this->mDefaultSort =
false;
576 $this->mHeadings = [];
577 $this->mDoubleUnderscores = [];
578 $this->mExpensiveFunctionCount = 0;
581 if ( isset( $this->mPreprocessor ) && $this->mPreprocessor->parser !== $this ) {
582 $this->mPreprocessor =
null;
587 $this->hookRunner->onParserClearState( $this );
595 $this->mOptions->registerWatcher( [ $this->mOutput,
'recordOption' ] );
617 $linestart =
true, $clearState =
true, $revid =
null
622 $text = strtr( $text,
"\x7f",
"?" );
623 $magicScopeVariable = $this->
lock();
626 $text = str_replace(
"\000",
'', $text );
630 $this->currentRevisionCache =
null;
631 $this->mInputSize = strlen( $text );
632 if ( $this->mOptions->getEnableLimitReport() ) {
633 $this->mOutput->resetParseStartTime();
642 if ( $revid !==
null ) {
643 $this->mRevisionId = $revid;
644 $this->mRevisionObject =
null;
645 $this->mRevisionRecordObject =
null;
646 $this->mRevisionTimestamp =
null;
647 $this->mRevisionUser =
null;
648 $this->mRevisionSize =
null;
652 $this->hookRunner->onParserAfterParse( $this, $text, $this->mStripState );
664 || isset( $this->mDoubleUnderscores[
'nocontentconvert'] )
665 || isset( $this->mDoubleUnderscores[
'notitleconvert'] )
666 || $this->mOutput->getDisplayTitle() !==
false )
669 if ( $convruletitle ) {
670 $this->mOutput->setTitleText( $convruletitle );
673 $this->mOutput->setTitleText( $titleText );
677 # Compute runtime adaptive expiry if set
678 $this->mOutput->finalizeAdaptiveCacheExpiry();
680 # Warn if too many heavyweight parser functions were used
681 if ( $this->mExpensiveFunctionCount > $this->mOptions->getExpensiveParserFunctionLimit() ) {
683 $this->mExpensiveFunctionCount,
684 $this->mOptions->getExpensiveParserFunctionLimit()
688 # Information on limits, for the benefit of users who try to skirt them
689 if ( $this->mOptions->getEnableLimitReport() ) {
693 # Wrap non-interface parser output in a <div> so it can be targeted
695 $class = $this->mOptions->getWrapOutputClass();
696 if ( $class !==
false && !$this->mOptions->getInterfaceMessage() ) {
697 $this->mOutput->addWrapperDivClass( $class );
700 $this->mOutput->setText( $text );
702 $this->mRevisionId = $oldRevisionId;
703 $this->mRevisionObject = $oldRevisionObject;
704 $this->mRevisionRecordObject = $oldRevisionRecordObject;
705 $this->mRevisionTimestamp = $oldRevisionTimestamp;
706 $this->mRevisionUser = $oldRevisionUser;
707 $this->mRevisionSize = $oldRevisionSize;
708 $this->mInputSize =
false;
709 $this->currentRevisionCache =
null;
721 $maxIncludeSize = $this->mOptions->getMaxIncludeSize();
723 $cpuTime = $this->mOutput->getTimeSinceStart(
'cpu' );
724 if ( $cpuTime !==
null ) {
725 $this->mOutput->setLimitReportData(
'limitreport-cputime',
726 sprintf(
"%.3f", $cpuTime )
730 $wallTime = $this->mOutput->getTimeSinceStart(
'wall' );
731 $this->mOutput->setLimitReportData(
'limitreport-walltime',
732 sprintf(
"%.3f", $wallTime )
735 $this->mOutput->setLimitReportData(
'limitreport-ppvisitednodes',
736 [ $this->mPPNodeCount, $this->mOptions->getMaxPPNodeCount() ]
738 $this->mOutput->setLimitReportData(
'limitreport-postexpandincludesize',
739 [ $this->mIncludeSizes[
'post-expand'], $maxIncludeSize ]
741 $this->mOutput->setLimitReportData(
'limitreport-templateargumentsize',
742 [ $this->mIncludeSizes[
'arg'], $maxIncludeSize ]
744 $this->mOutput->setLimitReportData(
'limitreport-expansiondepth',
745 [ $this->mHighestExpansionDepth, $this->mOptions->getMaxPPExpandDepth() ]
747 $this->mOutput->setLimitReportData(
'limitreport-expensivefunctioncount',
748 [ $this->mExpensiveFunctionCount, $this->mOptions->getExpensiveParserFunctionLimit() ]
751 foreach ( $this->mStripState->getLimitReport() as list( $key, $value ) ) {
752 $this->mOutput->setLimitReportData( $key, $value );
755 $this->hookRunner->onParserLimitReportPrepare( $this, $this->mOutput );
757 $limitReport =
"NewPP limit report\n";
758 if ( $this->svcOptions->get(
'ShowHostnames' ) ) {
759 $limitReport .=
'Parsed by ' .
wfHostname() .
"\n";
761 $limitReport .=
'Cached time: ' . $this->mOutput->getCacheTime() .
"\n";
762 $limitReport .=
'Cache expiry: ' . $this->mOutput->getCacheExpiry() .
"\n";
763 $limitReport .=
'Dynamic content: ' .
764 ( $this->mOutput->hasDynamicContent() ?
'true' :
'false' ) .
766 $limitReport .=
'Complications: [' . implode(
', ', $this->mOutput->getAllFlags() ) .
"]\n";
768 foreach ( $this->mOutput->getLimitReportData() as $key => $value ) {
769 if ( $this->hookRunner->onParserLimitReportFormat(
770 $key, $value, $limitReport,
false,
false )
772 $keyMsg =
wfMessage( $key )->inLanguage(
'en' )->useDatabase(
false );
773 $valueMsg =
wfMessage( [
"$key-value-text",
"$key-value" ] )
774 ->inLanguage(
'en' )->useDatabase(
false );
775 if ( !$valueMsg->exists() ) {
778 if ( !$keyMsg->isDisabled() && !$valueMsg->isDisabled() ) {
779 $valueMsg->params( $value );
780 $limitReport .=
"{$keyMsg->text()}: {$valueMsg->text()}\n";
786 $limitReport = htmlspecialchars_decode( $limitReport );
790 $limitReport = str_replace( [
'-',
'&' ], [
'‐',
'&' ], $limitReport );
791 $text =
"\n<!-- \n$limitReport-->\n";
794 $dataByFunc = $this->mProfiler->getFunctionStats();
795 uasort( $dataByFunc,
function ( $a, $b ) {
796 return $b[
'real'] <=> $a[
'real'];
799 foreach ( array_slice( $dataByFunc, 0, 10 ) as $item ) {
800 $profileReport[] = sprintf(
"%6.2f%% %8.3f %6d %s",
801 $item[
'%real'], $item[
'real'], $item[
'calls'],
802 htmlspecialchars( $item[
'name'] ) );
804 $text .=
"<!--\nTransclusion expansion time report (%,ms,calls,template)\n";
805 $text .= implode(
"\n", $profileReport ) .
"\n-->\n";
807 $this->mOutput->setLimitReportData(
'limitreport-timingprofile', $profileReport );
810 if ( $this->svcOptions->get(
'ShowHostnames' ) ) {
811 $this->mOutput->setLimitReportData(
'cachereport-origin',
wfHostname() );
813 $this->mOutput->setLimitReportData(
'cachereport-timestamp',
814 $this->mOutput->getCacheTime() );
815 $this->mOutput->setLimitReportData(
'cachereport-ttl',
816 $this->mOutput->getCacheExpiry() );
817 $this->mOutput->setLimitReportData(
'cachereport-transientcontent',
818 $this->mOutput->hasDynamicContent() );
898 $this->hookRunner->onParserAfterParse( $this, $text, $this->mStripState );
917 $magicScopeVariable = $this->
lock();
919 if ( $revid !==
null ) {
920 $this->mRevisionId = $revid;
922 $this->hookRunner->onParserBeforePreprocess( $this, $text, $this->mStripState );
924 $text = $this->mStripState->unstripBoth( $text );
939 $text = $this->mStripState->unstripBoth( $text );
958 $text = $msg->params( $params )->plain();
960 # Parser (re)initialisation
961 $magicScopeVariable = $this->
lock();
967 $text = $this->mStripState->unstripBoth( $text );
978 $this->mUser = $user;
991 if (
$t->hasFragment() ) {
992 # Strip the fragment to avoid various odd effects
993 $this->mTitle =
$t->createFragmentTarget(
'' );
1015 return wfSetVar( $this->mTitle, $x );
1032 $this->mOutputType =
$ot;
1051 return wfSetVar( $this->mOutputType, $x );
1074 $this->mOptions = $options;
1086 return wfSetVar( $this->mOptions, $x );
1093 return $this->mLinkID++;
1100 $this->mLinkID = $id;
1120 $target = $this->mOptions->getTargetLanguage();
1122 if ( $target !==
null ) {
1124 } elseif ( $this->mOptions->getInterfaceMessage() ) {
1125 return $this->mOptions->getUserLangObj();
1128 return $this->
getTitle()->getPageLanguage();
1138 if ( $this->mUser !==
null ) {
1141 return $this->mOptions->getUser();
1150 if ( !isset( $this->mPreprocessor ) ) {
1153 MediaWikiServices::getInstance()->getMainWANObjectCache(),
1155 'cacheThreshold' => $this->svcOptions->get(
'PreprocessorCacheThreshold' ),
1156 'disableLangConversion' => $this->svcOptions->get(
'DisableLangConversion' )
1172 if ( !$this->mLinkRenderer ) {
1173 $this->mLinkRenderer = $this->linkRendererFactory->create();
1174 $this->mLinkRenderer->setStubThreshold(
1236 $taglist = implode(
'|', $elements );
1237 $start =
"/<($taglist)(\\s+[^>]*?|\\s*?)(\/?>)|<(!--)/i";
1239 while ( $text !=
'' ) {
1240 $p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE );
1242 if ( count( $p ) < 5 ) {
1245 if ( count( $p ) > 5 ) {
1253 list( , $element, $attributes, $close, $inside ) = $p;
1256 $marker = self::MARKER_PREFIX .
"-$element-" . sprintf(
'%08X', $n++ ) . self::MARKER_SUFFIX;
1257 $stripped .= $marker;
1259 if ( $close ===
'/>' ) {
1260 # Empty element tag, <tag />
1265 if ( $element ===
'!--' ) {
1268 $end =
"/(<\\/$element\\s*>)/i";
1270 $q = preg_split( $end, $inside, 2, PREG_SPLIT_DELIM_CAPTURE );
1272 if ( count( $q ) < 3 ) {
1273 # No end tag -- let it run out to the end of the text.
1277 list( , $tail, $text ) = $q;
1284 "<$element$attributes$close$content$tail" ];
1315 $marker = self::MARKER_PREFIX .
"-item-{$this->mMarkerIndex}-" . self::MARKER_SUFFIX;
1316 $this->mMarkerIndex++;
1317 $this->mStripState->addGeneral( $marker, $text );
1330 $td_history = []; # Is currently a td tag open?
1331 $last_tag_history = []; # Save history of last lag activated (td, th or caption)
1332 $tr_history = []; # Is currently a tr tag open?
1333 $tr_attributes = []; # history of tr attributes
1334 $has_opened_tr = []; # Did
this table open a <tr> element?
1335 $indent_level = 0; # indent level of the table
1337 foreach (
$lines as $outLine ) {
1338 $line = trim( $outLine );
1340 if (
$line ===
'' ) { # empty line, go to next line
1341 $out .= $outLine .
"\n";
1345 $first_character =
$line[0];
1346 $first_two = substr(
$line, 0, 2 );
1349 if ( preg_match(
'/^(:*)\s*\{\|(.*)$/',
$line,
$matches ) ) {
1350 # First check if we are starting a new table
1351 $indent_level = strlen(
$matches[1] );
1353 $attributes = $this->mStripState->unstripBoth(
$matches[2] );
1356 $outLine = str_repeat(
'<dl><dd>', $indent_level ) .
"<table{$attributes}>";
1357 array_push( $td_history,
false );
1358 array_push( $last_tag_history,
'' );
1359 array_push( $tr_history,
false );
1360 array_push( $tr_attributes,
'' );
1361 array_push( $has_opened_tr,
false );
1362 } elseif ( count( $td_history ) == 0 ) {
1363 # Don't do any of the following
1364 $out .= $outLine .
"\n";
1366 } elseif ( $first_two ===
'|}' ) {
1367 # We are ending a table
1369 $last_tag = array_pop( $last_tag_history );
1371 if ( !array_pop( $has_opened_tr ) ) {
1372 $line =
"<tr><td></td></tr>{$line}";
1375 if ( array_pop( $tr_history ) ) {
1376 $line =
"</tr>{$line}";
1379 if ( array_pop( $td_history ) ) {
1380 $line =
"</{$last_tag}>{$line}";
1382 array_pop( $tr_attributes );
1383 if ( $indent_level > 0 ) {
1384 $outLine = rtrim(
$line ) . str_repeat(
'</dd></dl>', $indent_level );
1388 } elseif ( $first_two ===
'|-' ) {
1389 # Now we have a table row
1390 $line = preg_replace(
'#^\|-+#',
'',
$line );
1392 # Whats after the tag is now only attributes
1393 $attributes = $this->mStripState->unstripBoth(
$line );
1395 array_pop( $tr_attributes );
1396 array_push( $tr_attributes, $attributes );
1399 $last_tag = array_pop( $last_tag_history );
1400 array_pop( $has_opened_tr );
1401 array_push( $has_opened_tr,
true );
1403 if ( array_pop( $tr_history ) ) {
1407 if ( array_pop( $td_history ) ) {
1408 $line =
"</{$last_tag}>{$line}";
1412 array_push( $tr_history,
false );
1413 array_push( $td_history,
false );
1414 array_push( $last_tag_history,
'' );
1415 } elseif ( $first_character ===
'|'
1416 || $first_character ===
'!'
1417 || $first_two ===
'|+'
1419 # This might be cell elements, td, th or captions
1420 if ( $first_two ===
'|+' ) {
1421 $first_character =
'+';
1428 if ( $first_character ===
'!' ) {
1432 # Split up multiple cells on the same line.
1433 # FIXME : This can result in improper nesting of tags processed
1434 # by earlier parser steps.
1435 $cells = explode(
'||',
$line );
1439 # Loop through each table cell
1440 foreach ( $cells as $cell ) {
1442 if ( $first_character !==
'+' ) {
1443 $tr_after = array_pop( $tr_attributes );
1444 if ( !array_pop( $tr_history ) ) {
1445 $previous =
"<tr{$tr_after}>\n";
1447 array_push( $tr_history,
true );
1448 array_push( $tr_attributes,
'' );
1449 array_pop( $has_opened_tr );
1450 array_push( $has_opened_tr,
true );
1453 $last_tag = array_pop( $last_tag_history );
1455 if ( array_pop( $td_history ) ) {
1456 $previous =
"</{$last_tag}>\n{$previous}";
1459 if ( $first_character ===
'|' ) {
1461 } elseif ( $first_character ===
'!' ) {
1463 } elseif ( $first_character ===
'+' ) {
1464 $last_tag =
'caption';
1469 array_push( $last_tag_history, $last_tag );
1471 # A cell could contain both parameters and data
1472 $cell_data = explode(
'|', $cell, 2 );
1474 # T2553: Note that a '|' inside an invalid link should not
1475 # be mistaken as delimiting cell parameters
1476 # Bug T153140: Neither should language converter markup.
1477 if ( preg_match(
'/\[\[|-\{/', $cell_data[0] ) === 1 ) {
1478 $cell =
"{$previous}<{$last_tag}>" . trim( $cell );
1479 } elseif ( count( $cell_data ) == 1 ) {
1481 $cell =
"{$previous}<{$last_tag}>" . trim( $cell_data[0] );
1483 $attributes = $this->mStripState->unstripBoth( $cell_data[0] );
1486 $cell =
"{$previous}<{$last_tag}{$attributes}>" . trim( $cell_data[1] );
1490 array_push( $td_history,
true );
1493 $out .= $outLine .
"\n";
1496 # Closing open td, tr && table
1497 while ( count( $td_history ) > 0 ) {
1498 if ( array_pop( $td_history ) ) {
1501 if ( array_pop( $tr_history ) ) {
1504 if ( !array_pop( $has_opened_tr ) ) {
1505 $out .=
"<tr><td></td></tr>\n";
1508 $out .=
"</table>\n";
1511 # Remove trailing line-ending (b/c)
1512 if ( substr( $out, -1 ) ===
"\n" ) {
1513 $out = substr( $out, 0, -1 );
1516 # special case: don't return empty table
1517 if ( $out ===
"<table>\n<tr><td></td></tr>\n</table>" ) {
1540 # Hook to suspend the parser in this state
1541 if ( !$this->hookRunner->onParserBeforeInternalParse( $this, $text, $this->mStripState ) ) {
1545 # if $frame is provided, then use $frame for replacing any variables
1547 # use frame depth to infer how include/noinclude tags should be handled
1548 # depth=0 means this is the top-level document; otherwise it's an included document
1549 if ( !$frame->depth ) {
1555 $text = $frame->expand( $dom );
1557 # if $frame is not provided, then use old-style replaceVariables
1561 $this->hookRunner->onInternalParseBeforeSanitize( $this, $text, $this->mStripState );
1566 function ( &$text, $frame =
false ) {
1568 $text = $this->mStripState->unstripBoth( $text );
1574 $this->hookRunner->onInternalParseBeforeLinks( $this, $text, $this->mStripState );
1576 # Tables need to come after variable replacement for things to work
1577 # properly; putting them before other transformations should keep
1578 # exciting things like link expansions from showing up in surprising
1582 $text = preg_replace(
'/(^|\n)-----*/',
'\\1<hr />', $text );
1591 # handleInternalLinks may sometimes leave behind
1592 # absolute URLs, which have to be masked to hide them from handleExternalLinks
1593 $text = str_replace( self::MARKER_PREFIX .
'NOPARSE',
'', $text );
1607 return $this->languageConverterFactory->getLanguageConverter(
1618 return $this->languageConverterFactory->getLanguageConverter(
1656 $text = $this->mStripState->unstripGeneral( $text );
1669 if ( !( $this->mOptions->getDisableContentConversion()
1670 || isset( $this->mDoubleUnderscores[
'nocontentconvert'] ) )
1671 && !$this->mOptions->getInterfaceMessage()
1673 # The position of the convert() call should not be changed. it
1674 # assumes that the links are all replaced and the only thing left
1675 # is the <nowiki> mark.
1679 $text = $this->mStripState->unstripNoWiki( $text );
1681 $text = $this->mStripState->unstripGeneral( $text );
1683 # Clean up special characters, only run once, after doBlockLevels
1691 $this->hookRunner->onParserAfterTidy( $this, $text );
1712 $spdash =
"(?:-|$space)"; # a dash or a non-newline space
1713 $spaces =
"$space++"; # possessive match of 1 or more spaces
1714 $text = preg_replace_callback(
1716 (<a[ \t\r\n>].*?</a>) | # m[1]: Skip link text
1717 (<.*?>) | # m[2]: Skip stuff inside HTML elements' .
"
1718 (\b # m[3]: Free external links
1720 ($addr$urlChar*) # m[4]: Post-protocol path
1722 \b(?:RFC|PMID) $spaces # m[5]: RFC or PMID, capture number
1724 \bISBN $spaces ( # m[6]: ISBN, capture number
1725 (?: 97[89] $spdash? )? # optional 13-digit ISBN prefix
1726 (?: [0-9] $spdash? ){9} # 9 digits with opt. delimiters
1727 [0-9Xx] # check digit
1729 )!xu", [ $this,
'magicLinkCallback' ], $text );
1739 if ( isset( $m[1] ) && $m[1] !==
'' ) {
1742 } elseif ( isset( $m[2] ) && $m[2] !==
'' ) {
1745 } elseif ( isset( $m[3] ) && $m[3] !==
'' ) {
1746 # Free external link
1748 } elseif ( isset( $m[5] ) && $m[5] !==
'' ) {
1750 if ( substr( $m[0], 0, 3 ) ===
'RFC' ) {
1751 if ( !$this->mOptions->getMagicRFCLinks() ) {
1756 $cssClass =
'mw-magiclink-rfc';
1757 $trackingCat =
'magiclink-tracking-rfc';
1759 } elseif ( substr( $m[0], 0, 4 ) ===
'PMID' ) {
1760 if ( !$this->mOptions->getMagicPMIDLinks() ) {
1764 $urlmsg =
'pubmedurl';
1765 $cssClass =
'mw-magiclink-pmid';
1766 $trackingCat =
'magiclink-tracking-pmid';
1769 throw new MWException( __METHOD__ .
': unrecognised match type "' .
1770 substr( $m[0], 0, 20 ) .
'"' );
1772 $url =
wfMessage( $urlmsg, $id )->inContentLanguage()->text();
1782 } elseif ( isset( $m[6] ) && $m[6] !==
''
1783 && $this->mOptions->getMagicISBNLinks()
1788 $isbn = preg_replace(
"/$space/",
' ', $isbn );
1789 $num = strtr( $isbn, [
1799 'class' =>
'internal mw-magiclink-isbn',
1820 # The characters '<' and '>' (which were escaped by
1821 # removeHTMLtags()) should not be included in
1822 # URLs, per RFC 2396.
1823 # Make terminate a URL as well (bug T84937)
1826 '/&(lt|gt|nbsp|#x0*(3[CcEe]|[Aa]0)|#0*(60|62|160));/',
1831 $trail = substr( $url, $m2[0][1] ) . $trail;
1832 $url = substr( $url, 0, $m2[0][1] );
1835 # Move trailing punctuation to $trail
1837 # If there is no left bracket, then consider right brackets fair game too
1838 if ( strpos( $url,
'(' ) ===
false ) {
1842 $urlRev = strrev( $url );
1843 $numSepChars = strspn( $urlRev, $sep );
1844 # Don't break a trailing HTML entity by moving the ; into $trail
1845 # This is in hot code, so use substr_compare to avoid having to
1846 # create a new string object for the comparison
1847 if ( $numSepChars && substr_compare( $url,
";", -$numSepChars, 1 ) === 0 ) {
1848 # more optimization: instead of running preg_match with a $
1849 # anchor, which can be slow, do the match on the reversed
1850 # string starting at the desired offset.
1851 # un-reversed regexp is: /&([a-z]+|#x[\da-f]+|#\d+)$/i
1852 if ( preg_match(
'/\G([a-z]+|[\da-f]+x#|\d+#)&/i', $urlRev, $m2, 0, $numSepChars ) ) {
1856 if ( $numSepChars ) {
1857 $trail = substr( $url, -$numSepChars ) . $trail;
1858 $url = substr( $url, 0, -$numSepChars );
1861 # Verify that we still have a real URL after trail removal, and
1862 # not just lone protocol
1863 if ( strlen( $trail ) >= $numPostProto ) {
1864 return $url . $trail;
1869 # Is this an external image?
1871 if ( $text ===
false ) {
1872 # Not an image, make a link
1877 # Register it in the output object...
1878 $this->mOutput->addExternalLink( $url );
1880 return $text . $trail;
1890 for ( $i = 6; $i >= 1; --$i ) {
1891 $h = str_repeat(
'=', $i );
1894 $text = preg_replace(
"/^(?:$h)[ \\t]*(.+?)[ \\t]*(?:$h)\\s*$/m",
"<h$i>\\1</h$i>", $text );
1910 $outtext .= $this->
doQuotes( $line ) .
"\n";
1912 $outtext = substr( $outtext, 0, -1 );
1925 $arr = preg_split(
"/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE );
1926 $countarr = count( $arr );
1927 if ( $countarr == 1 ) {
1936 for ( $i = 1; $i < $countarr; $i += 2 ) {
1937 $thislen = strlen( $arr[$i] );
1941 if ( $thislen == 4 ) {
1942 $arr[$i - 1] .=
"'";
1945 } elseif ( $thislen > 5 ) {
1949 $arr[$i - 1] .= str_repeat(
"'", $thislen - 5 );
1954 if ( $thislen == 2 ) {
1956 } elseif ( $thislen == 3 ) {
1958 } elseif ( $thislen == 5 ) {
1968 if ( ( $numbold % 2 == 1 ) && ( $numitalics % 2 == 1 ) ) {
1969 $firstsingleletterword = -1;
1970 $firstmultiletterword = -1;
1972 for ( $i = 1; $i < $countarr; $i += 2 ) {
1973 if ( strlen( $arr[$i] ) == 3 ) {
1974 $x1 = substr( $arr[$i - 1], -1 );
1975 $x2 = substr( $arr[$i - 1], -2, 1 );
1976 if ( $x1 ===
' ' ) {
1977 if ( $firstspace == -1 ) {
1980 } elseif ( $x2 ===
' ' ) {
1981 $firstsingleletterword = $i;
1985 } elseif ( $firstmultiletterword == -1 ) {
1986 $firstmultiletterword = $i;
1992 if ( $firstsingleletterword > -1 ) {
1993 $arr[$firstsingleletterword] =
"''";
1994 $arr[$firstsingleletterword - 1] .=
"'";
1995 } elseif ( $firstmultiletterword > -1 ) {
1997 $arr[$firstmultiletterword] =
"''";
1998 $arr[$firstmultiletterword - 1] .=
"'";
1999 } elseif ( $firstspace > -1 ) {
2003 $arr[$firstspace] =
"''";
2004 $arr[$firstspace - 1] .=
"'";
2013 foreach ( $arr as $r ) {
2014 if ( ( $i % 2 ) == 0 ) {
2015 if ( $state ===
'both' ) {
2021 $thislen = strlen( $r );
2022 if ( $thislen == 2 ) {
2023 if ( $state ===
'i' ) {
2026 } elseif ( $state ===
'bi' ) {
2029 } elseif ( $state ===
'ib' ) {
2030 $output .=
'</b></i><b>';
2032 } elseif ( $state ===
'both' ) {
2033 $output .=
'<b><i>' . $buffer .
'</i>';
2039 } elseif ( $thislen == 3 ) {
2040 if ( $state ===
'b' ) {
2043 } elseif ( $state ===
'bi' ) {
2044 $output .=
'</i></b><i>';
2046 } elseif ( $state ===
'ib' ) {
2049 } elseif ( $state ===
'both' ) {
2050 $output .=
'<i><b>' . $buffer .
'</b>';
2056 } elseif ( $thislen == 5 ) {
2057 if ( $state ===
'b' ) {
2058 $output .=
'</b><i>';
2060 } elseif ( $state ===
'i' ) {
2061 $output .=
'</i><b>';
2063 } elseif ( $state ===
'bi' ) {
2064 $output .=
'</i></b>';
2066 } elseif ( $state ===
'ib' ) {
2067 $output .=
'</b></i>';
2069 } elseif ( $state ===
'both' ) {
2070 $output .=
'<i><b>' . $buffer .
'</b></i>';
2081 if ( $state ===
'b' || $state ===
'ib' ) {
2084 if ( $state ===
'i' || $state ===
'bi' || $state ===
'ib' ) {
2087 if ( $state ===
'bi' ) {
2091 if ( $state ===
'both' && $buffer ) {
2092 $output .=
'<b><i>' . $buffer .
'</i></b>';
2108 $bits = preg_split( $this->mExtLinkBracketedRegex, $text, -1, PREG_SPLIT_DELIM_CAPTURE );
2110 if ( $bits ===
false ) {
2111 throw new MWException(
"PCRE needs to be compiled with "
2112 .
"--enable-unicode-properties in order for MediaWiki to function" );
2114 $s = array_shift( $bits );
2117 while ( $i < count( $bits ) ) {
2120 $text = $bits[$i++];
2121 $trail = $bits[$i++];
2123 # The characters '<' and '>' (which were escaped by
2124 # removeHTMLtags()) should not be included in
2125 # URLs, per RFC 2396.
2127 if ( preg_match(
'/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE ) ) {
2128 $text = substr( $url, $m2[0][1] ) .
' ' . $text;
2129 $url = substr( $url, 0, $m2[0][1] );
2132 # If the link text is an image URL, replace it with an <img> tag
2133 # This happened by accident in the original parser, but some people used it extensively
2135 if ( $img !==
false ) {
2141 # Set linktype for CSS
2144 # No link text, e.g. [http://domain.tld/some.link]
2145 if ( $text ==
'' ) {
2148 $text =
'[' . $langObj->formatNum( ++$this->mAutonumber ) .
']';
2149 $linktype =
'autonumber';
2151 # Have link text, e.g. [http://domain.tld/some.link text]s
2163 # Use the encoded URL
2164 # This means that users can paste URLs directly into the text
2165 # Funny characters like ö aren't valid in URLs anyway
2166 # This was changed in August 2004
2171 # Register link in the output object.
2172 $this->mOutput->addExternalLink( $url );
2214 $target = $this->mOptions->getExternalLinkTarget();
2216 $attribs[
'target'] = $target;
2217 if ( !in_array( $target, [
'_self',
'_parent',
'_top' ] ) ) {
2221 if ( $rel !==
'' ) {
2224 $rel .=
'noreferrer noopener';
2227 $attribs[
'rel'] = $rel;
2242 # Test for RFC 3986 IPv6 syntax
2243 $scheme =
'[a-z][a-z0-9+.-]*:';
2244 $userinfo =
'(?:[a-z0-9\-._~!$&\'()*+,;=:]|%[0-9a-f]{2})*';
2245 $ipv6Host =
'\\[((?:[0-9a-f:]|%3[0-A]|%[46][1-6])+)\\]';
2246 if ( preg_match(
"<^(?:{$scheme})?//(?:{$userinfo}@)?{$ipv6Host}(?:[:/?#].*|)$>i", $url, $m ) &&
2247 IPUtils::isValid( rawurldecode( $m[1] ) )
2249 $isIPv6 = rawurldecode( $m[1] );
2254 # Make sure unsafe characters are encoded
2255 $url = preg_replace_callback(
'/[\x00-\x20"<>\[\\\\\]^`{|}\x7F-\xFF]/',
2257 return rawurlencode( $m[0] );
2263 $end = strlen( $url );
2265 # Fragment part - 'fragment'
2266 $start = strpos( $url,
'#' );
2267 if ( $start !==
false && $start < $end ) {
2269 substr( $url, $start, $end - $start ),
'"#%<>[\]^`{|}' ) . $ret;
2273 # Query part - 'query' minus &=+;
2274 $start = strpos( $url,
'?' );
2275 if ( $start !==
false && $start < $end ) {
2277 substr( $url, $start, $end - $start ),
'"#%<>[\]^`{|}&=+;' ) . $ret;
2281 # Scheme and path part - 'pchar'
2282 # (we assume no userinfo or encoded colons in the host)
2284 substr( $url, 0, $end ),
'"#%<>[\]^`{|}/?' ) . $ret;
2287 if ( $isIPv6 !==
false ) {
2288 $ipv6Host =
"%5B({$isIPv6})%5D";
2289 $ret = preg_replace(
2290 "<^((?:{$scheme})?//(?:{$userinfo}@)?){$ipv6Host}(?=[:/?#]|$)>i",
2300 $callback =
function (
$matches ) use ( $unsafe ) {
2302 $ord = ord( $char );
2303 if ( $ord > 32 && $ord < 127 && strpos( $unsafe, $char ) ===
false ) {
2307 # Leave it escaped, but use uppercase for a-f
2311 return preg_replace_callback(
'/%[0-9A-Fa-f]{2}/', $callback, $component );
2323 $imagesfrom = $this->mOptions->getAllowExternalImagesFrom();
2324 $imagesexception = !empty( $imagesfrom );
2326 # $imagesfrom could be either a single string or an array of strings, parse out the latter
2327 if ( $imagesexception && is_array( $imagesfrom ) ) {
2328 $imagematch =
false;
2329 foreach ( $imagesfrom as $match ) {
2330 if ( strpos( $url, $match ) === 0 ) {
2335 } elseif ( $imagesexception ) {
2336 $imagematch = ( strpos( $url, $imagesfrom ) === 0 );
2338 $imagematch =
false;
2341 if ( $this->mOptions->getAllowExternalImages()
2342 || ( $imagesexception && $imagematch )
2344 if ( preg_match( self::EXT_IMAGE_REGEX, $url ) ) {
2349 if ( !$text && $this->mOptions->getEnableImageWhitelist()
2350 && preg_match( self::EXT_IMAGE_REGEX, $url )
2352 $whitelist = explode(
2354 wfMessage(
'external_image_whitelist' )->inContentLanguage()->text()
2357 foreach ( $whitelist as $entry ) {
2358 # Sanitize the regex fragment, make it case-insensitive, ignore blank entries/comments
2359 if ( strpos( $entry,
'#' ) === 0 || $entry ===
'' ) {
2363 if ( preg_match(
'/' . str_replace(
'/',
'\\/', $entry ) .
'/i', $url ) ) {
2364 # Image matches a whitelist entry
2391 static $tc =
false, $e1, $e1_img;
2392 # the % is needed to support urlencoded titles as well
2395 # Match a link having the form [[namespace:link|alternate]]trail
2396 $e1 =
"/^([{$tc}]+)(?:\\|(.+?))?]](.*)\$/sD";
2397 # Match cases where there is no "]]", which might still be images
2398 $e1_img =
"/^([{$tc}]+)\\|(.*)\$/sD";
2406 # split the entire text string on occurrences of [[
2408 # get the first element (all text up to first [[), and remove the space we added
2411 $line = $a->current(); # Workaround
for broken ArrayIterator::next() that returns
"void"
2412 $s = substr(
$s, 1 );
2414 $nottalk = !$this->
getTitle()->isTalkPage();
2418 if ( $useLinkPrefixExtension ) {
2419 # Match the end of a line for a word that's not followed by whitespace,
2420 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
2421 $charset = $this->contLang->linkPrefixCharset();
2422 $e2 =
"/^((?>.*[^$charset]|))(.+)$/sDu";
2424 if ( preg_match( $e2,
$s, $m ) ) {
2425 $first_prefix = $m[2];
2427 $first_prefix =
false;
2433 # Some namespaces don't allow subpages
2434 $useSubpages = $this->nsInfo->hasSubpages(
2438 # Loop for each link
2439 for ( ;
$line !==
false &&
$line !==
null; $a->next(),
$line = $a->current() ) {
2440 # Check for excessive memory usage
2441 if ( $holders->isBig() ) {
2443 # Do the existence check, replace the link holders and clear the array
2444 $holders->replace(
$s );
2448 if ( $useLinkPrefixExtension ) {
2449 if ( preg_match( $e2,
$s, $m ) ) {
2450 list( ,
$s, $prefix ) = $m;
2455 if ( $first_prefix ) {
2456 $prefix = $first_prefix;
2457 $first_prefix =
false;
2461 $might_be_img =
false;
2463 if ( preg_match( $e1,
$line, $m ) ) { # page with normal text or alt
2465 # If we get a ] at the beginning of $m[3] that means we have a link that's something like:
2466 # [[Image:Foo.jpg|[http://example.com desc]]] <- having three ] in a row fucks up,
2467 # the real problem is with the $e1 regex
2469 # Still some problems for cases where the ] is meant to be outside punctuation,
2470 # and no image is in sight. See T4095.
2472 && substr( $m[3], 0, 1 ) ===
']'
2473 && strpos( $text,
'[' ) !==
false
2476 $m[3] = substr( $m[3], 1 );
2478 # fix up urlencoded title texts
2479 if ( strpos( $m[1],
'%' ) !==
false ) {
2480 # Should anchors '#' also be rejected?
2481 $m[1] = str_replace( [
'<',
'>' ], [
'<',
'>' ], rawurldecode( $m[1] ) );
2484 } elseif ( preg_match( $e1_img,
$line, $m ) ) {
2485 # Invalid, but might be an image with a link in its caption
2486 $might_be_img =
true;
2488 if ( strpos( $m[1],
'%' ) !==
false ) {
2489 $m[1] = str_replace( [
'<',
'>' ], [
'<',
'>' ], rawurldecode( $m[1] ) );
2492 }
else { # Invalid form; output directly
2497 $origLink = ltrim( $m[1],
' ' );
2499 # Don't allow internal links to pages containing
2500 # PROTO: where PROTO is a valid URL protocol; these
2501 # should be external links.
2502 if ( preg_match(
'/^(?i:' . $this->mUrlProtocols .
')/', $origLink ) ) {
2507 # Make subpage if necessary
2508 if ( $useSubpages ) {
2510 $this->
getTitle(), $origLink, $text
2520 $unstrip = $this->mStripState->killMarkers( $link );
2521 $noMarkers = ( $unstrip === $link );
2524 if ( $nt ===
null ) {
2529 $ns = $nt->getNamespace();
2530 $iw = $nt->getInterwiki();
2532 $noforce = ( substr( $origLink, 0, 1 ) !==
':' );
2534 if ( $might_be_img ) { #
if this is actually an invalid link
2535 if ( $ns ===
NS_FILE && $noforce ) { # but might be an image
2538 # look at the next 'line' to see if we can close it there
2540 $next_line = $a->current();
2541 if ( $next_line ===
false || $next_line ===
null ) {
2544 $m = explode(
']]', $next_line, 3 );
2545 if ( count( $m ) == 3 ) {
2546 # the first ]] closes the inner link, the second the image
2548 $text .=
"[[{$m[0]}]]{$m[1]}";
2551 } elseif ( count( $m ) == 2 ) {
2552 # if there's exactly one ]] that's fine, we'll keep looking
2553 $text .=
"[[{$m[0]}]]{$m[1]}";
2555 # if $next_line is invalid too, we need look no further
2556 $text .=
'[[' . $next_line;
2561 # we couldn't find the end of this imageLink, so output it raw
2562 # but don't ignore what might be perfectly normal links in the text we've examined
2564 $s .=
"{$prefix}[[$link|$text";
2565 # note: no $trail, because without an end, there *is* no trail
2568 }
else { # it
's not an image, so output it raw
2569 $s .= "{$prefix}[[$link|$text";
2570 # note: no $trail, because without an end, there *is* no trail
2575 $wasblank = ( $text == '' );
2579 # Strip off leading ':
'
2580 $text = substr( $text, 1 );
2583 # T6598 madness. Handle the quotes only if they come from the alternate part
2584 # [[Lista d''e paise d''o munno]] -> <a href="...">Lista d''e paise d''o munno</a>
2585 # [[Criticism of Harry Potter|Criticism of ''Harry Potter'']]
2586 # -> <a href="Criticism of Harry Potter">Criticism of <i>Harry Potter</i></a>
2587 $text = $this->doQuotes( $text );
2590 # Link not escaped by : , create the various objects
2591 if ( $noforce && !$nt->wasLocalInterwiki() ) {
2594 $iw && $this->mOptions->getInterwikiMagic() && $nottalk && (
2595 MediaWikiServices::getInstance()->getLanguageNameUtils()
2598 LanguageNameUtils::AUTONYMS,
2599 LanguageNameUtils::DEFINED
2601 || in_array( $iw, $this->svcOptions->get( 'ExtraInterlanguageLinkPrefixes
' ) )
2604 # T26502: filter duplicates
2605 if ( !isset( $this->mLangLinkLanguages[$iw] ) ) {
2606 $this->mLangLinkLanguages[$iw] = true;
2607 $this->mOutput->addLanguageLink( $nt->getFullText() );
2613 $s = rtrim( $s . $prefix ) . $trail; # T175416
2617 if ( $ns === NS_FILE ) {
2618 if ( !$this->badFileLookup->isBadFile( $nt->getDBkey(), $this->getTitle() ) ) {
2620 # if no parameters were passed, $text
2621 # becomes something like "File:Foo.png",
2622 # which we don't want to pass on to the
2626 # recursively parse links inside the image caption
2627 # actually, this will parse them in any other parameters, too,
2628 # but it might be hard to fix that, and it doesn't matter ATM
2632 # cloak any absolute URLs inside the image markup, so handleExternalLinks() won't touch them
2634 $this->
makeImage( $nt, $text, $holders ) ) . $trail;
2641 $s = rtrim(
$s . $prefix ) . $trail; # T2087, T87753
2649 $sortkey = str_replace(
"\n",
'', $sortkey );
2651 $this->mOutput->addCategory( $nt->getDBkey(), $sortkey );
2657 # Self-link checking. For some languages, variants of the title are checked in
2658 # LinkHolderArray::doVariants() to allow batching the existence checks necessary
2659 # for linking to a different variant.
2660 if ( $ns !==
NS_SPECIAL && $nt->equals( $this->getTitle() ) && !$nt->hasFragment() ) {
2665 # NS_MEDIA is a pseudo-namespace for linking directly to a file
2666 # @todo FIXME: Should do batch file existence checks, see comment below
2668 # Give extensions a chance to select the file revision for us
2671 $this->hookRunner->onBeforeParserFetchFileAndTitle(
2672 $this, $nt, $options, $descQuery );
2673 # Fetch and register the file (file title may be different via hooks)
2675 # Cloak with NOPARSE to avoid replacement in handleExternalLinks
2681 # Some titles, such as valid special pages or files in foreign repos, should
2682 # be shown as bluelinks even though they're not included in the page table
2683 # @todo FIXME: isAlwaysKnown() can be expensive for file links; we should really do
2684 # batch file existence checks for NS_FILE and NS_MEDIA
2685 if ( $iw ==
'' && $nt->isAlwaysKnown() ) {
2686 $this->mOutput->addLink( $nt );
2689 # Links will be added to the output link list after checking
2690 $s .= $holders->makeHolder( $nt, $text, $trail, $prefix );
2712 if ( $text ==
'' ) {
2716 $link = $this->getLinkRenderer()->makeKnownLink(
2717 $nt,
new HtmlArmor(
"$prefix$text$inside" )
2720 return $this->armorLinks( $link ) . $trail;
2734 return preg_replace(
'/\b((?i)' . $this->mUrlProtocols .
')/',
2735 self::MARKER_PREFIX .
"NOPARSE$1", $text );
2766 $this->hookRunner->onParserGetVariableValueVarCache( $this, $this->mVarCache ) &&
2767 isset( $this->mVarCache[$index] )
2769 return $this->mVarCache[$index];
2772 $ts =
wfTimestamp( TS_UNIX, $this->mOptions->getTimestamp() );
2773 $this->hookRunner->onParserGetVariableValueTs( $this, $ts );
2776 $this, $index, $ts, $this->nsInfo, $this->svcOptions, $this->logger
2779 if ( $value ===
null ) {
2782 $originalIndex = $index;
2783 $this->hookRunner->onParserGetVariableValueSwitch( $this,
2784 $this->mVarCache, $index, $ret, $frame );
2785 if ( $index !== $originalIndex ) {
2787 'A ParserGetVariableValueSwitch hook handler modified $index, ' .
2788 'this is deprecated since MediaWiki 1.35',
2789 '1.35',
false,
false
2792 if ( !isset( $this->mVarCache[$originalIndex] ) ||
2793 $this->mVarCache[$originalIndex] !== $ret ) {
2795 'A ParserGetVariableValueSwitch hook handler bypassed the cache, ' .
2796 'this is deprecated since MediaWiki 1.35',
'1.35',
false,
false
2804 $this->mVarCache[$index] = $value;
2814 $variableIDs = $this->magicWordFactory->getVariableIDs();
2815 $substIDs = $this->magicWordFactory->getSubstIDs();
2817 $this->mVariables = $this->magicWordFactory->newArray( $variableIDs );
2818 $this->mSubstWords = $this->magicWordFactory->newArray( $substIDs );
2839 return $this->getPreprocessor()->preprocessToObj( $text, $flags );
2863 # Is there any text? Also, Prevent too big inclusions!
2864 $textSize = strlen( $text );
2865 if ( $textSize < 1 || $textSize > $this->mOptions->getMaxIncludeSize() ) {
2869 if ( $frame ===
false ) {
2870 $frame = $this->getPreprocessor()->newFrame();
2871 } elseif ( !( $frame instanceof
PPFrame ) ) {
2872 $this->logger->debug(
2873 __METHOD__ .
" called using plain parameters instead of " .
2874 "a PPFrame instance. Creating custom frame."
2876 $frame = $this->getPreprocessor()->newCustomFrame( $frame );
2879 $dom = $this->preprocessToDom( $text );
2881 $text = $frame->expand( $dom, $flags );
2914 # does no harm if $current and $max are present but are unnecessary for the message
2915 # Not doing ->inLanguage( $this->mOptions->getUserLangObj() ), since this is shown
2916 # only during preview, and that would split the parser cache unnecessarily.
2917 $warning =
wfMessage(
"$limitationType-warning" )->numParams( $current, $max )
2919 $this->mOutput->addWarning( $warning );
2920 $this->addTrackingCategory(
"$limitationType-category" );
2946 $forceRawInterwiki =
false;
2948 $isChildObj =
false;
2950 $isLocalObj =
false;
2952 # Title object, where $text came from
2955 # $part1 is the bit before the first |, and must contain only title characters.
2956 # Various prefixes will be stripped from it later.
2957 $titleWithSpaces = $frame->
expand( $piece[
'title'] );
2958 $part1 = trim( $titleWithSpaces );
2961 # Original title text preserved for various purposes
2962 $originalTitle = $part1;
2964 # $args is a list of argument nodes, starting from index 0, not including $part1
2965 # @todo FIXME: If piece['parts'] is null then the call to getLength()
2966 # below won't work b/c this $args isn't an object
2967 $args = ( $piece[
'parts'] == null ) ? [] : $piece[
'parts'];
2969 $profileSection =
null;
2971 $sawDeprecatedTemplateEquals =
false;
2976 $substMatch = $this->mSubstWords->matchStartAndRemove( $part1 );
2978 # Possibilities for substMatch: "subst", "safesubst" or FALSE
2979 # Decide whether to expand template or keep wikitext as-is.
2980 if ( $this->ot[
'wiki'] ) {
2981 if ( $substMatch ===
false ) {
2982 $literal =
true; # literal when in PST with no prefix
2984 $literal =
false; # expand when in PST with subst: or safesubst:
2987 if ( $substMatch ==
'subst' ) {
2988 $literal =
true; # literal when not in PST with plain subst:
2990 $literal =
false; # expand when not in PST with safesubst: or no prefix
3001 if ( !$found &&
$args->getLength() == 0 ) {
3002 $id = $this->mVariables->matchStartToEnd( $part1 );
3003 if ( $id !==
false ) {
3004 $text = $this->expandMagicVariable( $id, $frame );
3005 if ( $this->magicWordFactory->getCacheTTL( $id ) > -1 ) {
3006 $this->mOutput->updateCacheExpiry(
3007 $this->magicWordFactory->getCacheTTL( $id ) );
3013 # MSG, MSGNW and RAW
3016 $mwMsgnw = $this->magicWordFactory->get(
'msgnw' );
3017 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
3020 # Remove obsolete MSG:
3021 $mwMsg = $this->magicWordFactory->get(
'msg' );
3022 $mwMsg->matchStartAndRemove( $part1 );
3026 $mwRaw = $this->magicWordFactory->get(
'raw' );
3027 if ( $mwRaw->matchStartAndRemove( $part1 ) ) {
3028 $forceRawInterwiki =
true;
3034 $colonPos = strpos( $part1,
':' );
3035 if ( $colonPos !==
false ) {
3036 $func = substr( $part1, 0, $colonPos );
3037 $funcArgs = [ trim( substr( $part1, $colonPos + 1 ) ) ];
3038 $argsLength =
$args->getLength();
3039 for ( $i = 0; $i < $argsLength; $i++ ) {
3040 $funcArgs[] =
$args->item( $i );
3043 $result = $this->callParserFunction( $frame, $func, $funcArgs );
3046 if ( isset( $result[
'title'] ) ) {
3047 $title = $result[
'title'];
3049 if ( isset( $result[
'found'] ) ) {
3050 $found = $result[
'found'];
3052 if ( array_key_exists(
'text', $result ) ) {
3054 $text = $result[
'text'];
3056 if ( isset( $result[
'nowiki'] ) ) {
3057 $nowiki = $result[
'nowiki'];
3059 if ( isset( $result[
'isHTML'] ) ) {
3060 $isHTML = $result[
'isHTML'];
3062 if ( isset( $result[
'forceRawInterwiki'] ) ) {
3063 $forceRawInterwiki = $result[
'forceRawInterwiki'];
3065 if ( isset( $result[
'isChildObj'] ) ) {
3066 $isChildObj = $result[
'isChildObj'];
3068 if ( isset( $result[
'isLocalObj'] ) ) {
3069 $isLocalObj = $result[
'isLocalObj'];
3074 # Finish mangling title and then check for loops.
3075 # Set $title to a Title object and $titleText to the PDBK
3078 # Split the title into page and subpage
3081 $this->
getTitle(), $part1, $subpage
3083 if ( $part1 !== $relative ) {
3085 $ns = $this->
getTitle()->getNamespace();
3089 $titleText =
$title->getPrefixedText();
3090 # Check for language variants if the template is not found
3091 if ( $this->getTargetLanguageConverter()->hasVariants() &&
$title->getArticleID() == 0 ) {
3092 $this->getTargetLanguageConverter()->findVariantLink( $part1,
$title,
true );
3094 # Do recursion depth check
3095 $limit = $this->mOptions->getMaxTemplateDepth();
3096 if ( $frame->depth >= $limit ) {
3098 $text =
'<span class="error">'
3099 .
wfMessage(
'parser-template-recursion-depth-warning' )
3100 ->numParams( $limit )->inContentLanguage()->text()
3106 # Load from database
3107 if ( !$found &&
$title ) {
3108 $profileSection = $this->mProfiler->scopedProfileIn(
$title->getPrefixedDBkey() );
3109 if ( !
$title->isExternal() ) {
3110 if (
$title->isSpecialPage()
3111 && $this->mOptions->getAllowSpecialInclusion()
3112 && $this->ot[
'html']
3114 $specialPage = $this->specialPageFactory->getPage(
$title->getDBkey() );
3119 $argsLength =
$args->getLength();
3120 for ( $i = 0; $i < $argsLength; $i++ ) {
3121 $bits =
$args->item( $i )->splitArg();
3122 if ( strval( $bits[
'index'] ) ===
'' ) {
3124 $value = trim( $frame->
expand( $bits[
'value'] ) );
3125 $pageArgs[$name] = $value;
3132 $context->setRequest(
new FauxRequest( $pageArgs ) );
3133 if ( $specialPage && $specialPage->maxIncludeCacheTime() === 0 ) {
3134 $context->setUser( $this->
getUser() );
3139 $context->setLanguage( $this->mOptions->getUserLangObj() );
3140 $ret = $this->specialPageFactory->capturePath(
$title, $context, $this->getLinkRenderer() );
3142 $text = $context->getOutput()->getHTML();
3143 $this->mOutput->addOutputPageMetadata( $context->getOutput() );
3146 if ( $specialPage && $specialPage->maxIncludeCacheTime() !==
false ) {
3147 $this->mOutput->updateRuntimeAdaptiveExpiry(
3148 $specialPage->maxIncludeCacheTime()
3152 } elseif ( $this->nsInfo->isNonincludable(
$title->getNamespace() ) ) {
3153 $found =
false; # access denied
3154 $this->logger->debug(
3156 ": template inclusion denied for " .
$title->getPrefixedDBkey()
3159 list( $text,
$title ) = $this->getTemplateDom(
$title );
3160 if ( $text !==
false ) {
3165 $title->getDBkey() ===
'=' &&
3166 $originalTitle ===
'='
3172 $sawDeprecatedTemplateEquals =
true;
3177 # If the title is valid but undisplayable, make a link to it
3178 if ( !$found && ( $this->ot[
'html'] || $this->ot[
'pre'] ) ) {
3179 $text =
"[[:$titleText]]";
3182 } elseif (
$title->isTrans() ) {
3183 # Interwiki transclusion
3184 if ( $this->ot[
'html'] && !$forceRawInterwiki ) {
3185 $text = $this->interwikiTransclude(
$title,
'render' );
3188 $text = $this->interwikiTransclude(
$title,
'raw' );
3189 # Preprocess it like a template
3196 # Do infinite loop check
3197 # This has to be done after redirect resolution to avoid infinite loops via redirects
3200 $text =
'<span class="error">'
3201 .
wfMessage(
'parser-template-loop-warning', $titleText )->inContentLanguage()->text()
3203 $this->addTrackingCategory(
'template-loop-category' );
3204 $this->mOutput->addWarning(
wfMessage(
'template-loop-warning',
3206 $this->logger->debug( __METHOD__ .
": template loop broken at '$titleText'" );
3210 # If we haven't found text to substitute by now, we're done
3211 # Recover the source wikitext and return it
3214 if ( $profileSection ) {
3215 $this->mProfiler->scopedProfileOut( $profileSection );
3217 return [
'object' => $text ];
3220 # Expand DOM-style return values in a child frame
3221 if ( $isChildObj ) {
3222 # Clean up argument array
3227 } elseif ( $titleText !==
false && $newFrame->isEmpty() ) {
3228 # Expansion is eligible for the empty-frame cache
3229 $text = $newFrame->cachedExpand( $titleText, $text );
3231 # Uncached expansion
3232 $text = $newFrame->expand( $text );
3235 if ( $isLocalObj && $nowiki ) {
3237 $isLocalObj =
false;
3240 if ( $profileSection ) {
3241 $this->mProfiler->scopedProfileOut( $profileSection );
3244 $sawDeprecatedTemplateEquals &&
3245 $this->mStripState->unstripBoth( $text ) !==
'='
3249 $this->addTrackingCategory(
'template-equals-category' );
3250 $this->mOutput->addWarning(
wfMessage(
'template-equals-warning' )->text() );
3253 # Replace raw HTML by a placeholder
3256 $text = $this->insertStripItem( $text );
3257 } elseif ( $nowiki && ( $this->ot[
'html'] || $this->ot[
'pre'] ) ) {
3258 # Escape nowiki-style return values
3261 } elseif ( is_string( $text )
3262 && !$piece[
'lineStart']
3263 && preg_match(
'/^(?:{\\||:|;|#|\*)/', $text )
3265 # T2529: if the template begins with a table or block-level
3266 # element, it should be treated as beginning a new line.
3267 # This behavior is somewhat controversial.
3268 $text =
"\n" . $text;
3271 if ( is_string( $text ) && !$this->incrementIncludeSize(
'post-expand', strlen( $text ) ) ) {
3272 # Error, oversize inclusion
3273 if ( $titleText !==
false ) {
3274 # Make a working, properly escaped link if possible (T25588)
3275 $text =
"[[:$titleText]]";
3277 # This will probably not be a working link, but at least it may
3278 # provide some hint of where the problem is
3279 preg_replace(
'/^:/',
'', $originalTitle );
3280 $text =
"[[:$originalTitle]]";
3282 $text .= $this->insertStripItem(
'<!-- WARNING: template omitted, '
3283 .
'post-expand include size too large -->' );
3284 $this->limitationWarn(
'post-expand-template-inclusion' );
3287 if ( $isLocalObj ) {
3288 $ret = [
'object' => $text ];
3290 $ret = [
'text' => $text ];
3315 # Case sensitive functions
3316 if ( isset( $this->mFunctionSynonyms[1][$function] ) ) {
3317 $function = $this->mFunctionSynonyms[1][$function];
3319 # Case insensitive functions
3320 $function = $this->contLang->lc( $function );
3321 if ( isset( $this->mFunctionSynonyms[0][$function] ) ) {
3322 $function = $this->mFunctionSynonyms[0][$function];
3324 return [
'found' => false ];
3328 list( $callback, $flags ) = $this->mFunctionHooks[$function];
3330 $allArgs = [ $this ];
3332 # Convert arguments to PPNodes and collect for appending to $allArgs
3334 foreach (
$args as $k => $v ) {
3335 if ( $v instanceof
PPNode || $k === 0 ) {
3338 $funcArgs[] = $this->mPreprocessor->newPartNodeArray( [ $k => $v ] )->item( 0 );
3342 # Add a frame parameter, and pass the arguments as an array
3343 $allArgs[] = $frame;
3344 $allArgs[] = $funcArgs;
3346 # Convert arguments to plain text and append to $allArgs
3347 foreach (
$args as $k => $v ) {
3348 if ( $v instanceof
PPNode ) {
3349 $allArgs[] = trim( $frame->
expand( $v ) );
3350 } elseif ( is_int( $k ) && $k >= 0 ) {
3351 $allArgs[] = trim( $v );
3353 $allArgs[] = trim(
"$k=$v" );
3358 $result = $callback( ...$allArgs );
3360 # The interface for function hooks allows them to return a wikitext
3361 # string or an array containing the string and any flags. This mungs
3362 # things around to match what this method should return.
3363 if ( !is_array( $result ) ) {
3369 if ( isset( $result[0] ) && !isset( $result[
'text'] ) ) {
3370 $result[
'text'] = $result[0];
3372 unset( $result[0] );
3379 $preprocessFlags = 0;
3380 if ( isset( $result[
'noparse'] ) ) {
3381 $noparse = $result[
'noparse'];
3383 if ( isset( $result[
'preprocessFlags'] ) ) {
3384 $preprocessFlags = $result[
'preprocessFlags'];
3388 $result[
'text'] = $this->preprocessToDom( $result[
'text'], $preprocessFlags );
3389 $result[
'isChildObj'] =
true;
3405 $titleText =
$title->getPrefixedDBkey();
3407 if ( isset( $this->mTplRedirCache[$titleText] ) ) {
3408 list( $ns, $dbk ) = $this->mTplRedirCache[$titleText];
3410 $titleText =
$title->getPrefixedDBkey();
3412 if ( isset( $this->mTplDomCache[$titleText] ) ) {
3413 return [ $this->mTplDomCache[$titleText],
$title ];
3416 # Cache miss, go to the database
3417 list( $text,
$title ) = $this->fetchTemplateAndTitle(
$title );
3419 if ( $text ===
false ) {
3420 $this->mTplDomCache[$titleText] =
false;
3421 return [
false,
$title ];
3425 $this->mTplDomCache[$titleText] = $dom;
3427 if ( !
$title->equals( $cacheTitle ) ) {
3428 $this->mTplRedirCache[$cacheTitle->getPrefixedDBkey()] =
3451 $revisionRecord = $this->fetchCurrentRevisionRecordOfTitle(
$title );
3452 if ( $revisionRecord ) {
3453 return new Revision( $revisionRecord );
3455 return $revisionRecord;
3472 $cacheKey =
$title->getPrefixedDBkey();
3473 if ( !$this->currentRevisionCache ) {
3474 $this->currentRevisionCache =
new MapCacheLRU( 100 );
3476 if ( !$this->currentRevisionCache->has( $cacheKey ) ) {
3480 $this->mOptions->getCurrentRevisionRecordCallback(),
3484 if ( !$revisionRecord ) {
3487 $revisionRecord =
null;
3489 $this->currentRevisionCache->set( $cacheKey, $revisionRecord );
3491 return $this->currentRevisionCache->get( $cacheKey );
3502 $this->currentRevisionCache &&
3503 $this->currentRevisionCache->has(
$title->getPrefixedText() )
3519 $revRecord = MediaWikiServices::getInstance()
3520 ->getRevisionLookup()
3521 ->getKnownCurrentRevision(
$title );
3522 return $revRecord ?
new Revision( $revRecord ) :
false;
3535 $revRecord = MediaWikiServices::getInstance()
3536 ->getRevisionLookup()
3537 ->getKnownCurrentRevision(
$title );
3548 $templateCb = $this->mOptions->getTemplateCallback();
3549 $stuff = call_user_func( $templateCb,
$title, $this );
3550 if ( isset( $stuff[
'revision-record'] ) ) {
3551 $revRecord = $stuff[
'revision-record'];
3554 $rev = $stuff[
'revision'] ??
null;
3556 $revRecord = $rev->getRevisionRecord();
3562 $text = $stuff[
'text'];
3563 if ( is_string( $stuff[
'text'] ) ) {
3565 $text = strtr( $text,
"\x7f",
"?" );
3567 $finalTitle = $stuff[
'finalTitle'] ??
$title;
3568 foreach ( ( $stuff[
'deps'] ?? [] ) as $dep ) {
3569 $this->mOutput->addTemplate( $dep[
'title'], $dep[
'page_id'], $dep[
'rev_id'] );
3573 $sha1 = $revRecord->getSha1();
3577 $this->setOutputFlag(
'vary-revision-sha1',
'Self transclusion' );
3578 $this->getOutput()->setRevisionUsedSha1Base36( $sha1 );
3582 return [ $text, $finalTitle ];
3593 return $this->fetchTemplateAndTitle(
$title )[0];
3606 $text = $skip =
false;
3611 # Loop to fetch the article, with up to 1 redirect
3612 $revLookup = MediaWikiServices::getInstance()->getRevisionLookup();
3613 for ( $i = 0; $i < 2 && is_object(
$title ); $i++ ) {
3614 # Give extensions a chance to select the revision instead
3615 $id =
false; # Assume current
3617 $parser,
$title, $skip, $id );
3623 'page_id' =>
$title->getArticleID(),
3629 # TODO rewrite using only RevisionRecord objects
3631 $revRecord = $revLookup->getRevisionById( $id );
3632 } elseif ( $parser ) {
3633 $revRecord = $parser->fetchCurrentRevisionRecordOfTitle(
$title );
3635 $revRecord = $revLookup->getRevisionByTitle(
$title );
3637 $rev_id = $revRecord ? $revRecord->getId() : 0;
3638 # If there is no current revision, there is no page
3639 if ( $id ===
false && !$revRecord ) {
3640 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
3641 $linkCache->addBadLinkObj(
$title );
3646 'page_id' =>
$title->getArticleID(),
3651 $revRecord->getPageAsLinkTarget()
3653 if ( !
$title->equals( $revTitle ) ) {
3654 # We fetched a rev from a different title; register it too...
3656 'title' => $revTitle,
3657 'page_id' => $revRecord->getPageId(),
3664 $content = $revRecord->getContent( SlotRecord::MAIN );
3670 $legacyRevision =
new Revision( $revRecord );
3680 if ( $text ===
false || $text ===
null ) {
3685 $message =
wfMessage( MediaWikiServices::getInstance()->getContentLanguage()->
3686 lcfirst(
$title->getText() ) )->inContentLanguage();
3687 if ( !$message->exists() ) {
3692 $text = $message->plain();
3704 $legacyRevision =
function () use ( $revRecord ) {
3705 return $revRecord ?
new Revision( $revRecord ) :
null;
3708 'revision' => $legacyRevision,
3709 'revision-record' => $revRecord ?:
false,
3711 'finalTitle' => $finalTitle,
3716 [
'revision' =>
'1.35' ],
3719 return $propertyArray;
3730 $file = $this->fetchFileNoRegister(
$title, $options );
3732 $time =
$file ?
$file->getTimestamp() :
false;
3734 # Register the file as a dependency...
3735 $this->mOutput->addImage(
$title->getDBkey(), $time, $sha1 );
3737 # Update fetched file title
3739 $this->mOutput->addImage(
$title->getDBkey(), $time, $sha1 );
3755 if ( isset( $options[
'broken'] ) ) {
3758 $repoGroup = MediaWikiServices::getInstance()->getRepoGroup();
3759 if ( isset( $options[
'sha1'] ) ) {
3760 $file = $repoGroup->findFileFromKey( $options[
'sha1'], $options );
3778 if ( !$this->svcOptions->get(
'EnableScaryTranscluding' ) ) {
3779 return wfMessage(
'scarytranscludedisabled' )->inContentLanguage()->text();
3782 $url =
$title->getFullURL( [
'action' => $action ] );
3783 if ( strlen( $url ) > 1024 ) {
3784 return wfMessage(
'scarytranscludetoolong' )->inContentLanguage()->text();
3787 $wikiId =
$title->getTransWikiID();
3789 $fname = __METHOD__;
3790 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
3792 $data =
$cache->getWithSetCallback(
3794 'interwiki-transclude',
3795 ( $wikiId !==
false ) ? $wikiId :
'external',
3798 $this->svcOptions->get(
'TranscludeCacheExpiry' ),
3799 function ( $oldValue, &$ttl ) use ( $url, $fname,
$cache ) {
3802 $status = $req->execute();
3803 if ( !$status->isOK() ) {
3804 $ttl = $cache::TTL_UNCACHEABLE;
3805 } elseif ( $req->getResponseHeader(
'X-Database-Lagged' ) !==
null ) {
3806 $ttl = min( $cache::TTL_LAGGED, $ttl );
3810 'text' => $status->isOK() ? $req->getContent() :
null,
3811 'code' => $req->getStatus()
3815 'checkKeys' => ( $wikiId !== false )
3816 ? [
$cache->makeGlobalKey(
'interwiki-page', $wikiId,
$title->getDBkey() ) ]
3818 'pcGroup' =>
'interwiki-transclude:5',
3819 'pcTTL' => $cache::TTL_PROC_LONG
3823 if ( is_string( $data[
'text'] ) ) {
3824 $text = $data[
'text'];
3825 } elseif ( $data[
'code'] != 200 ) {
3827 $text =
wfMessage(
'scarytranscludefailed-httpstatus' )
3828 ->params( $url, $data[
'code'] )->inContentLanguage()->text();
3830 $text =
wfMessage(
'scarytranscludefailed', $url )->inContentLanguage()->text();
3848 $parts = $piece[
'parts'];
3849 $nameWithSpaces = $frame->
expand( $piece[
'title'] );
3850 $argName = trim( $nameWithSpaces );
3853 if ( $text ===
false && $parts->getLength() > 0
3854 && ( $this->ot[
'html']
3856 || ( $this->ot[
'wiki'] && $frame->
isTemplate() )
3859 # No match in frame, use the supplied default
3860 $object = $parts->item( 0 )->getChildren();
3862 if ( !$this->incrementIncludeSize(
'arg', strlen( $text ) ) ) {
3863 $error =
'<!-- WARNING: argument omitted, expansion size too large -->';
3864 $this->limitationWarn(
'post-expand-template-argument' );
3867 if ( $text ===
false && $object ===
false ) {
3871 if ( $error !==
false ) {
3874 if ( $object !==
false ) {
3875 $ret = [
'object' => $object ];
3877 $ret = [
'text' => $text ];
3900 static $errorStr =
'<span class="error">';
3901 static $errorLen = 20;
3903 $name = $frame->
expand( $params[
'name'] );
3904 if ( substr( $name, 0, $errorLen ) === $errorStr ) {
3910 $attrText = !isset( $params[
'attr'] ) ? null : $frame->
expand( $params[
'attr'] );
3911 if ( substr( $attrText, 0, $errorLen ) === $errorStr ) {
3919 $content = !isset( $params[
'inner'] ) ? null : $frame->
expand( $params[
'inner'] );
3921 $marker = self::MARKER_PREFIX .
"-$name-"
3922 . sprintf(
'%08X', $this->mMarkerIndex++ ) . self::MARKER_SUFFIX;
3924 $markerType =
'general';
3925 if ( $this->ot[
'html'] ) {
3926 $name = strtolower( $name );
3928 if ( isset( $params[
'attributes'] ) ) {
3929 $attributes += $params[
'attributes'];
3932 if ( isset( $this->mTagHooks[$name] ) ) {
3935 $output = call_user_func_array( $this->mTagHooks[$name],
3936 [
$content, $attributes, $this, $frame ] );
3938 $output =
'<span class="error">Invalid tag extension name: ' .
3939 htmlspecialchars( $name ) .
'</span>';
3942 if ( is_array( $output ) ) {
3945 $output = $flags[0];
3946 if ( isset( $flags[
'markerType'] ) ) {
3947 $markerType = $flags[
'markerType'];
3951 if ( $attrText ===
null ) {
3954 if ( isset( $params[
'attributes'] ) ) {
3955 foreach ( $params[
'attributes'] as $attrName => $attrValue ) {
3956 $attrText .=
' ' . htmlspecialchars( $attrName ) .
'="' .
3957 htmlspecialchars( $attrValue ) .
'"';
3961 $output =
"<$name$attrText/>";
3963 $close = $params[
'close'] ===
null ?
'' : $frame->
expand( $params[
'close'] );
3964 if ( substr( $close, 0, $errorLen ) === $errorStr ) {
3968 $output =
"<$name$attrText>$content$close";
3972 if ( $markerType ===
'none' ) {
3974 } elseif ( $markerType ===
'nowiki' ) {
3975 $this->mStripState->addNoWiki( $marker, $output );
3976 } elseif ( $markerType ===
'general' ) {
3977 $this->mStripState->addGeneral( $marker, $output );
3979 throw new MWException( __METHOD__ .
': invalid marker type' );
3992 if ( $this->mIncludeSizes[
$type] + $size > $this->mOptions->getMaxIncludeSize() ) {
3995 $this->mIncludeSizes[
$type] += $size;
4004 $this->mExpensiveFunctionCount++;
4005 return $this->mExpensiveFunctionCount <= $this->mOptions->getExpensiveParserFunctionLimit();
4016 # The position of __TOC__ needs to be recorded
4017 $mw = $this->magicWordFactory->get(
'toc' );
4018 if ( $mw->match( $text ) ) {
4019 $this->mShowToc =
true;
4020 $this->mForceTocPosition =
true;
4022 # Set a placeholder. At the end we'll fill it in with the TOC.
4023 $text = $mw->replace(
'<!--MWTOC\'"-->', $text, 1 );
4025 # Only keep the first one.
4026 $text = $mw->replace(
'', $text );
4029 # Now match and remove the rest of them
4030 $mwa = $this->magicWordFactory->getDoubleUnderscoreArray();
4031 $this->mDoubleUnderscores = $mwa->matchAndRemove( $text );
4033 if ( isset( $this->mDoubleUnderscores[
'nogallery'] ) ) {
4034 $this->mOutput->setNoGallery(
true );
4036 if ( isset( $this->mDoubleUnderscores[
'notoc'] ) && !$this->mForceTocPosition ) {
4037 $this->mShowToc =
false;
4039 if ( isset( $this->mDoubleUnderscores[
'hiddencat'] )
4042 $this->addTrackingCategory(
'hidden-category-category' );
4044 # (T10068) Allow control over whether robots index a page.
4045 # __INDEX__ always overrides __NOINDEX__, see T16899
4046 if ( isset( $this->mDoubleUnderscores[
'noindex'] ) && $this->
getTitle()->canUseNoindex() ) {
4047 $this->mOutput->setIndexPolicy(
'noindex' );
4048 $this->addTrackingCategory(
'noindex-category' );
4050 if ( isset( $this->mDoubleUnderscores[
'index'] ) && $this->
getTitle()->canUseNoindex() ) {
4051 $this->mOutput->setIndexPolicy(
'index' );
4052 $this->addTrackingCategory(
'index-category' );
4055 # Cache all double underscores in the database
4056 foreach ( $this->mDoubleUnderscores as $key => $val ) {
4057 $this->mOutput->setProperty( $key,
'' );
4069 return $this->mOutput->addTrackingCategory( $msg, $this->
getTitle() );
4088 # Inhibit editsection links if requested in the page
4089 if ( isset( $this->mDoubleUnderscores[
'noeditsection'] ) ) {
4090 $maybeShowEditLink =
false;
4092 $maybeShowEditLink =
true;
4095 # Get all headlines for numbering them and adding funky stuff like [edit]
4096 # links - this is for later, but we need the number of headlines right now
4097 # NOTE: white space in headings have been trimmed in handleHeadings. They shouldn't
4098 # be trimmed here since whitespace in HTML headings is significant.
4100 $numMatches = preg_match_all(
4101 '/<H(?P<level>[1-6])(?P<attrib>.*?>)(?P<header>[\s\S]*?)<\/H[1-6] *>/i',
4106 # if there are fewer than 4 headlines in the article, do not show TOC
4107 # unless it's been explicitly enabled.
4108 $enoughToc = $this->mShowToc &&
4109 ( ( $numMatches >= 4 ) || $this->mForceTocPosition );
4111 # Allow user to stipulate that a page should have a "new section"
4112 # link added via __NEWSECTIONLINK__
4113 if ( isset( $this->mDoubleUnderscores[
'newsectionlink'] ) ) {
4114 $this->mOutput->setNewSection(
true );
4117 # Allow user to remove the "new section"
4118 # link via __NONEWSECTIONLINK__
4119 if ( isset( $this->mDoubleUnderscores[
'nonewsectionlink'] ) ) {
4120 $this->mOutput->hideNewSection(
true );
4123 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
4124 # override above conditions and always show TOC above first header
4125 if ( isset( $this->mDoubleUnderscores[
'forcetoc'] ) ) {
4126 $this->mShowToc =
true;
4134 # Ugh .. the TOC should have neat indentation levels which can be
4135 # passed to the skin functions. These are determined here
4139 $sublevelCount = [];
4145 $markerRegex = self::MARKER_PREFIX .
"-h-(\d+)-" . self::MARKER_SUFFIX;
4146 $baseTitleText = $this->
getTitle()->getPrefixedDBkey();
4147 $oldType = $this->mOutputType;
4149 $frame = $this->getPreprocessor()->newFrame();
4150 $root = $this->preprocessToDom( $origText );
4151 $node = $root->getFirstChild();
4156 $headlines = $numMatches !==
false ?
$matches[3] : [];
4158 $maxTocLevel = $this->svcOptions->get(
'MaxTocLevel' );
4159 foreach ( $headlines as $headline ) {
4160 $isTemplate =
false;
4162 $sectionIndex =
false;
4164 $markerMatches = [];
4165 if ( preg_match(
"/^$markerRegex/", $headline, $markerMatches ) ) {
4166 $serial = $markerMatches[1];
4167 list( $titleText, $sectionIndex ) = $this->mHeadings[$serial];
4168 $isTemplate = ( $titleText != $baseTitleText );
4169 $headline = preg_replace(
"/^$markerRegex\\s*/",
"", $headline );
4173 $prevlevel = $level;
4175 $level =
$matches[1][$headlineCount];
4177 if ( $level > $prevlevel ) {
4178 # Increase TOC level
4180 $sublevelCount[$toclevel] = 0;
4181 if ( $toclevel < $maxTocLevel ) {
4182 $prevtoclevel = $toclevel;
4186 } elseif ( $level < $prevlevel && $toclevel > 1 ) {
4187 # Decrease TOC level, find level to jump to
4189 for ( $i = $toclevel; $i > 0; $i-- ) {
4191 if ( $levelCount[$i] == $level ) {
4192 # Found last matching level
4195 } elseif ( $levelCount[$i] < $level ) {
4197 # Found first matching level below current level
4205 if ( $toclevel < $maxTocLevel ) {
4206 if ( $prevtoclevel < $maxTocLevel ) {
4207 # Unindent only if the previous toc level was shown :p
4209 $prevtoclevel = $toclevel;
4215 # No change in level, end TOC line
4216 if ( $toclevel < $maxTocLevel ) {
4221 $levelCount[$toclevel] = $level;
4223 # count number of headlines for each level
4224 $sublevelCount[$toclevel]++;
4226 for ( $i = 1; $i <= $toclevel; $i++ ) {
4227 if ( !empty( $sublevelCount[$i] ) ) {
4231 $numbering .= $this->getTargetLanguage()->formatNum( $sublevelCount[$i] );
4236 # The safe header is a version of the header text safe to use for links
4238 # Remove link placeholders by the link text.
4239 # <!--LINK number-->
4241 # link text with suffix
4242 # Do this before unstrip since link text can contain strip markers
4243 $safeHeadline = $this->replaceLinkHoldersText( $headline );
4245 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
4246 $safeHeadline = $this->mStripState->unstripBoth( $safeHeadline );
4248 # Remove any <style> or <script> tags (T198618)
4249 $safeHeadline = preg_replace(
4250 '#<(style|script)(?: [^>]*[^>/])?>.*?</\1>#is',
4255 # Strip out HTML (first regex removes any tag not allowed)
4257 # * <sup> and <sub> (T10393)
4261 # * <span dir="rtl"> and <span dir="ltr"> (T37167)
4262 # * <s> and <strike> (T35715)
4263 # We strip any parameter from accepted tags (second regex), except dir="rtl|ltr" from <span>,
4264 # to allow setting directionality in toc items.
4265 $tocline = preg_replace(
4267 '#<(?!/?(span|sup|sub|bdi|i|b|s|strike)(?: [^>]*)?>).*?>#',
4268 '#<(/?(?:span(?: dir="(?:rtl|ltr)")?|sup|sub|bdi|i|b|s|strike))(?: .*?)?>#'
4274 # Strip '<span></span>', which is the result from the above if
4275 # <span id="foo"></span> is used to produce an additional anchor
4277 $tocline = str_replace(
'<span></span>',
'', $tocline );
4279 $tocline = trim( $tocline );
4281 # For the anchor, strip out HTML-y stuff period
4282 $safeHeadline = preg_replace(
'/<.*?>/',
'', $safeHeadline );
4285 # Save headline for section edit hint before it's escaped
4286 $headlineHint = $safeHeadline;
4288 # Decode HTML entities
4291 $safeHeadline = self::normalizeSectionName( $safeHeadline );
4296 if ( $fallbackHeadline === $safeHeadline ) {
4297 # No reason to have both (in fact, we can't)
4298 $fallbackHeadline =
false;
4301 # HTML IDs must be case-insensitively unique for IE compatibility (T12721).
4302 # @todo FIXME: We may be changing them depending on the current locale.
4303 $arrayKey = strtolower( $safeHeadline );
4304 if ( $fallbackHeadline ===
false ) {
4305 $fallbackArrayKey =
false;
4307 $fallbackArrayKey = strtolower( $fallbackHeadline );
4310 # Create the anchor for linking from the TOC to the section
4311 $anchor = $safeHeadline;
4312 $fallbackAnchor = $fallbackHeadline;
4313 if ( isset( $refers[$arrayKey] ) ) {
4315 for ( $i = 2; isset( $refers[
"${arrayKey}_$i"] ); ++$i );
4317 $linkAnchor .=
"_$i";
4318 $refers[
"${arrayKey}_$i"] =
true;
4320 $refers[$arrayKey] =
true;
4322 if ( $fallbackHeadline !==
false && isset( $refers[$fallbackArrayKey] ) ) {
4324 for ( $i = 2; isset( $refers[
"${fallbackArrayKey}_$i"] ); ++$i );
4325 $fallbackAnchor .=
"_$i";
4326 $refers[
"${fallbackArrayKey}_$i"] =
true;
4328 $refers[$fallbackArrayKey] =
true;
4331 # Don't number the heading if it is the only one (looks silly)
4332 if ( count(
$matches[3] ) > 1 && $this->mOptions->getNumberHeadings() ) {
4333 # the two are different if the line contains a link
4336 [
'class' =>
'mw-headline-number' ],
4338 ) .
' ' . $headline;
4341 if ( $enoughToc && ( !isset( $maxTocLevel ) || $toclevel < $maxTocLevel ) ) {
4343 $numbering, $toclevel, ( $isTemplate ?
false : $sectionIndex ) );
4346 # Add the section to the section tree
4347 # Find the DOM node for this header
4348 $noOffset = ( $isTemplate || $sectionIndex === false );
4349 while ( $node && !$noOffset ) {
4350 if ( $node->getName() ===
'h' ) {
4351 $bits = $node->splitHeading();
4352 if ( $bits[
'i'] == $sectionIndex ) {
4356 $byteOffset += mb_strlen( $this->mStripState->unstripBoth(
4358 $node = $node->getNextSibling();
4361 'toclevel' => $toclevel,
4364 'number' => $numbering,
4365 'index' => ( $isTemplate ?
'T-' :
'' ) . $sectionIndex,
4366 'fromtitle' => $titleText,
4367 'byteoffset' => ( $noOffset ?
null : $byteOffset ),
4368 'anchor' => $anchor,
4371 # give headline the correct <h#> tag
4372 if ( $maybeShowEditLink && $sectionIndex !==
false ) {
4374 if ( $isTemplate ) {
4375 # Put a T flag in the section identifier, to indicate to extractSections()
4376 # that sections inside <includeonly> should be counted.
4377 $editsectionPage = $titleText;
4378 $editsectionSection =
"T-$sectionIndex";
4379 $editsectionContent =
null;
4381 $editsectionPage = $this->
getTitle()->getPrefixedText();
4382 $editsectionSection = $sectionIndex;
4383 $editsectionContent = $headlineHint;
4397 $editlink =
'<mw:editsection page="' . htmlspecialchars( $editsectionPage );
4398 $editlink .=
'" section="' . htmlspecialchars( $editsectionSection ) .
'"';
4399 if ( $editsectionContent !==
null ) {
4400 $editlink .=
'>' . $editsectionContent .
'</mw:editsection>';
4408 $matches[
'attrib'][$headlineCount], $anchor, $headline,
4409 $editlink, $fallbackAnchor );
4414 $this->setOutputType( $oldType );
4416 # Never ever show TOC if no headers
4417 if ( $numVisible < 1 ) {
4422 if ( $prevtoclevel > 0 && $prevtoclevel < $maxTocLevel ) {
4426 $this->mOutput->setTOCHTML( $toc );
4427 $toc = self::TOC_START . $toc . self::TOC_END;
4431 $this->mOutput->setSections( $tocraw );
4434 # split up and insert constructed headlines
4435 $blocks = preg_split(
'/<H[1-6].*?>[\s\S]*?<\/H[1-6]>/i', $text );
4440 foreach ( $blocks as $block ) {
4442 if ( empty( $head[$i - 1] ) ) {
4443 $sections[$i] = $block;
4445 $sections[$i] = $head[$i - 1] . $block;
4458 $this->hookRunner->onParserSectionCreate( $this, $i, $sections[$i], $maybeShowEditLink );
4463 if ( $enoughToc && $isMain && !$this->mForceTocPosition ) {
4466 $sections[0] .= $toc .
"\n";
4469 $full .= implode(
'', $sections );
4471 if ( $this->mForceTocPosition ) {
4472 return str_replace(
'<!--MWTOC\'"-->', $toc, $full );
4492 if ( $clearState ) {
4493 $magicScopeVariable = $this->lock();
4496 $this->setUser( $user );
4499 $text = str_replace(
"\000",
'', $text );
4507 $text = $this->pstPass2( $text, $user );
4509 $text = $this->mStripState->unstripBoth( $text );
4511 $this->hookRunner->onParserPreSaveTransformComplete( $this, $text );
4513 $this->setUser(
null ); # Reset
4527 # Note: This is the timestamp saved as hardcoded wikitext to the database, we use
4528 # $this->contLang here in order to give everyone the same signature and use the default one
4529 # rather than the one selected in each user's preferences. (see also T14815)
4530 $ts = $this->mOptions->getTimestamp();
4532 $ts = $timestamp->format(
'YmdHis' );
4533 $tzMsg = $timestamp->getTimezoneMessage()->inContentLanguage()->text();
4535 $d = $this->contLang->timeanddate( $ts,
false,
false ) .
" ($tzMsg)";
4537 # Variable replacement
4538 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
4539 $text = $this->replaceVariables( $text );
4541 # This works almost by chance, as the replaceVariables are done before the getUserSig(),
4542 # which may corrupt this parser instance via its wfMessage()->text() call-
4545 if ( strpos( $text,
'~~~' ) !==
false ) {
4546 $sigText = $this->getUserSig( $user );
4547 $text = strtr( $text, [
4549 '~~~~' =>
"$sigText $d",
4552 # The main two signature forms used above are time-sensitive
4553 $this->setOutputFlag(
'user-signature',
'User signature detected' );
4556 # Context links ("pipe tricks"): [[|name]] and [[name (context)|]]
4558 $nc =
'[ _0-9A-Za-z\x80-\xff-]'; # Namespaces can use non-ascii!
4561 $p1 =
"/\[\[(:?$nc+:|:|)($tc+?)( ?\\($tc+\\))\\|]]/";
4563 $p4 =
"/\[\[(:?$nc+:|:|)($tc+?)( ?($tc+))\\|]]/";
4565 $p3 =
"/\[\[(:?$nc+:|:|)($tc+?)( ?\\($tc+\\)|)((?:, |,|، )$tc+|)\\|]]/";
4567 $p2 =
"/\[\[\\|($tc+)]]/";
4569 # try $p1 first, to turn "[[A, B (C)|]]" into "[[A, B (C)|A, B]]"
4570 $text = preg_replace( $p1,
'[[\\1\\2\\3|\\2]]', $text );
4571 $text = preg_replace( $p4,
'[[\\1\\2\\3|\\2]]', $text );
4572 $text = preg_replace( $p3,
'[[\\1\\2\\3\\4|\\2]]', $text );
4576 if ( preg_match(
"/^($nc+:|)$tc+?( \\($tc+\\))$/",
$t, $m ) ) {
4577 $text = preg_replace( $p2,
"[[$m[1]\\1$m[2]|\\1]]", $text );
4578 } elseif ( preg_match(
"/^($nc+:|)$tc+?(, $tc+|)$/",
$t, $m ) &&
"$m[1]$m[2]" !=
'' ) {
4579 $text = preg_replace( $p2,
"[[$m[1]\\1$m[2]|\\1]]", $text );
4581 # if there's no context, don't bother duplicating the title
4582 $text = preg_replace( $p2,
'[[\\1]]', $text );
4605 # If not given, retrieve from the user object.
4606 if ( $nickname ===
false ) {
4607 $nickname = $user->
getOption(
'nickname' );
4610 if ( $fancySig ===
null ) {
4614 if ( $nickname ===
null || $nickname ===
'' ) {
4615 $nickname = $username;
4616 } elseif ( mb_strlen( $nickname ) > $this->svcOptions->get(
'MaxSigChars' ) ) {
4617 $nickname = $username;
4618 $this->logger->debug( __METHOD__ .
": $username has overlong signature." );
4619 } elseif ( $fancySig !==
false ) {
4620 # Sig. might contain markup; validate this
4621 $isValid = $this->validateSig( $nickname ) !==
false;
4624 $sigValidation = $this->svcOptions->get(
'SignatureValidation' );
4625 if ( $isValid && $sigValidation ===
'disallow' ) {
4631 $isValid = !$validator->validateSignature( $nickname );
4635 # Validated; clean up (if needed) and return it
4636 return $this->cleanSig( $nickname,
true );
4638 # Failed to validate; fall back to the default
4639 $nickname = $username;
4640 $this->logger->debug( __METHOD__ .
": $username has invalid signature." );
4644 # Make sure nickname doesnt get a sig in a sig
4645 $nickname = self::cleanSigInSig( $nickname );
4647 # If we're still here, make it a link to the user page
4650 $msgName = $user->
isAnon() ?
'signature-anon' :
'signature';
4652 return wfMessage( $msgName, $userText, $nickText )->inContentLanguage()
4653 ->title( $this->
getTitle() )->text();
4679 $magicScopeVariable = $this->lock();
4688 # Option to disable this feature
4689 if ( !$this->mOptions->getCleanSignatures() ) {
4693 # @todo FIXME: Regex doesn't respect extension tags or nowiki
4694 # => Move this logic to braceSubstitution()
4695 $substWord = $this->magicWordFactory->get(
'subst' );
4696 $substRegex =
'/\{\{(?!(?:' . $substWord->getBaseRegex() .
'))/x' . $substWord->getRegexCase();
4697 $substText =
'{{' . $substWord->getSynonym( 0 );
4699 $text = preg_replace( $substRegex, $substText, $text );
4700 $text = self::cleanSigInSig( $text );
4701 $dom = $this->preprocessToDom( $text );
4702 $frame = $this->getPreprocessor()->newFrame();
4703 $text = $frame->expand( $dom );
4706 $text = $this->mStripState->unstripBoth( $text );
4719 $text = preg_replace(
'/~{3,5}/',
'', $text );
4734 $outputType, $clearState =
true, $revId =
null
4736 $this->startParse(
$title, $options, $outputType, $clearState );
4737 if ( $revId !==
null ) {
4738 $this->mRevisionId = $revId;
4749 $outputType, $clearState =
true
4751 $this->setTitle(
$title );
4752 $this->mOptions = $options;
4753 $this->setOutputType( $outputType );
4754 if ( $clearState ) {
4755 $this->clearState();
4768 static $executing =
false;
4770 # Guard against infinite recursion
4781 $text = $this->preprocess( $text,
$title, $options );
4811 public function setHook( $tag, callable $callback ) {
4812 $tag = strtolower( $tag );
4813 if ( preg_match(
'/[<>\r\n]/', $tag, $m ) ) {
4814 throw new MWException(
"Invalid character {$m[0]} in setHook('$tag', ...) call" );
4816 $oldVal = $this->mTagHooks[$tag] ??
null;
4817 $this->mTagHooks[$tag] = $callback;
4818 if ( !in_array( $tag, $this->mStripList ) ) {
4819 $this->mStripList[] = $tag;
4829 $this->mTagHooks = [];
4830 $this->mStripList = [];
4877 $oldVal = $this->mFunctionHooks[$id][0] ??
null;
4878 $this->mFunctionHooks[$id] = [ $callback, $flags ];
4880 # Add to function cache
4881 $mw = $this->magicWordFactory->get( $id );
4883 throw new MWException( __METHOD__ .
'() expecting a magic word identifier.' );
4886 $synonyms = $mw->getSynonyms();
4887 $sensitive = intval( $mw->isCaseSensitive() );
4889 foreach ( $synonyms as $syn ) {
4891 if ( !$sensitive ) {
4892 $syn = $this->contLang->lc( $syn );
4898 # Remove trailing colon
4899 if ( substr( $syn, -1, 1 ) ===
':' ) {
4900 $syn = substr( $syn, 0, -1 );
4902 $this->mFunctionSynonyms[$sensitive][$syn] = $id;
4913 $this->firstCallInit();
4914 return array_keys( $this->mFunctionHooks );
4926 $this->replaceLinkHoldersPrivate( $text, $options );
4937 $this->mLinkHolders->replace( $text );
4948 return $this->mLinkHolders->replaceText( $text );
4967 if ( isset( $params[
'mode'] ) ) {
4968 $mode = $params[
'mode'];
4973 }
catch ( Exception $e ) {
4978 $ig->setContextTitle( $this->
getTitle() );
4979 $ig->setShowBytes(
false );
4980 $ig->setShowDimensions(
false );
4981 $ig->setShowFilename(
false );
4982 $ig->setParser( $this );
4983 $ig->setHideBadImages();
4986 if ( isset( $params[
'showfilename'] ) ) {
4987 $ig->setShowFilename(
true );
4989 $ig->setShowFilename(
false );
4991 if ( isset( $params[
'caption'] ) ) {
4995 $caption = $this->recursiveTagParse( $params[
'caption'] );
4996 $ig->setCaptionHtml( $caption );
4998 if ( isset( $params[
'perrow'] ) ) {
4999 $ig->setPerRow( $params[
'perrow'] );
5001 if ( isset( $params[
'widths'] ) ) {
5002 $ig->setWidths( $params[
'widths'] );
5004 if ( isset( $params[
'heights'] ) ) {
5005 $ig->setHeights( $params[
'heights'] );
5007 $ig->setAdditionalOptions( $params );
5009 $this->hookRunner->onBeforeParserrenderImageGallery( $this, $ig );
5013 # match lines like these:
5014 # Image:someimage.jpg|This is some image
5022 if ( strpos(
$matches[0],
'%' ) !==
false ) {
5027 # Bogus title. Ignore these so we don't bomb out later.
5031 # We need to get what handler the file uses, to figure out parameters.
5032 # Note, a hook can overide the file name, and chose an entirely different
5033 # file (which potentially could be of a different type and have different handler).
5036 $this->hookRunner->onBeforeParserFetchFileAndTitle(
5037 $this,
$title, $options, $descQuery );
5038 # Don't register it now, as TraditionalImageGallery does that later.
5039 $file = $this->fetchFileNoRegister(
$title, $options );
5040 $handler =
$file ?
$file->getHandler() :
false;
5043 'img_alt' =>
'gallery-internal-alt',
5044 'img_link' =>
'gallery-internal-link',
5047 $paramMap += $handler->getParamMap();
5050 unset( $paramMap[
'img_width'] );
5053 $mwArray = $this->magicWordFactory->newArray( array_keys( $paramMap ) );
5058 $handlerOptions = [];
5072 foreach ( $parameterMatches as $parameterMatch ) {
5073 list( $magicName, $match ) = $mwArray->matchVariableStartToEnd( $parameterMatch );
5075 $paramName = $paramMap[$magicName];
5077 switch ( $paramName ) {
5078 case 'gallery-internal-alt':
5079 $alt = $this->stripAltText( $match,
false );
5081 case 'gallery-internal-link':
5082 $linkValue = $this->stripAltText( $match,
false );
5083 if ( preg_match(
'/^-{R|(.*)}-$/', $linkValue ) ) {
5086 $linkValue = substr( $linkValue, 4, -2 );
5088 list(
$type, $target ) = $this->parseLinkParameter( $linkValue );
5089 if (
$type ===
'link-url' ) {
5091 $this->mOutput->addExternalLink( $target );
5092 } elseif (
$type ===
'link-title' ) {
5093 $link = $target->getLinkURL();
5094 $this->mOutput->addLink( $target );
5099 if ( $handler->validateParam( $paramName, $match ) ) {
5100 $handlerOptions[$paramName] = $match;
5103 $this->logger->debug(
5104 "$parameterMatch failed parameter validation" );
5105 $label = $parameterMatch;
5111 $label = $parameterMatch;
5116 $ig->add(
$title, $label, $alt, $link, $handlerOptions );
5118 $html = $ig->toHTML();
5119 $this->hookRunner->onAfterParserFetchFileAndTitle( $this, $ig, $html );
5129 $handlerClass = get_class( $handler );
5133 if ( !isset( $this->mImageParams[$handlerClass] ) ) {
5134 # Initialise static lists
5135 static $internalParamNames = [
5136 'horizAlign' => [
'left',
'right',
'center',
'none' ],
5137 'vertAlign' => [
'baseline',
'sub',
'super',
'top',
'text-top',
'middle',
5138 'bottom',
'text-bottom' ],
5139 'frame' => [
'thumbnail',
'manualthumb',
'framed',
'frameless',
5140 'upright',
'border',
'link',
'alt',
'class' ],
5142 static $internalParamMap;
5143 if ( !$internalParamMap ) {
5144 $internalParamMap = [];
5145 foreach ( $internalParamNames as
$type => $names ) {
5146 foreach ( $names as $name ) {
5152 $magicName = str_replace(
'-',
'_',
"img_$name" );
5153 $internalParamMap[$magicName] = [
$type, $name ];
5158 # Add handler params
5159 $paramMap = $internalParamMap;
5161 $handlerParamMap = $handler->getParamMap();
5162 foreach ( $handlerParamMap as $magic => $paramName ) {
5163 $paramMap[$magic] = [
'handler', $paramName ];
5166 $this->mImageParams[$handlerClass] = $paramMap;
5167 $this->mImageParamsMagicArray[$handlerClass] =
5168 $this->magicWordFactory->newArray( array_keys( $paramMap ) );
5170 return [ $this->mImageParams[$handlerClass], $this->mImageParamsMagicArray[$handlerClass] ];
5182 # Check if the options text is of the form "options|alt text"
5184 # * thumbnail make a thumbnail with enlarge-icon and caption, alignment depends on lang
5185 # * left no resizing, just left align. label is used for alt= only
5186 # * right same, but right aligned
5187 # * none same, but not aligned
5188 # * ___px scale to ___ pixels width, no aligning. e.g. use in taxobox
5189 # * center center the image
5190 # * frame Keep original image size, no magnify-button.
5191 # * framed Same as "frame"
5192 # * frameless like 'thumb' but without a frame. Keeps user preferences for width
5193 # * upright reduce width for upright images, rounded to full __0 px
5194 # * border draw a 1px border around the image
5195 # * alt Text for HTML alt attribute (defaults to empty)
5196 # * class Set a class for img node
5197 # * link Set the target of the image link. Can be external, interwiki, or local
5198 # vertical-align values (no % or length right now):
5208 # Protect LanguageConverter markup when splitting into parts
5210 '-{',
'}-',
'|', $options,
true
5213 # Give extensions a chance to select the file revision for us
5216 $this->hookRunner->onBeforeParserFetchFileAndTitle(
5217 $this,
$title, $options, $descQuery );
5218 # Fetch and register the file (file title may be different via hooks)
5222 $handler =
$file ?
$file->getHandler() :
false;
5224 list( $paramMap, $mwArray ) = $this->getImageParams( $handler );
5227 $this->addTrackingCategory(
'broken-file-category' );
5230 # Process the input parameters
5232 $params = [
'frame' => [],
'handler' => [],
5233 'horizAlign' => [],
'vertAlign' => [] ];
5234 $seenformat =
false;
5235 foreach ( $parts as $part ) {
5236 $part = trim( $part );
5237 list( $magicName, $value ) = $mwArray->matchVariableStartToEnd( $part );
5239 if ( isset( $paramMap[$magicName] ) ) {
5240 list(
$type, $paramName ) = $paramMap[$magicName];
5242 # Special case; width and height come in one variable together
5243 if (
$type ===
'handler' && $paramName ===
'width' ) {
5244 $parsedWidthParam = self::parseWidthParam( $value );
5245 if ( isset( $parsedWidthParam[
'width'] ) ) {
5246 $width = $parsedWidthParam[
'width'];
5247 if ( $handler->validateParam(
'width', $width ) ) {
5248 $params[
$type][
'width'] = $width;
5252 if ( isset( $parsedWidthParam[
'height'] ) ) {
5253 $height = $parsedWidthParam[
'height'];
5254 if ( $handler->validateParam(
'height', $height ) ) {
5255 $params[
$type][
'height'] = $height;
5259 # else no validation -- T15436
5261 if (
$type ===
'handler' ) {
5262 # Validate handler parameter
5263 $validated = $handler->validateParam( $paramName, $value );
5265 # Validate internal parameters
5266 switch ( $paramName ) {
5270 # @todo FIXME: Possibly check validity here for
5271 # manualthumb? downstream behavior seems odd with
5272 # missing manual thumbs.
5274 $value = $this->stripAltText( $value, $holders );
5277 list( $paramName, $value ) =
5278 $this->parseLinkParameter(
5279 $this->stripAltText( $value, $holders )
5283 if ( $paramName ===
'no-link' ) {
5286 if ( ( $paramName ===
'link-url' ) && $this->mOptions->getExternalLinkTarget() ) {
5287 $params[
$type][
'link-target'] = $this->mOptions->getExternalLinkTarget();
5295 $validated = !$seenformat;
5299 # Most other things appear to be empty or numeric...
5300 $validated = ( $value ===
false || is_numeric( trim( $value ) ) );
5305 $params[
$type][$paramName] = $value;
5309 if ( !$validated ) {
5314 # Process alignment parameters
5316 if ( $params[
'horizAlign'] ) {
5317 $params[
'frame'][
'align'] = key( $params[
'horizAlign'] );
5320 if ( $params[
'vertAlign'] ) {
5321 $params[
'frame'][
'valign'] = key( $params[
'vertAlign'] );
5324 $params[
'frame'][
'caption'] = $caption;
5326 # Will the image be presented in a frame, with the caption below?
5327 $imageIsFramed = isset( $params[
'frame'][
'frame'] )
5328 || isset( $params[
'frame'][
'framed'] )
5329 || isset( $params[
'frame'][
'thumbnail'] )
5330 || isset( $params[
'frame'][
'manualthumb'] );
5332 # In the old days, [[Image:Foo|text...]] would set alt text. Later it
5333 # came to also set the caption, ordinary text after the image -- which
5334 # makes no sense, because that just repeats the text multiple times in
5335 # screen readers. It *also* came to set the title attribute.
5336 # Now that we have an alt attribute, we should not set the alt text to
5337 # equal the caption: that's worse than useless, it just repeats the
5338 # text. This is the framed/thumbnail case. If there's no caption, we
5339 # use the unnamed parameter for alt text as well, just for the time be-
5340 # ing, if the unnamed param is set and the alt param is not.
5341 # For the future, we need to figure out if we want to tweak this more,
5342 # e.g., introducing a title= parameter for the title; ignoring the un-
5343 # named parameter entirely for images without a caption; adding an ex-
5344 # plicit caption= parameter and preserving the old magic unnamed para-
5346 if ( $imageIsFramed ) { # Framed image
5347 if ( $caption ===
'' && !isset( $params[
'frame'][
'alt'] ) ) {
5348 # No caption or alt text, add the filename as the alt text so
5349 # that screen readers at least get some description of the image
5350 $params[
'frame'][
'alt'] =
$title->getText();
5352 # Do not set $params['frame']['title'] because tooltips don't make sense
5354 }
else { # Inline image
5355 if ( !isset( $params[
'frame'][
'alt'] ) ) {
5356 # No alt text, use the "caption" for the alt text
5357 if ( $caption !==
'' ) {
5358 $params[
'frame'][
'alt'] = $this->stripAltText( $caption, $holders );
5360 # No caption, fall back to using the filename for the
5362 $params[
'frame'][
'alt'] =
$title->getText();
5365 # Use the "caption" for the tooltip text
5366 $params[
'frame'][
'title'] = $this->stripAltText( $caption, $holders );
5368 $params[
'handler'][
'targetlang'] = $this->getTargetLanguage()->getCode();
5370 $this->hookRunner->onParserMakeImageParams(
$title,
$file, $params, $this );
5372 # Linker does the rest
5373 $time = $options[
'time'] ??
false;
5375 $time, $descQuery, $this->mOptions->getThumbSize() );
5377 # Give the handler a chance to modify the parser object
5379 $handler->parserTransformHook( $this,
$file );
5404 $chars = self::EXT_LINK_URL_CLASS;
5405 $addr = self::EXT_LINK_ADDR;
5406 $prots = $this->mUrlProtocols;
5409 if ( $value ===
'' ) {
5411 } elseif ( preg_match(
"/^((?i)$prots)/", $value ) ) {
5412 if ( preg_match(
"/^((?i)$prots)$addr$chars*$/u", $value, $m ) ) {
5413 $this->mOutput->addExternalLink( $value );
5420 $this->mOutput->addLink( $linkTitle );
5421 $type =
'link-title';
5422 $target = $linkTitle;
5425 return [
$type, $target ];
5434 # Strip bad stuff out of the title (tooltip). We can't just use
5435 # replaceLinkHoldersText() here, because if this function is called
5436 # from handleInternalLinks2(), mLinkHolders won't be up-to-date.
5438 $tooltip = $holders->replaceText( $caption );
5440 $tooltip = $this->replaceLinkHoldersText( $caption );
5443 # make sure there are no placeholders in thumbnail attributes
5444 # that are later expanded to html- so expand them now and
5446 $tooltip = $this->mStripState->unstripBoth( $tooltip );
5447 # Compatibility hack! In HTML certain entity references not terminated
5448 # by a semicolon are decoded (but not if we're in an attribute; that's
5449 # how link URLs get away without properly escaping & in queries).
5450 # But wikitext has always required semicolon-termination of entities,
5451 # so encode & where needed to avoid decode of semicolon-less entities.
5453 # https://www.w3.org/TR/html5/syntax.html#named-character-references
5454 # T210437 discusses moving this workaround to Sanitizer::stripAllTags.
5455 $tooltip = preg_replace(
"/
5456 & # 1. entity prefix
5457 (?= # 2. followed by:
5458 (?: # a. one of the legacy semicolon-less named entities
5459 A(?:Elig|MP|acute|circ|grave|ring|tilde|uml)|
5460 C(?:OPY|cedil)|E(?:TH|acute|circ|grave|uml)|
5461 GT|I(?:acute|circ|grave|uml)|LT|Ntilde|
5462 O(?:acute|circ|grave|slash|tilde|uml)|QUOT|REG|THORN|
5463 U(?:acute|circ|grave|uml)|Yacute|
5464 a(?:acute|c(?:irc|ute)|elig|grave|mp|ring|tilde|uml)|brvbar|
5465 c(?:cedil|edil|urren)|cent(?!erdot;)|copy(?!sr;)|deg|
5466 divide(?!ontimes;)|e(?:acute|circ|grave|th|uml)|
5467 frac(?:1(?:2|4)|34)|
5468 gt(?!c(?:c|ir)|dot|lPar|quest|r(?:a(?:pprox|rr)|dot|eq(?:less|qless)|less|sim);)|
5469 i(?:acute|circ|excl|grave|quest|uml)|laquo|
5470 lt(?!c(?:c|ir)|dot|hree|imes|larr|quest|r(?:Par|i(?:e|f|));)|
5471 m(?:acr|i(?:cro|ddot))|n(?:bsp|tilde)|
5472 not(?!in(?:E|dot|v(?:a|b|c)|)|ni(?:v(?:a|b|c)|);)|
5473 o(?:acute|circ|grave|rd(?:f|m)|slash|tilde|uml)|
5474 p(?:lusmn|ound)|para(?!llel;)|quot|r(?:aquo|eg)|
5475 s(?:ect|hy|up(?:1|2|3)|zlig)|thorn|times(?!b(?:ar|)|d;)|
5476 u(?:acute|circ|grave|ml|uml)|y(?:acute|en|uml)
5478 (?:[^;]|$)) # b. and not followed by a semicolon
5479 # S = study, for efficiency
5480 /Sx",
'&', $tooltip );
5497 $text = $this->replaceVariables( $text, $frame );
5498 $text = $this->mStripState->unstripBoth( $text );
5508 $this->firstCallInit();
5509 return array_keys( $this->mTagHooks );
5517 $this->firstCallInit();
5518 return $this->mFunctionSynonyms;
5526 return $this->mUrlProtocols;
5559 global
$wgTitle; # not generally used but removes an ugly failure mode
5561 $magicScopeVariable = $this->lock();
5569 $frame = $this->getPreprocessor()->newFrame();
5571 # Process section extraction flags
5573 $sectionParts = explode(
'-', $sectionId );
5574 $sectionIndex = array_pop( $sectionParts );
5575 foreach ( $sectionParts as $part ) {
5576 if ( $part ===
'T' ) {
5581 # Check for empty input
5582 if ( strval( $text ) ===
'' ) {
5583 # Only sections 0 and T-0 exist in an empty document
5584 if ( $sectionIndex == 0 ) {
5585 if ( $mode ===
'get' ) {
5591 if ( $mode ===
'get' ) {
5599 # Preprocess the text
5600 $root = $this->preprocessToDom( $text, $flags );
5602 # <h> nodes indicate section breaks
5603 # They can only occur at the top level, so we can find them by iterating the root's children
5604 $node = $root->getFirstChild();
5606 # Find the target section
5607 if ( $sectionIndex == 0 ) {
5608 # Section zero doesn't nest, level=big
5609 $targetLevel = 1000;
5612 if ( $node->getName() ===
'h' ) {
5613 $bits = $node->splitHeading();
5614 if ( $bits[
'i'] == $sectionIndex ) {
5615 $targetLevel = $bits[
'level'];
5619 if ( $mode ===
'replace' ) {
5622 $node = $node->getNextSibling();
5628 if ( $mode ===
'get' ) {
5635 # Find the end of the section, including nested sections
5637 if ( $node->getName() ===
'h' ) {
5638 $bits = $node->splitHeading();
5639 $curLevel = $bits[
'level'];
5640 if ( $bits[
'i'] != $sectionIndex && $curLevel <= $targetLevel ) {
5644 if ( $mode ===
'get' ) {
5647 $node = $node->getNextSibling();
5650 # Write out the remainder (in replace mode only)
5651 if ( $mode ===
'replace' ) {
5652 # Output the replacement text
5653 # Add two newlines on -- trailing whitespace in $newText is conventionally
5654 # stripped by the editor, so we need both newlines to restore the paragraph gap
5655 # Only add trailing whitespace if there is newText
5656 if ( $newText !=
"" ) {
5657 $outText .= $newText .
"\n\n";
5662 $node = $node->getNextSibling();
5666 # Re-insert stripped tags
5667 $outText = rtrim( $this->mStripState->unstripBoth( $outText ) );
5686 public function getSection( $text, $sectionId, $defaultText =
'' ) {
5687 return $this->extractSections( $text, $sectionId,
'get', $defaultText );
5703 return $this->extractSections( $oldText, $sectionId,
'replace', $newText );
5736 $magicScopeVariable = $this->lock();
5743 $frame = $this->getPreprocessor()->newFrame();
5744 $root = $this->preprocessToDom( $text, 0 );
5745 $node = $root->getFirstChild();
5758 if ( $node->getName() ===
'h' ) {
5759 $bits = $node->splitHeading();
5760 $sections[] = $currentSection;
5762 'index' => $bits[
'i'],
5763 'level' => $bits[
'level'],
5764 'offset' => $offset,
5765 'heading' => $nodeText,
5769 $currentSection[
'text'] .= $nodeText;
5771 $offset += strlen( $nodeText );
5772 $node = $node->getNextSibling();
5774 $sections[] = $currentSection;
5789 return $this->mRevisionId;
5802 if ( $this->mRevisionObject ) {
5803 return $this->mRevisionObject;
5806 $this->mRevisionObject =
null;
5808 $revRecord = $this->getRevisionRecordObject();
5810 $this->mRevisionObject =
new Revision( $revRecord );
5813 return $this->mRevisionObject;
5823 if ( $this->mRevisionRecordObject ) {
5824 return $this->mRevisionRecordObject;
5834 $rev = call_user_func(
5835 $this->mOptions->getCurrentRevisionRecordCallback(),
5840 if ( $rev ===
false ) {
5848 if ( $this->mRevisionId ===
null && $rev && $rev->getId() ) {
5860 if ( $this->mRevisionId && $rev && $rev->getId() != $this->mRevisionId ) {
5861 $rev = MediaWikiServices::getInstance()
5862 ->getRevisionLookup()
5863 ->getRevisionById( $this->mRevisionId );
5866 $this->mRevisionRecordObject = $rev;
5868 return $this->mRevisionRecordObject;
5877 if ( $this->mRevisionTimestamp !==
null ) {
5878 return $this->mRevisionTimestamp;
5881 # Use specified revision timestamp, falling back to the current timestamp
5882 $revObject = $this->getRevisionRecordObject();
5883 $timestamp = $revObject ? $revObject->getTimestamp() : $this->mOptions->getTimestamp();
5884 $this->mOutput->setRevisionTimestampUsed( $timestamp );
5886 # The cryptic '' timezone parameter tells to use the site-default
5887 # timezone offset instead of the user settings.
5888 # Since this value will be saved into the parser cache, served
5889 # to other users, and potentially even used inside links and such,
5890 # it needs to be consistent for all visitors.
5891 $this->mRevisionTimestamp = $this->contLang->userAdjust( $timestamp,
'' );
5893 return $this->mRevisionTimestamp;
5902 if ( $this->mRevisionUser ===
null ) {
5903 $revObject = $this->getRevisionRecordObject();
5905 # if this template is subst: the revision id will be blank,
5906 # so just use the current user's name
5907 if ( $revObject && $revObject->getUser() ) {
5908 $this->mRevisionUser = $revObject->getUser()->getName();
5909 } elseif ( $this->ot[
'wiki'] || $this->mOptions->getIsPreview() ) {
5910 $this->mRevisionUser = $this->
getUser()->getName();
5912 # Note that we fall through here with
5913 # $this->mRevisionUser still null
5916 return $this->mRevisionUser;
5925 if ( $this->mRevisionSize ===
null ) {
5926 $revObject = $this->getRevisionRecordObject();
5928 # if this variable is subst: the revision id will be blank,
5929 # so just use the parser input size, because the own substituation
5930 # will change the size.
5932 $this->mRevisionSize = $revObject->getSize();
5934 $this->mRevisionSize = $this->mInputSize;
5937 return $this->mRevisionSize;
5946 $this->mDefaultSort = $sort;
5947 $this->mOutput->setProperty(
'defaultsort', $sort );
5961 if ( $this->mDefaultSort !==
false ) {
5962 return $this->mDefaultSort;
5975 return $this->mDefaultSort;
5981 $text = self::normalizeSectionName( $text );
5990 $fragmentMode = $this->svcOptions->get(
'FragmentMode' );
5991 if ( isset( $fragmentMode[1] ) && $fragmentMode[1] ===
'legacy' ) {
6010 # Strip out wikitext links(they break the anchor)
6011 $text = $this->stripSectionName( $text );
6012 $sectionName = self::getSectionNameFromStrippedText( $text );
6013 return self::makeAnchor( $sectionName );
6026 # Strip out wikitext links(they break the anchor)
6027 $text = $this->stripSectionName( $text );
6028 $sectionName = self::getSectionNameFromStrippedText( $text );
6029 return $this->makeLegacyAnchor( $sectionName );
6038 $sectionName = self::getSectionNameFromStrippedText( $text );
6039 return self::makeAnchor( $sectionName );
6049 # T90902: ensure the same normalization is applied for IDs as to links
6051 $titleParser = MediaWikiServices::getInstance()->getTitleParser();
6052 '@phan-var MediaWikiTitleCodec $titleParser';
6055 $parts = $titleParser->splitTitleString(
"#$text" );
6059 return $parts[
'fragment'];
6077 # Strip internal link markup
6078 $text = preg_replace(
'/\[\[:?([^[|]+)\|([^[]+)\]\]/',
'$2', $text );
6079 $text = preg_replace(
'/\[\[:?([^[]+)\|?\]\]/',
'$1', $text );
6081 # Strip external link markup
6082 # @todo FIXME: Not tolerant to blank link text
6083 # I.E. [https://www.mediawiki.org] will render as [1] or something depending
6084 # on how many empty links there are on the page - need to figure that out.
6085 $text = preg_replace(
'/\[(?i:' . $this->mUrlProtocols .
')([^ ]+?) ([^[]+)\]/',
'$2', $text );
6087 # Parse wikitext quotes (italics & bold)
6088 $text = $this->doQuotes( $text );
6108 $magicScopeVariable = $this->lock();
6109 $this->startParse(
$title, $options, $outputType,
true );
6111 $text = $this->replaceVariables( $text );
6112 $text = $this->mStripState->unstripBoth( $text );
6124 return $this->preSaveTransform( $text,
$title, $options->
getUser(), $options );
6157 while ( $i < strlen(
$s ) ) {
6158 $markerStart = strpos(
$s, self::MARKER_PREFIX, $i );
6159 if ( $markerStart ===
false ) {
6160 $out .= call_user_func( $callback, substr(
$s, $i ) );
6163 $out .= call_user_func( $callback, substr(
$s, $i, $markerStart - $i ) );
6164 $markerEnd = strpos(
$s, self::MARKER_SUFFIX, $markerStart );
6165 if ( $markerEnd ===
false ) {
6166 $out .= substr(
$s, $markerStart );
6169 $markerEnd += strlen( self::MARKER_SUFFIX );
6170 $out .= substr(
$s, $markerStart, $markerEnd - $markerStart );
6185 return $this->mStripState->killMarkers( $text );
6199 $parsedWidthParam = [];
6200 if ( $value ===
'' ) {
6201 return $parsedWidthParam;
6204 # (T15500) In both cases (width/height and width only),
6205 # permit trailing "px" for backward compatibility.
6206 if ( $parseHeight && preg_match(
'/^([0-9]*)x([0-9]*)\s*(?:px)?\s*$/', $value, $m ) ) {
6207 $width = intval( $m[1] );
6208 $height = intval( $m[2] );
6209 $parsedWidthParam[
'width'] = $width;
6210 $parsedWidthParam[
'height'] = $height;
6211 } elseif ( preg_match(
'/^[0-9]*\s*(?:px)?\s*$/', $value ) ) {
6212 $width = intval( $value );
6213 $parsedWidthParam[
'width'] = $width;
6215 return $parsedWidthParam;
6228 if ( $this->mInParse ) {
6229 throw new MWException(
"Parser state cleared while parsing. "
6230 .
"Did you call Parser::parse recursively? Lock is held by: " . $this->mInParse );
6236 $this->mInParse = $e->getTraceAsString();
6238 $recursiveCheck =
new ScopedCallback(
function () {
6239 $this->mInParse =
false;
6242 return $recursiveCheck;
6257 if ( preg_match(
'/^<p>(.*)\n?<\/p>\n?$/sU', $html, $m ) && strpos( $m[1],
'</p>' ) ===
false ) {
6275 if ( $this->mInParse ) {
6276 return $this->factory->create();
6292 $this->mOutput->setEnableOOUI(
true );
6302 $this->mOutput->setFlag( $flag );
6303 $name = $this->
getTitle()->getPrefixedText();
6304 $this->logger->debug( __METHOD__ .
": set $flag flag on '$name'; $reason" );