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 unset( $this->mLinkHolders
);
416 // @phan-suppress-next-line PhanTypeSuspiciousNonTraversableForeach
417 foreach ( $this as $name => $value ) {
418 unset( $this->$name );
423 * Allow extensions to clean up when the parser is cloned
425 public function __clone() {
426 $this->mInParse
= false;
428 // T58226: When you create a reference "to" an object field, that
429 // makes the object field itself be a reference too (until the other
430 // reference goes out of scope). When cloning, any field that's a
431 // reference is copied as a reference in the new object. Both of these
432 // are defined PHP5 behaviors, as inconvenient as it is for us when old
433 // hooks from PHP4 days are passing fields by reference.
434 foreach ( [ 'mStripState', 'mVarCache' ] as $k ) {
435 // Make a non-reference copy of the field, then rebind the field to
436 // reference the new copy.
442 Hooks
::run( 'ParserCloned', [ $this ] );
446 * Which class should we use for the preprocessor if not otherwise specified?
449 * @deprecated since 1.34, removing configurability of preprocessor
452 public static function getDefaultPreprocessorClass() {
453 return Preprocessor_Hash
::class;
457 * Do various kinds of initialisation on the first call of the parser
459 public function firstCallInit() {
460 if ( !$this->mFirstCall
) {
463 $this->mFirstCall
= false;
465 CoreParserFunctions
::register( $this );
466 CoreTagHooks
::register( $this );
467 $this->initialiseVariables();
469 // Avoid PHP 7.1 warning from passing $this by reference
471 Hooks
::run( 'ParserFirstCallInit', [ &$parser ] );
479 public function clearState() {
480 $this->firstCallInit();
481 $this->resetOutput();
482 $this->mAutonumber
= 0;
483 $this->mIncludeCount
= [];
484 $this->mLinkHolders
= new LinkHolderArray( $this );
486 $this->mRevisionObject
= $this->mRevisionTimestamp
=
487 $this->mRevisionId
= $this->mRevisionUser
= $this->mRevisionSize
= null;
488 $this->mVarCache
= [];
490 $this->mLangLinkLanguages
= [];
491 $this->currentRevisionCache
= null;
493 $this->mStripState
= new StripState( $this );
495 # Clear these on every parse, T6549
496 $this->mTplRedirCache
= $this->mTplDomCache
= [];
498 $this->mShowToc
= true;
499 $this->mForceTocPosition
= false;
500 $this->mIncludeSizes
= [
504 $this->mPPNodeCount
= 0;
505 $this->mGeneratedPPNodeCount
= 0;
506 $this->mHighestExpansionDepth
= 0;
507 $this->mDefaultSort
= false;
508 $this->mHeadings
= [];
509 $this->mDoubleUnderscores
= [];
510 $this->mExpensiveFunctionCount
= 0;
513 if ( isset( $this->mPreprocessor
) && $this->mPreprocessor
->parser
!== $this ) {
514 $this->mPreprocessor
= null;
517 $this->mProfiler
= new SectionProfiler();
519 // Avoid PHP 7.1 warning from passing $this by reference
521 Hooks
::run( 'ParserClearState', [ &$parser ] );
525 * Reset the ParserOutput
527 public function resetOutput() {
528 $this->mOutput
= new ParserOutput
;
529 $this->mOptions
->registerWatcher( [ $this->mOutput
, 'recordOption' ] );
533 * Convert wikitext to HTML
534 * Do not call this function recursively.
536 * @param string $text Text we want to parse
537 * @param-taint $text escapes_htmlnoent
538 * @param Title $title
539 * @param ParserOptions $options
540 * @param bool $linestart
541 * @param bool $clearState
542 * @param int|null $revid ID of the revision being rendered. This is used to render
543 * REVISION* magic words. 0 means that any current revision will be used. Null means
544 * that {{REVISIONID}}/{{REVISIONUSER}} will be empty and {{REVISIONTIMESTAMP}} will
545 * use the current timestamp.
546 * @return ParserOutput A ParserOutput
547 * @return-taint escaped
549 public function parse(
550 $text, Title
$title, ParserOptions
$options,
551 $linestart = true, $clearState = true, $revid = null
554 // We use U+007F DELETE to construct strip markers, so we have to make
555 // sure that this character does not occur in the input text.
556 $text = strtr( $text, "\x7f", "?" );
557 $magicScopeVariable = $this->lock();
559 // Strip U+0000 NULL (T159174)
560 $text = str_replace( "\000", '', $text );
562 $this->startParse( $title, $options, self
::OT_HTML
, $clearState );
564 $this->currentRevisionCache
= null;
565 $this->mInputSize
= strlen( $text );
566 if ( $this->mOptions
->getEnableLimitReport() ) {
567 $this->mOutput
->resetParseStartTime();
570 $oldRevisionId = $this->mRevisionId
;
571 $oldRevisionObject = $this->mRevisionObject
;
572 $oldRevisionTimestamp = $this->mRevisionTimestamp
;
573 $oldRevisionUser = $this->mRevisionUser
;
574 $oldRevisionSize = $this->mRevisionSize
;
575 if ( $revid !== null ) {
576 $this->mRevisionId
= $revid;
577 $this->mRevisionObject
= null;
578 $this->mRevisionTimestamp
= null;
579 $this->mRevisionUser
= null;
580 $this->mRevisionSize
= null;
583 // Avoid PHP 7.1 warning from passing $this by reference
585 Hooks
::run( 'ParserBeforeStrip', [ &$parser, &$text, &$this->mStripState
] );
587 Hooks
::run( 'ParserAfterStrip', [ &$parser, &$text, &$this->mStripState
] );
588 $text = $this->internalParse( $text );
589 Hooks
::run( 'ParserAfterParse', [ &$parser, &$text, &$this->mStripState
] );
591 $text = $this->internalParseHalfParsed( $text, true, $linestart );
594 * A converted title will be provided in the output object if title and
595 * content conversion are enabled, the article text does not contain
596 * a conversion-suppressing double-underscore tag, and no
597 * {{DISPLAYTITLE:...}} is present. DISPLAYTITLE takes precedence over
598 * automatic link conversion.
600 if ( !( $options->getDisableTitleConversion()
601 ||
isset( $this->mDoubleUnderscores
['nocontentconvert'] )
602 ||
isset( $this->mDoubleUnderscores
['notitleconvert'] )
603 ||
$this->mOutput
->getDisplayTitle() !== false )
605 $convruletitle = $this->getTargetLanguage()->getConvRuleTitle();
606 if ( $convruletitle ) {
607 $this->mOutput
->setTitleText( $convruletitle );
609 $titleText = $this->getTargetLanguage()->convertTitle( $title );
610 $this->mOutput
->setTitleText( $titleText );
614 # Compute runtime adaptive expiry if set
615 $this->mOutput
->finalizeAdaptiveCacheExpiry();
617 # Warn if too many heavyweight parser functions were used
618 if ( $this->mExpensiveFunctionCount
> $this->mOptions
->getExpensiveParserFunctionLimit() ) {
619 $this->limitationWarn( 'expensive-parserfunction',
620 $this->mExpensiveFunctionCount
,
621 $this->mOptions
->getExpensiveParserFunctionLimit()
625 # Information on limits, for the benefit of users who try to skirt them
626 if ( $this->mOptions
->getEnableLimitReport() ) {
627 $text .= $this->makeLimitReport();
630 # Wrap non-interface parser output in a <div> so it can be targeted
632 $class = $this->mOptions
->getWrapOutputClass();
633 if ( $class !== false && !$this->mOptions
->getInterfaceMessage() ) {
634 $this->mOutput
->addWrapperDivClass( $class );
637 $this->mOutput
->setText( $text );
639 $this->mRevisionId
= $oldRevisionId;
640 $this->mRevisionObject
= $oldRevisionObject;
641 $this->mRevisionTimestamp
= $oldRevisionTimestamp;
642 $this->mRevisionUser
= $oldRevisionUser;
643 $this->mRevisionSize
= $oldRevisionSize;
644 $this->mInputSize
= false;
645 $this->currentRevisionCache
= null;
647 return $this->mOutput
;
651 * Set the limit report data in the current ParserOutput, and return the
652 * limit report HTML comment.
656 protected function makeLimitReport() {
657 $maxIncludeSize = $this->mOptions
->getMaxIncludeSize();
659 $cpuTime = $this->mOutput
->getTimeSinceStart( 'cpu' );
660 if ( $cpuTime !== null ) {
661 $this->mOutput
->setLimitReportData( 'limitreport-cputime',
662 sprintf( "%.3f", $cpuTime )
666 $wallTime = $this->mOutput
->getTimeSinceStart( 'wall' );
667 $this->mOutput
->setLimitReportData( 'limitreport-walltime',
668 sprintf( "%.3f", $wallTime )
671 $this->mOutput
->setLimitReportData( 'limitreport-ppvisitednodes',
672 [ $this->mPPNodeCount
, $this->mOptions
->getMaxPPNodeCount() ]
674 $this->mOutput
->setLimitReportData( 'limitreport-ppgeneratednodes',
675 [ $this->mGeneratedPPNodeCount
, $this->mOptions
->getMaxGeneratedPPNodeCount() ]
677 $this->mOutput
->setLimitReportData( 'limitreport-postexpandincludesize',
678 [ $this->mIncludeSizes
['post-expand'], $maxIncludeSize ]
680 $this->mOutput
->setLimitReportData( 'limitreport-templateargumentsize',
681 [ $this->mIncludeSizes
['arg'], $maxIncludeSize ]
683 $this->mOutput
->setLimitReportData( 'limitreport-expansiondepth',
684 [ $this->mHighestExpansionDepth
, $this->mOptions
->getMaxPPExpandDepth() ]
686 $this->mOutput
->setLimitReportData( 'limitreport-expensivefunctioncount',
687 [ $this->mExpensiveFunctionCount
, $this->mOptions
->getExpensiveParserFunctionLimit() ]
690 foreach ( $this->mStripState
->getLimitReport() as list( $key, $value ) ) {
691 $this->mOutput
->setLimitReportData( $key, $value );
694 Hooks
::run( 'ParserLimitReportPrepare', [ $this, $this->mOutput
] );
696 $limitReport = "NewPP limit report\n";
697 if ( $this->svcOptions
->get( 'ShowHostnames' ) ) {
698 $limitReport .= 'Parsed by ' . wfHostname() . "\n";
700 $limitReport .= 'Cached time: ' . $this->mOutput
->getCacheTime() . "\n";
701 $limitReport .= 'Cache expiry: ' . $this->mOutput
->getCacheExpiry() . "\n";
702 $limitReport .= 'Dynamic content: ' .
703 ( $this->mOutput
->hasDynamicContent() ?
'true' : 'false' ) .
705 $limitReport .= 'Complications: [' . implode( ', ', $this->mOutput
->getAllFlags() ) . "]\n";
707 foreach ( $this->mOutput
->getLimitReportData() as $key => $value ) {
708 if ( Hooks
::run( 'ParserLimitReportFormat',
709 [ $key, &$value, &$limitReport, false, false ]
711 $keyMsg = wfMessage( $key )->inLanguage( 'en' )->useDatabase( false );
712 $valueMsg = wfMessage( [ "$key-value-text", "$key-value" ] )
713 ->inLanguage( 'en' )->useDatabase( false );
714 if ( !$valueMsg->exists() ) {
715 $valueMsg = new RawMessage( '$1' );
717 if ( !$keyMsg->isDisabled() && !$valueMsg->isDisabled() ) {
718 $valueMsg->params( $value );
719 $limitReport .= "{$keyMsg->text()}: {$valueMsg->text()}\n";
723 // Since we're not really outputting HTML, decode the entities and
724 // then re-encode the things that need hiding inside HTML comments.
725 $limitReport = htmlspecialchars_decode( $limitReport );
727 // Sanitize for comment. Note '‐' in the replacement is U+2010,
728 // which looks much like the problematic '-'.
729 $limitReport = str_replace( [ '-', '&' ], [ '‐', '&' ], $limitReport );
730 $text = "\n<!-- \n$limitReport-->\n";
732 // Add on template profiling data in human/machine readable way
733 $dataByFunc = $this->mProfiler
->getFunctionStats();
734 uasort( $dataByFunc, function ( $a, $b ) {
735 return $b['real'] <=> $a['real']; // descending order
738 foreach ( array_slice( $dataByFunc, 0, 10 ) as $item ) {
739 $profileReport[] = sprintf( "%6.2f%% %8.3f %6d %s",
740 $item['%real'], $item['real'], $item['calls'],
741 htmlspecialchars( $item['name'] ) );
743 $text .= "<!--\nTransclusion expansion time report (%,ms,calls,template)\n";
744 $text .= implode( "\n", $profileReport ) . "\n-->\n";
746 $this->mOutput
->setLimitReportData( 'limitreport-timingprofile', $profileReport );
748 // Add other cache related metadata
749 if ( $this->svcOptions
->get( 'ShowHostnames' ) ) {
750 $this->mOutput
->setLimitReportData( 'cachereport-origin', wfHostname() );
752 $this->mOutput
->setLimitReportData( 'cachereport-timestamp',
753 $this->mOutput
->getCacheTime() );
754 $this->mOutput
->setLimitReportData( 'cachereport-ttl',
755 $this->mOutput
->getCacheExpiry() );
756 $this->mOutput
->setLimitReportData( 'cachereport-transientcontent',
757 $this->mOutput
->hasDynamicContent() );
759 if ( $this->mGeneratedPPNodeCount
> $this->mOptions
->getMaxGeneratedPPNodeCount() / 10 ) {
760 wfDebugLog( 'generated-pp-node-count', $this->mGeneratedPPNodeCount
. ' ' .
761 $this->mTitle
->getPrefixedDBkey() );
767 * Half-parse wikitext to half-parsed HTML. This recursive parser entry point
768 * can be called from an extension tag hook.
770 * The output of this function IS NOT SAFE PARSED HTML; it is "half-parsed"
771 * instead, which means that lists and links have not been fully parsed yet,
772 * and strip markers are still present.
774 * Use recursiveTagParseFully() to fully parse wikitext to output-safe HTML.
776 * Use this function if you're a parser tag hook and you want to parse
777 * wikitext before or after applying additional transformations, and you
778 * intend to *return the result as hook output*, which will cause it to go
779 * through the rest of parsing process automatically.
781 * If $frame is not provided, then template variables (e.g., {{{1}}}) within
782 * $text are not expanded
784 * @param string $text Text extension wants to have parsed
785 * @param-taint $text escapes_htmlnoent
786 * @param bool|PPFrame $frame The frame to use for expanding any template variables
787 * @return string UNSAFE half-parsed HTML
788 * @return-taint escaped
790 public function recursiveTagParse( $text, $frame = false ) {
791 // Avoid PHP 7.1 warning from passing $this by reference
793 Hooks
::run( 'ParserBeforeStrip', [ &$parser, &$text, &$this->mStripState
] );
794 Hooks
::run( 'ParserAfterStrip', [ &$parser, &$text, &$this->mStripState
] );
795 $text = $this->internalParse( $text, false, $frame );
800 * Fully parse wikitext to fully parsed HTML. This recursive parser entry
801 * point can be called from an extension tag hook.
803 * The output of this function is fully-parsed HTML that is safe for output.
804 * If you're a parser tag hook, you might want to use recursiveTagParse()
807 * If $frame is not provided, then template variables (e.g., {{{1}}}) within
808 * $text are not expanded
812 * @param string $text Text extension wants to have parsed
813 * @param-taint $text escapes_htmlnoent
814 * @param bool|PPFrame $frame The frame to use for expanding any template variables
815 * @return string Fully parsed HTML
816 * @return-taint escaped
818 public function recursiveTagParseFully( $text, $frame = false ) {
819 $text = $this->recursiveTagParse( $text, $frame );
820 $text = $this->internalParseHalfParsed( $text, false );
825 * Expand templates and variables in the text, producing valid, static wikitext.
826 * Also removes comments.
827 * Do not call this function recursively.
828 * @param string $text
829 * @param Title|null $title
830 * @param ParserOptions $options
831 * @param int|null $revid
832 * @param bool|PPFrame $frame
833 * @return mixed|string
835 public function preprocess( $text, Title
$title = null,
836 ParserOptions
$options, $revid = null, $frame = false
838 $magicScopeVariable = $this->lock();
839 $this->startParse( $title, $options, self
::OT_PREPROCESS
, true );
840 if ( $revid !== null ) {
841 $this->mRevisionId
= $revid;
843 // Avoid PHP 7.1 warning from passing $this by reference
845 Hooks
::run( 'ParserBeforeStrip', [ &$parser, &$text, &$this->mStripState
] );
846 Hooks
::run( 'ParserAfterStrip', [ &$parser, &$text, &$this->mStripState
] );
847 $text = $this->replaceVariables( $text, $frame );
848 $text = $this->mStripState
->unstripBoth( $text );
853 * Recursive parser entry point that can be called from an extension tag
856 * @param string $text Text to be expanded
857 * @param bool|PPFrame $frame The frame to use for expanding any template variables
861 public function recursivePreprocess( $text, $frame = false ) {
862 $text = $this->replaceVariables( $text, $frame );
863 $text = $this->mStripState
->unstripBoth( $text );
868 * Process the wikitext for the "?preload=" feature. (T7210)
870 * "<noinclude>", "<includeonly>" etc. are parsed as for template
871 * transclusion, comments, templates, arguments, tags hooks and parser
872 * functions are untouched.
874 * @param string $text
875 * @param Title $title
876 * @param ParserOptions $options
877 * @param array $params
880 public function getPreloadText( $text, Title
$title, ParserOptions
$options, $params = [] ) {
881 $msg = new RawMessage( $text );
882 $text = $msg->params( $params )->plain();
884 # Parser (re)initialisation
885 $magicScopeVariable = $this->lock();
886 $this->startParse( $title, $options, self
::OT_PLAIN
, true );
888 $flags = PPFrame
::NO_ARGS | PPFrame
::NO_TEMPLATES
;
889 $dom = $this->preprocessToDom( $text, self
::PTD_FOR_INCLUSION
);
890 $text = $this->getPreprocessor()->newFrame()->expand( $dom, $flags );
891 $text = $this->mStripState
->unstripBoth( $text );
896 * Set the current user.
897 * Should only be used when doing pre-save transform.
899 * @param User|null $user User object or null (to reset)
901 public function setUser( $user ) {
902 $this->mUser
= $user;
906 * Set the context title
910 public function setTitle( $t ) {
912 $t = Title
::newFromText( 'NO TITLE' );
915 if ( $t->hasFragment() ) {
916 # Strip the fragment to avoid various odd effects
917 $this->mTitle
= $t->createFragmentTarget( '' );
924 * Accessor for the Title object
928 public function getTitle() {
929 return $this->mTitle
;
933 * Accessor/mutator for the Title object
935 * @param Title|null $x Title object or null to just get the current one
938 public function Title( $x = null ) {
939 return wfSetVar( $this->mTitle
, $x );
943 * Set the output type
945 * @param int $ot New value
947 public function setOutputType( $ot ) {
948 $this->mOutputType
= $ot;
951 'html' => $ot == self
::OT_HTML
,
952 'wiki' => $ot == self
::OT_WIKI
,
953 'pre' => $ot == self
::OT_PREPROCESS
,
954 'plain' => $ot == self
::OT_PLAIN
,
959 * Accessor/mutator for the output type
961 * @param int|null $x New value or null to just get the current one
964 public function OutputType( $x = null ) {
965 return wfSetVar( $this->mOutputType
, $x );
969 * Get the ParserOutput object
971 * @return ParserOutput
973 public function getOutput() {
974 return $this->mOutput
;
978 * Get the ParserOptions object
980 * @return ParserOptions
982 public function getOptions() {
983 return $this->mOptions
;
987 * Accessor/mutator for the ParserOptions object
989 * @param ParserOptions|null $x New value or null to just get the current one
990 * @return ParserOptions Current ParserOptions object
992 public function Options( $x = null ) {
993 return wfSetVar( $this->mOptions
, $x );
999 public function nextLinkID() {
1000 return $this->mLinkID++
;
1006 public function setLinkID( $id ) {
1007 $this->mLinkID
= $id;
1011 * Get a language object for use in parser functions such as {{FORMATNUM:}}
1014 public function getFunctionLang() {
1015 return $this->getTargetLanguage();
1019 * Get the target language for the content being parsed. This is usually the
1020 * language that the content is in.
1024 * @throws MWException
1027 public function getTargetLanguage() {
1028 $target = $this->mOptions
->getTargetLanguage();
1030 if ( $target !== null ) {
1032 } elseif ( $this->mOptions
->getInterfaceMessage() ) {
1033 return $this->mOptions
->getUserLangObj();
1034 } elseif ( is_null( $this->mTitle
) ) {
1035 throw new MWException( __METHOD__
. ': $this->mTitle is null' );
1038 return $this->mTitle
->getPageLanguage();
1042 * Get the language object for language conversion
1043 * @deprecated since 1.32, just use getTargetLanguage()
1044 * @return Language|null
1046 public function getConverterLanguage() {
1047 return $this->getTargetLanguage();
1051 * Get a User object either from $this->mUser, if set, or from the
1052 * ParserOptions object otherwise
1056 public function getUser() {
1057 if ( !is_null( $this->mUser
) ) {
1058 return $this->mUser
;
1060 return $this->mOptions
->getUser();
1064 * Get a preprocessor object
1066 * @return Preprocessor
1068 public function getPreprocessor() {
1069 if ( !isset( $this->mPreprocessor
) ) {
1070 $class = $this->svcOptions
->get( 'preprocessorClass' );
1071 $this->mPreprocessor
= new $class( $this );
1073 return $this->mPreprocessor
;
1077 * Get a LinkRenderer instance to make links with
1080 * @return LinkRenderer
1082 public function getLinkRenderer() {
1083 // XXX We make the LinkRenderer with current options and then cache it forever
1084 if ( !$this->mLinkRenderer
) {
1085 $this->mLinkRenderer
= $this->linkRendererFactory
->create();
1086 $this->mLinkRenderer
->setStubThreshold(
1087 $this->getOptions()->getStubThreshold()
1091 return $this->mLinkRenderer
;
1095 * Get the MagicWordFactory that this Parser is using
1098 * @return MagicWordFactory
1100 public function getMagicWordFactory() {
1101 return $this->magicWordFactory
;
1105 * Get the content language that this Parser is using
1110 public function getContentLanguage() {
1111 return $this->contLang
;
1115 * Replaces all occurrences of HTML-style comments and the given tags
1116 * in the text with a random marker and returns the next text. The output
1117 * parameter $matches will be an associative array filled with data in
1124 * [ 'param' => 'x' ],
1125 * '<element param="x">tag content</element>' ]
1128 * @param array $elements List of element names. Comments are always extracted.
1129 * @param string $text Source text string.
1130 * @param array &$matches Out parameter, Array: extracted tags
1131 * @return string Stripped text
1133 public static function extractTagsAndParams( $elements, $text, &$matches ) {
1138 $taglist = implode( '|', $elements );
1139 $start = "/<($taglist)(\\s+[^>]*?|\\s*?)(\/?>)|<(!--)/i";
1141 while ( $text != '' ) {
1142 $p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE
);
1144 if ( count( $p ) < 5 ) {
1147 if ( count( $p ) > 5 ) {
1155 list( , $element, $attributes, $close, $inside ) = $p;
1158 $marker = self
::MARKER_PREFIX
. "-$element-" . sprintf( '%08X', $n++
) . self
::MARKER_SUFFIX
;
1159 $stripped .= $marker;
1161 if ( $close === '/>' ) {
1162 # Empty element tag, <tag />
1167 if ( $element === '!--' ) {
1170 $end = "/(<\\/$element\\s*>)/i";
1172 $q = preg_split( $end, $inside, 2, PREG_SPLIT_DELIM_CAPTURE
);
1174 if ( count( $q ) < 3 ) {
1175 # No end tag -- let it run out to the end of the text.
1179 list( , $tail, $text ) = $q;
1183 $matches[$marker] = [ $element,
1185 Sanitizer
::decodeTagAttributes( $attributes ),
1186 "<$element$attributes$close$content$tail" ];
1192 * Get a list of strippable XML-like elements
1196 public function getStripList() {
1197 return $this->mStripList
;
1201 * Get the StripState
1203 * @return StripState
1205 public function getStripState() {
1206 return $this->mStripState
;
1210 * Add an item to the strip state
1211 * Returns the unique tag which must be inserted into the stripped text
1212 * The tag will be replaced with the original text in unstrip()
1214 * @param string $text
1218 public function insertStripItem( $text ) {
1219 $marker = self
::MARKER_PREFIX
. "-item-{$this->mMarkerIndex}-" . self
::MARKER_SUFFIX
;
1220 $this->mMarkerIndex++
;
1221 $this->mStripState
->addGeneral( $marker, $text );
1226 * parse the wiki syntax used to render tables
1229 * @param string $text
1232 public function doTableStuff( $text ) {
1233 $lines = StringUtils
::explode( "\n", $text );
1235 $td_history = []; # Is currently a td tag open?
1236 $last_tag_history = []; # Save history of last lag activated (td, th or caption)
1237 $tr_history = []; # Is currently a tr tag open?
1238 $tr_attributes = []; # history of tr attributes
1239 $has_opened_tr = []; # Did this table open a <tr> element?
1240 $indent_level = 0; # indent level of the table
1242 foreach ( $lines as $outLine ) {
1243 $line = trim( $outLine );
1245 if ( $line === '' ) { # empty line, go to next line
1246 $out .= $outLine . "\n";
1250 $first_character = $line[0];
1251 $first_two = substr( $line, 0, 2 );
1254 if ( preg_match( '/^(:*)\s*\{\|(.*)$/', $line, $matches ) ) {
1255 # First check if we are starting a new table
1256 $indent_level = strlen( $matches[1] );
1258 $attributes = $this->mStripState
->unstripBoth( $matches[2] );
1259 $attributes = Sanitizer
::fixTagAttributes( $attributes, 'table' );
1261 $outLine = str_repeat( '<dl><dd>', $indent_level ) . "<table{$attributes}>";
1262 array_push( $td_history, false );
1263 array_push( $last_tag_history, '' );
1264 array_push( $tr_history, false );
1265 array_push( $tr_attributes, '' );
1266 array_push( $has_opened_tr, false );
1267 } elseif ( count( $td_history ) == 0 ) {
1268 # Don't do any of the following
1269 $out .= $outLine . "\n";
1271 } elseif ( $first_two === '|}' ) {
1272 # We are ending a table
1273 $line = '</table>' . substr( $line, 2 );
1274 $last_tag = array_pop( $last_tag_history );
1276 if ( !array_pop( $has_opened_tr ) ) {
1277 $line = "<tr><td></td></tr>{$line}";
1280 if ( array_pop( $tr_history ) ) {
1281 $line = "</tr>{$line}";
1284 if ( array_pop( $td_history ) ) {
1285 $line = "</{$last_tag}>{$line}";
1287 array_pop( $tr_attributes );
1288 if ( $indent_level > 0 ) {
1289 $outLine = rtrim( $line ) . str_repeat( '</dd></dl>', $indent_level );
1293 } elseif ( $first_two === '|-' ) {
1294 # Now we have a table row
1295 $line = preg_replace( '#^\|-+#', '', $line );
1297 # Whats after the tag is now only attributes
1298 $attributes = $this->mStripState
->unstripBoth( $line );
1299 $attributes = Sanitizer
::fixTagAttributes( $attributes, 'tr' );
1300 array_pop( $tr_attributes );
1301 array_push( $tr_attributes, $attributes );
1304 $last_tag = array_pop( $last_tag_history );
1305 array_pop( $has_opened_tr );
1306 array_push( $has_opened_tr, true );
1308 if ( array_pop( $tr_history ) ) {
1312 if ( array_pop( $td_history ) ) {
1313 $line = "</{$last_tag}>{$line}";
1317 array_push( $tr_history, false );
1318 array_push( $td_history, false );
1319 array_push( $last_tag_history, '' );
1320 } elseif ( $first_character === '|'
1321 ||
$first_character === '!'
1322 ||
$first_two === '|+'
1324 # This might be cell elements, td, th or captions
1325 if ( $first_two === '|+' ) {
1326 $first_character = '+';
1327 $line = substr( $line, 2 );
1329 $line = substr( $line, 1 );
1332 // Implies both are valid for table headings.
1333 if ( $first_character === '!' ) {
1334 $line = StringUtils
::replaceMarkup( '!!', '||', $line );
1337 # Split up multiple cells on the same line.
1338 # FIXME : This can result in improper nesting of tags processed
1339 # by earlier parser steps.
1340 $cells = explode( '||', $line );
1344 # Loop through each table cell
1345 foreach ( $cells as $cell ) {
1347 if ( $first_character !== '+' ) {
1348 $tr_after = array_pop( $tr_attributes );
1349 if ( !array_pop( $tr_history ) ) {
1350 $previous = "<tr{$tr_after}>\n";
1352 array_push( $tr_history, true );
1353 array_push( $tr_attributes, '' );
1354 array_pop( $has_opened_tr );
1355 array_push( $has_opened_tr, true );
1358 $last_tag = array_pop( $last_tag_history );
1360 if ( array_pop( $td_history ) ) {
1361 $previous = "</{$last_tag}>\n{$previous}";
1364 if ( $first_character === '|' ) {
1366 } elseif ( $first_character === '!' ) {
1368 } elseif ( $first_character === '+' ) {
1369 $last_tag = 'caption';
1374 array_push( $last_tag_history, $last_tag );
1376 # A cell could contain both parameters and data
1377 $cell_data = explode( '|', $cell, 2 );
1379 # T2553: Note that a '|' inside an invalid link should not
1380 # be mistaken as delimiting cell parameters
1381 # Bug T153140: Neither should language converter markup.
1382 if ( preg_match( '/\[\[|-\{/', $cell_data[0] ) === 1 ) {
1383 $cell = "{$previous}<{$last_tag}>" . trim( $cell );
1384 } elseif ( count( $cell_data ) == 1 ) {
1385 // Whitespace in cells is trimmed
1386 $cell = "{$previous}<{$last_tag}>" . trim( $cell_data[0] );
1388 $attributes = $this->mStripState
->unstripBoth( $cell_data[0] );
1389 $attributes = Sanitizer
::fixTagAttributes( $attributes, $last_tag );
1390 // Whitespace in cells is trimmed
1391 $cell = "{$previous}<{$last_tag}{$attributes}>" . trim( $cell_data[1] );
1395 array_push( $td_history, true );
1398 $out .= $outLine . "\n";
1401 # Closing open td, tr && table
1402 while ( count( $td_history ) > 0 ) {
1403 if ( array_pop( $td_history ) ) {
1406 if ( array_pop( $tr_history ) ) {
1409 if ( !array_pop( $has_opened_tr ) ) {
1410 $out .= "<tr><td></td></tr>\n";
1413 $out .= "</table>\n";
1416 # Remove trailing line-ending (b/c)
1417 if ( substr( $out, -1 ) === "\n" ) {
1418 $out = substr( $out, 0, -1 );
1421 # special case: don't return empty table
1422 if ( $out === "<table>\n<tr><td></td></tr>\n</table>" ) {
1430 * Helper function for parse() that transforms wiki markup into half-parsed
1431 * HTML. Only called for $mOutputType == self::OT_HTML.
1435 * @param string $text The text to parse
1436 * @param-taint $text escapes_html
1437 * @param bool $isMain Whether this is being called from the main parse() function
1438 * @param PPFrame|bool $frame A pre-processor frame
1442 public function internalParse( $text, $isMain = true, $frame = false ) {
1445 // Avoid PHP 7.1 warning from passing $this by reference
1448 # Hook to suspend the parser in this state
1449 if ( !Hooks
::run( 'ParserBeforeInternalParse', [ &$parser, &$text, &$this->mStripState
] ) ) {
1453 # if $frame is provided, then use $frame for replacing any variables
1455 # use frame depth to infer how include/noinclude tags should be handled
1456 # depth=0 means this is the top-level document; otherwise it's an included document
1457 if ( !$frame->depth
) {
1460 $flag = self
::PTD_FOR_INCLUSION
;
1462 $dom = $this->preprocessToDom( $text, $flag );
1463 $text = $frame->expand( $dom );
1465 # if $frame is not provided, then use old-style replaceVariables
1466 $text = $this->replaceVariables( $text );
1469 Hooks
::run( 'InternalParseBeforeSanitize', [ &$parser, &$text, &$this->mStripState
] );
1470 $text = Sanitizer
::removeHTMLtags(
1472 [ $this, 'attributeStripCallback' ],
1474 array_keys( $this->mTransparentTagHooks
),
1476 [ $this, 'addTrackingCategory' ]
1478 Hooks
::run( 'InternalParseBeforeLinks', [ &$parser, &$text, &$this->mStripState
] );
1480 # Tables need to come after variable replacement for things to work
1481 # properly; putting them before other transformations should keep
1482 # exciting things like link expansions from showing up in surprising
1484 $text = $this->doTableStuff( $text );
1486 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
1488 $text = $this->doDoubleUnderscore( $text );
1490 $text = $this->doHeadings( $text );
1491 $text = $this->replaceInternalLinks( $text );
1492 $text = $this->doAllQuotes( $text );
1493 $text = $this->replaceExternalLinks( $text );
1495 # replaceInternalLinks may sometimes leave behind
1496 # absolute URLs, which have to be masked to hide them from replaceExternalLinks
1497 $text = str_replace( self
::MARKER_PREFIX
. 'NOPARSE', '', $text );
1499 $text = $this->doMagicLinks( $text );
1500 $text = $this->formatHeadings( $text, $origText, $isMain );
1506 * Helper function for parse() that transforms half-parsed HTML into fully
1509 * @param string $text
1510 * @param bool $isMain
1511 * @param bool $linestart
1514 private function internalParseHalfParsed( $text, $isMain = true, $linestart = true ) {
1515 $text = $this->mStripState
->unstripGeneral( $text );
1517 // Avoid PHP 7.1 warning from passing $this by reference
1521 Hooks
::run( 'ParserAfterUnstrip', [ &$parser, &$text ] );
1524 # Clean up special characters, only run once, next-to-last before doBlockLevels
1525 $text = Sanitizer
::armorFrenchSpaces( $text );
1527 $text = $this->doBlockLevels( $text, $linestart );
1529 $this->replaceLinkHolders( $text );
1532 * The input doesn't get language converted if
1534 * b) Content isn't converted
1535 * c) It's a conversion table
1536 * d) it is an interface message (which is in the user language)
1538 if ( !( $this->mOptions
->getDisableContentConversion()
1539 ||
isset( $this->mDoubleUnderscores
['nocontentconvert'] ) )
1540 && !$this->mOptions
->getInterfaceMessage()
1542 # The position of the convert() call should not be changed. it
1543 # assumes that the links are all replaced and the only thing left
1544 # is the <nowiki> mark.
1545 $text = $this->getTargetLanguage()->convert( $text );
1548 $text = $this->mStripState
->unstripNoWiki( $text );
1551 Hooks
::run( 'ParserBeforeTidy', [ &$parser, &$text ] );
1554 $text = $this->replaceTransparentTags( $text );
1555 $text = $this->mStripState
->unstripGeneral( $text );
1557 $text = Sanitizer
::normalizeCharReferences( $text );
1559 if ( MWTidy
::isEnabled() ) {
1560 if ( $this->mOptions
->getTidy() ) {
1561 $text = MWTidy
::tidy( $text );
1564 # attempt to sanitize at least some nesting problems
1565 # (T4702 and quite a few others)
1566 # This code path is buggy and deprecated!
1567 wfDeprecated( 'disabling tidy', '1.33' );
1569 # ''Something [http://www.cool.com cool''] -->
1570 # <i>Something</i><a href="http://www.cool.com"..><i>cool></i></a>
1571 '/(<([bi])>)(<([bi])>)?([^<]*)(<\/?a[^<]*>)([^<]*)(<\/\\4>)?(<\/\\2>)/' =>
1572 '\\1\\3\\5\\8\\9\\6\\1\\3\\7\\8\\9',
1573 # fix up an anchor inside another anchor, only
1574 # at least for a single single nested link (T5695)
1575 '/(<a[^>]+>)([^<]*)(<a[^>]+>[^<]*)<\/a>(.*)<\/a>/' =>
1576 '\\1\\2</a>\\3</a>\\1\\4</a>',
1577 # fix div inside inline elements- doBlockLevels won't wrap a line which
1578 # contains a div, so fix it up here; replace
1579 # div with escaped text
1580 '/(<([aib]) [^>]+>)([^<]*)(<div([^>]*)>)(.*)(<\/div>)([^<]*)(<\/\\2>)/' =>
1581 '\\1\\3<div\\5>\\6</div>\\8\\9',
1582 # remove empty italic or bold tag pairs, some
1583 # introduced by rules above
1584 '/<([bi])><\/\\1>/' => '',
1587 $text = preg_replace(
1588 array_keys( $tidyregs ),
1589 array_values( $tidyregs ),
1594 Hooks
::run( 'ParserAfterTidy', [ &$parser, &$text ] );
1601 * Replace special strings like "ISBN xxx" and "RFC xxx" with
1602 * magic external links.
1607 * @param string $text
1611 public function doMagicLinks( $text ) {
1612 $prots = wfUrlProtocolsWithoutProtRel();
1613 $urlChar = self
::EXT_LINK_URL_CLASS
;
1614 $addr = self
::EXT_LINK_ADDR
;
1615 $space = self
::SPACE_NOT_NL
; # non-newline space
1616 $spdash = "(?:-|$space)"; # a dash or a non-newline space
1617 $spaces = "$space++"; # possessive match of 1 or more spaces
1618 $text = preg_replace_callback(
1620 (<a[ \t\r\n>].*?</a>) | # m[1]: Skip link text
1621 (<.*?>) | # m[2]: Skip stuff inside HTML elements' . "
1622 (\b # m[3]: Free external links
1624 ($addr$urlChar*) # m[4]: Post-protocol path
1626 \b(?:RFC|PMID) $spaces # m[5]: RFC or PMID, capture number
1628 \bISBN $spaces ( # m[6]: ISBN, capture number
1629 (?: 97[89] $spdash? )? # optional 13-digit ISBN prefix
1630 (?: [0-9] $spdash? ){9} # 9 digits with opt. delimiters
1631 [0-9Xx] # check digit
1633 )!xu", [ $this, 'magicLinkCallback' ], $text );
1638 * @throws MWException
1640 * @return string HTML
1642 public function magicLinkCallback( $m ) {
1643 if ( isset( $m[1] ) && $m[1] !== '' ) {
1646 } elseif ( isset( $m[2] ) && $m[2] !== '' ) {
1649 } elseif ( isset( $m[3] ) && $m[3] !== '' ) {
1650 # Free external link
1651 return $this->makeFreeExternalLink( $m[0], strlen( $m[4] ) );
1652 } elseif ( isset( $m[5] ) && $m[5] !== '' ) {
1654 if ( substr( $m[0], 0, 3 ) === 'RFC' ) {
1655 if ( !$this->mOptions
->getMagicRFCLinks() ) {
1660 $cssClass = 'mw-magiclink-rfc';
1661 $trackingCat = 'magiclink-tracking-rfc';
1663 } elseif ( substr( $m[0], 0, 4 ) === 'PMID' ) {
1664 if ( !$this->mOptions
->getMagicPMIDLinks() ) {
1668 $urlmsg = 'pubmedurl';
1669 $cssClass = 'mw-magiclink-pmid';
1670 $trackingCat = 'magiclink-tracking-pmid';
1673 throw new MWException( __METHOD__
. ': unrecognised match type "' .
1674 substr( $m[0], 0, 20 ) . '"' );
1676 $url = wfMessage( $urlmsg, $id )->inContentLanguage()->text();
1677 $this->addTrackingCategory( $trackingCat );
1678 return Linker
::makeExternalLink( $url, "{$keyword} {$id}", true, $cssClass, [], $this->mTitle
);
1679 } elseif ( isset( $m[6] ) && $m[6] !== ''
1680 && $this->mOptions
->getMagicISBNLinks()
1684 $space = self
::SPACE_NOT_NL
; # non-newline space
1685 $isbn = preg_replace( "/$space/", ' ', $isbn );
1686 $num = strtr( $isbn, [
1691 $this->addTrackingCategory( 'magiclink-tracking-isbn' );
1692 return $this->getLinkRenderer()->makeKnownLink(
1693 SpecialPage
::getTitleFor( 'Booksources', $num ),
1696 'class' => 'internal mw-magiclink-isbn',
1697 'title' => false // suppress title attribute
1706 * Make a free external link, given a user-supplied URL
1708 * @param string $url
1709 * @param int $numPostProto
1710 * The number of characters after the protocol.
1711 * @return string HTML
1714 public function makeFreeExternalLink( $url, $numPostProto ) {
1717 # The characters '<' and '>' (which were escaped by
1718 # removeHTMLtags()) should not be included in
1719 # URLs, per RFC 2396.
1720 # Make terminate a URL as well (bug T84937)
1723 '/&(lt|gt|nbsp|#x0*(3[CcEe]|[Aa]0)|#0*(60|62|160));/',
1728 $trail = substr( $url, $m2[0][1] ) . $trail;
1729 $url = substr( $url, 0, $m2[0][1] );
1732 # Move trailing punctuation to $trail
1734 # If there is no left bracket, then consider right brackets fair game too
1735 if ( strpos( $url, '(' ) === false ) {
1739 $urlRev = strrev( $url );
1740 $numSepChars = strspn( $urlRev, $sep );
1741 # Don't break a trailing HTML entity by moving the ; into $trail
1742 # This is in hot code, so use substr_compare to avoid having to
1743 # create a new string object for the comparison
1744 if ( $numSepChars && substr_compare( $url, ";", -$numSepChars, 1 ) === 0 ) {
1745 # more optimization: instead of running preg_match with a $
1746 # anchor, which can be slow, do the match on the reversed
1747 # string starting at the desired offset.
1748 # un-reversed regexp is: /&([a-z]+|#x[\da-f]+|#\d+)$/i
1749 if ( preg_match( '/\G([a-z]+|[\da-f]+x#|\d+#)&/i', $urlRev, $m2, 0, $numSepChars ) ) {
1753 if ( $numSepChars ) {
1754 $trail = substr( $url, -$numSepChars ) . $trail;
1755 $url = substr( $url, 0, -$numSepChars );
1758 # Verify that we still have a real URL after trail removal, and
1759 # not just lone protocol
1760 if ( strlen( $trail ) >= $numPostProto ) {
1761 return $url . $trail;
1764 $url = Sanitizer
::cleanUrl( $url );
1766 # Is this an external image?
1767 $text = $this->maybeMakeExternalImage( $url );
1768 if ( $text === false ) {
1769 # Not an image, make a link
1770 $text = Linker
::makeExternalLink( $url,
1771 $this->getTargetLanguage()->getConverter()->markNoConversion( $url ),
1773 $this->getExternalLinkAttribs( $url ), $this->mTitle
);
1774 # Register it in the output object...
1775 $this->mOutput
->addExternalLink( $url );
1777 return $text . $trail;
1781 * Parse headers and return html
1785 * @param string $text
1789 public function doHeadings( $text ) {
1790 for ( $i = 6; $i >= 1; --$i ) {
1791 $h = str_repeat( '=', $i );
1792 // Trim non-newline whitespace from headings
1793 // Using \s* will break for: "==\n===\n" and parse as <h2>=</h2>
1794 $text = preg_replace( "/^(?:$h)[ \\t]*(.+?)[ \\t]*(?:$h)\\s*$/m", "<h$i>\\1</h$i>", $text );
1800 * Replace single quotes with HTML markup
1803 * @param string $text
1805 * @return string The altered text
1807 public function doAllQuotes( $text ) {
1809 $lines = StringUtils
::explode( "\n", $text );
1810 foreach ( $lines as $line ) {
1811 $outtext .= $this->doQuotes( $line ) . "\n";
1813 $outtext = substr( $outtext, 0, -1 );
1818 * Helper function for doAllQuotes()
1820 * @param string $text
1824 public function doQuotes( $text ) {
1825 $arr = preg_split( "/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
1826 $countarr = count( $arr );
1827 if ( $countarr == 1 ) {
1831 // First, do some preliminary work. This may shift some apostrophes from
1832 // being mark-up to being text. It also counts the number of occurrences
1833 // of bold and italics mark-ups.
1836 for ( $i = 1; $i < $countarr; $i +
= 2 ) {
1837 $thislen = strlen( $arr[$i] );
1838 // If there are ever four apostrophes, assume the first is supposed to
1839 // be text, and the remaining three constitute mark-up for bold text.
1840 // (T15227: ''''foo'''' turns into ' ''' foo ' ''')
1841 if ( $thislen == 4 ) {
1842 $arr[$i - 1] .= "'";
1845 } elseif ( $thislen > 5 ) {
1846 // If there are more than 5 apostrophes in a row, assume they're all
1847 // text except for the last 5.
1848 // (T15227: ''''''foo'''''' turns into ' ''''' foo ' ''''')
1849 $arr[$i - 1] .= str_repeat( "'", $thislen - 5 );
1853 // Count the number of occurrences of bold and italics mark-ups.
1854 if ( $thislen == 2 ) {
1856 } elseif ( $thislen == 3 ) {
1858 } elseif ( $thislen == 5 ) {
1864 // If there is an odd number of both bold and italics, it is likely
1865 // that one of the bold ones was meant to be an apostrophe followed
1866 // by italics. Which one we cannot know for certain, but it is more
1867 // likely to be one that has a single-letter word before it.
1868 if ( ( $numbold %
2 == 1 ) && ( $numitalics %
2 == 1 ) ) {
1869 $firstsingleletterword = -1;
1870 $firstmultiletterword = -1;
1872 for ( $i = 1; $i < $countarr; $i +
= 2 ) {
1873 if ( strlen( $arr[$i] ) == 3 ) {
1874 $x1 = substr( $arr[$i - 1], -1 );
1875 $x2 = substr( $arr[$i - 1], -2, 1 );
1876 if ( $x1 === ' ' ) {
1877 if ( $firstspace == -1 ) {
1880 } elseif ( $x2 === ' ' ) {
1881 $firstsingleletterword = $i;
1882 // if $firstsingleletterword is set, we don't
1883 // look at the other options, so we can bail early.
1885 } elseif ( $firstmultiletterword == -1 ) {
1886 $firstmultiletterword = $i;
1891 // If there is a single-letter word, use it!
1892 if ( $firstsingleletterword > -1 ) {
1893 $arr[$firstsingleletterword] = "''";
1894 $arr[$firstsingleletterword - 1] .= "'";
1895 } elseif ( $firstmultiletterword > -1 ) {
1896 // If not, but there's a multi-letter word, use that one.
1897 $arr[$firstmultiletterword] = "''";
1898 $arr[$firstmultiletterword - 1] .= "'";
1899 } elseif ( $firstspace > -1 ) {
1900 // ... otherwise use the first one that has neither.
1901 // (notice that it is possible for all three to be -1 if, for example,
1902 // there is only one pentuple-apostrophe in the line)
1903 $arr[$firstspace] = "''";
1904 $arr[$firstspace - 1] .= "'";
1908 // Now let's actually convert our apostrophic mush to HTML!
1913 foreach ( $arr as $r ) {
1914 if ( ( $i %
2 ) == 0 ) {
1915 if ( $state === 'both' ) {
1921 $thislen = strlen( $r );
1922 if ( $thislen == 2 ) {
1923 if ( $state === 'i' ) {
1926 } elseif ( $state === 'bi' ) {
1929 } elseif ( $state === 'ib' ) {
1930 $output .= '</b></i><b>';
1932 } elseif ( $state === 'both' ) {
1933 $output .= '<b><i>' . $buffer . '</i>';
1935 } else { // $state can be 'b' or ''
1939 } elseif ( $thislen == 3 ) {
1940 if ( $state === 'b' ) {
1943 } elseif ( $state === 'bi' ) {
1944 $output .= '</i></b><i>';
1946 } elseif ( $state === 'ib' ) {
1949 } elseif ( $state === 'both' ) {
1950 $output .= '<i><b>' . $buffer . '</b>';
1952 } else { // $state can be 'i' or ''
1956 } elseif ( $thislen == 5 ) {
1957 if ( $state === 'b' ) {
1958 $output .= '</b><i>';
1960 } elseif ( $state === 'i' ) {
1961 $output .= '</i><b>';
1963 } elseif ( $state === 'bi' ) {
1964 $output .= '</i></b>';
1966 } elseif ( $state === 'ib' ) {
1967 $output .= '</b></i>';
1969 } elseif ( $state === 'both' ) {
1970 $output .= '<i><b>' . $buffer . '</b></i>';
1972 } else { // ($state == '')
1980 // Now close all remaining tags. Notice that the order is important.
1981 if ( $state === 'b' ||
$state === 'ib' ) {
1984 if ( $state === 'i' ||
$state === 'bi' ||
$state === 'ib' ) {
1987 if ( $state === 'bi' ) {
1990 // There might be lonely ''''', so make sure we have a buffer
1991 if ( $state === 'both' && $buffer ) {
1992 $output .= '<b><i>' . $buffer . '</i></b>';
1998 * Replace external links (REL)
2000 * Note: this is all very hackish and the order of execution matters a lot.
2001 * Make sure to run tests/parser/parserTests.php if you change this code.
2005 * @param string $text
2007 * @throws MWException
2010 public function replaceExternalLinks( $text ) {
2011 $bits = preg_split( $this->mExtLinkBracketedRegex
, $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
2012 if ( $bits === false ) {
2013 throw new MWException( "PCRE needs to be compiled with "
2014 . "--enable-unicode-properties in order for MediaWiki to function" );
2016 $s = array_shift( $bits );
2019 while ( $i < count( $bits ) ) {
2022 $text = $bits[$i++
];
2023 $trail = $bits[$i++
];
2025 # The characters '<' and '>' (which were escaped by
2026 # removeHTMLtags()) should not be included in
2027 # URLs, per RFC 2396.
2029 if ( preg_match( '/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE
) ) {
2030 $text = substr( $url, $m2[0][1] ) . ' ' . $text;
2031 $url = substr( $url, 0, $m2[0][1] );
2034 # If the link text is an image URL, replace it with an <img> tag
2035 # This happened by accident in the original parser, but some people used it extensively
2036 $img = $this->maybeMakeExternalImage( $text );
2037 if ( $img !== false ) {
2043 # Set linktype for CSS
2046 # No link text, e.g. [http://domain.tld/some.link]
2047 if ( $text == '' ) {
2049 $langObj = $this->getTargetLanguage();
2050 $text = '[' . $langObj->formatNum( ++
$this->mAutonumber
) . ']';
2051 $linktype = 'autonumber';
2053 # Have link text, e.g. [http://domain.tld/some.link text]s
2055 list( $dtrail, $trail ) = Linker
::splitTrail( $trail );
2058 // Excluding protocol-relative URLs may avoid many false positives.
2059 if ( preg_match( '/^(?:' . wfUrlProtocolsWithoutProtRel() . ')/', $text ) ) {
2060 $text = $this->getTargetLanguage()->getConverter()->markNoConversion( $text );
2063 $url = Sanitizer
::cleanUrl( $url );
2065 # Use the encoded URL
2066 # This means that users can paste URLs directly into the text
2067 # Funny characters like ö aren't valid in URLs anyway
2068 # This was changed in August 2004
2069 $s .= Linker
::makeExternalLink( $url, $text, false, $linktype,
2070 $this->getExternalLinkAttribs( $url ), $this->mTitle
) . $dtrail . $trail;
2072 # Register link in the output object.
2073 $this->mOutput
->addExternalLink( $url );
2080 * Get the rel attribute for a particular external link.
2083 * @param string|bool $url Optional URL, to extract the domain from for rel =>
2084 * nofollow if appropriate
2085 * @param LinkTarget|null $title Optional LinkTarget, for wgNoFollowNsExceptions lookups
2086 * @return string|null Rel attribute for $url
2088 public static function getExternalLinkRel( $url = false, $title = null ) {
2089 global $wgNoFollowLinks, $wgNoFollowNsExceptions, $wgNoFollowDomainExceptions;
2090 $ns = $title ?
$title->getNamespace() : false;
2091 if ( $wgNoFollowLinks && !in_array( $ns, $wgNoFollowNsExceptions )
2092 && !wfMatchesDomainList( $url, $wgNoFollowDomainExceptions )
2100 * Get an associative array of additional HTML attributes appropriate for a
2101 * particular external link. This currently may include rel => nofollow
2102 * (depending on configuration, namespace, and the URL's domain) and/or a
2103 * target attribute (depending on configuration).
2105 * @param string $url URL to extract the domain from for rel =>
2106 * nofollow if appropriate
2107 * @return array Associative array of HTML attributes
2109 public function getExternalLinkAttribs( $url ) {
2111 $rel = self
::getExternalLinkRel( $url, $this->mTitle
);
2113 $target = $this->mOptions
->getExternalLinkTarget();
2115 $attribs['target'] = $target;
2116 if ( !in_array( $target, [ '_self', '_parent', '_top' ] ) ) {
2117 // T133507. New windows can navigate parent cross-origin.
2118 // Including noreferrer due to lacking browser
2119 // support of noopener. Eventually noreferrer should be removed.
2120 if ( $rel !== '' ) {
2123 $rel .= 'noreferrer noopener';
2126 $attribs['rel'] = $rel;
2131 * Replace unusual escape codes in a URL with their equivalent characters
2133 * This generally follows the syntax defined in RFC 3986, with special
2134 * consideration for HTTP query strings.
2136 * @param string $url
2139 public static function normalizeLinkUrl( $url ) {
2140 # Test for RFC 3986 IPv6 syntax
2141 $scheme = '[a-z][a-z0-9+.-]*:';
2142 $userinfo = '(?:[a-z0-9\-._~!$&\'()*+,;=:]|%[0-9a-f]{2})*';
2143 $ipv6Host = '\\[((?:[0-9a-f:]|%3[0-A]|%[46][1-6])+)\\]';
2144 if ( preg_match( "<^(?:{$scheme})?//(?:{$userinfo}@)?{$ipv6Host}(?:[:/?#].*|)$>i", $url, $m ) &&
2145 IP
::isValid( rawurldecode( $m[1] ) )
2147 $isIPv6 = rawurldecode( $m[1] );
2152 # Make sure unsafe characters are encoded
2153 $url = preg_replace_callback( '/[\x00-\x20"<>\[\\\\\]^`{|}\x7F-\xFF]/',
2155 return rawurlencode( $m[0] );
2161 $end = strlen( $url );
2163 # Fragment part - 'fragment'
2164 $start = strpos( $url, '#' );
2165 if ( $start !== false && $start < $end ) {
2166 $ret = self
::normalizeUrlComponent(
2167 substr( $url, $start, $end - $start ), '"#%<>[\]^`{|}' ) . $ret;
2171 # Query part - 'query' minus &=+;
2172 $start = strpos( $url, '?' );
2173 if ( $start !== false && $start < $end ) {
2174 $ret = self
::normalizeUrlComponent(
2175 substr( $url, $start, $end - $start ), '"#%<>[\]^`{|}&=+;' ) . $ret;
2179 # Scheme and path part - 'pchar'
2180 # (we assume no userinfo or encoded colons in the host)
2181 $ret = self
::normalizeUrlComponent(
2182 substr( $url, 0, $end ), '"#%<>[\]^`{|}/?' ) . $ret;
2185 if ( $isIPv6 !== false ) {
2186 $ipv6Host = "%5B({$isIPv6})%5D";
2187 $ret = preg_replace(
2188 "<^((?:{$scheme})?//(?:{$userinfo}@)?){$ipv6Host}(?=[:/?#]|$)>i",
2197 private static function normalizeUrlComponent( $component, $unsafe ) {
2198 $callback = function ( $matches ) use ( $unsafe ) {
2199 $char = urldecode( $matches[0] );
2200 $ord = ord( $char );
2201 if ( $ord > 32 && $ord < 127 && strpos( $unsafe, $char ) === false ) {
2205 # Leave it escaped, but use uppercase for a-f
2206 return strtoupper( $matches[0] );
2209 return preg_replace_callback( '/%[0-9A-Fa-f]{2}/', $callback, $component );
2213 * make an image if it's allowed, either through the global
2214 * option, through the exception, or through the on-wiki whitelist
2216 * @param string $url
2220 private function maybeMakeExternalImage( $url ) {
2221 $imagesfrom = $this->mOptions
->getAllowExternalImagesFrom();
2222 $imagesexception = !empty( $imagesfrom );
2224 # $imagesfrom could be either a single string or an array of strings, parse out the latter
2225 if ( $imagesexception && is_array( $imagesfrom ) ) {
2226 $imagematch = false;
2227 foreach ( $imagesfrom as $match ) {
2228 if ( strpos( $url, $match ) === 0 ) {
2233 } elseif ( $imagesexception ) {
2234 $imagematch = ( strpos( $url, $imagesfrom ) === 0 );
2236 $imagematch = false;
2239 if ( $this->mOptions
->getAllowExternalImages()
2240 ||
( $imagesexception && $imagematch )
2242 if ( preg_match( self
::EXT_IMAGE_REGEX
, $url ) ) {
2244 $text = Linker
::makeExternalImage( $url );
2247 if ( !$text && $this->mOptions
->getEnableImageWhitelist()
2248 && preg_match( self
::EXT_IMAGE_REGEX
, $url )
2250 $whitelist = explode(
2252 wfMessage( 'external_image_whitelist' )->inContentLanguage()->text()
2255 foreach ( $whitelist as $entry ) {
2256 # Sanitize the regex fragment, make it case-insensitive, ignore blank entries/comments
2257 if ( strpos( $entry, '#' ) === 0 ||
$entry === '' ) {
2260 if ( preg_match( '/' . str_replace( '/', '\\/', $entry ) . '/i', $url ) ) {
2261 # Image matches a whitelist entry
2262 $text = Linker
::makeExternalImage( $url );
2271 * Process [[ ]] wikilinks
2275 * @return string Processed text
2279 public function replaceInternalLinks( $s ) {
2280 $this->mLinkHolders
->merge( $this->replaceInternalLinks2( $s ) );
2285 * Process [[ ]] wikilinks (RIL)
2287 * @throws MWException
2288 * @return LinkHolderArray
2292 public function replaceInternalLinks2( &$s ) {
2293 static $tc = false, $e1, $e1_img;
2294 # the % is needed to support urlencoded titles as well
2296 $tc = Title
::legalChars() . '#%';
2297 # Match a link having the form [[namespace:link|alternate]]trail
2298 $e1 = "/^([{$tc}]+)(?:\\|(.+?))?]](.*)\$/sD";
2299 # Match cases where there is no "]]", which might still be images
2300 $e1_img = "/^([{$tc}]+)\\|(.*)\$/sD";
2303 $holders = new LinkHolderArray( $this );
2305 # split the entire text string on occurrences of [[
2306 $a = StringUtils
::explode( '[[', ' ' . $s );
2307 # get the first element (all text up to first [[), and remove the space we added
2310 $line = $a->current(); # Workaround for broken ArrayIterator::next() that returns "void"
2311 $s = substr( $s, 1 );
2313 $useLinkPrefixExtension = $this->getTargetLanguage()->linkPrefixExtension();
2315 if ( $useLinkPrefixExtension ) {
2316 # Match the end of a line for a word that's not followed by whitespace,
2317 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
2318 $charset = $this->contLang
->linkPrefixCharset();
2319 $e2 = "/^((?>.*[^$charset]|))(.+)$/sDu";
2322 if ( is_null( $this->mTitle
) ) {
2323 throw new MWException( __METHOD__
. ": \$this->mTitle is null\n" );
2325 $nottalk = !$this->mTitle
->isTalkPage();
2327 if ( $useLinkPrefixExtension ) {
2329 if ( preg_match( $e2, $s, $m ) ) {
2330 $first_prefix = $m[2];
2332 $first_prefix = false;
2338 $useSubpages = $this->areSubpagesAllowed();
2340 # Loop for each link
2341 for ( ; $line !== false && $line !== null; $a->next(), $line = $a->current() ) {
2342 # Check for excessive memory usage
2343 if ( $holders->isBig() ) {
2345 # Do the existence check, replace the link holders and clear the array
2346 $holders->replace( $s );
2350 if ( $useLinkPrefixExtension ) {
2351 if ( preg_match( $e2, $s, $m ) ) {
2352 list( , $s, $prefix ) = $m;
2357 if ( $first_prefix ) {
2358 $prefix = $first_prefix;
2359 $first_prefix = false;
2363 $might_be_img = false;
2365 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
2367 # If we get a ] at the beginning of $m[3] that means we have a link that's something like:
2368 # [[Image:Foo.jpg|[http://example.com desc]]] <- having three ] in a row fucks up,
2369 # the real problem is with the $e1 regex
2371 # Still some problems for cases where the ] is meant to be outside punctuation,
2372 # and no image is in sight. See T4095.
2374 && substr( $m[3], 0, 1 ) === ']'
2375 && strpos( $text, '[' ) !== false
2377 $text .= ']'; # so that replaceExternalLinks($text) works later
2378 $m[3] = substr( $m[3], 1 );
2380 # fix up urlencoded title texts
2381 if ( strpos( $m[1], '%' ) !== false ) {
2382 # Should anchors '#' also be rejected?
2383 $m[1] = str_replace( [ '<', '>' ], [ '<', '>' ], rawurldecode( $m[1] ) );
2386 } elseif ( preg_match( $e1_img, $line, $m ) ) {
2387 # Invalid, but might be an image with a link in its caption
2388 $might_be_img = true;
2390 if ( strpos( $m[1], '%' ) !== false ) {
2391 $m[1] = str_replace( [ '<', '>' ], [ '<', '>' ], rawurldecode( $m[1] ) );
2394 } else { # Invalid form; output directly
2395 $s .= $prefix . '[[' . $line;
2399 $origLink = ltrim( $m[1], ' ' );
2401 # Don't allow internal links to pages containing
2402 # PROTO: where PROTO is a valid URL protocol; these
2403 # should be external links.
2404 if ( preg_match( '/^(?i:' . $this->mUrlProtocols
. ')/', $origLink ) ) {
2405 $s .= $prefix . '[[' . $line;
2409 # Make subpage if necessary
2410 if ( $useSubpages ) {
2411 $link = $this->maybeDoSubpageLink( $origLink, $text );
2416 // \x7f isn't a default legal title char, so most likely strip
2417 // markers will force us into the "invalid form" path above. But,
2418 // just in case, let's assert that xmlish tags aren't valid in
2419 // the title position.
2420 $unstrip = $this->mStripState
->killMarkers( $link );
2421 $noMarkers = ( $unstrip === $link );
2423 $nt = $noMarkers ? Title
::newFromText( $link ) : null;
2424 if ( $nt === null ) {
2425 $s .= $prefix . '[[' . $line;
2429 $ns = $nt->getNamespace();
2430 $iw = $nt->getInterwiki();
2432 $noforce = ( substr( $origLink, 0, 1 ) !== ':' );
2434 if ( $might_be_img ) { # if this is actually an invalid link
2435 if ( $ns == NS_FILE
&& $noforce ) { # but might be an image
2438 # look at the next 'line' to see if we can close it there
2440 $next_line = $a->current();
2441 if ( $next_line === false ||
$next_line === null ) {
2444 $m = explode( ']]', $next_line, 3 );
2445 if ( count( $m ) == 3 ) {
2446 # the first ]] closes the inner link, the second the image
2448 $text .= "[[{$m[0]}]]{$m[1]}";
2451 } elseif ( count( $m ) == 2 ) {
2452 # if there's exactly one ]] that's fine, we'll keep looking
2453 $text .= "[[{$m[0]}]]{$m[1]}";
2455 # if $next_line is invalid too, we need look no further
2456 $text .= '[[' . $next_line;
2461 # we couldn't find the end of this imageLink, so output it raw
2462 # but don't ignore what might be perfectly normal links in the text we've examined
2463 $holders->merge( $this->replaceInternalLinks2( $text ) );
2464 $s .= "{$prefix}[[$link|$text";
2465 # note: no $trail, because without an end, there *is* no trail
2468 } else { # it's not an image, so output it raw
2469 $s .= "{$prefix}[[$link|$text";
2470 # note: no $trail, because without an end, there *is* no trail
2475 $wasblank = ( $text == '' );
2479 # Strip off leading ':'
2480 $text = substr( $text, 1 );
2483 # T6598 madness. Handle the quotes only if they come from the alternate part
2484 # [[Lista d''e paise d''o munno]] -> <a href="...">Lista d''e paise d''o munno</a>
2485 # [[Criticism of Harry Potter|Criticism of ''Harry Potter'']]
2486 # -> <a href="Criticism of Harry Potter">Criticism of <i>Harry Potter</i></a>
2487 $text = $this->doQuotes( $text );
2490 # Link not escaped by : , create the various objects
2491 if ( $noforce && !$nt->wasLocalInterwiki() ) {
2494 $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && (
2495 Language
::fetchLanguageName( $iw, null, 'mw' ) ||
2496 in_array( $iw, $this->svcOptions
->get( 'ExtraInterlanguageLinkPrefixes' ) )
2499 # T26502: filter duplicates
2500 if ( !isset( $this->mLangLinkLanguages
[$iw] ) ) {
2501 $this->mLangLinkLanguages
[$iw] = true;
2502 $this->mOutput
->addLanguageLink( $nt->getFullText() );
2506 * Strip the whitespace interwiki links produce, see T10897
2508 $s = rtrim( $s . $prefix ) . $trail; # T175416
2512 if ( $ns == NS_FILE
) {
2513 if ( !$this->badFileLookup
->isBadFile( $nt->getDBkey(), $this->mTitle
) ) {
2515 # if no parameters were passed, $text
2516 # becomes something like "File:Foo.png",
2517 # which we don't want to pass on to the
2521 # recursively parse links inside the image caption
2522 # actually, this will parse them in any other parameters, too,
2523 # but it might be hard to fix that, and it doesn't matter ATM
2524 $text = $this->replaceExternalLinks( $text );
2525 $holders->merge( $this->replaceInternalLinks2( $text ) );
2527 # cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them
2528 $s .= $prefix . $this->armorLinks(
2529 $this->makeImage( $nt, $text, $holders ) ) . $trail;
2532 } elseif ( $ns == NS_CATEGORY
) {
2534 * Strip the whitespace Category links produce, see T2087
2536 $s = rtrim( $s . $prefix ) . $trail; # T2087, T87753
2539 $sortkey = $this->getDefaultSort();
2543 $sortkey = Sanitizer
::decodeCharReferences( $sortkey );
2544 $sortkey = str_replace( "\n", '', $sortkey );
2545 $sortkey = $this->getTargetLanguage()->convertCategoryKey( $sortkey );
2546 $this->mOutput
->addCategory( $nt->getDBkey(), $sortkey );
2552 # Self-link checking. For some languages, variants of the title are checked in
2553 # LinkHolderArray::doVariants() to allow batching the existence checks necessary
2554 # for linking to a different variant.
2555 if ( $ns != NS_SPECIAL
&& $nt->equals( $this->mTitle
) && !$nt->hasFragment() ) {
2556 $s .= $prefix . Linker
::makeSelfLinkObj( $nt, $text, '', $trail );
2560 # NS_MEDIA is a pseudo-namespace for linking directly to a file
2561 # @todo FIXME: Should do batch file existence checks, see comment below
2562 if ( $ns == NS_MEDIA
) {
2563 # Give extensions a chance to select the file revision for us
2566 Hooks
::run( 'BeforeParserFetchFileAndTitle',
2567 [ $this, $nt, &$options, &$descQuery ] );
2568 # Fetch and register the file (file title may be different via hooks)
2569 list( $file, $nt ) = $this->fetchFileAndTitle( $nt, $options );
2570 # Cloak with NOPARSE to avoid replacement in replaceExternalLinks
2571 $s .= $prefix . $this->armorLinks(
2572 Linker
::makeMediaLinkFile( $nt, $file, $text ) ) . $trail;
2576 # Some titles, such as valid special pages or files in foreign repos, should
2577 # be shown as bluelinks even though they're not included in the page table
2578 # @todo FIXME: isAlwaysKnown() can be expensive for file links; we should really do
2579 # batch file existence checks for NS_FILE and NS_MEDIA
2580 if ( $iw == '' && $nt->isAlwaysKnown() ) {
2581 $this->mOutput
->addLink( $nt );
2582 $s .= $this->makeKnownLinkHolder( $nt, $text, $trail, $prefix );
2584 # Links will be added to the output link list after checking
2585 $s .= $holders->makeHolder( $nt, $text, [], $trail, $prefix );
2592 * Render a forced-blue link inline; protect against double expansion of
2593 * URLs if we're in a mode that prepends full URL prefixes to internal links.
2594 * Since this little disaster has to split off the trail text to avoid
2595 * breaking URLs in the following text without breaking trails on the
2596 * wiki links, it's been made into a horrible function.
2599 * @param string $text
2600 * @param string $trail
2601 * @param string $prefix
2602 * @return string HTML-wikitext mix oh yuck
2604 protected function makeKnownLinkHolder( $nt, $text = '', $trail = '', $prefix = '' ) {
2605 list( $inside, $trail ) = Linker
::splitTrail( $trail );
2607 if ( $text == '' ) {
2608 $text = htmlspecialchars( $nt->getPrefixedText() );
2611 $link = $this->getLinkRenderer()->makeKnownLink(
2612 $nt, new HtmlArmor( "$prefix$text$inside" )
2615 return $this->armorLinks( $link ) . $trail;
2619 * Insert a NOPARSE hacky thing into any inline links in a chunk that's
2620 * going to go through further parsing steps before inline URL expansion.
2622 * Not needed quite as much as it used to be since free links are a bit
2623 * more sensible these days. But bracketed links are still an issue.
2625 * @param string $text More-or-less HTML
2626 * @return string Less-or-more HTML with NOPARSE bits
2628 public function armorLinks( $text ) {
2629 return preg_replace( '/\b((?i)' . $this->mUrlProtocols
. ')/',
2630 self
::MARKER_PREFIX
. "NOPARSE$1", $text );
2634 * Return true if subpage links should be expanded on this page.
2637 public function areSubpagesAllowed() {
2638 # Some namespaces don't allow subpages
2639 return $this->nsInfo
->hasSubpages( $this->mTitle
->getNamespace() );
2643 * Handle link to subpage if necessary
2645 * @param string $target The source of the link
2646 * @param string &$text The link text, modified as necessary
2647 * @return string The full name of the link
2650 public function maybeDoSubpageLink( $target, &$text ) {
2651 return Linker
::normalizeSubpageLink( $this->mTitle
, $target, $text );
2655 * Make lists from lines starting with ':', '*', '#', etc. (DBL)
2657 * @param string $text
2658 * @param bool $linestart Whether or not this is at the start of a line.
2660 * @return string The lists rendered as HTML
2662 public function doBlockLevels( $text, $linestart ) {
2663 return BlockLevelPass
::doBlockLevels( $text, $linestart );
2667 * Return value of a magic variable (like PAGENAME)
2671 * @param string $index Magic variable identifier as mapped in MagicWordFactory::$mVariableIDs
2672 * @param bool|PPFrame $frame
2674 * @throws MWException
2677 public function getVariableValue( $index, $frame = false ) {
2678 if ( is_null( $this->mTitle
) ) {
2679 // If no title set, bad things are going to happen
2680 // later. Title should always be set since this
2681 // should only be called in the middle of a parse
2682 // operation (but the unit-tests do funky stuff)
2683 throw new MWException( __METHOD__
. ' Should only be '
2684 . ' called while parsing (no title set)' );
2687 // Avoid PHP 7.1 warning from passing $this by reference
2691 * Some of these require message or data lookups and can be
2692 * expensive to check many times.
2695 Hooks
::run( 'ParserGetVariableValueVarCache', [ &$parser, &$this->mVarCache
] ) &&
2696 isset( $this->mVarCache
[$index] )
2698 return $this->mVarCache
[$index];
2701 $ts = wfTimestamp( TS_UNIX
, $this->mOptions
->getTimestamp() );
2702 Hooks
::run( 'ParserGetVariableValueTs', [ &$parser, &$ts ] );
2704 $pageLang = $this->getFunctionLang();
2710 case 'currentmonth':
2711 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'm' ), true );
2713 case 'currentmonth1':
2714 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'n' ), true );
2716 case 'currentmonthname':
2717 $value = $pageLang->getMonthName( MWTimestamp
::getInstance( $ts )->format( 'n' ) );
2719 case 'currentmonthnamegen':
2720 $value = $pageLang->getMonthNameGen( MWTimestamp
::getInstance( $ts )->format( 'n' ) );
2722 case 'currentmonthabbrev':
2723 $value = $pageLang->getMonthAbbreviation( MWTimestamp
::getInstance( $ts )->format( 'n' ) );
2726 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'j' ), true );
2729 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'd' ), true );
2732 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'm' ), true );
2735 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'n' ), true );
2737 case 'localmonthname':
2738 $value = $pageLang->getMonthName( MWTimestamp
::getLocalInstance( $ts )->format( 'n' ) );
2740 case 'localmonthnamegen':
2741 $value = $pageLang->getMonthNameGen( MWTimestamp
::getLocalInstance( $ts )->format( 'n' ) );
2743 case 'localmonthabbrev':
2744 $value = $pageLang->getMonthAbbreviation( MWTimestamp
::getLocalInstance( $ts )->format( 'n' ) );
2747 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'j' ), true );
2750 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'd' ), true );
2753 $value = wfEscapeWikiText( $this->mTitle
->getText() );
2756 $value = wfEscapeWikiText( $this->mTitle
->getPartialURL() );
2758 case 'fullpagename':
2759 $value = wfEscapeWikiText( $this->mTitle
->getPrefixedText() );
2761 case 'fullpagenamee':
2762 $value = wfEscapeWikiText( $this->mTitle
->getPrefixedURL() );
2765 $value = wfEscapeWikiText( $this->mTitle
->getSubpageText() );
2767 case 'subpagenamee':
2768 $value = wfEscapeWikiText( $this->mTitle
->getSubpageUrlForm() );
2770 case 'rootpagename':
2771 $value = wfEscapeWikiText( $this->mTitle
->getRootText() );
2773 case 'rootpagenamee':
2774 $value = wfEscapeWikiText( wfUrlencode( str_replace(
2777 $this->mTitle
->getRootText()
2780 case 'basepagename':
2781 $value = wfEscapeWikiText( $this->mTitle
->getBaseText() );
2783 case 'basepagenamee':
2784 $value = wfEscapeWikiText( wfUrlencode( str_replace(
2787 $this->mTitle
->getBaseText()
2790 case 'talkpagename':
2791 if ( $this->mTitle
->canHaveTalkPage() ) {
2792 $talkPage = $this->mTitle
->getTalkPage();
2793 $value = wfEscapeWikiText( $talkPage->getPrefixedText() );
2798 case 'talkpagenamee':
2799 if ( $this->mTitle
->canHaveTalkPage() ) {
2800 $talkPage = $this->mTitle
->getTalkPage();
2801 $value = wfEscapeWikiText( $talkPage->getPrefixedURL() );
2806 case 'subjectpagename':
2807 $subjPage = $this->mTitle
->getSubjectPage();
2808 $value = wfEscapeWikiText( $subjPage->getPrefixedText() );
2810 case 'subjectpagenamee':
2811 $subjPage = $this->mTitle
->getSubjectPage();
2812 $value = wfEscapeWikiText( $subjPage->getPrefixedURL() );
2814 case 'pageid': // requested in T25427
2815 # Inform the edit saving system that getting the canonical output
2816 # after page insertion requires a parse that used that exact page ID
2817 $this->setOutputFlag( 'vary-page-id', '{{PAGEID}} used' );
2818 $value = $this->mTitle
->getArticleID();
2820 $value = $this->mOptions
->getSpeculativePageId();
2822 $this->mOutput
->setSpeculativePageIdUsed( $value );
2828 $this->svcOptions
->get( 'MiserMode' ) &&
2829 !$this->mOptions
->getInterfaceMessage() &&
2830 // @TODO: disallow this word on all namespaces
2831 $this->nsInfo
->isContent( $this->mTitle
->getNamespace() )
2833 // Use a stub result instead of the actual revision ID in order to avoid
2834 // double parses on page save but still allow preview detection (T137900)
2835 if ( $this->getRevisionId() ||
$this->mOptions
->getSpeculativeRevId() ) {
2838 $this->setOutputFlag( 'vary-revision-exists', '{{REVISIONID}} used' );
2842 # Inform the edit saving system that getting the canonical output after
2843 # revision insertion requires a parse that used that exact revision ID
2844 $this->setOutputFlag( 'vary-revision-id', '{{REVISIONID}} used' );
2845 $value = $this->getRevisionId();
2846 if ( $value === 0 ) {
2847 $rev = $this->getRevisionObject();
2848 $value = $rev ?
$rev->getId() : $value;
2851 $value = $this->mOptions
->getSpeculativeRevId();
2853 $this->mOutput
->setSpeculativeRevIdUsed( $value );
2859 $value = (int)$this->getRevisionTimestampSubstring( 6, 2, self
::MAX_TTS
, $index );
2861 case 'revisionday2':
2862 $value = $this->getRevisionTimestampSubstring( 6, 2, self
::MAX_TTS
, $index );
2864 case 'revisionmonth':
2865 $value = $this->getRevisionTimestampSubstring( 4, 2, self
::MAX_TTS
, $index );
2867 case 'revisionmonth1':
2868 $value = (int)$this->getRevisionTimestampSubstring( 4, 2, self
::MAX_TTS
, $index );
2870 case 'revisionyear':
2871 $value = $this->getRevisionTimestampSubstring( 0, 4, self
::MAX_TTS
, $index );
2873 case 'revisiontimestamp':
2874 $value = $this->getRevisionTimestampSubstring( 0, 14, self
::MAX_TTS
, $index );
2876 case 'revisionuser':
2877 # Inform the edit saving system that getting the canonical output after
2878 # revision insertion requires a parse that used the actual user ID
2879 $this->setOutputFlag( 'vary-user', '{{REVISIONUSER}} used' );
2880 $value = $this->getRevisionUser();
2882 case 'revisionsize':
2883 $value = $this->getRevisionSize();
2886 $value = str_replace( '_', ' ',
2887 $this->contLang
->getNsText( $this->mTitle
->getNamespace() ) );
2890 $value = wfUrlencode( $this->contLang
->getNsText( $this->mTitle
->getNamespace() ) );
2892 case 'namespacenumber':
2893 $value = $this->mTitle
->getNamespace();
2896 $value = $this->mTitle
->canHaveTalkPage()
2897 ?
str_replace( '_', ' ', $this->mTitle
->getTalkNsText() )
2901 $value = $this->mTitle
->canHaveTalkPage() ?
wfUrlencode( $this->mTitle
->getTalkNsText() ) : '';
2903 case 'subjectspace':
2904 $value = str_replace( '_', ' ', $this->mTitle
->getSubjectNsText() );
2906 case 'subjectspacee':
2907 $value = ( wfUrlencode( $this->mTitle
->getSubjectNsText() ) );
2909 case 'currentdayname':
2910 $value = $pageLang->getWeekdayName( (int)MWTimestamp
::getInstance( $ts )->format( 'w' ) +
1 );
2913 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'Y' ), true );
2916 $value = $pageLang->time( wfTimestamp( TS_MW
, $ts ), false, false );
2919 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'H' ), true );
2922 # @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
2923 # int to remove the padding
2924 $value = $pageLang->formatNum( (int)MWTimestamp
::getInstance( $ts )->format( 'W' ) );
2927 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'w' ) );
2929 case 'localdayname':
2930 $value = $pageLang->getWeekdayName(
2931 (int)MWTimestamp
::getLocalInstance( $ts )->format( 'w' ) +
1
2935 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'Y' ), true );
2938 $value = $pageLang->time(
2939 MWTimestamp
::getLocalInstance( $ts )->format( 'YmdHis' ),
2945 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'H' ), true );
2948 # @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
2949 # int to remove the padding
2950 $value = $pageLang->formatNum( (int)MWTimestamp
::getLocalInstance( $ts )->format( 'W' ) );
2953 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'w' ) );
2955 case 'numberofarticles':
2956 $value = $pageLang->formatNum( SiteStats
::articles() );
2958 case 'numberoffiles':
2959 $value = $pageLang->formatNum( SiteStats
::images() );
2961 case 'numberofusers':
2962 $value = $pageLang->formatNum( SiteStats
::users() );
2964 case 'numberofactiveusers':
2965 $value = $pageLang->formatNum( SiteStats
::activeUsers() );
2967 case 'numberofpages':
2968 $value = $pageLang->formatNum( SiteStats
::pages() );
2970 case 'numberofadmins':
2971 $value = $pageLang->formatNum( SiteStats
::numberingroup( 'sysop' ) );
2973 case 'numberofedits':
2974 $value = $pageLang->formatNum( SiteStats
::edits() );
2976 case 'currenttimestamp':
2977 $value = wfTimestamp( TS_MW
, $ts );
2979 case 'localtimestamp':
2980 $value = MWTimestamp
::getLocalInstance( $ts )->format( 'YmdHis' );
2982 case 'currentversion':
2983 $value = SpecialVersion
::getVersion();
2986 return $this->svcOptions
->get( 'ArticlePath' );
2988 return $this->svcOptions
->get( 'Sitename' );
2990 return $this->svcOptions
->get( 'Server' );
2992 return $this->svcOptions
->get( 'ServerName' );
2994 return $this->svcOptions
->get( 'ScriptPath' );
2996 return $this->svcOptions
->get( 'StylePath' );
2997 case 'directionmark':
2998 return $pageLang->getDirMark();
2999 case 'contentlanguage':
3000 return $this->svcOptions
->get( 'LanguageCode' );
3001 case 'pagelanguage':
3002 $value = $pageLang->getCode();
3004 case 'cascadingsources':
3005 $value = CoreParserFunctions
::cascadingsources( $this );
3010 'ParserGetVariableValueSwitch',
3011 [ &$parser, &$this->mVarCache
, &$index, &$ret, &$frame ]
3018 $this->mVarCache
[$index] = $value;
3027 * @param int $mtts Max time-till-save; sets vary-revision-timestamp if result changes by then
3028 * @param string $variable Parser variable name
3031 private function getRevisionTimestampSubstring( $start, $len, $mtts, $variable ) {
3032 # Get the timezone-adjusted timestamp to be used for this revision
3033 $resNow = substr( $this->getRevisionTimestamp(), $start, $len );
3034 # Possibly set vary-revision if there is not yet an associated revision
3035 if ( !$this->getRevisionObject() ) {
3036 # Get the timezone-adjusted timestamp $mtts seconds in the future.
3037 # This future is relative to the current time and not that of the
3038 # parser options. The rendered timestamp can be compared to that
3039 # of the timestamp specified by the parser options.
3041 $this->contLang
->userAdjust( wfTimestamp( TS_MW
, time() +
$mtts ), '' ),
3046 if ( $resNow !== $resThen ) {
3047 # Inform the edit saving system that getting the canonical output after
3048 # revision insertion requires a parse that used an actual revision timestamp
3049 $this->setOutputFlag( 'vary-revision-timestamp', "$variable used" );
3057 * initialise the magic variables (like CURRENTMONTHNAME) and substitution modifiers
3061 public function initialiseVariables() {
3062 $variableIDs = $this->magicWordFactory
->getVariableIDs();
3063 $substIDs = $this->magicWordFactory
->getSubstIDs();
3065 $this->mVariables
= $this->magicWordFactory
->newArray( $variableIDs );
3066 $this->mSubstWords
= $this->magicWordFactory
->newArray( $substIDs );
3070 * Preprocess some wikitext and return the document tree.
3071 * This is the ghost of replace_variables().
3073 * @param string $text The text to parse
3074 * @param int $flags Bitwise combination of:
3075 * - self::PTD_FOR_INCLUSION: Handle "<noinclude>" and "<includeonly>" as if the text is being
3076 * included. Default is to assume a direct page view.
3078 * The generated DOM tree must depend only on the input text and the flags.
3079 * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of T6899.
3081 * Any flag added to the $flags parameter here, or any other parameter liable to cause a
3082 * change in the DOM tree for a given text, must be passed through the section identifier
3083 * in the section edit link and thus back to extractSections().
3085 * The output of this function is currently only cached in process memory, but a persistent
3086 * cache may be implemented at a later date which takes further advantage of these strict
3087 * dependency requirements.
3091 public function preprocessToDom( $text, $flags = 0 ) {
3092 $dom = $this->getPreprocessor()->preprocessToObj( $text, $flags );
3097 * Return a three-element array: leading whitespace, string contents, trailing whitespace
3103 public static function splitWhitespace( $s ) {
3104 $ltrimmed = ltrim( $s );
3105 $w1 = substr( $s, 0, strlen( $s ) - strlen( $ltrimmed ) );
3106 $trimmed = rtrim( $ltrimmed );
3107 $diff = strlen( $ltrimmed ) - strlen( $trimmed );
3109 $w2 = substr( $ltrimmed, -$diff );
3113 return [ $w1, $trimmed, $w2 ];
3117 * Replace magic variables, templates, and template arguments
3118 * with the appropriate text. Templates are substituted recursively,
3119 * taking care to avoid infinite loops.
3121 * Note that the substitution depends on value of $mOutputType:
3122 * self::OT_WIKI: only {{subst:}} templates
3123 * self::OT_PREPROCESS: templates but not extension tags
3124 * self::OT_HTML: all templates and extension tags
3126 * @param string $text The text to transform
3127 * @param false|PPFrame|array $frame Object describing the arguments passed to the
3128 * template. Arguments may also be provided as an associative array, as
3129 * was the usual case before MW1.12. Providing arguments this way may be
3130 * useful for extensions wishing to perform variable replacement
3132 * @param bool $argsOnly Only do argument (triple-brace) expansion, not
3133 * double-brace expansion.
3136 public function replaceVariables( $text, $frame = false, $argsOnly = false ) {
3137 # Is there any text? Also, Prevent too big inclusions!
3138 $textSize = strlen( $text );
3139 if ( $textSize < 1 ||
$textSize > $this->mOptions
->getMaxIncludeSize() ) {
3143 if ( $frame === false ) {
3144 $frame = $this->getPreprocessor()->newFrame();
3145 } elseif ( !( $frame instanceof PPFrame
) ) {
3146 $this->logger
->debug(
3147 __METHOD__
. " called using plain parameters instead of " .
3148 "a PPFrame instance. Creating custom frame."
3150 $frame = $this->getPreprocessor()->newCustomFrame( $frame );
3153 $dom = $this->preprocessToDom( $text );
3154 $flags = $argsOnly ? PPFrame
::NO_TEMPLATES
: 0;
3155 $text = $frame->expand( $dom, $flags );
3161 * Clean up argument array - refactored in 1.9 so parserfunctions can use it, too.
3163 * @param array $args
3167 public static function createAssocArgs( $args ) {
3170 foreach ( $args as $arg ) {
3171 $eqpos = strpos( $arg, '=' );
3172 if ( $eqpos === false ) {
3173 $assocArgs[$index++
] = $arg;
3175 $name = trim( substr( $arg, 0, $eqpos ) );
3176 $value = trim( substr( $arg, $eqpos +
1 ) );
3177 if ( $value === false ) {
3180 if ( $name !== false ) {
3181 $assocArgs[$name] = $value;
3190 * Warn the user when a parser limitation is reached
3191 * Will warn at most once the user per limitation type
3193 * The results are shown during preview and run through the Parser (See EditPage.php)
3195 * @param string $limitationType Should be one of:
3196 * 'expensive-parserfunction' (corresponding messages:
3197 * 'expensive-parserfunction-warning',
3198 * 'expensive-parserfunction-category')
3199 * 'post-expand-template-argument' (corresponding messages:
3200 * 'post-expand-template-argument-warning',
3201 * 'post-expand-template-argument-category')
3202 * 'post-expand-template-inclusion' (corresponding messages:
3203 * 'post-expand-template-inclusion-warning',
3204 * 'post-expand-template-inclusion-category')
3205 * 'node-count-exceeded' (corresponding messages:
3206 * 'node-count-exceeded-warning',
3207 * 'node-count-exceeded-category')
3208 * 'expansion-depth-exceeded' (corresponding messages:
3209 * 'expansion-depth-exceeded-warning',
3210 * 'expansion-depth-exceeded-category')
3211 * @param string|int|null $current Current value
3212 * @param string|int|null $max Maximum allowed, when an explicit limit has been
3213 * exceeded, provide the values (optional)
3215 public function limitationWarn( $limitationType, $current = '', $max = '' ) {
3216 # does no harm if $current and $max are present but are unnecessary for the message
3217 # Not doing ->inLanguage( $this->mOptions->getUserLangObj() ), since this is shown
3218 # only during preview, and that would split the parser cache unnecessarily.
3219 $warning = wfMessage( "$limitationType-warning" )->numParams( $current, $max )
3221 $this->mOutput
->addWarning( $warning );
3222 $this->addTrackingCategory( "$limitationType-category" );
3226 * Return the text of a template, after recursively
3227 * replacing any variables or templates within the template.
3229 * @param array $piece The parts of the template
3230 * $piece['title']: the title, i.e. the part before the |
3231 * $piece['parts']: the parameter array
3232 * $piece['lineStart']: whether the brace was at the start of a line
3233 * @param PPFrame $frame The current frame, contains template arguments
3235 * @return string|array The text of the template
3237 public function braceSubstitution( $piece, $frame ) {
3240 // $text has been filled
3242 // wiki markup in $text should be escaped
3244 // $text is HTML, armour it against wikitext transformation
3246 // Force interwiki transclusion to be done in raw mode not rendered
3247 $forceRawInterwiki = false;
3248 // $text is a DOM node needing expansion in a child frame
3249 $isChildObj = false;
3250 // $text is a DOM node needing expansion in the current frame
3251 $isLocalObj = false;
3253 # Title object, where $text came from
3256 # $part1 is the bit before the first |, and must contain only title characters.
3257 # Various prefixes will be stripped from it later.
3258 $titleWithSpaces = $frame->expand( $piece['title'] );
3259 $part1 = trim( $titleWithSpaces );
3262 # Original title text preserved for various purposes
3263 $originalTitle = $part1;
3265 # $args is a list of argument nodes, starting from index 0, not including $part1
3266 # @todo FIXME: If piece['parts'] is null then the call to getLength()
3267 # below won't work b/c this $args isn't an object
3268 $args = ( $piece['parts'] == null ) ?
[] : $piece['parts'];
3270 $profileSection = null; // profile templates
3274 $substMatch = $this->mSubstWords
->matchStartAndRemove( $part1 );
3276 # Possibilities for substMatch: "subst", "safesubst" or FALSE
3277 # Decide whether to expand template or keep wikitext as-is.
3278 if ( $this->ot
['wiki'] ) {
3279 if ( $substMatch === false ) {
3280 $literal = true; # literal when in PST with no prefix
3282 $literal = false; # expand when in PST with subst: or safesubst:
3285 if ( $substMatch == 'subst' ) {
3286 $literal = true; # literal when not in PST with plain subst:
3288 $literal = false; # expand when not in PST with safesubst: or no prefix
3292 $text = $frame->virtualBracketedImplode( '{{', '|', '}}', $titleWithSpaces, $args );
3299 if ( !$found && $args->getLength() == 0 ) {
3300 $id = $this->mVariables
->matchStartToEnd( $part1 );
3301 if ( $id !== false ) {
3302 $text = $this->getVariableValue( $id, $frame );
3303 if ( $this->magicWordFactory
->getCacheTTL( $id ) > -1 ) {
3304 $this->mOutput
->updateCacheExpiry(
3305 $this->magicWordFactory
->getCacheTTL( $id ) );
3311 # MSG, MSGNW and RAW
3314 $mwMsgnw = $this->magicWordFactory
->get( 'msgnw' );
3315 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
3318 # Remove obsolete MSG:
3319 $mwMsg = $this->magicWordFactory
->get( 'msg' );
3320 $mwMsg->matchStartAndRemove( $part1 );
3324 $mwRaw = $this->magicWordFactory
->get( 'raw' );
3325 if ( $mwRaw->matchStartAndRemove( $part1 ) ) {
3326 $forceRawInterwiki = true;
3332 $colonPos = strpos( $part1, ':' );
3333 if ( $colonPos !== false ) {
3334 $func = substr( $part1, 0, $colonPos );
3335 $funcArgs = [ trim( substr( $part1, $colonPos +
1 ) ) ];
3336 $argsLength = $args->getLength();
3337 for ( $i = 0; $i < $argsLength; $i++
) {
3338 $funcArgs[] = $args->item( $i );
3341 $result = $this->callParserFunction( $frame, $func, $funcArgs );
3343 // Extract any forwarded flags
3344 if ( isset( $result['title'] ) ) {
3345 $title = $result['title'];
3347 if ( isset( $result['found'] ) ) {
3348 $found = $result['found'];
3350 if ( array_key_exists( 'text', $result ) ) {
3352 $text = $result['text'];
3354 if ( isset( $result['nowiki'] ) ) {
3355 $nowiki = $result['nowiki'];
3357 if ( isset( $result['isHTML'] ) ) {
3358 $isHTML = $result['isHTML'];
3360 if ( isset( $result['forceRawInterwiki'] ) ) {
3361 $forceRawInterwiki = $result['forceRawInterwiki'];
3363 if ( isset( $result['isChildObj'] ) ) {
3364 $isChildObj = $result['isChildObj'];
3366 if ( isset( $result['isLocalObj'] ) ) {
3367 $isLocalObj = $result['isLocalObj'];
3372 # Finish mangling title and then check for loops.
3373 # Set $title to a Title object and $titleText to the PDBK
3376 # Split the title into page and subpage
3378 $relative = $this->maybeDoSubpageLink( $part1, $subpage );
3379 if ( $part1 !== $relative ) {
3381 $ns = $this->mTitle
->getNamespace();
3383 $title = Title
::newFromText( $part1, $ns );
3385 $titleText = $title->getPrefixedText();
3386 # Check for language variants if the template is not found
3387 if ( $this->getTargetLanguage()->hasVariants() && $title->getArticleID() == 0 ) {
3388 $this->getTargetLanguage()->findVariantLink( $part1, $title, true );
3390 # Do recursion depth check
3391 $limit = $this->mOptions
->getMaxTemplateDepth();
3392 if ( $frame->depth
>= $limit ) {
3394 $text = '<span class="error">'
3395 . wfMessage( 'parser-template-recursion-depth-warning' )
3396 ->numParams( $limit )->inContentLanguage()->text()
3402 # Load from database
3403 if ( !$found && $title ) {
3404 $profileSection = $this->mProfiler
->scopedProfileIn( $title->getPrefixedDBkey() );
3405 if ( !$title->isExternal() ) {
3406 if ( $title->isSpecialPage()
3407 && $this->mOptions
->getAllowSpecialInclusion()
3408 && $this->ot
['html']
3410 $specialPage = $this->specialPageFactory
->getPage( $title->getDBkey() );
3411 // Pass the template arguments as URL parameters.
3412 // "uselang" will have no effect since the Language object
3413 // is forced to the one defined in ParserOptions.
3415 $argsLength = $args->getLength();
3416 for ( $i = 0; $i < $argsLength; $i++
) {
3417 $bits = $args->item( $i )->splitArg();
3418 if ( strval( $bits['index'] ) === '' ) {
3419 $name = trim( $frame->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
3420 $value = trim( $frame->expand( $bits['value'] ) );
3421 $pageArgs[$name] = $value;
3425 // Create a new context to execute the special page
3426 $context = new RequestContext
;
3427 $context->setTitle( $title );
3428 $context->setRequest( new FauxRequest( $pageArgs ) );
3429 if ( $specialPage && $specialPage->maxIncludeCacheTime() === 0 ) {
3430 $context->setUser( $this->getUser() );
3432 // If this page is cached, then we better not be per user.
3433 $context->setUser( User
::newFromName( '127.0.0.1', false ) );
3435 $context->setLanguage( $this->mOptions
->getUserLangObj() );
3436 $ret = $this->specialPageFactory
->capturePath( $title, $context, $this->getLinkRenderer() );
3438 $text = $context->getOutput()->getHTML();
3439 $this->mOutput
->addOutputPageMetadata( $context->getOutput() );
3442 if ( $specialPage && $specialPage->maxIncludeCacheTime() !== false ) {
3443 $this->mOutput
->updateRuntimeAdaptiveExpiry(
3444 $specialPage->maxIncludeCacheTime()
3448 } elseif ( $this->nsInfo
->isNonincludable( $title->getNamespace() ) ) {
3449 $found = false; # access denied
3450 $this->logger
->debug(
3452 ": template inclusion denied for " . $title->getPrefixedDBkey()
3455 list( $text, $title ) = $this->getTemplateDom( $title );
3456 if ( $text !== false ) {
3462 # If the title is valid but undisplayable, make a link to it
3463 if ( !$found && ( $this->ot
['html'] ||
$this->ot
['pre'] ) ) {
3464 $text = "[[:$titleText]]";
3467 } elseif ( $title->isTrans() ) {
3468 # Interwiki transclusion
3469 if ( $this->ot
['html'] && !$forceRawInterwiki ) {
3470 $text = $this->interwikiTransclude( $title, 'render' );
3473 $text = $this->interwikiTransclude( $title, 'raw' );
3474 # Preprocess it like a template
3475 $text = $this->preprocessToDom( $text, self
::PTD_FOR_INCLUSION
);
3481 # Do infinite loop check
3482 # This has to be done after redirect resolution to avoid infinite loops via redirects
3483 if ( !$frame->loopCheck( $title ) ) {
3485 $text = '<span class="error">'
3486 . wfMessage( 'parser-template-loop-warning', $titleText )->inContentLanguage()->text()
3488 $this->addTrackingCategory( 'template-loop-category' );
3489 $this->mOutput
->addWarning( wfMessage( 'template-loop-warning',
3490 wfEscapeWikiText( $titleText ) )->text() );
3491 $this->logger
->debug( __METHOD__
. ": template loop broken at '$titleText'" );
3495 # If we haven't found text to substitute by now, we're done
3496 # Recover the source wikitext and return it
3498 $text = $frame->virtualBracketedImplode( '{{', '|', '}}', $titleWithSpaces, $args );
3499 if ( $profileSection ) {
3500 $this->mProfiler
->scopedProfileOut( $profileSection );
3502 return [ 'object' => $text ];
3505 # Expand DOM-style return values in a child frame
3506 if ( $isChildObj ) {
3507 # Clean up argument array
3508 $newFrame = $frame->newChild( $args, $title );
3511 $text = $newFrame->expand( $text, PPFrame
::RECOVER_ORIG
);
3512 } elseif ( $titleText !== false && $newFrame->isEmpty() ) {
3513 # Expansion is eligible for the empty-frame cache
3514 $text = $newFrame->cachedExpand( $titleText, $text );
3516 # Uncached expansion
3517 $text = $newFrame->expand( $text );
3520 if ( $isLocalObj && $nowiki ) {
3521 $text = $frame->expand( $text, PPFrame
::RECOVER_ORIG
);
3522 $isLocalObj = false;
3525 if ( $profileSection ) {
3526 $this->mProfiler
->scopedProfileOut( $profileSection );
3529 # Replace raw HTML by a placeholder
3531 $text = $this->insertStripItem( $text );
3532 } elseif ( $nowiki && ( $this->ot
['html'] ||
$this->ot
['pre'] ) ) {
3533 # Escape nowiki-style return values
3534 $text = wfEscapeWikiText( $text );
3535 } elseif ( is_string( $text )
3536 && !$piece['lineStart']
3537 && preg_match( '/^(?:{\\||:|;|#|\*)/', $text )
3539 # T2529: if the template begins with a table or block-level
3540 # element, it should be treated as beginning a new line.
3541 # This behavior is somewhat controversial.
3542 $text = "\n" . $text;
3545 if ( is_string( $text ) && !$this->incrementIncludeSize( 'post-expand', strlen( $text ) ) ) {
3546 # Error, oversize inclusion
3547 if ( $titleText !== false ) {
3548 # Make a working, properly escaped link if possible (T25588)
3549 $text = "[[:$titleText]]";
3551 # This will probably not be a working link, but at least it may
3552 # provide some hint of where the problem is
3553 preg_replace( '/^:/', '', $originalTitle );
3554 $text = "[[:$originalTitle]]";
3556 $text .= $this->insertStripItem( '<!-- WARNING: template omitted, '
3557 . 'post-expand include size too large -->' );
3558 $this->limitationWarn( 'post-expand-template-inclusion' );
3561 if ( $isLocalObj ) {
3562 $ret = [ 'object' => $text ];
3564 $ret = [ 'text' => $text ];
3571 * Call a parser function and return an array with text and flags.
3573 * The returned array will always contain a boolean 'found', indicating
3574 * whether the parser function was found or not. It may also contain the
3576 * text: string|object, resulting wikitext or PP DOM object
3577 * isHTML: bool, $text is HTML, armour it against wikitext transformation
3578 * isChildObj: bool, $text is a DOM node needing expansion in a child frame
3579 * isLocalObj: bool, $text is a DOM node needing expansion in the current frame
3580 * nowiki: bool, wiki markup in $text should be escaped
3583 * @param PPFrame $frame The current frame, contains template arguments
3584 * @param string $function Function name
3585 * @param array $args Arguments to the function
3586 * @throws MWException
3589 public function callParserFunction( $frame, $function, array $args = [] ) {
3590 # Case sensitive functions
3591 if ( isset( $this->mFunctionSynonyms
[1][$function] ) ) {
3592 $function = $this->mFunctionSynonyms
[1][$function];
3594 # Case insensitive functions
3595 $function = $this->contLang
->lc( $function );
3596 if ( isset( $this->mFunctionSynonyms
[0][$function] ) ) {
3597 $function = $this->mFunctionSynonyms
[0][$function];
3599 return [ 'found' => false ];
3603 list( $callback, $flags ) = $this->mFunctionHooks
[$function];
3605 // Avoid PHP 7.1 warning from passing $this by reference
3608 $allArgs = [ &$parser ];
3609 if ( $flags & self
::SFH_OBJECT_ARGS
) {
3610 # Convert arguments to PPNodes and collect for appending to $allArgs
3612 foreach ( $args as $k => $v ) {
3613 if ( $v instanceof PPNode ||
$k === 0 ) {
3616 $funcArgs[] = $this->mPreprocessor
->newPartNodeArray( [ $k => $v ] )->item( 0 );
3620 # Add a frame parameter, and pass the arguments as an array
3621 $allArgs[] = $frame;
3622 $allArgs[] = $funcArgs;
3624 # Convert arguments to plain text and append to $allArgs
3625 foreach ( $args as $k => $v ) {
3626 if ( $v instanceof PPNode
) {
3627 $allArgs[] = trim( $frame->expand( $v ) );
3628 } elseif ( is_int( $k ) && $k >= 0 ) {
3629 $allArgs[] = trim( $v );
3631 $allArgs[] = trim( "$k=$v" );
3636 $result = $callback( ...$allArgs );
3638 # The interface for function hooks allows them to return a wikitext
3639 # string or an array containing the string and any flags. This mungs
3640 # things around to match what this method should return.
3641 if ( !is_array( $result ) ) {
3647 if ( isset( $result[0] ) && !isset( $result['text'] ) ) {
3648 $result['text'] = $result[0];
3650 unset( $result[0] );
3657 $preprocessFlags = 0;
3658 if ( isset( $result['noparse'] ) ) {
3659 $noparse = $result['noparse'];
3661 if ( isset( $result['preprocessFlags'] ) ) {
3662 $preprocessFlags = $result['preprocessFlags'];
3666 $result['text'] = $this->preprocessToDom( $result['text'], $preprocessFlags );
3667 $result['isChildObj'] = true;
3674 * Get the semi-parsed DOM representation of a template with a given title,
3675 * and its redirect destination title. Cached.
3677 * @param Title $title
3681 public function getTemplateDom( $title ) {
3682 $cacheTitle = $title;
3683 $titleText = $title->getPrefixedDBkey();
3685 if ( isset( $this->mTplRedirCache
[$titleText] ) ) {
3686 list( $ns, $dbk ) = $this->mTplRedirCache
[$titleText];
3687 $title = Title
::makeTitle( $ns, $dbk );
3688 $titleText = $title->getPrefixedDBkey();
3690 if ( isset( $this->mTplDomCache
[$titleText] ) ) {
3691 return [ $this->mTplDomCache
[$titleText], $title ];
3694 # Cache miss, go to the database
3695 list( $text, $title ) = $this->fetchTemplateAndTitle( $title );
3697 if ( $text === false ) {
3698 $this->mTplDomCache
[$titleText] = false;
3699 return [ false, $title ];
3702 $dom = $this->preprocessToDom( $text, self
::PTD_FOR_INCLUSION
);
3703 $this->mTplDomCache
[$titleText] = $dom;
3705 if ( !$title->equals( $cacheTitle ) ) {
3706 $this->mTplRedirCache
[$cacheTitle->getPrefixedDBkey()] =
3707 [ $title->getNamespace(), $title->getDBkey() ];
3710 return [ $dom, $title ];
3714 * Fetch the current revision of a given title. Note that the revision
3715 * (and even the title) may not exist in the database, so everything
3716 * contributing to the output of the parser should use this method
3717 * where possible, rather than getting the revisions themselves. This
3718 * method also caches its results, so using it benefits performance.
3721 * @param Title $title
3724 public function fetchCurrentRevisionOfTitle( $title ) {
3725 $cacheKey = $title->getPrefixedDBkey();
3726 if ( !$this->currentRevisionCache
) {
3727 $this->currentRevisionCache
= new MapCacheLRU( 100 );
3729 if ( !$this->currentRevisionCache
->has( $cacheKey ) ) {
3730 $this->currentRevisionCache
->set( $cacheKey,
3731 // Defaults to Parser::statelessFetchRevision()
3732 call_user_func( $this->mOptions
->getCurrentRevisionCallback(), $title, $this )
3735 return $this->currentRevisionCache
->get( $cacheKey );
3739 * @param Title $title
3743 public function isCurrentRevisionOfTitleCached( $title ) {
3745 $this->currentRevisionCache
&&
3746 $this->currentRevisionCache
->has( $title->getPrefixedText() )
3751 * Wrapper around Revision::newFromTitle to allow passing additional parameters
3752 * without passing them on to it.
3755 * @param Title $title
3756 * @param Parser|bool $parser
3757 * @return Revision|bool False if missing
3759 public static function statelessFetchRevision( Title
$title, $parser = false ) {
3760 $rev = Revision
::newKnownCurrent( wfGetDB( DB_REPLICA
), $title );
3766 * Fetch the unparsed text of a template and register a reference to it.
3767 * @param Title $title
3768 * @return array ( string or false, Title )
3770 public function fetchTemplateAndTitle( $title ) {
3771 // Defaults to Parser::statelessFetchTemplate()
3772 $templateCb = $this->mOptions
->getTemplateCallback();
3773 $stuff = call_user_func( $templateCb, $title, $this );
3774 $rev = $stuff['revision'] ??
null;
3775 $text = $stuff['text'];
3776 if ( is_string( $stuff['text'] ) ) {
3777 // We use U+007F DELETE to distinguish strip markers from regular text
3778 $text = strtr( $text, "\x7f", "?" );
3780 $finalTitle = $stuff['finalTitle'] ??
$title;
3781 foreach ( ( $stuff['deps'] ??
[] ) as $dep ) {
3782 $this->mOutput
->addTemplate( $dep['title'], $dep['page_id'], $dep['rev_id'] );
3783 if ( $dep['title']->equals( $this->getTitle() ) && $rev instanceof Revision
) {
3784 // Self-transclusion; final result may change based on the new page version
3785 $this->setOutputFlag( 'vary-revision-sha1', 'Self transclusion' );
3786 $this->getOutput()->setRevisionUsedSha1Base36( $rev->getSha1() );
3790 return [ $text, $finalTitle ];
3794 * Fetch the unparsed text of a template and register a reference to it.
3795 * @param Title $title
3796 * @return string|bool
3798 public function fetchTemplate( $title ) {
3799 return $this->fetchTemplateAndTitle( $title )[0];
3803 * Static function to get a template
3804 * Can be overridden via ParserOptions::setTemplateCallback().
3806 * @param Title $title
3807 * @param bool|Parser $parser
3811 public static function statelessFetchTemplate( $title, $parser = false ) {
3812 $text = $skip = false;
3813 $finalTitle = $title;
3817 # Loop to fetch the article, with up to 1 redirect
3818 for ( $i = 0; $i < 2 && is_object( $title ); $i++
) {
3819 # Give extensions a chance to select the revision instead
3820 $id = false; # Assume current
3821 Hooks
::run( 'BeforeParserFetchTemplateAndtitle',
3822 [ $parser, $title, &$skip, &$id ] );
3828 'page_id' => $title->getArticleID(),
3835 $rev = Revision
::newFromId( $id );
3836 } elseif ( $parser ) {
3837 $rev = $parser->fetchCurrentRevisionOfTitle( $title );
3839 $rev = Revision
::newFromTitle( $title );
3841 $rev_id = $rev ?
$rev->getId() : 0;
3842 # If there is no current revision, there is no page
3843 if ( $id === false && !$rev ) {
3844 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
3845 $linkCache->addBadLinkObj( $title );
3850 'page_id' => $title->getArticleID(),
3853 if ( $rev && !$title->equals( $rev->getTitle() ) ) {
3854 # We fetched a rev from a different title; register it too...
3856 'title' => $rev->getTitle(),
3857 'page_id' => $rev->getPage(),
3863 $content = $rev->getContent();
3864 $text = $content ?
$content->getWikitextForTransclusion() : null;
3866 Hooks
::run( 'ParserFetchTemplate',
3867 [ $parser, $title, $rev, &$text, &$deps ] );
3869 if ( $text === false ||
$text === null ) {
3873 } elseif ( $title->getNamespace() == NS_MEDIAWIKI
) {
3874 $message = wfMessage( MediaWikiServices
::getInstance()->getContentLanguage()->
3875 lcfirst( $title->getText() ) )->inContentLanguage();
3876 if ( !$message->exists() ) {
3880 $content = $message->content();
3881 $text = $message->plain();
3889 $finalTitle = $title;
3890 $title = $content->getRedirectTarget();
3895 'finalTitle' => $finalTitle,
3901 * Fetch a file and its title and register a reference to it.
3902 * If 'broken' is a key in $options then the file will appear as a broken thumbnail.
3903 * @param Title $title
3904 * @param array $options Array of options to RepoGroup::findFile
3905 * @return array ( File or false, Title of file )
3907 public function fetchFileAndTitle( $title, $options = [] ) {
3908 $file = $this->fetchFileNoRegister( $title, $options );
3910 $time = $file ?
$file->getTimestamp() : false;
3911 $sha1 = $file ?
$file->getSha1() : false;
3912 # Register the file as a dependency...
3913 $this->mOutput
->addImage( $title->getDBkey(), $time, $sha1 );
3914 if ( $file && !$title->equals( $file->getTitle() ) ) {
3915 # Update fetched file title
3916 $title = $file->getTitle();
3917 $this->mOutput
->addImage( $title->getDBkey(), $time, $sha1 );
3919 return [ $file, $title ];
3923 * Helper function for fetchFileAndTitle.
3925 * Also useful if you need to fetch a file but not use it yet,
3926 * for example to get the file's handler.
3928 * @param Title $title
3929 * @param array $options Array of options to RepoGroup::findFile
3932 protected function fetchFileNoRegister( $title, $options = [] ) {
3933 if ( isset( $options['broken'] ) ) {
3934 $file = false; // broken thumbnail forced by hook
3935 } elseif ( isset( $options['sha1'] ) ) { // get by (sha1,timestamp)
3936 $file = RepoGroup
::singleton()->findFileFromKey( $options['sha1'], $options );
3937 } else { // get by (name,timestamp)
3938 $file = MediaWikiServices
::getInstance()->getRepoGroup()->findFile( $title, $options );
3944 * Transclude an interwiki link.
3946 * @param Title $title
3947 * @param string $action Usually one of (raw, render)
3951 public function interwikiTransclude( $title, $action ) {
3952 if ( !$this->svcOptions
->get( 'EnableScaryTranscluding' ) ) {
3953 return wfMessage( 'scarytranscludedisabled' )->inContentLanguage()->text();
3956 $url = $title->getFullURL( [ 'action' => $action ] );
3957 if ( strlen( $url ) > 1024 ) {
3958 return wfMessage( 'scarytranscludetoolong' )->inContentLanguage()->text();
3961 $wikiId = $title->getTransWikiID(); // remote wiki ID or false
3963 $fname = __METHOD__
;
3964 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
3966 $data = $cache->getWithSetCallback(
3967 $cache->makeGlobalKey(
3968 'interwiki-transclude',
3969 ( $wikiId !== false ) ?
$wikiId : 'external',
3972 $this->svcOptions
->get( 'TranscludeCacheExpiry' ),
3973 function ( $oldValue, &$ttl ) use ( $url, $fname, $cache ) {
3974 $req = MWHttpRequest
::factory( $url, [], $fname );
3976 $status = $req->execute(); // Status object
3977 if ( !$status->isOK() ) {
3978 $ttl = $cache::TTL_UNCACHEABLE
;
3979 } elseif ( $req->getResponseHeader( 'X-Database-Lagged' ) !== null ) {
3980 $ttl = min( $cache::TTL_LAGGED
, $ttl );
3984 'text' => $status->isOK() ?
$req->getContent() : null,
3985 'code' => $req->getStatus()
3989 'checkKeys' => ( $wikiId !== false )
3990 ?
[ $cache->makeGlobalKey( 'interwiki-page', $wikiId, $title->getDBkey() ) ]
3992 'pcGroup' => 'interwiki-transclude:5',
3993 'pcTTL' => $cache::TTL_PROC_LONG
3997 if ( is_string( $data['text'] ) ) {
3998 $text = $data['text'];
3999 } elseif ( $data['code'] != 200 ) {
4000 // Though we failed to fetch the content, this status is useless.
4001 $text = wfMessage( 'scarytranscludefailed-httpstatus' )
4002 ->params( $url, $data['code'] )->inContentLanguage()->text();
4004 $text = wfMessage( 'scarytranscludefailed', $url )->inContentLanguage()->text();
4011 * Triple brace replacement -- used for template arguments
4014 * @param array $piece
4015 * @param PPFrame $frame
4019 public function argSubstitution( $piece, $frame ) {
4021 $parts = $piece['parts'];
4022 $nameWithSpaces = $frame->expand( $piece['title'] );
4023 $argName = trim( $nameWithSpaces );
4025 $text = $frame->getArgument( $argName );
4026 if ( $text === false && $parts->getLength() > 0
4027 && ( $this->ot
['html']
4029 ||
( $this->ot
['wiki'] && $frame->isTemplate() )
4032 # No match in frame, use the supplied default
4033 $object = $parts->item( 0 )->getChildren();
4035 if ( !$this->incrementIncludeSize( 'arg', strlen( $text ) ) ) {
4036 $error = '<!-- WARNING: argument omitted, expansion size too large -->';
4037 $this->limitationWarn( 'post-expand-template-argument' );
4040 if ( $text === false && $object === false ) {
4042 $object = $frame->virtualBracketedImplode( '{{{', '|', '}}}', $nameWithSpaces, $parts );
4044 if ( $error !== false ) {
4047 if ( $object !== false ) {
4048 $ret = [ 'object' => $object ];
4050 $ret = [ 'text' => $text ];
4057 * Return the text to be used for a given extension tag.
4058 * This is the ghost of strip().
4060 * @param array $params Associative array of parameters:
4061 * name PPNode for the tag name
4062 * attr PPNode for unparsed text where tag attributes are thought to be
4063 * attributes Optional associative array of parsed attributes
4064 * inner Contents of extension element
4065 * noClose Original text did not have a close tag
4066 * @param PPFrame $frame
4068 * @throws MWException
4071 public function extensionSubstitution( $params, $frame ) {
4072 static $errorStr = '<span class="error">';
4073 static $errorLen = 20;
4075 $name = $frame->expand( $params['name'] );
4076 if ( substr( $name, 0, $errorLen ) === $errorStr ) {
4077 // Probably expansion depth or node count exceeded. Just punt the
4082 $attrText = !isset( $params['attr'] ) ?
null : $frame->expand( $params['attr'] );
4083 if ( substr( $attrText, 0, $errorLen ) === $errorStr ) {
4088 // We can't safely check if the expansion for $content resulted in an
4089 // error, because the content could happen to be the error string
4091 $content = !isset( $params['inner'] ) ?
null : $frame->expand( $params['inner'] );
4093 $marker = self
::MARKER_PREFIX
. "-$name-"
4094 . sprintf( '%08X', $this->mMarkerIndex++
) . self
::MARKER_SUFFIX
;
4096 $isFunctionTag = isset( $this->mFunctionTagHooks
[strtolower( $name )] ) &&
4097 ( $this->ot
['html'] ||
$this->ot
['pre'] );
4098 if ( $isFunctionTag ) {
4099 $markerType = 'none';
4101 $markerType = 'general';
4103 if ( $this->ot
['html'] ||
$isFunctionTag ) {
4104 $name = strtolower( $name );
4105 $attributes = Sanitizer
::decodeTagAttributes( $attrText );
4106 if ( isset( $params['attributes'] ) ) {
4107 $attributes +
= $params['attributes'];
4110 if ( isset( $this->mTagHooks
[$name] ) ) {
4111 $output = call_user_func_array( $this->mTagHooks
[$name],
4112 [ $content, $attributes, $this, $frame ] );
4113 } elseif ( isset( $this->mFunctionTagHooks
[$name] ) ) {
4114 list( $callback, ) = $this->mFunctionTagHooks
[$name];
4116 // Avoid PHP 7.1 warning from passing $this by reference
4118 $output = call_user_func_array( $callback, [ &$parser, $frame, $content, $attributes ] );
4120 $output = '<span class="error">Invalid tag extension name: ' .
4121 htmlspecialchars( $name ) . '</span>';
4124 if ( is_array( $output ) ) {
4127 $output = $flags[0];
4128 if ( isset( $flags['markerType'] ) ) {
4129 $markerType = $flags['markerType'];
4133 if ( is_null( $attrText ) ) {
4136 if ( isset( $params['attributes'] ) ) {
4137 foreach ( $params['attributes'] as $attrName => $attrValue ) {
4138 $attrText .= ' ' . htmlspecialchars( $attrName ) . '="' .
4139 htmlspecialchars( $attrValue ) . '"';
4142 if ( $content === null ) {
4143 $output = "<$name$attrText/>";
4145 $close = is_null( $params['close'] ) ?
'' : $frame->expand( $params['close'] );
4146 if ( substr( $close, 0, $errorLen ) === $errorStr ) {
4150 $output = "<$name$attrText>$content$close";
4154 if ( $markerType === 'none' ) {
4156 } elseif ( $markerType === 'nowiki' ) {
4157 $this->mStripState
->addNoWiki( $marker, $output );
4158 } elseif ( $markerType === 'general' ) {
4159 $this->mStripState
->addGeneral( $marker, $output );
4161 throw new MWException( __METHOD__
. ': invalid marker type' );
4167 * Increment an include size counter
4169 * @param string $type The type of expansion
4170 * @param int $size The size of the text
4171 * @return bool False if this inclusion would take it over the maximum, true otherwise
4173 public function incrementIncludeSize( $type, $size ) {
4174 if ( $this->mIncludeSizes
[$type] +
$size > $this->mOptions
->getMaxIncludeSize() ) {
4177 $this->mIncludeSizes
[$type] +
= $size;
4183 * Increment the expensive function count
4185 * @return bool False if the limit has been exceeded
4187 public function incrementExpensiveFunctionCount() {
4188 $this->mExpensiveFunctionCount++
;
4189 return $this->mExpensiveFunctionCount
<= $this->mOptions
->getExpensiveParserFunctionLimit();
4193 * Strip double-underscore items like __NOGALLERY__ and __NOTOC__
4194 * Fills $this->mDoubleUnderscores, returns the modified text
4196 * @param string $text
4200 public function doDoubleUnderscore( $text ) {
4201 # The position of __TOC__ needs to be recorded
4202 $mw = $this->magicWordFactory
->get( 'toc' );
4203 if ( $mw->match( $text ) ) {
4204 $this->mShowToc
= true;
4205 $this->mForceTocPosition
= true;
4207 # Set a placeholder. At the end we'll fill it in with the TOC.
4208 $text = $mw->replace( '<!--MWTOC\'"-->', $text, 1 );
4210 # Only keep the first one.
4211 $text = $mw->replace( '', $text );
4214 # Now match and remove the rest of them
4215 $mwa = $this->magicWordFactory
->getDoubleUnderscoreArray();
4216 $this->mDoubleUnderscores
= $mwa->matchAndRemove( $text );
4218 if ( isset( $this->mDoubleUnderscores
['nogallery'] ) ) {
4219 $this->mOutput
->mNoGallery
= true;
4221 if ( isset( $this->mDoubleUnderscores
['notoc'] ) && !$this->mForceTocPosition
) {
4222 $this->mShowToc
= false;
4224 if ( isset( $this->mDoubleUnderscores
['hiddencat'] )
4225 && $this->mTitle
->getNamespace() == NS_CATEGORY
4227 $this->addTrackingCategory( 'hidden-category-category' );
4229 # (T10068) Allow control over whether robots index a page.
4230 # __INDEX__ always overrides __NOINDEX__, see T16899
4231 if ( isset( $this->mDoubleUnderscores
['noindex'] ) && $this->mTitle
->canUseNoindex() ) {
4232 $this->mOutput
->setIndexPolicy( 'noindex' );
4233 $this->addTrackingCategory( 'noindex-category' );
4235 if ( isset( $this->mDoubleUnderscores
['index'] ) && $this->mTitle
->canUseNoindex() ) {
4236 $this->mOutput
->setIndexPolicy( 'index' );
4237 $this->addTrackingCategory( 'index-category' );
4240 # Cache all double underscores in the database
4241 foreach ( $this->mDoubleUnderscores
as $key => $val ) {
4242 $this->mOutput
->setProperty( $key, '' );
4249 * @see ParserOutput::addTrackingCategory()
4250 * @param string $msg Message key
4251 * @return bool Whether the addition was successful
4253 public function addTrackingCategory( $msg ) {
4254 return $this->mOutput
->addTrackingCategory( $msg, $this->mTitle
);
4258 * This function accomplishes several tasks:
4259 * 1) Auto-number headings if that option is enabled
4260 * 2) Add an [edit] link to sections for users who have enabled the option and can edit the page
4261 * 3) Add a Table of contents on the top for users who have enabled the option
4262 * 4) Auto-anchor headings
4264 * It loops through all headlines, collects the necessary data, then splits up the
4265 * string and re-inserts the newly formatted headlines.
4267 * @param string $text
4268 * @param string $origText Original, untouched wikitext
4269 * @param bool $isMain
4270 * @return mixed|string
4273 public function formatHeadings( $text, $origText, $isMain = true ) {
4274 # Inhibit editsection links if requested in the page
4275 if ( isset( $this->mDoubleUnderscores
['noeditsection'] ) ) {
4276 $maybeShowEditLink = false;
4278 $maybeShowEditLink = true; /* Actual presence will depend on post-cache transforms */
4281 # Get all headlines for numbering them and adding funky stuff like [edit]
4282 # links - this is for later, but we need the number of headlines right now
4283 # NOTE: white space in headings have been trimmed in doHeadings. They shouldn't
4284 # be trimmed here since whitespace in HTML headings is significant.
4286 $numMatches = preg_match_all(
4287 '/<H(?P<level>[1-6])(?P<attrib>.*?>)(?P<header>[\s\S]*?)<\/H[1-6] *>/i',
4292 # if there are fewer than 4 headlines in the article, do not show TOC
4293 # unless it's been explicitly enabled.
4294 $enoughToc = $this->mShowToc
&&
4295 ( ( $numMatches >= 4 ) ||
$this->mForceTocPosition
);
4297 # Allow user to stipulate that a page should have a "new section"
4298 # link added via __NEWSECTIONLINK__
4299 if ( isset( $this->mDoubleUnderscores
['newsectionlink'] ) ) {
4300 $this->mOutput
->setNewSection( true );
4303 # Allow user to remove the "new section"
4304 # link via __NONEWSECTIONLINK__
4305 if ( isset( $this->mDoubleUnderscores
['nonewsectionlink'] ) ) {
4306 $this->mOutput
->hideNewSection( true );
4309 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
4310 # override above conditions and always show TOC above first header
4311 if ( isset( $this->mDoubleUnderscores
['forcetoc'] ) ) {
4312 $this->mShowToc
= true;
4320 # Ugh .. the TOC should have neat indentation levels which can be
4321 # passed to the skin functions. These are determined here
4325 $sublevelCount = [];
4331 $markerRegex = self
::MARKER_PREFIX
. "-h-(\d+)-" . self
::MARKER_SUFFIX
;
4332 $baseTitleText = $this->mTitle
->getPrefixedDBkey();
4333 $oldType = $this->mOutputType
;
4334 $this->setOutputType( self
::OT_WIKI
);
4335 $frame = $this->getPreprocessor()->newFrame();
4336 $root = $this->preprocessToDom( $origText );
4337 $node = $root->getFirstChild();
4342 $headlines = $numMatches !== false ?
$matches[3] : [];
4344 $maxTocLevel = $this->svcOptions
->get( 'MaxTocLevel' );
4345 foreach ( $headlines as $headline ) {
4346 $isTemplate = false;
4348 $sectionIndex = false;
4350 $markerMatches = [];
4351 if ( preg_match( "/^$markerRegex/", $headline, $markerMatches ) ) {
4352 $serial = $markerMatches[1];
4353 list( $titleText, $sectionIndex ) = $this->mHeadings
[$serial];
4354 $isTemplate = ( $titleText != $baseTitleText );
4355 $headline = preg_replace( "/^$markerRegex\\s*/", "", $headline );
4359 $prevlevel = $level;
4361 $level = $matches[1][$headlineCount];
4363 if ( $level > $prevlevel ) {
4364 # Increase TOC level
4366 $sublevelCount[$toclevel] = 0;
4367 if ( $toclevel < $maxTocLevel ) {
4368 $prevtoclevel = $toclevel;
4369 $toc .= Linker
::tocIndent();
4372 } elseif ( $level < $prevlevel && $toclevel > 1 ) {
4373 # Decrease TOC level, find level to jump to
4375 for ( $i = $toclevel; $i > 0; $i-- ) {
4376 if ( $levelCount[$i] == $level ) {
4377 # Found last matching level
4380 } elseif ( $levelCount[$i] < $level ) {
4381 # Found first matching level below current level
4389 if ( $toclevel < $maxTocLevel ) {
4390 if ( $prevtoclevel < $maxTocLevel ) {
4391 # Unindent only if the previous toc level was shown :p
4392 $toc .= Linker
::tocUnindent( $prevtoclevel - $toclevel );
4393 $prevtoclevel = $toclevel;
4395 $toc .= Linker
::tocLineEnd();
4399 # No change in level, end TOC line
4400 if ( $toclevel < $maxTocLevel ) {
4401 $toc .= Linker
::tocLineEnd();
4405 $levelCount[$toclevel] = $level;
4407 # count number of headlines for each level
4408 $sublevelCount[$toclevel]++
;
4410 for ( $i = 1; $i <= $toclevel; $i++
) {
4411 if ( !empty( $sublevelCount[$i] ) ) {
4415 $numbering .= $this->getTargetLanguage()->formatNum( $sublevelCount[$i] );
4420 # The safe header is a version of the header text safe to use for links
4422 # Remove link placeholders by the link text.
4423 # <!--LINK number-->
4425 # link text with suffix
4426 # Do this before unstrip since link text can contain strip markers
4427 $safeHeadline = $this->replaceLinkHoldersText( $headline );
4429 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
4430 $safeHeadline = $this->mStripState
->unstripBoth( $safeHeadline );
4432 # Remove any <style> or <script> tags (T198618)
4433 $safeHeadline = preg_replace(
4434 '#<(style|script)(?: [^>]*[^>/])?>.*?</\1>#is',
4439 # Strip out HTML (first regex removes any tag not allowed)
4441 # * <sup> and <sub> (T10393)
4445 # * <span dir="rtl"> and <span dir="ltr"> (T37167)
4446 # * <s> and <strike> (T35715)
4447 # We strip any parameter from accepted tags (second regex), except dir="rtl|ltr" from <span>,
4448 # to allow setting directionality in toc items.
4449 $tocline = preg_replace(
4451 '#<(?!/?(span|sup|sub|bdi|i|b|s|strike)(?: [^>]*)?>).*?>#',
4452 '#<(/?(?:span(?: dir="(?:rtl|ltr)")?|sup|sub|bdi|i|b|s|strike))(?: .*?)?>#'
4458 # Strip '<span></span>', which is the result from the above if
4459 # <span id="foo"></span> is used to produce an additional anchor
4461 $tocline = str_replace( '<span></span>', '', $tocline );
4463 $tocline = trim( $tocline );
4465 # For the anchor, strip out HTML-y stuff period
4466 $safeHeadline = preg_replace( '/<.*?>/', '', $safeHeadline );
4467 $safeHeadline = Sanitizer
::normalizeSectionNameWhitespace( $safeHeadline );
4469 # Save headline for section edit hint before it's escaped
4470 $headlineHint = $safeHeadline;
4472 # Decode HTML entities
4473 $safeHeadline = Sanitizer
::decodeCharReferences( $safeHeadline );
4475 $safeHeadline = self
::normalizeSectionName( $safeHeadline );
4477 $fallbackHeadline = Sanitizer
::escapeIdForAttribute( $safeHeadline, Sanitizer
::ID_FALLBACK
);
4478 $linkAnchor = Sanitizer
::escapeIdForLink( $safeHeadline );
4479 $safeHeadline = Sanitizer
::escapeIdForAttribute( $safeHeadline, Sanitizer
::ID_PRIMARY
);
4480 if ( $fallbackHeadline === $safeHeadline ) {
4481 # No reason to have both (in fact, we can't)
4482 $fallbackHeadline = false;
4485 # HTML IDs must be case-insensitively unique for IE compatibility (T12721).
4486 # @todo FIXME: We may be changing them depending on the current locale.
4487 $arrayKey = strtolower( $safeHeadline );
4488 if ( $fallbackHeadline === false ) {
4489 $fallbackArrayKey = false;
4491 $fallbackArrayKey = strtolower( $fallbackHeadline );
4494 # Create the anchor for linking from the TOC to the section
4495 $anchor = $safeHeadline;
4496 $fallbackAnchor = $fallbackHeadline;
4497 if ( isset( $refers[$arrayKey] ) ) {
4498 // phpcs:ignore Generic.Formatting.DisallowMultipleStatements
4499 for ( $i = 2; isset( $refers["${arrayKey}_$i"] ); ++
$i );
4501 $linkAnchor .= "_$i";
4502 $refers["${arrayKey}_$i"] = true;
4504 $refers[$arrayKey] = true;
4506 if ( $fallbackHeadline !== false && isset( $refers[$fallbackArrayKey] ) ) {
4507 // phpcs:ignore Generic.Formatting.DisallowMultipleStatements
4508 for ( $i = 2; isset( $refers["${fallbackArrayKey}_$i"] ); ++
$i );
4509 $fallbackAnchor .= "_$i";
4510 $refers["${fallbackArrayKey}_$i"] = true;
4512 $refers[$fallbackArrayKey] = true;
4515 # Don't number the heading if it is the only one (looks silly)
4516 if ( count( $matches[3] ) > 1 && $this->mOptions
->getNumberHeadings() ) {
4517 # the two are different if the line contains a link
4518 $headline = Html
::element(
4520 [ 'class' => 'mw-headline-number' ],
4522 ) . ' ' . $headline;
4525 if ( $enoughToc && ( !isset( $maxTocLevel ) ||
$toclevel < $maxTocLevel ) ) {
4526 $toc .= Linker
::tocLine( $linkAnchor, $tocline,
4527 $numbering, $toclevel, ( $isTemplate ?
false : $sectionIndex ) );
4530 # Add the section to the section tree
4531 # Find the DOM node for this header
4532 $noOffset = ( $isTemplate ||
$sectionIndex === false );
4533 while ( $node && !$noOffset ) {
4534 if ( $node->getName() === 'h' ) {
4535 $bits = $node->splitHeading();
4536 if ( $bits['i'] == $sectionIndex ) {
4540 $byteOffset +
= mb_strlen( $this->mStripState
->unstripBoth(
4541 $frame->expand( $node, PPFrame
::RECOVER_ORIG
) ) );
4542 $node = $node->getNextSibling();
4545 'toclevel' => $toclevel,
4548 'number' => $numbering,
4549 'index' => ( $isTemplate ?
'T-' : '' ) . $sectionIndex,
4550 'fromtitle' => $titleText,
4551 'byteoffset' => ( $noOffset ?
null : $byteOffset ),
4552 'anchor' => $anchor,
4555 # give headline the correct <h#> tag
4556 if ( $maybeShowEditLink && $sectionIndex !== false ) {
4557 // Output edit section links as markers with styles that can be customized by skins
4558 if ( $isTemplate ) {
4559 # Put a T flag in the section identifier, to indicate to extractSections()
4560 # that sections inside <includeonly> should be counted.
4561 $editsectionPage = $titleText;
4562 $editsectionSection = "T-$sectionIndex";
4563 $editsectionContent = null;
4565 $editsectionPage = $this->mTitle
->getPrefixedText();
4566 $editsectionSection = $sectionIndex;
4567 $editsectionContent = $headlineHint;
4569 // We use a bit of pesudo-xml for editsection markers. The
4570 // language converter is run later on. Using a UNIQ style marker
4571 // leads to the converter screwing up the tokens when it
4572 // converts stuff. And trying to insert strip tags fails too. At
4573 // this point all real inputted tags have already been escaped,
4574 // so we don't have to worry about a user trying to input one of
4575 // these markers directly. We use a page and section attribute
4576 // to stop the language converter from converting these
4577 // important bits of data, but put the headline hint inside a
4578 // content block because the language converter is supposed to
4579 // be able to convert that piece of data.
4580 // Gets replaced with html in ParserOutput::getText
4581 $editlink = '<mw:editsection page="' . htmlspecialchars( $editsectionPage );
4582 $editlink .= '" section="' . htmlspecialchars( $editsectionSection ) . '"';
4583 if ( $editsectionContent !== null ) {
4584 $editlink .= '>' . $editsectionContent . '</mw:editsection>';
4591 $head[$headlineCount] = Linker
::makeHeadline( $level,
4592 $matches['attrib'][$headlineCount], $anchor, $headline,
4593 $editlink, $fallbackAnchor );
4598 $this->setOutputType( $oldType );
4600 # Never ever show TOC if no headers
4601 if ( $numVisible < 1 ) {
4606 if ( $prevtoclevel > 0 && $prevtoclevel < $maxTocLevel ) {
4607 $toc .= Linker
::tocUnindent( $prevtoclevel - 1 );
4609 $toc = Linker
::tocList( $toc, $this->mOptions
->getUserLangObj() );
4610 $this->mOutput
->setTOCHTML( $toc );
4611 $toc = self
::TOC_START
. $toc . self
::TOC_END
;
4615 $this->mOutput
->setSections( $tocraw );
4618 # split up and insert constructed headlines
4619 $blocks = preg_split( '/<H[1-6].*?>[\s\S]*?<\/H[1-6]>/i', $text );
4622 // build an array of document sections
4624 foreach ( $blocks as $block ) {
4625 // $head is zero-based, sections aren't.
4626 if ( empty( $head[$i - 1] ) ) {
4627 $sections[$i] = $block;
4629 $sections[$i] = $head[$i - 1] . $block;
4633 * Send a hook, one per section.
4634 * The idea here is to be able to make section-level DIVs, but to do so in a
4635 * lower-impact, more correct way than r50769
4638 * $section : the section number
4639 * &$sectionContent : ref to the content of the section
4640 * $maybeShowEditLinks : boolean describing whether this section has an edit link
4642 Hooks
::run( 'ParserSectionCreate', [ $this, $i, &$sections[$i], $maybeShowEditLink ] );
4647 if ( $enoughToc && $isMain && !$this->mForceTocPosition
) {
4648 // append the TOC at the beginning
4649 // Top anchor now in skin
4650 $sections[0] .= $toc . "\n";
4653 $full .= implode( '', $sections );
4655 if ( $this->mForceTocPosition
) {
4656 return str_replace( '<!--MWTOC\'"-->', $toc, $full );
4663 * Transform wiki markup when saving a page by doing "\r\n" -> "\n"
4664 * conversion, substituting signatures, {{subst:}} templates, etc.
4666 * @param string $text The text to transform
4667 * @param Title $title The Title object for the current article
4668 * @param User $user The User object describing the current user
4669 * @param ParserOptions $options Parsing options
4670 * @param bool $clearState Whether to clear the parser state first
4671 * @return string The altered wiki markup
4673 public function preSaveTransform( $text, Title
$title, User
$user,
4674 ParserOptions
$options, $clearState = true
4676 if ( $clearState ) {
4677 $magicScopeVariable = $this->lock();
4679 $this->startParse( $title, $options, self
::OT_WIKI
, $clearState );
4680 $this->setUser( $user );
4682 // Strip U+0000 NULL (T159174)
4683 $text = str_replace( "\000", '', $text );
4685 // We still normalize line endings for backwards-compatibility
4686 // with other code that just calls PST, but this should already
4687 // be handled in TextContent subclasses
4688 $text = TextContent
::normalizeLineEndings( $text );
4690 if ( $options->getPreSaveTransform() ) {
4691 $text = $this->pstPass2( $text, $user );
4693 $text = $this->mStripState
->unstripBoth( $text );
4695 $this->setUser( null ); # Reset
4701 * Pre-save transform helper function
4703 * @param string $text
4708 private function pstPass2( $text, $user ) {
4709 # Note: This is the timestamp saved as hardcoded wikitext to the database, we use
4710 # $this->contLang here in order to give everyone the same signature and use the default one
4711 # rather than the one selected in each user's preferences. (see also T14815)
4712 $ts = $this->mOptions
->getTimestamp();
4713 $timestamp = MWTimestamp
::getLocalInstance( $ts );
4714 $ts = $timestamp->format( 'YmdHis' );
4715 $tzMsg = $timestamp->getTimezoneMessage()->inContentLanguage()->text();
4717 $d = $this->contLang
->timeanddate( $ts, false, false ) . " ($tzMsg)";
4719 # Variable replacement
4720 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
4721 $text = $this->replaceVariables( $text );
4723 # This works almost by chance, as the replaceVariables are done before the getUserSig(),
4724 # which may corrupt this parser instance via its wfMessage()->text() call-
4727 if ( strpos( $text, '~~~' ) !== false ) {
4728 $sigText = $this->getUserSig( $user );
4729 $text = strtr( $text, [
4731 '~~~~' => "$sigText $d",
4734 # The main two signature forms used above are time-sensitive
4735 $this->setOutputFlag( 'user-signature', 'User signature detected' );
4738 # Context links ("pipe tricks"): [[|name]] and [[name (context)|]]
4739 $tc = '[' . Title
::legalChars() . ']';
4740 $nc = '[ _0-9A-Za-z\x80-\xff-]'; # Namespaces can use non-ascii!
4742 // [[ns:page (context)|]]
4743 $p1 = "/\[\[(:?$nc+:|:|)($tc+?)( ?\\($tc+\\))\\|]]/";
4744 // [[ns:page(context)|]] (double-width brackets, added in r40257)
4745 $p4 = "/\[\[(:?$nc+:|:|)($tc+?)( ?($tc+))\\|]]/";
4746 // [[ns:page (context), context|]] (using either single or double-width comma)
4747 $p3 = "/\[\[(:?$nc+:|:|)($tc+?)( ?\\($tc+\\)|)((?:, |,)$tc+|)\\|]]/";
4748 // [[|page]] (reverse pipe trick: add context from page title)
4749 $p2 = "/\[\[\\|($tc+)]]/";
4751 # try $p1 first, to turn "[[A, B (C)|]]" into "[[A, B (C)|A, B]]"
4752 $text = preg_replace( $p1, '[[\\1\\2\\3|\\2]]', $text );
4753 $text = preg_replace( $p4, '[[\\1\\2\\3|\\2]]', $text );
4754 $text = preg_replace( $p3, '[[\\1\\2\\3\\4|\\2]]', $text );
4756 $t = $this->mTitle
->getText();
4758 if ( preg_match( "/^($nc+:|)$tc+?( \\($tc+\\))$/", $t, $m ) ) {
4759 $text = preg_replace( $p2, "[[$m[1]\\1$m[2]|\\1]]", $text );
4760 } elseif ( preg_match( "/^($nc+:|)$tc+?(, $tc+|)$/", $t, $m ) && "$m[1]$m[2]" != '' ) {
4761 $text = preg_replace( $p2, "[[$m[1]\\1$m[2]|\\1]]", $text );
4763 # if there's no context, don't bother duplicating the title
4764 $text = preg_replace( $p2, '[[\\1]]', $text );
4771 * Fetch the user's signature text, if any, and normalize to
4772 * validated, ready-to-insert wikitext.
4773 * If you have pre-fetched the nickname or the fancySig option, you can
4774 * specify them here to save a database query.
4775 * Do not reuse this parser instance after calling getUserSig(),
4776 * as it may have changed.
4778 * @param User &$user
4779 * @param string|bool $nickname Nickname to use or false to use user's default nickname
4780 * @param bool|null $fancySig whether the nicknname is the complete signature
4781 * or null to use default value
4784 public function getUserSig( &$user, $nickname = false, $fancySig = null ) {
4785 $username = $user->getName();
4787 # If not given, retrieve from the user object.
4788 if ( $nickname === false ) {
4789 $nickname = $user->getOption( 'nickname' );
4792 if ( is_null( $fancySig ) ) {
4793 $fancySig = $user->getBoolOption( 'fancysig' );
4796 $nickname = $nickname == null ?
$username : $nickname;
4798 if ( mb_strlen( $nickname ) > $this->svcOptions
->get( 'MaxSigChars' ) ) {
4799 $nickname = $username;
4800 $this->logger
->debug( __METHOD__
. ": $username has overlong signature." );
4801 } elseif ( $fancySig !== false ) {
4802 # Sig. might contain markup; validate this
4803 if ( $this->validateSig( $nickname ) !== false ) {
4804 # Validated; clean up (if needed) and return it
4805 return $this->cleanSig( $nickname, true );
4807 # Failed to validate; fall back to the default
4808 $nickname = $username;
4809 $this->logger
->debug( __METHOD__
. ": $username has bad XML tags in signature." );
4813 # Make sure nickname doesnt get a sig in a sig
4814 $nickname = self
::cleanSigInSig( $nickname );
4816 # If we're still here, make it a link to the user page
4817 $userText = wfEscapeWikiText( $username );
4818 $nickText = wfEscapeWikiText( $nickname );
4819 $msgName = $user->isAnon() ?
'signature-anon' : 'signature';
4821 return wfMessage( $msgName, $userText, $nickText )->inContentLanguage()
4822 ->title( $this->getTitle() )->text();
4826 * Check that the user's signature contains no bad XML
4828 * @param string $text
4829 * @return string|bool An expanded string, or false if invalid.
4831 public function validateSig( $text ) {
4832 return Xml
::isWellFormedXmlFragment( $text ) ?
$text : false;
4836 * Clean up signature text
4838 * 1) Strip 3, 4 or 5 tildes out of signatures @see cleanSigInSig
4839 * 2) Substitute all transclusions
4841 * @param string $text
4842 * @param bool $parsing Whether we're cleaning (preferences save) or parsing
4843 * @return string Signature text
4845 public function cleanSig( $text, $parsing = false ) {
4848 $magicScopeVariable = $this->lock();
4849 $this->startParse( $wgTitle, new ParserOptions
, self
::OT_PREPROCESS
, true );
4852 # Option to disable this feature
4853 if ( !$this->mOptions
->getCleanSignatures() ) {
4857 # @todo FIXME: Regex doesn't respect extension tags or nowiki
4858 # => Move this logic to braceSubstitution()
4859 $substWord = $this->magicWordFactory
->get( 'subst' );
4860 $substRegex = '/\{\{(?!(?:' . $substWord->getBaseRegex() . '))/x' . $substWord->getRegexCase();
4861 $substText = '{{' . $substWord->getSynonym( 0 );
4863 $text = preg_replace( $substRegex, $substText, $text );
4864 $text = self
::cleanSigInSig( $text );
4865 $dom = $this->preprocessToDom( $text );
4866 $frame = $this->getPreprocessor()->newFrame();
4867 $text = $frame->expand( $dom );
4870 $text = $this->mStripState
->unstripBoth( $text );
4877 * Strip 3, 4 or 5 tildes out of signatures.
4879 * @param string $text
4880 * @return string Signature text with /~{3,5}/ removed
4882 public static function cleanSigInSig( $text ) {
4883 $text = preg_replace( '/~{3,5}/', '', $text );
4888 * Set up some variables which are usually set up in parse()
4889 * so that an external function can call some class members with confidence
4891 * @param Title|null $title
4892 * @param ParserOptions $options
4893 * @param int $outputType
4894 * @param bool $clearState
4895 * @param int|null $revId
4897 public function startExternalParse( Title
$title = null, ParserOptions
$options,
4898 $outputType, $clearState = true, $revId = null
4900 $this->startParse( $title, $options, $outputType, $clearState );
4901 if ( $revId !== null ) {
4902 $this->mRevisionId
= $revId;
4907 * @param Title|null $title
4908 * @param ParserOptions $options
4909 * @param int $outputType
4910 * @param bool $clearState
4912 private function startParse( Title
$title = null, ParserOptions
$options,
4913 $outputType, $clearState = true
4915 $this->setTitle( $title );
4916 $this->mOptions
= $options;
4917 $this->setOutputType( $outputType );
4918 if ( $clearState ) {
4919 $this->clearState();
4924 * Wrapper for preprocess()
4926 * @param string $text The text to preprocess
4927 * @param ParserOptions $options
4928 * @param Title|null $title Title object or null to use $wgTitle
4931 public function transformMsg( $text, $options, $title = null ) {
4932 static $executing = false;
4934 # Guard against infinite recursion
4945 $text = $this->preprocess( $text, $title, $options );
4952 * Create an HTML-style tag, e.g. "<yourtag>special text</yourtag>"
4953 * The callback should have the following form:
4954 * function myParserHook( $text, $params, $parser, $frame ) { ... }
4956 * Transform and return $text. Use $parser for any required context, e.g. use
4957 * $parser->getTitle() and $parser->getOptions() not $wgTitle or $wgOut->mParserOptions
4959 * Hooks may return extended information by returning an array, of which the
4960 * first numbered element (index 0) must be the return string, and all other
4961 * entries are extracted into local variables within an internal function
4962 * in the Parser class.
4964 * This interface (introduced r61913) appears to be undocumented, but
4965 * 'markerType' is used by some core tag hooks to override which strip
4966 * array their results are placed in. **Use great caution if attempting
4967 * this interface, as it is not documented and injudicious use could smash
4968 * private variables.**
4970 * @param string $tag The tag to use, e.g. 'hook' for "<hook>"
4971 * @param callable $callback The callback function (and object) to use for the tag
4972 * @throws MWException
4973 * @return callable|null The old value of the mTagHooks array associated with the hook
4975 public function setHook( $tag, callable
$callback ) {
4976 $tag = strtolower( $tag );
4977 if ( preg_match( '/[<>\r\n]/', $tag, $m ) ) {
4978 throw new MWException( "Invalid character {$m[0]} in setHook('$tag', ...) call" );
4980 $oldVal = $this->mTagHooks
[$tag] ??
null;
4981 $this->mTagHooks
[$tag] = $callback;
4982 if ( !in_array( $tag, $this->mStripList
) ) {
4983 $this->mStripList
[] = $tag;
4990 * As setHook(), but letting the contents be parsed.
4992 * Transparent tag hooks are like regular XML-style tag hooks, except they
4993 * operate late in the transformation sequence, on HTML instead of wikitext.
4995 * This is probably obsoleted by things dealing with parser frames?
4996 * The only extension currently using it is geoserver.
4999 * @todo better document or deprecate this
5001 * @param string $tag The tag to use, e.g. 'hook' for "<hook>"
5002 * @param callable $callback The callback function (and object) to use for the tag
5003 * @throws MWException
5004 * @return callable|null The old value of the mTagHooks array associated with the hook
5006 public function setTransparentTagHook( $tag, callable
$callback ) {
5007 $tag = strtolower( $tag );
5008 if ( preg_match( '/[<>\r\n]/', $tag, $m ) ) {
5009 throw new MWException( "Invalid character {$m[0]} in setTransparentHook('$tag', ...) call" );
5011 $oldVal = $this->mTransparentTagHooks
[$tag] ??
null;
5012 $this->mTransparentTagHooks
[$tag] = $callback;
5018 * Remove all tag hooks
5020 public function clearTagHooks() {
5021 $this->mTagHooks
= [];
5022 $this->mFunctionTagHooks
= [];
5023 $this->mStripList
= $this->mDefaultStripList
;
5027 * Create a function, e.g. {{sum:1|2|3}}
5028 * The callback function should have the form:
5029 * function myParserFunction( &$parser, $arg1, $arg2, $arg3 ) { ... }
5031 * Or with Parser::SFH_OBJECT_ARGS:
5032 * function myParserFunction( $parser, $frame, $args ) { ... }
5034 * The callback may either return the text result of the function, or an array with the text
5035 * in element 0, and a number of flags in the other elements. The names of the flags are
5036 * specified in the keys. Valid flags are:
5037 * found The text returned is valid, stop processing the template. This
5039 * nowiki Wiki markup in the return value should be escaped
5040 * isHTML The returned text is HTML, armour it against wikitext transformation
5042 * @param string $id The magic word ID
5043 * @param callable $callback The callback function (and object) to use
5044 * @param int $flags A combination of the following flags:
5045 * Parser::SFH_NO_HASH No leading hash, i.e. {{plural:...}} instead of {{#if:...}}
5047 * Parser::SFH_OBJECT_ARGS Pass the template arguments as PPNode objects instead of text.
5048 * This allows for conditional expansion of the parse tree, allowing you to eliminate dead
5049 * branches and thus speed up parsing. It is also possible to analyse the parse tree of
5050 * the arguments, and to control the way they are expanded.
5052 * The $frame parameter is a PPFrame. This can be used to produce expanded text from the
5053 * arguments, for instance:
5054 * $text = isset( $args[0] ) ? $frame->expand( $args[0] ) : '';
5056 * For technical reasons, $args[0] is pre-expanded and will be a string. This may change in
5057 * future versions. Please call $frame->expand() on it anyway so that your code keeps
5058 * working if/when this is changed.
5060 * If you want whitespace to be trimmed from $args, you need to do it yourself, post-
5063 * Please read the documentation in includes/parser/Preprocessor.php for more information
5064 * about the methods available in PPFrame and PPNode.
5066 * @throws MWException
5067 * @return string|callable The old callback function for this name, if any
5069 public function setFunctionHook( $id, callable
$callback, $flags = 0 ) {
5070 $oldVal = isset( $this->mFunctionHooks
[$id] ) ?
$this->mFunctionHooks
[$id][0] : null;
5071 $this->mFunctionHooks
[$id] = [ $callback, $flags ];
5073 # Add to function cache
5074 $mw = $this->magicWordFactory
->get( $id );
5076 throw new MWException( __METHOD__
. '() expecting a magic word identifier.' );
5079 $synonyms = $mw->getSynonyms();
5080 $sensitive = intval( $mw->isCaseSensitive() );
5082 foreach ( $synonyms as $syn ) {
5084 if ( !$sensitive ) {
5085 $syn = $this->contLang
->lc( $syn );
5088 if ( !( $flags & self
::SFH_NO_HASH
) ) {
5091 # Remove trailing colon
5092 if ( substr( $syn, -1, 1 ) === ':' ) {
5093 $syn = substr( $syn, 0, -1 );
5095 $this->mFunctionSynonyms
[$sensitive][$syn] = $id;
5101 * Get all registered function hook identifiers
5105 public function getFunctionHooks() {
5106 $this->firstCallInit();
5107 return array_keys( $this->mFunctionHooks
);
5111 * Create a tag function, e.g. "<test>some stuff</test>".
5112 * Unlike tag hooks, tag functions are parsed at preprocessor level.
5113 * Unlike parser functions, their content is not preprocessed.
5114 * @param string $tag
5115 * @param callable $callback
5117 * @throws MWException
5120 public function setFunctionTagHook( $tag, callable
$callback, $flags ) {
5121 $tag = strtolower( $tag );
5122 if ( preg_match( '/[<>\r\n]/', $tag, $m ) ) {
5123 throw new MWException( "Invalid character {$m[0]} in setFunctionTagHook('$tag', ...) call" );
5125 $old = $this->mFunctionTagHooks
[$tag] ??
null;
5126 $this->mFunctionTagHooks
[$tag] = [ $callback, $flags ];
5128 if ( !in_array( $tag, $this->mStripList
) ) {
5129 $this->mStripList
[] = $tag;
5136 * Replace "<!--LINK-->" link placeholders with actual links, in the buffer
5137 * Placeholders created in Linker::link()
5139 * @param string &$text
5140 * @param int $options
5142 public function replaceLinkHolders( &$text, $options = 0 ) {
5143 $this->mLinkHolders
->replace( $text );
5147 * Replace "<!--LINK-->" link placeholders with plain text of links
5148 * (not HTML-formatted).
5150 * @param string $text
5153 public function replaceLinkHoldersText( $text ) {
5154 return $this->mLinkHolders
->replaceText( $text );
5158 * Renders an image gallery from a text with one line per image.
5159 * text labels may be given by using |-style alternative text. E.g.
5160 * Image:one.jpg|The number "1"
5161 * Image:tree.jpg|A tree
5162 * given as text will return the HTML of a gallery with two images,
5163 * labeled 'The number "1"' and
5166 * @param string $text
5167 * @param array $params
5168 * @return string HTML
5170 public function renderImageGallery( $text, $params ) {
5172 if ( isset( $params['mode'] ) ) {
5173 $mode = $params['mode'];
5177 $ig = ImageGalleryBase
::factory( $mode );
5178 } catch ( Exception
$e ) {
5179 // If invalid type set, fallback to default.
5180 $ig = ImageGalleryBase
::factory( false );
5183 $ig->setContextTitle( $this->mTitle
);
5184 $ig->setShowBytes( false );
5185 $ig->setShowDimensions( false );
5186 $ig->setShowFilename( false );
5187 $ig->setParser( $this );
5188 $ig->setHideBadImages();
5189 $ig->setAttributes( Sanitizer
::validateTagAttributes( $params, 'ul' ) );
5191 if ( isset( $params['showfilename'] ) ) {
5192 $ig->setShowFilename( true );
5194 $ig->setShowFilename( false );
5196 if ( isset( $params['caption'] ) ) {
5197 // NOTE: We aren't passing a frame here or below. Frame info
5198 // is currently opaque to Parsoid, which acts on OT_PREPROCESS.
5199 // See T107332#4030581
5200 $caption = $this->recursiveTagParse( $params['caption'] );
5201 $ig->setCaptionHtml( $caption );
5203 if ( isset( $params['perrow'] ) ) {
5204 $ig->setPerRow( $params['perrow'] );
5206 if ( isset( $params['widths'] ) ) {
5207 $ig->setWidths( $params['widths'] );
5209 if ( isset( $params['heights'] ) ) {
5210 $ig->setHeights( $params['heights'] );
5212 $ig->setAdditionalOptions( $params );
5214 // Avoid PHP 7.1 warning from passing $this by reference
5216 Hooks
::run( 'BeforeParserrenderImageGallery', [ &$parser, &$ig ] );
5218 $lines = StringUtils
::explode( "\n", $text );
5219 foreach ( $lines as $line ) {
5220 # match lines like these:
5221 # Image:someimage.jpg|This is some image
5223 preg_match( "/^([^|]+)(\\|(.*))?$/", $line, $matches );
5225 if ( count( $matches ) == 0 ) {
5229 if ( strpos( $matches[0], '%' ) !== false ) {
5230 $matches[1] = rawurldecode( $matches[1] );
5232 $title = Title
::newFromText( $matches[1], NS_FILE
);
5233 if ( is_null( $title ) ) {
5234 # Bogus title. Ignore these so we don't bomb out later.
5238 # We need to get what handler the file uses, to figure out parameters.
5239 # Note, a hook can overide the file name, and chose an entirely different
5240 # file (which potentially could be of a different type and have different handler).
5243 Hooks
::run( 'BeforeParserFetchFileAndTitle',
5244 [ $this, $title, &$options, &$descQuery ] );
5245 # Don't register it now, as TraditionalImageGallery does that later.
5246 $file = $this->fetchFileNoRegister( $title, $options );
5247 $handler = $file ?
$file->getHandler() : false;
5250 'img_alt' => 'gallery-internal-alt',
5251 'img_link' => 'gallery-internal-link',
5254 $paramMap +
= $handler->getParamMap();
5255 // We don't want people to specify per-image widths.
5256 // Additionally the width parameter would need special casing anyhow.
5257 unset( $paramMap['img_width'] );
5260 $mwArray = $this->magicWordFactory
->newArray( array_keys( $paramMap ) );
5265 $handlerOptions = [];
5266 if ( isset( $matches[3] ) ) {
5267 // look for an |alt= definition while trying not to break existing
5268 // captions with multiple pipes (|) in it, until a more sensible grammar
5269 // is defined for images in galleries
5271 // FIXME: Doing recursiveTagParse at this stage, and the trim before
5272 // splitting on '|' is a bit odd, and different from makeImage.
5273 $matches[3] = $this->recursiveTagParse( trim( $matches[3] ) );
5274 // Protect LanguageConverter markup
5275 $parameterMatches = StringUtils
::delimiterExplode(
5276 '-{', '}-', '|', $matches[3], true /* nested */
5279 foreach ( $parameterMatches as $parameterMatch ) {
5280 list( $magicName, $match ) = $mwArray->matchVariableStartToEnd( $parameterMatch );
5282 $paramName = $paramMap[$magicName];
5284 switch ( $paramName ) {
5285 case 'gallery-internal-alt':
5286 $alt = $this->stripAltText( $match, false );
5288 case 'gallery-internal-link':
5289 $linkValue = $this->stripAltText( $match, false );
5290 if ( preg_match( '/^-{R|(.*)}-$/', $linkValue ) ) {
5291 // Result of LanguageConverter::markNoConversion
5292 // invoked on an external link.
5293 $linkValue = substr( $linkValue, 4, -2 );
5295 list( $type, $target ) = $this->parseLinkParameter( $linkValue );
5296 if ( $type === 'link-url' ) {
5298 $this->mOutput
->addExternalLink( $target );
5299 } elseif ( $type === 'link-title' ) {
5300 $link = $target->getLinkURL();
5301 $this->mOutput
->addLink( $target );
5305 // Must be a handler specific parameter.
5306 if ( $handler->validateParam( $paramName, $match ) ) {
5307 $handlerOptions[$paramName] = $match;
5309 // Guess not, consider it as caption.
5310 $this->logger
->debug(
5311 "$parameterMatch failed parameter validation" );
5312 $label = $parameterMatch;
5318 $label = $parameterMatch;
5323 $ig->add( $title, $label, $alt, $link, $handlerOptions );
5325 $html = $ig->toHTML();
5326 Hooks
::run( 'AfterParserFetchFileAndTitle', [ $this, $ig, &$html ] );
5331 * @param MediaHandler $handler
5334 public function getImageParams( $handler ) {
5336 $handlerClass = get_class( $handler );
5340 if ( !isset( $this->mImageParams
[$handlerClass] ) ) {
5341 # Initialise static lists
5342 static $internalParamNames = [
5343 'horizAlign' => [ 'left', 'right', 'center', 'none' ],
5344 'vertAlign' => [ 'baseline', 'sub', 'super', 'top', 'text-top', 'middle',
5345 'bottom', 'text-bottom' ],
5346 'frame' => [ 'thumbnail', 'manualthumb', 'framed', 'frameless',
5347 'upright', 'border', 'link', 'alt', 'class' ],
5349 static $internalParamMap;
5350 if ( !$internalParamMap ) {
5351 $internalParamMap = [];
5352 foreach ( $internalParamNames as $type => $names ) {
5353 foreach ( $names as $name ) {
5354 // For grep: img_left, img_right, img_center, img_none,
5355 // img_baseline, img_sub, img_super, img_top, img_text_top, img_middle,
5356 // img_bottom, img_text_bottom,
5357 // img_thumbnail, img_manualthumb, img_framed, img_frameless, img_upright,
5358 // img_border, img_link, img_alt, img_class
5359 $magicName = str_replace( '-', '_', "img_$name" );
5360 $internalParamMap[$magicName] = [ $type, $name ];
5365 # Add handler params
5366 $paramMap = $internalParamMap;
5368 $handlerParamMap = $handler->getParamMap();
5369 foreach ( $handlerParamMap as $magic => $paramName ) {
5370 $paramMap[$magic] = [ 'handler', $paramName ];
5373 $this->mImageParams
[$handlerClass] = $paramMap;
5374 $this->mImageParamsMagicArray
[$handlerClass] =
5375 $this->magicWordFactory
->newArray( array_keys( $paramMap ) );
5377 return [ $this->mImageParams
[$handlerClass], $this->mImageParamsMagicArray
[$handlerClass] ];
5381 * Parse image options text and use it to make an image
5383 * @param Title $title
5384 * @param string $options
5385 * @param LinkHolderArray|bool $holders
5386 * @return string HTML
5388 public function makeImage( $title, $options, $holders = false ) {
5389 # Check if the options text is of the form "options|alt text"
5391 # * thumbnail make a thumbnail with enlarge-icon and caption, alignment depends on lang
5392 # * left no resizing, just left align. label is used for alt= only
5393 # * right same, but right aligned
5394 # * none same, but not aligned
5395 # * ___px scale to ___ pixels width, no aligning. e.g. use in taxobox
5396 # * center center the image
5397 # * frame Keep original image size, no magnify-button.
5398 # * framed Same as "frame"
5399 # * frameless like 'thumb' but without a frame. Keeps user preferences for width
5400 # * upright reduce width for upright images, rounded to full __0 px
5401 # * border draw a 1px border around the image
5402 # * alt Text for HTML alt attribute (defaults to empty)
5403 # * class Set a class for img node
5404 # * link Set the target of the image link. Can be external, interwiki, or local
5405 # vertical-align values (no % or length right now):
5415 # Protect LanguageConverter markup when splitting into parts
5416 $parts = StringUtils
::delimiterExplode(
5417 '-{', '}-', '|', $options, true /* allow nesting */
5420 # Give extensions a chance to select the file revision for us
5423 Hooks
::run( 'BeforeParserFetchFileAndTitle',
5424 [ $this, $title, &$options, &$descQuery ] );
5425 # Fetch and register the file (file title may be different via hooks)
5426 list( $file, $title ) = $this->fetchFileAndTitle( $title, $options );
5429 $handler = $file ?
$file->getHandler() : false;
5431 list( $paramMap, $mwArray ) = $this->getImageParams( $handler );
5434 $this->addTrackingCategory( 'broken-file-category' );
5437 # Process the input parameters
5439 $params = [ 'frame' => [], 'handler' => [],
5440 'horizAlign' => [], 'vertAlign' => [] ];
5441 $seenformat = false;
5442 foreach ( $parts as $part ) {
5443 $part = trim( $part );
5444 list( $magicName, $value ) = $mwArray->matchVariableStartToEnd( $part );
5446 if ( isset( $paramMap[$magicName] ) ) {
5447 list( $type, $paramName ) = $paramMap[$magicName];
5449 # Special case; width and height come in one variable together
5450 if ( $type === 'handler' && $paramName === 'width' ) {
5451 $parsedWidthParam = self
::parseWidthParam( $value );
5452 if ( isset( $parsedWidthParam['width'] ) ) {
5453 $width = $parsedWidthParam['width'];
5454 if ( $handler->validateParam( 'width', $width ) ) {
5455 $params[$type]['width'] = $width;
5459 if ( isset( $parsedWidthParam['height'] ) ) {
5460 $height = $parsedWidthParam['height'];
5461 if ( $handler->validateParam( 'height', $height ) ) {
5462 $params[$type]['height'] = $height;
5466 # else no validation -- T15436
5468 if ( $type === 'handler' ) {
5469 # Validate handler parameter
5470 $validated = $handler->validateParam( $paramName, $value );
5472 # Validate internal parameters
5473 switch ( $paramName ) {
5477 # @todo FIXME: Possibly check validity here for
5478 # manualthumb? downstream behavior seems odd with
5479 # missing manual thumbs.
5481 $value = $this->stripAltText( $value, $holders );
5484 list( $paramName, $value ) =
5485 $this->parseLinkParameter(
5486 $this->stripAltText( $value, $holders )
5490 if ( $paramName === 'no-link' ) {
5493 if ( ( $paramName === 'link-url' ) && $this->mOptions
->getExternalLinkTarget() ) {
5494 $params[$type]['link-target'] = $this->mOptions
->getExternalLinkTarget();
5501 // use first appearing option, discard others.
5502 $validated = !$seenformat;
5506 # Most other things appear to be empty or numeric...
5507 $validated = ( $value === false ||
is_numeric( trim( $value ) ) );
5512 $params[$type][$paramName] = $value;
5516 if ( !$validated ) {
5521 # Process alignment parameters
5522 if ( $params['horizAlign'] ) {
5523 $params['frame']['align'] = key( $params['horizAlign'] );
5525 if ( $params['vertAlign'] ) {
5526 $params['frame']['valign'] = key( $params['vertAlign'] );
5529 $params['frame']['caption'] = $caption;
5531 # Will the image be presented in a frame, with the caption below?
5532 $imageIsFramed = isset( $params['frame']['frame'] )
5533 ||
isset( $params['frame']['framed'] )
5534 ||
isset( $params['frame']['thumbnail'] )
5535 ||
isset( $params['frame']['manualthumb'] );
5537 # In the old days, [[Image:Foo|text...]] would set alt text. Later it
5538 # came to also set the caption, ordinary text after the image -- which
5539 # makes no sense, because that just repeats the text multiple times in
5540 # screen readers. It *also* came to set the title attribute.
5541 # Now that we have an alt attribute, we should not set the alt text to
5542 # equal the caption: that's worse than useless, it just repeats the
5543 # text. This is the framed/thumbnail case. If there's no caption, we
5544 # use the unnamed parameter for alt text as well, just for the time be-
5545 # ing, if the unnamed param is set and the alt param is not.
5546 # For the future, we need to figure out if we want to tweak this more,
5547 # e.g., introducing a title= parameter for the title; ignoring the un-
5548 # named parameter entirely for images without a caption; adding an ex-
5549 # plicit caption= parameter and preserving the old magic unnamed para-
5551 if ( $imageIsFramed ) { # Framed image
5552 if ( $caption === '' && !isset( $params['frame']['alt'] ) ) {
5553 # No caption or alt text, add the filename as the alt text so
5554 # that screen readers at least get some description of the image
5555 $params['frame']['alt'] = $title->getText();
5557 # Do not set $params['frame']['title'] because tooltips don't make sense
5559 } else { # Inline image
5560 if ( !isset( $params['frame']['alt'] ) ) {
5561 # No alt text, use the "caption" for the alt text
5562 if ( $caption !== '' ) {
5563 $params['frame']['alt'] = $this->stripAltText( $caption, $holders );
5565 # No caption, fall back to using the filename for the
5567 $params['frame']['alt'] = $title->getText();
5570 # Use the "caption" for the tooltip text
5571 $params['frame']['title'] = $this->stripAltText( $caption, $holders );
5573 $params['handler']['targetlang'] = $this->getTargetLanguage()->getCode();
5575 Hooks
::run( 'ParserMakeImageParams', [ $title, $file, &$params, $this ] );
5577 # Linker does the rest
5578 $time = $options['time'] ??
false;
5579 $ret = Linker
::makeImageLink( $this, $title, $file, $params['frame'], $params['handler'],
5580 $time, $descQuery, $this->mOptions
->getThumbSize() );
5582 # Give the handler a chance to modify the parser object
5584 $handler->parserTransformHook( $this, $file );
5591 * Parse the value of 'link' parameter in image syntax (`[[File:Foo.jpg|link=<value>]]`).
5593 * Adds an entry to appropriate link tables.
5596 * @param string $value
5597 * @return array of `[ type, target ]`, where:
5598 * - `type` is one of:
5599 * - `null`: Given value is not a valid link target, use default
5600 * - `'no-link'`: Given value is empty, do not generate a link
5601 * - `'link-url'`: Given value is a valid external link
5602 * - `'link-title'`: Given value is a valid internal link
5604 * - When `type` is `null` or `'no-link'`: `false`
5605 * - When `type` is `'link-url'`: URL string corresponding to given value
5606 * - When `type` is `'link-title'`: Title object corresponding to given value
5608 public function parseLinkParameter( $value ) {
5609 $chars = self
::EXT_LINK_URL_CLASS
;
5610 $addr = self
::EXT_LINK_ADDR
;
5611 $prots = $this->mUrlProtocols
;
5614 if ( $value === '' ) {
5616 } elseif ( preg_match( "/^((?i)$prots)/", $value ) ) {
5617 if ( preg_match( "/^((?i)$prots)$addr$chars*$/u", $value, $m ) ) {
5618 $this->mOutput
->addExternalLink( $value );
5623 $linkTitle = Title
::newFromText( $value );
5625 $this->mOutput
->addLink( $linkTitle );
5626 $type = 'link-title';
5627 $target = $linkTitle;
5630 return [ $type, $target ];
5634 * @param string $caption
5635 * @param LinkHolderArray|bool $holders
5636 * @return mixed|string
5638 protected function stripAltText( $caption, $holders ) {
5639 # Strip bad stuff out of the title (tooltip). We can't just use
5640 # replaceLinkHoldersText() here, because if this function is called
5641 # from replaceInternalLinks2(), mLinkHolders won't be up-to-date.
5643 $tooltip = $holders->replaceText( $caption );
5645 $tooltip = $this->replaceLinkHoldersText( $caption );
5648 # make sure there are no placeholders in thumbnail attributes
5649 # that are later expanded to html- so expand them now and
5651 $tooltip = $this->mStripState
->unstripBoth( $tooltip );
5652 # Compatibility hack! In HTML certain entity references not terminated
5653 # by a semicolon are decoded (but not if we're in an attribute; that's
5654 # how link URLs get away without properly escaping & in queries).
5655 # But wikitext has always required semicolon-termination of entities,
5656 # so encode & where needed to avoid decode of semicolon-less entities.
5658 # https://www.w3.org/TR/html5/syntax.html#named-character-references
5659 # T210437 discusses moving this workaround to Sanitizer::stripAllTags.
5660 $tooltip = preg_replace( "/
5661 & # 1. entity prefix
5662 (?= # 2. followed by:
5663 (?: # a. one of the legacy semicolon-less named entities
5664 A(?:Elig|MP|acute|circ|grave|ring|tilde|uml)|
5665 C(?:OPY|cedil)|E(?:TH|acute|circ|grave|uml)|
5666 GT|I(?:acute|circ|grave|uml)|LT|Ntilde|
5667 O(?:acute|circ|grave|slash|tilde|uml)|QUOT|REG|THORN|
5668 U(?:acute|circ|grave|uml)|Yacute|
5669 a(?:acute|c(?:irc|ute)|elig|grave|mp|ring|tilde|uml)|brvbar|
5670 c(?:cedil|edil|urren)|cent(?!erdot;)|copy(?!sr;)|deg|
5671 divide(?!ontimes;)|e(?:acute|circ|grave|th|uml)|
5672 frac(?:1(?:2|4)|34)|
5673 gt(?!c(?:c|ir)|dot|lPar|quest|r(?:a(?:pprox|rr)|dot|eq(?:less|qless)|less|sim);)|
5674 i(?:acute|circ|excl|grave|quest|uml)|laquo|
5675 lt(?!c(?:c|ir)|dot|hree|imes|larr|quest|r(?:Par|i(?:e|f|));)|
5676 m(?:acr|i(?:cro|ddot))|n(?:bsp|tilde)|
5677 not(?!in(?:E|dot|v(?:a|b|c)|)|ni(?:v(?:a|b|c)|);)|
5678 o(?:acute|circ|grave|rd(?:f|m)|slash|tilde|uml)|
5679 p(?:lusmn|ound)|para(?!llel;)|quot|r(?:aquo|eg)|
5680 s(?:ect|hy|up(?:1|2|3)|zlig)|thorn|times(?!b(?:ar|)|d;)|
5681 u(?:acute|circ|grave|ml|uml)|y(?:acute|en|uml)
5683 (?:[^;]|$)) # b. and not followed by a semicolon
5684 # S = study, for efficiency
5685 /Sx", '&', $tooltip );
5686 $tooltip = Sanitizer
::stripAllTags( $tooltip );
5692 * Set a flag in the output object indicating that the content is dynamic and
5693 * shouldn't be cached.
5694 * @deprecated since 1.28; use getOutput()->updateCacheExpiry()
5696 public function disableCache() {
5697 $this->logger
->debug( "Parser output marked as uncacheable." );
5698 if ( !$this->mOutput
) {
5699 throw new MWException( __METHOD__
.
5700 " can only be called when actually parsing something" );
5702 $this->mOutput
->updateCacheExpiry( 0 ); // new style, for consistency
5706 * Callback from the Sanitizer for expanding items found in HTML attribute
5707 * values, so they can be safely tested and escaped.
5709 * @param string &$text
5710 * @param bool|PPFrame $frame
5713 public function attributeStripCallback( &$text, $frame = false ) {
5714 $text = $this->replaceVariables( $text, $frame );
5715 $text = $this->mStripState
->unstripBoth( $text );
5724 public function getTags() {
5725 $this->firstCallInit();
5727 array_keys( $this->mTransparentTagHooks
),
5728 array_keys( $this->mTagHooks
),
5729 array_keys( $this->mFunctionTagHooks
)
5737 public function getFunctionSynonyms() {
5738 $this->firstCallInit();
5739 return $this->mFunctionSynonyms
;
5746 public function getUrlProtocols() {
5747 return $this->mUrlProtocols
;
5751 * Replace transparent tags in $text with the values given by the callbacks.
5753 * Transparent tag hooks are like regular XML-style tag hooks, except they
5754 * operate late in the transformation sequence, on HTML instead of wikitext.
5756 * @param string $text
5760 public function replaceTransparentTags( $text ) {
5762 $elements = array_keys( $this->mTransparentTagHooks
);
5763 $text = self
::extractTagsAndParams( $elements, $text, $matches );
5766 foreach ( $matches as $marker => $data ) {
5767 list( $element, $content, $params, $tag ) = $data;
5768 $tagName = strtolower( $element );
5769 if ( isset( $this->mTransparentTagHooks
[$tagName] ) ) {
5770 $output = call_user_func_array(
5771 $this->mTransparentTagHooks
[$tagName],
5772 [ $content, $params, $this ]
5777 $replacements[$marker] = $output;
5779 return strtr( $text, $replacements );
5783 * Break wikitext input into sections, and either pull or replace
5784 * some particular section's text.
5786 * External callers should use the getSection and replaceSection methods.
5788 * @param string $text Page wikitext
5789 * @param string|int $sectionId A section identifier string of the form:
5790 * "<flag1> - <flag2> - ... - <section number>"
5792 * Currently the only recognised flag is "T", which means the target section number
5793 * was derived during a template inclusion parse, in other words this is a template
5794 * section edit link. If no flags are given, it was an ordinary section edit link.
5795 * This flag is required to avoid a section numbering mismatch when a section is
5796 * enclosed by "<includeonly>" (T8563).
5798 * The section number 0 pulls the text before the first heading; other numbers will
5799 * pull the given section along with its lower-level subsections. If the section is
5800 * not found, $mode=get will return $newtext, and $mode=replace will return $text.
5802 * Section 0 is always considered to exist, even if it only contains the empty
5803 * string. If $text is the empty string and section 0 is replaced, $newText is
5806 * @param string $mode One of "get" or "replace"
5807 * @param string $newText Replacement text for section data.
5808 * @return string For "get", the extracted section text.
5809 * for "replace", the whole page with the section replaced.
5811 private function extractSections( $text, $sectionId, $mode, $newText = '' ) {
5812 global $wgTitle; # not generally used but removes an ugly failure mode
5814 $magicScopeVariable = $this->lock();
5815 $this->startParse( $wgTitle, new ParserOptions
, self
::OT_PLAIN
, true );
5817 $frame = $this->getPreprocessor()->newFrame();
5819 # Process section extraction flags
5821 $sectionParts = explode( '-', $sectionId );
5822 $sectionIndex = array_pop( $sectionParts );
5823 foreach ( $sectionParts as $part ) {
5824 if ( $part === 'T' ) {
5825 $flags |
= self
::PTD_FOR_INCLUSION
;
5829 # Check for empty input
5830 if ( strval( $text ) === '' ) {
5831 # Only sections 0 and T-0 exist in an empty document
5832 if ( $sectionIndex == 0 ) {
5833 if ( $mode === 'get' ) {
5839 if ( $mode === 'get' ) {
5847 # Preprocess the text
5848 $root = $this->preprocessToDom( $text, $flags );
5850 # <h> nodes indicate section breaks
5851 # They can only occur at the top level, so we can find them by iterating the root's children
5852 $node = $root->getFirstChild();
5854 # Find the target section
5855 if ( $sectionIndex == 0 ) {
5856 # Section zero doesn't nest, level=big
5857 $targetLevel = 1000;
5860 if ( $node->getName() === 'h' ) {
5861 $bits = $node->splitHeading();
5862 if ( $bits['i'] == $sectionIndex ) {
5863 $targetLevel = $bits['level'];
5867 if ( $mode === 'replace' ) {
5868 $outText .= $frame->expand( $node, PPFrame
::RECOVER_ORIG
);
5870 $node = $node->getNextSibling();
5876 if ( $mode === 'get' ) {
5883 # Find the end of the section, including nested sections
5885 if ( $node->getName() === 'h' ) {
5886 $bits = $node->splitHeading();
5887 $curLevel = $bits['level'];
5888 if ( $bits['i'] != $sectionIndex && $curLevel <= $targetLevel ) {
5892 if ( $mode === 'get' ) {
5893 $outText .= $frame->expand( $node, PPFrame
::RECOVER_ORIG
);
5895 $node = $node->getNextSibling();
5898 # Write out the remainder (in replace mode only)
5899 if ( $mode === 'replace' ) {
5900 # Output the replacement text
5901 # Add two newlines on -- trailing whitespace in $newText is conventionally
5902 # stripped by the editor, so we need both newlines to restore the paragraph gap
5903 # Only add trailing whitespace if there is newText
5904 if ( $newText != "" ) {
5905 $outText .= $newText . "\n\n";
5909 $outText .= $frame->expand( $node, PPFrame
::RECOVER_ORIG
);
5910 $node = $node->getNextSibling();
5914 if ( is_string( $outText ) ) {
5915 # Re-insert stripped tags
5916 $outText = rtrim( $this->mStripState
->unstripBoth( $outText ) );
5923 * This function returns the text of a section, specified by a number ($section).
5924 * A section is text under a heading like == Heading == or \<h1\>Heading\</h1\>, or
5925 * the first section before any such heading (section 0).
5927 * If a section contains subsections, these are also returned.
5929 * @param string $text Text to look in
5930 * @param string|int $sectionId Section identifier as a number or string
5931 * (e.g. 0, 1 or 'T-1').
5932 * @param string $defaultText Default to return if section is not found
5934 * @return string Text of the requested section
5936 public function getSection( $text, $sectionId, $defaultText = '' ) {
5937 return $this->extractSections( $text, $sectionId, 'get', $defaultText );
5941 * This function returns $oldtext after the content of the section
5942 * specified by $section has been replaced with $text. If the target
5943 * section does not exist, $oldtext is returned unchanged.
5945 * @param string $oldText Former text of the article
5946 * @param string|int $sectionId Section identifier as a number or string
5947 * (e.g. 0, 1 or 'T-1').
5948 * @param string $newText Replacing text
5950 * @return string Modified text
5952 public function replaceSection( $oldText, $sectionId, $newText ) {
5953 return $this->extractSections( $oldText, $sectionId, 'replace', $newText );
5957 * Get the ID of the revision we are parsing
5959 * The return value will be either:
5960 * - a) Positive, indicating a specific revision ID (current or old)
5961 * - b) Zero, meaning the revision ID is specified by getCurrentRevisionCallback()
5962 * - c) Null, meaning the parse is for preview mode and there is no revision
5966 public function getRevisionId() {
5967 return $this->mRevisionId
;
5971 * Get the revision object for $this->mRevisionId
5973 * @return Revision|null Either a Revision object or null
5974 * @since 1.23 (public since 1.23)
5976 public function getRevisionObject() {
5977 if ( $this->mRevisionObject
) {
5978 return $this->mRevisionObject
;
5981 // NOTE: try to get the RevisionObject even if mRevisionId is null.
5982 // This is useful when parsing a revision that has not yet been saved.
5983 // However, if we get back a saved revision even though we are in
5984 // preview mode, we'll have to ignore it, see below.
5985 // NOTE: This callback may be used to inject an OLD revision that was
5986 // already loaded, so "current" is a bit of a misnomer. We can't just
5987 // skip it if mRevisionId is set.
5988 $rev = call_user_func(
5989 $this->mOptions
->getCurrentRevisionCallback(),
5994 if ( $this->mRevisionId
=== null && $rev && $rev->getId() ) {
5995 // We are in preview mode (mRevisionId is null), and the current revision callback
5996 // returned an existing revision. Ignore it and return null, it's probably the page's
5997 // current revision, which is not what we want here. Note that we do want to call the
5998 // callback to allow the unsaved revision to be injected here, e.g. for
5999 // self-transclusion previews.
6003 // If the parse is for a new revision, then the callback should have
6004 // already been set to force the object and should match mRevisionId.
6005 // If not, try to fetch by mRevisionId for sanity.
6006 if ( $this->mRevisionId
&& $rev && $rev->getId() != $this->mRevisionId
) {
6007 $rev = Revision
::newFromId( $this->mRevisionId
);
6010 $this->mRevisionObject
= $rev;
6012 return $this->mRevisionObject
;
6016 * Get the timestamp associated with the current revision, adjusted for
6017 * the default server-local timestamp
6018 * @return string TS_MW timestamp
6020 public function getRevisionTimestamp() {
6021 if ( $this->mRevisionTimestamp
!== null ) {
6022 return $this->mRevisionTimestamp
;
6025 # Use specified revision timestamp, falling back to the current timestamp
6026 $revObject = $this->getRevisionObject();
6027 $timestamp = $revObject ?
$revObject->getTimestamp() : $this->mOptions
->getTimestamp();
6028 $this->mOutput
->setRevisionTimestampUsed( $timestamp ); // unadjusted time zone
6030 # The cryptic '' timezone parameter tells to use the site-default
6031 # timezone offset instead of the user settings.
6032 # Since this value will be saved into the parser cache, served
6033 # to other users, and potentially even used inside links and such,
6034 # it needs to be consistent for all visitors.
6035 $this->mRevisionTimestamp
= $this->contLang
->userAdjust( $timestamp, '' );
6037 return $this->mRevisionTimestamp
;
6041 * Get the name of the user that edited the last revision
6043 * @return string User name
6045 public function getRevisionUser() {
6046 if ( is_null( $this->mRevisionUser
) ) {
6047 $revObject = $this->getRevisionObject();
6049 # if this template is subst: the revision id will be blank,
6050 # so just use the current user's name
6052 $this->mRevisionUser
= $revObject->getUserText();
6053 } elseif ( $this->ot
['wiki'] ||
$this->mOptions
->getIsPreview() ) {
6054 $this->mRevisionUser
= $this->getUser()->getName();
6057 return $this->mRevisionUser
;
6061 * Get the size of the revision
6063 * @return int|null Revision size
6065 public function getRevisionSize() {
6066 if ( is_null( $this->mRevisionSize
) ) {
6067 $revObject = $this->getRevisionObject();
6069 # if this variable is subst: the revision id will be blank,
6070 # so just use the parser input size, because the own substituation
6071 # will change the size.
6073 $this->mRevisionSize
= $revObject->getSize();
6075 $this->mRevisionSize
= $this->mInputSize
;
6078 return $this->mRevisionSize
;
6082 * Mutator for $mDefaultSort
6084 * @param string $sort New value
6086 public function setDefaultSort( $sort ) {
6087 $this->mDefaultSort
= $sort;
6088 $this->mOutput
->setProperty( 'defaultsort', $sort );
6092 * Accessor for $mDefaultSort
6093 * Will use the empty string if none is set.
6095 * This value is treated as a prefix, so the
6096 * empty string is equivalent to sorting by
6101 public function getDefaultSort() {
6102 if ( $this->mDefaultSort
!== false ) {
6103 return $this->mDefaultSort
;
6110 * Accessor for $mDefaultSort
6111 * Unlike getDefaultSort(), will return false if none is set
6113 * @return string|bool
6115 public function getCustomDefaultSort() {
6116 return $this->mDefaultSort
;
6119 private static function getSectionNameFromStrippedText( $text ) {
6120 $text = Sanitizer
::normalizeSectionNameWhitespace( $text );
6121 $text = Sanitizer
::decodeCharReferences( $text );
6122 $text = self
::normalizeSectionName( $text );
6126 private static function makeAnchor( $sectionName ) {
6127 return '#' . Sanitizer
::escapeIdForLink( $sectionName );
6130 private function makeLegacyAnchor( $sectionName ) {
6131 $fragmentMode = $this->svcOptions
->get( 'FragmentMode' );
6132 if ( isset( $fragmentMode[1] ) && $fragmentMode[1] === 'legacy' ) {
6133 // ForAttribute() and ForLink() are the same for legacy encoding
6134 $id = Sanitizer
::escapeIdForAttribute( $sectionName, Sanitizer
::ID_FALLBACK
);
6136 $id = Sanitizer
::escapeIdForLink( $sectionName );
6143 * Try to guess the section anchor name based on a wikitext fragment
6144 * presumably extracted from a heading, for example "Header" from
6147 * @param string $text
6148 * @return string Anchor (starting with '#')
6150 public function guessSectionNameFromWikiText( $text ) {
6151 # Strip out wikitext links(they break the anchor)
6152 $text = $this->stripSectionName( $text );
6153 $sectionName = self
::getSectionNameFromStrippedText( $text );
6154 return self
::makeAnchor( $sectionName );
6158 * Same as guessSectionNameFromWikiText(), but produces legacy anchors
6159 * instead, if possible. For use in redirects, since various versions
6160 * of Microsoft browsers interpret Location: headers as something other
6161 * than UTF-8, resulting in breakage.
6163 * @param string $text The section name
6164 * @return string Anchor (starting with '#')
6166 public function guessLegacySectionNameFromWikiText( $text ) {
6167 # Strip out wikitext links(they break the anchor)
6168 $text = $this->stripSectionName( $text );
6169 $sectionName = self
::getSectionNameFromStrippedText( $text );
6170 return $this->makeLegacyAnchor( $sectionName );
6174 * Like guessSectionNameFromWikiText(), but takes already-stripped text as input.
6175 * @param string $text Section name (plain text)
6176 * @return string Anchor (starting with '#')
6178 public static function guessSectionNameFromStrippedText( $text ) {
6179 $sectionName = self
::getSectionNameFromStrippedText( $text );
6180 return self
::makeAnchor( $sectionName );
6184 * Apply the same normalization as code making links to this section would
6186 * @param string $text
6189 private static function normalizeSectionName( $text ) {
6190 # T90902: ensure the same normalization is applied for IDs as to links
6191 $titleParser = MediaWikiServices
::getInstance()->getTitleParser();
6194 $parts = $titleParser->splitTitleString( "#$text" );
6195 } catch ( MalformedTitleException
$ex ) {
6198 return $parts['fragment'];
6202 * Strips a text string of wikitext for use in a section anchor
6204 * Accepts a text string and then removes all wikitext from the
6205 * string and leaves only the resultant text (i.e. the result of
6206 * [[User:WikiSysop|Sysop]] would be "Sysop" and the result of
6207 * [[User:WikiSysop]] would be "User:WikiSysop") - this is intended
6208 * to create valid section anchors by mimicing the output of the
6209 * parser when headings are parsed.
6211 * @param string $text Text string to be stripped of wikitext
6212 * for use in a Section anchor
6213 * @return string Filtered text string
6215 public function stripSectionName( $text ) {
6216 # Strip internal link markup
6217 $text = preg_replace( '/\[\[:?([^[|]+)\|([^[]+)\]\]/', '$2', $text );
6218 $text = preg_replace( '/\[\[:?([^[]+)\|?\]\]/', '$1', $text );
6220 # Strip external link markup
6221 # @todo FIXME: Not tolerant to blank link text
6222 # I.E. [https://www.mediawiki.org] will render as [1] or something depending
6223 # on how many empty links there are on the page - need to figure that out.
6224 $text = preg_replace( '/\[(?i:' . $this->mUrlProtocols
. ')([^ ]+?) ([^[]+)\]/', '$2', $text );
6226 # Parse wikitext quotes (italics & bold)
6227 $text = $this->doQuotes( $text );
6230 $text = StringUtils
::delimiterReplace( '<', '>', '', $text );
6235 * strip/replaceVariables/unstrip for preprocessor regression testing
6237 * @param string $text
6238 * @param Title $title
6239 * @param ParserOptions $options
6240 * @param int $outputType
6244 public function testSrvus( $text, Title
$title, ParserOptions
$options,
6245 $outputType = self
::OT_HTML
6247 $magicScopeVariable = $this->lock();
6248 $this->startParse( $title, $options, $outputType, true );
6250 $text = $this->replaceVariables( $text );
6251 $text = $this->mStripState
->unstripBoth( $text );
6252 $text = Sanitizer
::removeHTMLtags( $text );
6257 * @param string $text
6258 * @param Title $title
6259 * @param ParserOptions $options
6262 public function testPst( $text, Title
$title, ParserOptions
$options ) {
6263 return $this->preSaveTransform( $text, $title, $options->getUser(), $options );
6267 * @param string $text
6268 * @param Title $title
6269 * @param ParserOptions $options
6272 public function testPreprocess( $text, Title
$title, ParserOptions
$options ) {
6273 return $this->testSrvus( $text, $title, $options, self
::OT_PREPROCESS
);
6277 * Call a callback function on all regions of the given text that are not
6278 * inside strip markers, and replace those regions with the return value
6279 * of the callback. For example, with input:
6283 * This will call the callback function twice, with 'aaa' and 'bbb'. Those
6284 * two strings will be replaced with the value returned by the callback in
6288 * @param callable $callback
6292 public function markerSkipCallback( $s, $callback ) {
6295 while ( $i < strlen( $s ) ) {
6296 $markerStart = strpos( $s, self
::MARKER_PREFIX
, $i );
6297 if ( $markerStart === false ) {
6298 $out .= call_user_func( $callback, substr( $s, $i ) );
6301 $out .= call_user_func( $callback, substr( $s, $i, $markerStart - $i ) );
6302 $markerEnd = strpos( $s, self
::MARKER_SUFFIX
, $markerStart );
6303 if ( $markerEnd === false ) {
6304 $out .= substr( $s, $markerStart );
6307 $markerEnd +
= strlen( self
::MARKER_SUFFIX
);
6308 $out .= substr( $s, $markerStart, $markerEnd - $markerStart );
6317 * Remove any strip markers found in the given text.
6319 * @param string $text
6322 public function killMarkers( $text ) {
6323 return $this->mStripState
->killMarkers( $text );
6327 * Save the parser state required to convert the given half-parsed text to
6328 * HTML. "Half-parsed" in this context means the output of
6329 * recursiveTagParse() or internalParse(). This output has strip markers
6330 * from replaceVariables (extensionSubstitution() etc.), and link
6331 * placeholders from replaceLinkHolders().
6333 * Returns an array which can be serialized and stored persistently. This
6334 * array can later be loaded into another parser instance with
6335 * unserializeHalfParsedText(). The text can then be safely incorporated into
6336 * the return value of a parser hook.
6338 * @deprecated since 1.31
6339 * @param string $text
6343 public function serializeHalfParsedText( $text ) {
6344 wfDeprecated( __METHOD__
, '1.31' );
6347 'version' => self
::HALF_PARSED_VERSION
,
6348 'stripState' => $this->mStripState
->getSubState( $text ),
6349 'linkHolders' => $this->mLinkHolders
->getSubArray( $text )
6355 * Load the parser state given in the $data array, which is assumed to
6356 * have been generated by serializeHalfParsedText(). The text contents is
6357 * extracted from the array, and its markers are transformed into markers
6358 * appropriate for the current Parser instance. This transformed text is
6359 * returned, and can be safely included in the return value of a parser
6362 * If the $data array has been stored persistently, the caller should first
6363 * check whether it is still valid, by calling isValidHalfParsedText().
6365 * @deprecated since 1.31
6366 * @param array $data Serialized data
6367 * @throws MWException
6370 public function unserializeHalfParsedText( $data ) {
6371 wfDeprecated( __METHOD__
, '1.31' );
6372 if ( !isset( $data['version'] ) ||
$data['version'] != self
::HALF_PARSED_VERSION
) {
6373 throw new MWException( __METHOD__
. ': invalid version' );
6376 # First, extract the strip state.
6377 $texts = [ $data['text'] ];
6378 $texts = $this->mStripState
->merge( $data['stripState'], $texts );
6380 # Now renumber links
6381 $texts = $this->mLinkHolders
->mergeForeign( $data['linkHolders'], $texts );
6383 # Should be good to go.
6388 * Returns true if the given array, presumed to be generated by
6389 * serializeHalfParsedText(), is compatible with the current version of the
6392 * @deprecated since 1.31
6393 * @param array $data
6397 public function isValidHalfParsedText( $data ) {
6398 wfDeprecated( __METHOD__
, '1.31' );
6399 return isset( $data['version'] ) && $data['version'] == self
::HALF_PARSED_VERSION
;
6403 * Parsed a width param of imagelink like 300px or 200x300px
6405 * @param string $value
6406 * @param bool $parseHeight
6411 public static function parseWidthParam( $value, $parseHeight = true ) {
6412 $parsedWidthParam = [];
6413 if ( $value === '' ) {
6414 return $parsedWidthParam;
6417 # (T15500) In both cases (width/height and width only),
6418 # permit trailing "px" for backward compatibility.
6419 if ( $parseHeight && preg_match( '/^([0-9]*)x([0-9]*)\s*(?:px)?\s*$/', $value, $m ) ) {
6420 $width = intval( $m[1] );
6421 $height = intval( $m[2] );
6422 $parsedWidthParam['width'] = $width;
6423 $parsedWidthParam['height'] = $height;
6424 } elseif ( preg_match( '/^[0-9]*\s*(?:px)?\s*$/', $value ) ) {
6425 $width = intval( $value );
6426 $parsedWidthParam['width'] = $width;
6428 return $parsedWidthParam;
6432 * Lock the current instance of the parser.
6434 * This is meant to stop someone from calling the parser
6435 * recursively and messing up all the strip state.
6437 * @throws MWException If parser is in a parse
6438 * @return ScopedCallback The lock will be released once the return value goes out of scope.
6440 protected function lock() {
6441 if ( $this->mInParse
) {
6442 throw new MWException( "Parser state cleared while parsing. "
6443 . "Did you call Parser::parse recursively? Lock is held by: " . $this->mInParse
);
6446 // Save the backtrace when locking, so that if some code tries locking again,
6447 // we can print the lock owner's backtrace for easier debugging
6449 $this->mInParse
= $e->getTraceAsString();
6451 $recursiveCheck = new ScopedCallback( function () {
6452 $this->mInParse
= false;
6455 return $recursiveCheck;
6459 * Strip outer <p></p> tag from the HTML source of a single paragraph.
6461 * Returns original HTML if the <p/> tag has any attributes, if there's no wrapping <p/> tag,
6462 * or if there is more than one <p/> tag in the input HTML.
6464 * @param string $html
6468 public static function stripOuterParagraph( $html ) {
6470 if ( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $html, $m ) && strpos( $m[1], '</p>' ) === false ) {
6478 * Return this parser if it is not doing anything, otherwise
6479 * get a fresh parser. You can use this method by doing
6480 * $newParser = $oldParser->getFreshParser(), or more simply
6481 * $oldParser->getFreshParser()->parse( ... );
6482 * if you're unsure if $oldParser is safe to use.
6485 * @return Parser A parser object that is not parsing anything
6487 public function getFreshParser() {
6488 if ( $this->mInParse
) {
6489 return $this->factory
->create();
6496 * Set's up the PHP implementation of OOUI for use in this request
6497 * and instructs OutputPage to enable OOUI for itself.
6501 public function enableOOUI() {
6502 OutputPage
::setupOOUI();
6503 $this->mOutput
->setEnableOOUI( true );
6507 * @param string $flag
6508 * @param string $reason
6510 protected function setOutputFlag( $flag, $reason ) {
6511 $this->mOutput
->setFlag( $flag );
6512 $name = $this->mTitle
->getPrefixedText();
6513 $this->logger
->debug( __METHOD__
. ": set $flag flag on '$name'; $reason" );