27 use Wikimedia\ScopedCallback;
75 const VERSION =
'1.6.4';
81 const HALF_PARSED_VERSION = 2;
83 # Flags for Parser::setFunctionHook
87 # Constants needed for external link processing
88 # Everything except bracket, space, or control characters
89 # \p{Zs} is unicode 'separator, space' category. It covers the space 0x20
90 # as well as U+3000 is IDEOGRAPHIC SPACE for T21052
91 # \x{FFFD} is the Unicode replacement character, which Preprocessor_DOM
92 # uses to replace invalid HTML characters.
93 const EXT_LINK_URL_CLASS =
'[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}]';
94 # Simplified expression to match an IPv4 or IPv6 address, or
95 # at least one character of a host name (embeds EXT_LINK_URL_CLASS)
96 const EXT_LINK_ADDR =
'(?:[0-9.]+|\\[(?i:[0-9a-f:.]+)\\]|[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}])';
97 # RegExp to make image URLs (embeds IPv6 part of EXT_LINK_ADDR)
99 const EXT_IMAGE_REGEX =
'/^(http:\/\/|https:\/\/)((?:\\[(?i:[0-9a-f:.]+)\\])?[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}]+)
100 \\/([A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]+)\\.((?i)gif|png|jpg|jpeg)$/Sxu';
102 # Regular expression for a non-newline space
103 const SPACE_NOT_NL =
'(?:\t| |&\#0*160;|&\#[Xx]0*[Aa]0;|\p{Zs})';
105 # Flags for preprocessToDom
106 const PTD_FOR_INCLUSION = 1;
108 # Allowed values for $this->mOutputType
109 # Parameter to startExternalParse().
111 const
OT_WIKI = 2;
# like preSaveTransform()
114 const
OT_PLAIN = 4;
# like extractSections() - portions of the original are returned unchanged.
133 const MARKER_SUFFIX =
"-QINU`\"'\x7f";
134 const MARKER_PREFIX =
"\x7f'\"`UNIQ-";
136 # Markers used for wrapping the table of contents
137 const TOC_START =
'<mw:toc>';
138 const TOC_END =
'</mw:toc>';
144 public $mTagHooks = [];
145 public $mTransparentTagHooks = [];
146 public $mFunctionHooks = [];
147 public $mFunctionSynonyms = [ 0 => [], 1 => [] ];
148 public $mFunctionTagHooks = [];
149 public $mStripList = [];
150 public $mDefaultStripList = [];
151 public $mVarCache = [];
152 public $mImageParams = [];
153 public $mImageParamsMagicArray = [];
154 public $mMarkerIndex = 0;
158 public $mFirstCall =
true;
160 # Initialised by initialiseVariables()
171 # Initialised in constructor
172 public $mConf, $mExtLinkBracketedRegex, $mUrlProtocols;
174 # Initialized in getPreprocessor()
176 public $mPreprocessor;
178 # Cleared with clearState():
190 public $mIncludeCount;
194 public $mLinkHolders;
197 public $mIncludeSizes, $mPPNodeCount, $mGeneratedPPNodeCount, $mHighestExpansionDepth;
198 public $mDefaultSort;
199 public $mTplRedirCache, $mTplDomCache, $mHeadings, $mDoubleUnderscores;
200 public $mExpensiveFunctionCount; # number
of expensive
parser function calls
201 public $mShowToc, $mForceTocPosition;
209 # These are variables reset at least once per parse regardless of $clearState
220 public $mOutputType; # Output
type, one
of the OT_xxx constants
221 public $ot; # Shortcut alias,
see setOutputType()
222 public $mRevisionObject;
# The revision object of the specified revision ID
223 public $mRevisionId; # ID to
display in {{REVISIONID}} tags
226 public $mRevisionSize; # Size to
display in {{REVISIONSIZE}} variable
227 public $mRevIdForTs; # The
revision ID which was
used to fetch the timestamp
228 public $mInputSize =
false; # For {{PAGESIZE}}
on current
page.
234 public $mUniqPrefix = self::MARKER_PREFIX;
241 public $mLangLinkLanguages;
249 public $currentRevisionCache;
255 public $mInParse =
false;
258 protected $mProfiler;
263 protected $mLinkRenderer;
266 private $magicWordFactory;
275 private $specialPageFactory;
281 private $linkRendererFactory;
293 public function __construct(
299 $this->mConf = $parserConf;
301 $this->mExtLinkBracketedRegex =
'/\[(((?i)' . $this->mUrlProtocols .
')' .
302 self::EXT_LINK_ADDR .
303 self::EXT_LINK_URL_CLASS .
'*)\p{Zs}*([^\]\\x00-\\x08\\x0a-\\x1F\\x{FFFD}]*?)\]/Su';
304 if ( isset( $parserConf[
'preprocessorClass'] ) ) {
305 $this->mPreprocessorClass = $parserConf[
'preprocessorClass'];
307 # Under HHVM Preprocessor_Hash is much faster than Preprocessor_DOM
309 } elseif ( extension_loaded(
'domxml' ) ) {
310 # PECL extension that conflicts with the core DOM extension (T15770)
311 wfDebug(
"Warning: you have the obsolete domxml extension for PHP. Please remove it!\n" );
313 } elseif ( extension_loaded(
'dom' ) ) {
318 wfDebug( __CLASS__ .
": using preprocessor: {$this->mPreprocessorClass}\n" );
320 $services = MediaWikiServices::getInstance();
321 $this->magicWordFactory = $magicWordFactory ??
324 $this->contLang = $contLang ??
$services->getContentLanguage();
326 $this->factory = $factory ??
$services->getParserFactory();
327 $this->specialPageFactory = $spFactory ??
$services->getSpecialPageFactory();
328 $this->siteConfig = $siteConfig ?? MediaWikiServices::getInstance()->getMainConfig();
330 $this->linkRendererFactory =
331 $linkRendererFactory ?? MediaWikiServices::getInstance()->getLinkRendererFactory();
337 public function __destruct() {
338 if ( isset( $this->mLinkHolders ) ) {
339 unset( $this->mLinkHolders );
342 unset( $this->$name );
349 public function __clone() {
350 $this->mInParse =
false;
358 foreach ( [
'mStripState',
'mVarCache' ]
as $k ) {
372 public function firstCallInit() {
373 if ( !$this->mFirstCall ) {
376 $this->mFirstCall =
false;
380 $this->initialiseVariables();
392 public function clearState() {
393 $this->firstCallInit();
395 $this->mOptions->registerWatcher( [ $this->mOutput,
'recordOption' ] );
396 $this->mAutonumber = 0;
397 $this->mIncludeCount = [];
400 $this->mRevisionObject = $this->mRevisionTimestamp =
401 $this->mRevisionId = $this->mRevisionUser = $this->mRevisionSize =
null;
402 $this->mVarCache = [];
404 $this->mLangLinkLanguages = [];
405 $this->currentRevisionCache =
null;
409 # Clear these on every parse, T6549
410 $this->mTplRedirCache = $this->mTplDomCache = [];
412 $this->mShowToc =
true;
413 $this->mForceTocPosition =
false;
414 $this->mIncludeSizes = [
418 $this->mPPNodeCount = 0;
419 $this->mGeneratedPPNodeCount = 0;
420 $this->mHighestExpansionDepth = 0;
421 $this->mDefaultSort =
false;
422 $this->mHeadings = [];
423 $this->mDoubleUnderscores = [];
424 $this->mExpensiveFunctionCount = 0;
427 if ( isset( $this->mPreprocessor ) && $this->mPreprocessor->parser !== $this ) {
428 $this->mPreprocessor =
null;
452 public function parse(
454 $linestart =
true, $clearState =
true, $revid =
null
459 $text = strtr( $text,
"\x7f",
"?" );
460 $magicScopeVariable = $this->lock();
463 $text = str_replace(
"\000",
'', $text );
467 $this->currentRevisionCache =
null;
468 $this->mInputSize = strlen( $text );
469 if ( $this->mOptions->getEnableLimitReport() ) {
470 $this->mOutput->resetParseStartTime();
473 $oldRevisionId = $this->mRevisionId;
474 $oldRevisionObject = $this->mRevisionObject;
475 $oldRevisionTimestamp = $this->mRevisionTimestamp;
476 $oldRevisionUser = $this->mRevisionUser;
477 $oldRevisionSize = $this->mRevisionSize;
478 if ( $revid !==
null ) {
479 $this->mRevisionId = $revid;
480 $this->mRevisionObject =
null;
481 $this->mRevisionTimestamp =
null;
482 $this->mRevisionUser =
null;
483 $this->mRevisionSize =
null;
491 $text = $this->internalParse( $text );
494 $text = $this->internalParseHalfParsed( $text,
true, $linestart );
503 if ( !(
$options->getDisableTitleConversion()
504 || isset( $this->mDoubleUnderscores[
'nocontentconvert'] )
505 || isset( $this->mDoubleUnderscores[
'notitleconvert'] )
506 || $this->mOutput->getDisplayTitle() !==
false )
508 $convruletitle = $this->getTargetLanguage()->getConvRuleTitle();
509 if ( $convruletitle ) {
510 $this->mOutput->setTitleText( $convruletitle );
512 $titleText = $this->getTargetLanguage()->convertTitle(
$title );
513 $this->mOutput->setTitleText( $titleText );
517 # Compute runtime adaptive expiry if set
518 $this->mOutput->finalizeAdaptiveCacheExpiry();
520 # Warn if too many heavyweight parser functions were used
521 if ( $this->mExpensiveFunctionCount > $this->mOptions->getExpensiveParserFunctionLimit() ) {
522 $this->limitationWarn(
'expensive-parserfunction',
523 $this->mExpensiveFunctionCount,
524 $this->mOptions->getExpensiveParserFunctionLimit()
528 # Information on limits, for the benefit of users who try to skirt them
529 if ( $this->mOptions->getEnableLimitReport() ) {
530 $text .= $this->makeLimitReport();
533 # Wrap non-interface parser output in a <div> so it can be targeted
535 $class = $this->mOptions->getWrapOutputClass();
536 if ( $class !==
false && !$this->mOptions->getInterfaceMessage() ) {
537 $this->mOutput->addWrapperDivClass( $class );
540 $this->mOutput->setText( $text );
542 $this->mRevisionId = $oldRevisionId;
543 $this->mRevisionObject = $oldRevisionObject;
544 $this->mRevisionTimestamp = $oldRevisionTimestamp;
545 $this->mRevisionUser = $oldRevisionUser;
546 $this->mRevisionSize = $oldRevisionSize;
547 $this->mInputSize =
false;
548 $this->currentRevisionCache =
null;
550 return $this->mOutput;
559 protected function makeLimitReport() {
560 $maxIncludeSize = $this->mOptions->getMaxIncludeSize();
562 $cpuTime = $this->mOutput->getTimeSinceStart(
'cpu' );
563 if ( $cpuTime !==
null ) {
564 $this->mOutput->setLimitReportData(
'limitreport-cputime',
565 sprintf(
"%.3f", $cpuTime )
569 $wallTime = $this->mOutput->getTimeSinceStart(
'wall' );
570 $this->mOutput->setLimitReportData(
'limitreport-walltime',
571 sprintf(
"%.3f", $wallTime )
574 $this->mOutput->setLimitReportData(
'limitreport-ppvisitednodes',
575 [ $this->mPPNodeCount, $this->mOptions->getMaxPPNodeCount() ]
577 $this->mOutput->setLimitReportData(
'limitreport-ppgeneratednodes',
578 [ $this->mGeneratedPPNodeCount, $this->mOptions->getMaxGeneratedPPNodeCount() ]
580 $this->mOutput->setLimitReportData(
'limitreport-postexpandincludesize',
581 [ $this->mIncludeSizes[
'post-expand'], $maxIncludeSize ]
583 $this->mOutput->setLimitReportData(
'limitreport-templateargumentsize',
584 [ $this->mIncludeSizes[
'arg'], $maxIncludeSize ]
586 $this->mOutput->setLimitReportData(
'limitreport-expansiondepth',
587 [ $this->mHighestExpansionDepth, $this->mOptions->getMaxPPExpandDepth() ]
589 $this->mOutput->setLimitReportData(
'limitreport-expensivefunctioncount',
590 [ $this->mExpensiveFunctionCount, $this->mOptions->getExpensiveParserFunctionLimit() ]
593 foreach ( $this->mStripState->getLimitReport()
as list( $key,
$value ) ) {
594 $this->mOutput->setLimitReportData( $key,
$value );
597 Hooks::run(
'ParserLimitReportPrepare', [ $this, $this->mOutput ] );
599 $limitReport =
"NewPP limit report\n";
600 if ( $this->siteConfig->get(
'ShowHostnames' ) ) {
601 $limitReport .=
'Parsed by ' .
wfHostname() .
"\n";
603 $limitReport .=
'Cached time: ' . $this->mOutput->getCacheTime() .
"\n";
604 $limitReport .=
'Cache expiry: ' . $this->mOutput->getCacheExpiry() .
"\n";
605 $limitReport .=
'Dynamic content: ' .
606 ( $this->mOutput->hasDynamicContent() ?
'true' :
'false' ) .
609 foreach ( $this->mOutput->getLimitReportData()
as $key =>
$value ) {
611 [ $key, &
$value, &$limitReport,
false,
false ]
613 $keyMsg =
wfMessage( $key )->inLanguage(
'en' )->useDatabase(
false );
614 $valueMsg =
wfMessage( [
"$key-value-text",
"$key-value" ] )
615 ->inLanguage(
'en' )->useDatabase(
false );
616 if ( !$valueMsg->exists() ) {
619 if ( !$keyMsg->isDisabled() && !$valueMsg->isDisabled() ) {
620 $valueMsg->params(
$value );
621 $limitReport .=
"{$keyMsg->text()}: {$valueMsg->text()}\n";
627 $limitReport = htmlspecialchars_decode( $limitReport );
631 $limitReport = str_replace( [
'-',
'&' ], [
'‐',
'&' ], $limitReport );
632 $text =
"\n<!-- \n$limitReport-->\n";
635 $dataByFunc = $this->mProfiler->getFunctionStats();
636 uasort( $dataByFunc,
function ( $a, $b ) {
637 return $b[
'real'] <=> $a[
'real'];
640 foreach ( array_slice( $dataByFunc, 0, 10 )
as $item ) {
641 $profileReport[] = sprintf(
"%6.2f%% %8.3f %6d %s",
642 $item[
'%real'], $item[
'real'], $item[
'calls'],
643 htmlspecialchars( $item[
'name'] ) );
645 $text .=
"<!--\nTransclusion expansion time report (%,ms,calls,template)\n";
646 $text .= implode(
"\n", $profileReport ) .
"\n-->\n";
648 $this->mOutput->setLimitReportData(
'limitreport-timingprofile', $profileReport );
651 if ( $this->siteConfig->get(
'ShowHostnames' ) ) {
652 $this->mOutput->setLimitReportData(
'cachereport-origin',
wfHostname() );
654 $this->mOutput->setLimitReportData(
'cachereport-timestamp',
655 $this->mOutput->getCacheTime() );
656 $this->mOutput->setLimitReportData(
'cachereport-ttl',
657 $this->mOutput->getCacheExpiry() );
658 $this->mOutput->setLimitReportData(
'cachereport-transientcontent',
659 $this->mOutput->hasDynamicContent() );
661 if ( $this->mGeneratedPPNodeCount > $this->mOptions->getMaxGeneratedPPNodeCount() / 10 ) {
662 wfDebugLog(
'generated-pp-node-count', $this->mGeneratedPPNodeCount .
' ' .
663 $this->mTitle->getPrefixedDBkey() );
692 public function recursiveTagParse( $text, $frame =
false ) {
697 $text = $this->internalParse( $text,
false, $frame );
720 public function recursiveTagParseFully( $text, $frame =
false ) {
721 $text = $this->recursiveTagParse( $text, $frame );
722 $text = $this->internalParseHalfParsed( $text,
false );
737 public function preprocess( $text,
Title $title =
null,
740 $magicScopeVariable = $this->lock();
742 if ( $revid !==
null ) {
743 $this->mRevisionId = $revid;
749 $text = $this->replaceVariables( $text, $frame );
750 $text = $this->mStripState->unstripBoth( $text );
763 public function recursivePreprocess( $text, $frame =
false ) {
764 $text = $this->replaceVariables( $text, $frame );
765 $text = $this->mStripState->unstripBoth( $text );
784 $text = $msg->params(
$params )->plain();
786 # Parser (re)initialisation
787 $magicScopeVariable = $this->lock();
791 $dom = $this->preprocessToDom( $text, self::PTD_FOR_INCLUSION );
792 $text = $this->getPreprocessor()->newFrame()->expand( $dom, $flags );
793 $text = $this->mStripState->unstripBoth( $text );
803 public function setUser(
$user ) {
804 $this->mUser =
$user;
812 public function setTitle(
$t ) {
817 if (
$t->hasFragment() ) {
818 # Strip the fragment to avoid various odd effects
819 $this->mTitle =
$t->createFragmentTarget(
'' );
830 public function getTitle() {
831 return $this->mTitle;
840 public function Title( $x =
null ) {
841 return wfSetVar( $this->mTitle, $x );
849 public function setOutputType( $ot ) {
850 $this->mOutputType = $ot;
866 public function OutputType( $x =
null ) {
867 return wfSetVar( $this->mOutputType, $x );
875 public function getOutput() {
876 return $this->mOutput;
884 public function getOptions() {
885 return $this->mOptions;
894 public function Options( $x =
null ) {
895 return wfSetVar( $this->mOptions, $x );
901 public function nextLinkID() {
902 return $this->mLinkID++;
908 public function setLinkID( $id ) {
909 $this->mLinkID = $id;
916 public function getFunctionLang() {
917 return $this->getTargetLanguage();
929 public function getTargetLanguage() {
930 $target = $this->mOptions->getTargetLanguage();
932 if ( $target !==
null ) {
934 } elseif ( $this->mOptions->getInterfaceMessage() ) {
935 return $this->mOptions->getUserLangObj();
936 } elseif ( is_null( $this->mTitle ) ) {
937 throw new MWException( __METHOD__ .
': $this->mTitle is null' );
940 return $this->mTitle->getPageLanguage();
948 public function getConverterLanguage() {
949 return $this->getTargetLanguage();
958 public function getUser() {
959 if ( !is_null( $this->mUser ) ) {
962 return $this->mOptions->getUser();
970 public function getPreprocessor() {
971 if ( !isset( $this->mPreprocessor ) ) {
972 $class = $this->mPreprocessorClass;
973 $this->mPreprocessor =
new $class( $this );
975 return $this->mPreprocessor;
984 public function getLinkRenderer() {
986 if ( !$this->mLinkRenderer ) {
987 $this->mLinkRenderer = $this->linkRendererFactory->create();
988 $this->mLinkRenderer->setStubThreshold(
989 $this->getOptions()->getStubThreshold()
993 return $this->mLinkRenderer;
1002 public function getMagicWordFactory() {
1003 return $this->magicWordFactory;
1012 public function getContentLanguage() {
1013 return $this->contLang;
1035 public static function extractTagsAndParams( $elements, $text, &
$matches ) {
1040 $taglist = implode(
'|', $elements );
1041 $start =
"/<($taglist)(\\s+[^>]*?|\\s*?)(\/?>)|<(!--)/i";
1043 while ( $text !=
'' ) {
1044 $p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE );
1046 if (
count( $p ) < 5 ) {
1049 if (
count( $p ) > 5 ) {
1057 list( , $element, $attributes, $close, $inside ) = $p;
1060 $marker = self::MARKER_PREFIX .
"-$element-" . sprintf(
'%08X', $n++ ) . self::MARKER_SUFFIX;
1061 $stripped .= $marker;
1063 if ( $close ===
'/>' ) {
1064 # Empty element tag, <tag />
1069 if ( $element ===
'!--' ) {
1072 $end =
"/(<\\/$element\\s*>)/i";
1074 $q = preg_split( $end, $inside, 2, PREG_SPLIT_DELIM_CAPTURE );
1076 if (
count( $q ) < 3 ) {
1077 # No end tag -- let it run out to the end of the text.
1081 list( , $tail, $text ) = $q;
1087 Sanitizer::decodeTagAttributes( $attributes ),
1088 "<$element$attributes$close$content$tail" ];
1098 public function getStripList() {
1099 return $this->mStripList;
1111 public function insertStripItem( $text ) {
1112 $marker = self::MARKER_PREFIX .
"-item-{$this->mMarkerIndex}-" . self::MARKER_SUFFIX;
1113 $this->mMarkerIndex++;
1114 $this->mStripState->addGeneral( $marker, $text );
1125 public function doTableStuff( $text ) {
1128 $td_history = []; # Is currently
a td
tag open?
1129 $last_tag_history = []; # Save
history of last lag activated (td, th
or caption)
1130 $tr_history = []; # Is currently
a tr
tag open?
1131 $tr_attributes = []; #
history of tr attributes
1132 $has_opened_tr = []; # Did
this table open a <tr> element?
1133 $indent_level = 0; # indent level
of the
table
1136 $line = trim( $outLine );
1138 if (
$line ===
'' ) { # empty line, go to next line
1139 $out .= $outLine .
"\n";
1143 $first_character =
$line[0];
1144 $first_two = substr(
$line, 0, 2 );
1147 if ( preg_match(
'/^(:*)\s*\{\|(.*)$/',
$line,
$matches ) ) {
1148 # First check if we are starting a new table
1149 $indent_level = strlen(
$matches[1] );
1151 $attributes = $this->mStripState->unstripBoth(
$matches[2] );
1152 $attributes = Sanitizer::fixTagAttributes( $attributes,
'table' );
1154 $outLine = str_repeat(
'<dl><dd>', $indent_level ) .
"<table{$attributes}>";
1155 array_push( $td_history,
false );
1156 array_push( $last_tag_history,
'' );
1157 array_push( $tr_history,
false );
1158 array_push( $tr_attributes,
'' );
1159 array_push( $has_opened_tr,
false );
1160 } elseif (
count( $td_history ) == 0 ) {
1161 # Don't do any of the following
1162 $out .= $outLine .
"\n";
1164 } elseif ( $first_two ===
'|}' ) {
1165 # We are ending a table
1167 $last_tag = array_pop( $last_tag_history );
1169 if ( !array_pop( $has_opened_tr ) ) {
1170 $line =
"<tr><td></td></tr>{$line}";
1173 if ( array_pop( $tr_history ) ) {
1174 $line =
"</tr>{$line}";
1177 if ( array_pop( $td_history ) ) {
1178 $line =
"</{$last_tag}>{$line}";
1180 array_pop( $tr_attributes );
1181 if ( $indent_level > 0 ) {
1182 $outLine = rtrim(
$line ) . str_repeat(
'</dd></dl>', $indent_level );
1186 } elseif ( $first_two ===
'|-' ) {
1187 # Now we have a table row
1188 $line = preg_replace(
'#^\|-+#',
'',
$line );
1190 # Whats after the tag is now only attributes
1191 $attributes = $this->mStripState->unstripBoth(
$line );
1192 $attributes = Sanitizer::fixTagAttributes( $attributes,
'tr' );
1193 array_pop( $tr_attributes );
1194 array_push( $tr_attributes, $attributes );
1197 $last_tag = array_pop( $last_tag_history );
1198 array_pop( $has_opened_tr );
1199 array_push( $has_opened_tr,
true );
1201 if ( array_pop( $tr_history ) ) {
1205 if ( array_pop( $td_history ) ) {
1206 $line =
"</{$last_tag}>{$line}";
1210 array_push( $tr_history,
false );
1211 array_push( $td_history,
false );
1212 array_push( $last_tag_history,
'' );
1213 } elseif ( $first_character ===
'|'
1214 || $first_character ===
'!'
1215 || $first_two ===
'|+'
1217 # This might be cell elements, td, th or captions
1218 if ( $first_two ===
'|+' ) {
1219 $first_character =
'+';
1226 if ( $first_character ===
'!' ) {
1230 # Split up multiple cells on the same line.
1231 # FIXME : This can result in improper nesting of tags processed
1232 # by earlier parser steps.
1233 $cells = explode(
'||',
$line );
1237 # Loop through each table cell
1238 foreach ( $cells
as $cell ) {
1240 if ( $first_character !==
'+' ) {
1241 $tr_after = array_pop( $tr_attributes );
1242 if ( !array_pop( $tr_history ) ) {
1243 $previous =
"<tr{$tr_after}>\n";
1245 array_push( $tr_history,
true );
1246 array_push( $tr_attributes,
'' );
1247 array_pop( $has_opened_tr );
1248 array_push( $has_opened_tr,
true );
1251 $last_tag = array_pop( $last_tag_history );
1253 if ( array_pop( $td_history ) ) {
1254 $previous =
"</{$last_tag}>\n{$previous}";
1257 if ( $first_character ===
'|' ) {
1259 } elseif ( $first_character ===
'!' ) {
1261 } elseif ( $first_character ===
'+' ) {
1262 $last_tag =
'caption';
1267 array_push( $last_tag_history, $last_tag );
1269 # A cell could contain both parameters and data
1270 $cell_data = explode(
'|', $cell, 2 );
1272 # T2553: Note that a '|' inside an invalid link should not
1273 # be mistaken as delimiting cell parameters
1274 # Bug T153140: Neither should language converter markup.
1275 if ( preg_match(
'/\[\[|-\{/', $cell_data[0] ) === 1 ) {
1276 $cell =
"{$previous}<{$last_tag}>" . trim( $cell );
1277 } elseif (
count( $cell_data ) == 1 ) {
1279 $cell =
"{$previous}<{$last_tag}>" . trim( $cell_data[0] );
1281 $attributes = $this->mStripState->unstripBoth( $cell_data[0] );
1282 $attributes = Sanitizer::fixTagAttributes( $attributes, $last_tag );
1284 $cell =
"{$previous}<{$last_tag}{$attributes}>" . trim( $cell_data[1] );
1288 array_push( $td_history,
true );
1291 $out .= $outLine .
"\n";
1294 # Closing open td, tr && table
1295 while (
count( $td_history ) > 0 ) {
1296 if ( array_pop( $td_history ) ) {
1299 if ( array_pop( $tr_history ) ) {
1302 if ( !array_pop( $has_opened_tr ) ) {
1303 $out .=
"<tr><td></td></tr>\n";
1306 $out .=
"</table>\n";
1309 # Remove trailing line-ending (b/c)
1310 if ( substr(
$out, -1 ) ===
"\n" ) {
1314 # special case: don't return empty table
1315 if (
$out ===
"<table>\n<tr><td></td></tr>\n</table>" ) {
1335 public function internalParse( $text, $isMain =
true, $frame =
false ) {
1341 # Hook to suspend the parser in this state
1342 if ( !
Hooks::run(
'ParserBeforeInternalParse', [ &
$parser, &$text, &$this->mStripState ] ) ) {
1346 # if $frame is provided, then use $frame for replacing any variables
1348 # use frame depth to infer how include/noinclude tags should be handled
1349 # depth=0 means this is the top-level document; otherwise it's an included document
1350 if ( !$frame->depth ) {
1353 $flag = self::PTD_FOR_INCLUSION;
1355 $dom = $this->preprocessToDom( $text, $flag );
1356 $text = $frame->expand( $dom );
1358 # if $frame is not provided, then use old-style replaceVariables
1359 $text = $this->replaceVariables( $text );
1362 Hooks::run(
'InternalParseBeforeSanitize', [ &
$parser, &$text, &$this->mStripState ] );
1363 $text = Sanitizer::removeHTMLtags(
1365 [ $this,
'attributeStripCallback' ],
1367 array_keys( $this->mTransparentTagHooks ),
1369 [ $this,
'addTrackingCategory' ]
1371 Hooks::run(
'InternalParseBeforeLinks', [ &
$parser, &$text, &$this->mStripState ] );
1373 # Tables need to come after variable replacement for things to work
1374 # properly; putting them before other transformations should keep
1375 # exciting things like link expansions from showing up in surprising
1377 $text = $this->doTableStuff( $text );
1379 $text = preg_replace(
'/(^|\n)-----*/',
'\\1<hr />', $text );
1381 $text = $this->doDoubleUnderscore( $text );
1383 $text = $this->doHeadings( $text );
1384 $text = $this->replaceInternalLinks( $text );
1385 $text = $this->doAllQuotes( $text );
1386 $text = $this->replaceExternalLinks( $text );
1388 # replaceInternalLinks may sometimes leave behind
1389 # absolute URLs, which have to be masked to hide them from replaceExternalLinks
1390 $text = str_replace( self::MARKER_PREFIX .
'NOPARSE',
'', $text );
1392 $text = $this->doMagicLinks( $text );
1393 $text = $this->formatHeadings( $text, $origText, $isMain );
1407 private function internalParseHalfParsed( $text, $isMain =
true, $linestart =
true ) {
1408 $text = $this->mStripState->unstripGeneral( $text );
1417 # Clean up special characters, only run once, next-to-last before doBlockLevels
1418 $text = Sanitizer::armorFrenchSpaces( $text );
1420 $text = $this->doBlockLevels( $text, $linestart );
1422 $this->replaceLinkHolders( $text );
1431 if ( !( $this->mOptions->getDisableContentConversion()
1432 || isset( $this->mDoubleUnderscores[
'nocontentconvert'] ) )
1433 && !$this->mOptions->getInterfaceMessage()
1435 # The position of the convert() call should not be changed. it
1436 # assumes that the links are all replaced and the only thing left
1437 # is the <nowiki> mark.
1438 $text = $this->getTargetLanguage()->convert( $text );
1441 $text = $this->mStripState->unstripNoWiki( $text );
1447 $text = $this->replaceTransparentTags( $text );
1448 $text = $this->mStripState->unstripGeneral( $text );
1450 $text = Sanitizer::normalizeCharReferences( $text );
1453 if ( $this->mOptions->getTidy() ) {
1457 # attempt to sanitize at least some nesting problems
1458 # (T4702 and quite a few others)
1459 # This code path is buggy and deprecated!
1462 # ''Something [http://www.cool.com cool''] -->
1463 # <i>Something</i><a href="http://www.cool.com"..><i>cool></i></a>
1464 '/(<([bi])>)(<([bi])>)?([^<]*)(<\/?a[^<]*>)([^<]*)(<\/\\4>)?(<\/\\2>)/' =>
1465 '\\1\\3\\5\\8\\9\\6\\1\\3\\7\\8\\9',
1466 # fix up an anchor inside another anchor, only
1467 # at least for a single single nested link (T5695)
1468 '/(<a[^>]+>)([^<]*)(<a[^>]+>[^<]*)<\/a>(.*)<\/a>/' =>
1469 '\\1\\2</a>\\3</a>\\1\\4</a>',
1470 # fix div inside inline elements- doBlockLevels won't wrap a line which
1471 # contains a div, so fix it up here; replace
1472 # div with escaped text
1473 '/(<([aib]) [^>]+>)([^<]*)(<div([^>]*)>)(.*)(<\/div>)([^<]*)(<\/\\2>)/' =>
1474 '\\1\\3<div\\5>\\6</div>\\8\\9',
1475 # remove empty italic or bold tag pairs, some
1476 # introduced by rules above
1477 '/<([bi])><\/\\1>/' =>
'',
1480 $text = preg_replace(
1481 array_keys( $tidyregs ),
1482 array_values( $tidyregs ),
1504 public function doMagicLinks( $text ) {
1506 $urlChar = self::EXT_LINK_URL_CLASS;
1507 $addr = self::EXT_LINK_ADDR;
1508 $space = self::SPACE_NOT_NL; # non-newline space
1509 $spdash =
"(?:-|$space)"; #
a dash
or a non-newline space
1510 $spaces =
"$space++"; # possessive match
of 1
or more spaces
1511 $text = preg_replace_callback(
1513 (<a[ \t\r\n>].*?</a>) | # m[1]: Skip link text
1514 (<.*?>) | # m[2]: Skip stuff inside HTML elements' .
"
1515 (\b # m[3]: Free external links
1517 ($addr$urlChar*) # m[4]: Post-protocol path
1519 \b(?:RFC|PMID) $spaces # m[5]: RFC or PMID, capture number
1521 \bISBN $spaces ( # m[6]: ISBN, capture number
1522 (?: 97[89] $spdash? )? # optional 13-digit ISBN prefix
1523 (?: [0-9] $spdash? ){9} # 9 digits with opt. delimiters
1524 [0-9Xx] # check digit
1526 )!xu", [ $this,
'magicLinkCallback' ], $text );
1535 public function magicLinkCallback( $m ) {
1536 if ( isset( $m[1] ) && $m[1] !==
'' ) {
1539 } elseif ( isset( $m[2] ) && $m[2] !==
'' ) {
1542 } elseif ( isset( $m[3] ) && $m[3] !==
'' ) {
1543 # Free external link
1544 return $this->makeFreeExternalLink( $m[0], strlen( $m[4] ) );
1545 } elseif ( isset( $m[5] ) && $m[5] !==
'' ) {
1547 if ( substr( $m[0], 0, 3 ) ===
'RFC' ) {
1548 if ( !$this->mOptions->getMagicRFCLinks() ) {
1553 $cssClass =
'mw-magiclink-rfc';
1554 $trackingCat =
'magiclink-tracking-rfc';
1556 } elseif ( substr( $m[0], 0, 4 ) ===
'PMID' ) {
1557 if ( !$this->mOptions->getMagicPMIDLinks() ) {
1561 $urlmsg =
'pubmedurl';
1562 $cssClass =
'mw-magiclink-pmid';
1563 $trackingCat =
'magiclink-tracking-pmid';
1566 throw new MWException( __METHOD__ .
': unrecognised match type "' .
1567 substr( $m[0], 0, 20 ) .
'"' );
1569 $url =
wfMessage( $urlmsg, $id )->inContentLanguage()->text();
1570 $this->addTrackingCategory( $trackingCat );
1572 } elseif ( isset( $m[6] ) && $m[6] !==
''
1573 && $this->mOptions->getMagicISBNLinks()
1577 $space = self::SPACE_NOT_NL; # non-newline space
1578 $isbn = preg_replace(
"/$space/",
' ', $isbn );
1579 $num = strtr( $isbn, [
1584 $this->addTrackingCategory(
'magiclink-tracking-isbn' );
1585 return $this->getLinkRenderer()->makeKnownLink(
1589 'class' =>
'internal mw-magiclink-isbn',
1607 public function makeFreeExternalLink( $url, $numPostProto ) {
1610 # The characters '<' and '>' (which were escaped by
1611 # removeHTMLtags()) should not be included in
1612 # URLs, per RFC 2396.
1613 # Make terminate a URL as well (bug T84937)
1616 '/&(lt|gt|nbsp|#x0*(3[CcEe]|[Aa]0)|#0*(60|62|160));/',
1621 $trail = substr( $url, $m2[0][1] ) . $trail;
1622 $url = substr( $url, 0, $m2[0][1] );
1625 # Move trailing punctuation to $trail
1627 # If there is no left bracket, then consider right brackets fair game too
1628 if ( strpos( $url,
'(' ) ===
false ) {
1632 $urlRev = strrev( $url );
1633 $numSepChars = strspn( $urlRev, $sep );
1634 # Don't break a trailing HTML entity by moving the ; into $trail
1635 # This is in hot code, so use substr_compare to avoid having to
1636 # create a new string object for the comparison
1637 if ( $numSepChars && substr_compare( $url,
";", -$numSepChars, 1 ) === 0 ) {
1638 # more optimization: instead of running preg_match with a $
1639 # anchor, which can be slow, do the match on the reversed
1640 # string starting at the desired offset.
1641 # un-reversed regexp is: /&([a-z]+|#x[\da-f]+|#\d+)$/i
1642 if ( preg_match(
'/\G([a-z]+|[\da-f]+x#|\d+#)&/i', $urlRev, $m2, 0, $numSepChars ) ) {
1646 if ( $numSepChars ) {
1647 $trail = substr( $url, -$numSepChars ) . $trail;
1648 $url = substr( $url, 0, -$numSepChars );
1651 # Verify that we still have a real URL after trail removal, and
1652 # not just lone protocol
1653 if ( strlen( $trail ) >= $numPostProto ) {
1654 return $url . $trail;
1657 $url = Sanitizer::cleanUrl( $url );
1659 # Is this an external image?
1660 $text = $this->maybeMakeExternalImage( $url );
1661 if ( $text ===
false ) {
1662 # Not an image, make a link
1664 $this->getTargetLanguage()->getConverter()->markNoConversion( $url ),
1666 $this->getExternalLinkAttribs( $url ), $this->mTitle );
1667 # Register it in the output object...
1668 $this->mOutput->addExternalLink( $url );
1670 return $text . $trail;
1682 public function doHeadings( $text ) {
1683 for ( $i = 6; $i >= 1; --$i ) {
1684 $h = str_repeat(
'=', $i );
1687 $text = preg_replace(
"/^(?:$h)[ \\t]*(.+?)[ \\t]*(?:$h)\\s*$/m",
"<h$i>\\1</h$i>", $text );
1700 public function doAllQuotes( $text ) {
1704 $outtext .= $this->doQuotes(
$line ) .
"\n";
1706 $outtext = substr( $outtext, 0, -1 );
1717 public function doQuotes( $text ) {
1718 $arr = preg_split(
"/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE );
1719 $countarr =
count( $arr );
1720 if ( $countarr == 1 ) {
1729 for ( $i = 1; $i < $countarr; $i += 2 ) {
1730 $thislen = strlen( $arr[$i] );
1734 if ( $thislen == 4 ) {
1735 $arr[$i - 1] .=
"'";
1738 } elseif ( $thislen > 5 ) {
1742 $arr[$i - 1] .= str_repeat(
"'", $thislen - 5 );
1747 if ( $thislen == 2 ) {
1749 } elseif ( $thislen == 3 ) {
1751 } elseif ( $thislen == 5 ) {
1761 if ( ( $numbold % 2 == 1 ) && ( $numitalics % 2 == 1 ) ) {
1762 $firstsingleletterword = -1;
1763 $firstmultiletterword = -1;
1765 for ( $i = 1; $i < $countarr; $i += 2 ) {
1766 if ( strlen( $arr[$i] ) == 3 ) {
1767 $x1 = substr( $arr[$i - 1], -1 );
1768 $x2 = substr( $arr[$i - 1], -2, 1 );
1769 if ( $x1 ===
' ' ) {
1770 if ( $firstspace == -1 ) {
1773 } elseif ( $x2 ===
' ' ) {
1774 $firstsingleletterword = $i;
1778 } elseif ( $firstmultiletterword == -1 ) {
1779 $firstmultiletterword = $i;
1785 if ( $firstsingleletterword > -1 ) {
1786 $arr[$firstsingleletterword] =
"''";
1787 $arr[$firstsingleletterword - 1] .=
"'";
1788 } elseif ( $firstmultiletterword > -1 ) {
1790 $arr[$firstmultiletterword] =
"''";
1791 $arr[$firstmultiletterword - 1] .=
"'";
1792 } elseif ( $firstspace > -1 ) {
1796 $arr[$firstspace] =
"''";
1797 $arr[$firstspace - 1] .=
"'";
1806 foreach ( $arr
as $r ) {
1807 if ( ( $i % 2 ) == 0 ) {
1808 if ( $state ===
'both' ) {
1814 $thislen = strlen( $r );
1815 if ( $thislen == 2 ) {
1816 if ( $state ===
'i' ) {
1819 } elseif ( $state ===
'bi' ) {
1822 } elseif ( $state ===
'ib' ) {
1825 } elseif ( $state ===
'both' ) {
1832 } elseif ( $thislen == 3 ) {
1833 if ( $state ===
'b' ) {
1836 } elseif ( $state ===
'bi' ) {
1839 } elseif ( $state ===
'ib' ) {
1842 } elseif ( $state ===
'both' ) {
1849 } elseif ( $thislen == 5 ) {
1850 if ( $state ===
'b' ) {
1853 } elseif ( $state ===
'i' ) {
1856 } elseif ( $state ===
'bi' ) {
1859 } elseif ( $state ===
'ib' ) {
1862 } elseif ( $state ===
'both' ) {
1874 if ( $state ===
'b' || $state ===
'ib' ) {
1877 if ( $state ===
'i' || $state ===
'bi' || $state ===
'ib' ) {
1880 if ( $state ===
'bi' ) {
1884 if ( $state ===
'both' &&
$buffer ) {
1903 public function replaceExternalLinks( $text ) {
1904 $bits = preg_split( $this->mExtLinkBracketedRegex, $text, -1, PREG_SPLIT_DELIM_CAPTURE );
1905 if ( $bits ===
false ) {
1906 throw new MWException(
"PCRE needs to be compiled with "
1907 .
"--enable-unicode-properties in order for MediaWiki to function" );
1909 $s = array_shift( $bits );
1912 while ( $i <
count( $bits ) ) {
1915 $text = $bits[$i++];
1916 $trail = $bits[$i++];
1918 # The characters '<' and '>' (which were escaped by
1919 # removeHTMLtags()) should not be included in
1920 # URLs, per RFC 2396.
1922 if ( preg_match(
'/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE ) ) {
1923 $text = substr( $url, $m2[0][1] ) .
' ' . $text;
1924 $url = substr( $url, 0, $m2[0][1] );
1927 # If the link text is an image URL, replace it with an <img> tag
1928 # This happened by accident in the original parser, but some people used it extensively
1929 $img = $this->maybeMakeExternalImage( $text );
1930 if ( $img !==
false ) {
1936 # Set linktype for CSS
1939 # No link text, e.g. [http://domain.tld/some.link]
1940 if ( $text ==
'' ) {
1942 $langObj = $this->getTargetLanguage();
1943 $text =
'[' . $langObj->formatNum( ++$this->mAutonumber ) .
']';
1944 $linktype =
'autonumber';
1946 # Have link text, e.g. [http://domain.tld/some.link text]s
1953 $text = $this->getTargetLanguage()->getConverter()->markNoConversion( $text );
1956 $url = Sanitizer::cleanUrl( $url );
1958 # Use the encoded URL
1959 # This means that users can paste URLs directly into the text
1960 # Funny characters like ö aren't valid in URLs anyway
1961 # This was changed in August 2004
1963 $this->getExternalLinkAttribs( $url ), $this->mTitle ) . $dtrail . $trail;
1965 # Register link in the output object.
1966 $this->mOutput->addExternalLink( $url );
1981 public static function getExternalLinkRel( $url =
false,
$title =
null ) {
2002 public function getExternalLinkAttribs( $url ) {
2004 $rel = self::getExternalLinkRel( $url, $this->mTitle );
2006 $target = $this->mOptions->getExternalLinkTarget();
2009 if ( !in_array( $target, [
'_self',
'_parent',
'_top' ] ) ) {
2013 if ( $rel !==
'' ) {
2016 $rel .=
'noreferrer noopener';
2032 public static function normalizeLinkUrl( $url ) {
2033 # Test for RFC 3986 IPv6 syntax
2034 $scheme =
'[a-z][a-z0-9+.-]*:';
2035 $userinfo =
'(?:[a-z0-9\-._~!$&\'()*+,;=:]|%[0-9a-f]{2})*';
2036 $ipv6Host =
'\\[((?:[0-9a-f:]|%3[0-A]|%[46][1-6])+)\\]';
2037 if ( preg_match(
"<^(?:{$scheme})?//(?:{$userinfo}@)?{$ipv6Host}(?:[:/?#].*|)$>i", $url, $m ) &&
2040 $isIPv6 = rawurldecode( $m[1] );
2045 # Make sure unsafe characters are encoded
2046 $url = preg_replace_callback(
'/[\x00-\x20"<>\[\\\\\]^`{|}\x7F-\xFF]/',
2048 return rawurlencode( $m[0] );
2054 $end = strlen( $url );
2056 # Fragment part - 'fragment'
2057 $start = strpos( $url,
'#' );
2058 if ( $start !==
false && $start < $end ) {
2059 $ret = self::normalizeUrlComponent(
2060 substr( $url, $start, $end - $start ),
'"#%<>[\]^`{|}' ) .
$ret;
2064 # Query part - 'query' minus &=+;
2065 $start = strpos( $url,
'?' );
2066 if ( $start !==
false && $start < $end ) {
2067 $ret = self::normalizeUrlComponent(
2068 substr( $url, $start, $end - $start ),
'"#%<>[\]^`{|}&=+;' ) .
$ret;
2072 # Scheme and path part - 'pchar'
2073 # (we assume no userinfo or encoded colons in the host)
2074 $ret = self::normalizeUrlComponent(
2075 substr( $url, 0, $end ),
'"#%<>[\]^`{|}/?' ) .
$ret;
2078 if ( $isIPv6 !==
false ) {
2079 $ipv6Host =
"%5B({$isIPv6})%5D";
2080 $ret = preg_replace(
2081 "<^((?:{$scheme})?//(?:{$userinfo}@)?){$ipv6Host}(?=[:/?#]|$)>i",
2090 private static function normalizeUrlComponent( $component, $unsafe ) {
2091 $callback =
function (
$matches )
use ( $unsafe ) {
2093 $ord = ord( $char );
2094 if ( $ord > 32 && $ord < 127 && strpos( $unsafe, $char ) ===
false ) {
2098 # Leave it escaped, but use uppercase for a-f
2102 return preg_replace_callback(
'/%[0-9A-Fa-f]{2}/', $callback, $component );
2113 private function maybeMakeExternalImage( $url ) {
2114 $imagesfrom = $this->mOptions->getAllowExternalImagesFrom();
2115 $imagesexception = !empty( $imagesfrom );
2117 # $imagesfrom could be either a single string or an array of strings, parse out the latter
2118 if ( $imagesexception && is_array( $imagesfrom ) ) {
2119 $imagematch =
false;
2120 foreach ( $imagesfrom
as $match ) {
2121 if ( strpos( $url, $match ) === 0 ) {
2126 } elseif ( $imagesexception ) {
2127 $imagematch = ( strpos( $url, $imagesfrom ) === 0 );
2129 $imagematch =
false;
2132 if ( $this->mOptions->getAllowExternalImages()
2133 || ( $imagesexception && $imagematch )
2135 if ( preg_match( self::EXT_IMAGE_REGEX, $url ) ) {
2140 if ( !$text && $this->mOptions->getEnableImageWhitelist()
2141 && preg_match( self::EXT_IMAGE_REGEX, $url )
2143 $whitelist = explode(
2145 wfMessage(
'external_image_whitelist' )->inContentLanguage()->
text()
2148 foreach ( $whitelist
as $entry ) {
2149 # Sanitize the regex fragment, make it case-insensitive, ignore blank entries/comments
2150 if ( strpos( $entry,
'#' ) === 0 || $entry ===
'' ) {
2153 if ( preg_match(
'/' . str_replace(
'/',
'\\/', $entry ) .
'/i', $url ) ) {
2154 # Image matches a whitelist entry
2172 public function replaceInternalLinks(
$s ) {
2173 $this->mLinkHolders->merge( $this->replaceInternalLinks2(
$s ) );
2185 public function replaceInternalLinks2( &
$s ) {
2186 static $tc =
false, $e1, $e1_img;
2187 # the % is needed to support urlencoded titles as well
2190 # Match a link having the form [[namespace:link|alternate]]trail
2191 $e1 =
"/^([{$tc}]+)(?:\\|(.+?))?]](.*)\$/sD";
2192 # Match cases where there is no "]]", which might still be images
2193 $e1_img =
"/^([{$tc}]+)\\|(.*)\$/sD";
2198 # split the entire text string on occurrences of [[
2200 # get the first element (all text up to first [[), and remove the space we added
2203 $line = $a->current(); # Workaround
for broken ArrayIterator::next()
that returns "
void"
2204 $s = substr(
$s, 1 );
2206 $useLinkPrefixExtension = $this->getTargetLanguage()->linkPrefixExtension();
2208 if ( $useLinkPrefixExtension ) {
2209 # Match the end of a line for a word that's not followed by whitespace,
2210 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
2211 $charset = $this->contLang->linkPrefixCharset();
2212 $e2 =
"/^((?>.*[^$charset]|))(.+)$/sDu";
2215 if ( is_null( $this->mTitle ) ) {
2216 throw new MWException( __METHOD__ .
": \$this->mTitle is null\n" );
2218 $nottalk = !$this->mTitle->isTalkPage();
2220 if ( $useLinkPrefixExtension ) {
2222 if ( preg_match( $e2,
$s, $m ) ) {
2223 $first_prefix = $m[2];
2225 $first_prefix =
false;
2231 $useSubpages = $this->areSubpagesAllowed();
2233 # Loop for each link
2234 for ( ;
$line !==
false &&
$line !==
null; $a->next(),
$line = $a->current() ) {
2235 # Check for excessive memory usage
2236 if ( $holders->isBig() ) {
2238 # Do the existence check, replace the link holders and clear the array
2239 $holders->replace(
$s );
2243 if ( $useLinkPrefixExtension ) {
2244 if ( preg_match( $e2,
$s, $m ) ) {
2245 list( ,
$s, $prefix ) = $m;
2250 if ( $first_prefix ) {
2251 $prefix = $first_prefix;
2252 $first_prefix =
false;
2256 $might_be_img =
false;
2260 # If we get a ] at the beginning of $m[3] that means we have a link that's something like:
2261 # [[Image:Foo.jpg|[http://example.com desc]]] <- having three ] in a row fucks up,
2262 # the real problem is with the $e1 regex
2264 # Still some problems for cases where the ] is meant to be outside punctuation,
2265 # and no image is in sight. See T4095.
2267 && substr( $m[3], 0, 1 ) ===
']'
2268 && strpos( $text,
'[' ) !==
false
2270 $text .=
']'; #
so that replaceExternalLinks($text) works
later
2271 $m[3] = substr( $m[3], 1 );
2273 # fix up urlencoded title texts
2274 if ( strpos( $m[1],
'%' ) !==
false ) {
2275 # Should anchors '#' also be rejected?
2276 $m[1] = str_replace( [
'<',
'>' ], [
'<',
'>' ], rawurldecode( $m[1] ) );
2279 } elseif ( preg_match( $e1_img,
$line, $m ) ) {
2280 # Invalid, but might be an image with a link in its caption
2281 $might_be_img =
true;
2283 if ( strpos( $m[1],
'%' ) !==
false ) {
2284 $m[1] = str_replace( [
'<',
'>' ], [
'<',
'>' ], rawurldecode( $m[1] ) );
2292 $origLink = ltrim( $m[1],
' ' );
2294 # Don't allow internal links to pages containing
2295 # PROTO: where PROTO is a valid URL protocol; these
2296 # should be external links.
2297 if ( preg_match(
'/^(?i:' . $this->mUrlProtocols .
')/', $origLink ) ) {
2302 # Make subpage if necessary
2303 if ( $useSubpages ) {
2304 $link = $this->maybeDoSubpageLink( $origLink, $text );
2313 $unstrip = $this->mStripState->killMarkers(
$link );
2314 $noMarkers = ( $unstrip ===
$link );
2317 if ( $nt ===
null ) {
2322 $ns = $nt->getNamespace();
2323 $iw = $nt->getInterwiki();
2325 $noforce = ( substr( $origLink, 0, 1 ) !==
':' );
2327 if ( $might_be_img ) { #
if this is actually an invalid
link
2328 if ( $ns ==
NS_FILE && $noforce ) { # but might be an image
2331 # look at the next 'line' to see if we can close it there
2333 $next_line = $a->current();
2334 if ( $next_line ===
false || $next_line ===
null ) {
2337 $m = explode(
']]', $next_line, 3 );
2338 if (
count( $m ) == 3 ) {
2339 # the first ]] closes the inner link, the second the image
2341 $text .=
"[[{$m[0]}]]{$m[1]}";
2344 } elseif (
count( $m ) == 2 ) {
2345 # if there's exactly one ]] that's fine, we'll keep looking
2346 $text .=
"[[{$m[0]}]]{$m[1]}";
2348 # if $next_line is invalid too, we need look no further
2349 $text .=
'[[' . $next_line;
2354 # we couldn't find the end of this imageLink, so output it raw
2355 # but don't ignore what might be perfectly normal links in the text we've examined
2356 $holders->merge( $this->replaceInternalLinks2( $text ) );
2357 $s .=
"{$prefix}[[$link|$text";
2358 # note: no $trail, because without an end, there *is* no trail
2361 }
else { #
it's not an image, so output it raw
2362 $s .= "{$prefix}[[$link|$text";
2363 # note: no $trail, because without an end, there *is* no trail
2368 $wasblank = ( $text == '' );
2372 # Strip off leading ':
'
2373 $text = substr( $text, 1 );
2376 # T6598 madness. Handle the quotes only if they come from the alternate part
2377 # [[Lista d''e paise d''o munno]] -> <a href="...">Lista d''e paise d''o munno</a>
2378 # [[Criticism of Harry Potter|Criticism of ''Harry Potter'']]
2379 # -> <a href="Criticism of Harry Potter">Criticism of <i>Harry Potter</i></a>
2380 $text = $this->doQuotes( $text );
2383 # Link not escaped by : , create the various objects
2384 if ( $noforce && !$nt->wasLocalInterwiki() ) {
2387 $iw && $this->mOptions->getInterwikiMagic() && $nottalk && (
2388 Language::fetchLanguageName( $iw, null, 'mw
' ) ||
2389 in_array( $iw, $this->siteConfig->get( 'ExtraInterlanguageLinkPrefixes
' ) )
2392 # T26502: filter duplicates
2393 if ( !isset( $this->mLangLinkLanguages[$iw] ) ) {
2394 $this->mLangLinkLanguages[$iw] = true;
2395 $this->mOutput->addLanguageLink( $nt->getFullText() );
2401 $s = rtrim( $s . $prefix ) . $trail; # T175416
2405 if ( $ns == NS_FILE ) {
2406 if ( !wfIsBadImage( $nt->getDBkey(), $this->mTitle ) ) {
2408 # if no parameters were passed, $text
2409 # becomes something like "File:Foo.png",
2410 # which we don't want to pass
on to the
2414 # recursively parse links inside the image caption
2415 # actually, this will parse them in any other parameters, too,
2416 # but it might be hard to fix that, and it doesn't matter ATM
2417 $text = $this->replaceExternalLinks( $text );
2418 $holders->merge( $this->replaceInternalLinks2( $text ) );
2420 # cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them
2421 $s .= $prefix . $this->armorLinks(
2422 $this->makeImage( $nt, $text, $holders ) ) . $trail;
2429 $s = rtrim(
$s . $prefix ) . $trail; # T2087, T87753
2432 $sortkey = $this->getDefaultSort();
2436 $sortkey = Sanitizer::decodeCharReferences( $sortkey );
2437 $sortkey = str_replace(
"\n",
'', $sortkey );
2438 $sortkey = $this->getTargetLanguage()->convertCategoryKey( $sortkey );
2439 $this->mOutput->addCategory( $nt->getDBkey(), $sortkey );
2445 # Self-link checking. For some languages, variants of the title are checked in
2446 # LinkHolderArray::doVariants() to allow batching the existence checks necessary
2447 # for linking to a different variant.
2448 if ( $ns !=
NS_SPECIAL && $nt->equals( $this->mTitle ) && !$nt->hasFragment() ) {
2453 # NS_MEDIA is a pseudo-namespace for linking directly to a file
2454 # @todo FIXME: Should do batch file existence checks, see comment below
2456 # Give extensions a chance to select the file revision for us
2460 [ $this, $nt, &
$options, &$descQuery ] );
2461 # Fetch and register the file (file title may be different via hooks)
2463 # Cloak with NOPARSE to avoid replacement in replaceExternalLinks
2464 $s .= $prefix . $this->armorLinks(
2469 # Some titles, such as valid special pages or files in foreign repos, should
2470 # be shown as bluelinks even though they're not included in the page table
2471 # @todo FIXME: isAlwaysKnown() can be expensive for file links; we should really do
2472 # batch file existence checks for NS_FILE and NS_MEDIA
2473 if ( $iw ==
'' && $nt->isAlwaysKnown() ) {
2474 $this->mOutput->addLink( $nt );
2475 $s .= $this->makeKnownLinkHolder( $nt, $text, $trail, $prefix );
2477 # Links will be added to the output link list after checking
2478 $s .= $holders->makeHolder( $nt, $text, [], $trail, $prefix );
2497 protected function makeKnownLinkHolder( $nt, $text =
'', $trail =
'', $prefix =
'' ) {
2500 if ( $text ==
'' ) {
2501 $text = htmlspecialchars( $nt->getPrefixedText() );
2504 $link = $this->getLinkRenderer()->makeKnownLink(
2505 $nt,
new HtmlArmor(
"$prefix$text$inside" )
2508 return $this->armorLinks(
$link ) . $trail;
2521 public function armorLinks( $text ) {
2522 return preg_replace(
'/\b((?i)' . $this->mUrlProtocols .
')/',
2523 self::MARKER_PREFIX .
"NOPARSE$1", $text );
2530 public function areSubpagesAllowed() {
2531 # Some namespaces don't allow subpages
2543 public function maybeDoSubpageLink( $target, &$text ) {
2555 public function doBlockLevels( $text, $linestart ) {
2570 public function getVariableValue( $index, $frame =
false ) {
2571 if ( is_null( $this->mTitle ) ) {
2576 throw new MWException( __METHOD__ .
' Should only be '
2577 .
' called while parsing (no title set)' );
2587 if (
Hooks::run(
'ParserGetVariableValueVarCache', [ &
$parser, &$this->mVarCache ] )
2588 && isset( $this->mVarCache[$index] )
2590 return $this->mVarCache[$index];
2593 $ts =
wfTimestamp( TS_UNIX, $this->mOptions->getTimestamp() );
2597 static $slowRevWords = [
'revisionid' =>
true ];
2599 isset( $slowRevWords[$index] ) &&
2600 $this->siteConfig->get(
'MiserMode' ) &&
2601 !$this->mOptions->getInterfaceMessage() &&
2605 return $this->mRevisionId ?
'-' :
'';
2608 $pageLang = $this->getFunctionLang();
2614 case 'currentmonth':
2617 case 'currentmonth1':
2620 case 'currentmonthname':
2623 case 'currentmonthnamegen':
2626 case 'currentmonthabbrev':
2641 case 'localmonthname':
2644 case 'localmonthnamegen':
2647 case 'localmonthabbrev':
2662 case 'fullpagename':
2665 case 'fullpagenamee':
2671 case 'subpagenamee':
2674 case 'rootpagename':
2677 case 'rootpagenamee':
2681 $this->mTitle->getRootText()
2684 case 'basepagename':
2687 case 'basepagenamee':
2691 $this->mTitle->getBaseText()
2694 case 'talkpagename':
2695 if ( $this->mTitle->canHaveTalkPage() ) {
2696 $talkPage = $this->mTitle->getTalkPage();
2702 case 'talkpagenamee':
2703 if ( $this->mTitle->canHaveTalkPage() ) {
2704 $talkPage = $this->mTitle->getTalkPage();
2710 case 'subjectpagename':
2711 $subjPage = $this->mTitle->getSubjectPage();
2714 case 'subjectpagenamee':
2715 $subjPage = $this->mTitle->getSubjectPage();
2719 $pageid = $this->getTitle()->getArticleID();
2720 if ( $pageid == 0 ) {
2721 # 0 means the page doesn't exist in the database,
2722 # which means the user is previewing a new page.
2723 # The vary-revision flag must be set, because the magic word
2724 # will have a different value once the page is saved.
2725 $this->mOutput->setFlag(
'vary-revision' );
2726 wfDebug( __METHOD__ .
": {{PAGEID}} used in a new page, setting vary-revision...\n" );
2728 $value = $pageid ?:
null;
2731 # Let the edit saving system know we should parse the page
2732 # *after* a revision ID has been assigned.
2733 $this->mOutput->setFlag(
'vary-revision-id' );
2734 wfDebug( __METHOD__ .
": {{REVISIONID}} used, setting vary-revision-id...\n" );
2735 $value = $this->mRevisionId;
2738 $rev = $this->getRevisionObject();
2745 $value = $this->mOptions->getSpeculativeRevId();
2747 $this->mOutput->setSpeculativeRevIdUsed(
$value );
2752 $value = (int)$this->getRevisionTimestampSubstring( 6, 2, self::MAX_TTS, $index );
2754 case 'revisionday2':
2755 $value = $this->getRevisionTimestampSubstring( 6, 2, self::MAX_TTS, $index );
2757 case 'revisionmonth':
2758 $value = $this->getRevisionTimestampSubstring( 4, 2, self::MAX_TTS, $index );
2760 case 'revisionmonth1':
2761 $value = (int)$this->getRevisionTimestampSubstring( 4, 2, self::MAX_TTS, $index );
2763 case 'revisionyear':
2764 $value = $this->getRevisionTimestampSubstring( 0, 4, self::MAX_TTS, $index );
2766 case 'revisiontimestamp':
2767 # Let the edit saving system know we should parse the page
2768 # *after* a revision ID has been assigned. This is for null edits.
2769 $this->mOutput->setFlag(
'vary-revision' );
2770 wfDebug( __METHOD__ .
": {{REVISIONTIMESTAMP}} used, setting vary-revision...\n" );
2771 $value = $this->getRevisionTimestamp();
2773 case 'revisionuser':
2774 # Let the edit saving system know we should parse the page
2775 # *after* a revision ID has been assigned for null edits.
2776 $this->mOutput->setFlag(
'vary-user' );
2777 wfDebug( __METHOD__ .
": {{REVISIONUSER}} used, setting vary-user...\n" );
2778 $value = $this->getRevisionUser();
2780 case 'revisionsize':
2781 $value = $this->getRevisionSize();
2784 $value = str_replace(
'_',
' ',
2785 $this->contLang->getNsText( $this->mTitle->getNamespace() ) );
2788 $value =
wfUrlencode( $this->contLang->getNsText( $this->mTitle->getNamespace() ) );
2790 case 'namespacenumber':
2791 $value = $this->mTitle->getNamespace();
2794 $value = $this->mTitle->canHaveTalkPage()
2795 ? str_replace(
'_',
' ', $this->mTitle->getTalkNsText() )
2799 $value = $this->mTitle->canHaveTalkPage() ?
wfUrlencode( $this->mTitle->getTalkNsText() ) :
'';
2801 case 'subjectspace':
2802 $value = str_replace(
'_',
' ', $this->mTitle->getSubjectNsText() );
2804 case 'subjectspacee':
2807 case 'currentdayname':
2820 # @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
2821 # int to remove the padding
2827 case 'localdayname':
2828 $value = $pageLang->getWeekdayName(
2836 $value = $pageLang->time(
2846 # @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
2847 # int to remove the padding
2853 case 'numberofarticles':
2856 case 'numberoffiles':
2859 case 'numberofusers':
2862 case 'numberofactiveusers':
2865 case 'numberofpages':
2868 case 'numberofadmins':
2871 case 'numberofedits':
2874 case 'currenttimestamp':
2877 case 'localtimestamp':
2880 case 'currentversion':
2884 return $this->siteConfig->get(
'ArticlePath' );
2886 return $this->siteConfig->get(
'Sitename' );
2888 return $this->siteConfig->get(
'Server' );
2890 return $this->siteConfig->get(
'ServerName' );
2892 return $this->siteConfig->get(
'ScriptPath' );
2894 return $this->siteConfig->get(
'StylePath' );
2895 case 'directionmark':
2896 return $pageLang->getDirMark();
2897 case 'contentlanguage':
2898 return $this->siteConfig->get(
'LanguageCode' );
2899 case 'pagelanguage':
2900 $value = $pageLang->getCode();
2902 case 'cascadingsources':
2908 'ParserGetVariableValueSwitch',
2909 [ &
$parser, &$this->mVarCache, &$index, &
$ret, &$frame ]
2916 $this->mVarCache[$index] =
$value;
2929 private function getRevisionTimestampSubstring( $start, $len, $mtts, $variable ) {
2930 # Get the timezone-adjusted timestamp to be used for this revision
2931 $resNow = substr( $this->getRevisionTimestamp(), $start, $len );
2932 # Possibly set vary-revision if there is not yet an associated revision
2933 if ( !$this->getRevisionObject() ) {
2934 # Get the timezone-adjusted timestamp $mtts seconds in the future
2936 $this->contLang->userAdjust(
wfTimestamp( TS_MW, time() + $mtts ),
'' ),
2941 if ( $resNow !== $resThen ) {
2942 # Let the edit saving system know we should parse the page
2943 # *after* a revision ID has been assigned. This is for null edits.
2944 $this->mOutput->setFlag(
'vary-revision' );
2945 wfDebug( __METHOD__ .
": $variable used, setting vary-revision...\n" );
2957 public function initialiseVariables() {
2958 $variableIDs = $this->magicWordFactory->getVariableIDs();
2959 $substIDs = $this->magicWordFactory->getSubstIDs();
2961 $this->mVariables = $this->magicWordFactory->newArray( $variableIDs );
2962 $this->mSubstWords = $this->magicWordFactory->newArray( $substIDs );
2987 public function preprocessToDom( $text, $flags = 0 ) {
2988 $dom = $this->getPreprocessor()->preprocessToObj( $text, $flags );
2999 public static function splitWhitespace(
$s ) {
3000 $ltrimmed = ltrim(
$s );
3001 $w1 = substr(
$s, 0, strlen(
$s ) - strlen( $ltrimmed ) );
3002 $trimmed = rtrim( $ltrimmed );
3003 $diff = strlen( $ltrimmed ) - strlen( $trimmed );
3005 $w2 = substr( $ltrimmed, -$diff );
3009 return [ $w1, $trimmed, $w2 ];
3032 public function replaceVariables( $text, $frame =
false, $argsOnly =
false ) {
3033 # Is there any text? Also, Prevent too big inclusions!
3034 $textSize = strlen( $text );
3035 if ( $textSize < 1 || $textSize > $this->mOptions->getMaxIncludeSize() ) {
3039 if ( $frame ===
false ) {
3040 $frame = $this->getPreprocessor()->newFrame();
3041 } elseif ( !( $frame instanceof
PPFrame ) ) {
3042 wfDebug( __METHOD__ .
" called using plain parameters instead of "
3043 .
"a PPFrame instance. Creating custom frame.\n" );
3044 $frame = $this->getPreprocessor()->newCustomFrame( $frame );
3047 $dom = $this->preprocessToDom( $text );
3049 $text = $frame->expand( $dom, $flags );
3061 public static function createAssocArgs(
$args ) {
3065 $eqpos = strpos( $arg,
'=' );
3066 if ( $eqpos ===
false ) {
3067 $assocArgs[$index++] = $arg;
3069 $name = trim( substr( $arg, 0, $eqpos ) );
3070 $value = trim( substr( $arg, $eqpos + 1 ) );
3071 if (
$value ===
false ) {
3074 if (
$name !==
false ) {
3109 public function limitationWarn( $limitationType, $current =
'', $max =
'' ) {
3110 # does no harm if $current and $max are present but are unnecessary for the message
3111 # Not doing ->inLanguage( $this->mOptions->getUserLangObj() ), since this is shown
3112 # only during preview, and that would split the parser cache unnecessarily.
3113 $warning =
wfMessage(
"$limitationType-warning" )->numParams( $current, $max )
3115 $this->mOutput->addWarning( $warning );
3116 $this->addTrackingCategory(
"$limitationType-category" );
3131 public function braceSubstitution( $piece, $frame ) {
3141 $forceRawInterwiki =
false;
3143 $isChildObj =
false;
3145 $isLocalObj =
false;
3147 # Title object, where $text came from
3150 # $part1 is the bit before the first |, and must contain only title characters.
3151 # Various prefixes will be stripped from it later.
3152 $titleWithSpaces = $frame->expand( $piece[
'title'] );
3153 $part1 = trim( $titleWithSpaces );
3156 # Original title text preserved for various purposes
3157 $originalTitle = $part1;
3159 # $args is a list of argument nodes, starting from index 0, not including $part1
3160 # @todo FIXME: If piece['parts'] is null then the call to getLength()
3161 # below won't work b/c this $args isn't an object
3162 $args = ( $piece[
'parts'] ==
null ) ? [] : $piece[
'parts'];
3164 $profileSection =
null;
3168 $substMatch = $this->mSubstWords->matchStartAndRemove( $part1 );
3170 # Possibilities for substMatch: "subst", "safesubst" or FALSE
3171 # Decide whether to expand template or keep wikitext as-is.
3172 if ( $this->ot[
'wiki'] ) {
3173 if ( $substMatch ===
false ) {
3174 $literal =
true; # literal when
in PST with no prefix
3176 $literal =
false; # expand when
in PST with subst:
or safesubst:
3179 if ( $substMatch ==
'subst' ) {
3180 $literal =
true; # literal when
not in PST with
plain subst:
3182 $literal =
false; # expand when
not in PST with safesubst:
or no prefix
3186 $text = $frame->virtualBracketedImplode(
'{{',
'|',
'}}', $titleWithSpaces,
$args );
3193 if ( !$found &&
$args->getLength() == 0 ) {
3194 $id = $this->mVariables->matchStartToEnd( $part1 );
3195 if ( $id !==
false ) {
3196 $text = $this->getVariableValue( $id, $frame );
3197 if ( $this->magicWordFactory->getCacheTTL( $id ) > -1 ) {
3198 $this->mOutput->updateCacheExpiry(
3199 $this->magicWordFactory->getCacheTTL( $id ) );
3205 # MSG, MSGNW and RAW
3208 $mwMsgnw = $this->magicWordFactory->get(
'msgnw' );
3209 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
3212 # Remove obsolete MSG:
3213 $mwMsg = $this->magicWordFactory->get(
'msg' );
3214 $mwMsg->matchStartAndRemove( $part1 );
3218 $mwRaw = $this->magicWordFactory->get(
'raw' );
3219 if ( $mwRaw->matchStartAndRemove( $part1 ) ) {
3220 $forceRawInterwiki =
true;
3226 $colonPos = strpos( $part1,
':' );
3227 if ( $colonPos !==
false ) {
3228 $func = substr( $part1, 0, $colonPos );
3229 $funcArgs = [ trim( substr( $part1, $colonPos + 1 ) ) ];
3230 $argsLength =
$args->getLength();
3231 for ( $i = 0; $i < $argsLength; $i++ ) {
3232 $funcArgs[] =
$args->item( $i );
3235 $result = $this->callParserFunction( $frame, $func, $funcArgs );
3238 if ( isset(
$result[
'title'] ) ) {
3241 if ( isset(
$result[
'found'] ) ) {
3244 if ( array_key_exists(
'text',
$result ) ) {
3248 if ( isset(
$result[
'nowiki'] ) ) {
3251 if ( isset(
$result[
'isHTML'] ) ) {
3254 if ( isset(
$result[
'forceRawInterwiki'] ) ) {
3255 $forceRawInterwiki =
$result[
'forceRawInterwiki'];
3257 if ( isset(
$result[
'isChildObj'] ) ) {
3258 $isChildObj =
$result[
'isChildObj'];
3260 if ( isset(
$result[
'isLocalObj'] ) ) {
3261 $isLocalObj =
$result[
'isLocalObj'];
3266 # Finish mangling title and then check for loops.
3267 # Set $title to a Title object and $titleText to the PDBK
3270 # Split the title into page and subpage
3272 $relative = $this->maybeDoSubpageLink( $part1, $subpage );
3273 if ( $part1 !== $relative ) {
3275 $ns = $this->mTitle->getNamespace();
3279 $titleText =
$title->getPrefixedText();
3280 # Check for language variants if the template is not found
3281 if ( $this->getTargetLanguage()->hasVariants() &&
$title->getArticleID() == 0 ) {
3282 $this->getTargetLanguage()->findVariantLink( $part1,
$title,
true );
3284 # Do recursion depth check
3285 $limit = $this->mOptions->getMaxTemplateDepth();
3286 if ( $frame->depth >= $limit ) {
3288 $text =
'<span class="error">'
3289 .
wfMessage(
'parser-template-recursion-depth-warning' )
3290 ->numParams( $limit )->inContentLanguage()->text()
3296 # Load from database
3297 if ( !$found &&
$title ) {
3298 $profileSection = $this->mProfiler->scopedProfileIn(
$title->getPrefixedDBkey() );
3299 if ( !
$title->isExternal() ) {
3300 if (
$title->isSpecialPage()
3301 && $this->mOptions->getAllowSpecialInclusion()
3302 && $this->ot[
'html']
3304 $specialPage = $this->specialPageFactory->getPage(
$title->getDBkey() );
3309 $argsLength =
$args->getLength();
3310 for ( $i = 0; $i < $argsLength; $i++ ) {
3311 $bits =
$args->item( $i )->splitArg();
3312 if ( strval( $bits[
'index'] ) ===
'' ) {
3314 $value = trim( $frame->expand( $bits[
'value'] ) );
3323 if ( $specialPage && $specialPage->maxIncludeCacheTime() === 0 ) {
3324 $context->setUser( $this->getUser() );
3329 $context->setLanguage( $this->mOptions->getUserLangObj() );
3330 $ret = $this->specialPageFactory->capturePath(
$title,
$context, $this->getLinkRenderer() );
3332 $text =
$context->getOutput()->getHTML();
3333 $this->mOutput->addOutputPageMetadata(
$context->getOutput() );
3336 if ( $specialPage && $specialPage->maxIncludeCacheTime() !==
false ) {
3337 $this->mOutput->updateRuntimeAdaptiveExpiry(
3338 $specialPage->maxIncludeCacheTime()
3343 $found =
false; # access denied
3344 wfDebug( __METHOD__ .
": template inclusion denied for " .
3345 $title->getPrefixedDBkey() .
"\n" );
3348 if ( $text !==
false ) {
3354 # If the title is valid but undisplayable, make a link to it
3355 if ( !$found && ( $this->ot[
'html'] || $this->ot[
'pre'] ) ) {
3356 $text =
"[[:$titleText]]";
3359 } elseif (
$title->isTrans() ) {
3360 # Interwiki transclusion
3361 if ( $this->ot[
'html'] && !$forceRawInterwiki ) {
3362 $text = $this->interwikiTransclude(
$title,
'render' );
3365 $text = $this->interwikiTransclude(
$title,
'raw' );
3366 # Preprocess it like a template
3367 $text = $this->preprocessToDom( $text, self::PTD_FOR_INCLUSION );
3373 # Do infinite loop check
3374 # This has to be done after redirect resolution to avoid infinite loops via redirects
3375 if ( !$frame->loopCheck(
$title ) ) {
3377 $text =
'<span class="error">'
3378 .
wfMessage(
'parser-template-loop-warning', $titleText )->inContentLanguage()->text()
3380 $this->addTrackingCategory(
'template-loop-category' );
3381 $this->mOutput->addWarning(
wfMessage(
'template-loop-warning',
3383 wfDebug( __METHOD__ .
": template loop broken at '$titleText'\n" );
3387 # If we haven't found text to substitute by now, we're done
3388 # Recover the source wikitext and return it
3390 $text = $frame->virtualBracketedImplode(
'{{',
'|',
'}}', $titleWithSpaces,
$args );
3391 if ( $profileSection ) {
3392 $this->mProfiler->scopedProfileOut( $profileSection );
3394 return [
'object' => $text ];
3397 # Expand DOM-style return values in a child frame
3398 if ( $isChildObj ) {
3399 # Clean up argument array
3404 } elseif ( $titleText !==
false && $newFrame->isEmpty() ) {
3405 # Expansion is eligible for the empty-frame cache
3406 $text = $newFrame->cachedExpand( $titleText, $text );
3408 # Uncached expansion
3409 $text = $newFrame->expand( $text );
3412 if ( $isLocalObj && $nowiki ) {
3414 $isLocalObj =
false;
3417 if ( $profileSection ) {
3418 $this->mProfiler->scopedProfileOut( $profileSection );
3421 # Replace raw HTML by a placeholder
3423 $text = $this->insertStripItem( $text );
3424 } elseif ( $nowiki && ( $this->ot[
'html'] || $this->ot[
'pre'] ) ) {
3425 # Escape nowiki-style return values
3427 } elseif ( is_string( $text )
3428 && !$piece[
'lineStart']
3429 && preg_match(
'/^(?:{\\||:|;|#|\*)/', $text )
3431 # T2529: if the template begins with a table or block-level
3432 # element, it should be treated as beginning a new line.
3433 # This behavior is somewhat controversial.
3434 $text =
"\n" . $text;
3437 if ( is_string( $text ) && !$this->incrementIncludeSize(
'post-expand', strlen( $text ) ) ) {
3438 # Error, oversize inclusion
3439 if ( $titleText !==
false ) {
3440 # Make a working, properly escaped link if possible (T25588)
3441 $text =
"[[:$titleText]]";
3443 # This will probably not be a working link, but at least it may
3444 # provide some hint of where the problem is
3445 preg_replace(
'/^:/',
'', $originalTitle );
3446 $text =
"[[:$originalTitle]]";
3448 $text .= $this->insertStripItem(
'<!-- WARNING: template omitted, '
3449 .
'post-expand include size too large -->' );
3450 $this->limitationWarn(
'post-expand-template-inclusion' );
3453 if ( $isLocalObj ) {
3454 $ret = [
'object' => $text ];
3456 $ret = [
'text' => $text ];
3481 public function callParserFunction( $frame, $function,
array $args = [] ) {
3482 # Case sensitive functions
3483 if ( isset( $this->mFunctionSynonyms[1][$function] ) ) {
3484 $function = $this->mFunctionSynonyms[1][$function];
3486 # Case insensitive functions
3487 $function = $this->contLang->lc( $function );
3488 if ( isset( $this->mFunctionSynonyms[0][$function] ) ) {
3489 $function = $this->mFunctionSynonyms[0][$function];
3491 return [
'found' =>
false ];
3495 list( $callback, $flags ) = $this->mFunctionHooks[$function];
3502 # Convert arguments to PPNodes and collect for appending to $allArgs
3504 foreach (
$args as $k => $v ) {
3505 if ( $v instanceof
PPNode || $k === 0 ) {
3508 $funcArgs[] = $this->mPreprocessor->newPartNodeArray( [ $k => $v ] )->item( 0 );
3512 # Add a frame parameter, and pass the arguments as an array
3513 $allArgs[] = $frame;
3514 $allArgs[] = $funcArgs;
3516 # Convert arguments to plain text and append to $allArgs
3517 foreach (
$args as $k => $v ) {
3518 if ( $v instanceof
PPNode ) {
3519 $allArgs[] = trim( $frame->expand( $v ) );
3520 } elseif ( is_int( $k ) && $k >= 0 ) {
3521 $allArgs[] = trim( $v );
3523 $allArgs[] = trim(
"$k=$v" );
3528 $result = $callback( ...$allArgs );
3530 # The interface for function hooks allows them to return a wikitext
3531 # string or an array containing the string and any flags. This mungs
3532 # things around to match what this method should return.
3549 $preprocessFlags = 0;
3550 if ( isset(
$result[
'noparse'] ) ) {
3551 $noparse =
$result[
'noparse'];
3553 if ( isset(
$result[
'preprocessFlags'] ) ) {
3554 $preprocessFlags =
$result[
'preprocessFlags'];
3558 $result[
'text'] = $this->preprocessToDom(
$result[
'text'], $preprocessFlags );
3573 public function getTemplateDom(
$title ) {
3575 $titleText =
$title->getPrefixedDBkey();
3577 if ( isset( $this->mTplRedirCache[$titleText] ) ) {
3578 list( $ns, $dbk ) = $this->mTplRedirCache[$titleText];
3580 $titleText =
$title->getPrefixedDBkey();
3582 if ( isset( $this->mTplDomCache[$titleText] ) ) {
3583 return [ $this->mTplDomCache[$titleText],
$title ];
3586 # Cache miss, go to the database
3589 if ( $text ===
false ) {
3590 $this->mTplDomCache[$titleText] =
false;
3591 return [
false,
$title ];
3594 $dom = $this->preprocessToDom( $text, self::PTD_FOR_INCLUSION );
3595 $this->mTplDomCache[$titleText] = $dom;
3597 if ( !
$title->equals( $cacheTitle ) ) {
3598 $this->mTplRedirCache[$cacheTitle->getPrefixedDBkey()] =
3616 public function fetchCurrentRevisionOfTitle(
$title ) {
3617 $cacheKey =
$title->getPrefixedDBkey();
3618 if ( !$this->currentRevisionCache ) {
3619 $this->currentRevisionCache =
new MapCacheLRU( 100 );
3621 if ( !$this->currentRevisionCache->has( $cacheKey ) ) {
3622 $this->currentRevisionCache->set( $cacheKey,
3624 call_user_func( $this->mOptions->getCurrentRevisionCallback(),
$title, $this )
3627 return $this->currentRevisionCache->get( $cacheKey );
3650 public function fetchTemplateAndTitle(
$title ) {
3652 $templateCb = $this->mOptions->getTemplateCallback();
3653 $stuff = call_user_func( $templateCb,
$title, $this );
3655 $text = $stuff[
'text'];
3656 if ( is_string( $stuff[
'text'] ) ) {
3657 $text = strtr( $text,
"\x7f",
"?" );
3659 $finalTitle = $stuff[
'finalTitle'] ??
$title;
3660 if ( isset( $stuff[
'deps'] ) ) {
3661 foreach ( $stuff[
'deps']
as $dep ) {
3662 $this->mOutput->addTemplate( $dep[
'title'], $dep[
'page_id'], $dep[
'rev_id'] );
3663 if ( $dep[
'title']->equals( $this->getTitle() ) ) {
3666 $this->mOutput->setFlag(
'vary-revision' );
3670 return [ $text, $finalTitle ];
3678 public function fetchTemplate(
$title ) {
3679 return $this->fetchTemplateAndTitle(
$title )[0];
3691 public static function statelessFetchTemplate(
$title,
$parser =
false ) {
3692 $text = $skip =
false;
3696 # Loop to fetch the article, with up to 1 redirect
3697 for ( $i = 0; $i < 2 && is_object(
$title ); $i++ ) {
3698 # Give extensions a chance to select the revision instead
3699 $id =
false; # Assume current
3700 Hooks::run(
'BeforeParserFetchTemplateAndtitle',
3707 'page_id' =>
$title->getArticleID(),
3720 $rev_id =
$rev ?
$rev->getId() : 0;
3721 # If there is no current revision, there is no page
3722 if ( $id ===
false && !
$rev ) {
3723 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
3724 $linkCache->addBadLinkObj(
$title );
3729 'page_id' =>
$title->getArticleID(),
3730 'rev_id' => $rev_id ];
3732 # We fetched a rev from a different title; register it too...
3734 'title' =>
$rev->getTitle(),
3735 'page_id' =>
$rev->getPage(),
3736 'rev_id' => $rev_id ];
3746 if ( $text ===
false || $text ===
null ) {
3751 $message =
wfMessage( MediaWikiServices::getInstance()->getContentLanguage()->
3752 lcfirst(
$title->getText() ) )->inContentLanguage();
3753 if ( !$message->exists() ) {
3758 $text = $message->plain();
3771 'finalTitle' => $finalTitle,
3800 # Register the file as a dependency...
3801 $this->mOutput->addImage(
$title->getDBkey(),
$time, $sha1 );
3803 # Update fetched file title
3805 $this->mOutput->addImage(
$title->getDBkey(),
$time, $sha1 );
3820 protected function fetchFileNoRegister(
$title,
$options = [] ) {
3821 if ( isset(
$options[
'broken'] ) ) {
3823 } elseif ( isset(
$options[
'sha1'] ) ) {
3839 public function interwikiTransclude(
$title, $action ) {
3840 if ( !$this->siteConfig->get(
'EnableScaryTranscluding' ) ) {
3841 return wfMessage(
'scarytranscludedisabled' )->inContentLanguage()->text();
3844 $url =
$title->getFullURL( [
'action' => $action ] );
3845 if ( strlen( $url ) > 1024 ) {
3846 return wfMessage(
'scarytranscludetoolong' )->inContentLanguage()->text();
3849 $wikiId =
$title->getTransWikiID();
3852 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
3856 'interwiki-transclude',
3857 ( $wikiId !==
false ) ? $wikiId :
'external',
3860 $this->siteConfig->get(
'TranscludeCacheExpiry' ),
3866 $ttl = $cache::TTL_UNCACHEABLE;
3867 } elseif (
$req->getResponseHeader(
'X-Database-Lagged' ) !==
null ) {
3868 $ttl = min( $cache::TTL_LAGGED, $ttl );
3872 'text' =>
$status->isOK() ?
$req->getContent() :
null,
3873 'code' =>
$req->getStatus()
3877 'checkKeys' => ( $wikiId !==
false )
3878 ? [
$cache->makeGlobalKey(
'interwiki-page', $wikiId,
$title->getDBkey() ) ]
3880 'pcGroup' =>
'interwiki-transclude:5',
3881 'pcTTL' => $cache::TTL_PROC_LONG
3885 if ( is_string(
$data[
'text'] ) ) {
3886 $text =
$data[
'text'];
3887 } elseif (
$data[
'code'] != 200 ) {
3889 $text =
wfMessage(
'scarytranscludefailed-httpstatus' )
3890 ->params( $url,
$data[
'code'] )->inContentLanguage()->text();
3892 $text =
wfMessage(
'scarytranscludefailed', $url )->inContentLanguage()->text();
3907 public function argSubstitution( $piece, $frame ) {
3909 $parts = $piece[
'parts'];
3910 $nameWithSpaces = $frame->expand( $piece[
'title'] );
3911 $argName = trim( $nameWithSpaces );
3913 $text = $frame->getArgument( $argName );
3914 if ( $text ===
false && $parts->getLength() > 0
3915 && ( $this->ot[
'html']
3917 || ( $this->ot[
'wiki'] && $frame->isTemplate() )
3920 # No match in frame, use the supplied default
3921 $object = $parts->item( 0 )->getChildren();
3923 if ( !$this->incrementIncludeSize(
'arg', strlen( $text ) ) ) {
3924 $error =
'<!-- WARNING: argument omitted, expansion size too large -->';
3925 $this->limitationWarn(
'post-expand-template-argument' );
3928 if ( $text ===
false && $object ===
false ) {
3930 $object = $frame->virtualBracketedImplode(
'{{{',
'|',
'}}}', $nameWithSpaces, $parts );
3932 if ( $error !==
false ) {
3935 if ( $object !==
false ) {
3936 $ret = [
'object' => $object ];
3938 $ret = [
'text' => $text ];
3959 public function extensionSubstitution(
$params, $frame ) {
3960 static $errorStr =
'<span class="error">';
3961 static $errorLen = 20;
3964 if ( substr(
$name, 0, $errorLen ) === $errorStr ) {
3971 if ( substr( $attrText, 0, $errorLen ) === $errorStr ) {
3981 $marker = self::MARKER_PREFIX .
"-$name-"
3982 . sprintf(
'%08X', $this->mMarkerIndex++ ) . self::MARKER_SUFFIX;
3984 $isFunctionTag = isset( $this->mFunctionTagHooks[strtolower(
$name )] ) &&
3985 ( $this->ot[
'html'] || $this->ot[
'pre'] );
3986 if ( $isFunctionTag ) {
3987 $markerType =
'none';
3989 $markerType =
'general';
3991 if ( $this->ot[
'html'] || $isFunctionTag ) {
3993 $attributes = Sanitizer::decodeTagAttributes( $attrText );
3994 if ( isset(
$params[
'attributes'] ) ) {
3995 $attributes +=
$params[
'attributes'];
3998 if ( isset( $this->mTagHooks[
$name] ) ) {
3999 $output = call_user_func_array( $this->mTagHooks[
$name],
4000 [
$content, $attributes, $this, $frame ] );
4001 } elseif ( isset( $this->mFunctionTagHooks[
$name] ) ) {
4002 list( $callback, ) = $this->mFunctionTagHooks[
$name];
4008 $output =
'<span class="error">Invalid tag extension name: ' .
4009 htmlspecialchars(
$name ) .
'</span>';
4016 if ( isset( $flags[
'markerType'] ) ) {
4017 $markerType = $flags[
'markerType'];
4021 if ( is_null( $attrText ) ) {
4024 if ( isset(
$params[
'attributes'] ) ) {
4025 foreach (
$params[
'attributes']
as $attrName => $attrValue ) {
4026 $attrText .=
' ' . htmlspecialchars( $attrName ) .
'="' .
4027 htmlspecialchars( $attrValue ) .
'"';
4031 $output =
"<$name$attrText/>";
4033 $close = is_null(
$params[
'close'] ) ?
'' : $frame->expand(
$params[
'close'] );
4034 if ( substr( $close, 0, $errorLen ) === $errorStr ) {
4038 $output =
"<$name$attrText>$content$close";
4042 if ( $markerType ===
'none' ) {
4044 } elseif ( $markerType ===
'nowiki' ) {
4045 $this->mStripState->addNoWiki( $marker,
$output );
4046 } elseif ( $markerType ===
'general' ) {
4047 $this->mStripState->addGeneral( $marker,
$output );
4049 throw new MWException( __METHOD__ .
': invalid marker type' );
4061 public function incrementIncludeSize(
$type, $size ) {
4062 if ( $this->mIncludeSizes[
$type] + $size > $this->mOptions->getMaxIncludeSize() ) {
4065 $this->mIncludeSizes[
$type] += $size;
4075 public function incrementExpensiveFunctionCount() {
4076 $this->mExpensiveFunctionCount++;
4077 return $this->mExpensiveFunctionCount <= $this->mOptions->getExpensiveParserFunctionLimit();
4088 public function doDoubleUnderscore( $text ) {
4089 # The position of __TOC__ needs to be recorded
4090 $mw = $this->magicWordFactory->get(
'toc' );
4091 if ( $mw->match( $text ) ) {
4092 $this->mShowToc =
true;
4093 $this->mForceTocPosition =
true;
4095 # Set a placeholder. At the end we'll fill it in with the TOC.
4096 $text = $mw->replace(
'<!--MWTOC\'"-->', $text, 1 );
4098 # Only keep the first one.
4099 $text = $mw->replace(
'', $text );
4102 # Now match and remove the rest of them
4103 $mwa = $this->magicWordFactory->getDoubleUnderscoreArray();
4104 $this->mDoubleUnderscores = $mwa->matchAndRemove( $text );
4106 if ( isset( $this->mDoubleUnderscores[
'nogallery'] ) ) {
4107 $this->mOutput->mNoGallery =
true;
4109 if ( isset( $this->mDoubleUnderscores[
'notoc'] ) && !$this->mForceTocPosition ) {
4110 $this->mShowToc =
false;
4112 if ( isset( $this->mDoubleUnderscores[
'hiddencat'] )
4115 $this->addTrackingCategory(
'hidden-category-category' );
4117 # (T10068) Allow control over whether robots index a page.
4118 # __INDEX__ always overrides __NOINDEX__, see T16899
4119 if ( isset( $this->mDoubleUnderscores[
'noindex'] ) && $this->mTitle->canUseNoindex() ) {
4120 $this->mOutput->setIndexPolicy(
'noindex' );
4121 $this->addTrackingCategory(
'noindex-category' );
4123 if ( isset( $this->mDoubleUnderscores[
'index'] ) && $this->mTitle->canUseNoindex() ) {
4124 $this->mOutput->setIndexPolicy(
'index' );
4125 $this->addTrackingCategory(
'index-category' );
4128 # Cache all double underscores in the database
4129 foreach ( $this->mDoubleUnderscores
as $key => $val ) {
4130 $this->mOutput->setProperty( $key,
'' );
4141 public function addTrackingCategory( $msg ) {
4142 return $this->mOutput->addTrackingCategory( $msg, $this->mTitle );
4161 public function formatHeadings( $text, $origText, $isMain =
true ) {
4162 # Inhibit editsection links if requested in the page
4163 if ( isset( $this->mDoubleUnderscores[
'noeditsection'] ) ) {
4164 $maybeShowEditLink =
false;
4166 $maybeShowEditLink =
true;
4169 # Get all headlines for numbering them and adding funky stuff like [edit]
4170 # links - this is for later, but we need the number of headlines right now
4171 # NOTE: white space in headings have been trimmed in doHeadings. They shouldn't
4172 # be trimmed here since whitespace in HTML headings is significant.
4174 $numMatches = preg_match_all(
4175 '/<H(?P<level>[1-6])(?P<attrib>.*?>)(?P<header>[\s\S]*?)<\/H[1-6] *>/i',
4180 # if there are fewer than 4 headlines in the article, do not show TOC
4181 # unless it's been explicitly enabled.
4182 $enoughToc = $this->mShowToc &&
4183 ( ( $numMatches >= 4 ) || $this->mForceTocPosition );
4185 # Allow user to stipulate that a page should have a "new section"
4186 # link added via __NEWSECTIONLINK__
4187 if ( isset( $this->mDoubleUnderscores[
'newsectionlink'] ) ) {
4188 $this->mOutput->setNewSection(
true );
4191 # Allow user to remove the "new section"
4192 # link via __NONEWSECTIONLINK__
4193 if ( isset( $this->mDoubleUnderscores[
'nonewsectionlink'] ) ) {
4194 $this->mOutput->hideNewSection(
true );
4197 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
4198 # override above conditions and always show TOC above first header
4199 if ( isset( $this->mDoubleUnderscores[
'forcetoc'] ) ) {
4200 $this->mShowToc =
true;
4208 # Ugh .. the TOC should have neat indentation levels which can be
4209 # passed to the skin functions. These are determined here
4213 $sublevelCount = [];
4219 $markerRegex = self::MARKER_PREFIX .
"-h-(\d+)-" . self::MARKER_SUFFIX;
4220 $baseTitleText = $this->mTitle->getPrefixedDBkey();
4221 $oldType = $this->mOutputType;
4223 $frame = $this->getPreprocessor()->newFrame();
4224 $root = $this->preprocessToDom( $origText );
4225 $node = $root->getFirstChild();
4230 $headlines = $numMatches !==
false ?
$matches[3] : [];
4232 $maxTocLevel = $this->siteConfig->get(
'MaxTocLevel' );
4233 foreach ( $headlines
as $headline ) {
4234 $isTemplate =
false;
4236 $sectionIndex =
false;
4238 $markerMatches = [];
4239 if ( preg_match(
"/^$markerRegex/", $headline, $markerMatches ) ) {
4240 $serial = $markerMatches[1];
4241 list( $titleText, $sectionIndex ) = $this->mHeadings[$serial];
4242 $isTemplate = ( $titleText != $baseTitleText );
4243 $headline = preg_replace(
"/^$markerRegex\\s*/",
"", $headline );
4247 $prevlevel = $level;
4249 $level =
$matches[1][$headlineCount];
4251 if ( $level > $prevlevel ) {
4252 # Increase TOC level
4254 $sublevelCount[$toclevel] = 0;
4255 if ( $toclevel < $maxTocLevel ) {
4256 $prevtoclevel = $toclevel;
4260 } elseif ( $level < $prevlevel && $toclevel > 1 ) {
4261 # Decrease TOC level, find level to jump to
4263 for ( $i = $toclevel; $i > 0; $i-- ) {
4264 if ( $levelCount[$i] == $level ) {
4265 # Found last matching level
4268 } elseif ( $levelCount[$i] < $level ) {
4269 # Found first matching level below current level
4277 if ( $toclevel < $maxTocLevel ) {
4278 if ( $prevtoclevel < $maxTocLevel ) {
4279 # Unindent only if the previous toc level was shown :p
4281 $prevtoclevel = $toclevel;
4287 # No change in level, end TOC line
4288 if ( $toclevel < $maxTocLevel ) {
4293 $levelCount[$toclevel] = $level;
4295 # count number of headlines for each level
4296 $sublevelCount[$toclevel]++;
4298 for ( $i = 1; $i <= $toclevel; $i++ ) {
4299 if ( !empty( $sublevelCount[$i] ) ) {
4303 $numbering .= $this->getTargetLanguage()->formatNum( $sublevelCount[$i] );
4308 # The safe header is a version of the header text safe to use for links
4310 # Remove link placeholders by the link text.
4311 # <!--LINK number-->
4313 # link text with suffix
4314 # Do this before unstrip since link text can contain strip markers
4315 $safeHeadline = $this->replaceLinkHoldersText( $headline );
4317 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
4318 $safeHeadline = $this->mStripState->unstripBoth( $safeHeadline );
4320 # Remove any <style> or <script> tags (T198618)
4321 $safeHeadline = preg_replace(
4322 '#<(style|script)(?: [^>]*[^>/])?>.*?</\1>#is',
4327 # Strip out HTML (first regex removes any tag not allowed)
4329 # * <sup> and <sub> (T10393)
4333 # * <span dir="rtl"> and <span dir="ltr"> (T37167)
4334 # * <s> and <strike> (T35715)
4335 # We strip any parameter from accepted tags (second regex), except dir="rtl|ltr" from <span>,
4336 # to allow setting directionality in toc items.
4337 $tocline = preg_replace(
4339 '#<(?!/?(span|sup|sub|bdi|i|b|s|strike)(?: [^>]*)?>).*?>#',
4340 '#<(/?(?:span(?: dir="(?:rtl|ltr)")?|sup|sub|bdi|i|b|s|strike))(?: .*?)?>#'
4346 # Strip '<span></span>', which is the result from the above if
4347 # <span id="foo"></span> is used to produce an additional anchor
4349 $tocline = str_replace(
'<span></span>',
'', $tocline );
4351 $tocline = trim( $tocline );
4353 # For the anchor, strip out HTML-y stuff period
4354 $safeHeadline = preg_replace(
'/<.*?>/',
'', $safeHeadline );
4355 $safeHeadline = Sanitizer::normalizeSectionNameWhitespace( $safeHeadline );
4357 # Save headline for section edit hint before it's escaped
4358 $headlineHint = $safeHeadline;
4360 # Decode HTML entities
4361 $safeHeadline = Sanitizer::decodeCharReferences( $safeHeadline );
4363 $safeHeadline = self::normalizeSectionName( $safeHeadline );
4365 $fallbackHeadline = Sanitizer::escapeIdForAttribute( $safeHeadline, Sanitizer::ID_FALLBACK );
4366 $linkAnchor = Sanitizer::escapeIdForLink( $safeHeadline );
4367 $safeHeadline = Sanitizer::escapeIdForAttribute( $safeHeadline, Sanitizer::ID_PRIMARY );
4368 if ( $fallbackHeadline === $safeHeadline ) {
4369 # No reason to have both (in fact, we can't)
4370 $fallbackHeadline =
false;
4373 # HTML IDs must be case-insensitively unique for IE compatibility (T12721).
4374 # @todo FIXME: We may be changing them depending on the current locale.
4375 $arrayKey = strtolower( $safeHeadline );
4376 if ( $fallbackHeadline ===
false ) {
4377 $fallbackArrayKey =
false;
4379 $fallbackArrayKey = strtolower( $fallbackHeadline );
4382 # Create the anchor for linking from the TOC to the section
4383 $anchor = $safeHeadline;
4384 $fallbackAnchor = $fallbackHeadline;
4385 if ( isset( $refers[$arrayKey] ) ) {
4387 for ( $i = 2; isset( $refers[
"${arrayKey}_$i"] ); ++$i );
4389 $linkAnchor .=
"_$i";
4390 $refers[
"${arrayKey}_$i"] =
true;
4392 $refers[$arrayKey] =
true;
4394 if ( $fallbackHeadline !==
false && isset( $refers[$fallbackArrayKey] ) ) {
4396 for ( $i = 2; isset( $refers[
"${fallbackArrayKey}_$i"] ); ++$i );
4397 $fallbackAnchor .=
"_$i";
4398 $refers[
"${fallbackArrayKey}_$i"] =
true;
4400 $refers[$fallbackArrayKey] =
true;
4403 # Don't number the heading if it is the only one (looks silly)
4404 if (
count(
$matches[3] ) > 1 && $this->mOptions->getNumberHeadings() ) {
4405 # the two are different if the line contains a link
4406 $headline = Html::element(
4408 [
'class' =>
'mw-headline-number' ],
4410 ) .
' ' . $headline;
4413 if ( $enoughToc && ( !isset( $maxTocLevel ) || $toclevel < $maxTocLevel ) ) {
4415 $numbering, $toclevel, ( $isTemplate ?
false : $sectionIndex ) );
4418 # Add the section to the section tree
4419 # Find the DOM node for this header
4420 $noOffset = ( $isTemplate || $sectionIndex ===
false );
4421 while ( $node && !$noOffset ) {
4422 if ( $node->getName() ===
'h' ) {
4423 $bits = $node->splitHeading();
4424 if ( $bits[
'i'] == $sectionIndex ) {
4428 $byteOffset += mb_strlen( $this->mStripState->unstripBoth(
4430 $node = $node->getNextSibling();
4433 'toclevel' => $toclevel,
4436 'number' => $numbering,
4437 'index' => ( $isTemplate ?
'T-' :
'' ) . $sectionIndex,
4438 'fromtitle' => $titleText,
4439 'byteoffset' => ( $noOffset ?
null : $byteOffset ),
4440 'anchor' => $anchor,
4443 # give headline the correct <h#> tag
4444 if ( $maybeShowEditLink && $sectionIndex !==
false ) {
4446 if ( $isTemplate ) {
4447 # Put a T flag in the section identifier, to indicate to extractSections()
4448 # that sections inside <includeonly> should be counted.
4449 $editsectionPage = $titleText;
4450 $editsectionSection =
"T-$sectionIndex";
4451 $editsectionContent =
null;
4453 $editsectionPage = $this->mTitle->getPrefixedText();
4454 $editsectionSection = $sectionIndex;
4455 $editsectionContent = $headlineHint;
4469 $editlink =
'<mw:editsection page="' . htmlspecialchars( $editsectionPage );
4470 $editlink .=
'" section="' . htmlspecialchars( $editsectionSection ) .
'"';
4471 if ( $editsectionContent !==
null ) {
4472 $editlink .=
'>' . $editsectionContent .
'</mw:editsection>';
4480 $matches[
'attrib'][$headlineCount], $anchor, $headline,
4481 $editlink, $fallbackAnchor );
4486 $this->setOutputType( $oldType );
4488 # Never ever show TOC if no headers
4489 if ( $numVisible < 1 ) {
4494 if ( $prevtoclevel > 0 && $prevtoclevel < $maxTocLevel ) {
4498 $this->mOutput->setTOCHTML( $toc );
4499 $toc = self::TOC_START . $toc . self::TOC_END;
4503 $this->mOutput->setSections( $tocraw );
4506 # split up and insert constructed headlines
4507 $blocks = preg_split(
'/<H[1-6].*?>[\s\S]*?<\/H[1-6]>/i', $text );
4512 foreach ( $blocks
as $block ) {
4514 if ( empty( $head[$i - 1] ) ) {
4515 $sections[$i] = $block;
4517 $sections[$i] = $head[$i - 1] . $block;
4530 Hooks::run(
'ParserSectionCreate', [ $this, $i, &$sections[$i], $maybeShowEditLink ] );
4535 if ( $enoughToc && $isMain && !$this->mForceTocPosition ) {
4538 $sections[0] .= $toc .
"\n";
4541 $full .= implode(
'', $sections );
4543 if ( $this->mForceTocPosition ) {
4544 return str_replace(
'<!--MWTOC\'"-->', $toc, $full );
4564 if ( $clearState ) {
4565 $magicScopeVariable = $this->lock();
4568 $this->setUser(
$user );
4571 $text = str_replace(
"\000",
'', $text );
4578 if (
$options->getPreSaveTransform() ) {
4579 $text = $this->pstPass2( $text,
$user );
4581 $text = $this->mStripState->unstripBoth( $text );
4583 $this->setUser(
null ); # Reset
4596 private function pstPass2( $text,
$user ) {
4597 # Note: This is the timestamp saved as hardcoded wikitext to the database, we use
4598 # $this->contLang here in order to give everyone the same signature and use the default one
4599 # rather than the one selected in each user's preferences. (see also T14815)
4600 $ts = $this->mOptions->getTimestamp();
4602 $ts = $timestamp->format(
'YmdHis' );
4603 $tzMsg = $timestamp->getTimezoneMessage()->inContentLanguage()->text();
4605 $d = $this->contLang->timeanddate( $ts,
false,
false ) .
" ($tzMsg)";
4607 # Variable replacement
4608 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
4609 $text = $this->replaceVariables( $text );
4611 # This works almost by chance, as the replaceVariables are done before the getUserSig(),
4612 # which may corrupt this parser instance via its wfMessage()->text() call-
4615 if ( strpos( $text,
'~~~' ) !==
false ) {
4616 $sigText = $this->getUserSig(
$user );
4617 $text = strtr( $text, [
4619 '~~~~' =>
"$sigText $d",
4622 # The main two signature forms used above are time-sensitive
4623 $this->mOutput->setFlag(
'user-signature' );
4626 # Context links ("pipe tricks"): [[|name]] and [[name (context)|]]
4628 $nc =
'[ _0-9A-Za-z\x80-\xff-]'; # Namespaces can
use non-ascii!
4631 $p1 =
"/\[\[(:?$nc+:|:|)($tc+?)( ?\\($tc+\\))\\|]]/";
4633 $p4 =
"/\[\[(:?$nc+:|:|)($tc+?)( ?($tc+))\\|]]/";
4635 $p3 =
"/\[\[(:?$nc+:|:|)($tc+?)( ?\\($tc+\\)|)((?:, |,)$tc+|)\\|]]/";
4637 $p2 =
"/\[\[\\|($tc+)]]/";
4639 # try $p1 first, to turn "[[A, B (C)|]]" into "[[A, B (C)|A, B]]"
4640 $text = preg_replace( $p1,
'[[\\1\\2\\3|\\2]]', $text );
4641 $text = preg_replace( $p4,
'[[\\1\\2\\3|\\2]]', $text );
4642 $text = preg_replace( $p3,
'[[\\1\\2\\3\\4|\\2]]', $text );
4644 $t = $this->mTitle->getText();
4646 if ( preg_match(
"/^($nc+:|)$tc+?( \\($tc+\\))$/",
$t, $m ) ) {
4647 $text = preg_replace( $p2,
"[[$m[1]\\1$m[2]|\\1]]", $text );
4648 } elseif ( preg_match(
"/^($nc+:|)$tc+?(, $tc+|)$/",
$t, $m ) &&
"$m[1]$m[2]" !=
'' ) {
4649 $text = preg_replace( $p2,
"[[$m[1]\\1$m[2]|\\1]]", $text );
4651 # if there's no context, don't bother duplicating the title
4652 $text = preg_replace( $p2,
'[[\\1]]', $text );
4672 public function getUserSig( &
$user, $nickname =
false, $fancySig =
null ) {
4675 # If not given, retrieve from the user object.
4676 if ( $nickname ===
false ) {
4677 $nickname =
$user->getOption(
'nickname' );
4680 if ( is_null( $fancySig ) ) {
4681 $fancySig =
$user->getBoolOption(
'fancysig' );
4684 $nickname = $nickname ==
null ?
$username : $nickname;
4686 if ( mb_strlen( $nickname ) > $this->siteConfig->get(
'MaxSigChars' ) ) {
4688 wfDebug( __METHOD__ .
": $username has overlong signature.\n" );
4689 } elseif ( $fancySig !==
false ) {
4690 # Sig. might contain markup; validate this
4691 if ( $this->validateSig( $nickname ) !==
false ) {
4692 # Validated; clean up (if needed) and return it
4693 return $this->cleanSig( $nickname,
true );
4695 # Failed to validate; fall back to the default
4697 wfDebug( __METHOD__ .
": $username has bad XML tags in signature.\n" );
4701 # Make sure nickname doesnt get a sig in a sig
4702 $nickname = self::cleanSigInSig( $nickname );
4704 # If we're still here, make it a link to the user page
4707 $msgName =
$user->isAnon() ?
'signature-anon' :
'signature';
4709 return wfMessage( $msgName, $userText, $nickText )->inContentLanguage()
4710 ->title( $this->getTitle() )->text();
4719 public function validateSig( $text ) {
4733 public function cleanSig( $text, $parsing =
false ) {
4736 $magicScopeVariable = $this->lock();
4740 # Option to disable this feature
4741 if ( !$this->mOptions->getCleanSignatures() ) {
4745 # @todo FIXME: Regex doesn't respect extension tags or nowiki
4746 # => Move this logic to braceSubstitution()
4747 $substWord = $this->magicWordFactory->get(
'subst' );
4748 $substRegex =
'/\{\{(?!(?:' . $substWord->getBaseRegex() .
'))/x' . $substWord->getRegexCase();
4749 $substText =
'{{' . $substWord->getSynonym( 0 );
4751 $text = preg_replace( $substRegex, $substText, $text );
4752 $text = self::cleanSigInSig( $text );
4753 $dom = $this->preprocessToDom( $text );
4754 $frame = $this->getPreprocessor()->newFrame();
4755 $text = $frame->expand( $dom );
4758 $text = $this->mStripState->unstripBoth( $text );
4770 public static function cleanSigInSig( $text ) {
4771 $text = preg_replace(
'/~{3,5}/',
'', $text );
4785 $outputType, $clearState =
true
4797 $outputType, $clearState =
true
4799 $this->setTitle(
$title );
4801 $this->setOutputType( $outputType );
4802 if ( $clearState ) {
4803 $this->clearState();
4816 static $executing =
false;
4818 # Guard against infinite recursion
4859 public function setHook( $tag, callable $callback ) {
4860 $tag = strtolower( $tag );
4861 if ( preg_match(
'/[<>\r\n]/', $tag, $m ) ) {
4862 throw new MWException(
"Invalid character {$m[0]} in setHook('$tag', ...) call" );
4864 $oldVal = $this->mTagHooks[$tag] ??
null;
4865 $this->mTagHooks[$tag] = $callback;
4866 if ( !in_array( $tag, $this->mStripList ) ) {
4867 $this->mStripList[] = $tag;
4890 public function setTransparentTagHook( $tag, callable $callback ) {
4891 $tag = strtolower( $tag );
4892 if ( preg_match(
'/[<>\r\n]/', $tag, $m ) ) {
4893 throw new MWException(
"Invalid character {$m[0]} in setTransparentHook('$tag', ...) call" );
4895 $oldVal = $this->mTransparentTagHooks[$tag] ??
null;
4896 $this->mTransparentTagHooks[$tag] = $callback;
4904 public function clearTagHooks() {
4905 $this->mTagHooks = [];
4906 $this->mFunctionTagHooks = [];
4907 $this->mStripList = $this->mDefaultStripList;
4953 public function setFunctionHook( $id, callable $callback, $flags = 0 ) {
4954 $oldVal = isset( $this->mFunctionHooks[$id] ) ? $this->mFunctionHooks[$id][0] :
null;
4955 $this->mFunctionHooks[$id] = [ $callback, $flags ];
4957 # Add to function cache
4958 $mw = $this->magicWordFactory->get( $id );
4960 throw new MWException( __METHOD__ .
'() expecting a magic word identifier.' );
4963 $synonyms = $mw->getSynonyms();
4964 $sensitive = intval( $mw->isCaseSensitive() );
4966 foreach ( $synonyms
as $syn ) {
4968 if ( !$sensitive ) {
4969 $syn = $this->contLang->lc( $syn );
4975 # Remove trailing colon
4976 if ( substr( $syn, -1, 1 ) ===
':' ) {
4977 $syn = substr( $syn, 0, -1 );
4979 $this->mFunctionSynonyms[$sensitive][$syn] = $id;
4989 public function getFunctionHooks() {
4990 $this->firstCallInit();
4991 return array_keys( $this->mFunctionHooks );
5004 public function setFunctionTagHook( $tag, callable $callback, $flags ) {
5005 $tag = strtolower( $tag );
5006 if ( preg_match(
'/[<>\r\n]/', $tag, $m ) ) {
5007 throw new MWException(
"Invalid character {$m[0]} in setFunctionTagHook('$tag', ...) call" );
5009 $old = $this->mFunctionTagHooks[$tag] ??
null;
5010 $this->mFunctionTagHooks[$tag] = [ $callback, $flags ];
5012 if ( !in_array( $tag, $this->mStripList ) ) {
5013 $this->mStripList[] = $tag;
5026 public function replaceLinkHolders( &$text,
$options = 0 ) {
5027 $this->mLinkHolders->replace( $text );
5037 public function replaceLinkHoldersText( $text ) {
5038 return $this->mLinkHolders->replaceText( $text );
5054 public function renderImageGallery( $text,
$params ) {
5056 if ( isset(
$params[
'mode'] ) ) {
5062 }
catch ( Exception
$e ) {
5067 $ig->setContextTitle( $this->mTitle );
5068 $ig->setShowBytes(
false );
5069 $ig->setShowDimensions(
false );
5070 $ig->setShowFilename(
false );
5071 $ig->setParser( $this );
5072 $ig->setHideBadImages();
5073 $ig->setAttributes( Sanitizer::validateTagAttributes(
$params,
'ul' ) );
5075 if ( isset(
$params[
'showfilename'] ) ) {
5076 $ig->setShowFilename(
true );
5078 $ig->setShowFilename(
false );
5080 if ( isset(
$params[
'caption'] ) ) {
5084 $caption = $this->recursiveTagParse(
$params[
'caption'] );
5085 $ig->setCaptionHtml( $caption );
5087 if ( isset(
$params[
'perrow'] ) ) {
5088 $ig->setPerRow(
$params[
'perrow'] );
5090 if ( isset(
$params[
'widths'] ) ) {
5091 $ig->setWidths(
$params[
'widths'] );
5093 if ( isset(
$params[
'heights'] ) ) {
5094 $ig->setHeights(
$params[
'heights'] );
5096 $ig->setAdditionalOptions(
$params );
5104 # match lines like these:
5105 # Image:someimage.jpg|This is some image
5113 if ( strpos(
$matches[0],
'%' ) !==
false ) {
5117 if ( is_null(
$title ) ) {
5118 # Bogus title. Ignore these so we don't bomb out later.
5122 # We need to get what handler the file uses, to figure out parameters.
5123 # Note, a hook can overide the file name, and chose an entirely different
5124 # file (which potentially could be of a different type and have different handler).
5129 # Don't register it now, as TraditionalImageGallery does that later.
5134 'img_alt' =>
'gallery-internal-alt',
5135 'img_link' =>
'gallery-internal-link',
5138 $paramMap +=
$handler->getParamMap();
5141 unset( $paramMap[
'img_width'] );
5144 $mwArray = $this->magicWordFactory->newArray( array_keys( $paramMap ) );
5149 $handlerOptions = [];
5163 foreach ( $parameterMatches
as $parameterMatch ) {
5164 list( $magicName, $match ) = $mwArray->matchVariableStartToEnd( $parameterMatch );
5166 $paramName = $paramMap[$magicName];
5168 switch ( $paramName ) {
5169 case 'gallery-internal-alt':
5170 $alt = $this->stripAltText( $match,
false );
5172 case 'gallery-internal-link':
5173 $linkValue = $this->stripAltText( $match,
false );
5174 if ( preg_match(
'/^-{R|(.*)}-$/', $linkValue ) ) {
5177 $linkValue = substr( $linkValue, 4, -2 );
5179 list(
$type, $target ) = $this->parseLinkParameter( $linkValue );
5180 if (
$type ===
'link-url' ) {
5182 $this->mOutput->addExternalLink( $target );
5183 } elseif (
$type ===
'link-title' ) {
5184 $link = $target->getLinkURL();
5185 $this->mOutput->addLink( $target );
5190 if (
$handler->validateParam( $paramName, $match ) ) {
5191 $handlerOptions[$paramName] = $match;
5194 wfDebug(
"$parameterMatch failed parameter validation\n" );
5195 $label = $parameterMatch;
5201 $label = $parameterMatch;
5206 $ig->add(
$title, $label, $alt,
$link, $handlerOptions );
5208 $html = $ig->toHTML();
5217 public function getImageParams(
$handler ) {
5219 $handlerClass = get_class(
$handler );
5223 if ( !isset( $this->mImageParams[$handlerClass] ) ) {
5224 # Initialise static lists
5225 static $internalParamNames = [
5226 'horizAlign' => [
'left',
'right',
'center',
'none' ],
5227 'vertAlign' => [
'baseline',
'sub',
'super',
'top',
'text-top',
'middle',
5228 'bottom',
'text-bottom' ],
5229 'frame' => [
'thumbnail',
'manualthumb',
'framed',
'frameless',
5230 'upright',
'border',
'link',
'alt',
'class' ],
5232 static $internalParamMap;
5233 if ( !$internalParamMap ) {
5234 $internalParamMap = [];
5235 foreach ( $internalParamNames
as $type => $names ) {
5242 $magicName = str_replace(
'-',
'_',
"img_$name" );
5243 $internalParamMap[$magicName] = [
$type,
$name ];
5248 # Add handler params
5249 $paramMap = $internalParamMap;
5251 $handlerParamMap =
$handler->getParamMap();
5252 foreach ( $handlerParamMap
as $magic => $paramName ) {
5253 $paramMap[$magic] = [
'handler', $paramName ];
5256 $this->mImageParams[$handlerClass] = $paramMap;
5257 $this->mImageParamsMagicArray[$handlerClass] =
5258 $this->magicWordFactory->newArray( array_keys( $paramMap ) );
5260 return [ $this->mImageParams[$handlerClass], $this->mImageParamsMagicArray[$handlerClass] ];
5271 public function makeImage(
$title,
$options, $holders =
false ) {
5272 # Check if the options text is of the form "options|alt text"
5274 # * thumbnail make a thumbnail with enlarge-icon and caption, alignment depends on lang
5275 # * left no resizing, just left align. label is used for alt= only
5276 # * right same, but right aligned
5277 # * none same, but not aligned
5278 # * ___px scale to ___ pixels width, no aligning. e.g. use in taxobox
5279 # * center center the image
5280 # * frame Keep original image size, no magnify-button.
5281 # * framed Same as "frame"
5282 # * frameless like 'thumb' but without a frame. Keeps user preferences for width
5283 # * upright reduce width for upright images, rounded to full __0 px
5284 # * border draw a 1px border around the image
5285 # * alt Text for HTML alt attribute (defaults to empty)
5286 # * class Set a class for img node
5287 # * link Set the target of the image link. Can be external, interwiki, or local
5288 # vertical-align values (no % or length right now):
5298 # Protect LanguageConverter markup when splitting into parts
5303 # Give extensions a chance to select the file revision for us
5308 # Fetch and register the file (file title may be different via hooks)
5314 list( $paramMap, $mwArray ) = $this->getImageParams(
$handler );
5317 $this->addTrackingCategory(
'broken-file-category' );
5320 # Process the input parameters
5322 $params = [
'frame' => [],
'handler' => [],
5323 'horizAlign' => [],
'vertAlign' => [] ];
5324 $seenformat =
false;
5325 foreach ( $parts
as $part ) {
5326 $part = trim( $part );
5327 list( $magicName,
$value ) = $mwArray->matchVariableStartToEnd( $part );
5329 if ( isset( $paramMap[$magicName] ) ) {
5330 list(
$type, $paramName ) = $paramMap[$magicName];
5332 # Special case; width and height come in one variable together
5333 if (
$type ===
'handler' && $paramName ===
'width' ) {
5334 $parsedWidthParam = self::parseWidthParam(
$value );
5335 if ( isset( $parsedWidthParam[
'width'] ) ) {
5336 $width = $parsedWidthParam[
'width'];
5337 if (
$handler->validateParam(
'width', $width ) ) {
5342 if ( isset( $parsedWidthParam[
'height'] ) ) {
5343 $height = $parsedWidthParam[
'height'];
5344 if (
$handler->validateParam(
'height', $height ) ) {
5349 # else no validation -- T15436
5351 if (
$type ===
'handler' ) {
5352 # Validate handler parameter
5355 # Validate internal parameters
5356 switch ( $paramName ) {
5360 # @todo FIXME: Possibly check validity here for
5361 # manualthumb? downstream behavior seems odd with
5362 # missing manual thumbs.
5368 $this->parseLinkParameter(
5369 $this->stripAltText(
$value, $holders )
5373 if ( $paramName ===
'no-link' ) {
5376 if ( ( $paramName ===
'link-url' ) && $this->mOptions->getExternalLinkTarget() ) {
5377 $params[
$type][
'link-target'] = $this->mOptions->getExternalLinkTarget();
5385 $validated = !$seenformat;
5389 # Most other things appear to be empty or numeric...
5390 $validated = (
$value ===
false || is_numeric( trim(
$value ) ) );
5399 if ( !$validated ) {
5404 # Process alignment parameters
5405 if (
$params[
'horizAlign'] ) {
5412 $params[
'frame'][
'caption'] = $caption;
5414 # Will the image be presented in a frame, with the caption below?
5415 $imageIsFramed = isset(
$params[
'frame'][
'frame'] )
5416 || isset(
$params[
'frame'][
'framed'] )
5417 || isset(
$params[
'frame'][
'thumbnail'] )
5418 || isset(
$params[
'frame'][
'manualthumb'] );
5420 # In the old days, [[Image:Foo|text...]] would set alt text. Later it
5421 # came to also set the caption, ordinary text after the image -- which
5422 # makes no sense, because that just repeats the text multiple times in
5423 # screen readers. It *also* came to set the title attribute.
5424 # Now that we have an alt attribute, we should not set the alt text to
5425 # equal the caption: that's worse than useless, it just repeats the
5426 # text. This is the framed/thumbnail case. If there's no caption, we
5427 # use the unnamed parameter for alt text as well, just for the time be-
5428 # ing, if the unnamed param is set and the alt param is not.
5429 # For the future, we need to figure out if we want to tweak this more,
5430 # e.g., introducing a title= parameter for the title; ignoring the un-
5431 # named parameter entirely for images without a caption; adding an ex-
5432 # plicit caption= parameter and preserving the old magic unnamed para-
5434 if ( $imageIsFramed ) { # Framed image
5435 if ( $caption ===
'' && !isset(
$params[
'frame'][
'alt'] ) ) {
5436 # No caption or alt text, add the filename as the alt text so
5437 # that screen readers at least get some description of the image
5440 # Do not set $params['frame']['title'] because tooltips don't make sense
5442 }
else { # Inline image
5443 if ( !isset(
$params[
'frame'][
'alt'] ) ) {
5444 # No alt text, use the "caption" for the alt text
5445 if ( $caption !==
'' ) {
5446 $params[
'frame'][
'alt'] = $this->stripAltText( $caption, $holders );
5448 # No caption, fall back to using the filename for the
5453 # Use the "caption" for the tooltip text
5454 $params[
'frame'][
'title'] = $this->stripAltText( $caption, $holders );
5456 $params[
'handler'][
'targetlang'] = $this->getTargetLanguage()->getCode();
5460 # Linker does the rest
5463 $time, $descQuery, $this->mOptions->getThumbSize() );
5465 # Give the handler a chance to modify the parser object
5491 public function parseLinkParameter(
$value ) {
5492 $chars = self::EXT_LINK_URL_CLASS;
5493 $addr = self::EXT_LINK_ADDR;
5494 $prots = $this->mUrlProtocols;
5499 } elseif ( preg_match(
"/^((?i)$prots)/",
$value ) ) {
5500 if ( preg_match(
"/^((?i)$prots)$addr$chars*$/u",
$value, $m ) ) {
5501 $this->mOutput->addExternalLink(
$value );
5508 $this->mOutput->addLink( $linkTitle );
5509 $type =
'link-title';
5510 $target = $linkTitle;
5513 return [
$type, $target ];
5521 protected function stripAltText( $caption, $holders ) {
5522 # Strip bad stuff out of the title (tooltip). We can't just use
5523 # replaceLinkHoldersText() here, because if this function is called
5524 # from replaceInternalLinks2(), mLinkHolders won't be up-to-date.
5526 $tooltip = $holders->replaceText( $caption );
5528 $tooltip = $this->replaceLinkHoldersText( $caption );
5531 # make sure there are no placeholders in thumbnail attributes
5532 # that are later expanded to html- so expand them now and
5534 $tooltip = $this->mStripState->unstripBoth( $tooltip );
5535 # Compatibility hack! In HTML certain entity references not terminated
5536 # by a semicolon are decoded (but not if we're in an attribute; that's
5537 # how link URLs get away without properly escaping & in queries).
5538 # But wikitext has always required semicolon-termination of entities,
5539 # so encode & where needed to avoid decode of semicolon-less entities.
5541 # https://www.w3.org/TR/html5/syntax.html#named-character-references
5542 # T210437 discusses moving this workaround to Sanitizer::stripAllTags.
5543 $tooltip = preg_replace(
"/
5544 & # 1. entity prefix
5545 (?= # 2. followed by:
5546 (?: # a. one of the legacy semicolon-less named entities
5547 A(?:Elig|MP|acute|circ|grave|ring|tilde|uml)|
5548 C(?:OPY|cedil)|E(?:TH|acute|circ|grave|uml)|
5549 GT|I(?:acute|circ|grave|uml)|LT|Ntilde|
5550 O(?:acute|circ|grave|slash|tilde|uml)|QUOT|REG|THORN|
5551 U(?:acute|circ|grave|uml)|Yacute|
5552 a(?:acute|c(?:irc|ute)|elig|grave|mp|ring|tilde|uml)|brvbar|
5553 c(?:cedil|edil|urren)|cent(?!erdot;)|copy(?!sr;)|deg|
5554 divide(?!ontimes;)|e(?:acute|circ|grave|th|uml)|
5555 frac(?:1(?:2|4)|34)|
5556 gt(?!c(?:c|ir)|dot|lPar|quest|r(?:a(?:pprox|rr)|dot|eq(?:less|qless)|less|sim);)|
5557 i(?:acute|circ|excl|grave|quest|uml)|laquo|
5558 lt(?!c(?:c|ir)|dot|hree|imes|larr|quest|r(?:Par|i(?:e|f|));)|
5559 m(?:acr|i(?:cro|ddot))|n(?:bsp|tilde)|
5560 not(?!in(?:E|dot|v(?:a|b|c)|)|ni(?:v(?:a|b|c)|);)|
5561 o(?:acute|circ|grave|rd(?:f|m)|slash|tilde|uml)|
5562 p(?:lusmn|ound)|para(?!llel;)|quot|r(?:aquo|eg)|
5563 s(?:ect|hy|up(?:1|2|3)|zlig)|thorn|times(?!b(?:ar|)|d;)|
5564 u(?:acute|circ|grave|ml|uml)|y(?:acute|en|uml)
5566 (?:[^;]|$)) # b. and not followed by a semicolon
5567 # S = study, for efficiency
5568 /Sx",
'&', $tooltip );
5569 $tooltip = Sanitizer::stripAllTags( $tooltip );
5579 public function disableCache() {
5580 wfDebug(
"Parser output marked as uncacheable.\n" );
5581 if ( !$this->mOutput ) {
5583 " can only be called when actually parsing something" );
5585 $this->mOutput->updateCacheExpiry( 0 );
5596 public function attributeStripCallback( &$text, $frame =
false ) {
5597 $text = $this->replaceVariables( $text, $frame );
5598 $text = $this->mStripState->unstripBoth( $text );
5607 public function getTags() {
5608 $this->firstCallInit();
5610 array_keys( $this->mTransparentTagHooks ),
5611 array_keys( $this->mTagHooks ),
5612 array_keys( $this->mFunctionTagHooks )
5620 public function getFunctionSynonyms() {
5621 $this->firstCallInit();
5622 return $this->mFunctionSynonyms;
5629 public function getUrlProtocols() {
5630 return $this->mUrlProtocols;
5643 public function replaceTransparentTags( $text ) {
5645 $elements = array_keys( $this->mTransparentTagHooks );
5646 $text = self::extractTagsAndParams( $elements, $text,
$matches );
5651 $tagName = strtolower( $element );
5652 if ( isset( $this->mTransparentTagHooks[$tagName] ) ) {
5653 $output = call_user_func_array(
5654 $this->mTransparentTagHooks[$tagName],
5660 $replacements[$marker] =
$output;
5662 return strtr( $text, $replacements );
5694 private function extractSections( $text, $sectionId, $mode, $newText =
'' ) {
5697 $magicScopeVariable = $this->lock();
5700 $frame = $this->getPreprocessor()->newFrame();
5702 # Process section extraction flags
5704 $sectionParts = explode(
'-', $sectionId );
5705 $sectionIndex = array_pop( $sectionParts );
5706 foreach ( $sectionParts
as $part ) {
5707 if ( $part ===
'T' ) {
5708 $flags |= self::PTD_FOR_INCLUSION;
5712 # Check for empty input
5713 if ( strval( $text ) ===
'' ) {
5714 # Only sections 0 and T-0 exist in an empty document
5715 if ( $sectionIndex == 0 ) {
5716 if ( $mode ===
'get' ) {
5722 if ( $mode ===
'get' ) {
5730 # Preprocess the text
5731 $root = $this->preprocessToDom( $text, $flags );
5733 # <h> nodes indicate section breaks
5734 # They can only occur at the top level, so we can find them by iterating the root's children
5735 $node = $root->getFirstChild();
5737 # Find the target section
5738 if ( $sectionIndex == 0 ) {
5739 # Section zero doesn't nest, level=big
5740 $targetLevel = 1000;
5743 if ( $node->getName() ===
'h' ) {
5744 $bits = $node->splitHeading();
5745 if ( $bits[
'i'] == $sectionIndex ) {
5746 $targetLevel = $bits[
'level'];
5750 if ( $mode ===
'replace' ) {
5753 $node = $node->getNextSibling();
5759 if ( $mode ===
'get' ) {
5766 # Find the end of the section, including nested sections
5768 if ( $node->getName() ===
'h' ) {
5769 $bits = $node->splitHeading();
5770 $curLevel = $bits[
'level'];
5771 if ( $bits[
'i'] != $sectionIndex && $curLevel <= $targetLevel ) {
5775 if ( $mode ===
'get' ) {
5778 $node = $node->getNextSibling();
5781 # Write out the remainder (in replace mode only)
5782 if ( $mode ===
'replace' ) {
5783 # Output the replacement text
5784 # Add two newlines on -- trailing whitespace in $newText is conventionally
5785 # stripped by the editor, so we need both newlines to restore the paragraph gap
5786 # Only add trailing whitespace if there is newText
5787 if ( $newText !=
"" ) {
5788 $outText .= $newText .
"\n\n";
5793 $node = $node->getNextSibling();
5797 if ( is_string( $outText ) ) {
5798 # Re-insert stripped tags
5799 $outText = rtrim( $this->mStripState->unstripBoth( $outText ) );
5819 public function getSection( $text, $sectionId, $defaultText =
'' ) {
5820 return $this->extractSections( $text, $sectionId,
'get', $defaultText );
5835 public function replaceSection( $oldText, $sectionId, $newText ) {
5836 return $this->extractSections( $oldText, $sectionId,
'replace', $newText );
5844 public function getRevisionId() {
5845 return $this->mRevisionId;
5854 public function getRevisionObject() {
5855 if ( !is_null( $this->mRevisionObject ) ) {
5856 return $this->mRevisionObject;
5866 $rev = call_user_func(
5867 $this->mOptions->getCurrentRevisionCallback(), $this->getTitle(), $this
5870 if ( $this->mRevisionId ===
null &&
$rev &&
$rev->getId() ) {
5882 if ( $this->mRevisionId &&
$rev &&
$rev->getId() != $this->mRevisionId ) {
5886 $this->mRevisionObject =
$rev;
5888 return $this->mRevisionObject;
5896 public function getRevisionTimestamp() {
5897 if ( is_null( $this->mRevisionTimestamp ) ) {
5898 $revObject = $this->getRevisionObject();
5899 $timestamp = $revObject ? $revObject->getTimestamp() :
wfTimestampNow();
5901 # The cryptic '' timezone parameter tells to use the site-default
5902 # timezone offset instead of the user settings.
5903 # Since this value will be saved into the parser cache, served
5904 # to other users, and potentially even used inside links and such,
5905 # it needs to be consistent for all visitors.
5906 $this->mRevisionTimestamp = $this->contLang->userAdjust( $timestamp,
'' );
5908 return $this->mRevisionTimestamp;
5916 public function getRevisionUser() {
5917 if ( is_null( $this->mRevisionUser ) ) {
5918 $revObject = $this->getRevisionObject();
5920 # if this template is subst: the revision id will be blank,
5921 # so just use the current user's name
5923 $this->mRevisionUser = $revObject->getUserText();
5924 } elseif ( $this->ot[
'wiki'] || $this->mOptions->getIsPreview() ) {
5925 $this->mRevisionUser = $this->getUser()->getName();
5928 return $this->mRevisionUser;
5936 public function getRevisionSize() {
5937 if ( is_null( $this->mRevisionSize ) ) {
5938 $revObject = $this->getRevisionObject();
5940 # if this variable is subst: the revision id will be blank,
5941 # so just use the parser input size, because the own substituation
5942 # will change the size.
5944 $this->mRevisionSize = $revObject->getSize();
5946 $this->mRevisionSize = $this->mInputSize;
5949 return $this->mRevisionSize;
5957 public function setDefaultSort(
$sort ) {
5958 $this->mDefaultSort =
$sort;
5959 $this->mOutput->setProperty(
'defaultsort',
$sort );
5972 public function getDefaultSort() {
5973 if ( $this->mDefaultSort !==
false ) {
5974 return $this->mDefaultSort;
5986 public function getCustomDefaultSort() {
5987 return $this->mDefaultSort;
5990 private static function getSectionNameFromStrippedText( $text ) {
5991 $text = Sanitizer::normalizeSectionNameWhitespace( $text );
5992 $text = Sanitizer::decodeCharReferences( $text );
5993 $text = self::normalizeSectionName( $text );
5997 private static function makeAnchor( $sectionName ) {
5998 return '#' . Sanitizer::escapeIdForLink( $sectionName );
6001 private function makeLegacyAnchor( $sectionName ) {
6002 $fragmentMode = $this->siteConfig->get(
'FragmentMode' );
6003 if ( isset( $fragmentMode[1] ) && $fragmentMode[1] ===
'legacy' ) {
6005 $id = Sanitizer::escapeIdForAttribute( $sectionName, Sanitizer::ID_FALLBACK );
6007 $id = Sanitizer::escapeIdForLink( $sectionName );
6021 public function guessSectionNameFromWikiText( $text ) {
6022 # Strip out wikitext links(they break the anchor)
6023 $text = $this->stripSectionName( $text );
6024 $sectionName = self::getSectionNameFromStrippedText( $text );
6025 return self::makeAnchor( $sectionName );
6037 public function guessLegacySectionNameFromWikiText( $text ) {
6038 # Strip out wikitext links(they break the anchor)
6039 $text = $this->stripSectionName( $text );
6040 $sectionName = self::getSectionNameFromStrippedText( $text );
6041 return $this->makeLegacyAnchor( $sectionName );
6049 public static function guessSectionNameFromStrippedText( $text ) {
6050 $sectionName = self::getSectionNameFromStrippedText( $text );
6051 return self::makeAnchor( $sectionName );
6060 private static function normalizeSectionName( $text ) {
6061 # T90902: ensure the same normalization is applied for IDs as to links
6062 $titleParser = MediaWikiServices::getInstance()->getTitleParser();
6065 $parts = $titleParser->splitTitleString(
"#$text" );
6069 return $parts[
'fragment'];
6086 public function stripSectionName( $text ) {
6087 # Strip internal link markup
6088 $text = preg_replace(
'/\[\[:?([^[|]+)\|([^[]+)\]\]/',
'$2', $text );
6089 $text = preg_replace(
'/\[\[:?([^[]+)\|?\]\]/',
'$1', $text );
6091 # Strip external link markup
6092 # @todo FIXME: Not tolerant to blank link text
6093 # I.E. [https://www.mediawiki.org] will render as [1] or something depending
6094 # on how many empty links there are on the page - need to figure that out.
6095 $text = preg_replace(
'/\[(?i:' . $this->mUrlProtocols .
')([^ ]+?) ([^[]+)\]/',
'$2', $text );
6097 # Parse wikitext quotes (italics & bold)
6098 $text = $this->doQuotes( $text );
6118 $magicScopeVariable = $this->lock();
6121 $text = $this->replaceVariables( $text );
6122 $text = $this->mStripState->unstripBoth( $text );
6123 $text = Sanitizer::removeHTMLtags( $text );
6163 public function markerSkipCallback(
$s, $callback ) {
6166 while ( $i < strlen(
$s ) ) {
6167 $markerStart = strpos(
$s, self::MARKER_PREFIX, $i );
6168 if ( $markerStart ===
false ) {
6169 $out .= call_user_func( $callback, substr(
$s, $i ) );
6172 $out .= call_user_func( $callback, substr(
$s, $i, $markerStart - $i ) );
6173 $markerEnd = strpos(
$s, self::MARKER_SUFFIX, $markerStart );
6174 if ( $markerEnd ===
false ) {
6175 $out .= substr(
$s, $markerStart );
6178 $markerEnd += strlen( self::MARKER_SUFFIX );
6179 $out .= substr(
$s, $markerStart, $markerEnd - $markerStart );
6193 public function killMarkers( $text ) {
6194 return $this->mStripState->killMarkers( $text );
6214 public function serializeHalfParsedText( $text ) {
6218 'version' => self::HALF_PARSED_VERSION,
6219 'stripState' => $this->mStripState->getSubState( $text ),
6220 'linkHolders' => $this->mLinkHolders->getSubArray( $text )
6241 public function unserializeHalfParsedText(
$data ) {
6243 if ( !isset(
$data[
'version'] ) ||
$data[
'version'] != self::HALF_PARSED_VERSION ) {
6244 throw new MWException( __METHOD__ .
': invalid version' );
6247 # First, extract the strip state.
6248 $texts = [
$data[
'text'] ];
6249 $texts = $this->mStripState->merge(
$data[
'stripState'], $texts );
6251 # Now renumber links
6252 $texts = $this->mLinkHolders->mergeForeign(
$data[
'linkHolders'], $texts );
6254 # Should be good to go.
6268 public function isValidHalfParsedText(
$data ) {
6270 return isset(
$data[
'version'] ) &&
$data[
'version'] == self::HALF_PARSED_VERSION;
6282 public static function parseWidthParam(
$value, $parseHeight =
true ) {
6283 $parsedWidthParam = [];
6285 return $parsedWidthParam;
6288 # (T15500) In both cases (width/height and width only),
6289 # permit trailing "px" for backward compatibility.
6290 if ( $parseHeight && preg_match(
'/^([0-9]*)x([0-9]*)\s*(?:px)?\s*$/',
$value, $m ) ) {
6291 $width = intval( $m[1] );
6292 $height = intval( $m[2] );
6293 $parsedWidthParam[
'width'] = $width;
6294 $parsedWidthParam[
'height'] = $height;
6295 } elseif ( preg_match(
'/^[0-9]*\s*(?:px)?\s*$/',
$value ) ) {
6296 $width = intval(
$value );
6297 $parsedWidthParam[
'width'] = $width;
6299 return $parsedWidthParam;
6311 protected function lock() {
6312 if ( $this->mInParse ) {
6313 throw new MWException(
"Parser state cleared while parsing. "
6314 .
"Did you call Parser::parse recursively? Lock is held by: " . $this->mInParse );
6320 $this->mInParse =
$e->getTraceAsString();
6322 $recursiveCheck =
new ScopedCallback(
function () {
6323 $this->mInParse =
false;
6326 return $recursiveCheck;
6339 public static function stripOuterParagraph(
$html ) {
6341 if ( preg_match(
'/^<p>(.*)\n?<\/p>\n?$/sU',
$html, $m ) && strpos( $m[1],
'</p>' ) ===
false ) {
6358 public function getFreshParser() {
6359 if ( $this->mInParse ) {
6360 return $this->factory->create();
6372 public function enableOOUI() {
6373 OutputPage::setupOOUI();
6374 $this->mOutput->setEnableOOUI(
true );