3 * PHP parser that converts wiki markup to HTML.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\BadFileLookup
;
24 use MediaWiki\Config\ServiceOptions
;
25 use MediaWiki\Linker\LinkRenderer
;
26 use MediaWiki\Linker\LinkRendererFactory
;
27 use MediaWiki\Linker\LinkTarget
;
28 use MediaWiki\MediaWikiServices
;
29 use MediaWiki\Special\SpecialPageFactory
;
30 use Psr\Log\NullLogger
;
31 use Wikimedia\ScopedCallback
;
32 use Psr\Log\LoggerInterface
;
35 * @defgroup Parser Parser
39 * PHP Parser - Processes wiki markup (which uses a more user-friendly
40 * syntax, such as "[[link]]" for making links), and provides a one-way
41 * transformation of that wiki markup it into (X)HTML output / markup
42 * (which in turn the browser understands, and can display).
44 * There are seven main entry points into the Parser class:
47 * produces HTML output
48 * - Parser::preSaveTransform()
49 * produces altered wiki markup
50 * - Parser::preprocess()
51 * removes HTML comments and expands templates
52 * - Parser::cleanSig() and Parser::cleanSigInSig()
53 * cleans a signature before saving it to preferences
54 * - Parser::getSection()
55 * return the content of a section from an article for section editing
56 * - Parser::replaceSection()
57 * replaces a section by number inside an article
58 * - Parser::getPreloadText()
59 * removes <noinclude> sections and <includeonly> tags
61 * @warning $wgUser or $wgTitle or $wgRequest or $wgLang. Keep them away!
64 * $wgNamespacesWithSubpages
66 * @par Settings only within ParserOptions:
67 * $wgAllowExternalImages
68 * $wgAllowSpecialInclusion
76 * Update this version number when the ParserOutput format
77 * changes in an incompatible way, so the parser cache
78 * can automatically discard old data.
80 const VERSION
= '1.6.4';
83 * Update this version number when the output of serialiseHalfParsedText()
84 * changes in an incompatible way
86 const HALF_PARSED_VERSION
= 2;
88 # Flags for Parser::setFunctionHook
89 const SFH_NO_HASH
= 1;
90 const SFH_OBJECT_ARGS
= 2;
92 # Constants needed for external link processing
93 # Everything except bracket, space, or control characters
94 # \p{Zs} is unicode 'separator, space' category. It covers the space 0x20
95 # as well as U+3000 is IDEOGRAPHIC SPACE for T21052
96 # \x{FFFD} is the Unicode replacement character, which Preprocessor_DOM
97 # uses to replace invalid HTML characters.
98 const EXT_LINK_URL_CLASS
= '[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}]';
99 # Simplified expression to match an IPv4 or IPv6 address, or
100 # at least one character of a host name (embeds EXT_LINK_URL_CLASS)
101 const EXT_LINK_ADDR
= '(?:[0-9.]+|\\[(?i:[0-9a-f:.]+)\\]|[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}])';
102 # RegExp to make image URLs (embeds IPv6 part of EXT_LINK_ADDR)
103 // phpcs:ignore Generic.Files.LineLength
104 const EXT_IMAGE_REGEX
= '/^(http:\/\/|https:\/\/)((?:\\[(?i:[0-9a-f:.]+)\\])?[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}]+)
105 \\/([A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]+)\\.((?i)gif|png|jpg|jpeg)$/Sxu';
107 # Regular expression for a non-newline space
108 const SPACE_NOT_NL
= '(?:\t| |&\#0*160;|&\#[Xx]0*[Aa]0;|\p{Zs})';
110 # Flags for preprocessToDom
111 const PTD_FOR_INCLUSION
= 1;
113 # Allowed values for $this->mOutputType
114 # Parameter to startExternalParse().
115 const OT_HTML
= 1; # like parse()
116 const OT_WIKI
= 2; # like preSaveTransform()
117 const OT_PREPROCESS
= 3; # like preprocess()
119 const OT_PLAIN
= 4; # like extractSections() - portions of the original are returned unchanged.
122 * @var string Prefix and suffix for temporary replacement strings
123 * for the multipass parser.
125 * \x7f should never appear in input as it's disallowed in XML.
126 * Using it at the front also gives us a little extra robustness
127 * since it shouldn't match when butted up against identifier-like
130 * Must not consist of all title characters, or else it will change
131 * the behavior of <nowiki> in a link.
133 * Must have a character that needs escaping in attributes, otherwise
134 * someone could put a strip marker in an attribute, to get around
135 * escaping quote marks, and break out of the attribute. Thus we add
138 const MARKER_SUFFIX
= "-QINU`\"'\x7f";
139 const MARKER_PREFIX
= "\x7f'\"`UNIQ-";
141 # Markers used for wrapping the table of contents
142 const TOC_START
= '<mw:toc>';
143 const TOC_END
= '</mw:toc>';
145 /** @var int Assume that no output will later be saved this many seconds after parsing */
149 public $mTagHooks = [];
150 public $mTransparentTagHooks = [];
151 public $mFunctionHooks = [];
152 public $mFunctionSynonyms = [ 0 => [], 1 => [] ];
153 public $mFunctionTagHooks = [];
154 public $mStripList = [];
155 public $mDefaultStripList = [];
156 public $mVarCache = [];
157 public $mImageParams = [];
158 public $mImageParamsMagicArray = [];
159 public $mMarkerIndex = 0;
161 * @var bool Whether firstCallInit still needs to be called
163 public $mFirstCall = true;
165 # Initialised by initialiseVariables()
168 * @var MagicWordArray
173 * @var MagicWordArray
178 * @deprecated since 1.34, there should be no need to use this
183 # Initialised in constructor
184 public $mExtLinkBracketedRegex, $mUrlProtocols;
186 # Initialized in getPreprocessor()
187 /** @var Preprocessor */
188 public $mPreprocessor;
190 # Cleared with clearState():
202 public $mIncludeCount;
204 * @var LinkHolderArray
206 public $mLinkHolders;
209 public $mIncludeSizes, $mPPNodeCount, $mGeneratedPPNodeCount, $mHighestExpansionDepth;
210 public $mDefaultSort;
211 public $mTplRedirCache, $mHeadings, $mDoubleUnderscores;
212 public $mExpensiveFunctionCount; # number of expensive parser function calls
213 public $mShowToc, $mForceTocPosition;
215 public $mTplDomCache;
220 public $mUser; # User object; only used when doing pre-save transform
223 # These are variables reset at least once per parse regardless of $clearState
233 public $mTitle; # Title context, used for self-link rendering and similar things
234 public $mOutputType; # Output type, one of the OT_xxx constants
235 public $ot; # Shortcut alias, see setOutputType()
236 public $mRevisionObject; # The revision object of the specified revision ID
237 public $mRevisionId; # ID to display in {{REVISIONID}} tags
238 public $mRevisionTimestamp; # The timestamp of the specified revision ID
239 public $mRevisionUser; # User to display in {{REVISIONUSER}} tag
240 public $mRevisionSize; # Size to display in {{REVISIONSIZE}} variable
241 public $mRevIdForTs; # The revision ID which was used to fetch the timestamp
242 public $mInputSize = false; # For {{PAGESIZE}} on current page.
245 * @var array Array with the language name of each language link (i.e. the
246 * interwiki prefix) in the key, value arbitrary. Used to avoid sending
247 * duplicate language links to the ParserOutput.
249 public $mLangLinkLanguages;
252 * @var MapCacheLRU|null
255 * A cache of the current revisions of titles. Keys are $title->getPrefixedDbKey()
257 public $currentRevisionCache;
260 * @var bool|string Recursive call protection.
261 * This variable should be treated as if it were private.
263 public $mInParse = false;
265 /** @var SectionProfiler */
266 protected $mProfiler;
271 protected $mLinkRenderer;
273 /** @var MagicWordFactory */
274 private $magicWordFactory;
279 /** @var ParserFactory */
282 /** @var SpecialPageFactory */
283 private $specialPageFactory;
286 * This is called $svcOptions instead of $options like elsewhere to avoid confusion with
287 * $mOptions, which is public and widely used, and also with the local variable $options used
288 * for ParserOptions throughout this file.
290 * @var ServiceOptions
294 /** @var LinkRendererFactory */
295 private $linkRendererFactory;
297 /** @var NamespaceInfo */
300 /** @var LoggerInterface */
303 /** @var BadFileLookup */
304 private $badFileLookup;
307 * TODO Make this a const when HHVM support is dropped (T192166)
312 public static $constructorOptions = [
313 // See $wgParserConf documentation
316 // See documentation for the corresponding config options
318 'EnableScaryTranscluding',
319 'ExtraInterlanguageLinkPrefixes',
331 'TranscludeCacheExpiry',
335 * Constructing parsers directly is deprecated! Use a ParserFactory.
337 * @param ServiceOptions|null $svcOptions
338 * @param MagicWordFactory|null $magicWordFactory
339 * @param Language|null $contLang Content language
340 * @param ParserFactory|null $factory
341 * @param string|null $urlProtocols As returned from wfUrlProtocols()
342 * @param SpecialPageFactory|null $spFactory
343 * @param LinkRendererFactory|null $linkRendererFactory
344 * @param NamespaceInfo|null $nsInfo
345 * @param LoggerInterface|null $logger
346 * @param BadFileLookup|null $badFileLookup
348 public function __construct(
350 MagicWordFactory
$magicWordFactory = null,
351 Language
$contLang = null,
352 ParserFactory
$factory = null,
353 $urlProtocols = null,
354 SpecialPageFactory
$spFactory = null,
355 $linkRendererFactory = null,
358 BadFileLookup
$badFileLookup = null
360 if ( !$svcOptions ||
is_array( $svcOptions ) ) {
361 // Pre-1.34 calling convention is the first parameter is just ParserConf, the seventh is
362 // Config, and the eighth is LinkRendererFactory.
363 $this->mConf
= (array)$svcOptions;
364 if ( empty( $this->mConf
['class'] ) ) {
365 $this->mConf
['class'] = self
::class;
367 if ( empty( $this->mConf
['preprocessorClass'] ) ) {
368 $this->mConf
['preprocessorClass'] = self
::getDefaultPreprocessorClass();
370 $this->svcOptions
= new ServiceOptions( self
::$constructorOptions,
371 $this->mConf
, func_num_args() > 6
372 ?
func_get_arg( 6 ) : MediaWikiServices
::getInstance()->getMainConfig()
374 $linkRendererFactory = func_num_args() > 7 ?
func_get_arg( 7 ) : null;
375 $nsInfo = func_num_args() > 8 ?
func_get_arg( 8 ) : null;
377 // New calling convention
378 $svcOptions->assertRequiredOptions( self
::$constructorOptions );
379 // $this->mConf is public, so we'll keep those two options there as well for
380 // compatibility until it's removed
382 'class' => $svcOptions->get( 'class' ),
383 'preprocessorClass' => $svcOptions->get( 'preprocessorClass' ),
385 $this->svcOptions
= $svcOptions;
388 $this->mUrlProtocols
= $urlProtocols ??
wfUrlProtocols();
389 $this->mExtLinkBracketedRegex
= '/\[(((?i)' . $this->mUrlProtocols
. ')' .
390 self
::EXT_LINK_ADDR
.
391 self
::EXT_LINK_URL_CLASS
. '*)\p{Zs}*([^\]\\x00-\\x08\\x0a-\\x1F\\x{FFFD}]*?)\]/Su';
393 $this->magicWordFactory
= $magicWordFactory ??
394 MediaWikiServices
::getInstance()->getMagicWordFactory();
396 $this->contLang
= $contLang ?? MediaWikiServices
::getInstance()->getContentLanguage();
398 $this->factory
= $factory ?? MediaWikiServices
::getInstance()->getParserFactory();
399 $this->specialPageFactory
= $spFactory ??
400 MediaWikiServices
::getInstance()->getSpecialPageFactory();
401 $this->linkRendererFactory
= $linkRendererFactory ??
402 MediaWikiServices
::getInstance()->getLinkRendererFactory();
403 $this->nsInfo
= $nsInfo ?? MediaWikiServices
::getInstance()->getNamespaceInfo();
404 $this->logger
= $logger ?
: new NullLogger();
405 $this->badFileLookup
= $badFileLookup ??
406 MediaWikiServices
::getInstance()->getBadFileLookup();
410 * Reduce memory usage to reduce the impact of circular references
412 public function __destruct() {
413 if ( isset( $this->mLinkHolders
) ) {
414 // @phan-suppress-next-line PhanTypeObjectUnsetDeclaredProperty
415 unset( $this->mLinkHolders
);
417 // @phan-suppress-next-line PhanTypeSuspiciousNonTraversableForeach
418 foreach ( $this as $name => $value ) {
419 unset( $this->$name );
424 * Allow extensions to clean up when the parser is cloned
426 public function __clone() {
427 $this->mInParse
= false;
429 // T58226: When you create a reference "to" an object field, that
430 // makes the object field itself be a reference too (until the other
431 // reference goes out of scope). When cloning, any field that's a
432 // reference is copied as a reference in the new object. Both of these
433 // are defined PHP5 behaviors, as inconvenient as it is for us when old
434 // hooks from PHP4 days are passing fields by reference.
435 foreach ( [ 'mStripState', 'mVarCache' ] as $k ) {
436 // Make a non-reference copy of the field, then rebind the field to
437 // reference the new copy.
443 Hooks
::run( 'ParserCloned', [ $this ] );
447 * Which class should we use for the preprocessor if not otherwise specified?
450 * @deprecated since 1.34, removing configurability of preprocessor
453 public static function getDefaultPreprocessorClass() {
454 return Preprocessor_Hash
::class;
458 * Do various kinds of initialisation on the first call of the parser
460 public function firstCallInit() {
461 if ( !$this->mFirstCall
) {
464 $this->mFirstCall
= false;
466 CoreParserFunctions
::register( $this );
467 CoreTagHooks
::register( $this );
468 $this->initialiseVariables();
470 // Avoid PHP 7.1 warning from passing $this by reference
472 Hooks
::run( 'ParserFirstCallInit', [ &$parser ] );
480 public function clearState() {
481 $this->firstCallInit();
482 $this->resetOutput();
483 $this->mAutonumber
= 0;
484 $this->mIncludeCount
= [];
485 $this->mLinkHolders
= new LinkHolderArray( $this );
487 $this->mRevisionObject
= $this->mRevisionTimestamp
=
488 $this->mRevisionId
= $this->mRevisionUser
= $this->mRevisionSize
= null;
489 $this->mVarCache
= [];
491 $this->mLangLinkLanguages
= [];
492 $this->currentRevisionCache
= null;
494 $this->mStripState
= new StripState( $this );
496 # Clear these on every parse, T6549
497 $this->mTplRedirCache
= $this->mTplDomCache
= [];
499 $this->mShowToc
= true;
500 $this->mForceTocPosition
= false;
501 $this->mIncludeSizes
= [
505 $this->mPPNodeCount
= 0;
506 $this->mGeneratedPPNodeCount
= 0;
507 $this->mHighestExpansionDepth
= 0;
508 $this->mDefaultSort
= false;
509 $this->mHeadings
= [];
510 $this->mDoubleUnderscores
= [];
511 $this->mExpensiveFunctionCount
= 0;
514 if ( isset( $this->mPreprocessor
) && $this->mPreprocessor
->parser
!== $this ) {
515 $this->mPreprocessor
= null;
518 $this->mProfiler
= new SectionProfiler();
520 // Avoid PHP 7.1 warning from passing $this by reference
522 Hooks
::run( 'ParserClearState', [ &$parser ] );
526 * Reset the ParserOutput
528 public function resetOutput() {
529 $this->mOutput
= new ParserOutput
;
530 $this->mOptions
->registerWatcher( [ $this->mOutput
, 'recordOption' ] );
534 * Convert wikitext to HTML
535 * Do not call this function recursively.
537 * @param string $text Text we want to parse
538 * @param-taint $text escapes_htmlnoent
539 * @param Title $title
540 * @param ParserOptions $options
541 * @param bool $linestart
542 * @param bool $clearState
543 * @param int|null $revid ID of the revision being rendered. This is used to render
544 * REVISION* magic words. 0 means that any current revision will be used. Null means
545 * that {{REVISIONID}}/{{REVISIONUSER}} will be empty and {{REVISIONTIMESTAMP}} will
546 * use the current timestamp.
547 * @return ParserOutput A ParserOutput
548 * @return-taint escaped
550 public function parse(
551 $text, Title
$title, ParserOptions
$options,
552 $linestart = true, $clearState = true, $revid = null
555 // We use U+007F DELETE to construct strip markers, so we have to make
556 // sure that this character does not occur in the input text.
557 $text = strtr( $text, "\x7f", "?" );
558 $magicScopeVariable = $this->lock();
560 // Strip U+0000 NULL (T159174)
561 $text = str_replace( "\000", '', $text );
563 $this->startParse( $title, $options, self
::OT_HTML
, $clearState );
565 $this->currentRevisionCache
= null;
566 $this->mInputSize
= strlen( $text );
567 if ( $this->mOptions
->getEnableLimitReport() ) {
568 $this->mOutput
->resetParseStartTime();
571 $oldRevisionId = $this->mRevisionId
;
572 $oldRevisionObject = $this->mRevisionObject
;
573 $oldRevisionTimestamp = $this->mRevisionTimestamp
;
574 $oldRevisionUser = $this->mRevisionUser
;
575 $oldRevisionSize = $this->mRevisionSize
;
576 if ( $revid !== null ) {
577 $this->mRevisionId
= $revid;
578 $this->mRevisionObject
= null;
579 $this->mRevisionTimestamp
= null;
580 $this->mRevisionUser
= null;
581 $this->mRevisionSize
= null;
584 // Avoid PHP 7.1 warning from passing $this by reference
586 Hooks
::run( 'ParserBeforeStrip', [ &$parser, &$text, &$this->mStripState
] );
588 Hooks
::run( 'ParserAfterStrip', [ &$parser, &$text, &$this->mStripState
] );
589 $text = $this->internalParse( $text );
590 Hooks
::run( 'ParserAfterParse', [ &$parser, &$text, &$this->mStripState
] );
592 $text = $this->internalParseHalfParsed( $text, true, $linestart );
595 * A converted title will be provided in the output object if title and
596 * content conversion are enabled, the article text does not contain
597 * a conversion-suppressing double-underscore tag, and no
598 * {{DISPLAYTITLE:...}} is present. DISPLAYTITLE takes precedence over
599 * automatic link conversion.
601 if ( !( $options->getDisableTitleConversion()
602 ||
isset( $this->mDoubleUnderscores
['nocontentconvert'] )
603 ||
isset( $this->mDoubleUnderscores
['notitleconvert'] )
604 ||
$this->mOutput
->getDisplayTitle() !== false )
606 $convruletitle = $this->getTargetLanguage()->getConvRuleTitle();
607 if ( $convruletitle ) {
608 $this->mOutput
->setTitleText( $convruletitle );
610 $titleText = $this->getTargetLanguage()->convertTitle( $title );
611 $this->mOutput
->setTitleText( $titleText );
615 # Compute runtime adaptive expiry if set
616 $this->mOutput
->finalizeAdaptiveCacheExpiry();
618 # Warn if too many heavyweight parser functions were used
619 if ( $this->mExpensiveFunctionCount
> $this->mOptions
->getExpensiveParserFunctionLimit() ) {
620 $this->limitationWarn( 'expensive-parserfunction',
621 $this->mExpensiveFunctionCount
,
622 $this->mOptions
->getExpensiveParserFunctionLimit()
626 # Information on limits, for the benefit of users who try to skirt them
627 if ( $this->mOptions
->getEnableLimitReport() ) {
628 $text .= $this->makeLimitReport();
631 # Wrap non-interface parser output in a <div> so it can be targeted
633 $class = $this->mOptions
->getWrapOutputClass();
634 if ( $class !== false && !$this->mOptions
->getInterfaceMessage() ) {
635 $this->mOutput
->addWrapperDivClass( $class );
638 $this->mOutput
->setText( $text );
640 $this->mRevisionId
= $oldRevisionId;
641 $this->mRevisionObject
= $oldRevisionObject;
642 $this->mRevisionTimestamp
= $oldRevisionTimestamp;
643 $this->mRevisionUser
= $oldRevisionUser;
644 $this->mRevisionSize
= $oldRevisionSize;
645 $this->mInputSize
= false;
646 $this->currentRevisionCache
= null;
648 return $this->mOutput
;
652 * Set the limit report data in the current ParserOutput, and return the
653 * limit report HTML comment.
657 protected function makeLimitReport() {
658 $maxIncludeSize = $this->mOptions
->getMaxIncludeSize();
660 $cpuTime = $this->mOutput
->getTimeSinceStart( 'cpu' );
661 if ( $cpuTime !== null ) {
662 $this->mOutput
->setLimitReportData( 'limitreport-cputime',
663 sprintf( "%.3f", $cpuTime )
667 $wallTime = $this->mOutput
->getTimeSinceStart( 'wall' );
668 $this->mOutput
->setLimitReportData( 'limitreport-walltime',
669 sprintf( "%.3f", $wallTime )
672 $this->mOutput
->setLimitReportData( 'limitreport-ppvisitednodes',
673 [ $this->mPPNodeCount
, $this->mOptions
->getMaxPPNodeCount() ]
675 $this->mOutput
->setLimitReportData( 'limitreport-ppgeneratednodes',
676 [ $this->mGeneratedPPNodeCount
, $this->mOptions
->getMaxGeneratedPPNodeCount() ]
678 $this->mOutput
->setLimitReportData( 'limitreport-postexpandincludesize',
679 [ $this->mIncludeSizes
['post-expand'], $maxIncludeSize ]
681 $this->mOutput
->setLimitReportData( 'limitreport-templateargumentsize',
682 [ $this->mIncludeSizes
['arg'], $maxIncludeSize ]
684 $this->mOutput
->setLimitReportData( 'limitreport-expansiondepth',
685 [ $this->mHighestExpansionDepth
, $this->mOptions
->getMaxPPExpandDepth() ]
687 $this->mOutput
->setLimitReportData( 'limitreport-expensivefunctioncount',
688 [ $this->mExpensiveFunctionCount
, $this->mOptions
->getExpensiveParserFunctionLimit() ]
691 foreach ( $this->mStripState
->getLimitReport() as list( $key, $value ) ) {
692 $this->mOutput
->setLimitReportData( $key, $value );
695 Hooks
::run( 'ParserLimitReportPrepare', [ $this, $this->mOutput
] );
697 $limitReport = "NewPP limit report\n";
698 if ( $this->svcOptions
->get( 'ShowHostnames' ) ) {
699 $limitReport .= 'Parsed by ' . wfHostname() . "\n";
701 $limitReport .= 'Cached time: ' . $this->mOutput
->getCacheTime() . "\n";
702 $limitReport .= 'Cache expiry: ' . $this->mOutput
->getCacheExpiry() . "\n";
703 $limitReport .= 'Dynamic content: ' .
704 ( $this->mOutput
->hasDynamicContent() ?
'true' : 'false' ) .
706 $limitReport .= 'Complications: [' . implode( ', ', $this->mOutput
->getAllFlags() ) . "]\n";
708 foreach ( $this->mOutput
->getLimitReportData() as $key => $value ) {
709 if ( Hooks
::run( 'ParserLimitReportFormat',
710 [ $key, &$value, &$limitReport, false, false ]
712 $keyMsg = wfMessage( $key )->inLanguage( 'en' )->useDatabase( false );
713 $valueMsg = wfMessage( [ "$key-value-text", "$key-value" ] )
714 ->inLanguage( 'en' )->useDatabase( false );
715 if ( !$valueMsg->exists() ) {
716 $valueMsg = new RawMessage( '$1' );
718 if ( !$keyMsg->isDisabled() && !$valueMsg->isDisabled() ) {
719 $valueMsg->params( $value );
720 $limitReport .= "{$keyMsg->text()}: {$valueMsg->text()}\n";
724 // Since we're not really outputting HTML, decode the entities and
725 // then re-encode the things that need hiding inside HTML comments.
726 $limitReport = htmlspecialchars_decode( $limitReport );
728 // Sanitize for comment. Note '‐' in the replacement is U+2010,
729 // which looks much like the problematic '-'.
730 $limitReport = str_replace( [ '-', '&' ], [ '‐', '&' ], $limitReport );
731 $text = "\n<!-- \n$limitReport-->\n";
733 // Add on template profiling data in human/machine readable way
734 $dataByFunc = $this->mProfiler
->getFunctionStats();
735 uasort( $dataByFunc, function ( $a, $b ) {
736 return $b['real'] <=> $a['real']; // descending order
739 foreach ( array_slice( $dataByFunc, 0, 10 ) as $item ) {
740 $profileReport[] = sprintf( "%6.2f%% %8.3f %6d %s",
741 $item['%real'], $item['real'], $item['calls'],
742 htmlspecialchars( $item['name'] ) );
744 $text .= "<!--\nTransclusion expansion time report (%,ms,calls,template)\n";
745 $text .= implode( "\n", $profileReport ) . "\n-->\n";
747 $this->mOutput
->setLimitReportData( 'limitreport-timingprofile', $profileReport );
749 // Add other cache related metadata
750 if ( $this->svcOptions
->get( 'ShowHostnames' ) ) {
751 $this->mOutput
->setLimitReportData( 'cachereport-origin', wfHostname() );
753 $this->mOutput
->setLimitReportData( 'cachereport-timestamp',
754 $this->mOutput
->getCacheTime() );
755 $this->mOutput
->setLimitReportData( 'cachereport-ttl',
756 $this->mOutput
->getCacheExpiry() );
757 $this->mOutput
->setLimitReportData( 'cachereport-transientcontent',
758 $this->mOutput
->hasDynamicContent() );
760 if ( $this->mGeneratedPPNodeCount
> $this->mOptions
->getMaxGeneratedPPNodeCount() / 10 ) {
761 wfDebugLog( 'generated-pp-node-count', $this->mGeneratedPPNodeCount
. ' ' .
762 $this->getTitle()->getPrefixedDBkey() );
768 * Half-parse wikitext to half-parsed HTML. This recursive parser entry point
769 * can be called from an extension tag hook.
771 * The output of this function IS NOT SAFE PARSED HTML; it is "half-parsed"
772 * instead, which means that lists and links have not been fully parsed yet,
773 * and strip markers are still present.
775 * Use recursiveTagParseFully() to fully parse wikitext to output-safe HTML.
777 * Use this function if you're a parser tag hook and you want to parse
778 * wikitext before or after applying additional transformations, and you
779 * intend to *return the result as hook output*, which will cause it to go
780 * through the rest of parsing process automatically.
782 * If $frame is not provided, then template variables (e.g., {{{1}}}) within
783 * $text are not expanded
785 * @param string $text Text extension wants to have parsed
786 * @param-taint $text escapes_htmlnoent
787 * @param bool|PPFrame $frame The frame to use for expanding any template variables
788 * @return string UNSAFE half-parsed HTML
789 * @return-taint escaped
791 public function recursiveTagParse( $text, $frame = false ) {
792 // Avoid PHP 7.1 warning from passing $this by reference
794 Hooks
::run( 'ParserBeforeStrip', [ &$parser, &$text, &$this->mStripState
] );
795 Hooks
::run( 'ParserAfterStrip', [ &$parser, &$text, &$this->mStripState
] );
796 $text = $this->internalParse( $text, false, $frame );
801 * Fully parse wikitext to fully parsed HTML. This recursive parser entry
802 * point can be called from an extension tag hook.
804 * The output of this function is fully-parsed HTML that is safe for output.
805 * If you're a parser tag hook, you might want to use recursiveTagParse()
808 * If $frame is not provided, then template variables (e.g., {{{1}}}) within
809 * $text are not expanded
813 * @param string $text Text extension wants to have parsed
814 * @param-taint $text escapes_htmlnoent
815 * @param bool|PPFrame $frame The frame to use for expanding any template variables
816 * @return string Fully parsed HTML
817 * @return-taint escaped
819 public function recursiveTagParseFully( $text, $frame = false ) {
820 $text = $this->recursiveTagParse( $text, $frame );
821 $text = $this->internalParseHalfParsed( $text, false );
826 * Expand templates and variables in the text, producing valid, static wikitext.
827 * Also removes comments.
828 * Do not call this function recursively.
829 * @param string $text
830 * @param Title|null $title
831 * @param ParserOptions $options
832 * @param int|null $revid
833 * @param bool|PPFrame $frame
834 * @return mixed|string
836 public function preprocess( $text, Title
$title = null,
837 ParserOptions
$options, $revid = null, $frame = false
839 $magicScopeVariable = $this->lock();
840 $this->startParse( $title, $options, self
::OT_PREPROCESS
, true );
841 if ( $revid !== null ) {
842 $this->mRevisionId
= $revid;
844 // Avoid PHP 7.1 warning from passing $this by reference
846 Hooks
::run( 'ParserBeforeStrip', [ &$parser, &$text, &$this->mStripState
] );
847 Hooks
::run( 'ParserAfterStrip', [ &$parser, &$text, &$this->mStripState
] );
848 $text = $this->replaceVariables( $text, $frame );
849 $text = $this->mStripState
->unstripBoth( $text );
854 * Recursive parser entry point that can be called from an extension tag
857 * @param string $text Text to be expanded
858 * @param bool|PPFrame $frame The frame to use for expanding any template variables
862 public function recursivePreprocess( $text, $frame = false ) {
863 $text = $this->replaceVariables( $text, $frame );
864 $text = $this->mStripState
->unstripBoth( $text );
869 * Process the wikitext for the "?preload=" feature. (T7210)
871 * "<noinclude>", "<includeonly>" etc. are parsed as for template
872 * transclusion, comments, templates, arguments, tags hooks and parser
873 * functions are untouched.
875 * @param string $text
876 * @param Title $title
877 * @param ParserOptions $options
878 * @param array $params
881 public function getPreloadText( $text, Title
$title, ParserOptions
$options, $params = [] ) {
882 $msg = new RawMessage( $text );
883 $text = $msg->params( $params )->plain();
885 # Parser (re)initialisation
886 $magicScopeVariable = $this->lock();
887 $this->startParse( $title, $options, self
::OT_PLAIN
, true );
889 $flags = PPFrame
::NO_ARGS | PPFrame
::NO_TEMPLATES
;
890 $dom = $this->preprocessToDom( $text, self
::PTD_FOR_INCLUSION
);
891 $text = $this->getPreprocessor()->newFrame()->expand( $dom, $flags );
892 $text = $this->mStripState
->unstripBoth( $text );
897 * Set the current user.
898 * Should only be used when doing pre-save transform.
900 * @param User|null $user User object or null (to reset)
902 public function setUser( $user ) {
903 $this->mUser
= $user;
907 * Set the context title
909 * @param Title|null $t
911 public function setTitle( Title
$t = null ) {
913 $t = Title
::makeTitle( NS_SPECIAL
, 'Badtitle/Parser' );
916 if ( $t->hasFragment() ) {
917 # Strip the fragment to avoid various odd effects
918 $this->mTitle
= $t->createFragmentTarget( '' );
925 * Accessor for the Title object
929 public function getTitle() : Title
{
930 return $this->mTitle
;
934 * Accessor/mutator for the Title object
936 * @param Title|null $x Title object or null to just get the current one
939 public function Title( Title
$x = null ) : Title
{
940 return wfSetVar( $this->mTitle
, $x );
944 * Set the output type
946 * @param int $ot New value
948 public function setOutputType( $ot ) {
949 $this->mOutputType
= $ot;
952 'html' => $ot == self
::OT_HTML
,
953 'wiki' => $ot == self
::OT_WIKI
,
954 'pre' => $ot == self
::OT_PREPROCESS
,
955 'plain' => $ot == self
::OT_PLAIN
,
960 * Accessor/mutator for the output type
962 * @param int|null $x New value or null to just get the current one
965 public function OutputType( $x = null ) {
966 return wfSetVar( $this->mOutputType
, $x );
970 * Get the ParserOutput object
972 * @return ParserOutput
974 public function getOutput() {
975 return $this->mOutput
;
979 * Get the ParserOptions object
981 * @return ParserOptions
983 public function getOptions() {
984 return $this->mOptions
;
988 * Accessor/mutator for the ParserOptions object
990 * @param ParserOptions|null $x New value or null to just get the current one
991 * @return ParserOptions Current ParserOptions object
993 public function Options( $x = null ) {
994 return wfSetVar( $this->mOptions
, $x );
1000 public function nextLinkID() {
1001 return $this->mLinkID++
;
1007 public function setLinkID( $id ) {
1008 $this->mLinkID
= $id;
1012 * Get a language object for use in parser functions such as {{FORMATNUM:}}
1015 public function getFunctionLang() {
1016 return $this->getTargetLanguage();
1020 * Get the target language for the content being parsed. This is usually the
1021 * language that the content is in.
1027 public function getTargetLanguage() {
1028 $target = $this->mOptions
->getTargetLanguage();
1030 if ( $target !== null ) {
1032 } elseif ( $this->mOptions
->getInterfaceMessage() ) {
1033 return $this->mOptions
->getUserLangObj();
1036 return $this->getTitle()->getPageLanguage();
1040 * Get the language object for language conversion
1041 * @deprecated since 1.32, just use getTargetLanguage()
1042 * @return Language|null
1044 public function getConverterLanguage() {
1045 return $this->getTargetLanguage();
1049 * Get a User object either from $this->mUser, if set, or from the
1050 * ParserOptions object otherwise
1054 public function getUser() {
1055 if ( !is_null( $this->mUser
) ) {
1056 return $this->mUser
;
1058 return $this->mOptions
->getUser();
1062 * Get a preprocessor object
1064 * @return Preprocessor
1066 public function getPreprocessor() {
1067 if ( !isset( $this->mPreprocessor
) ) {
1068 $class = $this->svcOptions
->get( 'preprocessorClass' );
1069 $this->mPreprocessor
= new $class( $this );
1071 return $this->mPreprocessor
;
1075 * Get a LinkRenderer instance to make links with
1078 * @return LinkRenderer
1080 public function getLinkRenderer() {
1081 // XXX We make the LinkRenderer with current options and then cache it forever
1082 if ( !$this->mLinkRenderer
) {
1083 $this->mLinkRenderer
= $this->linkRendererFactory
->create();
1084 $this->mLinkRenderer
->setStubThreshold(
1085 $this->getOptions()->getStubThreshold()
1089 return $this->mLinkRenderer
;
1093 * Get the MagicWordFactory that this Parser is using
1096 * @return MagicWordFactory
1098 public function getMagicWordFactory() {
1099 return $this->magicWordFactory
;
1103 * Get the content language that this Parser is using
1108 public function getContentLanguage() {
1109 return $this->contLang
;
1113 * Replaces all occurrences of HTML-style comments and the given tags
1114 * in the text with a random marker and returns the next text. The output
1115 * parameter $matches will be an associative array filled with data in
1122 * [ 'param' => 'x' ],
1123 * '<element param="x">tag content</element>' ]
1126 * @param array $elements List of element names. Comments are always extracted.
1127 * @param string $text Source text string.
1128 * @param array &$matches Out parameter, Array: extracted tags
1129 * @return string Stripped text
1131 public static function extractTagsAndParams( $elements, $text, &$matches ) {
1136 $taglist = implode( '|', $elements );
1137 $start = "/<($taglist)(\\s+[^>]*?|\\s*?)(\/?>)|<(!--)/i";
1139 while ( $text != '' ) {
1140 $p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE
);
1142 if ( count( $p ) < 5 ) {
1145 if ( count( $p ) > 5 ) {
1153 list( , $element, $attributes, $close, $inside ) = $p;
1156 $marker = self
::MARKER_PREFIX
. "-$element-" . sprintf( '%08X', $n++
) . self
::MARKER_SUFFIX
;
1157 $stripped .= $marker;
1159 if ( $close === '/>' ) {
1160 # Empty element tag, <tag />
1165 if ( $element === '!--' ) {
1168 $end = "/(<\\/$element\\s*>)/i";
1170 $q = preg_split( $end, $inside, 2, PREG_SPLIT_DELIM_CAPTURE
);
1172 if ( count( $q ) < 3 ) {
1173 # No end tag -- let it run out to the end of the text.
1177 list( , $tail, $text ) = $q;
1181 $matches[$marker] = [ $element,
1183 Sanitizer
::decodeTagAttributes( $attributes ),
1184 "<$element$attributes$close$content$tail" ];
1190 * Get a list of strippable XML-like elements
1194 public function getStripList() {
1195 return $this->mStripList
;
1199 * Get the StripState
1201 * @return StripState
1203 public function getStripState() {
1204 return $this->mStripState
;
1208 * Add an item to the strip state
1209 * Returns the unique tag which must be inserted into the stripped text
1210 * The tag will be replaced with the original text in unstrip()
1212 * @param string $text
1216 public function insertStripItem( $text ) {
1217 $marker = self
::MARKER_PREFIX
. "-item-{$this->mMarkerIndex}-" . self
::MARKER_SUFFIX
;
1218 $this->mMarkerIndex++
;
1219 $this->mStripState
->addGeneral( $marker, $text );
1224 * parse the wiki syntax used to render tables
1227 * @param string $text
1230 public function doTableStuff( $text ) {
1231 $lines = StringUtils
::explode( "\n", $text );
1233 $td_history = []; # Is currently a td tag open?
1234 $last_tag_history = []; # Save history of last lag activated (td, th or caption)
1235 $tr_history = []; # Is currently a tr tag open?
1236 $tr_attributes = []; # history of tr attributes
1237 $has_opened_tr = []; # Did this table open a <tr> element?
1238 $indent_level = 0; # indent level of the table
1240 foreach ( $lines as $outLine ) {
1241 $line = trim( $outLine );
1243 if ( $line === '' ) { # empty line, go to next line
1244 $out .= $outLine . "\n";
1248 $first_character = $line[0];
1249 $first_two = substr( $line, 0, 2 );
1252 if ( preg_match( '/^(:*)\s*\{\|(.*)$/', $line, $matches ) ) {
1253 # First check if we are starting a new table
1254 $indent_level = strlen( $matches[1] );
1256 $attributes = $this->mStripState
->unstripBoth( $matches[2] );
1257 $attributes = Sanitizer
::fixTagAttributes( $attributes, 'table' );
1259 $outLine = str_repeat( '<dl><dd>', $indent_level ) . "<table{$attributes}>";
1260 array_push( $td_history, false );
1261 array_push( $last_tag_history, '' );
1262 array_push( $tr_history, false );
1263 array_push( $tr_attributes, '' );
1264 array_push( $has_opened_tr, false );
1265 } elseif ( count( $td_history ) == 0 ) {
1266 # Don't do any of the following
1267 $out .= $outLine . "\n";
1269 } elseif ( $first_two === '|}' ) {
1270 # We are ending a table
1271 $line = '</table>' . substr( $line, 2 );
1272 $last_tag = array_pop( $last_tag_history );
1274 if ( !array_pop( $has_opened_tr ) ) {
1275 $line = "<tr><td></td></tr>{$line}";
1278 if ( array_pop( $tr_history ) ) {
1279 $line = "</tr>{$line}";
1282 if ( array_pop( $td_history ) ) {
1283 $line = "</{$last_tag}>{$line}";
1285 array_pop( $tr_attributes );
1286 if ( $indent_level > 0 ) {
1287 $outLine = rtrim( $line ) . str_repeat( '</dd></dl>', $indent_level );
1291 } elseif ( $first_two === '|-' ) {
1292 # Now we have a table row
1293 $line = preg_replace( '#^\|-+#', '', $line );
1295 # Whats after the tag is now only attributes
1296 $attributes = $this->mStripState
->unstripBoth( $line );
1297 $attributes = Sanitizer
::fixTagAttributes( $attributes, 'tr' );
1298 array_pop( $tr_attributes );
1299 array_push( $tr_attributes, $attributes );
1302 $last_tag = array_pop( $last_tag_history );
1303 array_pop( $has_opened_tr );
1304 array_push( $has_opened_tr, true );
1306 if ( array_pop( $tr_history ) ) {
1310 if ( array_pop( $td_history ) ) {
1311 $line = "</{$last_tag}>{$line}";
1315 array_push( $tr_history, false );
1316 array_push( $td_history, false );
1317 array_push( $last_tag_history, '' );
1318 } elseif ( $first_character === '|'
1319 ||
$first_character === '!'
1320 ||
$first_two === '|+'
1322 # This might be cell elements, td, th or captions
1323 if ( $first_two === '|+' ) {
1324 $first_character = '+';
1325 $line = substr( $line, 2 );
1327 $line = substr( $line, 1 );
1330 // Implies both are valid for table headings.
1331 if ( $first_character === '!' ) {
1332 $line = StringUtils
::replaceMarkup( '!!', '||', $line );
1335 # Split up multiple cells on the same line.
1336 # FIXME : This can result in improper nesting of tags processed
1337 # by earlier parser steps.
1338 $cells = explode( '||', $line );
1342 # Loop through each table cell
1343 foreach ( $cells as $cell ) {
1345 if ( $first_character !== '+' ) {
1346 $tr_after = array_pop( $tr_attributes );
1347 if ( !array_pop( $tr_history ) ) {
1348 $previous = "<tr{$tr_after}>\n";
1350 array_push( $tr_history, true );
1351 array_push( $tr_attributes, '' );
1352 array_pop( $has_opened_tr );
1353 array_push( $has_opened_tr, true );
1356 $last_tag = array_pop( $last_tag_history );
1358 if ( array_pop( $td_history ) ) {
1359 $previous = "</{$last_tag}>\n{$previous}";
1362 if ( $first_character === '|' ) {
1364 } elseif ( $first_character === '!' ) {
1366 } elseif ( $first_character === '+' ) {
1367 $last_tag = 'caption';
1372 array_push( $last_tag_history, $last_tag );
1374 # A cell could contain both parameters and data
1375 $cell_data = explode( '|', $cell, 2 );
1377 # T2553: Note that a '|' inside an invalid link should not
1378 # be mistaken as delimiting cell parameters
1379 # Bug T153140: Neither should language converter markup.
1380 if ( preg_match( '/\[\[|-\{/', $cell_data[0] ) === 1 ) {
1381 $cell = "{$previous}<{$last_tag}>" . trim( $cell );
1382 } elseif ( count( $cell_data ) == 1 ) {
1383 // Whitespace in cells is trimmed
1384 $cell = "{$previous}<{$last_tag}>" . trim( $cell_data[0] );
1386 $attributes = $this->mStripState
->unstripBoth( $cell_data[0] );
1387 $attributes = Sanitizer
::fixTagAttributes( $attributes, $last_tag );
1388 // Whitespace in cells is trimmed
1389 $cell = "{$previous}<{$last_tag}{$attributes}>" . trim( $cell_data[1] );
1393 array_push( $td_history, true );
1396 $out .= $outLine . "\n";
1399 # Closing open td, tr && table
1400 while ( count( $td_history ) > 0 ) {
1401 if ( array_pop( $td_history ) ) {
1404 if ( array_pop( $tr_history ) ) {
1407 if ( !array_pop( $has_opened_tr ) ) {
1408 $out .= "<tr><td></td></tr>\n";
1411 $out .= "</table>\n";
1414 # Remove trailing line-ending (b/c)
1415 if ( substr( $out, -1 ) === "\n" ) {
1416 $out = substr( $out, 0, -1 );
1419 # special case: don't return empty table
1420 if ( $out === "<table>\n<tr><td></td></tr>\n</table>" ) {
1428 * Helper function for parse() that transforms wiki markup into half-parsed
1429 * HTML. Only called for $mOutputType == self::OT_HTML.
1433 * @param string $text The text to parse
1434 * @param-taint $text escapes_html
1435 * @param bool $isMain Whether this is being called from the main parse() function
1436 * @param PPFrame|bool $frame A pre-processor frame
1440 public function internalParse( $text, $isMain = true, $frame = false ) {
1443 // Avoid PHP 7.1 warning from passing $this by reference
1446 # Hook to suspend the parser in this state
1447 if ( !Hooks
::run( 'ParserBeforeInternalParse', [ &$parser, &$text, &$this->mStripState
] ) ) {
1451 # if $frame is provided, then use $frame for replacing any variables
1453 # use frame depth to infer how include/noinclude tags should be handled
1454 # depth=0 means this is the top-level document; otherwise it's an included document
1455 if ( !$frame->depth
) {
1458 $flag = self
::PTD_FOR_INCLUSION
;
1460 $dom = $this->preprocessToDom( $text, $flag );
1461 $text = $frame->expand( $dom );
1463 # if $frame is not provided, then use old-style replaceVariables
1464 $text = $this->replaceVariables( $text );
1467 Hooks
::run( 'InternalParseBeforeSanitize', [ &$parser, &$text, &$this->mStripState
] );
1468 $text = Sanitizer
::removeHTMLtags(
1470 [ $this, 'attributeStripCallback' ],
1472 array_keys( $this->mTransparentTagHooks
),
1474 [ $this, 'addTrackingCategory' ]
1476 Hooks
::run( 'InternalParseBeforeLinks', [ &$parser, &$text, &$this->mStripState
] );
1478 # Tables need to come after variable replacement for things to work
1479 # properly; putting them before other transformations should keep
1480 # exciting things like link expansions from showing up in surprising
1482 $text = $this->doTableStuff( $text );
1484 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
1486 $text = $this->doDoubleUnderscore( $text );
1488 $text = $this->doHeadings( $text );
1489 $text = $this->replaceInternalLinks( $text );
1490 $text = $this->doAllQuotes( $text );
1491 $text = $this->replaceExternalLinks( $text );
1493 # replaceInternalLinks may sometimes leave behind
1494 # absolute URLs, which have to be masked to hide them from replaceExternalLinks
1495 $text = str_replace( self
::MARKER_PREFIX
. 'NOPARSE', '', $text );
1497 $text = $this->doMagicLinks( $text );
1498 $text = $this->formatHeadings( $text, $origText, $isMain );
1504 * Helper function for parse() that transforms half-parsed HTML into fully
1507 * @param string $text
1508 * @param bool $isMain
1509 * @param bool $linestart
1512 private function internalParseHalfParsed( $text, $isMain = true, $linestart = true ) {
1513 $text = $this->mStripState
->unstripGeneral( $text );
1515 // Avoid PHP 7.1 warning from passing $this by reference
1519 Hooks
::run( 'ParserAfterUnstrip', [ &$parser, &$text ] );
1522 # Clean up special characters, only run once, next-to-last before doBlockLevels
1523 $text = Sanitizer
::armorFrenchSpaces( $text );
1525 $text = $this->doBlockLevels( $text, $linestart );
1527 $this->replaceLinkHolders( $text );
1530 * The input doesn't get language converted if
1532 * b) Content isn't converted
1533 * c) It's a conversion table
1534 * d) it is an interface message (which is in the user language)
1536 if ( !( $this->mOptions
->getDisableContentConversion()
1537 ||
isset( $this->mDoubleUnderscores
['nocontentconvert'] ) )
1538 && !$this->mOptions
->getInterfaceMessage()
1540 # The position of the convert() call should not be changed. it
1541 # assumes that the links are all replaced and the only thing left
1542 # is the <nowiki> mark.
1543 $text = $this->getTargetLanguage()->convert( $text );
1546 $text = $this->mStripState
->unstripNoWiki( $text );
1549 Hooks
::run( 'ParserBeforeTidy', [ &$parser, &$text ] );
1552 $text = $this->replaceTransparentTags( $text );
1553 $text = $this->mStripState
->unstripGeneral( $text );
1555 $text = Sanitizer
::normalizeCharReferences( $text );
1557 if ( MWTidy
::isEnabled() ) {
1558 if ( $this->mOptions
->getTidy() ) {
1559 $text = MWTidy
::tidy( $text );
1562 # attempt to sanitize at least some nesting problems
1563 # (T4702 and quite a few others)
1564 # This code path is buggy and deprecated!
1565 wfDeprecated( 'disabling tidy', '1.33' );
1567 # ''Something [http://www.cool.com cool''] -->
1568 # <i>Something</i><a href="http://www.cool.com"..><i>cool></i></a>
1569 '/(<([bi])>)(<([bi])>)?([^<]*)(<\/?a[^<]*>)([^<]*)(<\/\\4>)?(<\/\\2>)/' =>
1570 '\\1\\3\\5\\8\\9\\6\\1\\3\\7\\8\\9',
1571 # fix up an anchor inside another anchor, only
1572 # at least for a single single nested link (T5695)
1573 '/(<a[^>]+>)([^<]*)(<a[^>]+>[^<]*)<\/a>(.*)<\/a>/' =>
1574 '\\1\\2</a>\\3</a>\\1\\4</a>',
1575 # fix div inside inline elements- doBlockLevels won't wrap a line which
1576 # contains a div, so fix it up here; replace
1577 # div with escaped text
1578 '/(<([aib]) [^>]+>)([^<]*)(<div([^>]*)>)(.*)(<\/div>)([^<]*)(<\/\\2>)/' =>
1579 '\\1\\3<div\\5>\\6</div>\\8\\9',
1580 # remove empty italic or bold tag pairs, some
1581 # introduced by rules above
1582 '/<([bi])><\/\\1>/' => '',
1585 $text = preg_replace(
1586 array_keys( $tidyregs ),
1587 array_values( $tidyregs ),
1592 Hooks
::run( 'ParserAfterTidy', [ &$parser, &$text ] );
1599 * Replace special strings like "ISBN xxx" and "RFC xxx" with
1600 * magic external links.
1605 * @param string $text
1609 public function doMagicLinks( $text ) {
1610 $prots = wfUrlProtocolsWithoutProtRel();
1611 $urlChar = self
::EXT_LINK_URL_CLASS
;
1612 $addr = self
::EXT_LINK_ADDR
;
1613 $space = self
::SPACE_NOT_NL
; # non-newline space
1614 $spdash = "(?:-|$space)"; # a dash or a non-newline space
1615 $spaces = "$space++"; # possessive match of 1 or more spaces
1616 $text = preg_replace_callback(
1618 (<a[ \t\r\n>].*?</a>) | # m[1]: Skip link text
1619 (<.*?>) | # m[2]: Skip stuff inside HTML elements' . "
1620 (\b # m[3]: Free external links
1622 ($addr$urlChar*) # m[4]: Post-protocol path
1624 \b(?:RFC|PMID) $spaces # m[5]: RFC or PMID, capture number
1626 \bISBN $spaces ( # m[6]: ISBN, capture number
1627 (?: 97[89] $spdash? )? # optional 13-digit ISBN prefix
1628 (?: [0-9] $spdash? ){9} # 9 digits with opt. delimiters
1629 [0-9Xx] # check digit
1631 )!xu", [ $this, 'magicLinkCallback' ], $text );
1636 * @throws MWException
1638 * @return string HTML
1640 public function magicLinkCallback( $m ) {
1641 if ( isset( $m[1] ) && $m[1] !== '' ) {
1644 } elseif ( isset( $m[2] ) && $m[2] !== '' ) {
1647 } elseif ( isset( $m[3] ) && $m[3] !== '' ) {
1648 # Free external link
1649 return $this->makeFreeExternalLink( $m[0], strlen( $m[4] ) );
1650 } elseif ( isset( $m[5] ) && $m[5] !== '' ) {
1652 if ( substr( $m[0], 0, 3 ) === 'RFC' ) {
1653 if ( !$this->mOptions
->getMagicRFCLinks() ) {
1658 $cssClass = 'mw-magiclink-rfc';
1659 $trackingCat = 'magiclink-tracking-rfc';
1661 } elseif ( substr( $m[0], 0, 4 ) === 'PMID' ) {
1662 if ( !$this->mOptions
->getMagicPMIDLinks() ) {
1666 $urlmsg = 'pubmedurl';
1667 $cssClass = 'mw-magiclink-pmid';
1668 $trackingCat = 'magiclink-tracking-pmid';
1671 throw new MWException( __METHOD__
. ': unrecognised match type "' .
1672 substr( $m[0], 0, 20 ) . '"' );
1674 $url = wfMessage( $urlmsg, $id )->inContentLanguage()->text();
1675 $this->addTrackingCategory( $trackingCat );
1676 return Linker
::makeExternalLink(
1684 } elseif ( isset( $m[6] ) && $m[6] !== ''
1685 && $this->mOptions
->getMagicISBNLinks()
1689 $space = self
::SPACE_NOT_NL
; # non-newline space
1690 $isbn = preg_replace( "/$space/", ' ', $isbn );
1691 $num = strtr( $isbn, [
1696 $this->addTrackingCategory( 'magiclink-tracking-isbn' );
1697 return $this->getLinkRenderer()->makeKnownLink(
1698 SpecialPage
::getTitleFor( 'Booksources', $num ),
1701 'class' => 'internal mw-magiclink-isbn',
1702 'title' => false // suppress title attribute
1711 * Make a free external link, given a user-supplied URL
1713 * @param string $url
1714 * @param int $numPostProto
1715 * The number of characters after the protocol.
1716 * @return string HTML
1719 public function makeFreeExternalLink( $url, $numPostProto ) {
1722 # The characters '<' and '>' (which were escaped by
1723 # removeHTMLtags()) should not be included in
1724 # URLs, per RFC 2396.
1725 # Make terminate a URL as well (bug T84937)
1728 '/&(lt|gt|nbsp|#x0*(3[CcEe]|[Aa]0)|#0*(60|62|160));/',
1733 $trail = substr( $url, $m2[0][1] ) . $trail;
1734 $url = substr( $url, 0, $m2[0][1] );
1737 # Move trailing punctuation to $trail
1739 # If there is no left bracket, then consider right brackets fair game too
1740 if ( strpos( $url, '(' ) === false ) {
1744 $urlRev = strrev( $url );
1745 $numSepChars = strspn( $urlRev, $sep );
1746 # Don't break a trailing HTML entity by moving the ; into $trail
1747 # This is in hot code, so use substr_compare to avoid having to
1748 # create a new string object for the comparison
1749 if ( $numSepChars && substr_compare( $url, ";", -$numSepChars, 1 ) === 0 ) {
1750 # more optimization: instead of running preg_match with a $
1751 # anchor, which can be slow, do the match on the reversed
1752 # string starting at the desired offset.
1753 # un-reversed regexp is: /&([a-z]+|#x[\da-f]+|#\d+)$/i
1754 if ( preg_match( '/\G([a-z]+|[\da-f]+x#|\d+#)&/i', $urlRev, $m2, 0, $numSepChars ) ) {
1758 if ( $numSepChars ) {
1759 $trail = substr( $url, -$numSepChars ) . $trail;
1760 $url = substr( $url, 0, -$numSepChars );
1763 # Verify that we still have a real URL after trail removal, and
1764 # not just lone protocol
1765 if ( strlen( $trail ) >= $numPostProto ) {
1766 return $url . $trail;
1769 $url = Sanitizer
::cleanUrl( $url );
1771 # Is this an external image?
1772 $text = $this->maybeMakeExternalImage( $url );
1773 if ( $text === false ) {
1774 # Not an image, make a link
1775 $text = Linker
::makeExternalLink( $url,
1776 $this->getTargetLanguage()->getConverter()->markNoConversion( $url ),
1778 $this->getExternalLinkAttribs( $url ), $this->getTitle() );
1779 # Register it in the output object...
1780 $this->mOutput
->addExternalLink( $url );
1782 return $text . $trail;
1786 * Parse headers and return html
1790 * @param string $text
1794 public function doHeadings( $text ) {
1795 for ( $i = 6; $i >= 1; --$i ) {
1796 $h = str_repeat( '=', $i );
1797 // Trim non-newline whitespace from headings
1798 // Using \s* will break for: "==\n===\n" and parse as <h2>=</h2>
1799 $text = preg_replace( "/^(?:$h)[ \\t]*(.+?)[ \\t]*(?:$h)\\s*$/m", "<h$i>\\1</h$i>", $text );
1805 * Replace single quotes with HTML markup
1808 * @param string $text
1810 * @return string The altered text
1812 public function doAllQuotes( $text ) {
1814 $lines = StringUtils
::explode( "\n", $text );
1815 foreach ( $lines as $line ) {
1816 $outtext .= $this->doQuotes( $line ) . "\n";
1818 $outtext = substr( $outtext, 0, -1 );
1823 * Helper function for doAllQuotes()
1825 * @param string $text
1829 public function doQuotes( $text ) {
1830 $arr = preg_split( "/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
1831 $countarr = count( $arr );
1832 if ( $countarr == 1 ) {
1836 // First, do some preliminary work. This may shift some apostrophes from
1837 // being mark-up to being text. It also counts the number of occurrences
1838 // of bold and italics mark-ups.
1841 for ( $i = 1; $i < $countarr; $i +
= 2 ) {
1842 $thislen = strlen( $arr[$i] );
1843 // If there are ever four apostrophes, assume the first is supposed to
1844 // be text, and the remaining three constitute mark-up for bold text.
1845 // (T15227: ''''foo'''' turns into ' ''' foo ' ''')
1846 if ( $thislen == 4 ) {
1847 $arr[$i - 1] .= "'";
1850 } elseif ( $thislen > 5 ) {
1851 // If there are more than 5 apostrophes in a row, assume they're all
1852 // text except for the last 5.
1853 // (T15227: ''''''foo'''''' turns into ' ''''' foo ' ''''')
1854 $arr[$i - 1] .= str_repeat( "'", $thislen - 5 );
1858 // Count the number of occurrences of bold and italics mark-ups.
1859 if ( $thislen == 2 ) {
1861 } elseif ( $thislen == 3 ) {
1863 } elseif ( $thislen == 5 ) {
1869 // If there is an odd number of both bold and italics, it is likely
1870 // that one of the bold ones was meant to be an apostrophe followed
1871 // by italics. Which one we cannot know for certain, but it is more
1872 // likely to be one that has a single-letter word before it.
1873 if ( ( $numbold %
2 == 1 ) && ( $numitalics %
2 == 1 ) ) {
1874 $firstsingleletterword = -1;
1875 $firstmultiletterword = -1;
1877 for ( $i = 1; $i < $countarr; $i +
= 2 ) {
1878 if ( strlen( $arr[$i] ) == 3 ) {
1879 $x1 = substr( $arr[$i - 1], -1 );
1880 $x2 = substr( $arr[$i - 1], -2, 1 );
1881 if ( $x1 === ' ' ) {
1882 if ( $firstspace == -1 ) {
1885 } elseif ( $x2 === ' ' ) {
1886 $firstsingleletterword = $i;
1887 // if $firstsingleletterword is set, we don't
1888 // look at the other options, so we can bail early.
1890 } elseif ( $firstmultiletterword == -1 ) {
1891 $firstmultiletterword = $i;
1896 // If there is a single-letter word, use it!
1897 if ( $firstsingleletterword > -1 ) {
1898 $arr[$firstsingleletterword] = "''";
1899 $arr[$firstsingleletterword - 1] .= "'";
1900 } elseif ( $firstmultiletterword > -1 ) {
1901 // If not, but there's a multi-letter word, use that one.
1902 $arr[$firstmultiletterword] = "''";
1903 $arr[$firstmultiletterword - 1] .= "'";
1904 } elseif ( $firstspace > -1 ) {
1905 // ... otherwise use the first one that has neither.
1906 // (notice that it is possible for all three to be -1 if, for example,
1907 // there is only one pentuple-apostrophe in the line)
1908 $arr[$firstspace] = "''";
1909 $arr[$firstspace - 1] .= "'";
1913 // Now let's actually convert our apostrophic mush to HTML!
1918 foreach ( $arr as $r ) {
1919 if ( ( $i %
2 ) == 0 ) {
1920 if ( $state === 'both' ) {
1926 $thislen = strlen( $r );
1927 if ( $thislen == 2 ) {
1928 if ( $state === 'i' ) {
1931 } elseif ( $state === 'bi' ) {
1934 } elseif ( $state === 'ib' ) {
1935 $output .= '</b></i><b>';
1937 } elseif ( $state === 'both' ) {
1938 $output .= '<b><i>' . $buffer . '</i>';
1940 } else { // $state can be 'b' or ''
1944 } elseif ( $thislen == 3 ) {
1945 if ( $state === 'b' ) {
1948 } elseif ( $state === 'bi' ) {
1949 $output .= '</i></b><i>';
1951 } elseif ( $state === 'ib' ) {
1954 } elseif ( $state === 'both' ) {
1955 $output .= '<i><b>' . $buffer . '</b>';
1957 } else { // $state can be 'i' or ''
1961 } elseif ( $thislen == 5 ) {
1962 if ( $state === 'b' ) {
1963 $output .= '</b><i>';
1965 } elseif ( $state === 'i' ) {
1966 $output .= '</i><b>';
1968 } elseif ( $state === 'bi' ) {
1969 $output .= '</i></b>';
1971 } elseif ( $state === 'ib' ) {
1972 $output .= '</b></i>';
1974 } elseif ( $state === 'both' ) {
1975 $output .= '<i><b>' . $buffer . '</b></i>';
1977 } else { // ($state == '')
1985 // Now close all remaining tags. Notice that the order is important.
1986 if ( $state === 'b' ||
$state === 'ib' ) {
1989 if ( $state === 'i' ||
$state === 'bi' ||
$state === 'ib' ) {
1992 if ( $state === 'bi' ) {
1995 // There might be lonely ''''', so make sure we have a buffer
1996 if ( $state === 'both' && $buffer ) {
1997 $output .= '<b><i>' . $buffer . '</i></b>';
2003 * Replace external links (REL)
2005 * Note: this is all very hackish and the order of execution matters a lot.
2006 * Make sure to run tests/parser/parserTests.php if you change this code.
2010 * @param string $text
2012 * @throws MWException
2015 public function replaceExternalLinks( $text ) {
2016 $bits = preg_split( $this->mExtLinkBracketedRegex
, $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
2017 // @phan-suppress-next-line PhanTypeComparisonFromArray See phan issue #3161
2018 if ( $bits === false ) {
2019 throw new MWException( "PCRE needs to be compiled with "
2020 . "--enable-unicode-properties in order for MediaWiki to function" );
2022 $s = array_shift( $bits );
2025 while ( $i < count( $bits ) ) {
2028 $text = $bits[$i++
];
2029 $trail = $bits[$i++
];
2031 # The characters '<' and '>' (which were escaped by
2032 # removeHTMLtags()) should not be included in
2033 # URLs, per RFC 2396.
2035 if ( preg_match( '/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE
) ) {
2036 $text = substr( $url, $m2[0][1] ) . ' ' . $text;
2037 $url = substr( $url, 0, $m2[0][1] );
2040 # If the link text is an image URL, replace it with an <img> tag
2041 # This happened by accident in the original parser, but some people used it extensively
2042 $img = $this->maybeMakeExternalImage( $text );
2043 if ( $img !== false ) {
2049 # Set linktype for CSS
2052 # No link text, e.g. [http://domain.tld/some.link]
2053 if ( $text == '' ) {
2055 $langObj = $this->getTargetLanguage();
2056 $text = '[' . $langObj->formatNum( ++
$this->mAutonumber
) . ']';
2057 $linktype = 'autonumber';
2059 # Have link text, e.g. [http://domain.tld/some.link text]s
2061 list( $dtrail, $trail ) = Linker
::splitTrail( $trail );
2064 // Excluding protocol-relative URLs may avoid many false positives.
2065 if ( preg_match( '/^(?:' . wfUrlProtocolsWithoutProtRel() . ')/', $text ) ) {
2066 $text = $this->getTargetLanguage()->getConverter()->markNoConversion( $text );
2069 $url = Sanitizer
::cleanUrl( $url );
2071 # Use the encoded URL
2072 # This means that users can paste URLs directly into the text
2073 # Funny characters like ö aren't valid in URLs anyway
2074 # This was changed in August 2004
2075 $s .= Linker
::makeExternalLink( $url, $text, false, $linktype,
2076 $this->getExternalLinkAttribs( $url ), $this->getTitle() ) . $dtrail . $trail;
2078 # Register link in the output object.
2079 $this->mOutput
->addExternalLink( $url );
2086 * Get the rel attribute for a particular external link.
2089 * @param string|bool $url Optional URL, to extract the domain from for rel =>
2090 * nofollow if appropriate
2091 * @param LinkTarget|null $title Optional LinkTarget, for wgNoFollowNsExceptions lookups
2092 * @return string|null Rel attribute for $url
2094 public static function getExternalLinkRel( $url = false, $title = null ) {
2095 global $wgNoFollowLinks, $wgNoFollowNsExceptions, $wgNoFollowDomainExceptions;
2096 $ns = $title ?
$title->getNamespace() : false;
2097 if ( $wgNoFollowLinks && !in_array( $ns, $wgNoFollowNsExceptions )
2098 && !wfMatchesDomainList( $url, $wgNoFollowDomainExceptions )
2106 * Get an associative array of additional HTML attributes appropriate for a
2107 * particular external link. This currently may include rel => nofollow
2108 * (depending on configuration, namespace, and the URL's domain) and/or a
2109 * target attribute (depending on configuration).
2111 * @param string $url URL to extract the domain from for rel =>
2112 * nofollow if appropriate
2113 * @return array Associative array of HTML attributes
2115 public function getExternalLinkAttribs( $url ) {
2117 $rel = self
::getExternalLinkRel( $url, $this->getTitle() );
2119 $target = $this->mOptions
->getExternalLinkTarget();
2121 $attribs['target'] = $target;
2122 if ( !in_array( $target, [ '_self', '_parent', '_top' ] ) ) {
2123 // T133507. New windows can navigate parent cross-origin.
2124 // Including noreferrer due to lacking browser
2125 // support of noopener. Eventually noreferrer should be removed.
2126 if ( $rel !== '' ) {
2129 $rel .= 'noreferrer noopener';
2132 $attribs['rel'] = $rel;
2137 * Replace unusual escape codes in a URL with their equivalent characters
2139 * This generally follows the syntax defined in RFC 3986, with special
2140 * consideration for HTTP query strings.
2142 * @param string $url
2145 public static function normalizeLinkUrl( $url ) {
2146 # Test for RFC 3986 IPv6 syntax
2147 $scheme = '[a-z][a-z0-9+.-]*:';
2148 $userinfo = '(?:[a-z0-9\-._~!$&\'()*+,;=:]|%[0-9a-f]{2})*';
2149 $ipv6Host = '\\[((?:[0-9a-f:]|%3[0-A]|%[46][1-6])+)\\]';
2150 if ( preg_match( "<^(?:{$scheme})?//(?:{$userinfo}@)?{$ipv6Host}(?:[:/?#].*|)$>i", $url, $m ) &&
2151 IP
::isValid( rawurldecode( $m[1] ) )
2153 $isIPv6 = rawurldecode( $m[1] );
2158 # Make sure unsafe characters are encoded
2159 $url = preg_replace_callback( '/[\x00-\x20"<>\[\\\\\]^`{|}\x7F-\xFF]/',
2161 return rawurlencode( $m[0] );
2167 $end = strlen( $url );
2169 # Fragment part - 'fragment'
2170 $start = strpos( $url, '#' );
2171 if ( $start !== false && $start < $end ) {
2172 $ret = self
::normalizeUrlComponent(
2173 substr( $url, $start, $end - $start ), '"#%<>[\]^`{|}' ) . $ret;
2177 # Query part - 'query' minus &=+;
2178 $start = strpos( $url, '?' );
2179 if ( $start !== false && $start < $end ) {
2180 $ret = self
::normalizeUrlComponent(
2181 substr( $url, $start, $end - $start ), '"#%<>[\]^`{|}&=+;' ) . $ret;
2185 # Scheme and path part - 'pchar'
2186 # (we assume no userinfo or encoded colons in the host)
2187 $ret = self
::normalizeUrlComponent(
2188 substr( $url, 0, $end ), '"#%<>[\]^`{|}/?' ) . $ret;
2191 if ( $isIPv6 !== false ) {
2192 $ipv6Host = "%5B({$isIPv6})%5D";
2193 $ret = preg_replace(
2194 "<^((?:{$scheme})?//(?:{$userinfo}@)?){$ipv6Host}(?=[:/?#]|$)>i",
2203 private static function normalizeUrlComponent( $component, $unsafe ) {
2204 $callback = function ( $matches ) use ( $unsafe ) {
2205 $char = urldecode( $matches[0] );
2206 $ord = ord( $char );
2207 if ( $ord > 32 && $ord < 127 && strpos( $unsafe, $char ) === false ) {
2211 # Leave it escaped, but use uppercase for a-f
2212 return strtoupper( $matches[0] );
2215 return preg_replace_callback( '/%[0-9A-Fa-f]{2}/', $callback, $component );
2219 * make an image if it's allowed, either through the global
2220 * option, through the exception, or through the on-wiki whitelist
2222 * @param string $url
2226 private function maybeMakeExternalImage( $url ) {
2227 $imagesfrom = $this->mOptions
->getAllowExternalImagesFrom();
2228 $imagesexception = !empty( $imagesfrom );
2230 # $imagesfrom could be either a single string or an array of strings, parse out the latter
2231 if ( $imagesexception && is_array( $imagesfrom ) ) {
2232 $imagematch = false;
2233 foreach ( $imagesfrom as $match ) {
2234 if ( strpos( $url, $match ) === 0 ) {
2239 } elseif ( $imagesexception ) {
2240 $imagematch = ( strpos( $url, $imagesfrom ) === 0 );
2242 $imagematch = false;
2245 if ( $this->mOptions
->getAllowExternalImages()
2246 ||
( $imagesexception && $imagematch )
2248 if ( preg_match( self
::EXT_IMAGE_REGEX
, $url ) ) {
2250 $text = Linker
::makeExternalImage( $url );
2253 if ( !$text && $this->mOptions
->getEnableImageWhitelist()
2254 && preg_match( self
::EXT_IMAGE_REGEX
, $url )
2256 $whitelist = explode(
2258 wfMessage( 'external_image_whitelist' )->inContentLanguage()->text()
2261 foreach ( $whitelist as $entry ) {
2262 # Sanitize the regex fragment, make it case-insensitive, ignore blank entries/comments
2263 if ( strpos( $entry, '#' ) === 0 ||
$entry === '' ) {
2266 if ( preg_match( '/' . str_replace( '/', '\\/', $entry ) . '/i', $url ) ) {
2267 # Image matches a whitelist entry
2268 $text = Linker
::makeExternalImage( $url );
2277 * Process [[ ]] wikilinks
2281 * @return string Processed text
2285 public function replaceInternalLinks( $s ) {
2286 $this->mLinkHolders
->merge( $this->replaceInternalLinks2( $s ) );
2291 * Process [[ ]] wikilinks (RIL)
2293 * @return LinkHolderArray
2297 public function replaceInternalLinks2( &$s ) {
2298 static $tc = false, $e1, $e1_img;
2299 # the % is needed to support urlencoded titles as well
2301 $tc = Title
::legalChars() . '#%';
2302 # Match a link having the form [[namespace:link|alternate]]trail
2303 $e1 = "/^([{$tc}]+)(?:\\|(.+?))?]](.*)\$/sD";
2304 # Match cases where there is no "]]", which might still be images
2305 $e1_img = "/^([{$tc}]+)\\|(.*)\$/sD";
2308 $holders = new LinkHolderArray( $this );
2310 # split the entire text string on occurrences of [[
2311 $a = StringUtils
::explode( '[[', ' ' . $s );
2312 # get the first element (all text up to first [[), and remove the space we added
2315 $line = $a->current(); # Workaround for broken ArrayIterator::next() that returns "void"
2316 $s = substr( $s, 1 );
2318 $nottalk = !$this->getTitle()->isTalkPage();
2320 $useLinkPrefixExtension = $this->getTargetLanguage()->linkPrefixExtension();
2322 if ( $useLinkPrefixExtension ) {
2323 # Match the end of a line for a word that's not followed by whitespace,
2324 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
2325 $charset = $this->contLang
->linkPrefixCharset();
2326 $e2 = "/^((?>.*[^$charset]|))(.+)$/sDu";
2328 if ( preg_match( $e2, $s, $m ) ) {
2329 $first_prefix = $m[2];
2331 $first_prefix = false;
2337 $useSubpages = $this->areSubpagesAllowed();
2339 # Loop for each link
2340 for ( ; $line !== false && $line !== null; $a->next(), $line = $a->current() ) {
2341 # Check for excessive memory usage
2342 if ( $holders->isBig() ) {
2344 # Do the existence check, replace the link holders and clear the array
2345 $holders->replace( $s );
2349 if ( $useLinkPrefixExtension ) {
2350 if ( preg_match( $e2, $s, $m ) ) {
2351 list( , $s, $prefix ) = $m;
2356 if ( $first_prefix ) {
2357 $prefix = $first_prefix;
2358 $first_prefix = false;
2362 $might_be_img = false;
2364 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
2366 # If we get a ] at the beginning of $m[3] that means we have a link that's something like:
2367 # [[Image:Foo.jpg|[http://example.com desc]]] <- having three ] in a row fucks up,
2368 # the real problem is with the $e1 regex
2370 # Still some problems for cases where the ] is meant to be outside punctuation,
2371 # and no image is in sight. See T4095.
2373 && substr( $m[3], 0, 1 ) === ']'
2374 && strpos( $text, '[' ) !== false
2376 $text .= ']'; # so that replaceExternalLinks($text) works later
2377 $m[3] = substr( $m[3], 1 );
2379 # fix up urlencoded title texts
2380 if ( strpos( $m[1], '%' ) !== false ) {
2381 # Should anchors '#' also be rejected?
2382 $m[1] = str_replace( [ '<', '>' ], [ '<', '>' ], rawurldecode( $m[1] ) );
2385 } elseif ( preg_match( $e1_img, $line, $m ) ) {
2386 # Invalid, but might be an image with a link in its caption
2387 $might_be_img = true;
2389 if ( strpos( $m[1], '%' ) !== false ) {
2390 $m[1] = str_replace( [ '<', '>' ], [ '<', '>' ], rawurldecode( $m[1] ) );
2393 } else { # Invalid form; output directly
2394 $s .= $prefix . '[[' . $line;
2398 $origLink = ltrim( $m[1], ' ' );
2400 # Don't allow internal links to pages containing
2401 # PROTO: where PROTO is a valid URL protocol; these
2402 # should be external links.
2403 if ( preg_match( '/^(?i:' . $this->mUrlProtocols
. ')/', $origLink ) ) {
2404 $s .= $prefix . '[[' . $line;
2408 # Make subpage if necessary
2409 if ( $useSubpages ) {
2410 $link = $this->maybeDoSubpageLink( $origLink, $text );
2415 // \x7f isn't a default legal title char, so most likely strip
2416 // markers will force us into the "invalid form" path above. But,
2417 // just in case, let's assert that xmlish tags aren't valid in
2418 // the title position.
2419 $unstrip = $this->mStripState
->killMarkers( $link );
2420 $noMarkers = ( $unstrip === $link );
2422 $nt = $noMarkers ? Title
::newFromText( $link ) : null;
2423 if ( $nt === null ) {
2424 $s .= $prefix . '[[' . $line;
2428 $ns = $nt->getNamespace();
2429 $iw = $nt->getInterwiki();
2431 $noforce = ( substr( $origLink, 0, 1 ) !== ':' );
2433 if ( $might_be_img ) { # if this is actually an invalid link
2434 if ( $ns == NS_FILE
&& $noforce ) { # but might be an image
2437 # look at the next 'line' to see if we can close it there
2439 $next_line = $a->current();
2440 if ( $next_line === false ||
$next_line === null ) {
2443 $m = explode( ']]', $next_line, 3 );
2444 if ( count( $m ) == 3 ) {
2445 # the first ]] closes the inner link, the second the image
2447 $text .= "[[{$m[0]}]]{$m[1]}";
2450 } elseif ( count( $m ) == 2 ) {
2451 # if there's exactly one ]] that's fine, we'll keep looking
2452 $text .= "[[{$m[0]}]]{$m[1]}";
2454 # if $next_line is invalid too, we need look no further
2455 $text .= '[[' . $next_line;
2460 # we couldn't find the end of this imageLink, so output it raw
2461 # but don't ignore what might be perfectly normal links in the text we've examined
2462 $holders->merge( $this->replaceInternalLinks2( $text ) );
2463 $s .= "{$prefix}[[$link|$text";
2464 # note: no $trail, because without an end, there *is* no trail
2467 } else { # it's not an image, so output it raw
2468 $s .= "{$prefix}[[$link|$text";
2469 # note: no $trail, because without an end, there *is* no trail
2474 $wasblank = ( $text == '' );
2478 # Strip off leading ':'
2479 $text = substr( $text, 1 );
2482 # T6598 madness. Handle the quotes only if they come from the alternate part
2483 # [[Lista d''e paise d''o munno]] -> <a href="...">Lista d''e paise d''o munno</a>
2484 # [[Criticism of Harry Potter|Criticism of ''Harry Potter'']]
2485 # -> <a href="Criticism of Harry Potter">Criticism of <i>Harry Potter</i></a>
2486 $text = $this->doQuotes( $text );
2489 # Link not escaped by : , create the various objects
2490 if ( $noforce && !$nt->wasLocalInterwiki() ) {
2493 $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && (
2494 Language
::fetchLanguageName( $iw, null, 'mw' ) ||
2495 in_array( $iw, $this->svcOptions
->get( 'ExtraInterlanguageLinkPrefixes' ) )
2498 # T26502: filter duplicates
2499 if ( !isset( $this->mLangLinkLanguages
[$iw] ) ) {
2500 $this->mLangLinkLanguages
[$iw] = true;
2501 $this->mOutput
->addLanguageLink( $nt->getFullText() );
2505 * Strip the whitespace interwiki links produce, see T10897
2507 $s = rtrim( $s . $prefix ) . $trail; # T175416
2511 if ( $ns == NS_FILE
) {
2512 if ( !$this->badFileLookup
->isBadFile( $nt->getDBkey(), $this->getTitle() ) ) {
2514 # if no parameters were passed, $text
2515 # becomes something like "File:Foo.png",
2516 # which we don't want to pass on to the
2520 # recursively parse links inside the image caption
2521 # actually, this will parse them in any other parameters, too,
2522 # but it might be hard to fix that, and it doesn't matter ATM
2523 $text = $this->replaceExternalLinks( $text );
2524 $holders->merge( $this->replaceInternalLinks2( $text ) );
2526 # cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them
2527 $s .= $prefix . $this->armorLinks(
2528 $this->makeImage( $nt, $text, $holders ) ) . $trail;
2531 } elseif ( $ns == NS_CATEGORY
) {
2533 * Strip the whitespace Category links produce, see T2087
2535 $s = rtrim( $s . $prefix ) . $trail; # T2087, T87753
2538 $sortkey = $this->getDefaultSort();
2542 $sortkey = Sanitizer
::decodeCharReferences( $sortkey );
2543 $sortkey = str_replace( "\n", '', $sortkey );
2544 $sortkey = $this->getTargetLanguage()->convertCategoryKey( $sortkey );
2545 $this->mOutput
->addCategory( $nt->getDBkey(), $sortkey );
2551 # Self-link checking. For some languages, variants of the title are checked in
2552 # LinkHolderArray::doVariants() to allow batching the existence checks necessary
2553 # for linking to a different variant.
2554 if ( $ns != NS_SPECIAL
&& $nt->equals( $this->getTitle() ) && !$nt->hasFragment() ) {
2555 $s .= $prefix . Linker
::makeSelfLinkObj( $nt, $text, '', $trail );
2559 # NS_MEDIA is a pseudo-namespace for linking directly to a file
2560 # @todo FIXME: Should do batch file existence checks, see comment below
2561 if ( $ns == NS_MEDIA
) {
2562 # Give extensions a chance to select the file revision for us
2565 Hooks
::run( 'BeforeParserFetchFileAndTitle',
2566 [ $this, $nt, &$options, &$descQuery ] );
2567 # Fetch and register the file (file title may be different via hooks)
2568 list( $file, $nt ) = $this->fetchFileAndTitle( $nt, $options );
2569 # Cloak with NOPARSE to avoid replacement in replaceExternalLinks
2570 $s .= $prefix . $this->armorLinks(
2571 Linker
::makeMediaLinkFile( $nt, $file, $text ) ) . $trail;
2575 # Some titles, such as valid special pages or files in foreign repos, should
2576 # be shown as bluelinks even though they're not included in the page table
2577 # @todo FIXME: isAlwaysKnown() can be expensive for file links; we should really do
2578 # batch file existence checks for NS_FILE and NS_MEDIA
2579 if ( $iw == '' && $nt->isAlwaysKnown() ) {
2580 $this->mOutput
->addLink( $nt );
2581 $s .= $this->makeKnownLinkHolder( $nt, $text, $trail, $prefix );
2583 # Links will be added to the output link list after checking
2584 $s .= $holders->makeHolder( $nt, $text, [], $trail, $prefix );
2591 * Render a forced-blue link inline; protect against double expansion of
2592 * URLs if we're in a mode that prepends full URL prefixes to internal links.
2593 * Since this little disaster has to split off the trail text to avoid
2594 * breaking URLs in the following text without breaking trails on the
2595 * wiki links, it's been made into a horrible function.
2598 * @param string $text
2599 * @param string $trail
2600 * @param string $prefix
2601 * @return string HTML-wikitext mix oh yuck
2603 protected function makeKnownLinkHolder( $nt, $text = '', $trail = '', $prefix = '' ) {
2604 list( $inside, $trail ) = Linker
::splitTrail( $trail );
2606 if ( $text == '' ) {
2607 $text = htmlspecialchars( $nt->getPrefixedText() );
2610 $link = $this->getLinkRenderer()->makeKnownLink(
2611 $nt, new HtmlArmor( "$prefix$text$inside" )
2614 return $this->armorLinks( $link ) . $trail;
2618 * Insert a NOPARSE hacky thing into any inline links in a chunk that's
2619 * going to go through further parsing steps before inline URL expansion.
2621 * Not needed quite as much as it used to be since free links are a bit
2622 * more sensible these days. But bracketed links are still an issue.
2624 * @param string $text More-or-less HTML
2625 * @return string Less-or-more HTML with NOPARSE bits
2627 public function armorLinks( $text ) {
2628 return preg_replace( '/\b((?i)' . $this->mUrlProtocols
. ')/',
2629 self
::MARKER_PREFIX
. "NOPARSE$1", $text );
2633 * Return true if subpage links should be expanded on this page.
2636 public function areSubpagesAllowed() {
2637 # Some namespaces don't allow subpages
2638 return $this->nsInfo
->hasSubpages( $this->getTitle()->getNamespace() );
2642 * Handle link to subpage if necessary
2644 * @param string $target The source of the link
2645 * @param string &$text The link text, modified as necessary
2646 * @return string The full name of the link
2649 public function maybeDoSubpageLink( $target, &$text ) {
2650 return Linker
::normalizeSubpageLink( $this->getTitle(), $target, $text );
2654 * Make lists from lines starting with ':', '*', '#', etc. (DBL)
2656 * @param string $text
2657 * @param bool $linestart Whether or not this is at the start of a line.
2659 * @return string The lists rendered as HTML
2661 public function doBlockLevels( $text, $linestart ) {
2662 return BlockLevelPass
::doBlockLevels( $text, $linestart );
2666 * Return value of a magic variable (like PAGENAME)
2670 * @param string $index Magic variable identifier as mapped in MagicWordFactory::$mVariableIDs
2671 * @param bool|PPFrame $frame
2675 public function getVariableValue( $index, $frame = false ) {
2676 // Avoid PHP 7.1 warning from passing $this by reference
2680 * Some of these require message or data lookups and can be
2681 * expensive to check many times.
2684 Hooks
::run( 'ParserGetVariableValueVarCache', [ &$parser, &$this->mVarCache
] ) &&
2685 isset( $this->mVarCache
[$index] )
2687 return $this->mVarCache
[$index];
2690 $ts = wfTimestamp( TS_UNIX
, $this->mOptions
->getTimestamp() );
2691 Hooks
::run( 'ParserGetVariableValueTs', [ &$parser, &$ts ] );
2693 $pageLang = $this->getFunctionLang();
2699 case 'currentmonth':
2700 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'm' ), true );
2702 case 'currentmonth1':
2703 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'n' ), true );
2705 case 'currentmonthname':
2706 $value = $pageLang->getMonthName( MWTimestamp
::getInstance( $ts )->format( 'n' ) );
2708 case 'currentmonthnamegen':
2709 $value = $pageLang->getMonthNameGen( MWTimestamp
::getInstance( $ts )->format( 'n' ) );
2711 case 'currentmonthabbrev':
2712 $value = $pageLang->getMonthAbbreviation( MWTimestamp
::getInstance( $ts )->format( 'n' ) );
2715 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'j' ), true );
2718 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'd' ), true );
2721 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'm' ), true );
2724 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'n' ), true );
2726 case 'localmonthname':
2727 $value = $pageLang->getMonthName( MWTimestamp
::getLocalInstance( $ts )->format( 'n' ) );
2729 case 'localmonthnamegen':
2730 $value = $pageLang->getMonthNameGen( MWTimestamp
::getLocalInstance( $ts )->format( 'n' ) );
2732 case 'localmonthabbrev':
2733 $value = $pageLang->getMonthAbbreviation( MWTimestamp
::getLocalInstance( $ts )->format( 'n' ) );
2736 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'j' ), true );
2739 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'd' ), true );
2742 $value = wfEscapeWikiText( $this->getTitle()->getText() );
2745 $value = wfEscapeWikiText( $this->getTitle()->getPartialURL() );
2747 case 'fullpagename':
2748 $value = wfEscapeWikiText( $this->getTitle()->getPrefixedText() );
2750 case 'fullpagenamee':
2751 $value = wfEscapeWikiText( $this->getTitle()->getPrefixedURL() );
2754 $value = wfEscapeWikiText( $this->getTitle()->getSubpageText() );
2756 case 'subpagenamee':
2757 $value = wfEscapeWikiText( $this->getTitle()->getSubpageUrlForm() );
2759 case 'rootpagename':
2760 $value = wfEscapeWikiText( $this->getTitle()->getRootText() );
2762 case 'rootpagenamee':
2763 $value = wfEscapeWikiText( wfUrlencode( str_replace(
2766 $this->getTitle()->getRootText()
2769 case 'basepagename':
2770 $value = wfEscapeWikiText( $this->getTitle()->getBaseText() );
2772 case 'basepagenamee':
2773 $value = wfEscapeWikiText( wfUrlencode( str_replace(
2776 $this->getTitle()->getBaseText()
2779 case 'talkpagename':
2780 if ( $this->getTitle()->canHaveTalkPage() ) {
2781 $talkPage = $this->getTitle()->getTalkPage();
2782 $value = wfEscapeWikiText( $talkPage->getPrefixedText() );
2787 case 'talkpagenamee':
2788 if ( $this->getTitle()->canHaveTalkPage() ) {
2789 $talkPage = $this->getTitle()->getTalkPage();
2790 $value = wfEscapeWikiText( $talkPage->getPrefixedURL() );
2795 case 'subjectpagename':
2796 $subjPage = $this->getTitle()->getSubjectPage();
2797 $value = wfEscapeWikiText( $subjPage->getPrefixedText() );
2799 case 'subjectpagenamee':
2800 $subjPage = $this->getTitle()->getSubjectPage();
2801 $value = wfEscapeWikiText( $subjPage->getPrefixedURL() );
2803 case 'pageid': // requested in T25427
2804 # Inform the edit saving system that getting the canonical output
2805 # after page insertion requires a parse that used that exact page ID
2806 $this->setOutputFlag( 'vary-page-id', '{{PAGEID}} used' );
2807 $value = $this->getTitle()->getArticleID();
2809 $value = $this->mOptions
->getSpeculativePageId();
2811 $this->mOutput
->setSpeculativePageIdUsed( $value );
2817 $this->svcOptions
->get( 'MiserMode' ) &&
2818 !$this->mOptions
->getInterfaceMessage() &&
2819 // @TODO: disallow this word on all namespaces
2820 $this->nsInfo
->isContent( $this->getTitle()->getNamespace() )
2822 // Use a stub result instead of the actual revision ID in order to avoid
2823 // double parses on page save but still allow preview detection (T137900)
2824 if ( $this->getRevisionId() ||
$this->mOptions
->getSpeculativeRevId() ) {
2827 $this->setOutputFlag( 'vary-revision-exists', '{{REVISIONID}} used' );
2831 # Inform the edit saving system that getting the canonical output after
2832 # revision insertion requires a parse that used that exact revision ID
2833 $this->setOutputFlag( 'vary-revision-id', '{{REVISIONID}} used' );
2834 $value = $this->getRevisionId();
2835 if ( $value === 0 ) {
2836 $rev = $this->getRevisionObject();
2837 $value = $rev ?
$rev->getId() : $value;
2840 $value = $this->mOptions
->getSpeculativeRevId();
2842 $this->mOutput
->setSpeculativeRevIdUsed( $value );
2848 $value = (int)$this->getRevisionTimestampSubstring( 6, 2, self
::MAX_TTS
, $index );
2850 case 'revisionday2':
2851 $value = $this->getRevisionTimestampSubstring( 6, 2, self
::MAX_TTS
, $index );
2853 case 'revisionmonth':
2854 $value = $this->getRevisionTimestampSubstring( 4, 2, self
::MAX_TTS
, $index );
2856 case 'revisionmonth1':
2857 $value = (int)$this->getRevisionTimestampSubstring( 4, 2, self
::MAX_TTS
, $index );
2859 case 'revisionyear':
2860 $value = $this->getRevisionTimestampSubstring( 0, 4, self
::MAX_TTS
, $index );
2862 case 'revisiontimestamp':
2863 $value = $this->getRevisionTimestampSubstring( 0, 14, self
::MAX_TTS
, $index );
2865 case 'revisionuser':
2866 # Inform the edit saving system that getting the canonical output after
2867 # revision insertion requires a parse that used the actual user ID
2868 $this->setOutputFlag( 'vary-user', '{{REVISIONUSER}} used' );
2869 $value = $this->getRevisionUser();
2871 case 'revisionsize':
2872 $value = $this->getRevisionSize();
2875 $value = str_replace( '_', ' ',
2876 $this->contLang
->getNsText( $this->getTitle()->getNamespace() ) );
2879 $value = wfUrlencode( $this->contLang
->getNsText( $this->getTitle()->getNamespace() ) );
2881 case 'namespacenumber':
2882 $value = $this->getTitle()->getNamespace();
2885 $value = $this->getTitle()->canHaveTalkPage()
2886 ?
str_replace( '_', ' ', $this->getTitle()->getTalkNsText() )
2890 $value = $this->getTitle()->canHaveTalkPage()
2891 ?
wfUrlencode( $this->getTitle()->getTalkNsText() )
2894 case 'subjectspace':
2895 $value = str_replace( '_', ' ', $this->getTitle()->getSubjectNsText() );
2897 case 'subjectspacee':
2898 $value = ( wfUrlencode( $this->getTitle()->getSubjectNsText() ) );
2900 case 'currentdayname':
2901 $value = $pageLang->getWeekdayName( (int)MWTimestamp
::getInstance( $ts )->format( 'w' ) +
1 );
2904 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'Y' ), true );
2907 $value = $pageLang->time( wfTimestamp( TS_MW
, $ts ), false, false );
2910 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'H' ), true );
2913 # @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
2914 # int to remove the padding
2915 $value = $pageLang->formatNum( (int)MWTimestamp
::getInstance( $ts )->format( 'W' ) );
2918 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'w' ) );
2920 case 'localdayname':
2921 $value = $pageLang->getWeekdayName(
2922 (int)MWTimestamp
::getLocalInstance( $ts )->format( 'w' ) +
1
2926 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'Y' ), true );
2929 $value = $pageLang->time(
2930 MWTimestamp
::getLocalInstance( $ts )->format( 'YmdHis' ),
2936 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'H' ), true );
2939 # @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
2940 # int to remove the padding
2941 $value = $pageLang->formatNum( (int)MWTimestamp
::getLocalInstance( $ts )->format( 'W' ) );
2944 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'w' ) );
2946 case 'numberofarticles':
2947 $value = $pageLang->formatNum( SiteStats
::articles() );
2949 case 'numberoffiles':
2950 $value = $pageLang->formatNum( SiteStats
::images() );
2952 case 'numberofusers':
2953 $value = $pageLang->formatNum( SiteStats
::users() );
2955 case 'numberofactiveusers':
2956 $value = $pageLang->formatNum( SiteStats
::activeUsers() );
2958 case 'numberofpages':
2959 $value = $pageLang->formatNum( SiteStats
::pages() );
2961 case 'numberofadmins':
2962 $value = $pageLang->formatNum( SiteStats
::numberingroup( 'sysop' ) );
2964 case 'numberofedits':
2965 $value = $pageLang->formatNum( SiteStats
::edits() );
2967 case 'currenttimestamp':
2968 $value = wfTimestamp( TS_MW
, $ts );
2970 case 'localtimestamp':
2971 $value = MWTimestamp
::getLocalInstance( $ts )->format( 'YmdHis' );
2973 case 'currentversion':
2974 $value = SpecialVersion
::getVersion();
2977 return $this->svcOptions
->get( 'ArticlePath' );
2979 return $this->svcOptions
->get( 'Sitename' );
2981 return $this->svcOptions
->get( 'Server' );
2983 return $this->svcOptions
->get( 'ServerName' );
2985 return $this->svcOptions
->get( 'ScriptPath' );
2987 return $this->svcOptions
->get( 'StylePath' );
2988 case 'directionmark':
2989 return $pageLang->getDirMark();
2990 case 'contentlanguage':
2991 return $this->svcOptions
->get( 'LanguageCode' );
2992 case 'pagelanguage':
2993 $value = $pageLang->getCode();
2995 case 'cascadingsources':
2996 $value = CoreParserFunctions
::cascadingsources( $this );
3001 'ParserGetVariableValueSwitch',
3002 [ &$parser, &$this->mVarCache
, &$index, &$ret, &$frame ]
3009 $this->mVarCache
[$index] = $value;
3018 * @param int $mtts Max time-till-save; sets vary-revision-timestamp if result changes by then
3019 * @param string $variable Parser variable name
3022 private function getRevisionTimestampSubstring( $start, $len, $mtts, $variable ) {
3023 # Get the timezone-adjusted timestamp to be used for this revision
3024 $resNow = substr( $this->getRevisionTimestamp(), $start, $len );
3025 # Possibly set vary-revision if there is not yet an associated revision
3026 if ( !$this->getRevisionObject() ) {
3027 # Get the timezone-adjusted timestamp $mtts seconds in the future.
3028 # This future is relative to the current time and not that of the
3029 # parser options. The rendered timestamp can be compared to that
3030 # of the timestamp specified by the parser options.
3032 $this->contLang
->userAdjust( wfTimestamp( TS_MW
, time() +
$mtts ), '' ),
3037 if ( $resNow !== $resThen ) {
3038 # Inform the edit saving system that getting the canonical output after
3039 # revision insertion requires a parse that used an actual revision timestamp
3040 $this->setOutputFlag( 'vary-revision-timestamp', "$variable used" );
3048 * initialise the magic variables (like CURRENTMONTHNAME) and substitution modifiers
3052 public function initialiseVariables() {
3053 $variableIDs = $this->magicWordFactory
->getVariableIDs();
3054 $substIDs = $this->magicWordFactory
->getSubstIDs();
3056 $this->mVariables
= $this->magicWordFactory
->newArray( $variableIDs );
3057 $this->mSubstWords
= $this->magicWordFactory
->newArray( $substIDs );
3061 * Preprocess some wikitext and return the document tree.
3062 * This is the ghost of replace_variables().
3064 * @param string $text The text to parse
3065 * @param int $flags Bitwise combination of:
3066 * - self::PTD_FOR_INCLUSION: Handle "<noinclude>" and "<includeonly>" as if the text is being
3067 * included. Default is to assume a direct page view.
3069 * The generated DOM tree must depend only on the input text and the flags.
3070 * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of T6899.
3072 * Any flag added to the $flags parameter here, or any other parameter liable to cause a
3073 * change in the DOM tree for a given text, must be passed through the section identifier
3074 * in the section edit link and thus back to extractSections().
3076 * The output of this function is currently only cached in process memory, but a persistent
3077 * cache may be implemented at a later date which takes further advantage of these strict
3078 * dependency requirements.
3082 public function preprocessToDom( $text, $flags = 0 ) {
3083 $dom = $this->getPreprocessor()->preprocessToObj( $text, $flags );
3088 * Return a three-element array: leading whitespace, string contents, trailing whitespace
3094 public static function splitWhitespace( $s ) {
3095 $ltrimmed = ltrim( $s );
3096 $w1 = substr( $s, 0, strlen( $s ) - strlen( $ltrimmed ) );
3097 $trimmed = rtrim( $ltrimmed );
3098 $diff = strlen( $ltrimmed ) - strlen( $trimmed );
3100 $w2 = substr( $ltrimmed, -$diff );
3104 return [ $w1, $trimmed, $w2 ];
3108 * Replace magic variables, templates, and template arguments
3109 * with the appropriate text. Templates are substituted recursively,
3110 * taking care to avoid infinite loops.
3112 * Note that the substitution depends on value of $mOutputType:
3113 * self::OT_WIKI: only {{subst:}} templates
3114 * self::OT_PREPROCESS: templates but not extension tags
3115 * self::OT_HTML: all templates and extension tags
3117 * @param string $text The text to transform
3118 * @param false|PPFrame|array $frame Object describing the arguments passed to the
3119 * template. Arguments may also be provided as an associative array, as
3120 * was the usual case before MW1.12. Providing arguments this way may be
3121 * useful for extensions wishing to perform variable replacement
3123 * @param bool $argsOnly Only do argument (triple-brace) expansion, not
3124 * double-brace expansion.
3127 public function replaceVariables( $text, $frame = false, $argsOnly = false ) {
3128 # Is there any text? Also, Prevent too big inclusions!
3129 $textSize = strlen( $text );
3130 if ( $textSize < 1 ||
$textSize > $this->mOptions
->getMaxIncludeSize() ) {
3134 if ( $frame === false ) {
3135 $frame = $this->getPreprocessor()->newFrame();
3136 } elseif ( !( $frame instanceof PPFrame
) ) {
3137 $this->logger
->debug(
3138 __METHOD__
. " called using plain parameters instead of " .
3139 "a PPFrame instance. Creating custom frame."
3141 $frame = $this->getPreprocessor()->newCustomFrame( $frame );
3144 $dom = $this->preprocessToDom( $text );
3145 $flags = $argsOnly ? PPFrame
::NO_TEMPLATES
: 0;
3146 $text = $frame->expand( $dom, $flags );
3152 * Clean up argument array - refactored in 1.9 so parserfunctions can use it, too.
3154 * @param array $args
3158 public static function createAssocArgs( $args ) {
3161 foreach ( $args as $arg ) {
3162 $eqpos = strpos( $arg, '=' );
3163 if ( $eqpos === false ) {
3164 $assocArgs[$index++
] = $arg;
3166 $name = trim( substr( $arg, 0, $eqpos ) );
3167 $value = trim( substr( $arg, $eqpos +
1 ) );
3168 if ( $value === false ) {
3171 if ( $name !== false ) {
3172 $assocArgs[$name] = $value;
3181 * Warn the user when a parser limitation is reached
3182 * Will warn at most once the user per limitation type
3184 * The results are shown during preview and run through the Parser (See EditPage.php)
3186 * @param string $limitationType Should be one of:
3187 * 'expensive-parserfunction' (corresponding messages:
3188 * 'expensive-parserfunction-warning',
3189 * 'expensive-parserfunction-category')
3190 * 'post-expand-template-argument' (corresponding messages:
3191 * 'post-expand-template-argument-warning',
3192 * 'post-expand-template-argument-category')
3193 * 'post-expand-template-inclusion' (corresponding messages:
3194 * 'post-expand-template-inclusion-warning',
3195 * 'post-expand-template-inclusion-category')
3196 * 'node-count-exceeded' (corresponding messages:
3197 * 'node-count-exceeded-warning',
3198 * 'node-count-exceeded-category')
3199 * 'expansion-depth-exceeded' (corresponding messages:
3200 * 'expansion-depth-exceeded-warning',
3201 * 'expansion-depth-exceeded-category')
3202 * @param string|int|null $current Current value
3203 * @param string|int|null $max Maximum allowed, when an explicit limit has been
3204 * exceeded, provide the values (optional)
3206 public function limitationWarn( $limitationType, $current = '', $max = '' ) {
3207 # does no harm if $current and $max are present but are unnecessary for the message
3208 # Not doing ->inLanguage( $this->mOptions->getUserLangObj() ), since this is shown
3209 # only during preview, and that would split the parser cache unnecessarily.
3210 $warning = wfMessage( "$limitationType-warning" )->numParams( $current, $max )
3212 $this->mOutput
->addWarning( $warning );
3213 $this->addTrackingCategory( "$limitationType-category" );
3217 * Return the text of a template, after recursively
3218 * replacing any variables or templates within the template.
3220 * @param array $piece The parts of the template
3221 * $piece['title']: the title, i.e. the part before the |
3222 * $piece['parts']: the parameter array
3223 * $piece['lineStart']: whether the brace was at the start of a line
3224 * @param PPFrame $frame The current frame, contains template arguments
3226 * @return string|array The text of the template
3228 public function braceSubstitution( $piece, $frame ) {
3231 // $text has been filled
3233 // wiki markup in $text should be escaped
3235 // $text is HTML, armour it against wikitext transformation
3237 // Force interwiki transclusion to be done in raw mode not rendered
3238 $forceRawInterwiki = false;
3239 // $text is a DOM node needing expansion in a child frame
3240 $isChildObj = false;
3241 // $text is a DOM node needing expansion in the current frame
3242 $isLocalObj = false;
3244 # Title object, where $text came from
3247 # $part1 is the bit before the first |, and must contain only title characters.
3248 # Various prefixes will be stripped from it later.
3249 $titleWithSpaces = $frame->expand( $piece['title'] );
3250 $part1 = trim( $titleWithSpaces );
3253 # Original title text preserved for various purposes
3254 $originalTitle = $part1;
3256 # $args is a list of argument nodes, starting from index 0, not including $part1
3257 # @todo FIXME: If piece['parts'] is null then the call to getLength()
3258 # below won't work b/c this $args isn't an object
3259 $args = ( $piece['parts'] == null ) ?
[] : $piece['parts'];
3261 $profileSection = null; // profile templates
3265 $substMatch = $this->mSubstWords
->matchStartAndRemove( $part1 );
3267 # Possibilities for substMatch: "subst", "safesubst" or FALSE
3268 # Decide whether to expand template or keep wikitext as-is.
3269 if ( $this->ot
['wiki'] ) {
3270 if ( $substMatch === false ) {
3271 $literal = true; # literal when in PST with no prefix
3273 $literal = false; # expand when in PST with subst: or safesubst:
3276 if ( $substMatch == 'subst' ) {
3277 $literal = true; # literal when not in PST with plain subst:
3279 $literal = false; # expand when not in PST with safesubst: or no prefix
3283 $text = $frame->virtualBracketedImplode( '{{', '|', '}}', $titleWithSpaces, $args );
3290 if ( !$found && $args->getLength() == 0 ) {
3291 $id = $this->mVariables
->matchStartToEnd( $part1 );
3292 if ( $id !== false ) {
3293 $text = $this->getVariableValue( $id, $frame );
3294 if ( $this->magicWordFactory
->getCacheTTL( $id ) > -1 ) {
3295 $this->mOutput
->updateCacheExpiry(
3296 $this->magicWordFactory
->getCacheTTL( $id ) );
3302 # MSG, MSGNW and RAW
3305 $mwMsgnw = $this->magicWordFactory
->get( 'msgnw' );
3306 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
3309 # Remove obsolete MSG:
3310 $mwMsg = $this->magicWordFactory
->get( 'msg' );
3311 $mwMsg->matchStartAndRemove( $part1 );
3315 $mwRaw = $this->magicWordFactory
->get( 'raw' );
3316 if ( $mwRaw->matchStartAndRemove( $part1 ) ) {
3317 $forceRawInterwiki = true;
3323 $colonPos = strpos( $part1, ':' );
3324 if ( $colonPos !== false ) {
3325 $func = substr( $part1, 0, $colonPos );
3326 $funcArgs = [ trim( substr( $part1, $colonPos +
1 ) ) ];
3327 $argsLength = $args->getLength();
3328 for ( $i = 0; $i < $argsLength; $i++
) {
3329 $funcArgs[] = $args->item( $i );
3332 $result = $this->callParserFunction( $frame, $func, $funcArgs );
3334 // Extract any forwarded flags
3335 if ( isset( $result['title'] ) ) {
3336 $title = $result['title'];
3338 if ( isset( $result['found'] ) ) {
3339 $found = $result['found'];
3341 if ( array_key_exists( 'text', $result ) ) {
3343 $text = $result['text'];
3345 if ( isset( $result['nowiki'] ) ) {
3346 $nowiki = $result['nowiki'];
3348 if ( isset( $result['isHTML'] ) ) {
3349 $isHTML = $result['isHTML'];
3351 if ( isset( $result['forceRawInterwiki'] ) ) {
3352 $forceRawInterwiki = $result['forceRawInterwiki'];
3354 if ( isset( $result['isChildObj'] ) ) {
3355 $isChildObj = $result['isChildObj'];
3357 if ( isset( $result['isLocalObj'] ) ) {
3358 $isLocalObj = $result['isLocalObj'];
3363 # Finish mangling title and then check for loops.
3364 # Set $title to a Title object and $titleText to the PDBK
3367 # Split the title into page and subpage
3369 $relative = $this->maybeDoSubpageLink( $part1, $subpage );
3370 if ( $part1 !== $relative ) {
3372 $ns = $this->getTitle()->getNamespace();
3374 $title = Title
::newFromText( $part1, $ns );
3376 $titleText = $title->getPrefixedText();
3377 # Check for language variants if the template is not found
3378 if ( $this->getTargetLanguage()->hasVariants() && $title->getArticleID() == 0 ) {
3379 $this->getTargetLanguage()->findVariantLink( $part1, $title, true );
3381 # Do recursion depth check
3382 $limit = $this->mOptions
->getMaxTemplateDepth();
3383 if ( $frame->depth
>= $limit ) {
3385 $text = '<span class="error">'
3386 . wfMessage( 'parser-template-recursion-depth-warning' )
3387 ->numParams( $limit )->inContentLanguage()->text()
3393 # Load from database
3394 if ( !$found && $title ) {
3395 $profileSection = $this->mProfiler
->scopedProfileIn( $title->getPrefixedDBkey() );
3396 if ( !$title->isExternal() ) {
3397 if ( $title->isSpecialPage()
3398 && $this->mOptions
->getAllowSpecialInclusion()
3399 && $this->ot
['html']
3401 $specialPage = $this->specialPageFactory
->getPage( $title->getDBkey() );
3402 // Pass the template arguments as URL parameters.
3403 // "uselang" will have no effect since the Language object
3404 // is forced to the one defined in ParserOptions.
3406 $argsLength = $args->getLength();
3407 for ( $i = 0; $i < $argsLength; $i++
) {
3408 $bits = $args->item( $i )->splitArg();
3409 if ( strval( $bits['index'] ) === '' ) {
3410 $name = trim( $frame->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
3411 $value = trim( $frame->expand( $bits['value'] ) );
3412 $pageArgs[$name] = $value;
3416 // Create a new context to execute the special page
3417 $context = new RequestContext
;
3418 $context->setTitle( $title );
3419 $context->setRequest( new FauxRequest( $pageArgs ) );
3420 if ( $specialPage && $specialPage->maxIncludeCacheTime() === 0 ) {
3421 $context->setUser( $this->getUser() );
3423 // If this page is cached, then we better not be per user.
3424 $context->setUser( User
::newFromName( '127.0.0.1', false ) );
3426 $context->setLanguage( $this->mOptions
->getUserLangObj() );
3427 $ret = $this->specialPageFactory
->capturePath( $title, $context, $this->getLinkRenderer() );
3429 $text = $context->getOutput()->getHTML();
3430 $this->mOutput
->addOutputPageMetadata( $context->getOutput() );
3433 if ( $specialPage && $specialPage->maxIncludeCacheTime() !== false ) {
3434 $this->mOutput
->updateRuntimeAdaptiveExpiry(
3435 $specialPage->maxIncludeCacheTime()
3439 } elseif ( $this->nsInfo
->isNonincludable( $title->getNamespace() ) ) {
3440 $found = false; # access denied
3441 $this->logger
->debug(
3443 ": template inclusion denied for " . $title->getPrefixedDBkey()
3446 list( $text, $title ) = $this->getTemplateDom( $title );
3447 if ( $text !== false ) {
3453 # If the title is valid but undisplayable, make a link to it
3454 if ( !$found && ( $this->ot
['html'] ||
$this->ot
['pre'] ) ) {
3455 $text = "[[:$titleText]]";
3458 } elseif ( $title->isTrans() ) {
3459 # Interwiki transclusion
3460 if ( $this->ot
['html'] && !$forceRawInterwiki ) {
3461 $text = $this->interwikiTransclude( $title, 'render' );
3464 $text = $this->interwikiTransclude( $title, 'raw' );
3465 # Preprocess it like a template
3466 $text = $this->preprocessToDom( $text, self
::PTD_FOR_INCLUSION
);
3472 # Do infinite loop check
3473 # This has to be done after redirect resolution to avoid infinite loops via redirects
3474 if ( !$frame->loopCheck( $title ) ) {
3476 $text = '<span class="error">'
3477 . wfMessage( 'parser-template-loop-warning', $titleText )->inContentLanguage()->text()
3479 $this->addTrackingCategory( 'template-loop-category' );
3480 $this->mOutput
->addWarning( wfMessage( 'template-loop-warning',
3481 wfEscapeWikiText( $titleText ) )->text() );
3482 $this->logger
->debug( __METHOD__
. ": template loop broken at '$titleText'" );
3486 # If we haven't found text to substitute by now, we're done
3487 # Recover the source wikitext and return it
3489 $text = $frame->virtualBracketedImplode( '{{', '|', '}}', $titleWithSpaces, $args );
3490 if ( $profileSection ) {
3491 $this->mProfiler
->scopedProfileOut( $profileSection );
3493 return [ 'object' => $text ];
3496 # Expand DOM-style return values in a child frame
3497 if ( $isChildObj ) {
3498 # Clean up argument array
3499 $newFrame = $frame->newChild( $args, $title );
3502 $text = $newFrame->expand( $text, PPFrame
::RECOVER_ORIG
);
3503 } elseif ( $titleText !== false && $newFrame->isEmpty() ) {
3504 # Expansion is eligible for the empty-frame cache
3505 $text = $newFrame->cachedExpand( $titleText, $text );
3507 # Uncached expansion
3508 $text = $newFrame->expand( $text );
3511 if ( $isLocalObj && $nowiki ) {
3512 $text = $frame->expand( $text, PPFrame
::RECOVER_ORIG
);
3513 $isLocalObj = false;
3516 if ( $profileSection ) {
3517 $this->mProfiler
->scopedProfileOut( $profileSection );
3520 # Replace raw HTML by a placeholder
3522 $text = $this->insertStripItem( $text );
3523 } elseif ( $nowiki && ( $this->ot
['html'] ||
$this->ot
['pre'] ) ) {
3524 # Escape nowiki-style return values
3525 $text = wfEscapeWikiText( $text );
3526 } elseif ( is_string( $text )
3527 && !$piece['lineStart']
3528 && preg_match( '/^(?:{\\||:|;|#|\*)/', $text )
3530 # T2529: if the template begins with a table or block-level
3531 # element, it should be treated as beginning a new line.
3532 # This behavior is somewhat controversial.
3533 $text = "\n" . $text;
3536 if ( is_string( $text ) && !$this->incrementIncludeSize( 'post-expand', strlen( $text ) ) ) {
3537 # Error, oversize inclusion
3538 if ( $titleText !== false ) {
3539 # Make a working, properly escaped link if possible (T25588)
3540 $text = "[[:$titleText]]";
3542 # This will probably not be a working link, but at least it may
3543 # provide some hint of where the problem is
3544 preg_replace( '/^:/', '', $originalTitle );
3545 $text = "[[:$originalTitle]]";
3547 $text .= $this->insertStripItem( '<!-- WARNING: template omitted, '
3548 . 'post-expand include size too large -->' );
3549 $this->limitationWarn( 'post-expand-template-inclusion' );
3552 if ( $isLocalObj ) {
3553 $ret = [ 'object' => $text ];
3555 $ret = [ 'text' => $text ];
3562 * Call a parser function and return an array with text and flags.
3564 * The returned array will always contain a boolean 'found', indicating
3565 * whether the parser function was found or not. It may also contain the
3567 * text: string|object, resulting wikitext or PP DOM object
3568 * isHTML: bool, $text is HTML, armour it against wikitext transformation
3569 * isChildObj: bool, $text is a DOM node needing expansion in a child frame
3570 * isLocalObj: bool, $text is a DOM node needing expansion in the current frame
3571 * nowiki: bool, wiki markup in $text should be escaped
3574 * @param PPFrame $frame The current frame, contains template arguments
3575 * @param string $function Function name
3576 * @param array $args Arguments to the function
3579 public function callParserFunction( $frame, $function, array $args = [] ) {
3580 # Case sensitive functions
3581 if ( isset( $this->mFunctionSynonyms
[1][$function] ) ) {
3582 $function = $this->mFunctionSynonyms
[1][$function];
3584 # Case insensitive functions
3585 $function = $this->contLang
->lc( $function );
3586 if ( isset( $this->mFunctionSynonyms
[0][$function] ) ) {
3587 $function = $this->mFunctionSynonyms
[0][$function];
3589 return [ 'found' => false ];
3593 list( $callback, $flags ) = $this->mFunctionHooks
[$function];
3595 // Avoid PHP 7.1 warning from passing $this by reference
3598 $allArgs = [ &$parser ];
3599 if ( $flags & self
::SFH_OBJECT_ARGS
) {
3600 # Convert arguments to PPNodes and collect for appending to $allArgs
3602 foreach ( $args as $k => $v ) {
3603 if ( $v instanceof PPNode ||
$k === 0 ) {
3606 $funcArgs[] = $this->mPreprocessor
->newPartNodeArray( [ $k => $v ] )->item( 0 );
3610 # Add a frame parameter, and pass the arguments as an array
3611 $allArgs[] = $frame;
3612 $allArgs[] = $funcArgs;
3614 # Convert arguments to plain text and append to $allArgs
3615 foreach ( $args as $k => $v ) {
3616 if ( $v instanceof PPNode
) {
3617 $allArgs[] = trim( $frame->expand( $v ) );
3618 } elseif ( is_int( $k ) && $k >= 0 ) {
3619 $allArgs[] = trim( $v );
3621 $allArgs[] = trim( "$k=$v" );
3626 $result = $callback( ...$allArgs );
3628 # The interface for function hooks allows them to return a wikitext
3629 # string or an array containing the string and any flags. This mungs
3630 # things around to match what this method should return.
3631 if ( !is_array( $result ) ) {
3637 if ( isset( $result[0] ) && !isset( $result['text'] ) ) {
3638 $result['text'] = $result[0];
3640 unset( $result[0] );
3647 $preprocessFlags = 0;
3648 if ( isset( $result['noparse'] ) ) {
3649 $noparse = $result['noparse'];
3651 if ( isset( $result['preprocessFlags'] ) ) {
3652 $preprocessFlags = $result['preprocessFlags'];
3656 $result['text'] = $this->preprocessToDom( $result['text'], $preprocessFlags );
3657 $result['isChildObj'] = true;
3664 * Get the semi-parsed DOM representation of a template with a given title,
3665 * and its redirect destination title. Cached.
3667 * @param Title $title
3671 public function getTemplateDom( $title ) {
3672 $cacheTitle = $title;
3673 $titleText = $title->getPrefixedDBkey();
3675 if ( isset( $this->mTplRedirCache
[$titleText] ) ) {
3676 list( $ns, $dbk ) = $this->mTplRedirCache
[$titleText];
3677 $title = Title
::makeTitle( $ns, $dbk );
3678 $titleText = $title->getPrefixedDBkey();
3680 if ( isset( $this->mTplDomCache
[$titleText] ) ) {
3681 return [ $this->mTplDomCache
[$titleText], $title ];
3684 # Cache miss, go to the database
3685 list( $text, $title ) = $this->fetchTemplateAndTitle( $title );
3687 if ( $text === false ) {
3688 $this->mTplDomCache
[$titleText] = false;
3689 return [ false, $title ];
3692 $dom = $this->preprocessToDom( $text, self
::PTD_FOR_INCLUSION
);
3693 $this->mTplDomCache
[$titleText] = $dom;
3695 if ( !$title->equals( $cacheTitle ) ) {
3696 $this->mTplRedirCache
[$cacheTitle->getPrefixedDBkey()] =
3697 [ $title->getNamespace(), $title->getDBkey() ];
3700 return [ $dom, $title ];
3704 * Fetch the current revision of a given title. Note that the revision
3705 * (and even the title) may not exist in the database, so everything
3706 * contributing to the output of the parser should use this method
3707 * where possible, rather than getting the revisions themselves. This
3708 * method also caches its results, so using it benefits performance.
3711 * @param Title $title
3714 public function fetchCurrentRevisionOfTitle( $title ) {
3715 $cacheKey = $title->getPrefixedDBkey();
3716 if ( !$this->currentRevisionCache
) {
3717 $this->currentRevisionCache
= new MapCacheLRU( 100 );
3719 if ( !$this->currentRevisionCache
->has( $cacheKey ) ) {
3720 $this->currentRevisionCache
->set( $cacheKey,
3721 // Defaults to Parser::statelessFetchRevision()
3722 call_user_func( $this->mOptions
->getCurrentRevisionCallback(), $title, $this )
3725 return $this->currentRevisionCache
->get( $cacheKey );
3729 * @param Title $title
3733 public function isCurrentRevisionOfTitleCached( $title ) {
3735 $this->currentRevisionCache
&&
3736 $this->currentRevisionCache
->has( $title->getPrefixedText() )
3741 * Wrapper around Revision::newFromTitle to allow passing additional parameters
3742 * without passing them on to it.
3745 * @param Title $title
3746 * @param Parser|bool $parser
3747 * @return Revision|bool False if missing
3749 public static function statelessFetchRevision( Title
$title, $parser = false ) {
3750 $rev = Revision
::newKnownCurrent( wfGetDB( DB_REPLICA
), $title );
3756 * Fetch the unparsed text of a template and register a reference to it.
3757 * @param Title $title
3758 * @return array ( string or false, Title )
3760 public function fetchTemplateAndTitle( $title ) {
3761 // Defaults to Parser::statelessFetchTemplate()
3762 $templateCb = $this->mOptions
->getTemplateCallback();
3763 $stuff = call_user_func( $templateCb, $title, $this );
3764 $rev = $stuff['revision'] ??
null;
3765 $text = $stuff['text'];
3766 if ( is_string( $stuff['text'] ) ) {
3767 // We use U+007F DELETE to distinguish strip markers from regular text
3768 $text = strtr( $text, "\x7f", "?" );
3770 $finalTitle = $stuff['finalTitle'] ??
$title;
3771 foreach ( ( $stuff['deps'] ??
[] ) as $dep ) {
3772 $this->mOutput
->addTemplate( $dep['title'], $dep['page_id'], $dep['rev_id'] );
3773 if ( $dep['title']->equals( $this->getTitle() ) && $rev instanceof Revision
) {
3774 // Self-transclusion; final result may change based on the new page version
3775 $this->setOutputFlag( 'vary-revision-sha1', 'Self transclusion' );
3776 $this->getOutput()->setRevisionUsedSha1Base36( $rev->getSha1() );
3780 return [ $text, $finalTitle ];
3784 * Fetch the unparsed text of a template and register a reference to it.
3785 * @param Title $title
3786 * @return string|bool
3788 public function fetchTemplate( $title ) {
3789 return $this->fetchTemplateAndTitle( $title )[0];
3793 * Static function to get a template
3794 * Can be overridden via ParserOptions::setTemplateCallback().
3796 * @param Title $title
3797 * @param bool|Parser $parser
3801 public static function statelessFetchTemplate( $title, $parser = false ) {
3802 $text = $skip = false;
3803 $finalTitle = $title;
3807 # Loop to fetch the article, with up to 1 redirect
3808 for ( $i = 0; $i < 2 && is_object( $title ); $i++
) {
3809 # Give extensions a chance to select the revision instead
3810 $id = false; # Assume current
3811 Hooks
::run( 'BeforeParserFetchTemplateAndtitle',
3812 [ $parser, $title, &$skip, &$id ] );
3818 'page_id' => $title->getArticleID(),
3825 $rev = Revision
::newFromId( $id );
3826 } elseif ( $parser ) {
3827 $rev = $parser->fetchCurrentRevisionOfTitle( $title );
3829 $rev = Revision
::newFromTitle( $title );
3831 $rev_id = $rev ?
$rev->getId() : 0;
3832 # If there is no current revision, there is no page
3833 if ( $id === false && !$rev ) {
3834 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
3835 $linkCache->addBadLinkObj( $title );
3840 'page_id' => $title->getArticleID(),
3843 if ( $rev && !$title->equals( $rev->getTitle() ) ) {
3844 # We fetched a rev from a different title; register it too...
3846 'title' => $rev->getTitle(),
3847 'page_id' => $rev->getPage(),
3853 $content = $rev->getContent();
3854 $text = $content ?
$content->getWikitextForTransclusion() : null;
3856 Hooks
::run( 'ParserFetchTemplate',
3857 [ $parser, $title, $rev, &$text, &$deps ] );
3859 if ( $text === false ||
$text === null ) {
3863 } elseif ( $title->getNamespace() == NS_MEDIAWIKI
) {
3864 $message = wfMessage( MediaWikiServices
::getInstance()->getContentLanguage()->
3865 lcfirst( $title->getText() ) )->inContentLanguage();
3866 if ( !$message->exists() ) {
3870 $content = $message->content();
3871 $text = $message->plain();
3879 $finalTitle = $title;
3880 $title = $content->getRedirectTarget();
3885 'finalTitle' => $finalTitle,
3891 * Fetch a file and its title and register a reference to it.
3892 * If 'broken' is a key in $options then the file will appear as a broken thumbnail.
3893 * @param Title $title
3894 * @param array $options Array of options to RepoGroup::findFile
3895 * @return array ( File or false, Title of file )
3897 public function fetchFileAndTitle( $title, $options = [] ) {
3898 $file = $this->fetchFileNoRegister( $title, $options );
3900 $time = $file ?
$file->getTimestamp() : false;
3901 $sha1 = $file ?
$file->getSha1() : false;
3902 # Register the file as a dependency...
3903 $this->mOutput
->addImage( $title->getDBkey(), $time, $sha1 );
3904 if ( $file && !$title->equals( $file->getTitle() ) ) {
3905 # Update fetched file title
3906 $title = $file->getTitle();
3907 $this->mOutput
->addImage( $title->getDBkey(), $time, $sha1 );
3909 return [ $file, $title ];
3913 * Helper function for fetchFileAndTitle.
3915 * Also useful if you need to fetch a file but not use it yet,
3916 * for example to get the file's handler.
3918 * @param Title $title
3919 * @param array $options Array of options to RepoGroup::findFile
3922 protected function fetchFileNoRegister( $title, $options = [] ) {
3923 if ( isset( $options['broken'] ) ) {
3924 $file = false; // broken thumbnail forced by hook
3925 } elseif ( isset( $options['sha1'] ) ) { // get by (sha1,timestamp)
3926 $file = RepoGroup
::singleton()->findFileFromKey( $options['sha1'], $options );
3927 } else { // get by (name,timestamp)
3928 $file = MediaWikiServices
::getInstance()->getRepoGroup()->findFile( $title, $options );
3934 * Transclude an interwiki link.
3936 * @param Title $title
3937 * @param string $action Usually one of (raw, render)
3941 public function interwikiTransclude( $title, $action ) {
3942 if ( !$this->svcOptions
->get( 'EnableScaryTranscluding' ) ) {
3943 return wfMessage( 'scarytranscludedisabled' )->inContentLanguage()->text();
3946 $url = $title->getFullURL( [ 'action' => $action ] );
3947 if ( strlen( $url ) > 1024 ) {
3948 return wfMessage( 'scarytranscludetoolong' )->inContentLanguage()->text();
3951 $wikiId = $title->getTransWikiID(); // remote wiki ID or false
3953 $fname = __METHOD__
;
3954 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
3956 $data = $cache->getWithSetCallback(
3957 $cache->makeGlobalKey(
3958 'interwiki-transclude',
3959 ( $wikiId !== false ) ?
$wikiId : 'external',
3962 $this->svcOptions
->get( 'TranscludeCacheExpiry' ),
3963 function ( $oldValue, &$ttl ) use ( $url, $fname, $cache ) {
3964 $req = MWHttpRequest
::factory( $url, [], $fname );
3966 $status = $req->execute(); // Status object
3967 if ( !$status->isOK() ) {
3968 $ttl = $cache::TTL_UNCACHEABLE
;
3969 } elseif ( $req->getResponseHeader( 'X-Database-Lagged' ) !== null ) {
3970 $ttl = min( $cache::TTL_LAGGED
, $ttl );
3974 'text' => $status->isOK() ?
$req->getContent() : null,
3975 'code' => $req->getStatus()
3979 'checkKeys' => ( $wikiId !== false )
3980 ?
[ $cache->makeGlobalKey( 'interwiki-page', $wikiId, $title->getDBkey() ) ]
3982 'pcGroup' => 'interwiki-transclude:5',
3983 'pcTTL' => $cache::TTL_PROC_LONG
3987 if ( is_string( $data['text'] ) ) {
3988 $text = $data['text'];
3989 } elseif ( $data['code'] != 200 ) {
3990 // Though we failed to fetch the content, this status is useless.
3991 $text = wfMessage( 'scarytranscludefailed-httpstatus' )
3992 ->params( $url, $data['code'] )->inContentLanguage()->text();
3994 $text = wfMessage( 'scarytranscludefailed', $url )->inContentLanguage()->text();
4001 * Triple brace replacement -- used for template arguments
4004 * @param array $piece
4005 * @param PPFrame $frame
4009 public function argSubstitution( $piece, $frame ) {
4011 $parts = $piece['parts'];
4012 $nameWithSpaces = $frame->expand( $piece['title'] );
4013 $argName = trim( $nameWithSpaces );
4015 $text = $frame->getArgument( $argName );
4016 if ( $text === false && $parts->getLength() > 0
4017 && ( $this->ot
['html']
4019 ||
( $this->ot
['wiki'] && $frame->isTemplate() )
4022 # No match in frame, use the supplied default
4023 $object = $parts->item( 0 )->getChildren();
4025 if ( !$this->incrementIncludeSize( 'arg', strlen( $text ) ) ) {
4026 $error = '<!-- WARNING: argument omitted, expansion size too large -->';
4027 $this->limitationWarn( 'post-expand-template-argument' );
4030 if ( $text === false && $object === false ) {
4032 $object = $frame->virtualBracketedImplode( '{{{', '|', '}}}', $nameWithSpaces, $parts );
4034 if ( $error !== false ) {
4037 if ( $object !== false ) {
4038 $ret = [ 'object' => $object ];
4040 $ret = [ 'text' => $text ];
4047 * Return the text to be used for a given extension tag.
4048 * This is the ghost of strip().
4050 * @param array $params Associative array of parameters:
4051 * name PPNode for the tag name
4052 * attr PPNode for unparsed text where tag attributes are thought to be
4053 * attributes Optional associative array of parsed attributes
4054 * inner Contents of extension element
4055 * noClose Original text did not have a close tag
4056 * @param PPFrame $frame
4058 * @throws MWException
4061 public function extensionSubstitution( $params, $frame ) {
4062 static $errorStr = '<span class="error">';
4063 static $errorLen = 20;
4065 $name = $frame->expand( $params['name'] );
4066 if ( substr( $name, 0, $errorLen ) === $errorStr ) {
4067 // Probably expansion depth or node count exceeded. Just punt the
4072 $attrText = !isset( $params['attr'] ) ?
null : $frame->expand( $params['attr'] );
4073 if ( substr( $attrText, 0, $errorLen ) === $errorStr ) {
4078 // We can't safely check if the expansion for $content resulted in an
4079 // error, because the content could happen to be the error string
4081 $content = !isset( $params['inner'] ) ?
null : $frame->expand( $params['inner'] );
4083 $marker = self
::MARKER_PREFIX
. "-$name-"
4084 . sprintf( '%08X', $this->mMarkerIndex++
) . self
::MARKER_SUFFIX
;
4086 $isFunctionTag = isset( $this->mFunctionTagHooks
[strtolower( $name )] ) &&
4087 ( $this->ot
['html'] ||
$this->ot
['pre'] );
4088 if ( $isFunctionTag ) {
4089 $markerType = 'none';
4091 $markerType = 'general';
4093 if ( $this->ot
['html'] ||
$isFunctionTag ) {
4094 $name = strtolower( $name );
4095 $attributes = Sanitizer
::decodeTagAttributes( $attrText );
4096 if ( isset( $params['attributes'] ) ) {
4097 $attributes +
= $params['attributes'];
4100 if ( isset( $this->mTagHooks
[$name] ) ) {
4101 $output = call_user_func_array( $this->mTagHooks
[$name],
4102 [ $content, $attributes, $this, $frame ] );
4103 } elseif ( isset( $this->mFunctionTagHooks
[$name] ) ) {
4104 list( $callback, ) = $this->mFunctionTagHooks
[$name];
4106 // Avoid PHP 7.1 warning from passing $this by reference
4108 $output = call_user_func_array( $callback, [ &$parser, $frame, $content, $attributes ] );
4110 $output = '<span class="error">Invalid tag extension name: ' .
4111 htmlspecialchars( $name ) . '</span>';
4114 if ( is_array( $output ) ) {
4117 $output = $flags[0];
4118 if ( isset( $flags['markerType'] ) ) {
4119 $markerType = $flags['markerType'];
4123 if ( is_null( $attrText ) ) {
4126 if ( isset( $params['attributes'] ) ) {
4127 foreach ( $params['attributes'] as $attrName => $attrValue ) {
4128 $attrText .= ' ' . htmlspecialchars( $attrName ) . '="' .
4129 htmlspecialchars( $attrValue ) . '"';
4132 if ( $content === null ) {
4133 $output = "<$name$attrText/>";
4135 $close = is_null( $params['close'] ) ?
'' : $frame->expand( $params['close'] );
4136 if ( substr( $close, 0, $errorLen ) === $errorStr ) {
4140 $output = "<$name$attrText>$content$close";
4144 if ( $markerType === 'none' ) {
4146 } elseif ( $markerType === 'nowiki' ) {
4147 $this->mStripState
->addNoWiki( $marker, $output );
4148 } elseif ( $markerType === 'general' ) {
4149 $this->mStripState
->addGeneral( $marker, $output );
4151 throw new MWException( __METHOD__
. ': invalid marker type' );
4157 * Increment an include size counter
4159 * @param string $type The type of expansion
4160 * @param int $size The size of the text
4161 * @return bool False if this inclusion would take it over the maximum, true otherwise
4163 public function incrementIncludeSize( $type, $size ) {
4164 if ( $this->mIncludeSizes
[$type] +
$size > $this->mOptions
->getMaxIncludeSize() ) {
4167 $this->mIncludeSizes
[$type] +
= $size;
4173 * Increment the expensive function count
4175 * @return bool False if the limit has been exceeded
4177 public function incrementExpensiveFunctionCount() {
4178 $this->mExpensiveFunctionCount++
;
4179 return $this->mExpensiveFunctionCount
<= $this->mOptions
->getExpensiveParserFunctionLimit();
4183 * Strip double-underscore items like __NOGALLERY__ and __NOTOC__
4184 * Fills $this->mDoubleUnderscores, returns the modified text
4186 * @param string $text
4190 public function doDoubleUnderscore( $text ) {
4191 # The position of __TOC__ needs to be recorded
4192 $mw = $this->magicWordFactory
->get( 'toc' );
4193 if ( $mw->match( $text ) ) {
4194 $this->mShowToc
= true;
4195 $this->mForceTocPosition
= true;
4197 # Set a placeholder. At the end we'll fill it in with the TOC.
4198 $text = $mw->replace( '<!--MWTOC\'"-->', $text, 1 );
4200 # Only keep the first one.
4201 $text = $mw->replace( '', $text );
4204 # Now match and remove the rest of them
4205 $mwa = $this->magicWordFactory
->getDoubleUnderscoreArray();
4206 $this->mDoubleUnderscores
= $mwa->matchAndRemove( $text );
4208 if ( isset( $this->mDoubleUnderscores
['nogallery'] ) ) {
4209 $this->mOutput
->mNoGallery
= true;
4211 if ( isset( $this->mDoubleUnderscores
['notoc'] ) && !$this->mForceTocPosition
) {
4212 $this->mShowToc
= false;
4214 if ( isset( $this->mDoubleUnderscores
['hiddencat'] )
4215 && $this->getTitle()->getNamespace() == NS_CATEGORY
4217 $this->addTrackingCategory( 'hidden-category-category' );
4219 # (T10068) Allow control over whether robots index a page.
4220 # __INDEX__ always overrides __NOINDEX__, see T16899
4221 if ( isset( $this->mDoubleUnderscores
['noindex'] ) && $this->getTitle()->canUseNoindex() ) {
4222 $this->mOutput
->setIndexPolicy( 'noindex' );
4223 $this->addTrackingCategory( 'noindex-category' );
4225 if ( isset( $this->mDoubleUnderscores
['index'] ) && $this->getTitle()->canUseNoindex() ) {
4226 $this->mOutput
->setIndexPolicy( 'index' );
4227 $this->addTrackingCategory( 'index-category' );
4230 # Cache all double underscores in the database
4231 foreach ( $this->mDoubleUnderscores
as $key => $val ) {
4232 $this->mOutput
->setProperty( $key, '' );
4239 * @see ParserOutput::addTrackingCategory()
4240 * @param string $msg Message key
4241 * @return bool Whether the addition was successful
4243 public function addTrackingCategory( $msg ) {
4244 return $this->mOutput
->addTrackingCategory( $msg, $this->getTitle() );
4248 * This function accomplishes several tasks:
4249 * 1) Auto-number headings if that option is enabled
4250 * 2) Add an [edit] link to sections for users who have enabled the option and can edit the page
4251 * 3) Add a Table of contents on the top for users who have enabled the option
4252 * 4) Auto-anchor headings
4254 * It loops through all headlines, collects the necessary data, then splits up the
4255 * string and re-inserts the newly formatted headlines.
4257 * @param string $text
4258 * @param string $origText Original, untouched wikitext
4259 * @param bool $isMain
4260 * @return mixed|string
4263 public function formatHeadings( $text, $origText, $isMain = true ) {
4264 # Inhibit editsection links if requested in the page
4265 if ( isset( $this->mDoubleUnderscores
['noeditsection'] ) ) {
4266 $maybeShowEditLink = false;
4268 $maybeShowEditLink = true; /* Actual presence will depend on post-cache transforms */
4271 # Get all headlines for numbering them and adding funky stuff like [edit]
4272 # links - this is for later, but we need the number of headlines right now
4273 # NOTE: white space in headings have been trimmed in doHeadings. They shouldn't
4274 # be trimmed here since whitespace in HTML headings is significant.
4276 $numMatches = preg_match_all(
4277 '/<H(?P<level>[1-6])(?P<attrib>.*?>)(?P<header>[\s\S]*?)<\/H[1-6] *>/i',
4282 # if there are fewer than 4 headlines in the article, do not show TOC
4283 # unless it's been explicitly enabled.
4284 $enoughToc = $this->mShowToc
&&
4285 ( ( $numMatches >= 4 ) ||
$this->mForceTocPosition
);
4287 # Allow user to stipulate that a page should have a "new section"
4288 # link added via __NEWSECTIONLINK__
4289 if ( isset( $this->mDoubleUnderscores
['newsectionlink'] ) ) {
4290 $this->mOutput
->setNewSection( true );
4293 # Allow user to remove the "new section"
4294 # link via __NONEWSECTIONLINK__
4295 if ( isset( $this->mDoubleUnderscores
['nonewsectionlink'] ) ) {
4296 $this->mOutput
->hideNewSection( true );
4299 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
4300 # override above conditions and always show TOC above first header
4301 if ( isset( $this->mDoubleUnderscores
['forcetoc'] ) ) {
4302 $this->mShowToc
= true;
4310 # Ugh .. the TOC should have neat indentation levels which can be
4311 # passed to the skin functions. These are determined here
4315 $sublevelCount = [];
4321 $markerRegex = self
::MARKER_PREFIX
. "-h-(\d+)-" . self
::MARKER_SUFFIX
;
4322 $baseTitleText = $this->getTitle()->getPrefixedDBkey();
4323 $oldType = $this->mOutputType
;
4324 $this->setOutputType( self
::OT_WIKI
);
4325 $frame = $this->getPreprocessor()->newFrame();
4326 $root = $this->preprocessToDom( $origText );
4327 $node = $root->getFirstChild();
4332 $headlines = $numMatches !== false ?
$matches[3] : [];
4334 $maxTocLevel = $this->svcOptions
->get( 'MaxTocLevel' );
4335 foreach ( $headlines as $headline ) {
4336 $isTemplate = false;
4338 $sectionIndex = false;
4340 $markerMatches = [];
4341 if ( preg_match( "/^$markerRegex/", $headline, $markerMatches ) ) {
4342 $serial = $markerMatches[1];
4343 list( $titleText, $sectionIndex ) = $this->mHeadings
[$serial];
4344 $isTemplate = ( $titleText != $baseTitleText );
4345 $headline = preg_replace( "/^$markerRegex\\s*/", "", $headline );
4349 $prevlevel = $level;
4351 $level = $matches[1][$headlineCount];
4353 if ( $level > $prevlevel ) {
4354 # Increase TOC level
4356 $sublevelCount[$toclevel] = 0;
4357 if ( $toclevel < $maxTocLevel ) {
4358 $prevtoclevel = $toclevel;
4359 $toc .= Linker
::tocIndent();
4362 } elseif ( $level < $prevlevel && $toclevel > 1 ) {
4363 # Decrease TOC level, find level to jump to
4365 for ( $i = $toclevel; $i > 0; $i-- ) {
4366 if ( $levelCount[$i] == $level ) {
4367 # Found last matching level
4370 } elseif ( $levelCount[$i] < $level ) {
4371 # Found first matching level below current level
4379 if ( $toclevel < $maxTocLevel ) {
4380 if ( $prevtoclevel < $maxTocLevel ) {
4381 # Unindent only if the previous toc level was shown :p
4382 $toc .= Linker
::tocUnindent( $prevtoclevel - $toclevel );
4383 $prevtoclevel = $toclevel;
4385 $toc .= Linker
::tocLineEnd();
4389 # No change in level, end TOC line
4390 if ( $toclevel < $maxTocLevel ) {
4391 $toc .= Linker
::tocLineEnd();
4395 $levelCount[$toclevel] = $level;
4397 # count number of headlines for each level
4398 $sublevelCount[$toclevel]++
;
4400 for ( $i = 1; $i <= $toclevel; $i++
) {
4401 if ( !empty( $sublevelCount[$i] ) ) {
4405 $numbering .= $this->getTargetLanguage()->formatNum( $sublevelCount[$i] );
4410 # The safe header is a version of the header text safe to use for links
4412 # Remove link placeholders by the link text.
4413 # <!--LINK number-->
4415 # link text with suffix
4416 # Do this before unstrip since link text can contain strip markers
4417 $safeHeadline = $this->replaceLinkHoldersText( $headline );
4419 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
4420 $safeHeadline = $this->mStripState
->unstripBoth( $safeHeadline );
4422 # Remove any <style> or <script> tags (T198618)
4423 $safeHeadline = preg_replace(
4424 '#<(style|script)(?: [^>]*[^>/])?>.*?</\1>#is',
4429 # Strip out HTML (first regex removes any tag not allowed)
4431 # * <sup> and <sub> (T10393)
4435 # * <span dir="rtl"> and <span dir="ltr"> (T37167)
4436 # * <s> and <strike> (T35715)
4437 # We strip any parameter from accepted tags (second regex), except dir="rtl|ltr" from <span>,
4438 # to allow setting directionality in toc items.
4439 $tocline = preg_replace(
4441 '#<(?!/?(span|sup|sub|bdi|i|b|s|strike)(?: [^>]*)?>).*?>#',
4442 '#<(/?(?:span(?: dir="(?:rtl|ltr)")?|sup|sub|bdi|i|b|s|strike))(?: .*?)?>#'
4448 # Strip '<span></span>', which is the result from the above if
4449 # <span id="foo"></span> is used to produce an additional anchor
4451 $tocline = str_replace( '<span></span>', '', $tocline );
4453 $tocline = trim( $tocline );
4455 # For the anchor, strip out HTML-y stuff period
4456 $safeHeadline = preg_replace( '/<.*?>/', '', $safeHeadline );
4457 $safeHeadline = Sanitizer
::normalizeSectionNameWhitespace( $safeHeadline );
4459 # Save headline for section edit hint before it's escaped
4460 $headlineHint = $safeHeadline;
4462 # Decode HTML entities
4463 $safeHeadline = Sanitizer
::decodeCharReferences( $safeHeadline );
4465 $safeHeadline = self
::normalizeSectionName( $safeHeadline );
4467 $fallbackHeadline = Sanitizer
::escapeIdForAttribute( $safeHeadline, Sanitizer
::ID_FALLBACK
);
4468 $linkAnchor = Sanitizer
::escapeIdForLink( $safeHeadline );
4469 $safeHeadline = Sanitizer
::escapeIdForAttribute( $safeHeadline, Sanitizer
::ID_PRIMARY
);
4470 if ( $fallbackHeadline === $safeHeadline ) {
4471 # No reason to have both (in fact, we can't)
4472 $fallbackHeadline = false;
4475 # HTML IDs must be case-insensitively unique for IE compatibility (T12721).
4476 # @todo FIXME: We may be changing them depending on the current locale.
4477 $arrayKey = strtolower( $safeHeadline );
4478 if ( $fallbackHeadline === false ) {
4479 $fallbackArrayKey = false;
4481 $fallbackArrayKey = strtolower( $fallbackHeadline );
4484 # Create the anchor for linking from the TOC to the section
4485 $anchor = $safeHeadline;
4486 $fallbackAnchor = $fallbackHeadline;
4487 if ( isset( $refers[$arrayKey] ) ) {
4488 // phpcs:ignore Generic.Formatting.DisallowMultipleStatements
4489 for ( $i = 2; isset( $refers["${arrayKey}_$i"] ); ++
$i );
4491 $linkAnchor .= "_$i";
4492 $refers["${arrayKey}_$i"] = true;
4494 $refers[$arrayKey] = true;
4496 if ( $fallbackHeadline !== false && isset( $refers[$fallbackArrayKey] ) ) {
4497 // phpcs:ignore Generic.Formatting.DisallowMultipleStatements
4498 for ( $i = 2; isset( $refers["${fallbackArrayKey}_$i"] ); ++
$i );
4499 $fallbackAnchor .= "_$i";
4500 $refers["${fallbackArrayKey}_$i"] = true;
4502 $refers[$fallbackArrayKey] = true;
4505 # Don't number the heading if it is the only one (looks silly)
4506 if ( count( $matches[3] ) > 1 && $this->mOptions
->getNumberHeadings() ) {
4507 # the two are different if the line contains a link
4508 $headline = Html
::element(
4510 [ 'class' => 'mw-headline-number' ],
4512 ) . ' ' . $headline;
4515 if ( $enoughToc && ( !isset( $maxTocLevel ) ||
$toclevel < $maxTocLevel ) ) {
4516 $toc .= Linker
::tocLine( $linkAnchor, $tocline,
4517 $numbering, $toclevel, ( $isTemplate ?
false : $sectionIndex ) );
4520 # Add the section to the section tree
4521 # Find the DOM node for this header
4522 $noOffset = ( $isTemplate ||
$sectionIndex === false );
4523 while ( $node && !$noOffset ) {
4524 if ( $node->getName() === 'h' ) {
4525 $bits = $node->splitHeading();
4526 if ( $bits['i'] == $sectionIndex ) {
4530 $byteOffset +
= mb_strlen( $this->mStripState
->unstripBoth(
4531 $frame->expand( $node, PPFrame
::RECOVER_ORIG
) ) );
4532 $node = $node->getNextSibling();
4535 'toclevel' => $toclevel,
4538 'number' => $numbering,
4539 'index' => ( $isTemplate ?
'T-' : '' ) . $sectionIndex,
4540 'fromtitle' => $titleText,
4541 'byteoffset' => ( $noOffset ?
null : $byteOffset ),
4542 'anchor' => $anchor,
4545 # give headline the correct <h#> tag
4546 if ( $maybeShowEditLink && $sectionIndex !== false ) {
4547 // Output edit section links as markers with styles that can be customized by skins
4548 if ( $isTemplate ) {
4549 # Put a T flag in the section identifier, to indicate to extractSections()
4550 # that sections inside <includeonly> should be counted.
4551 $editsectionPage = $titleText;
4552 $editsectionSection = "T-$sectionIndex";
4553 $editsectionContent = null;
4555 $editsectionPage = $this->getTitle()->getPrefixedText();
4556 $editsectionSection = $sectionIndex;
4557 $editsectionContent = $headlineHint;
4559 // We use a bit of pesudo-xml for editsection markers. The
4560 // language converter is run later on. Using a UNIQ style marker
4561 // leads to the converter screwing up the tokens when it
4562 // converts stuff. And trying to insert strip tags fails too. At
4563 // this point all real inputted tags have already been escaped,
4564 // so we don't have to worry about a user trying to input one of
4565 // these markers directly. We use a page and section attribute
4566 // to stop the language converter from converting these
4567 // important bits of data, but put the headline hint inside a
4568 // content block because the language converter is supposed to
4569 // be able to convert that piece of data.
4570 // Gets replaced with html in ParserOutput::getText
4571 $editlink = '<mw:editsection page="' . htmlspecialchars( $editsectionPage );
4572 $editlink .= '" section="' . htmlspecialchars( $editsectionSection ) . '"';
4573 if ( $editsectionContent !== null ) {
4574 $editlink .= '>' . $editsectionContent . '</mw:editsection>';
4581 $head[$headlineCount] = Linker
::makeHeadline( $level,
4582 $matches['attrib'][$headlineCount], $anchor, $headline,
4583 $editlink, $fallbackAnchor );
4588 $this->setOutputType( $oldType );
4590 # Never ever show TOC if no headers
4591 if ( $numVisible < 1 ) {
4596 if ( $prevtoclevel > 0 && $prevtoclevel < $maxTocLevel ) {
4597 $toc .= Linker
::tocUnindent( $prevtoclevel - 1 );
4599 $toc = Linker
::tocList( $toc, $this->mOptions
->getUserLangObj() );
4600 $this->mOutput
->setTOCHTML( $toc );
4601 $toc = self
::TOC_START
. $toc . self
::TOC_END
;
4605 $this->mOutput
->setSections( $tocraw );
4608 # split up and insert constructed headlines
4609 $blocks = preg_split( '/<H[1-6].*?>[\s\S]*?<\/H[1-6]>/i', $text );
4612 // build an array of document sections
4614 foreach ( $blocks as $block ) {
4615 // $head is zero-based, sections aren't.
4616 if ( empty( $head[$i - 1] ) ) {
4617 $sections[$i] = $block;
4619 $sections[$i] = $head[$i - 1] . $block;
4623 * Send a hook, one per section.
4624 * The idea here is to be able to make section-level DIVs, but to do so in a
4625 * lower-impact, more correct way than r50769
4628 * $section : the section number
4629 * &$sectionContent : ref to the content of the section
4630 * $maybeShowEditLinks : boolean describing whether this section has an edit link
4632 Hooks
::run( 'ParserSectionCreate', [ $this, $i, &$sections[$i], $maybeShowEditLink ] );
4637 if ( $enoughToc && $isMain && !$this->mForceTocPosition
) {
4638 // append the TOC at the beginning
4639 // Top anchor now in skin
4640 $sections[0] .= $toc . "\n";
4643 $full .= implode( '', $sections );
4645 if ( $this->mForceTocPosition
) {
4646 return str_replace( '<!--MWTOC\'"-->', $toc, $full );
4653 * Transform wiki markup when saving a page by doing "\r\n" -> "\n"
4654 * conversion, substituting signatures, {{subst:}} templates, etc.
4656 * @param string $text The text to transform
4657 * @param Title $title The Title object for the current article
4658 * @param User $user The User object describing the current user
4659 * @param ParserOptions $options Parsing options
4660 * @param bool $clearState Whether to clear the parser state first
4661 * @return string The altered wiki markup
4663 public function preSaveTransform( $text, Title
$title, User
$user,
4664 ParserOptions
$options, $clearState = true
4666 if ( $clearState ) {
4667 $magicScopeVariable = $this->lock();
4669 $this->startParse( $title, $options, self
::OT_WIKI
, $clearState );
4670 $this->setUser( $user );
4672 // Strip U+0000 NULL (T159174)
4673 $text = str_replace( "\000", '', $text );
4675 // We still normalize line endings for backwards-compatibility
4676 // with other code that just calls PST, but this should already
4677 // be handled in TextContent subclasses
4678 $text = TextContent
::normalizeLineEndings( $text );
4680 if ( $options->getPreSaveTransform() ) {
4681 $text = $this->pstPass2( $text, $user );
4683 $text = $this->mStripState
->unstripBoth( $text );
4685 $this->setUser( null ); # Reset
4691 * Pre-save transform helper function
4693 * @param string $text
4698 private function pstPass2( $text, $user ) {
4699 # Note: This is the timestamp saved as hardcoded wikitext to the database, we use
4700 # $this->contLang here in order to give everyone the same signature and use the default one
4701 # rather than the one selected in each user's preferences. (see also T14815)
4702 $ts = $this->mOptions
->getTimestamp();
4703 $timestamp = MWTimestamp
::getLocalInstance( $ts );
4704 $ts = $timestamp->format( 'YmdHis' );
4705 $tzMsg = $timestamp->getTimezoneMessage()->inContentLanguage()->text();
4707 $d = $this->contLang
->timeanddate( $ts, false, false ) . " ($tzMsg)";
4709 # Variable replacement
4710 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
4711 $text = $this->replaceVariables( $text );
4713 # This works almost by chance, as the replaceVariables are done before the getUserSig(),
4714 # which may corrupt this parser instance via its wfMessage()->text() call-
4717 if ( strpos( $text, '~~~' ) !== false ) {
4718 $sigText = $this->getUserSig( $user );
4719 $text = strtr( $text, [
4721 '~~~~' => "$sigText $d",
4724 # The main two signature forms used above are time-sensitive
4725 $this->setOutputFlag( 'user-signature', 'User signature detected' );
4728 # Context links ("pipe tricks"): [[|name]] and [[name (context)|]]
4729 $tc = '[' . Title
::legalChars() . ']';
4730 $nc = '[ _0-9A-Za-z\x80-\xff-]'; # Namespaces can use non-ascii!
4732 // [[ns:page (context)|]]
4733 $p1 = "/\[\[(:?$nc+:|:|)($tc+?)( ?\\($tc+\\))\\|]]/";
4734 // [[ns:page(context)|]] (double-width brackets, added in r40257)
4735 $p4 = "/\[\[(:?$nc+:|:|)($tc+?)( ?($tc+))\\|]]/";
4736 // [[ns:page (context), context|]] (using either single or double-width comma)
4737 $p3 = "/\[\[(:?$nc+:|:|)($tc+?)( ?\\($tc+\\)|)((?:, |,)$tc+|)\\|]]/";
4738 // [[|page]] (reverse pipe trick: add context from page title)
4739 $p2 = "/\[\[\\|($tc+)]]/";
4741 # try $p1 first, to turn "[[A, B (C)|]]" into "[[A, B (C)|A, B]]"
4742 $text = preg_replace( $p1, '[[\\1\\2\\3|\\2]]', $text );
4743 $text = preg_replace( $p4, '[[\\1\\2\\3|\\2]]', $text );
4744 $text = preg_replace( $p3, '[[\\1\\2\\3\\4|\\2]]', $text );
4746 $t = $this->getTitle()->getText();
4748 if ( preg_match( "/^($nc+:|)$tc+?( \\($tc+\\))$/", $t, $m ) ) {
4749 $text = preg_replace( $p2, "[[$m[1]\\1$m[2]|\\1]]", $text );
4750 } elseif ( preg_match( "/^($nc+:|)$tc+?(, $tc+|)$/", $t, $m ) && "$m[1]$m[2]" != '' ) {
4751 $text = preg_replace( $p2, "[[$m[1]\\1$m[2]|\\1]]", $text );
4753 # if there's no context, don't bother duplicating the title
4754 $text = preg_replace( $p2, '[[\\1]]', $text );
4761 * Fetch the user's signature text, if any, and normalize to
4762 * validated, ready-to-insert wikitext.
4763 * If you have pre-fetched the nickname or the fancySig option, you can
4764 * specify them here to save a database query.
4765 * Do not reuse this parser instance after calling getUserSig(),
4766 * as it may have changed.
4768 * @param User &$user
4769 * @param string|bool $nickname Nickname to use or false to use user's default nickname
4770 * @param bool|null $fancySig whether the nicknname is the complete signature
4771 * or null to use default value
4774 public function getUserSig( &$user, $nickname = false, $fancySig = null ) {
4775 $username = $user->getName();
4777 # If not given, retrieve from the user object.
4778 if ( $nickname === false ) {
4779 $nickname = $user->getOption( 'nickname' );
4782 if ( is_null( $fancySig ) ) {
4783 $fancySig = $user->getBoolOption( 'fancysig' );
4786 $nickname = $nickname == null ?
$username : $nickname;
4788 if ( mb_strlen( $nickname ) > $this->svcOptions
->get( 'MaxSigChars' ) ) {
4789 $nickname = $username;
4790 $this->logger
->debug( __METHOD__
. ": $username has overlong signature." );
4791 } elseif ( $fancySig !== false ) {
4792 # Sig. might contain markup; validate this
4793 if ( $this->validateSig( $nickname ) !== false ) {
4794 # Validated; clean up (if needed) and return it
4795 return $this->cleanSig( $nickname, true );
4797 # Failed to validate; fall back to the default
4798 $nickname = $username;
4799 $this->logger
->debug( __METHOD__
. ": $username has bad XML tags in signature." );
4803 # Make sure nickname doesnt get a sig in a sig
4804 $nickname = self
::cleanSigInSig( $nickname );
4806 # If we're still here, make it a link to the user page
4807 $userText = wfEscapeWikiText( $username );
4808 $nickText = wfEscapeWikiText( $nickname );
4809 $msgName = $user->isAnon() ?
'signature-anon' : 'signature';
4811 return wfMessage( $msgName, $userText, $nickText )->inContentLanguage()
4812 ->title( $this->getTitle() )->text();
4816 * Check that the user's signature contains no bad XML
4818 * @param string $text
4819 * @return string|bool An expanded string, or false if invalid.
4821 public function validateSig( $text ) {
4822 return Xml
::isWellFormedXmlFragment( $text ) ?
$text : false;
4826 * Clean up signature text
4828 * 1) Strip 3, 4 or 5 tildes out of signatures @see cleanSigInSig
4829 * 2) Substitute all transclusions
4831 * @param string $text
4832 * @param bool $parsing Whether we're cleaning (preferences save) or parsing
4833 * @return string Signature text
4835 public function cleanSig( $text, $parsing = false ) {
4838 $magicScopeVariable = $this->lock();
4839 $this->startParse( $wgTitle, new ParserOptions
, self
::OT_PREPROCESS
, true );
4842 # Option to disable this feature
4843 if ( !$this->mOptions
->getCleanSignatures() ) {
4847 # @todo FIXME: Regex doesn't respect extension tags or nowiki
4848 # => Move this logic to braceSubstitution()
4849 $substWord = $this->magicWordFactory
->get( 'subst' );
4850 $substRegex = '/\{\{(?!(?:' . $substWord->getBaseRegex() . '))/x' . $substWord->getRegexCase();
4851 $substText = '{{' . $substWord->getSynonym( 0 );
4853 $text = preg_replace( $substRegex, $substText, $text );
4854 $text = self
::cleanSigInSig( $text );
4855 $dom = $this->preprocessToDom( $text );
4856 $frame = $this->getPreprocessor()->newFrame();
4857 $text = $frame->expand( $dom );
4860 $text = $this->mStripState
->unstripBoth( $text );
4867 * Strip 3, 4 or 5 tildes out of signatures.
4869 * @param string $text
4870 * @return string Signature text with /~{3,5}/ removed
4872 public static function cleanSigInSig( $text ) {
4873 $text = preg_replace( '/~{3,5}/', '', $text );
4878 * Set up some variables which are usually set up in parse()
4879 * so that an external function can call some class members with confidence
4881 * @param Title|null $title
4882 * @param ParserOptions $options
4883 * @param int $outputType
4884 * @param bool $clearState
4885 * @param int|null $revId
4887 public function startExternalParse( Title
$title = null, ParserOptions
$options,
4888 $outputType, $clearState = true, $revId = null
4890 $this->startParse( $title, $options, $outputType, $clearState );
4891 if ( $revId !== null ) {
4892 $this->mRevisionId
= $revId;
4897 * @param Title|null $title
4898 * @param ParserOptions $options
4899 * @param int $outputType
4900 * @param bool $clearState
4902 private function startParse( Title
$title = null, ParserOptions
$options,
4903 $outputType, $clearState = true
4905 $this->setTitle( $title );
4906 $this->mOptions
= $options;
4907 $this->setOutputType( $outputType );
4908 if ( $clearState ) {
4909 $this->clearState();
4914 * Wrapper for preprocess()
4916 * @param string $text The text to preprocess
4917 * @param ParserOptions $options
4918 * @param Title|null $title Title object or null to use $wgTitle
4921 public function transformMsg( $text, $options, $title = null ) {
4922 static $executing = false;
4924 # Guard against infinite recursion
4935 $text = $this->preprocess( $text, $title, $options );
4942 * Create an HTML-style tag, e.g. "<yourtag>special text</yourtag>"
4943 * The callback should have the following form:
4944 * function myParserHook( $text, $params, $parser, $frame ) { ... }
4946 * Transform and return $text. Use $parser for any required context, e.g. use
4947 * $parser->getTitle() and $parser->getOptions() not $wgTitle or $wgOut->mParserOptions
4949 * Hooks may return extended information by returning an array, of which the
4950 * first numbered element (index 0) must be the return string, and all other
4951 * entries are extracted into local variables within an internal function
4952 * in the Parser class.
4954 * This interface (introduced r61913) appears to be undocumented, but
4955 * 'markerType' is used by some core tag hooks to override which strip
4956 * array their results are placed in. **Use great caution if attempting
4957 * this interface, as it is not documented and injudicious use could smash
4958 * private variables.**
4960 * @param string $tag The tag to use, e.g. 'hook' for "<hook>"
4961 * @param callable $callback The callback function (and object) to use for the tag
4962 * @throws MWException
4963 * @return callable|null The old value of the mTagHooks array associated with the hook
4965 public function setHook( $tag, callable
$callback ) {
4966 $tag = strtolower( $tag );
4967 if ( preg_match( '/[<>\r\n]/', $tag, $m ) ) {
4968 throw new MWException( "Invalid character {$m[0]} in setHook('$tag', ...) call" );
4970 $oldVal = $this->mTagHooks
[$tag] ??
null;
4971 $this->mTagHooks
[$tag] = $callback;
4972 if ( !in_array( $tag, $this->mStripList
) ) {
4973 $this->mStripList
[] = $tag;
4980 * As setHook(), but letting the contents be parsed.
4982 * Transparent tag hooks are like regular XML-style tag hooks, except they
4983 * operate late in the transformation sequence, on HTML instead of wikitext.
4985 * This is probably obsoleted by things dealing with parser frames?
4986 * The only extension currently using it is geoserver.
4989 * @todo better document or deprecate this
4991 * @param string $tag The tag to use, e.g. 'hook' for "<hook>"
4992 * @param callable $callback The callback function (and object) to use for the tag
4993 * @throws MWException
4994 * @return callable|null The old value of the mTagHooks array associated with the hook
4996 public function setTransparentTagHook( $tag, callable
$callback ) {
4997 $tag = strtolower( $tag );
4998 if ( preg_match( '/[<>\r\n]/', $tag, $m ) ) {
4999 throw new MWException( "Invalid character {$m[0]} in setTransparentHook('$tag', ...) call" );
5001 $oldVal = $this->mTransparentTagHooks
[$tag] ??
null;
5002 $this->mTransparentTagHooks
[$tag] = $callback;
5008 * Remove all tag hooks
5010 public function clearTagHooks() {
5011 $this->mTagHooks
= [];
5012 $this->mFunctionTagHooks
= [];
5013 $this->mStripList
= $this->mDefaultStripList
;
5017 * Create a function, e.g. {{sum:1|2|3}}
5018 * The callback function should have the form:
5019 * function myParserFunction( &$parser, $arg1, $arg2, $arg3 ) { ... }
5021 * Or with Parser::SFH_OBJECT_ARGS:
5022 * function myParserFunction( $parser, $frame, $args ) { ... }
5024 * The callback may either return the text result of the function, or an array with the text
5025 * in element 0, and a number of flags in the other elements. The names of the flags are
5026 * specified in the keys. Valid flags are:
5027 * found The text returned is valid, stop processing the template. This
5029 * nowiki Wiki markup in the return value should be escaped
5030 * isHTML The returned text is HTML, armour it against wikitext transformation
5032 * @param string $id The magic word ID
5033 * @param callable $callback The callback function (and object) to use
5034 * @param int $flags A combination of the following flags:
5035 * Parser::SFH_NO_HASH No leading hash, i.e. {{plural:...}} instead of {{#if:...}}
5037 * Parser::SFH_OBJECT_ARGS Pass the template arguments as PPNode objects instead of text.
5038 * This allows for conditional expansion of the parse tree, allowing you to eliminate dead
5039 * branches and thus speed up parsing. It is also possible to analyse the parse tree of
5040 * the arguments, and to control the way they are expanded.
5042 * The $frame parameter is a PPFrame. This can be used to produce expanded text from the
5043 * arguments, for instance:
5044 * $text = isset( $args[0] ) ? $frame->expand( $args[0] ) : '';
5046 * For technical reasons, $args[0] is pre-expanded and will be a string. This may change in
5047 * future versions. Please call $frame->expand() on it anyway so that your code keeps
5048 * working if/when this is changed.
5050 * If you want whitespace to be trimmed from $args, you need to do it yourself, post-
5053 * Please read the documentation in includes/parser/Preprocessor.php for more information
5054 * about the methods available in PPFrame and PPNode.
5056 * @throws MWException
5057 * @return string|callable The old callback function for this name, if any
5059 public function setFunctionHook( $id, callable
$callback, $flags = 0 ) {
5060 $oldVal = isset( $this->mFunctionHooks
[$id] ) ?
$this->mFunctionHooks
[$id][0] : null;
5061 $this->mFunctionHooks
[$id] = [ $callback, $flags ];
5063 # Add to function cache
5064 $mw = $this->magicWordFactory
->get( $id );
5066 throw new MWException( __METHOD__
. '() expecting a magic word identifier.' );
5069 $synonyms = $mw->getSynonyms();
5070 $sensitive = intval( $mw->isCaseSensitive() );
5072 foreach ( $synonyms as $syn ) {
5074 if ( !$sensitive ) {
5075 $syn = $this->contLang
->lc( $syn );
5078 if ( !( $flags & self
::SFH_NO_HASH
) ) {
5081 # Remove trailing colon
5082 if ( substr( $syn, -1, 1 ) === ':' ) {
5083 $syn = substr( $syn, 0, -1 );
5085 $this->mFunctionSynonyms
[$sensitive][$syn] = $id;
5091 * Get all registered function hook identifiers
5095 public function getFunctionHooks() {
5096 $this->firstCallInit();
5097 return array_keys( $this->mFunctionHooks
);
5101 * Create a tag function, e.g. "<test>some stuff</test>".
5102 * Unlike tag hooks, tag functions are parsed at preprocessor level.
5103 * Unlike parser functions, their content is not preprocessed.
5104 * @param string $tag
5105 * @param callable $callback
5107 * @throws MWException
5110 public function setFunctionTagHook( $tag, callable
$callback, $flags ) {
5111 $tag = strtolower( $tag );
5112 if ( preg_match( '/[<>\r\n]/', $tag, $m ) ) {
5113 throw new MWException( "Invalid character {$m[0]} in setFunctionTagHook('$tag', ...) call" );
5115 $old = $this->mFunctionTagHooks
[$tag] ??
null;
5116 $this->mFunctionTagHooks
[$tag] = [ $callback, $flags ];
5118 if ( !in_array( $tag, $this->mStripList
) ) {
5119 $this->mStripList
[] = $tag;
5126 * Replace "<!--LINK-->" link placeholders with actual links, in the buffer
5127 * Placeholders created in Linker::link()
5129 * @param string &$text
5130 * @param int $options
5132 public function replaceLinkHolders( &$text, $options = 0 ) {
5133 $this->mLinkHolders
->replace( $text );
5137 * Replace "<!--LINK-->" link placeholders with plain text of links
5138 * (not HTML-formatted).
5140 * @param string $text
5143 public function replaceLinkHoldersText( $text ) {
5144 return $this->mLinkHolders
->replaceText( $text );
5148 * Renders an image gallery from a text with one line per image.
5149 * text labels may be given by using |-style alternative text. E.g.
5150 * Image:one.jpg|The number "1"
5151 * Image:tree.jpg|A tree
5152 * given as text will return the HTML of a gallery with two images,
5153 * labeled 'The number "1"' and
5156 * @param string $text
5157 * @param array $params
5158 * @return string HTML
5160 public function renderImageGallery( $text, $params ) {
5162 if ( isset( $params['mode'] ) ) {
5163 $mode = $params['mode'];
5167 $ig = ImageGalleryBase
::factory( $mode );
5168 } catch ( Exception
$e ) {
5169 // If invalid type set, fallback to default.
5170 $ig = ImageGalleryBase
::factory( false );
5173 $ig->setContextTitle( $this->getTitle() );
5174 $ig->setShowBytes( false );
5175 $ig->setShowDimensions( false );
5176 $ig->setShowFilename( false );
5177 $ig->setParser( $this );
5178 $ig->setHideBadImages();
5179 $ig->setAttributes( Sanitizer
::validateTagAttributes( $params, 'ul' ) );
5181 if ( isset( $params['showfilename'] ) ) {
5182 $ig->setShowFilename( true );
5184 $ig->setShowFilename( false );
5186 if ( isset( $params['caption'] ) ) {
5187 // NOTE: We aren't passing a frame here or below. Frame info
5188 // is currently opaque to Parsoid, which acts on OT_PREPROCESS.
5189 // See T107332#4030581
5190 $caption = $this->recursiveTagParse( $params['caption'] );
5191 $ig->setCaptionHtml( $caption );
5193 if ( isset( $params['perrow'] ) ) {
5194 $ig->setPerRow( $params['perrow'] );
5196 if ( isset( $params['widths'] ) ) {
5197 $ig->setWidths( $params['widths'] );
5199 if ( isset( $params['heights'] ) ) {
5200 $ig->setHeights( $params['heights'] );
5202 $ig->setAdditionalOptions( $params );
5204 // Avoid PHP 7.1 warning from passing $this by reference
5206 Hooks
::run( 'BeforeParserrenderImageGallery', [ &$parser, &$ig ] );
5208 $lines = StringUtils
::explode( "\n", $text );
5209 foreach ( $lines as $line ) {
5210 # match lines like these:
5211 # Image:someimage.jpg|This is some image
5213 preg_match( "/^([^|]+)(\\|(.*))?$/", $line, $matches );
5215 if ( count( $matches ) == 0 ) {
5219 if ( strpos( $matches[0], '%' ) !== false ) {
5220 $matches[1] = rawurldecode( $matches[1] );
5222 $title = Title
::newFromText( $matches[1], NS_FILE
);
5223 if ( is_null( $title ) ) {
5224 # Bogus title. Ignore these so we don't bomb out later.
5228 # We need to get what handler the file uses, to figure out parameters.
5229 # Note, a hook can overide the file name, and chose an entirely different
5230 # file (which potentially could be of a different type and have different handler).
5233 Hooks
::run( 'BeforeParserFetchFileAndTitle',
5234 [ $this, $title, &$options, &$descQuery ] );
5235 # Don't register it now, as TraditionalImageGallery does that later.
5236 $file = $this->fetchFileNoRegister( $title, $options );
5237 $handler = $file ?
$file->getHandler() : false;
5240 'img_alt' => 'gallery-internal-alt',
5241 'img_link' => 'gallery-internal-link',
5244 $paramMap +
= $handler->getParamMap();
5245 // We don't want people to specify per-image widths.
5246 // Additionally the width parameter would need special casing anyhow.
5247 unset( $paramMap['img_width'] );
5250 $mwArray = $this->magicWordFactory
->newArray( array_keys( $paramMap ) );
5255 $handlerOptions = [];
5256 if ( isset( $matches[3] ) ) {
5257 // look for an |alt= definition while trying not to break existing
5258 // captions with multiple pipes (|) in it, until a more sensible grammar
5259 // is defined for images in galleries
5261 // FIXME: Doing recursiveTagParse at this stage, and the trim before
5262 // splitting on '|' is a bit odd, and different from makeImage.
5263 $matches[3] = $this->recursiveTagParse( trim( $matches[3] ) );
5264 // Protect LanguageConverter markup
5265 $parameterMatches = StringUtils
::delimiterExplode(
5266 '-{', '}-', '|', $matches[3], true /* nested */
5269 foreach ( $parameterMatches as $parameterMatch ) {
5270 list( $magicName, $match ) = $mwArray->matchVariableStartToEnd( $parameterMatch );
5272 $paramName = $paramMap[$magicName];
5274 switch ( $paramName ) {
5275 case 'gallery-internal-alt':
5276 $alt = $this->stripAltText( $match, false );
5278 case 'gallery-internal-link':
5279 $linkValue = $this->stripAltText( $match, false );
5280 if ( preg_match( '/^-{R|(.*)}-$/', $linkValue ) ) {
5281 // Result of LanguageConverter::markNoConversion
5282 // invoked on an external link.
5283 $linkValue = substr( $linkValue, 4, -2 );
5285 list( $type, $target ) = $this->parseLinkParameter( $linkValue );
5286 if ( $type === 'link-url' ) {
5288 $this->mOutput
->addExternalLink( $target );
5289 } elseif ( $type === 'link-title' ) {
5290 $link = $target->getLinkURL();
5291 $this->mOutput
->addLink( $target );
5295 // Must be a handler specific parameter.
5296 if ( $handler->validateParam( $paramName, $match ) ) {
5297 $handlerOptions[$paramName] = $match;
5299 // Guess not, consider it as caption.
5300 $this->logger
->debug(
5301 "$parameterMatch failed parameter validation" );
5302 $label = $parameterMatch;
5308 $label = $parameterMatch;
5313 $ig->add( $title, $label, $alt, $link, $handlerOptions );
5315 $html = $ig->toHTML();
5316 Hooks
::run( 'AfterParserFetchFileAndTitle', [ $this, $ig, &$html ] );
5321 * @param MediaHandler $handler
5324 public function getImageParams( $handler ) {
5326 $handlerClass = get_class( $handler );
5330 if ( !isset( $this->mImageParams
[$handlerClass] ) ) {
5331 # Initialise static lists
5332 static $internalParamNames = [
5333 'horizAlign' => [ 'left', 'right', 'center', 'none' ],
5334 'vertAlign' => [ 'baseline', 'sub', 'super', 'top', 'text-top', 'middle',
5335 'bottom', 'text-bottom' ],
5336 'frame' => [ 'thumbnail', 'manualthumb', 'framed', 'frameless',
5337 'upright', 'border', 'link', 'alt', 'class' ],
5339 static $internalParamMap;
5340 if ( !$internalParamMap ) {
5341 $internalParamMap = [];
5342 foreach ( $internalParamNames as $type => $names ) {
5343 foreach ( $names as $name ) {
5344 // For grep: img_left, img_right, img_center, img_none,
5345 // img_baseline, img_sub, img_super, img_top, img_text_top, img_middle,
5346 // img_bottom, img_text_bottom,
5347 // img_thumbnail, img_manualthumb, img_framed, img_frameless, img_upright,
5348 // img_border, img_link, img_alt, img_class
5349 $magicName = str_replace( '-', '_', "img_$name" );
5350 $internalParamMap[$magicName] = [ $type, $name ];
5355 # Add handler params
5356 $paramMap = $internalParamMap;
5358 $handlerParamMap = $handler->getParamMap();
5359 foreach ( $handlerParamMap as $magic => $paramName ) {
5360 $paramMap[$magic] = [ 'handler', $paramName ];
5363 $this->mImageParams
[$handlerClass] = $paramMap;
5364 $this->mImageParamsMagicArray
[$handlerClass] =
5365 $this->magicWordFactory
->newArray( array_keys( $paramMap ) );
5367 return [ $this->mImageParams
[$handlerClass], $this->mImageParamsMagicArray
[$handlerClass] ];
5371 * Parse image options text and use it to make an image
5373 * @param Title $title
5374 * @param string $options
5375 * @param LinkHolderArray|bool $holders
5376 * @return string HTML
5378 public function makeImage( $title, $options, $holders = false ) {
5379 # Check if the options text is of the form "options|alt text"
5381 # * thumbnail make a thumbnail with enlarge-icon and caption, alignment depends on lang
5382 # * left no resizing, just left align. label is used for alt= only
5383 # * right same, but right aligned
5384 # * none same, but not aligned
5385 # * ___px scale to ___ pixels width, no aligning. e.g. use in taxobox
5386 # * center center the image
5387 # * frame Keep original image size, no magnify-button.
5388 # * framed Same as "frame"
5389 # * frameless like 'thumb' but without a frame. Keeps user preferences for width
5390 # * upright reduce width for upright images, rounded to full __0 px
5391 # * border draw a 1px border around the image
5392 # * alt Text for HTML alt attribute (defaults to empty)
5393 # * class Set a class for img node
5394 # * link Set the target of the image link. Can be external, interwiki, or local
5395 # vertical-align values (no % or length right now):
5405 # Protect LanguageConverter markup when splitting into parts
5406 $parts = StringUtils
::delimiterExplode(
5407 '-{', '}-', '|', $options, true /* allow nesting */
5410 # Give extensions a chance to select the file revision for us
5413 Hooks
::run( 'BeforeParserFetchFileAndTitle',
5414 [ $this, $title, &$options, &$descQuery ] );
5415 # Fetch and register the file (file title may be different via hooks)
5416 list( $file, $title ) = $this->fetchFileAndTitle( $title, $options );
5419 $handler = $file ?
$file->getHandler() : false;
5421 list( $paramMap, $mwArray ) = $this->getImageParams( $handler );
5424 $this->addTrackingCategory( 'broken-file-category' );
5427 # Process the input parameters
5429 $params = [ 'frame' => [], 'handler' => [],
5430 'horizAlign' => [], 'vertAlign' => [] ];
5431 $seenformat = false;
5432 foreach ( $parts as $part ) {
5433 $part = trim( $part );
5434 list( $magicName, $value ) = $mwArray->matchVariableStartToEnd( $part );
5436 if ( isset( $paramMap[$magicName] ) ) {
5437 list( $type, $paramName ) = $paramMap[$magicName];
5439 # Special case; width and height come in one variable together
5440 if ( $type === 'handler' && $paramName === 'width' ) {
5441 $parsedWidthParam = self
::parseWidthParam( $value );
5442 if ( isset( $parsedWidthParam['width'] ) ) {
5443 $width = $parsedWidthParam['width'];
5444 if ( $handler->validateParam( 'width', $width ) ) {
5445 $params[$type]['width'] = $width;
5449 if ( isset( $parsedWidthParam['height'] ) ) {
5450 $height = $parsedWidthParam['height'];
5451 if ( $handler->validateParam( 'height', $height ) ) {
5452 $params[$type]['height'] = $height;
5456 # else no validation -- T15436
5458 if ( $type === 'handler' ) {
5459 # Validate handler parameter
5460 $validated = $handler->validateParam( $paramName, $value );
5462 # Validate internal parameters
5463 switch ( $paramName ) {
5467 # @todo FIXME: Possibly check validity here for
5468 # manualthumb? downstream behavior seems odd with
5469 # missing manual thumbs.
5471 $value = $this->stripAltText( $value, $holders );
5474 list( $paramName, $value ) =
5475 $this->parseLinkParameter(
5476 $this->stripAltText( $value, $holders )
5480 if ( $paramName === 'no-link' ) {
5483 if ( ( $paramName === 'link-url' ) && $this->mOptions
->getExternalLinkTarget() ) {
5484 $params[$type]['link-target'] = $this->mOptions
->getExternalLinkTarget();
5491 // use first appearing option, discard others.
5492 $validated = !$seenformat;
5496 # Most other things appear to be empty or numeric...
5497 $validated = ( $value === false ||
is_numeric( trim( $value ) ) );
5502 $params[$type][$paramName] = $value;
5506 if ( !$validated ) {
5511 # Process alignment parameters
5512 if ( $params['horizAlign'] ) {
5513 $params['frame']['align'] = key( $params['horizAlign'] );
5515 if ( $params['vertAlign'] ) {
5516 $params['frame']['valign'] = key( $params['vertAlign'] );
5519 $params['frame']['caption'] = $caption;
5521 # Will the image be presented in a frame, with the caption below?
5522 $imageIsFramed = isset( $params['frame']['frame'] )
5523 ||
isset( $params['frame']['framed'] )
5524 ||
isset( $params['frame']['thumbnail'] )
5525 ||
isset( $params['frame']['manualthumb'] );
5527 # In the old days, [[Image:Foo|text...]] would set alt text. Later it
5528 # came to also set the caption, ordinary text after the image -- which
5529 # makes no sense, because that just repeats the text multiple times in
5530 # screen readers. It *also* came to set the title attribute.
5531 # Now that we have an alt attribute, we should not set the alt text to
5532 # equal the caption: that's worse than useless, it just repeats the
5533 # text. This is the framed/thumbnail case. If there's no caption, we
5534 # use the unnamed parameter for alt text as well, just for the time be-
5535 # ing, if the unnamed param is set and the alt param is not.
5536 # For the future, we need to figure out if we want to tweak this more,
5537 # e.g., introducing a title= parameter for the title; ignoring the un-
5538 # named parameter entirely for images without a caption; adding an ex-
5539 # plicit caption= parameter and preserving the old magic unnamed para-
5541 if ( $imageIsFramed ) { # Framed image
5542 if ( $caption === '' && !isset( $params['frame']['alt'] ) ) {
5543 # No caption or alt text, add the filename as the alt text so
5544 # that screen readers at least get some description of the image
5545 $params['frame']['alt'] = $title->getText();
5547 # Do not set $params['frame']['title'] because tooltips don't make sense
5549 } else { # Inline image
5550 if ( !isset( $params['frame']['alt'] ) ) {
5551 # No alt text, use the "caption" for the alt text
5552 if ( $caption !== '' ) {
5553 $params['frame']['alt'] = $this->stripAltText( $caption, $holders );
5555 # No caption, fall back to using the filename for the
5557 $params['frame']['alt'] = $title->getText();
5560 # Use the "caption" for the tooltip text
5561 $params['frame']['title'] = $this->stripAltText( $caption, $holders );
5563 $params['handler']['targetlang'] = $this->getTargetLanguage()->getCode();
5565 Hooks
::run( 'ParserMakeImageParams', [ $title, $file, &$params, $this ] );
5567 # Linker does the rest
5568 $time = $options['time'] ??
false;
5569 $ret = Linker
::makeImageLink( $this, $title, $file, $params['frame'], $params['handler'],
5570 $time, $descQuery, $this->mOptions
->getThumbSize() );
5572 # Give the handler a chance to modify the parser object
5574 $handler->parserTransformHook( $this, $file );
5581 * Parse the value of 'link' parameter in image syntax (`[[File:Foo.jpg|link=<value>]]`).
5583 * Adds an entry to appropriate link tables.
5586 * @param string $value
5587 * @return array of `[ type, target ]`, where:
5588 * - `type` is one of:
5589 * - `null`: Given value is not a valid link target, use default
5590 * - `'no-link'`: Given value is empty, do not generate a link
5591 * - `'link-url'`: Given value is a valid external link
5592 * - `'link-title'`: Given value is a valid internal link
5594 * - When `type` is `null` or `'no-link'`: `false`
5595 * - When `type` is `'link-url'`: URL string corresponding to given value
5596 * - When `type` is `'link-title'`: Title object corresponding to given value
5598 public function parseLinkParameter( $value ) {
5599 $chars = self
::EXT_LINK_URL_CLASS
;
5600 $addr = self
::EXT_LINK_ADDR
;
5601 $prots = $this->mUrlProtocols
;
5604 if ( $value === '' ) {
5606 } elseif ( preg_match( "/^((?i)$prots)/", $value ) ) {
5607 if ( preg_match( "/^((?i)$prots)$addr$chars*$/u", $value, $m ) ) {
5608 $this->mOutput
->addExternalLink( $value );
5613 $linkTitle = Title
::newFromText( $value );
5615 $this->mOutput
->addLink( $linkTitle );
5616 $type = 'link-title';
5617 $target = $linkTitle;
5620 return [ $type, $target ];
5624 * @param string $caption
5625 * @param LinkHolderArray|bool $holders
5626 * @return mixed|string
5628 protected function stripAltText( $caption, $holders ) {
5629 # Strip bad stuff out of the title (tooltip). We can't just use
5630 # replaceLinkHoldersText() here, because if this function is called
5631 # from replaceInternalLinks2(), mLinkHolders won't be up-to-date.
5633 $tooltip = $holders->replaceText( $caption );
5635 $tooltip = $this->replaceLinkHoldersText( $caption );
5638 # make sure there are no placeholders in thumbnail attributes
5639 # that are later expanded to html- so expand them now and
5641 $tooltip = $this->mStripState
->unstripBoth( $tooltip );
5642 # Compatibility hack! In HTML certain entity references not terminated
5643 # by a semicolon are decoded (but not if we're in an attribute; that's
5644 # how link URLs get away without properly escaping & in queries).
5645 # But wikitext has always required semicolon-termination of entities,
5646 # so encode & where needed to avoid decode of semicolon-less entities.
5648 # https://www.w3.org/TR/html5/syntax.html#named-character-references
5649 # T210437 discusses moving this workaround to Sanitizer::stripAllTags.
5650 $tooltip = preg_replace( "/
5651 & # 1. entity prefix
5652 (?= # 2. followed by:
5653 (?: # a. one of the legacy semicolon-less named entities
5654 A(?:Elig|MP|acute|circ|grave|ring|tilde|uml)|
5655 C(?:OPY|cedil)|E(?:TH|acute|circ|grave|uml)|
5656 GT|I(?:acute|circ|grave|uml)|LT|Ntilde|
5657 O(?:acute|circ|grave|slash|tilde|uml)|QUOT|REG|THORN|
5658 U(?:acute|circ|grave|uml)|Yacute|
5659 a(?:acute|c(?:irc|ute)|elig|grave|mp|ring|tilde|uml)|brvbar|
5660 c(?:cedil|edil|urren)|cent(?!erdot;)|copy(?!sr;)|deg|
5661 divide(?!ontimes;)|e(?:acute|circ|grave|th|uml)|
5662 frac(?:1(?:2|4)|34)|
5663 gt(?!c(?:c|ir)|dot|lPar|quest|r(?:a(?:pprox|rr)|dot|eq(?:less|qless)|less|sim);)|
5664 i(?:acute|circ|excl|grave|quest|uml)|laquo|
5665 lt(?!c(?:c|ir)|dot|hree|imes|larr|quest|r(?:Par|i(?:e|f|));)|
5666 m(?:acr|i(?:cro|ddot))|n(?:bsp|tilde)|
5667 not(?!in(?:E|dot|v(?:a|b|c)|)|ni(?:v(?:a|b|c)|);)|
5668 o(?:acute|circ|grave|rd(?:f|m)|slash|tilde|uml)|
5669 p(?:lusmn|ound)|para(?!llel;)|quot|r(?:aquo|eg)|
5670 s(?:ect|hy|up(?:1|2|3)|zlig)|thorn|times(?!b(?:ar|)|d;)|
5671 u(?:acute|circ|grave|ml|uml)|y(?:acute|en|uml)
5673 (?:[^;]|$)) # b. and not followed by a semicolon
5674 # S = study, for efficiency
5675 /Sx", '&', $tooltip );
5676 $tooltip = Sanitizer
::stripAllTags( $tooltip );
5682 * Set a flag in the output object indicating that the content is dynamic and
5683 * shouldn't be cached.
5684 * @deprecated since 1.28; use getOutput()->updateCacheExpiry()
5686 public function disableCache() {
5687 $this->logger
->debug( "Parser output marked as uncacheable." );
5688 if ( !$this->mOutput
) {
5689 throw new MWException( __METHOD__
.
5690 " can only be called when actually parsing something" );
5692 $this->mOutput
->updateCacheExpiry( 0 ); // new style, for consistency
5696 * Callback from the Sanitizer for expanding items found in HTML attribute
5697 * values, so they can be safely tested and escaped.
5699 * @param string &$text
5700 * @param bool|PPFrame $frame
5703 public function attributeStripCallback( &$text, $frame = false ) {
5704 $text = $this->replaceVariables( $text, $frame );
5705 $text = $this->mStripState
->unstripBoth( $text );
5714 public function getTags() {
5715 $this->firstCallInit();
5717 array_keys( $this->mTransparentTagHooks
),
5718 array_keys( $this->mTagHooks
),
5719 array_keys( $this->mFunctionTagHooks
)
5727 public function getFunctionSynonyms() {
5728 $this->firstCallInit();
5729 return $this->mFunctionSynonyms
;
5736 public function getUrlProtocols() {
5737 return $this->mUrlProtocols
;
5741 * Replace transparent tags in $text with the values given by the callbacks.
5743 * Transparent tag hooks are like regular XML-style tag hooks, except they
5744 * operate late in the transformation sequence, on HTML instead of wikitext.
5746 * @param string $text
5750 public function replaceTransparentTags( $text ) {
5752 $elements = array_keys( $this->mTransparentTagHooks
);
5753 $text = self
::extractTagsAndParams( $elements, $text, $matches );
5756 foreach ( $matches as $marker => $data ) {
5757 list( $element, $content, $params, $tag ) = $data;
5758 $tagName = strtolower( $element );
5759 if ( isset( $this->mTransparentTagHooks
[$tagName] ) ) {
5760 $output = call_user_func_array(
5761 $this->mTransparentTagHooks
[$tagName],
5762 [ $content, $params, $this ]
5767 $replacements[$marker] = $output;
5769 return strtr( $text, $replacements );
5773 * Break wikitext input into sections, and either pull or replace
5774 * some particular section's text.
5776 * External callers should use the getSection and replaceSection methods.
5778 * @param string $text Page wikitext
5779 * @param string|int $sectionId A section identifier string of the form:
5780 * "<flag1> - <flag2> - ... - <section number>"
5782 * Currently the only recognised flag is "T", which means the target section number
5783 * was derived during a template inclusion parse, in other words this is a template
5784 * section edit link. If no flags are given, it was an ordinary section edit link.
5785 * This flag is required to avoid a section numbering mismatch when a section is
5786 * enclosed by "<includeonly>" (T8563).
5788 * The section number 0 pulls the text before the first heading; other numbers will
5789 * pull the given section along with its lower-level subsections. If the section is
5790 * not found, $mode=get will return $newtext, and $mode=replace will return $text.
5792 * Section 0 is always considered to exist, even if it only contains the empty
5793 * string. If $text is the empty string and section 0 is replaced, $newText is
5796 * @param string $mode One of "get" or "replace"
5797 * @param string $newText Replacement text for section data.
5798 * @return string For "get", the extracted section text.
5799 * for "replace", the whole page with the section replaced.
5801 private function extractSections( $text, $sectionId, $mode, $newText = '' ) {
5802 global $wgTitle; # not generally used but removes an ugly failure mode
5804 $magicScopeVariable = $this->lock();
5805 $this->startParse( $wgTitle, new ParserOptions
, self
::OT_PLAIN
, true );
5807 $frame = $this->getPreprocessor()->newFrame();
5809 # Process section extraction flags
5811 $sectionParts = explode( '-', $sectionId );
5812 $sectionIndex = array_pop( $sectionParts );
5813 foreach ( $sectionParts as $part ) {
5814 if ( $part === 'T' ) {
5815 $flags |
= self
::PTD_FOR_INCLUSION
;
5819 # Check for empty input
5820 if ( strval( $text ) === '' ) {
5821 # Only sections 0 and T-0 exist in an empty document
5822 if ( $sectionIndex == 0 ) {
5823 if ( $mode === 'get' ) {
5829 if ( $mode === 'get' ) {
5837 # Preprocess the text
5838 $root = $this->preprocessToDom( $text, $flags );
5840 # <h> nodes indicate section breaks
5841 # They can only occur at the top level, so we can find them by iterating the root's children
5842 $node = $root->getFirstChild();
5844 # Find the target section
5845 if ( $sectionIndex == 0 ) {
5846 # Section zero doesn't nest, level=big
5847 $targetLevel = 1000;
5850 if ( $node->getName() === 'h' ) {
5851 $bits = $node->splitHeading();
5852 if ( $bits['i'] == $sectionIndex ) {
5853 $targetLevel = $bits['level'];
5857 if ( $mode === 'replace' ) {
5858 $outText .= $frame->expand( $node, PPFrame
::RECOVER_ORIG
);
5860 $node = $node->getNextSibling();
5866 if ( $mode === 'get' ) {
5873 # Find the end of the section, including nested sections
5875 if ( $node->getName() === 'h' ) {
5876 $bits = $node->splitHeading();
5877 $curLevel = $bits['level'];
5878 if ( $bits['i'] != $sectionIndex && $curLevel <= $targetLevel ) {
5882 if ( $mode === 'get' ) {
5883 $outText .= $frame->expand( $node, PPFrame
::RECOVER_ORIG
);
5885 $node = $node->getNextSibling();
5888 # Write out the remainder (in replace mode only)
5889 if ( $mode === 'replace' ) {
5890 # Output the replacement text
5891 # Add two newlines on -- trailing whitespace in $newText is conventionally
5892 # stripped by the editor, so we need both newlines to restore the paragraph gap
5893 # Only add trailing whitespace if there is newText
5894 if ( $newText != "" ) {
5895 $outText .= $newText . "\n\n";
5899 $outText .= $frame->expand( $node, PPFrame
::RECOVER_ORIG
);
5900 $node = $node->getNextSibling();
5904 if ( is_string( $outText ) ) {
5905 # Re-insert stripped tags
5906 $outText = rtrim( $this->mStripState
->unstripBoth( $outText ) );
5913 * This function returns the text of a section, specified by a number ($section).
5914 * A section is text under a heading like == Heading == or \<h1\>Heading\</h1\>, or
5915 * the first section before any such heading (section 0).
5917 * If a section contains subsections, these are also returned.
5919 * @param string $text Text to look in
5920 * @param string|int $sectionId Section identifier as a number or string
5921 * (e.g. 0, 1 or 'T-1').
5922 * @param string $defaultText Default to return if section is not found
5924 * @return string Text of the requested section
5926 public function getSection( $text, $sectionId, $defaultText = '' ) {
5927 return $this->extractSections( $text, $sectionId, 'get', $defaultText );
5931 * This function returns $oldtext after the content of the section
5932 * specified by $section has been replaced with $text. If the target
5933 * section does not exist, $oldtext is returned unchanged.
5935 * @param string $oldText Former text of the article
5936 * @param string|int $sectionId Section identifier as a number or string
5937 * (e.g. 0, 1 or 'T-1').
5938 * @param string $newText Replacing text
5940 * @return string Modified text
5942 public function replaceSection( $oldText, $sectionId, $newText ) {
5943 return $this->extractSections( $oldText, $sectionId, 'replace', $newText );
5947 * Get the ID of the revision we are parsing
5949 * The return value will be either:
5950 * - a) Positive, indicating a specific revision ID (current or old)
5951 * - b) Zero, meaning the revision ID is specified by getCurrentRevisionCallback()
5952 * - c) Null, meaning the parse is for preview mode and there is no revision
5956 public function getRevisionId() {
5957 return $this->mRevisionId
;
5961 * Get the revision object for $this->mRevisionId
5963 * @return Revision|null Either a Revision object or null
5964 * @since 1.23 (public since 1.23)
5966 public function getRevisionObject() {
5967 if ( $this->mRevisionObject
) {
5968 return $this->mRevisionObject
;
5971 // NOTE: try to get the RevisionObject even if mRevisionId is null.
5972 // This is useful when parsing a revision that has not yet been saved.
5973 // However, if we get back a saved revision even though we are in
5974 // preview mode, we'll have to ignore it, see below.
5975 // NOTE: This callback may be used to inject an OLD revision that was
5976 // already loaded, so "current" is a bit of a misnomer. We can't just
5977 // skip it if mRevisionId is set.
5978 $rev = call_user_func(
5979 $this->mOptions
->getCurrentRevisionCallback(),
5984 if ( $this->mRevisionId
=== null && $rev && $rev->getId() ) {
5985 // We are in preview mode (mRevisionId is null), and the current revision callback
5986 // returned an existing revision. Ignore it and return null, it's probably the page's
5987 // current revision, which is not what we want here. Note that we do want to call the
5988 // callback to allow the unsaved revision to be injected here, e.g. for
5989 // self-transclusion previews.
5993 // If the parse is for a new revision, then the callback should have
5994 // already been set to force the object and should match mRevisionId.
5995 // If not, try to fetch by mRevisionId for sanity.
5996 if ( $this->mRevisionId
&& $rev && $rev->getId() != $this->mRevisionId
) {
5997 $rev = Revision
::newFromId( $this->mRevisionId
);
6000 $this->mRevisionObject
= $rev;
6002 return $this->mRevisionObject
;
6006 * Get the timestamp associated with the current revision, adjusted for
6007 * the default server-local timestamp
6008 * @return string TS_MW timestamp
6010 public function getRevisionTimestamp() {
6011 if ( $this->mRevisionTimestamp
!== null ) {
6012 return $this->mRevisionTimestamp
;
6015 # Use specified revision timestamp, falling back to the current timestamp
6016 $revObject = $this->getRevisionObject();
6017 $timestamp = $revObject ?
$revObject->getTimestamp() : $this->mOptions
->getTimestamp();
6018 $this->mOutput
->setRevisionTimestampUsed( $timestamp ); // unadjusted time zone
6020 # The cryptic '' timezone parameter tells to use the site-default
6021 # timezone offset instead of the user settings.
6022 # Since this value will be saved into the parser cache, served
6023 # to other users, and potentially even used inside links and such,
6024 # it needs to be consistent for all visitors.
6025 $this->mRevisionTimestamp
= $this->contLang
->userAdjust( $timestamp, '' );
6027 return $this->mRevisionTimestamp
;
6031 * Get the name of the user that edited the last revision
6033 * @return string User name
6035 public function getRevisionUser() {
6036 if ( is_null( $this->mRevisionUser
) ) {
6037 $revObject = $this->getRevisionObject();
6039 # if this template is subst: the revision id will be blank,
6040 # so just use the current user's name
6042 $this->mRevisionUser
= $revObject->getUserText();
6043 } elseif ( $this->ot
['wiki'] ||
$this->mOptions
->getIsPreview() ) {
6044 $this->mRevisionUser
= $this->getUser()->getName();
6047 return $this->mRevisionUser
;
6051 * Get the size of the revision
6053 * @return int|null Revision size
6055 public function getRevisionSize() {
6056 if ( is_null( $this->mRevisionSize
) ) {
6057 $revObject = $this->getRevisionObject();
6059 # if this variable is subst: the revision id will be blank,
6060 # so just use the parser input size, because the own substituation
6061 # will change the size.
6063 $this->mRevisionSize
= $revObject->getSize();
6065 $this->mRevisionSize
= $this->mInputSize
;
6068 return $this->mRevisionSize
;
6072 * Mutator for $mDefaultSort
6074 * @param string $sort New value
6076 public function setDefaultSort( $sort ) {
6077 $this->mDefaultSort
= $sort;
6078 $this->mOutput
->setProperty( 'defaultsort', $sort );
6082 * Accessor for $mDefaultSort
6083 * Will use the empty string if none is set.
6085 * This value is treated as a prefix, so the
6086 * empty string is equivalent to sorting by
6091 public function getDefaultSort() {
6092 if ( $this->mDefaultSort
!== false ) {
6093 return $this->mDefaultSort
;
6100 * Accessor for $mDefaultSort
6101 * Unlike getDefaultSort(), will return false if none is set
6103 * @return string|bool
6105 public function getCustomDefaultSort() {
6106 return $this->mDefaultSort
;
6109 private static function getSectionNameFromStrippedText( $text ) {
6110 $text = Sanitizer
::normalizeSectionNameWhitespace( $text );
6111 $text = Sanitizer
::decodeCharReferences( $text );
6112 $text = self
::normalizeSectionName( $text );
6116 private static function makeAnchor( $sectionName ) {
6117 return '#' . Sanitizer
::escapeIdForLink( $sectionName );
6120 private function makeLegacyAnchor( $sectionName ) {
6121 $fragmentMode = $this->svcOptions
->get( 'FragmentMode' );
6122 if ( isset( $fragmentMode[1] ) && $fragmentMode[1] === 'legacy' ) {
6123 // ForAttribute() and ForLink() are the same for legacy encoding
6124 $id = Sanitizer
::escapeIdForAttribute( $sectionName, Sanitizer
::ID_FALLBACK
);
6126 $id = Sanitizer
::escapeIdForLink( $sectionName );
6133 * Try to guess the section anchor name based on a wikitext fragment
6134 * presumably extracted from a heading, for example "Header" from
6137 * @param string $text
6138 * @return string Anchor (starting with '#')
6140 public function guessSectionNameFromWikiText( $text ) {
6141 # Strip out wikitext links(they break the anchor)
6142 $text = $this->stripSectionName( $text );
6143 $sectionName = self
::getSectionNameFromStrippedText( $text );
6144 return self
::makeAnchor( $sectionName );
6148 * Same as guessSectionNameFromWikiText(), but produces legacy anchors
6149 * instead, if possible. For use in redirects, since various versions
6150 * of Microsoft browsers interpret Location: headers as something other
6151 * than UTF-8, resulting in breakage.
6153 * @param string $text The section name
6154 * @return string Anchor (starting with '#')
6156 public function guessLegacySectionNameFromWikiText( $text ) {
6157 # Strip out wikitext links(they break the anchor)
6158 $text = $this->stripSectionName( $text );
6159 $sectionName = self
::getSectionNameFromStrippedText( $text );
6160 return $this->makeLegacyAnchor( $sectionName );
6164 * Like guessSectionNameFromWikiText(), but takes already-stripped text as input.
6165 * @param string $text Section name (plain text)
6166 * @return string Anchor (starting with '#')
6168 public static function guessSectionNameFromStrippedText( $text ) {
6169 $sectionName = self
::getSectionNameFromStrippedText( $text );
6170 return self
::makeAnchor( $sectionName );
6174 * Apply the same normalization as code making links to this section would
6176 * @param string $text
6179 private static function normalizeSectionName( $text ) {
6180 # T90902: ensure the same normalization is applied for IDs as to links
6181 /** @var MediaWikiTitleCodec $titleParser */
6182 $titleParser = MediaWikiServices
::getInstance()->getTitleParser();
6183 '@phan-var MediaWikiTitleCodec $titleParser';
6186 $parts = $titleParser->splitTitleString( "#$text" );
6187 } catch ( MalformedTitleException
$ex ) {
6190 return $parts['fragment'];
6194 * Strips a text string of wikitext for use in a section anchor
6196 * Accepts a text string and then removes all wikitext from the
6197 * string and leaves only the resultant text (i.e. the result of
6198 * [[User:WikiSysop|Sysop]] would be "Sysop" and the result of
6199 * [[User:WikiSysop]] would be "User:WikiSysop") - this is intended
6200 * to create valid section anchors by mimicing the output of the
6201 * parser when headings are parsed.
6203 * @param string $text Text string to be stripped of wikitext
6204 * for use in a Section anchor
6205 * @return string Filtered text string
6207 public function stripSectionName( $text ) {
6208 # Strip internal link markup
6209 $text = preg_replace( '/\[\[:?([^[|]+)\|([^[]+)\]\]/', '$2', $text );
6210 $text = preg_replace( '/\[\[:?([^[]+)\|?\]\]/', '$1', $text );
6212 # Strip external link markup
6213 # @todo FIXME: Not tolerant to blank link text
6214 # I.E. [https://www.mediawiki.org] will render as [1] or something depending
6215 # on how many empty links there are on the page - need to figure that out.
6216 $text = preg_replace( '/\[(?i:' . $this->mUrlProtocols
. ')([^ ]+?) ([^[]+)\]/', '$2', $text );
6218 # Parse wikitext quotes (italics & bold)
6219 $text = $this->doQuotes( $text );
6222 $text = StringUtils
::delimiterReplace( '<', '>', '', $text );
6227 * strip/replaceVariables/unstrip for preprocessor regression testing
6229 * @param string $text
6230 * @param Title $title
6231 * @param ParserOptions $options
6232 * @param int $outputType
6236 public function testSrvus( $text, Title
$title, ParserOptions
$options,
6237 $outputType = self
::OT_HTML
6239 $magicScopeVariable = $this->lock();
6240 $this->startParse( $title, $options, $outputType, true );
6242 $text = $this->replaceVariables( $text );
6243 $text = $this->mStripState
->unstripBoth( $text );
6244 $text = Sanitizer
::removeHTMLtags( $text );
6249 * @param string $text
6250 * @param Title $title
6251 * @param ParserOptions $options
6254 public function testPst( $text, Title
$title, ParserOptions
$options ) {
6255 return $this->preSaveTransform( $text, $title, $options->getUser(), $options );
6259 * @param string $text
6260 * @param Title $title
6261 * @param ParserOptions $options
6264 public function testPreprocess( $text, Title
$title, ParserOptions
$options ) {
6265 return $this->testSrvus( $text, $title, $options, self
::OT_PREPROCESS
);
6269 * Call a callback function on all regions of the given text that are not
6270 * inside strip markers, and replace those regions with the return value
6271 * of the callback. For example, with input:
6275 * This will call the callback function twice, with 'aaa' and 'bbb'. Those
6276 * two strings will be replaced with the value returned by the callback in
6280 * @param callable $callback
6284 public function markerSkipCallback( $s, $callback ) {
6287 while ( $i < strlen( $s ) ) {
6288 $markerStart = strpos( $s, self
::MARKER_PREFIX
, $i );
6289 if ( $markerStart === false ) {
6290 $out .= call_user_func( $callback, substr( $s, $i ) );
6293 $out .= call_user_func( $callback, substr( $s, $i, $markerStart - $i ) );
6294 $markerEnd = strpos( $s, self
::MARKER_SUFFIX
, $markerStart );
6295 if ( $markerEnd === false ) {
6296 $out .= substr( $s, $markerStart );
6299 $markerEnd +
= strlen( self
::MARKER_SUFFIX
);
6300 $out .= substr( $s, $markerStart, $markerEnd - $markerStart );
6309 * Remove any strip markers found in the given text.
6311 * @param string $text
6314 public function killMarkers( $text ) {
6315 return $this->mStripState
->killMarkers( $text );
6319 * Save the parser state required to convert the given half-parsed text to
6320 * HTML. "Half-parsed" in this context means the output of
6321 * recursiveTagParse() or internalParse(). This output has strip markers
6322 * from replaceVariables (extensionSubstitution() etc.), and link
6323 * placeholders from replaceLinkHolders().
6325 * Returns an array which can be serialized and stored persistently. This
6326 * array can later be loaded into another parser instance with
6327 * unserializeHalfParsedText(). The text can then be safely incorporated into
6328 * the return value of a parser hook.
6330 * @deprecated since 1.31
6331 * @param string $text
6335 public function serializeHalfParsedText( $text ) {
6336 wfDeprecated( __METHOD__
, '1.31' );
6339 'version' => self
::HALF_PARSED_VERSION
,
6340 'stripState' => $this->mStripState
->getSubState( $text ),
6341 'linkHolders' => $this->mLinkHolders
->getSubArray( $text )
6347 * Load the parser state given in the $data array, which is assumed to
6348 * have been generated by serializeHalfParsedText(). The text contents is
6349 * extracted from the array, and its markers are transformed into markers
6350 * appropriate for the current Parser instance. This transformed text is
6351 * returned, and can be safely included in the return value of a parser
6354 * If the $data array has been stored persistently, the caller should first
6355 * check whether it is still valid, by calling isValidHalfParsedText().
6357 * @deprecated since 1.31
6358 * @param array $data Serialized data
6359 * @throws MWException
6362 public function unserializeHalfParsedText( $data ) {
6363 wfDeprecated( __METHOD__
, '1.31' );
6364 if ( !isset( $data['version'] ) ||
$data['version'] != self
::HALF_PARSED_VERSION
) {
6365 throw new MWException( __METHOD__
. ': invalid version' );
6368 # First, extract the strip state.
6369 $texts = [ $data['text'] ];
6370 $texts = $this->mStripState
->merge( $data['stripState'], $texts );
6372 # Now renumber links
6373 $texts = $this->mLinkHolders
->mergeForeign( $data['linkHolders'], $texts );
6375 # Should be good to go.
6380 * Returns true if the given array, presumed to be generated by
6381 * serializeHalfParsedText(), is compatible with the current version of the
6384 * @deprecated since 1.31
6385 * @param array $data
6389 public function isValidHalfParsedText( $data ) {
6390 wfDeprecated( __METHOD__
, '1.31' );
6391 return isset( $data['version'] ) && $data['version'] == self
::HALF_PARSED_VERSION
;
6395 * Parsed a width param of imagelink like 300px or 200x300px
6397 * @param string $value
6398 * @param bool $parseHeight
6403 public static function parseWidthParam( $value, $parseHeight = true ) {
6404 $parsedWidthParam = [];
6405 if ( $value === '' ) {
6406 return $parsedWidthParam;
6409 # (T15500) In both cases (width/height and width only),
6410 # permit trailing "px" for backward compatibility.
6411 if ( $parseHeight && preg_match( '/^([0-9]*)x([0-9]*)\s*(?:px)?\s*$/', $value, $m ) ) {
6412 $width = intval( $m[1] );
6413 $height = intval( $m[2] );
6414 $parsedWidthParam['width'] = $width;
6415 $parsedWidthParam['height'] = $height;
6416 } elseif ( preg_match( '/^[0-9]*\s*(?:px)?\s*$/', $value ) ) {
6417 $width = intval( $value );
6418 $parsedWidthParam['width'] = $width;
6420 return $parsedWidthParam;
6424 * Lock the current instance of the parser.
6426 * This is meant to stop someone from calling the parser
6427 * recursively and messing up all the strip state.
6429 * @throws MWException If parser is in a parse
6430 * @return ScopedCallback The lock will be released once the return value goes out of scope.
6432 protected function lock() {
6433 if ( $this->mInParse
) {
6434 throw new MWException( "Parser state cleared while parsing. "
6435 . "Did you call Parser::parse recursively? Lock is held by: " . $this->mInParse
);
6438 // Save the backtrace when locking, so that if some code tries locking again,
6439 // we can print the lock owner's backtrace for easier debugging
6441 $this->mInParse
= $e->getTraceAsString();
6443 $recursiveCheck = new ScopedCallback( function () {
6444 $this->mInParse
= false;
6447 return $recursiveCheck;
6451 * Strip outer <p></p> tag from the HTML source of a single paragraph.
6453 * Returns original HTML if the <p/> tag has any attributes, if there's no wrapping <p/> tag,
6454 * or if there is more than one <p/> tag in the input HTML.
6456 * @param string $html
6460 public static function stripOuterParagraph( $html ) {
6462 if ( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $html, $m ) && strpos( $m[1], '</p>' ) === false ) {
6470 * Return this parser if it is not doing anything, otherwise
6471 * get a fresh parser. You can use this method by doing
6472 * $newParser = $oldParser->getFreshParser(), or more simply
6473 * $oldParser->getFreshParser()->parse( ... );
6474 * if you're unsure if $oldParser is safe to use.
6477 * @return Parser A parser object that is not parsing anything
6479 public function getFreshParser() {
6480 if ( $this->mInParse
) {
6481 return $this->factory
->create();
6488 * Set's up the PHP implementation of OOUI for use in this request
6489 * and instructs OutputPage to enable OOUI for itself.
6493 public function enableOOUI() {
6494 OutputPage
::setupOOUI();
6495 $this->mOutput
->setEnableOOUI( true );
6499 * @param string $flag
6500 * @param string $reason
6502 protected function setOutputFlag( $flag, $reason ) {
6503 $this->mOutput
->setFlag( $flag );
6504 $name = $this->getTitle()->getPrefixedText();
6505 $this->logger
->debug( __METHOD__
. ": set $flag flag on '$name'; $reason" );