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\Config\ServiceOptions
;
24 use MediaWiki\Linker\LinkRenderer
;
25 use MediaWiki\Linker\LinkRendererFactory
;
26 use MediaWiki\Linker\LinkTarget
;
27 use MediaWiki\MediaWikiServices
;
28 use MediaWiki\Special\SpecialPageFactory
;
29 use Psr\Log\NullLogger
;
30 use Wikimedia\ScopedCallback
;
31 use Psr\Log\LoggerInterface
;
34 * @defgroup Parser Parser
38 * PHP Parser - Processes wiki markup (which uses a more user-friendly
39 * syntax, such as "[[link]]" for making links), and provides a one-way
40 * transformation of that wiki markup it into (X)HTML output / markup
41 * (which in turn the browser understands, and can display).
43 * There are seven main entry points into the Parser class:
46 * produces HTML output
47 * - Parser::preSaveTransform()
48 * produces altered wiki markup
49 * - Parser::preprocess()
50 * removes HTML comments and expands templates
51 * - Parser::cleanSig() and Parser::cleanSigInSig()
52 * cleans a signature before saving it to preferences
53 * - Parser::getSection()
54 * return the content of a section from an article for section editing
55 * - Parser::replaceSection()
56 * replaces a section by number inside an article
57 * - Parser::getPreloadText()
58 * removes <noinclude> sections and <includeonly> tags
60 * @warning $wgUser or $wgTitle or $wgRequest or $wgLang. Keep them away!
63 * $wgNamespacesWithSubpages
65 * @par Settings only within ParserOptions:
66 * $wgAllowExternalImages
67 * $wgAllowSpecialInclusion
75 * Update this version number when the ParserOutput format
76 * changes in an incompatible way, so the parser cache
77 * can automatically discard old data.
79 const VERSION
= '1.6.4';
82 * Update this version number when the output of serialiseHalfParsedText()
83 * changes in an incompatible way
85 const HALF_PARSED_VERSION
= 2;
87 # Flags for Parser::setFunctionHook
88 const SFH_NO_HASH
= 1;
89 const SFH_OBJECT_ARGS
= 2;
91 # Constants needed for external link processing
92 # Everything except bracket, space, or control characters
93 # \p{Zs} is unicode 'separator, space' category. It covers the space 0x20
94 # as well as U+3000 is IDEOGRAPHIC SPACE for T21052
95 # \x{FFFD} is the Unicode replacement character, which Preprocessor_DOM
96 # uses to replace invalid HTML characters.
97 const EXT_LINK_URL_CLASS
= '[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}]';
98 # Simplified expression to match an IPv4 or IPv6 address, or
99 # at least one character of a host name (embeds EXT_LINK_URL_CLASS)
100 const EXT_LINK_ADDR
= '(?:[0-9.]+|\\[(?i:[0-9a-f:.]+)\\]|[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}])';
101 # RegExp to make image URLs (embeds IPv6 part of EXT_LINK_ADDR)
102 // phpcs:ignore Generic.Files.LineLength
103 const EXT_IMAGE_REGEX
= '/^(http:\/\/|https:\/\/)((?:\\[(?i:[0-9a-f:.]+)\\])?[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}]+)
104 \\/([A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]+)\\.((?i)gif|png|jpg|jpeg)$/Sxu';
106 # Regular expression for a non-newline space
107 const SPACE_NOT_NL
= '(?:\t| |&\#0*160;|&\#[Xx]0*[Aa]0;|\p{Zs})';
109 # Flags for preprocessToDom
110 const PTD_FOR_INCLUSION
= 1;
112 # Allowed values for $this->mOutputType
113 # Parameter to startExternalParse().
114 const OT_HTML
= 1; # like parse()
115 const OT_WIKI
= 2; # like preSaveTransform()
116 const OT_PREPROCESS
= 3; # like preprocess()
118 const OT_PLAIN
= 4; # like extractSections() - portions of the original are returned unchanged.
121 * @var string Prefix and suffix for temporary replacement strings
122 * for the multipass parser.
124 * \x7f should never appear in input as it's disallowed in XML.
125 * Using it at the front also gives us a little extra robustness
126 * since it shouldn't match when butted up against identifier-like
129 * Must not consist of all title characters, or else it will change
130 * the behavior of <nowiki> in a link.
132 * Must have a character that needs escaping in attributes, otherwise
133 * someone could put a strip marker in an attribute, to get around
134 * escaping quote marks, and break out of the attribute. Thus we add
137 const MARKER_SUFFIX
= "-QINU`\"'\x7f";
138 const MARKER_PREFIX
= "\x7f'\"`UNIQ-";
140 # Markers used for wrapping the table of contents
141 const TOC_START
= '<mw:toc>';
142 const TOC_END
= '</mw:toc>';
144 /** @var int Assume that no output will later be saved this many seconds after parsing */
148 public $mTagHooks = [];
149 public $mTransparentTagHooks = [];
150 public $mFunctionHooks = [];
151 public $mFunctionSynonyms = [ 0 => [], 1 => [] ];
152 public $mFunctionTagHooks = [];
153 public $mStripList = [];
154 public $mDefaultStripList = [];
155 public $mVarCache = [];
156 public $mImageParams = [];
157 public $mImageParamsMagicArray = [];
158 public $mMarkerIndex = 0;
160 * @var bool Whether firstCallInit still needs to be called
162 public $mFirstCall = true;
164 # Initialised by initialiseVariables()
167 * @var MagicWordArray
172 * @var MagicWordArray
177 * @deprecated since 1.34, there should be no need to use this
182 # Initialised in constructor
183 public $mExtLinkBracketedRegex, $mUrlProtocols;
185 # Initialized in getPreprocessor()
186 /** @var Preprocessor */
187 public $mPreprocessor;
189 # Cleared with clearState():
201 public $mIncludeCount;
203 * @var LinkHolderArray
205 public $mLinkHolders;
208 public $mIncludeSizes, $mPPNodeCount, $mGeneratedPPNodeCount, $mHighestExpansionDepth;
209 public $mDefaultSort;
210 public $mTplRedirCache, $mHeadings, $mDoubleUnderscores;
211 public $mExpensiveFunctionCount; # number of expensive parser function calls
212 public $mShowToc, $mForceTocPosition;
214 public $mTplDomCache;
219 public $mUser; # User object; only used when doing pre-save transform
222 # These are variables reset at least once per parse regardless of $clearState
232 public $mTitle; # Title context, used for self-link rendering and similar things
233 public $mOutputType; # Output type, one of the OT_xxx constants
234 public $ot; # Shortcut alias, see setOutputType()
235 public $mRevisionObject; # The revision object of the specified revision ID
236 public $mRevisionId; # ID to display in {{REVISIONID}} tags
237 public $mRevisionTimestamp; # The timestamp of the specified revision ID
238 public $mRevisionUser; # User to display in {{REVISIONUSER}} tag
239 public $mRevisionSize; # Size to display in {{REVISIONSIZE}} variable
240 public $mRevIdForTs; # The revision ID which was used to fetch the timestamp
241 public $mInputSize = false; # For {{PAGESIZE}} on current page.
244 * @var array Array with the language name of each language link (i.e. the
245 * interwiki prefix) in the key, value arbitrary. Used to avoid sending
246 * duplicate language links to the ParserOutput.
248 public $mLangLinkLanguages;
251 * @var MapCacheLRU|null
254 * A cache of the current revisions of titles. Keys are $title->getPrefixedDbKey()
256 public $currentRevisionCache;
259 * @var bool|string Recursive call protection.
260 * This variable should be treated as if it were private.
262 public $mInParse = false;
264 /** @var SectionProfiler */
265 protected $mProfiler;
270 protected $mLinkRenderer;
272 /** @var MagicWordFactory */
273 private $magicWordFactory;
278 /** @var ParserFactory */
281 /** @var SpecialPageFactory */
282 private $specialPageFactory;
285 * This is called $svcOptions instead of $options like elsewhere to avoid confusion with
286 * $mOptions, which is public and widely used, and also with the local variable $options used
287 * for ParserOptions throughout this file.
289 * @var ServiceOptions
293 /** @var LinkRendererFactory */
294 private $linkRendererFactory;
296 /** @var NamespaceInfo */
299 /** @var LoggerInterface */
303 * TODO Make this a const when HHVM support is dropped (T192166)
308 public static $constructorOptions = [
309 // See $wgParserConf documentation
312 // See documentation for the corresponding config options
314 'EnableScaryTranscluding',
315 'ExtraInterlanguageLinkPrefixes',
327 'TranscludeCacheExpiry',
331 * Constructing parsers directly is deprecated! Use a ParserFactory.
333 * @param ServiceOptions|null $svcOptions
334 * @param MagicWordFactory|null $magicWordFactory
335 * @param Language|null $contLang Content language
336 * @param ParserFactory|null $factory
337 * @param string|null $urlProtocols As returned from wfUrlProtocols()
338 * @param SpecialPageFactory|null $spFactory
339 * @param LinkRendererFactory|null $linkRendererFactory
340 * @param NamespaceInfo|null $nsInfo
341 * @param LoggerInterface|null $logger
343 public function __construct(
345 MagicWordFactory
$magicWordFactory = null,
346 Language
$contLang = null,
347 ParserFactory
$factory = null,
348 $urlProtocols = null,
349 SpecialPageFactory
$spFactory = null,
350 $linkRendererFactory = null,
354 if ( !$svcOptions ||
is_array( $svcOptions ) ) {
355 // Pre-1.34 calling convention is the first parameter is just ParserConf, the seventh is
356 // Config, and the eighth is LinkRendererFactory.
357 $this->mConf
= (array)$svcOptions;
358 if ( empty( $this->mConf
['class'] ) ) {
359 $this->mConf
['class'] = self
::class;
361 if ( empty( $this->mConf
['preprocessorClass'] ) ) {
362 $this->mConf
['preprocessorClass'] = self
::getDefaultPreprocessorClass();
364 $this->svcOptions
= new ServiceOptions( self
::$constructorOptions,
365 $this->mConf
, func_num_args() > 6
366 ?
func_get_arg( 6 ) : MediaWikiServices
::getInstance()->getMainConfig()
368 $linkRendererFactory = func_num_args() > 7 ?
func_get_arg( 7 ) : null;
369 $nsInfo = func_num_args() > 8 ?
func_get_arg( 8 ) : null;
371 // New calling convention
372 $svcOptions->assertRequiredOptions( self
::$constructorOptions );
373 // $this->mConf is public, so we'll keep those two options there as well for
374 // compatibility until it's removed
376 'class' => $svcOptions->get( 'class' ),
377 'preprocessorClass' => $svcOptions->get( 'preprocessorClass' ),
379 $this->svcOptions
= $svcOptions;
382 $this->mUrlProtocols
= $urlProtocols ??
wfUrlProtocols();
383 $this->mExtLinkBracketedRegex
= '/\[(((?i)' . $this->mUrlProtocols
. ')' .
384 self
::EXT_LINK_ADDR
.
385 self
::EXT_LINK_URL_CLASS
. '*)\p{Zs}*([^\]\\x00-\\x08\\x0a-\\x1F\\x{FFFD}]*?)\]/Su';
387 $this->magicWordFactory
= $magicWordFactory ??
388 MediaWikiServices
::getInstance()->getMagicWordFactory();
390 $this->contLang
= $contLang ?? MediaWikiServices
::getInstance()->getContentLanguage();
392 $this->factory
= $factory ?? MediaWikiServices
::getInstance()->getParserFactory();
393 $this->specialPageFactory
= $spFactory ??
394 MediaWikiServices
::getInstance()->getSpecialPageFactory();
395 $this->linkRendererFactory
= $linkRendererFactory ??
396 MediaWikiServices
::getInstance()->getLinkRendererFactory();
397 $this->nsInfo
= $nsInfo ?? MediaWikiServices
::getInstance()->getNamespaceInfo();
398 $this->logger
= $logger ?
: new NullLogger();
402 * Reduce memory usage to reduce the impact of circular references
404 public function __destruct() {
405 if ( isset( $this->mLinkHolders
) ) {
406 unset( $this->mLinkHolders
);
408 foreach ( $this as $name => $value ) {
409 unset( $this->$name );
414 * Allow extensions to clean up when the parser is cloned
416 public function __clone() {
417 $this->mInParse
= false;
419 // T58226: When you create a reference "to" an object field, that
420 // makes the object field itself be a reference too (until the other
421 // reference goes out of scope). When cloning, any field that's a
422 // reference is copied as a reference in the new object. Both of these
423 // are defined PHP5 behaviors, as inconvenient as it is for us when old
424 // hooks from PHP4 days are passing fields by reference.
425 foreach ( [ 'mStripState', 'mVarCache' ] as $k ) {
426 // Make a non-reference copy of the field, then rebind the field to
427 // reference the new copy.
433 Hooks
::run( 'ParserCloned', [ $this ] );
437 * Which class should we use for the preprocessor if not otherwise specified?
440 * @deprecated since 1.34, removing configurability of preprocessor
443 public static function getDefaultPreprocessorClass() {
444 return Preprocessor_Hash
::class;
448 * Do various kinds of initialisation on the first call of the parser
450 public function firstCallInit() {
451 if ( !$this->mFirstCall
) {
454 $this->mFirstCall
= false;
456 CoreParserFunctions
::register( $this );
457 CoreTagHooks
::register( $this );
458 $this->initialiseVariables();
460 // Avoid PHP 7.1 warning from passing $this by reference
462 Hooks
::run( 'ParserFirstCallInit', [ &$parser ] );
470 public function clearState() {
471 $this->firstCallInit();
472 $this->resetOutput();
473 $this->mAutonumber
= 0;
474 $this->mIncludeCount
= [];
475 $this->mLinkHolders
= new LinkHolderArray( $this );
477 $this->mRevisionObject
= $this->mRevisionTimestamp
=
478 $this->mRevisionId
= $this->mRevisionUser
= $this->mRevisionSize
= null;
479 $this->mVarCache
= [];
481 $this->mLangLinkLanguages
= [];
482 $this->currentRevisionCache
= null;
484 $this->mStripState
= new StripState( $this );
486 # Clear these on every parse, T6549
487 $this->mTplRedirCache
= $this->mTplDomCache
= [];
489 $this->mShowToc
= true;
490 $this->mForceTocPosition
= false;
491 $this->mIncludeSizes
= [
495 $this->mPPNodeCount
= 0;
496 $this->mGeneratedPPNodeCount
= 0;
497 $this->mHighestExpansionDepth
= 0;
498 $this->mDefaultSort
= false;
499 $this->mHeadings
= [];
500 $this->mDoubleUnderscores
= [];
501 $this->mExpensiveFunctionCount
= 0;
504 if ( isset( $this->mPreprocessor
) && $this->mPreprocessor
->parser
!== $this ) {
505 $this->mPreprocessor
= null;
508 $this->mProfiler
= new SectionProfiler();
510 // Avoid PHP 7.1 warning from passing $this by reference
512 Hooks
::run( 'ParserClearState', [ &$parser ] );
516 * Reset the ParserOutput
518 public function resetOutput() {
519 $this->mOutput
= new ParserOutput
;
520 $this->mOptions
->registerWatcher( [ $this->mOutput
, 'recordOption' ] );
524 * Convert wikitext to HTML
525 * Do not call this function recursively.
527 * @param string $text Text we want to parse
528 * @param-taint $text escapes_htmlnoent
529 * @param Title $title
530 * @param ParserOptions $options
531 * @param bool $linestart
532 * @param bool $clearState
533 * @param int|null $revid Number to pass in {{REVISIONID}}
534 * @return ParserOutput A ParserOutput
535 * @return-taint escaped
537 public function parse(
538 $text, Title
$title, ParserOptions
$options,
539 $linestart = true, $clearState = true, $revid = null
542 // We use U+007F DELETE to construct strip markers, so we have to make
543 // sure that this character does not occur in the input text.
544 $text = strtr( $text, "\x7f", "?" );
545 $magicScopeVariable = $this->lock();
547 // Strip U+0000 NULL (T159174)
548 $text = str_replace( "\000", '', $text );
550 $this->startParse( $title, $options, self
::OT_HTML
, $clearState );
552 $this->currentRevisionCache
= null;
553 $this->mInputSize
= strlen( $text );
554 if ( $this->mOptions
->getEnableLimitReport() ) {
555 $this->mOutput
->resetParseStartTime();
558 $oldRevisionId = $this->mRevisionId
;
559 $oldRevisionObject = $this->mRevisionObject
;
560 $oldRevisionTimestamp = $this->mRevisionTimestamp
;
561 $oldRevisionUser = $this->mRevisionUser
;
562 $oldRevisionSize = $this->mRevisionSize
;
563 if ( $revid !== null ) {
564 $this->mRevisionId
= $revid;
565 $this->mRevisionObject
= null;
566 $this->mRevisionTimestamp
= null;
567 $this->mRevisionUser
= null;
568 $this->mRevisionSize
= null;
571 // Avoid PHP 7.1 warning from passing $this by reference
573 Hooks
::run( 'ParserBeforeStrip', [ &$parser, &$text, &$this->mStripState
] );
575 Hooks
::run( 'ParserAfterStrip', [ &$parser, &$text, &$this->mStripState
] );
576 $text = $this->internalParse( $text );
577 Hooks
::run( 'ParserAfterParse', [ &$parser, &$text, &$this->mStripState
] );
579 $text = $this->internalParseHalfParsed( $text, true, $linestart );
582 * A converted title will be provided in the output object if title and
583 * content conversion are enabled, the article text does not contain
584 * a conversion-suppressing double-underscore tag, and no
585 * {{DISPLAYTITLE:...}} is present. DISPLAYTITLE takes precedence over
586 * automatic link conversion.
588 if ( !( $options->getDisableTitleConversion()
589 ||
isset( $this->mDoubleUnderscores
['nocontentconvert'] )
590 ||
isset( $this->mDoubleUnderscores
['notitleconvert'] )
591 ||
$this->mOutput
->getDisplayTitle() !== false )
593 $convruletitle = $this->getTargetLanguage()->getConvRuleTitle();
594 if ( $convruletitle ) {
595 $this->mOutput
->setTitleText( $convruletitle );
597 $titleText = $this->getTargetLanguage()->convertTitle( $title );
598 $this->mOutput
->setTitleText( $titleText );
602 # Compute runtime adaptive expiry if set
603 $this->mOutput
->finalizeAdaptiveCacheExpiry();
605 # Warn if too many heavyweight parser functions were used
606 if ( $this->mExpensiveFunctionCount
> $this->mOptions
->getExpensiveParserFunctionLimit() ) {
607 $this->limitationWarn( 'expensive-parserfunction',
608 $this->mExpensiveFunctionCount
,
609 $this->mOptions
->getExpensiveParserFunctionLimit()
613 # Information on limits, for the benefit of users who try to skirt them
614 if ( $this->mOptions
->getEnableLimitReport() ) {
615 $text .= $this->makeLimitReport();
618 # Wrap non-interface parser output in a <div> so it can be targeted
620 $class = $this->mOptions
->getWrapOutputClass();
621 if ( $class !== false && !$this->mOptions
->getInterfaceMessage() ) {
622 $this->mOutput
->addWrapperDivClass( $class );
625 $this->mOutput
->setText( $text );
627 $this->mRevisionId
= $oldRevisionId;
628 $this->mRevisionObject
= $oldRevisionObject;
629 $this->mRevisionTimestamp
= $oldRevisionTimestamp;
630 $this->mRevisionUser
= $oldRevisionUser;
631 $this->mRevisionSize
= $oldRevisionSize;
632 $this->mInputSize
= false;
633 $this->currentRevisionCache
= null;
635 return $this->mOutput
;
639 * Set the limit report data in the current ParserOutput, and return the
640 * limit report HTML comment.
644 protected function makeLimitReport() {
645 $maxIncludeSize = $this->mOptions
->getMaxIncludeSize();
647 $cpuTime = $this->mOutput
->getTimeSinceStart( 'cpu' );
648 if ( $cpuTime !== null ) {
649 $this->mOutput
->setLimitReportData( 'limitreport-cputime',
650 sprintf( "%.3f", $cpuTime )
654 $wallTime = $this->mOutput
->getTimeSinceStart( 'wall' );
655 $this->mOutput
->setLimitReportData( 'limitreport-walltime',
656 sprintf( "%.3f", $wallTime )
659 $this->mOutput
->setLimitReportData( 'limitreport-ppvisitednodes',
660 [ $this->mPPNodeCount
, $this->mOptions
->getMaxPPNodeCount() ]
662 $this->mOutput
->setLimitReportData( 'limitreport-ppgeneratednodes',
663 [ $this->mGeneratedPPNodeCount
, $this->mOptions
->getMaxGeneratedPPNodeCount() ]
665 $this->mOutput
->setLimitReportData( 'limitreport-postexpandincludesize',
666 [ $this->mIncludeSizes
['post-expand'], $maxIncludeSize ]
668 $this->mOutput
->setLimitReportData( 'limitreport-templateargumentsize',
669 [ $this->mIncludeSizes
['arg'], $maxIncludeSize ]
671 $this->mOutput
->setLimitReportData( 'limitreport-expansiondepth',
672 [ $this->mHighestExpansionDepth
, $this->mOptions
->getMaxPPExpandDepth() ]
674 $this->mOutput
->setLimitReportData( 'limitreport-expensivefunctioncount',
675 [ $this->mExpensiveFunctionCount
, $this->mOptions
->getExpensiveParserFunctionLimit() ]
678 foreach ( $this->mStripState
->getLimitReport() as list( $key, $value ) ) {
679 $this->mOutput
->setLimitReportData( $key, $value );
682 Hooks
::run( 'ParserLimitReportPrepare', [ $this, $this->mOutput
] );
684 $limitReport = "NewPP limit report\n";
685 if ( $this->svcOptions
->get( 'ShowHostnames' ) ) {
686 $limitReport .= 'Parsed by ' . wfHostname() . "\n";
688 $limitReport .= 'Cached time: ' . $this->mOutput
->getCacheTime() . "\n";
689 $limitReport .= 'Cache expiry: ' . $this->mOutput
->getCacheExpiry() . "\n";
690 $limitReport .= 'Dynamic content: ' .
691 ( $this->mOutput
->hasDynamicContent() ?
'true' : 'false' ) .
693 $limitReport .= 'Complications: [' . implode( ', ', $this->mOutput
->getAllFlags() ) . "]\n";
695 foreach ( $this->mOutput
->getLimitReportData() as $key => $value ) {
696 if ( Hooks
::run( 'ParserLimitReportFormat',
697 [ $key, &$value, &$limitReport, false, false ]
699 $keyMsg = wfMessage( $key )->inLanguage( 'en' )->useDatabase( false );
700 $valueMsg = wfMessage( [ "$key-value-text", "$key-value" ] )
701 ->inLanguage( 'en' )->useDatabase( false );
702 if ( !$valueMsg->exists() ) {
703 $valueMsg = new RawMessage( '$1' );
705 if ( !$keyMsg->isDisabled() && !$valueMsg->isDisabled() ) {
706 $valueMsg->params( $value );
707 $limitReport .= "{$keyMsg->text()}: {$valueMsg->text()}\n";
711 // Since we're not really outputting HTML, decode the entities and
712 // then re-encode the things that need hiding inside HTML comments.
713 $limitReport = htmlspecialchars_decode( $limitReport );
715 // Sanitize for comment. Note '‐' in the replacement is U+2010,
716 // which looks much like the problematic '-'.
717 $limitReport = str_replace( [ '-', '&' ], [ '‐', '&' ], $limitReport );
718 $text = "\n<!-- \n$limitReport-->\n";
720 // Add on template profiling data in human/machine readable way
721 $dataByFunc = $this->mProfiler
->getFunctionStats();
722 uasort( $dataByFunc, function ( $a, $b ) {
723 return $b['real'] <=> $a['real']; // descending order
726 foreach ( array_slice( $dataByFunc, 0, 10 ) as $item ) {
727 $profileReport[] = sprintf( "%6.2f%% %8.3f %6d %s",
728 $item['%real'], $item['real'], $item['calls'],
729 htmlspecialchars( $item['name'] ) );
731 $text .= "<!--\nTransclusion expansion time report (%,ms,calls,template)\n";
732 $text .= implode( "\n", $profileReport ) . "\n-->\n";
734 $this->mOutput
->setLimitReportData( 'limitreport-timingprofile', $profileReport );
736 // Add other cache related metadata
737 if ( $this->svcOptions
->get( 'ShowHostnames' ) ) {
738 $this->mOutput
->setLimitReportData( 'cachereport-origin', wfHostname() );
740 $this->mOutput
->setLimitReportData( 'cachereport-timestamp',
741 $this->mOutput
->getCacheTime() );
742 $this->mOutput
->setLimitReportData( 'cachereport-ttl',
743 $this->mOutput
->getCacheExpiry() );
744 $this->mOutput
->setLimitReportData( 'cachereport-transientcontent',
745 $this->mOutput
->hasDynamicContent() );
747 if ( $this->mGeneratedPPNodeCount
> $this->mOptions
->getMaxGeneratedPPNodeCount() / 10 ) {
748 wfDebugLog( 'generated-pp-node-count', $this->mGeneratedPPNodeCount
. ' ' .
749 $this->mTitle
->getPrefixedDBkey() );
755 * Half-parse wikitext to half-parsed HTML. This recursive parser entry point
756 * can be called from an extension tag hook.
758 * The output of this function IS NOT SAFE PARSED HTML; it is "half-parsed"
759 * instead, which means that lists and links have not been fully parsed yet,
760 * and strip markers are still present.
762 * Use recursiveTagParseFully() to fully parse wikitext to output-safe HTML.
764 * Use this function if you're a parser tag hook and you want to parse
765 * wikitext before or after applying additional transformations, and you
766 * intend to *return the result as hook output*, which will cause it to go
767 * through the rest of parsing process automatically.
769 * If $frame is not provided, then template variables (e.g., {{{1}}}) within
770 * $text are not expanded
772 * @param string $text Text extension wants to have parsed
773 * @param-taint $text escapes_htmlnoent
774 * @param bool|PPFrame $frame The frame to use for expanding any template variables
775 * @return string UNSAFE half-parsed HTML
776 * @return-taint escaped
778 public function recursiveTagParse( $text, $frame = false ) {
779 // Avoid PHP 7.1 warning from passing $this by reference
781 Hooks
::run( 'ParserBeforeStrip', [ &$parser, &$text, &$this->mStripState
] );
782 Hooks
::run( 'ParserAfterStrip', [ &$parser, &$text, &$this->mStripState
] );
783 $text = $this->internalParse( $text, false, $frame );
788 * Fully parse wikitext to fully parsed HTML. This recursive parser entry
789 * point can be called from an extension tag hook.
791 * The output of this function is fully-parsed HTML that is safe for output.
792 * If you're a parser tag hook, you might want to use recursiveTagParse()
795 * If $frame is not provided, then template variables (e.g., {{{1}}}) within
796 * $text are not expanded
800 * @param string $text Text extension wants to have parsed
801 * @param-taint $text escapes_htmlnoent
802 * @param bool|PPFrame $frame The frame to use for expanding any template variables
803 * @return string Fully parsed HTML
804 * @return-taint escaped
806 public function recursiveTagParseFully( $text, $frame = false ) {
807 $text = $this->recursiveTagParse( $text, $frame );
808 $text = $this->internalParseHalfParsed( $text, false );
813 * Expand templates and variables in the text, producing valid, static wikitext.
814 * Also removes comments.
815 * Do not call this function recursively.
816 * @param string $text
817 * @param Title|null $title
818 * @param ParserOptions $options
819 * @param int|null $revid
820 * @param bool|PPFrame $frame
821 * @return mixed|string
823 public function preprocess( $text, Title
$title = null,
824 ParserOptions
$options, $revid = null, $frame = false
826 $magicScopeVariable = $this->lock();
827 $this->startParse( $title, $options, self
::OT_PREPROCESS
, true );
828 if ( $revid !== null ) {
829 $this->mRevisionId
= $revid;
831 // Avoid PHP 7.1 warning from passing $this by reference
833 Hooks
::run( 'ParserBeforeStrip', [ &$parser, &$text, &$this->mStripState
] );
834 Hooks
::run( 'ParserAfterStrip', [ &$parser, &$text, &$this->mStripState
] );
835 $text = $this->replaceVariables( $text, $frame );
836 $text = $this->mStripState
->unstripBoth( $text );
841 * Recursive parser entry point that can be called from an extension tag
844 * @param string $text Text to be expanded
845 * @param bool|PPFrame $frame The frame to use for expanding any template variables
849 public function recursivePreprocess( $text, $frame = false ) {
850 $text = $this->replaceVariables( $text, $frame );
851 $text = $this->mStripState
->unstripBoth( $text );
856 * Process the wikitext for the "?preload=" feature. (T7210)
858 * "<noinclude>", "<includeonly>" etc. are parsed as for template
859 * transclusion, comments, templates, arguments, tags hooks and parser
860 * functions are untouched.
862 * @param string $text
863 * @param Title $title
864 * @param ParserOptions $options
865 * @param array $params
868 public function getPreloadText( $text, Title
$title, ParserOptions
$options, $params = [] ) {
869 $msg = new RawMessage( $text );
870 $text = $msg->params( $params )->plain();
872 # Parser (re)initialisation
873 $magicScopeVariable = $this->lock();
874 $this->startParse( $title, $options, self
::OT_PLAIN
, true );
876 $flags = PPFrame
::NO_ARGS | PPFrame
::NO_TEMPLATES
;
877 $dom = $this->preprocessToDom( $text, self
::PTD_FOR_INCLUSION
);
878 $text = $this->getPreprocessor()->newFrame()->expand( $dom, $flags );
879 $text = $this->mStripState
->unstripBoth( $text );
884 * Set the current user.
885 * Should only be used when doing pre-save transform.
887 * @param User|null $user User object or null (to reset)
889 public function setUser( $user ) {
890 $this->mUser
= $user;
894 * Set the context title
898 public function setTitle( $t ) {
900 $t = Title
::newFromText( 'NO TITLE' );
903 if ( $t->hasFragment() ) {
904 # Strip the fragment to avoid various odd effects
905 $this->mTitle
= $t->createFragmentTarget( '' );
912 * Accessor for the Title object
916 public function getTitle() {
917 return $this->mTitle
;
921 * Accessor/mutator for the Title object
923 * @param Title|null $x Title object or null to just get the current one
926 public function Title( $x = null ) {
927 return wfSetVar( $this->mTitle
, $x );
931 * Set the output type
933 * @param int $ot New value
935 public function setOutputType( $ot ) {
936 $this->mOutputType
= $ot;
939 'html' => $ot == self
::OT_HTML
,
940 'wiki' => $ot == self
::OT_WIKI
,
941 'pre' => $ot == self
::OT_PREPROCESS
,
942 'plain' => $ot == self
::OT_PLAIN
,
947 * Accessor/mutator for the output type
949 * @param int|null $x New value or null to just get the current one
952 public function OutputType( $x = null ) {
953 return wfSetVar( $this->mOutputType
, $x );
957 * Get the ParserOutput object
959 * @return ParserOutput
961 public function getOutput() {
962 return $this->mOutput
;
966 * Get the ParserOptions object
968 * @return ParserOptions
970 public function getOptions() {
971 return $this->mOptions
;
975 * Accessor/mutator for the ParserOptions object
977 * @param ParserOptions|null $x New value or null to just get the current one
978 * @return ParserOptions Current ParserOptions object
980 public function Options( $x = null ) {
981 return wfSetVar( $this->mOptions
, $x );
987 public function nextLinkID() {
988 return $this->mLinkID++
;
994 public function setLinkID( $id ) {
995 $this->mLinkID
= $id;
999 * Get a language object for use in parser functions such as {{FORMATNUM:}}
1002 public function getFunctionLang() {
1003 return $this->getTargetLanguage();
1007 * Get the target language for the content being parsed. This is usually the
1008 * language that the content is in.
1012 * @throws MWException
1015 public function getTargetLanguage() {
1016 $target = $this->mOptions
->getTargetLanguage();
1018 if ( $target !== null ) {
1020 } elseif ( $this->mOptions
->getInterfaceMessage() ) {
1021 return $this->mOptions
->getUserLangObj();
1022 } elseif ( is_null( $this->mTitle
) ) {
1023 throw new MWException( __METHOD__
. ': $this->mTitle is null' );
1026 return $this->mTitle
->getPageLanguage();
1030 * Get the language object for language conversion
1031 * @deprecated since 1.32, just use getTargetLanguage()
1032 * @return Language|null
1034 public function getConverterLanguage() {
1035 return $this->getTargetLanguage();
1039 * Get a User object either from $this->mUser, if set, or from the
1040 * ParserOptions object otherwise
1044 public function getUser() {
1045 if ( !is_null( $this->mUser
) ) {
1046 return $this->mUser
;
1048 return $this->mOptions
->getUser();
1052 * Get a preprocessor object
1054 * @return Preprocessor
1056 public function getPreprocessor() {
1057 if ( !isset( $this->mPreprocessor
) ) {
1058 $class = $this->svcOptions
->get( 'preprocessorClass' );
1059 $this->mPreprocessor
= new $class( $this );
1061 return $this->mPreprocessor
;
1065 * Get a LinkRenderer instance to make links with
1068 * @return LinkRenderer
1070 public function getLinkRenderer() {
1071 // XXX We make the LinkRenderer with current options and then cache it forever
1072 if ( !$this->mLinkRenderer
) {
1073 $this->mLinkRenderer
= $this->linkRendererFactory
->create();
1074 $this->mLinkRenderer
->setStubThreshold(
1075 $this->getOptions()->getStubThreshold()
1079 return $this->mLinkRenderer
;
1083 * Get the MagicWordFactory that this Parser is using
1086 * @return MagicWordFactory
1088 public function getMagicWordFactory() {
1089 return $this->magicWordFactory
;
1093 * Get the content language that this Parser is using
1098 public function getContentLanguage() {
1099 return $this->contLang
;
1103 * Replaces all occurrences of HTML-style comments and the given tags
1104 * in the text with a random marker and returns the next text. The output
1105 * parameter $matches will be an associative array filled with data in
1112 * [ 'param' => 'x' ],
1113 * '<element param="x">tag content</element>' ]
1116 * @param array $elements List of element names. Comments are always extracted.
1117 * @param string $text Source text string.
1118 * @param array &$matches Out parameter, Array: extracted tags
1119 * @return string Stripped text
1121 public static function extractTagsAndParams( $elements, $text, &$matches ) {
1126 $taglist = implode( '|', $elements );
1127 $start = "/<($taglist)(\\s+[^>]*?|\\s*?)(\/?>)|<(!--)/i";
1129 while ( $text != '' ) {
1130 $p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE
);
1132 if ( count( $p ) < 5 ) {
1135 if ( count( $p ) > 5 ) {
1143 list( , $element, $attributes, $close, $inside ) = $p;
1146 $marker = self
::MARKER_PREFIX
. "-$element-" . sprintf( '%08X', $n++
) . self
::MARKER_SUFFIX
;
1147 $stripped .= $marker;
1149 if ( $close === '/>' ) {
1150 # Empty element tag, <tag />
1155 if ( $element === '!--' ) {
1158 $end = "/(<\\/$element\\s*>)/i";
1160 $q = preg_split( $end, $inside, 2, PREG_SPLIT_DELIM_CAPTURE
);
1162 if ( count( $q ) < 3 ) {
1163 # No end tag -- let it run out to the end of the text.
1167 list( , $tail, $text ) = $q;
1171 $matches[$marker] = [ $element,
1173 Sanitizer
::decodeTagAttributes( $attributes ),
1174 "<$element$attributes$close$content$tail" ];
1180 * Get a list of strippable XML-like elements
1184 public function getStripList() {
1185 return $this->mStripList
;
1189 * Get the StripState
1191 * @return StripState
1193 public function getStripState() {
1194 return $this->mStripState
;
1198 * Add an item to the strip state
1199 * Returns the unique tag which must be inserted into the stripped text
1200 * The tag will be replaced with the original text in unstrip()
1202 * @param string $text
1206 public function insertStripItem( $text ) {
1207 $marker = self
::MARKER_PREFIX
. "-item-{$this->mMarkerIndex}-" . self
::MARKER_SUFFIX
;
1208 $this->mMarkerIndex++
;
1209 $this->mStripState
->addGeneral( $marker, $text );
1214 * parse the wiki syntax used to render tables
1217 * @param string $text
1220 public function doTableStuff( $text ) {
1221 $lines = StringUtils
::explode( "\n", $text );
1223 $td_history = []; # Is currently a td tag open?
1224 $last_tag_history = []; # Save history of last lag activated (td, th or caption)
1225 $tr_history = []; # Is currently a tr tag open?
1226 $tr_attributes = []; # history of tr attributes
1227 $has_opened_tr = []; # Did this table open a <tr> element?
1228 $indent_level = 0; # indent level of the table
1230 foreach ( $lines as $outLine ) {
1231 $line = trim( $outLine );
1233 if ( $line === '' ) { # empty line, go to next line
1234 $out .= $outLine . "\n";
1238 $first_character = $line[0];
1239 $first_two = substr( $line, 0, 2 );
1242 if ( preg_match( '/^(:*)\s*\{\|(.*)$/', $line, $matches ) ) {
1243 # First check if we are starting a new table
1244 $indent_level = strlen( $matches[1] );
1246 $attributes = $this->mStripState
->unstripBoth( $matches[2] );
1247 $attributes = Sanitizer
::fixTagAttributes( $attributes, 'table' );
1249 $outLine = str_repeat( '<dl><dd>', $indent_level ) . "<table{$attributes}>";
1250 array_push( $td_history, false );
1251 array_push( $last_tag_history, '' );
1252 array_push( $tr_history, false );
1253 array_push( $tr_attributes, '' );
1254 array_push( $has_opened_tr, false );
1255 } elseif ( count( $td_history ) == 0 ) {
1256 # Don't do any of the following
1257 $out .= $outLine . "\n";
1259 } elseif ( $first_two === '|}' ) {
1260 # We are ending a table
1261 $line = '</table>' . substr( $line, 2 );
1262 $last_tag = array_pop( $last_tag_history );
1264 if ( !array_pop( $has_opened_tr ) ) {
1265 $line = "<tr><td></td></tr>{$line}";
1268 if ( array_pop( $tr_history ) ) {
1269 $line = "</tr>{$line}";
1272 if ( array_pop( $td_history ) ) {
1273 $line = "</{$last_tag}>{$line}";
1275 array_pop( $tr_attributes );
1276 if ( $indent_level > 0 ) {
1277 $outLine = rtrim( $line ) . str_repeat( '</dd></dl>', $indent_level );
1281 } elseif ( $first_two === '|-' ) {
1282 # Now we have a table row
1283 $line = preg_replace( '#^\|-+#', '', $line );
1285 # Whats after the tag is now only attributes
1286 $attributes = $this->mStripState
->unstripBoth( $line );
1287 $attributes = Sanitizer
::fixTagAttributes( $attributes, 'tr' );
1288 array_pop( $tr_attributes );
1289 array_push( $tr_attributes, $attributes );
1292 $last_tag = array_pop( $last_tag_history );
1293 array_pop( $has_opened_tr );
1294 array_push( $has_opened_tr, true );
1296 if ( array_pop( $tr_history ) ) {
1300 if ( array_pop( $td_history ) ) {
1301 $line = "</{$last_tag}>{$line}";
1305 array_push( $tr_history, false );
1306 array_push( $td_history, false );
1307 array_push( $last_tag_history, '' );
1308 } elseif ( $first_character === '|'
1309 ||
$first_character === '!'
1310 ||
$first_two === '|+'
1312 # This might be cell elements, td, th or captions
1313 if ( $first_two === '|+' ) {
1314 $first_character = '+';
1315 $line = substr( $line, 2 );
1317 $line = substr( $line, 1 );
1320 // Implies both are valid for table headings.
1321 if ( $first_character === '!' ) {
1322 $line = StringUtils
::replaceMarkup( '!!', '||', $line );
1325 # Split up multiple cells on the same line.
1326 # FIXME : This can result in improper nesting of tags processed
1327 # by earlier parser steps.
1328 $cells = explode( '||', $line );
1332 # Loop through each table cell
1333 foreach ( $cells as $cell ) {
1335 if ( $first_character !== '+' ) {
1336 $tr_after = array_pop( $tr_attributes );
1337 if ( !array_pop( $tr_history ) ) {
1338 $previous = "<tr{$tr_after}>\n";
1340 array_push( $tr_history, true );
1341 array_push( $tr_attributes, '' );
1342 array_pop( $has_opened_tr );
1343 array_push( $has_opened_tr, true );
1346 $last_tag = array_pop( $last_tag_history );
1348 if ( array_pop( $td_history ) ) {
1349 $previous = "</{$last_tag}>\n{$previous}";
1352 if ( $first_character === '|' ) {
1354 } elseif ( $first_character === '!' ) {
1356 } elseif ( $first_character === '+' ) {
1357 $last_tag = 'caption';
1362 array_push( $last_tag_history, $last_tag );
1364 # A cell could contain both parameters and data
1365 $cell_data = explode( '|', $cell, 2 );
1367 # T2553: Note that a '|' inside an invalid link should not
1368 # be mistaken as delimiting cell parameters
1369 # Bug T153140: Neither should language converter markup.
1370 if ( preg_match( '/\[\[|-\{/', $cell_data[0] ) === 1 ) {
1371 $cell = "{$previous}<{$last_tag}>" . trim( $cell );
1372 } elseif ( count( $cell_data ) == 1 ) {
1373 // Whitespace in cells is trimmed
1374 $cell = "{$previous}<{$last_tag}>" . trim( $cell_data[0] );
1376 $attributes = $this->mStripState
->unstripBoth( $cell_data[0] );
1377 $attributes = Sanitizer
::fixTagAttributes( $attributes, $last_tag );
1378 // Whitespace in cells is trimmed
1379 $cell = "{$previous}<{$last_tag}{$attributes}>" . trim( $cell_data[1] );
1383 array_push( $td_history, true );
1386 $out .= $outLine . "\n";
1389 # Closing open td, tr && table
1390 while ( count( $td_history ) > 0 ) {
1391 if ( array_pop( $td_history ) ) {
1394 if ( array_pop( $tr_history ) ) {
1397 if ( !array_pop( $has_opened_tr ) ) {
1398 $out .= "<tr><td></td></tr>\n";
1401 $out .= "</table>\n";
1404 # Remove trailing line-ending (b/c)
1405 if ( substr( $out, -1 ) === "\n" ) {
1406 $out = substr( $out, 0, -1 );
1409 # special case: don't return empty table
1410 if ( $out === "<table>\n<tr><td></td></tr>\n</table>" ) {
1418 * Helper function for parse() that transforms wiki markup into half-parsed
1419 * HTML. Only called for $mOutputType == self::OT_HTML.
1423 * @param string $text The text to parse
1424 * @param-taint $text escapes_html
1425 * @param bool $isMain Whether this is being called from the main parse() function
1426 * @param PPFrame|bool $frame A pre-processor frame
1430 public function internalParse( $text, $isMain = true, $frame = false ) {
1433 // Avoid PHP 7.1 warning from passing $this by reference
1436 # Hook to suspend the parser in this state
1437 if ( !Hooks
::run( 'ParserBeforeInternalParse', [ &$parser, &$text, &$this->mStripState
] ) ) {
1441 # if $frame is provided, then use $frame for replacing any variables
1443 # use frame depth to infer how include/noinclude tags should be handled
1444 # depth=0 means this is the top-level document; otherwise it's an included document
1445 if ( !$frame->depth
) {
1448 $flag = self
::PTD_FOR_INCLUSION
;
1450 $dom = $this->preprocessToDom( $text, $flag );
1451 $text = $frame->expand( $dom );
1453 # if $frame is not provided, then use old-style replaceVariables
1454 $text = $this->replaceVariables( $text );
1457 Hooks
::run( 'InternalParseBeforeSanitize', [ &$parser, &$text, &$this->mStripState
] );
1458 $text = Sanitizer
::removeHTMLtags(
1460 [ $this, 'attributeStripCallback' ],
1462 array_keys( $this->mTransparentTagHooks
),
1464 [ $this, 'addTrackingCategory' ]
1466 Hooks
::run( 'InternalParseBeforeLinks', [ &$parser, &$text, &$this->mStripState
] );
1468 # Tables need to come after variable replacement for things to work
1469 # properly; putting them before other transformations should keep
1470 # exciting things like link expansions from showing up in surprising
1472 $text = $this->doTableStuff( $text );
1474 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
1476 $text = $this->doDoubleUnderscore( $text );
1478 $text = $this->doHeadings( $text );
1479 $text = $this->replaceInternalLinks( $text );
1480 $text = $this->doAllQuotes( $text );
1481 $text = $this->replaceExternalLinks( $text );
1483 # replaceInternalLinks may sometimes leave behind
1484 # absolute URLs, which have to be masked to hide them from replaceExternalLinks
1485 $text = str_replace( self
::MARKER_PREFIX
. 'NOPARSE', '', $text );
1487 $text = $this->doMagicLinks( $text );
1488 $text = $this->formatHeadings( $text, $origText, $isMain );
1494 * Helper function for parse() that transforms half-parsed HTML into fully
1497 * @param string $text
1498 * @param bool $isMain
1499 * @param bool $linestart
1502 private function internalParseHalfParsed( $text, $isMain = true, $linestart = true ) {
1503 $text = $this->mStripState
->unstripGeneral( $text );
1505 // Avoid PHP 7.1 warning from passing $this by reference
1509 Hooks
::run( 'ParserAfterUnstrip', [ &$parser, &$text ] );
1512 # Clean up special characters, only run once, next-to-last before doBlockLevels
1513 $text = Sanitizer
::armorFrenchSpaces( $text );
1515 $text = $this->doBlockLevels( $text, $linestart );
1517 $this->replaceLinkHolders( $text );
1520 * The input doesn't get language converted if
1522 * b) Content isn't converted
1523 * c) It's a conversion table
1524 * d) it is an interface message (which is in the user language)
1526 if ( !( $this->mOptions
->getDisableContentConversion()
1527 ||
isset( $this->mDoubleUnderscores
['nocontentconvert'] ) )
1528 && !$this->mOptions
->getInterfaceMessage()
1530 # The position of the convert() call should not be changed. it
1531 # assumes that the links are all replaced and the only thing left
1532 # is the <nowiki> mark.
1533 $text = $this->getTargetLanguage()->convert( $text );
1536 $text = $this->mStripState
->unstripNoWiki( $text );
1539 Hooks
::run( 'ParserBeforeTidy', [ &$parser, &$text ] );
1542 $text = $this->replaceTransparentTags( $text );
1543 $text = $this->mStripState
->unstripGeneral( $text );
1545 $text = Sanitizer
::normalizeCharReferences( $text );
1547 if ( MWTidy
::isEnabled() ) {
1548 if ( $this->mOptions
->getTidy() ) {
1549 $text = MWTidy
::tidy( $text );
1552 # attempt to sanitize at least some nesting problems
1553 # (T4702 and quite a few others)
1554 # This code path is buggy and deprecated!
1555 wfDeprecated( 'disabling tidy', '1.33' );
1557 # ''Something [http://www.cool.com cool''] -->
1558 # <i>Something</i><a href="http://www.cool.com"..><i>cool></i></a>
1559 '/(<([bi])>)(<([bi])>)?([^<]*)(<\/?a[^<]*>)([^<]*)(<\/\\4>)?(<\/\\2>)/' =>
1560 '\\1\\3\\5\\8\\9\\6\\1\\3\\7\\8\\9',
1561 # fix up an anchor inside another anchor, only
1562 # at least for a single single nested link (T5695)
1563 '/(<a[^>]+>)([^<]*)(<a[^>]+>[^<]*)<\/a>(.*)<\/a>/' =>
1564 '\\1\\2</a>\\3</a>\\1\\4</a>',
1565 # fix div inside inline elements- doBlockLevels won't wrap a line which
1566 # contains a div, so fix it up here; replace
1567 # div with escaped text
1568 '/(<([aib]) [^>]+>)([^<]*)(<div([^>]*)>)(.*)(<\/div>)([^<]*)(<\/\\2>)/' =>
1569 '\\1\\3<div\\5>\\6</div>\\8\\9',
1570 # remove empty italic or bold tag pairs, some
1571 # introduced by rules above
1572 '/<([bi])><\/\\1>/' => '',
1575 $text = preg_replace(
1576 array_keys( $tidyregs ),
1577 array_values( $tidyregs ),
1582 Hooks
::run( 'ParserAfterTidy', [ &$parser, &$text ] );
1589 * Replace special strings like "ISBN xxx" and "RFC xxx" with
1590 * magic external links.
1595 * @param string $text
1599 public function doMagicLinks( $text ) {
1600 $prots = wfUrlProtocolsWithoutProtRel();
1601 $urlChar = self
::EXT_LINK_URL_CLASS
;
1602 $addr = self
::EXT_LINK_ADDR
;
1603 $space = self
::SPACE_NOT_NL
; # non-newline space
1604 $spdash = "(?:-|$space)"; # a dash or a non-newline space
1605 $spaces = "$space++"; # possessive match of 1 or more spaces
1606 $text = preg_replace_callback(
1608 (<a[ \t\r\n>].*?</a>) | # m[1]: Skip link text
1609 (<.*?>) | # m[2]: Skip stuff inside HTML elements' . "
1610 (\b # m[3]: Free external links
1612 ($addr$urlChar*) # m[4]: Post-protocol path
1614 \b(?:RFC|PMID) $spaces # m[5]: RFC or PMID, capture number
1616 \bISBN $spaces ( # m[6]: ISBN, capture number
1617 (?: 97[89] $spdash? )? # optional 13-digit ISBN prefix
1618 (?: [0-9] $spdash? ){9} # 9 digits with opt. delimiters
1619 [0-9Xx] # check digit
1621 )!xu", [ $this, 'magicLinkCallback' ], $text );
1626 * @throws MWException
1628 * @return string HTML
1630 public function magicLinkCallback( $m ) {
1631 if ( isset( $m[1] ) && $m[1] !== '' ) {
1634 } elseif ( isset( $m[2] ) && $m[2] !== '' ) {
1637 } elseif ( isset( $m[3] ) && $m[3] !== '' ) {
1638 # Free external link
1639 return $this->makeFreeExternalLink( $m[0], strlen( $m[4] ) );
1640 } elseif ( isset( $m[5] ) && $m[5] !== '' ) {
1642 if ( substr( $m[0], 0, 3 ) === 'RFC' ) {
1643 if ( !$this->mOptions
->getMagicRFCLinks() ) {
1648 $cssClass = 'mw-magiclink-rfc';
1649 $trackingCat = 'magiclink-tracking-rfc';
1651 } elseif ( substr( $m[0], 0, 4 ) === 'PMID' ) {
1652 if ( !$this->mOptions
->getMagicPMIDLinks() ) {
1656 $urlmsg = 'pubmedurl';
1657 $cssClass = 'mw-magiclink-pmid';
1658 $trackingCat = 'magiclink-tracking-pmid';
1661 throw new MWException( __METHOD__
. ': unrecognised match type "' .
1662 substr( $m[0], 0, 20 ) . '"' );
1664 $url = wfMessage( $urlmsg, $id )->inContentLanguage()->text();
1665 $this->addTrackingCategory( $trackingCat );
1666 return Linker
::makeExternalLink( $url, "{$keyword} {$id}", true, $cssClass, [], $this->mTitle
);
1667 } elseif ( isset( $m[6] ) && $m[6] !== ''
1668 && $this->mOptions
->getMagicISBNLinks()
1672 $space = self
::SPACE_NOT_NL
; # non-newline space
1673 $isbn = preg_replace( "/$space/", ' ', $isbn );
1674 $num = strtr( $isbn, [
1679 $this->addTrackingCategory( 'magiclink-tracking-isbn' );
1680 return $this->getLinkRenderer()->makeKnownLink(
1681 SpecialPage
::getTitleFor( 'Booksources', $num ),
1684 'class' => 'internal mw-magiclink-isbn',
1685 'title' => false // suppress title attribute
1694 * Make a free external link, given a user-supplied URL
1696 * @param string $url
1697 * @param int $numPostProto
1698 * The number of characters after the protocol.
1699 * @return string HTML
1702 public function makeFreeExternalLink( $url, $numPostProto ) {
1705 # The characters '<' and '>' (which were escaped by
1706 # removeHTMLtags()) should not be included in
1707 # URLs, per RFC 2396.
1708 # Make terminate a URL as well (bug T84937)
1711 '/&(lt|gt|nbsp|#x0*(3[CcEe]|[Aa]0)|#0*(60|62|160));/',
1716 $trail = substr( $url, $m2[0][1] ) . $trail;
1717 $url = substr( $url, 0, $m2[0][1] );
1720 # Move trailing punctuation to $trail
1722 # If there is no left bracket, then consider right brackets fair game too
1723 if ( strpos( $url, '(' ) === false ) {
1727 $urlRev = strrev( $url );
1728 $numSepChars = strspn( $urlRev, $sep );
1729 # Don't break a trailing HTML entity by moving the ; into $trail
1730 # This is in hot code, so use substr_compare to avoid having to
1731 # create a new string object for the comparison
1732 if ( $numSepChars && substr_compare( $url, ";", -$numSepChars, 1 ) === 0 ) {
1733 # more optimization: instead of running preg_match with a $
1734 # anchor, which can be slow, do the match on the reversed
1735 # string starting at the desired offset.
1736 # un-reversed regexp is: /&([a-z]+|#x[\da-f]+|#\d+)$/i
1737 if ( preg_match( '/\G([a-z]+|[\da-f]+x#|\d+#)&/i', $urlRev, $m2, 0, $numSepChars ) ) {
1741 if ( $numSepChars ) {
1742 $trail = substr( $url, -$numSepChars ) . $trail;
1743 $url = substr( $url, 0, -$numSepChars );
1746 # Verify that we still have a real URL after trail removal, and
1747 # not just lone protocol
1748 if ( strlen( $trail ) >= $numPostProto ) {
1749 return $url . $trail;
1752 $url = Sanitizer
::cleanUrl( $url );
1754 # Is this an external image?
1755 $text = $this->maybeMakeExternalImage( $url );
1756 if ( $text === false ) {
1757 # Not an image, make a link
1758 $text = Linker
::makeExternalLink( $url,
1759 $this->getTargetLanguage()->getConverter()->markNoConversion( $url ),
1761 $this->getExternalLinkAttribs( $url ), $this->mTitle
);
1762 # Register it in the output object...
1763 $this->mOutput
->addExternalLink( $url );
1765 return $text . $trail;
1769 * Parse headers and return html
1773 * @param string $text
1777 public function doHeadings( $text ) {
1778 for ( $i = 6; $i >= 1; --$i ) {
1779 $h = str_repeat( '=', $i );
1780 // Trim non-newline whitespace from headings
1781 // Using \s* will break for: "==\n===\n" and parse as <h2>=</h2>
1782 $text = preg_replace( "/^(?:$h)[ \\t]*(.+?)[ \\t]*(?:$h)\\s*$/m", "<h$i>\\1</h$i>", $text );
1788 * Replace single quotes with HTML markup
1791 * @param string $text
1793 * @return string The altered text
1795 public function doAllQuotes( $text ) {
1797 $lines = StringUtils
::explode( "\n", $text );
1798 foreach ( $lines as $line ) {
1799 $outtext .= $this->doQuotes( $line ) . "\n";
1801 $outtext = substr( $outtext, 0, -1 );
1806 * Helper function for doAllQuotes()
1808 * @param string $text
1812 public function doQuotes( $text ) {
1813 $arr = preg_split( "/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
1814 $countarr = count( $arr );
1815 if ( $countarr == 1 ) {
1819 // First, do some preliminary work. This may shift some apostrophes from
1820 // being mark-up to being text. It also counts the number of occurrences
1821 // of bold and italics mark-ups.
1824 for ( $i = 1; $i < $countarr; $i +
= 2 ) {
1825 $thislen = strlen( $arr[$i] );
1826 // If there are ever four apostrophes, assume the first is supposed to
1827 // be text, and the remaining three constitute mark-up for bold text.
1828 // (T15227: ''''foo'''' turns into ' ''' foo ' ''')
1829 if ( $thislen == 4 ) {
1830 $arr[$i - 1] .= "'";
1833 } elseif ( $thislen > 5 ) {
1834 // If there are more than 5 apostrophes in a row, assume they're all
1835 // text except for the last 5.
1836 // (T15227: ''''''foo'''''' turns into ' ''''' foo ' ''''')
1837 $arr[$i - 1] .= str_repeat( "'", $thislen - 5 );
1841 // Count the number of occurrences of bold and italics mark-ups.
1842 if ( $thislen == 2 ) {
1844 } elseif ( $thislen == 3 ) {
1846 } elseif ( $thislen == 5 ) {
1852 // If there is an odd number of both bold and italics, it is likely
1853 // that one of the bold ones was meant to be an apostrophe followed
1854 // by italics. Which one we cannot know for certain, but it is more
1855 // likely to be one that has a single-letter word before it.
1856 if ( ( $numbold %
2 == 1 ) && ( $numitalics %
2 == 1 ) ) {
1857 $firstsingleletterword = -1;
1858 $firstmultiletterword = -1;
1860 for ( $i = 1; $i < $countarr; $i +
= 2 ) {
1861 if ( strlen( $arr[$i] ) == 3 ) {
1862 $x1 = substr( $arr[$i - 1], -1 );
1863 $x2 = substr( $arr[$i - 1], -2, 1 );
1864 if ( $x1 === ' ' ) {
1865 if ( $firstspace == -1 ) {
1868 } elseif ( $x2 === ' ' ) {
1869 $firstsingleletterword = $i;
1870 // if $firstsingleletterword is set, we don't
1871 // look at the other options, so we can bail early.
1873 } elseif ( $firstmultiletterword == -1 ) {
1874 $firstmultiletterword = $i;
1879 // If there is a single-letter word, use it!
1880 if ( $firstsingleletterword > -1 ) {
1881 $arr[$firstsingleletterword] = "''";
1882 $arr[$firstsingleletterword - 1] .= "'";
1883 } elseif ( $firstmultiletterword > -1 ) {
1884 // If not, but there's a multi-letter word, use that one.
1885 $arr[$firstmultiletterword] = "''";
1886 $arr[$firstmultiletterword - 1] .= "'";
1887 } elseif ( $firstspace > -1 ) {
1888 // ... otherwise use the first one that has neither.
1889 // (notice that it is possible for all three to be -1 if, for example,
1890 // there is only one pentuple-apostrophe in the line)
1891 $arr[$firstspace] = "''";
1892 $arr[$firstspace - 1] .= "'";
1896 // Now let's actually convert our apostrophic mush to HTML!
1901 foreach ( $arr as $r ) {
1902 if ( ( $i %
2 ) == 0 ) {
1903 if ( $state === 'both' ) {
1909 $thislen = strlen( $r );
1910 if ( $thislen == 2 ) {
1911 if ( $state === 'i' ) {
1914 } elseif ( $state === 'bi' ) {
1917 } elseif ( $state === 'ib' ) {
1918 $output .= '</b></i><b>';
1920 } elseif ( $state === 'both' ) {
1921 $output .= '<b><i>' . $buffer . '</i>';
1923 } else { // $state can be 'b' or ''
1927 } elseif ( $thislen == 3 ) {
1928 if ( $state === 'b' ) {
1931 } elseif ( $state === 'bi' ) {
1932 $output .= '</i></b><i>';
1934 } elseif ( $state === 'ib' ) {
1937 } elseif ( $state === 'both' ) {
1938 $output .= '<i><b>' . $buffer . '</b>';
1940 } else { // $state can be 'i' or ''
1944 } elseif ( $thislen == 5 ) {
1945 if ( $state === 'b' ) {
1946 $output .= '</b><i>';
1948 } elseif ( $state === 'i' ) {
1949 $output .= '</i><b>';
1951 } elseif ( $state === 'bi' ) {
1952 $output .= '</i></b>';
1954 } elseif ( $state === 'ib' ) {
1955 $output .= '</b></i>';
1957 } elseif ( $state === 'both' ) {
1958 $output .= '<i><b>' . $buffer . '</b></i>';
1960 } else { // ($state == '')
1968 // Now close all remaining tags. Notice that the order is important.
1969 if ( $state === 'b' ||
$state === 'ib' ) {
1972 if ( $state === 'i' ||
$state === 'bi' ||
$state === 'ib' ) {
1975 if ( $state === 'bi' ) {
1978 // There might be lonely ''''', so make sure we have a buffer
1979 if ( $state === 'both' && $buffer ) {
1980 $output .= '<b><i>' . $buffer . '</i></b>';
1986 * Replace external links (REL)
1988 * Note: this is all very hackish and the order of execution matters a lot.
1989 * Make sure to run tests/parser/parserTests.php if you change this code.
1993 * @param string $text
1995 * @throws MWException
1998 public function replaceExternalLinks( $text ) {
1999 $bits = preg_split( $this->mExtLinkBracketedRegex
, $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
2000 if ( $bits === false ) {
2001 throw new MWException( "PCRE needs to be compiled with "
2002 . "--enable-unicode-properties in order for MediaWiki to function" );
2004 $s = array_shift( $bits );
2007 while ( $i < count( $bits ) ) {
2010 $text = $bits[$i++
];
2011 $trail = $bits[$i++
];
2013 # The characters '<' and '>' (which were escaped by
2014 # removeHTMLtags()) should not be included in
2015 # URLs, per RFC 2396.
2017 if ( preg_match( '/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE
) ) {
2018 $text = substr( $url, $m2[0][1] ) . ' ' . $text;
2019 $url = substr( $url, 0, $m2[0][1] );
2022 # If the link text is an image URL, replace it with an <img> tag
2023 # This happened by accident in the original parser, but some people used it extensively
2024 $img = $this->maybeMakeExternalImage( $text );
2025 if ( $img !== false ) {
2031 # Set linktype for CSS
2034 # No link text, e.g. [http://domain.tld/some.link]
2035 if ( $text == '' ) {
2037 $langObj = $this->getTargetLanguage();
2038 $text = '[' . $langObj->formatNum( ++
$this->mAutonumber
) . ']';
2039 $linktype = 'autonumber';
2041 # Have link text, e.g. [http://domain.tld/some.link text]s
2043 list( $dtrail, $trail ) = Linker
::splitTrail( $trail );
2046 // Excluding protocol-relative URLs may avoid many false positives.
2047 if ( preg_match( '/^(?:' . wfUrlProtocolsWithoutProtRel() . ')/', $text ) ) {
2048 $text = $this->getTargetLanguage()->getConverter()->markNoConversion( $text );
2051 $url = Sanitizer
::cleanUrl( $url );
2053 # Use the encoded URL
2054 # This means that users can paste URLs directly into the text
2055 # Funny characters like ö aren't valid in URLs anyway
2056 # This was changed in August 2004
2057 $s .= Linker
::makeExternalLink( $url, $text, false, $linktype,
2058 $this->getExternalLinkAttribs( $url ), $this->mTitle
) . $dtrail . $trail;
2060 # Register link in the output object.
2061 $this->mOutput
->addExternalLink( $url );
2068 * Get the rel attribute for a particular external link.
2071 * @param string|bool $url Optional URL, to extract the domain from for rel =>
2072 * nofollow if appropriate
2073 * @param LinkTarget|null $title Optional LinkTarget, for wgNoFollowNsExceptions lookups
2074 * @return string|null Rel attribute for $url
2076 public static function getExternalLinkRel( $url = false, $title = null ) {
2077 global $wgNoFollowLinks, $wgNoFollowNsExceptions, $wgNoFollowDomainExceptions;
2078 $ns = $title ?
$title->getNamespace() : false;
2079 if ( $wgNoFollowLinks && !in_array( $ns, $wgNoFollowNsExceptions )
2080 && !wfMatchesDomainList( $url, $wgNoFollowDomainExceptions )
2088 * Get an associative array of additional HTML attributes appropriate for a
2089 * particular external link. This currently may include rel => nofollow
2090 * (depending on configuration, namespace, and the URL's domain) and/or a
2091 * target attribute (depending on configuration).
2093 * @param string $url URL to extract the domain from for rel =>
2094 * nofollow if appropriate
2095 * @return array Associative array of HTML attributes
2097 public function getExternalLinkAttribs( $url ) {
2099 $rel = self
::getExternalLinkRel( $url, $this->mTitle
);
2101 $target = $this->mOptions
->getExternalLinkTarget();
2103 $attribs['target'] = $target;
2104 if ( !in_array( $target, [ '_self', '_parent', '_top' ] ) ) {
2105 // T133507. New windows can navigate parent cross-origin.
2106 // Including noreferrer due to lacking browser
2107 // support of noopener. Eventually noreferrer should be removed.
2108 if ( $rel !== '' ) {
2111 $rel .= 'noreferrer noopener';
2114 $attribs['rel'] = $rel;
2119 * Replace unusual escape codes in a URL with their equivalent characters
2121 * This generally follows the syntax defined in RFC 3986, with special
2122 * consideration for HTTP query strings.
2124 * @param string $url
2127 public static function normalizeLinkUrl( $url ) {
2128 # Test for RFC 3986 IPv6 syntax
2129 $scheme = '[a-z][a-z0-9+.-]*:';
2130 $userinfo = '(?:[a-z0-9\-._~!$&\'()*+,;=:]|%[0-9a-f]{2})*';
2131 $ipv6Host = '\\[((?:[0-9a-f:]|%3[0-A]|%[46][1-6])+)\\]';
2132 if ( preg_match( "<^(?:{$scheme})?//(?:{$userinfo}@)?{$ipv6Host}(?:[:/?#].*|)$>i", $url, $m ) &&
2133 IP
::isValid( rawurldecode( $m[1] ) )
2135 $isIPv6 = rawurldecode( $m[1] );
2140 # Make sure unsafe characters are encoded
2141 $url = preg_replace_callback( '/[\x00-\x20"<>\[\\\\\]^`{|}\x7F-\xFF]/',
2143 return rawurlencode( $m[0] );
2149 $end = strlen( $url );
2151 # Fragment part - 'fragment'
2152 $start = strpos( $url, '#' );
2153 if ( $start !== false && $start < $end ) {
2154 $ret = self
::normalizeUrlComponent(
2155 substr( $url, $start, $end - $start ), '"#%<>[\]^`{|}' ) . $ret;
2159 # Query part - 'query' minus &=+;
2160 $start = strpos( $url, '?' );
2161 if ( $start !== false && $start < $end ) {
2162 $ret = self
::normalizeUrlComponent(
2163 substr( $url, $start, $end - $start ), '"#%<>[\]^`{|}&=+;' ) . $ret;
2167 # Scheme and path part - 'pchar'
2168 # (we assume no userinfo or encoded colons in the host)
2169 $ret = self
::normalizeUrlComponent(
2170 substr( $url, 0, $end ), '"#%<>[\]^`{|}/?' ) . $ret;
2173 if ( $isIPv6 !== false ) {
2174 $ipv6Host = "%5B({$isIPv6})%5D";
2175 $ret = preg_replace(
2176 "<^((?:{$scheme})?//(?:{$userinfo}@)?){$ipv6Host}(?=[:/?#]|$)>i",
2185 private static function normalizeUrlComponent( $component, $unsafe ) {
2186 $callback = function ( $matches ) use ( $unsafe ) {
2187 $char = urldecode( $matches[0] );
2188 $ord = ord( $char );
2189 if ( $ord > 32 && $ord < 127 && strpos( $unsafe, $char ) === false ) {
2193 # Leave it escaped, but use uppercase for a-f
2194 return strtoupper( $matches[0] );
2197 return preg_replace_callback( '/%[0-9A-Fa-f]{2}/', $callback, $component );
2201 * make an image if it's allowed, either through the global
2202 * option, through the exception, or through the on-wiki whitelist
2204 * @param string $url
2208 private function maybeMakeExternalImage( $url ) {
2209 $imagesfrom = $this->mOptions
->getAllowExternalImagesFrom();
2210 $imagesexception = !empty( $imagesfrom );
2212 # $imagesfrom could be either a single string or an array of strings, parse out the latter
2213 if ( $imagesexception && is_array( $imagesfrom ) ) {
2214 $imagematch = false;
2215 foreach ( $imagesfrom as $match ) {
2216 if ( strpos( $url, $match ) === 0 ) {
2221 } elseif ( $imagesexception ) {
2222 $imagematch = ( strpos( $url, $imagesfrom ) === 0 );
2224 $imagematch = false;
2227 if ( $this->mOptions
->getAllowExternalImages()
2228 ||
( $imagesexception && $imagematch )
2230 if ( preg_match( self
::EXT_IMAGE_REGEX
, $url ) ) {
2232 $text = Linker
::makeExternalImage( $url );
2235 if ( !$text && $this->mOptions
->getEnableImageWhitelist()
2236 && preg_match( self
::EXT_IMAGE_REGEX
, $url )
2238 $whitelist = explode(
2240 wfMessage( 'external_image_whitelist' )->inContentLanguage()->text()
2243 foreach ( $whitelist as $entry ) {
2244 # Sanitize the regex fragment, make it case-insensitive, ignore blank entries/comments
2245 if ( strpos( $entry, '#' ) === 0 ||
$entry === '' ) {
2248 if ( preg_match( '/' . str_replace( '/', '\\/', $entry ) . '/i', $url ) ) {
2249 # Image matches a whitelist entry
2250 $text = Linker
::makeExternalImage( $url );
2259 * Process [[ ]] wikilinks
2263 * @return string Processed text
2267 public function replaceInternalLinks( $s ) {
2268 $this->mLinkHolders
->merge( $this->replaceInternalLinks2( $s ) );
2273 * Process [[ ]] wikilinks (RIL)
2275 * @throws MWException
2276 * @return LinkHolderArray
2280 public function replaceInternalLinks2( &$s ) {
2281 static $tc = false, $e1, $e1_img;
2282 # the % is needed to support urlencoded titles as well
2284 $tc = Title
::legalChars() . '#%';
2285 # Match a link having the form [[namespace:link|alternate]]trail
2286 $e1 = "/^([{$tc}]+)(?:\\|(.+?))?]](.*)\$/sD";
2287 # Match cases where there is no "]]", which might still be images
2288 $e1_img = "/^([{$tc}]+)\\|(.*)\$/sD";
2291 $holders = new LinkHolderArray( $this );
2293 # split the entire text string on occurrences of [[
2294 $a = StringUtils
::explode( '[[', ' ' . $s );
2295 # get the first element (all text up to first [[), and remove the space we added
2298 $line = $a->current(); # Workaround for broken ArrayIterator::next() that returns "void"
2299 $s = substr( $s, 1 );
2301 $useLinkPrefixExtension = $this->getTargetLanguage()->linkPrefixExtension();
2303 if ( $useLinkPrefixExtension ) {
2304 # Match the end of a line for a word that's not followed by whitespace,
2305 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
2306 $charset = $this->contLang
->linkPrefixCharset();
2307 $e2 = "/^((?>.*[^$charset]|))(.+)$/sDu";
2310 if ( is_null( $this->mTitle
) ) {
2311 throw new MWException( __METHOD__
. ": \$this->mTitle is null\n" );
2313 $nottalk = !$this->mTitle
->isTalkPage();
2315 if ( $useLinkPrefixExtension ) {
2317 if ( preg_match( $e2, $s, $m ) ) {
2318 $first_prefix = $m[2];
2320 $first_prefix = false;
2326 $useSubpages = $this->areSubpagesAllowed();
2328 # Loop for each link
2329 for ( ; $line !== false && $line !== null; $a->next(), $line = $a->current() ) {
2330 # Check for excessive memory usage
2331 if ( $holders->isBig() ) {
2333 # Do the existence check, replace the link holders and clear the array
2334 $holders->replace( $s );
2338 if ( $useLinkPrefixExtension ) {
2339 if ( preg_match( $e2, $s, $m ) ) {
2340 list( , $s, $prefix ) = $m;
2345 if ( $first_prefix ) {
2346 $prefix = $first_prefix;
2347 $first_prefix = false;
2351 $might_be_img = false;
2353 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
2355 # If we get a ] at the beginning of $m[3] that means we have a link that's something like:
2356 # [[Image:Foo.jpg|[http://example.com desc]]] <- having three ] in a row fucks up,
2357 # the real problem is with the $e1 regex
2359 # Still some problems for cases where the ] is meant to be outside punctuation,
2360 # and no image is in sight. See T4095.
2362 && substr( $m[3], 0, 1 ) === ']'
2363 && strpos( $text, '[' ) !== false
2365 $text .= ']'; # so that replaceExternalLinks($text) works later
2366 $m[3] = substr( $m[3], 1 );
2368 # fix up urlencoded title texts
2369 if ( strpos( $m[1], '%' ) !== false ) {
2370 # Should anchors '#' also be rejected?
2371 $m[1] = str_replace( [ '<', '>' ], [ '<', '>' ], rawurldecode( $m[1] ) );
2374 } elseif ( preg_match( $e1_img, $line, $m ) ) {
2375 # Invalid, but might be an image with a link in its caption
2376 $might_be_img = true;
2378 if ( strpos( $m[1], '%' ) !== false ) {
2379 $m[1] = str_replace( [ '<', '>' ], [ '<', '>' ], rawurldecode( $m[1] ) );
2382 } else { # Invalid form; output directly
2383 $s .= $prefix . '[[' . $line;
2387 $origLink = ltrim( $m[1], ' ' );
2389 # Don't allow internal links to pages containing
2390 # PROTO: where PROTO is a valid URL protocol; these
2391 # should be external links.
2392 if ( preg_match( '/^(?i:' . $this->mUrlProtocols
. ')/', $origLink ) ) {
2393 $s .= $prefix . '[[' . $line;
2397 # Make subpage if necessary
2398 if ( $useSubpages ) {
2399 $link = $this->maybeDoSubpageLink( $origLink, $text );
2404 // \x7f isn't a default legal title char, so most likely strip
2405 // markers will force us into the "invalid form" path above. But,
2406 // just in case, let's assert that xmlish tags aren't valid in
2407 // the title position.
2408 $unstrip = $this->mStripState
->killMarkers( $link );
2409 $noMarkers = ( $unstrip === $link );
2411 $nt = $noMarkers ? Title
::newFromText( $link ) : null;
2412 if ( $nt === null ) {
2413 $s .= $prefix . '[[' . $line;
2417 $ns = $nt->getNamespace();
2418 $iw = $nt->getInterwiki();
2420 $noforce = ( substr( $origLink, 0, 1 ) !== ':' );
2422 if ( $might_be_img ) { # if this is actually an invalid link
2423 if ( $ns == NS_FILE
&& $noforce ) { # but might be an image
2426 # look at the next 'line' to see if we can close it there
2428 $next_line = $a->current();
2429 if ( $next_line === false ||
$next_line === null ) {
2432 $m = explode( ']]', $next_line, 3 );
2433 if ( count( $m ) == 3 ) {
2434 # the first ]] closes the inner link, the second the image
2436 $text .= "[[{$m[0]}]]{$m[1]}";
2439 } elseif ( count( $m ) == 2 ) {
2440 # if there's exactly one ]] that's fine, we'll keep looking
2441 $text .= "[[{$m[0]}]]{$m[1]}";
2443 # if $next_line is invalid too, we need look no further
2444 $text .= '[[' . $next_line;
2449 # we couldn't find the end of this imageLink, so output it raw
2450 # but don't ignore what might be perfectly normal links in the text we've examined
2451 $holders->merge( $this->replaceInternalLinks2( $text ) );
2452 $s .= "{$prefix}[[$link|$text";
2453 # note: no $trail, because without an end, there *is* no trail
2456 } else { # it's not an image, so output it raw
2457 $s .= "{$prefix}[[$link|$text";
2458 # note: no $trail, because without an end, there *is* no trail
2463 $wasblank = ( $text == '' );
2467 # Strip off leading ':'
2468 $text = substr( $text, 1 );
2471 # T6598 madness. Handle the quotes only if they come from the alternate part
2472 # [[Lista d''e paise d''o munno]] -> <a href="...">Lista d''e paise d''o munno</a>
2473 # [[Criticism of Harry Potter|Criticism of ''Harry Potter'']]
2474 # -> <a href="Criticism of Harry Potter">Criticism of <i>Harry Potter</i></a>
2475 $text = $this->doQuotes( $text );
2478 # Link not escaped by : , create the various objects
2479 if ( $noforce && !$nt->wasLocalInterwiki() ) {
2482 $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && (
2483 Language
::fetchLanguageName( $iw, null, 'mw' ) ||
2484 in_array( $iw, $this->svcOptions
->get( 'ExtraInterlanguageLinkPrefixes' ) )
2487 # T26502: filter duplicates
2488 if ( !isset( $this->mLangLinkLanguages
[$iw] ) ) {
2489 $this->mLangLinkLanguages
[$iw] = true;
2490 $this->mOutput
->addLanguageLink( $nt->getFullText() );
2494 * Strip the whitespace interwiki links produce, see T10897
2496 $s = rtrim( $s . $prefix ) . $trail; # T175416
2500 if ( $ns == NS_FILE
) {
2501 if ( !wfIsBadImage( $nt->getDBkey(), $this->mTitle
) ) {
2503 # if no parameters were passed, $text
2504 # becomes something like "File:Foo.png",
2505 # which we don't want to pass on to the
2509 # recursively parse links inside the image caption
2510 # actually, this will parse them in any other parameters, too,
2511 # but it might be hard to fix that, and it doesn't matter ATM
2512 $text = $this->replaceExternalLinks( $text );
2513 $holders->merge( $this->replaceInternalLinks2( $text ) );
2515 # cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them
2516 $s .= $prefix . $this->armorLinks(
2517 $this->makeImage( $nt, $text, $holders ) ) . $trail;
2520 } elseif ( $ns == NS_CATEGORY
) {
2522 * Strip the whitespace Category links produce, see T2087
2524 $s = rtrim( $s . $prefix ) . $trail; # T2087, T87753
2527 $sortkey = $this->getDefaultSort();
2531 $sortkey = Sanitizer
::decodeCharReferences( $sortkey );
2532 $sortkey = str_replace( "\n", '', $sortkey );
2533 $sortkey = $this->getTargetLanguage()->convertCategoryKey( $sortkey );
2534 $this->mOutput
->addCategory( $nt->getDBkey(), $sortkey );
2540 # Self-link checking. For some languages, variants of the title are checked in
2541 # LinkHolderArray::doVariants() to allow batching the existence checks necessary
2542 # for linking to a different variant.
2543 if ( $ns != NS_SPECIAL
&& $nt->equals( $this->mTitle
) && !$nt->hasFragment() ) {
2544 $s .= $prefix . Linker
::makeSelfLinkObj( $nt, $text, '', $trail );
2548 # NS_MEDIA is a pseudo-namespace for linking directly to a file
2549 # @todo FIXME: Should do batch file existence checks, see comment below
2550 if ( $ns == NS_MEDIA
) {
2551 # Give extensions a chance to select the file revision for us
2554 Hooks
::run( 'BeforeParserFetchFileAndTitle',
2555 [ $this, $nt, &$options, &$descQuery ] );
2556 # Fetch and register the file (file title may be different via hooks)
2557 list( $file, $nt ) = $this->fetchFileAndTitle( $nt, $options );
2558 # Cloak with NOPARSE to avoid replacement in replaceExternalLinks
2559 $s .= $prefix . $this->armorLinks(
2560 Linker
::makeMediaLinkFile( $nt, $file, $text ) ) . $trail;
2564 # Some titles, such as valid special pages or files in foreign repos, should
2565 # be shown as bluelinks even though they're not included in the page table
2566 # @todo FIXME: isAlwaysKnown() can be expensive for file links; we should really do
2567 # batch file existence checks for NS_FILE and NS_MEDIA
2568 if ( $iw == '' && $nt->isAlwaysKnown() ) {
2569 $this->mOutput
->addLink( $nt );
2570 $s .= $this->makeKnownLinkHolder( $nt, $text, $trail, $prefix );
2572 # Links will be added to the output link list after checking
2573 $s .= $holders->makeHolder( $nt, $text, [], $trail, $prefix );
2580 * Render a forced-blue link inline; protect against double expansion of
2581 * URLs if we're in a mode that prepends full URL prefixes to internal links.
2582 * Since this little disaster has to split off the trail text to avoid
2583 * breaking URLs in the following text without breaking trails on the
2584 * wiki links, it's been made into a horrible function.
2587 * @param string $text
2588 * @param string $trail
2589 * @param string $prefix
2590 * @return string HTML-wikitext mix oh yuck
2592 protected function makeKnownLinkHolder( $nt, $text = '', $trail = '', $prefix = '' ) {
2593 list( $inside, $trail ) = Linker
::splitTrail( $trail );
2595 if ( $text == '' ) {
2596 $text = htmlspecialchars( $nt->getPrefixedText() );
2599 $link = $this->getLinkRenderer()->makeKnownLink(
2600 $nt, new HtmlArmor( "$prefix$text$inside" )
2603 return $this->armorLinks( $link ) . $trail;
2607 * Insert a NOPARSE hacky thing into any inline links in a chunk that's
2608 * going to go through further parsing steps before inline URL expansion.
2610 * Not needed quite as much as it used to be since free links are a bit
2611 * more sensible these days. But bracketed links are still an issue.
2613 * @param string $text More-or-less HTML
2614 * @return string Less-or-more HTML with NOPARSE bits
2616 public function armorLinks( $text ) {
2617 return preg_replace( '/\b((?i)' . $this->mUrlProtocols
. ')/',
2618 self
::MARKER_PREFIX
. "NOPARSE$1", $text );
2622 * Return true if subpage links should be expanded on this page.
2625 public function areSubpagesAllowed() {
2626 # Some namespaces don't allow subpages
2627 return $this->nsInfo
->hasSubpages( $this->mTitle
->getNamespace() );
2631 * Handle link to subpage if necessary
2633 * @param string $target The source of the link
2634 * @param string &$text The link text, modified as necessary
2635 * @return string The full name of the link
2638 public function maybeDoSubpageLink( $target, &$text ) {
2639 return Linker
::normalizeSubpageLink( $this->mTitle
, $target, $text );
2643 * Make lists from lines starting with ':', '*', '#', etc. (DBL)
2645 * @param string $text
2646 * @param bool $linestart Whether or not this is at the start of a line.
2648 * @return string The lists rendered as HTML
2650 public function doBlockLevels( $text, $linestart ) {
2651 return BlockLevelPass
::doBlockLevels( $text, $linestart );
2655 * Return value of a magic variable (like PAGENAME)
2659 * @param string $index Magic variable identifier as mapped in MagicWordFactory::$mVariableIDs
2660 * @param bool|PPFrame $frame
2662 * @throws MWException
2665 public function getVariableValue( $index, $frame = false ) {
2666 if ( is_null( $this->mTitle
) ) {
2667 // If no title set, bad things are going to happen
2668 // later. Title should always be set since this
2669 // should only be called in the middle of a parse
2670 // operation (but the unit-tests do funky stuff)
2671 throw new MWException( __METHOD__
. ' Should only be '
2672 . ' called while parsing (no title set)' );
2675 // Avoid PHP 7.1 warning from passing $this by reference
2679 * Some of these require message or data lookups and can be
2680 * expensive to check many times.
2683 Hooks
::run( 'ParserGetVariableValueVarCache', [ &$parser, &$this->mVarCache
] ) &&
2684 isset( $this->mVarCache
[$index] )
2686 return $this->mVarCache
[$index];
2689 $ts = wfTimestamp( TS_UNIX
, $this->mOptions
->getTimestamp() );
2690 Hooks
::run( 'ParserGetVariableValueTs', [ &$parser, &$ts ] );
2692 $pageLang = $this->getFunctionLang();
2698 case 'currentmonth':
2699 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'm' ), true );
2701 case 'currentmonth1':
2702 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'n' ), true );
2704 case 'currentmonthname':
2705 $value = $pageLang->getMonthName( MWTimestamp
::getInstance( $ts )->format( 'n' ) );
2707 case 'currentmonthnamegen':
2708 $value = $pageLang->getMonthNameGen( MWTimestamp
::getInstance( $ts )->format( 'n' ) );
2710 case 'currentmonthabbrev':
2711 $value = $pageLang->getMonthAbbreviation( MWTimestamp
::getInstance( $ts )->format( 'n' ) );
2714 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'j' ), true );
2717 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'd' ), true );
2720 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'm' ), true );
2723 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'n' ), true );
2725 case 'localmonthname':
2726 $value = $pageLang->getMonthName( MWTimestamp
::getLocalInstance( $ts )->format( 'n' ) );
2728 case 'localmonthnamegen':
2729 $value = $pageLang->getMonthNameGen( MWTimestamp
::getLocalInstance( $ts )->format( 'n' ) );
2731 case 'localmonthabbrev':
2732 $value = $pageLang->getMonthAbbreviation( MWTimestamp
::getLocalInstance( $ts )->format( 'n' ) );
2735 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'j' ), true );
2738 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'd' ), true );
2741 $value = wfEscapeWikiText( $this->mTitle
->getText() );
2744 $value = wfEscapeWikiText( $this->mTitle
->getPartialURL() );
2746 case 'fullpagename':
2747 $value = wfEscapeWikiText( $this->mTitle
->getPrefixedText() );
2749 case 'fullpagenamee':
2750 $value = wfEscapeWikiText( $this->mTitle
->getPrefixedURL() );
2753 $value = wfEscapeWikiText( $this->mTitle
->getSubpageText() );
2755 case 'subpagenamee':
2756 $value = wfEscapeWikiText( $this->mTitle
->getSubpageUrlForm() );
2758 case 'rootpagename':
2759 $value = wfEscapeWikiText( $this->mTitle
->getRootText() );
2761 case 'rootpagenamee':
2762 $value = wfEscapeWikiText( wfUrlencode( str_replace(
2765 $this->mTitle
->getRootText()
2768 case 'basepagename':
2769 $value = wfEscapeWikiText( $this->mTitle
->getBaseText() );
2771 case 'basepagenamee':
2772 $value = wfEscapeWikiText( wfUrlencode( str_replace(
2775 $this->mTitle
->getBaseText()
2778 case 'talkpagename':
2779 if ( $this->mTitle
->canHaveTalkPage() ) {
2780 $talkPage = $this->mTitle
->getTalkPage();
2781 $value = wfEscapeWikiText( $talkPage->getPrefixedText() );
2786 case 'talkpagenamee':
2787 if ( $this->mTitle
->canHaveTalkPage() ) {
2788 $talkPage = $this->mTitle
->getTalkPage();
2789 $value = wfEscapeWikiText( $talkPage->getPrefixedURL() );
2794 case 'subjectpagename':
2795 $subjPage = $this->mTitle
->getSubjectPage();
2796 $value = wfEscapeWikiText( $subjPage->getPrefixedText() );
2798 case 'subjectpagenamee':
2799 $subjPage = $this->mTitle
->getSubjectPage();
2800 $value = wfEscapeWikiText( $subjPage->getPrefixedURL() );
2802 case 'pageid': // requested in T25427
2803 # Inform the edit saving system that getting the canonical output
2804 # after page insertion requires a parse that used that exact page ID
2805 $this->setOutputFlag( 'vary-page-id', '{{PAGEID}} used' );
2806 $value = $this->mTitle
->getArticleID();
2808 $value = $this->mOptions
->getSpeculativePageId();
2810 $this->mOutput
->setSpeculativePageIdUsed( $value );
2816 $this->svcOptions
->get( 'MiserMode' ) &&
2817 !$this->mOptions
->getInterfaceMessage() &&
2818 // @TODO: disallow this word on all namespaces
2819 $this->nsInfo
->isContent( $this->mTitle
->getNamespace() )
2821 // Use a stub result instead of the actual revision ID in order to avoid
2822 // double parses on page save but still allow preview detection (T137900)
2823 if ( $this->getRevisionId() ||
$this->mOptions
->getSpeculativeRevId() ) {
2826 $this->setOutputFlag( 'vary-revision-exists', '{{REVISIONID}} used' );
2830 # Inform the edit saving system that getting the canonical output after
2831 # revision insertion requires a parse that used that exact revision ID
2832 $this->setOutputFlag( 'vary-revision-id', '{{REVISIONID}} used' );
2833 $value = $this->getRevisionId();
2834 if ( $value === 0 ) {
2835 $rev = $this->getRevisionObject();
2836 $value = $rev ?
$rev->getId() : $value;
2839 $value = $this->mOptions
->getSpeculativeRevId();
2841 $this->mOutput
->setSpeculativeRevIdUsed( $value );
2847 $value = (int)$this->getRevisionTimestampSubstring( 6, 2, self
::MAX_TTS
, $index );
2849 case 'revisionday2':
2850 $value = $this->getRevisionTimestampSubstring( 6, 2, self
::MAX_TTS
, $index );
2852 case 'revisionmonth':
2853 $value = $this->getRevisionTimestampSubstring( 4, 2, self
::MAX_TTS
, $index );
2855 case 'revisionmonth1':
2856 $value = (int)$this->getRevisionTimestampSubstring( 4, 2, self
::MAX_TTS
, $index );
2858 case 'revisionyear':
2859 $value = $this->getRevisionTimestampSubstring( 0, 4, self
::MAX_TTS
, $index );
2861 case 'revisiontimestamp':
2862 $value = $this->getRevisionTimestampSubstring( 0, 14, self
::MAX_TTS
, $index );
2864 case 'revisionuser':
2865 # Inform the edit saving system that getting the canonical output after
2866 # revision insertion requires a parse that used the actual user ID
2867 $this->setOutputFlag( 'vary-user', '{{REVISIONUSER}} used' );
2868 $value = $this->getRevisionUser();
2870 case 'revisionsize':
2871 $value = $this->getRevisionSize();
2874 $value = str_replace( '_', ' ',
2875 $this->contLang
->getNsText( $this->mTitle
->getNamespace() ) );
2878 $value = wfUrlencode( $this->contLang
->getNsText( $this->mTitle
->getNamespace() ) );
2880 case 'namespacenumber':
2881 $value = $this->mTitle
->getNamespace();
2884 $value = $this->mTitle
->canHaveTalkPage()
2885 ?
str_replace( '_', ' ', $this->mTitle
->getTalkNsText() )
2889 $value = $this->mTitle
->canHaveTalkPage() ?
wfUrlencode( $this->mTitle
->getTalkNsText() ) : '';
2891 case 'subjectspace':
2892 $value = str_replace( '_', ' ', $this->mTitle
->getSubjectNsText() );
2894 case 'subjectspacee':
2895 $value = ( wfUrlencode( $this->mTitle
->getSubjectNsText() ) );
2897 case 'currentdayname':
2898 $value = $pageLang->getWeekdayName( (int)MWTimestamp
::getInstance( $ts )->format( 'w' ) +
1 );
2901 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'Y' ), true );
2904 $value = $pageLang->time( wfTimestamp( TS_MW
, $ts ), false, false );
2907 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'H' ), true );
2910 # @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
2911 # int to remove the padding
2912 $value = $pageLang->formatNum( (int)MWTimestamp
::getInstance( $ts )->format( 'W' ) );
2915 $value = $pageLang->formatNum( MWTimestamp
::getInstance( $ts )->format( 'w' ) );
2917 case 'localdayname':
2918 $value = $pageLang->getWeekdayName(
2919 (int)MWTimestamp
::getLocalInstance( $ts )->format( 'w' ) +
1
2923 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'Y' ), true );
2926 $value = $pageLang->time(
2927 MWTimestamp
::getLocalInstance( $ts )->format( 'YmdHis' ),
2933 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'H' ), true );
2936 # @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
2937 # int to remove the padding
2938 $value = $pageLang->formatNum( (int)MWTimestamp
::getLocalInstance( $ts )->format( 'W' ) );
2941 $value = $pageLang->formatNum( MWTimestamp
::getLocalInstance( $ts )->format( 'w' ) );
2943 case 'numberofarticles':
2944 $value = $pageLang->formatNum( SiteStats
::articles() );
2946 case 'numberoffiles':
2947 $value = $pageLang->formatNum( SiteStats
::images() );
2949 case 'numberofusers':
2950 $value = $pageLang->formatNum( SiteStats
::users() );
2952 case 'numberofactiveusers':
2953 $value = $pageLang->formatNum( SiteStats
::activeUsers() );
2955 case 'numberofpages':
2956 $value = $pageLang->formatNum( SiteStats
::pages() );
2958 case 'numberofadmins':
2959 $value = $pageLang->formatNum( SiteStats
::numberingroup( 'sysop' ) );
2961 case 'numberofedits':
2962 $value = $pageLang->formatNum( SiteStats
::edits() );
2964 case 'currenttimestamp':
2965 $value = wfTimestamp( TS_MW
, $ts );
2967 case 'localtimestamp':
2968 $value = MWTimestamp
::getLocalInstance( $ts )->format( 'YmdHis' );
2970 case 'currentversion':
2971 $value = SpecialVersion
::getVersion();
2974 return $this->svcOptions
->get( 'ArticlePath' );
2976 return $this->svcOptions
->get( 'Sitename' );
2978 return $this->svcOptions
->get( 'Server' );
2980 return $this->svcOptions
->get( 'ServerName' );
2982 return $this->svcOptions
->get( 'ScriptPath' );
2984 return $this->svcOptions
->get( 'StylePath' );
2985 case 'directionmark':
2986 return $pageLang->getDirMark();
2987 case 'contentlanguage':
2988 return $this->svcOptions
->get( 'LanguageCode' );
2989 case 'pagelanguage':
2990 $value = $pageLang->getCode();
2992 case 'cascadingsources':
2993 $value = CoreParserFunctions
::cascadingsources( $this );
2998 'ParserGetVariableValueSwitch',
2999 [ &$parser, &$this->mVarCache
, &$index, &$ret, &$frame ]
3006 $this->mVarCache
[$index] = $value;
3015 * @param int $mtts Max time-till-save; sets vary-revision-timestamp if result changes by then
3016 * @param string $variable Parser variable name
3019 private function getRevisionTimestampSubstring( $start, $len, $mtts, $variable ) {
3020 # Get the timezone-adjusted timestamp to be used for this revision
3021 $resNow = substr( $this->getRevisionTimestamp(), $start, $len );
3022 # Possibly set vary-revision if there is not yet an associated revision
3023 if ( !$this->getRevisionObject() ) {
3024 # Get the timezone-adjusted timestamp $mtts seconds in the future.
3025 # This future is relative to the current time and not that of the
3026 # parser options. The rendered timestamp can be compared to that
3027 # of the timestamp specified by the parser options.
3029 $this->contLang
->userAdjust( wfTimestamp( TS_MW
, time() +
$mtts ), '' ),
3034 if ( $resNow !== $resThen ) {
3035 # Inform the edit saving system that getting the canonical output after
3036 # revision insertion requires a parse that used an actual revision timestamp
3037 $this->setOutputFlag( 'vary-revision-timestamp', "$variable used" );
3045 * initialise the magic variables (like CURRENTMONTHNAME) and substitution modifiers
3049 public function initialiseVariables() {
3050 $variableIDs = $this->magicWordFactory
->getVariableIDs();
3051 $substIDs = $this->magicWordFactory
->getSubstIDs();
3053 $this->mVariables
= $this->magicWordFactory
->newArray( $variableIDs );
3054 $this->mSubstWords
= $this->magicWordFactory
->newArray( $substIDs );
3058 * Preprocess some wikitext and return the document tree.
3059 * This is the ghost of replace_variables().
3061 * @param string $text The text to parse
3062 * @param int $flags Bitwise combination of:
3063 * - self::PTD_FOR_INCLUSION: Handle "<noinclude>" and "<includeonly>" as if the text is being
3064 * included. Default is to assume a direct page view.
3066 * The generated DOM tree must depend only on the input text and the flags.
3067 * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of T6899.
3069 * Any flag added to the $flags parameter here, or any other parameter liable to cause a
3070 * change in the DOM tree for a given text, must be passed through the section identifier
3071 * in the section edit link and thus back to extractSections().
3073 * The output of this function is currently only cached in process memory, but a persistent
3074 * cache may be implemented at a later date which takes further advantage of these strict
3075 * dependency requirements.
3079 public function preprocessToDom( $text, $flags = 0 ) {
3080 $dom = $this->getPreprocessor()->preprocessToObj( $text, $flags );
3085 * Return a three-element array: leading whitespace, string contents, trailing whitespace
3091 public static function splitWhitespace( $s ) {
3092 $ltrimmed = ltrim( $s );
3093 $w1 = substr( $s, 0, strlen( $s ) - strlen( $ltrimmed ) );
3094 $trimmed = rtrim( $ltrimmed );
3095 $diff = strlen( $ltrimmed ) - strlen( $trimmed );
3097 $w2 = substr( $ltrimmed, -$diff );
3101 return [ $w1, $trimmed, $w2 ];
3105 * Replace magic variables, templates, and template arguments
3106 * with the appropriate text. Templates are substituted recursively,
3107 * taking care to avoid infinite loops.
3109 * Note that the substitution depends on value of $mOutputType:
3110 * self::OT_WIKI: only {{subst:}} templates
3111 * self::OT_PREPROCESS: templates but not extension tags
3112 * self::OT_HTML: all templates and extension tags
3114 * @param string $text The text to transform
3115 * @param false|PPFrame|array $frame Object describing the arguments passed to the
3116 * template. Arguments may also be provided as an associative array, as
3117 * was the usual case before MW1.12. Providing arguments this way may be
3118 * useful for extensions wishing to perform variable replacement
3120 * @param bool $argsOnly Only do argument (triple-brace) expansion, not
3121 * double-brace expansion.
3124 public function replaceVariables( $text, $frame = false, $argsOnly = false ) {
3125 # Is there any text? Also, Prevent too big inclusions!
3126 $textSize = strlen( $text );
3127 if ( $textSize < 1 ||
$textSize > $this->mOptions
->getMaxIncludeSize() ) {
3131 if ( $frame === false ) {
3132 $frame = $this->getPreprocessor()->newFrame();
3133 } elseif ( !( $frame instanceof PPFrame
) ) {
3134 $this->logger
->debug(
3135 __METHOD__
. " called using plain parameters instead of " .
3136 "a PPFrame instance. Creating custom frame."
3138 $frame = $this->getPreprocessor()->newCustomFrame( $frame );
3141 $dom = $this->preprocessToDom( $text );
3142 $flags = $argsOnly ? PPFrame
::NO_TEMPLATES
: 0;
3143 $text = $frame->expand( $dom, $flags );
3149 * Clean up argument array - refactored in 1.9 so parserfunctions can use it, too.
3151 * @param array $args
3155 public static function createAssocArgs( $args ) {
3158 foreach ( $args as $arg ) {
3159 $eqpos = strpos( $arg, '=' );
3160 if ( $eqpos === false ) {
3161 $assocArgs[$index++
] = $arg;
3163 $name = trim( substr( $arg, 0, $eqpos ) );
3164 $value = trim( substr( $arg, $eqpos +
1 ) );
3165 if ( $value === false ) {
3168 if ( $name !== false ) {
3169 $assocArgs[$name] = $value;
3178 * Warn the user when a parser limitation is reached
3179 * Will warn at most once the user per limitation type
3181 * The results are shown during preview and run through the Parser (See EditPage.php)
3183 * @param string $limitationType Should be one of:
3184 * 'expensive-parserfunction' (corresponding messages:
3185 * 'expensive-parserfunction-warning',
3186 * 'expensive-parserfunction-category')
3187 * 'post-expand-template-argument' (corresponding messages:
3188 * 'post-expand-template-argument-warning',
3189 * 'post-expand-template-argument-category')
3190 * 'post-expand-template-inclusion' (corresponding messages:
3191 * 'post-expand-template-inclusion-warning',
3192 * 'post-expand-template-inclusion-category')
3193 * 'node-count-exceeded' (corresponding messages:
3194 * 'node-count-exceeded-warning',
3195 * 'node-count-exceeded-category')
3196 * 'expansion-depth-exceeded' (corresponding messages:
3197 * 'expansion-depth-exceeded-warning',
3198 * 'expansion-depth-exceeded-category')
3199 * @param string|int|null $current Current value
3200 * @param string|int|null $max Maximum allowed, when an explicit limit has been
3201 * exceeded, provide the values (optional)
3203 public function limitationWarn( $limitationType, $current = '', $max = '' ) {
3204 # does no harm if $current and $max are present but are unnecessary for the message
3205 # Not doing ->inLanguage( $this->mOptions->getUserLangObj() ), since this is shown
3206 # only during preview, and that would split the parser cache unnecessarily.
3207 $warning = wfMessage( "$limitationType-warning" )->numParams( $current, $max )
3209 $this->mOutput
->addWarning( $warning );
3210 $this->addTrackingCategory( "$limitationType-category" );
3214 * Return the text of a template, after recursively
3215 * replacing any variables or templates within the template.
3217 * @param array $piece The parts of the template
3218 * $piece['title']: the title, i.e. the part before the |
3219 * $piece['parts']: the parameter array
3220 * $piece['lineStart']: whether the brace was at the start of a line
3221 * @param PPFrame $frame The current frame, contains template arguments
3223 * @return string|array The text of the template
3225 public function braceSubstitution( $piece, $frame ) {
3228 // $text has been filled
3230 // wiki markup in $text should be escaped
3232 // $text is HTML, armour it against wikitext transformation
3234 // Force interwiki transclusion to be done in raw mode not rendered
3235 $forceRawInterwiki = false;
3236 // $text is a DOM node needing expansion in a child frame
3237 $isChildObj = false;
3238 // $text is a DOM node needing expansion in the current frame
3239 $isLocalObj = false;
3241 # Title object, where $text came from
3244 # $part1 is the bit before the first |, and must contain only title characters.
3245 # Various prefixes will be stripped from it later.
3246 $titleWithSpaces = $frame->expand( $piece['title'] );
3247 $part1 = trim( $titleWithSpaces );
3250 # Original title text preserved for various purposes
3251 $originalTitle = $part1;
3253 # $args is a list of argument nodes, starting from index 0, not including $part1
3254 # @todo FIXME: If piece['parts'] is null then the call to getLength()
3255 # below won't work b/c this $args isn't an object
3256 $args = ( $piece['parts'] == null ) ?
[] : $piece['parts'];
3258 $profileSection = null; // profile templates
3262 $substMatch = $this->mSubstWords
->matchStartAndRemove( $part1 );
3264 # Possibilities for substMatch: "subst", "safesubst" or FALSE
3265 # Decide whether to expand template or keep wikitext as-is.
3266 if ( $this->ot
['wiki'] ) {
3267 if ( $substMatch === false ) {
3268 $literal = true; # literal when in PST with no prefix
3270 $literal = false; # expand when in PST with subst: or safesubst:
3273 if ( $substMatch == 'subst' ) {
3274 $literal = true; # literal when not in PST with plain subst:
3276 $literal = false; # expand when not in PST with safesubst: or no prefix
3280 $text = $frame->virtualBracketedImplode( '{{', '|', '}}', $titleWithSpaces, $args );
3287 if ( !$found && $args->getLength() == 0 ) {
3288 $id = $this->mVariables
->matchStartToEnd( $part1 );
3289 if ( $id !== false ) {
3290 $text = $this->getVariableValue( $id, $frame );
3291 if ( $this->magicWordFactory
->getCacheTTL( $id ) > -1 ) {
3292 $this->mOutput
->updateCacheExpiry(
3293 $this->magicWordFactory
->getCacheTTL( $id ) );
3299 # MSG, MSGNW and RAW
3302 $mwMsgnw = $this->magicWordFactory
->get( 'msgnw' );
3303 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
3306 # Remove obsolete MSG:
3307 $mwMsg = $this->magicWordFactory
->get( 'msg' );
3308 $mwMsg->matchStartAndRemove( $part1 );
3312 $mwRaw = $this->magicWordFactory
->get( 'raw' );
3313 if ( $mwRaw->matchStartAndRemove( $part1 ) ) {
3314 $forceRawInterwiki = true;
3320 $colonPos = strpos( $part1, ':' );
3321 if ( $colonPos !== false ) {
3322 $func = substr( $part1, 0, $colonPos );
3323 $funcArgs = [ trim( substr( $part1, $colonPos +
1 ) ) ];
3324 $argsLength = $args->getLength();
3325 for ( $i = 0; $i < $argsLength; $i++
) {
3326 $funcArgs[] = $args->item( $i );
3329 $result = $this->callParserFunction( $frame, $func, $funcArgs );
3331 // Extract any forwarded flags
3332 if ( isset( $result['title'] ) ) {
3333 $title = $result['title'];
3335 if ( isset( $result['found'] ) ) {
3336 $found = $result['found'];
3338 if ( array_key_exists( 'text', $result ) ) {
3340 $text = $result['text'];
3342 if ( isset( $result['nowiki'] ) ) {
3343 $nowiki = $result['nowiki'];
3345 if ( isset( $result['isHTML'] ) ) {
3346 $isHTML = $result['isHTML'];
3348 if ( isset( $result['forceRawInterwiki'] ) ) {
3349 $forceRawInterwiki = $result['forceRawInterwiki'];
3351 if ( isset( $result['isChildObj'] ) ) {
3352 $isChildObj = $result['isChildObj'];
3354 if ( isset( $result['isLocalObj'] ) ) {
3355 $isLocalObj = $result['isLocalObj'];
3360 # Finish mangling title and then check for loops.
3361 # Set $title to a Title object and $titleText to the PDBK
3364 # Split the title into page and subpage
3366 $relative = $this->maybeDoSubpageLink( $part1, $subpage );
3367 if ( $part1 !== $relative ) {
3369 $ns = $this->mTitle
->getNamespace();
3371 $title = Title
::newFromText( $part1, $ns );
3373 $titleText = $title->getPrefixedText();
3374 # Check for language variants if the template is not found
3375 if ( $this->getTargetLanguage()->hasVariants() && $title->getArticleID() == 0 ) {
3376 $this->getTargetLanguage()->findVariantLink( $part1, $title, true );
3378 # Do recursion depth check
3379 $limit = $this->mOptions
->getMaxTemplateDepth();
3380 if ( $frame->depth
>= $limit ) {
3382 $text = '<span class="error">'
3383 . wfMessage( 'parser-template-recursion-depth-warning' )
3384 ->numParams( $limit )->inContentLanguage()->text()
3390 # Load from database
3391 if ( !$found && $title ) {
3392 $profileSection = $this->mProfiler
->scopedProfileIn( $title->getPrefixedDBkey() );
3393 if ( !$title->isExternal() ) {
3394 if ( $title->isSpecialPage()
3395 && $this->mOptions
->getAllowSpecialInclusion()
3396 && $this->ot
['html']
3398 $specialPage = $this->specialPageFactory
->getPage( $title->getDBkey() );
3399 // Pass the template arguments as URL parameters.
3400 // "uselang" will have no effect since the Language object
3401 // is forced to the one defined in ParserOptions.
3403 $argsLength = $args->getLength();
3404 for ( $i = 0; $i < $argsLength; $i++
) {
3405 $bits = $args->item( $i )->splitArg();
3406 if ( strval( $bits['index'] ) === '' ) {
3407 $name = trim( $frame->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
3408 $value = trim( $frame->expand( $bits['value'] ) );
3409 $pageArgs[$name] = $value;
3413 // Create a new context to execute the special page
3414 $context = new RequestContext
;
3415 $context->setTitle( $title );
3416 $context->setRequest( new FauxRequest( $pageArgs ) );
3417 if ( $specialPage && $specialPage->maxIncludeCacheTime() === 0 ) {
3418 $context->setUser( $this->getUser() );
3420 // If this page is cached, then we better not be per user.
3421 $context->setUser( User
::newFromName( '127.0.0.1', false ) );
3423 $context->setLanguage( $this->mOptions
->getUserLangObj() );
3424 $ret = $this->specialPageFactory
->capturePath( $title, $context, $this->getLinkRenderer() );
3426 $text = $context->getOutput()->getHTML();
3427 $this->mOutput
->addOutputPageMetadata( $context->getOutput() );
3430 if ( $specialPage && $specialPage->maxIncludeCacheTime() !== false ) {
3431 $this->mOutput
->updateRuntimeAdaptiveExpiry(
3432 $specialPage->maxIncludeCacheTime()
3436 } elseif ( $this->nsInfo
->isNonincludable( $title->getNamespace() ) ) {
3437 $found = false; # access denied
3438 $this->logger
->debug(
3440 ": template inclusion denied for " . $title->getPrefixedDBkey()
3443 list( $text, $title ) = $this->getTemplateDom( $title );
3444 if ( $text !== false ) {
3450 # If the title is valid but undisplayable, make a link to it
3451 if ( !$found && ( $this->ot
['html'] ||
$this->ot
['pre'] ) ) {
3452 $text = "[[:$titleText]]";
3455 } elseif ( $title->isTrans() ) {
3456 # Interwiki transclusion
3457 if ( $this->ot
['html'] && !$forceRawInterwiki ) {
3458 $text = $this->interwikiTransclude( $title, 'render' );
3461 $text = $this->interwikiTransclude( $title, 'raw' );
3462 # Preprocess it like a template
3463 $text = $this->preprocessToDom( $text, self
::PTD_FOR_INCLUSION
);
3469 # Do infinite loop check
3470 # This has to be done after redirect resolution to avoid infinite loops via redirects
3471 if ( !$frame->loopCheck( $title ) ) {
3473 $text = '<span class="error">'
3474 . wfMessage( 'parser-template-loop-warning', $titleText )->inContentLanguage()->text()
3476 $this->addTrackingCategory( 'template-loop-category' );
3477 $this->mOutput
->addWarning( wfMessage( 'template-loop-warning',
3478 wfEscapeWikiText( $titleText ) )->text() );
3479 $this->logger
->debug( __METHOD__
. ": template loop broken at '$titleText'" );
3483 # If we haven't found text to substitute by now, we're done
3484 # Recover the source wikitext and return it
3486 $text = $frame->virtualBracketedImplode( '{{', '|', '}}', $titleWithSpaces, $args );
3487 if ( $profileSection ) {
3488 $this->mProfiler
->scopedProfileOut( $profileSection );
3490 return [ 'object' => $text ];
3493 # Expand DOM-style return values in a child frame
3494 if ( $isChildObj ) {
3495 # Clean up argument array
3496 $newFrame = $frame->newChild( $args, $title );
3499 $text = $newFrame->expand( $text, PPFrame
::RECOVER_ORIG
);
3500 } elseif ( $titleText !== false && $newFrame->isEmpty() ) {
3501 # Expansion is eligible for the empty-frame cache
3502 $text = $newFrame->cachedExpand( $titleText, $text );
3504 # Uncached expansion
3505 $text = $newFrame->expand( $text );
3508 if ( $isLocalObj && $nowiki ) {
3509 $text = $frame->expand( $text, PPFrame
::RECOVER_ORIG
);
3510 $isLocalObj = false;
3513 if ( $profileSection ) {
3514 $this->mProfiler
->scopedProfileOut( $profileSection );
3517 # Replace raw HTML by a placeholder
3519 $text = $this->insertStripItem( $text );
3520 } elseif ( $nowiki && ( $this->ot
['html'] ||
$this->ot
['pre'] ) ) {
3521 # Escape nowiki-style return values
3522 $text = wfEscapeWikiText( $text );
3523 } elseif ( is_string( $text )
3524 && !$piece['lineStart']
3525 && preg_match( '/^(?:{\\||:|;|#|\*)/', $text )
3527 # T2529: if the template begins with a table or block-level
3528 # element, it should be treated as beginning a new line.
3529 # This behavior is somewhat controversial.
3530 $text = "\n" . $text;
3533 if ( is_string( $text ) && !$this->incrementIncludeSize( 'post-expand', strlen( $text ) ) ) {
3534 # Error, oversize inclusion
3535 if ( $titleText !== false ) {
3536 # Make a working, properly escaped link if possible (T25588)
3537 $text = "[[:$titleText]]";
3539 # This will probably not be a working link, but at least it may
3540 # provide some hint of where the problem is
3541 preg_replace( '/^:/', '', $originalTitle );
3542 $text = "[[:$originalTitle]]";
3544 $text .= $this->insertStripItem( '<!-- WARNING: template omitted, '
3545 . 'post-expand include size too large -->' );
3546 $this->limitationWarn( 'post-expand-template-inclusion' );
3549 if ( $isLocalObj ) {
3550 $ret = [ 'object' => $text ];
3552 $ret = [ 'text' => $text ];
3559 * Call a parser function and return an array with text and flags.
3561 * The returned array will always contain a boolean 'found', indicating
3562 * whether the parser function was found or not. It may also contain the
3564 * text: string|object, resulting wikitext or PP DOM object
3565 * isHTML: bool, $text is HTML, armour it against wikitext transformation
3566 * isChildObj: bool, $text is a DOM node needing expansion in a child frame
3567 * isLocalObj: bool, $text is a DOM node needing expansion in the current frame
3568 * nowiki: bool, wiki markup in $text should be escaped
3571 * @param PPFrame $frame The current frame, contains template arguments
3572 * @param string $function Function name
3573 * @param array $args Arguments to the function
3574 * @throws MWException
3577 public function callParserFunction( $frame, $function, array $args = [] ) {
3578 # Case sensitive functions
3579 if ( isset( $this->mFunctionSynonyms
[1][$function] ) ) {
3580 $function = $this->mFunctionSynonyms
[1][$function];
3582 # Case insensitive functions
3583 $function = $this->contLang
->lc( $function );
3584 if ( isset( $this->mFunctionSynonyms
[0][$function] ) ) {
3585 $function = $this->mFunctionSynonyms
[0][$function];
3587 return [ 'found' => false ];
3591 list( $callback, $flags ) = $this->mFunctionHooks
[$function];
3593 // Avoid PHP 7.1 warning from passing $this by reference
3596 $allArgs = [ &$parser ];
3597 if ( $flags & self
::SFH_OBJECT_ARGS
) {
3598 # Convert arguments to PPNodes and collect for appending to $allArgs
3600 foreach ( $args as $k => $v ) {
3601 if ( $v instanceof PPNode ||
$k === 0 ) {
3604 $funcArgs[] = $this->mPreprocessor
->newPartNodeArray( [ $k => $v ] )->item( 0 );
3608 # Add a frame parameter, and pass the arguments as an array
3609 $allArgs[] = $frame;
3610 $allArgs[] = $funcArgs;
3612 # Convert arguments to plain text and append to $allArgs
3613 foreach ( $args as $k => $v ) {
3614 if ( $v instanceof PPNode
) {
3615 $allArgs[] = trim( $frame->expand( $v ) );
3616 } elseif ( is_int( $k ) && $k >= 0 ) {
3617 $allArgs[] = trim( $v );
3619 $allArgs[] = trim( "$k=$v" );
3624 $result = $callback( ...$allArgs );
3626 # The interface for function hooks allows them to return a wikitext
3627 # string or an array containing the string and any flags. This mungs
3628 # things around to match what this method should return.
3629 if ( !is_array( $result ) ) {
3635 if ( isset( $result[0] ) && !isset( $result['text'] ) ) {
3636 $result['text'] = $result[0];
3638 unset( $result[0] );
3645 $preprocessFlags = 0;
3646 if ( isset( $result['noparse'] ) ) {
3647 $noparse = $result['noparse'];
3649 if ( isset( $result['preprocessFlags'] ) ) {
3650 $preprocessFlags = $result['preprocessFlags'];
3654 $result['text'] = $this->preprocessToDom( $result['text'], $preprocessFlags );
3655 $result['isChildObj'] = true;
3662 * Get the semi-parsed DOM representation of a template with a given title,
3663 * and its redirect destination title. Cached.
3665 * @param Title $title
3669 public function getTemplateDom( $title ) {
3670 $cacheTitle = $title;
3671 $titleText = $title->getPrefixedDBkey();
3673 if ( isset( $this->mTplRedirCache
[$titleText] ) ) {
3674 list( $ns, $dbk ) = $this->mTplRedirCache
[$titleText];
3675 $title = Title
::makeTitle( $ns, $dbk );
3676 $titleText = $title->getPrefixedDBkey();
3678 if ( isset( $this->mTplDomCache
[$titleText] ) ) {
3679 return [ $this->mTplDomCache
[$titleText], $title ];
3682 # Cache miss, go to the database
3683 list( $text, $title ) = $this->fetchTemplateAndTitle( $title );
3685 if ( $text === false ) {
3686 $this->mTplDomCache
[$titleText] = false;
3687 return [ false, $title ];
3690 $dom = $this->preprocessToDom( $text, self
::PTD_FOR_INCLUSION
);
3691 $this->mTplDomCache
[$titleText] = $dom;
3693 if ( !$title->equals( $cacheTitle ) ) {
3694 $this->mTplRedirCache
[$cacheTitle->getPrefixedDBkey()] =
3695 [ $title->getNamespace(), $title->getDBkey() ];
3698 return [ $dom, $title ];
3702 * Fetch the current revision of a given title. Note that the revision
3703 * (and even the title) may not exist in the database, so everything
3704 * contributing to the output of the parser should use this method
3705 * where possible, rather than getting the revisions themselves. This
3706 * method also caches its results, so using it benefits performance.
3709 * @param Title $title
3712 public function fetchCurrentRevisionOfTitle( $title ) {
3713 $cacheKey = $title->getPrefixedDBkey();
3714 if ( !$this->currentRevisionCache
) {
3715 $this->currentRevisionCache
= new MapCacheLRU( 100 );
3717 if ( !$this->currentRevisionCache
->has( $cacheKey ) ) {
3718 $this->currentRevisionCache
->set( $cacheKey,
3719 // Defaults to Parser::statelessFetchRevision()
3720 call_user_func( $this->mOptions
->getCurrentRevisionCallback(), $title, $this )
3723 return $this->currentRevisionCache
->get( $cacheKey );
3727 * @param Title $title
3731 public function isCurrentRevisionOfTitleCached( $title ) {
3733 $this->currentRevisionCache
&&
3734 $this->currentRevisionCache
->has( $title->getPrefixedText() )
3739 * Wrapper around Revision::newFromTitle to allow passing additional parameters
3740 * without passing them on to it.
3743 * @param Title $title
3744 * @param Parser|bool $parser
3745 * @return Revision|bool False if missing
3747 public static function statelessFetchRevision( Title
$title, $parser = false ) {
3748 $rev = Revision
::newKnownCurrent( wfGetDB( DB_REPLICA
), $title );
3754 * Fetch the unparsed text of a template and register a reference to it.
3755 * @param Title $title
3756 * @return array ( string or false, Title )
3758 public function fetchTemplateAndTitle( $title ) {
3759 // Defaults to Parser::statelessFetchTemplate()
3760 $templateCb = $this->mOptions
->getTemplateCallback();
3761 $stuff = call_user_func( $templateCb, $title, $this );
3762 $rev = $stuff['revision'] ??
null;
3763 $text = $stuff['text'];
3764 if ( is_string( $stuff['text'] ) ) {
3765 // We use U+007F DELETE to distinguish strip markers from regular text
3766 $text = strtr( $text, "\x7f", "?" );
3768 $finalTitle = $stuff['finalTitle'] ??
$title;
3769 foreach ( ( $stuff['deps'] ??
[] ) as $dep ) {
3770 $this->mOutput
->addTemplate( $dep['title'], $dep['page_id'], $dep['rev_id'] );
3771 if ( $dep['title']->equals( $this->getTitle() ) && $rev instanceof Revision
) {
3772 // Self-transclusion; final result may change based on the new page version
3773 $this->setOutputFlag( 'vary-revision-sha1', 'Self transclusion' );
3774 $this->getOutput()->setRevisionUsedSha1Base36( $rev->getSha1() );
3778 return [ $text, $finalTitle ];
3782 * Fetch the unparsed text of a template and register a reference to it.
3783 * @param Title $title
3784 * @return string|bool
3786 public function fetchTemplate( $title ) {
3787 return $this->fetchTemplateAndTitle( $title )[0];
3791 * Static function to get a template
3792 * Can be overridden via ParserOptions::setTemplateCallback().
3794 * @param Title $title
3795 * @param bool|Parser $parser
3799 public static function statelessFetchTemplate( $title, $parser = false ) {
3800 $text = $skip = false;
3801 $finalTitle = $title;
3805 # Loop to fetch the article, with up to 1 redirect
3806 for ( $i = 0; $i < 2 && is_object( $title ); $i++
) {
3807 # Give extensions a chance to select the revision instead
3808 $id = false; # Assume current
3809 Hooks
::run( 'BeforeParserFetchTemplateAndtitle',
3810 [ $parser, $title, &$skip, &$id ] );
3816 'page_id' => $title->getArticleID(),
3823 $rev = Revision
::newFromId( $id );
3824 } elseif ( $parser ) {
3825 $rev = $parser->fetchCurrentRevisionOfTitle( $title );
3827 $rev = Revision
::newFromTitle( $title );
3829 $rev_id = $rev ?
$rev->getId() : 0;
3830 # If there is no current revision, there is no page
3831 if ( $id === false && !$rev ) {
3832 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
3833 $linkCache->addBadLinkObj( $title );
3838 'page_id' => $title->getArticleID(),
3841 if ( $rev && !$title->equals( $rev->getTitle() ) ) {
3842 # We fetched a rev from a different title; register it too...
3844 'title' => $rev->getTitle(),
3845 'page_id' => $rev->getPage(),
3851 $content = $rev->getContent();
3852 $text = $content ?
$content->getWikitextForTransclusion() : null;
3854 Hooks
::run( 'ParserFetchTemplate',
3855 [ $parser, $title, $rev, &$text, &$deps ] );
3857 if ( $text === false ||
$text === null ) {
3861 } elseif ( $title->getNamespace() == NS_MEDIAWIKI
) {
3862 $message = wfMessage( MediaWikiServices
::getInstance()->getContentLanguage()->
3863 lcfirst( $title->getText() ) )->inContentLanguage();
3864 if ( !$message->exists() ) {
3868 $content = $message->content();
3869 $text = $message->plain();
3877 $finalTitle = $title;
3878 $title = $content->getRedirectTarget();
3883 'finalTitle' => $finalTitle,
3889 * Fetch a file and its title and register a reference to it.
3890 * If 'broken' is a key in $options then the file will appear as a broken thumbnail.
3891 * @param Title $title
3892 * @param array $options Array of options to RepoGroup::findFile
3893 * @return array ( File or false, Title of file )
3895 public function fetchFileAndTitle( $title, $options = [] ) {
3896 $file = $this->fetchFileNoRegister( $title, $options );
3898 $time = $file ?
$file->getTimestamp() : false;
3899 $sha1 = $file ?
$file->getSha1() : false;
3900 # Register the file as a dependency...
3901 $this->mOutput
->addImage( $title->getDBkey(), $time, $sha1 );
3902 if ( $file && !$title->equals( $file->getTitle() ) ) {
3903 # Update fetched file title
3904 $title = $file->getTitle();
3905 $this->mOutput
->addImage( $title->getDBkey(), $time, $sha1 );
3907 return [ $file, $title ];
3911 * Helper function for fetchFileAndTitle.
3913 * Also useful if you need to fetch a file but not use it yet,
3914 * for example to get the file's handler.
3916 * @param Title $title
3917 * @param array $options Array of options to RepoGroup::findFile
3920 protected function fetchFileNoRegister( $title, $options = [] ) {
3921 if ( isset( $options['broken'] ) ) {
3922 $file = false; // broken thumbnail forced by hook
3923 } elseif ( isset( $options['sha1'] ) ) { // get by (sha1,timestamp)
3924 $file = RepoGroup
::singleton()->findFileFromKey( $options['sha1'], $options );
3925 } else { // get by (name,timestamp)
3926 $file = MediaWikiServices
::getInstance()->getRepoGroup()->findFile( $title, $options );
3932 * Transclude an interwiki link.
3934 * @param Title $title
3935 * @param string $action Usually one of (raw, render)
3939 public function interwikiTransclude( $title, $action ) {
3940 if ( !$this->svcOptions
->get( 'EnableScaryTranscluding' ) ) {
3941 return wfMessage( 'scarytranscludedisabled' )->inContentLanguage()->text();
3944 $url = $title->getFullURL( [ 'action' => $action ] );
3945 if ( strlen( $url ) > 1024 ) {
3946 return wfMessage( 'scarytranscludetoolong' )->inContentLanguage()->text();
3949 $wikiId = $title->getTransWikiID(); // remote wiki ID or false
3951 $fname = __METHOD__
;
3952 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
3954 $data = $cache->getWithSetCallback(
3955 $cache->makeGlobalKey(
3956 'interwiki-transclude',
3957 ( $wikiId !== false ) ?
$wikiId : 'external',
3960 $this->svcOptions
->get( 'TranscludeCacheExpiry' ),
3961 function ( $oldValue, &$ttl ) use ( $url, $fname, $cache ) {
3962 $req = MWHttpRequest
::factory( $url, [], $fname );
3964 $status = $req->execute(); // Status object
3965 if ( !$status->isOK() ) {
3966 $ttl = $cache::TTL_UNCACHEABLE
;
3967 } elseif ( $req->getResponseHeader( 'X-Database-Lagged' ) !== null ) {
3968 $ttl = min( $cache::TTL_LAGGED
, $ttl );
3972 'text' => $status->isOK() ?
$req->getContent() : null,
3973 'code' => $req->getStatus()
3977 'checkKeys' => ( $wikiId !== false )
3978 ?
[ $cache->makeGlobalKey( 'interwiki-page', $wikiId, $title->getDBkey() ) ]
3980 'pcGroup' => 'interwiki-transclude:5',
3981 'pcTTL' => $cache::TTL_PROC_LONG
3985 if ( is_string( $data['text'] ) ) {
3986 $text = $data['text'];
3987 } elseif ( $data['code'] != 200 ) {
3988 // Though we failed to fetch the content, this status is useless.
3989 $text = wfMessage( 'scarytranscludefailed-httpstatus' )
3990 ->params( $url, $data['code'] )->inContentLanguage()->text();
3992 $text = wfMessage( 'scarytranscludefailed', $url )->inContentLanguage()->text();
3999 * Triple brace replacement -- used for template arguments
4002 * @param array $piece
4003 * @param PPFrame $frame
4007 public function argSubstitution( $piece, $frame ) {
4009 $parts = $piece['parts'];
4010 $nameWithSpaces = $frame->expand( $piece['title'] );
4011 $argName = trim( $nameWithSpaces );
4013 $text = $frame->getArgument( $argName );
4014 if ( $text === false && $parts->getLength() > 0
4015 && ( $this->ot
['html']
4017 ||
( $this->ot
['wiki'] && $frame->isTemplate() )
4020 # No match in frame, use the supplied default
4021 $object = $parts->item( 0 )->getChildren();
4023 if ( !$this->incrementIncludeSize( 'arg', strlen( $text ) ) ) {
4024 $error = '<!-- WARNING: argument omitted, expansion size too large -->';
4025 $this->limitationWarn( 'post-expand-template-argument' );
4028 if ( $text === false && $object === false ) {
4030 $object = $frame->virtualBracketedImplode( '{{{', '|', '}}}', $nameWithSpaces, $parts );
4032 if ( $error !== false ) {
4035 if ( $object !== false ) {
4036 $ret = [ 'object' => $object ];
4038 $ret = [ 'text' => $text ];
4045 * Return the text to be used for a given extension tag.
4046 * This is the ghost of strip().
4048 * @param array $params Associative array of parameters:
4049 * name PPNode for the tag name
4050 * attr PPNode for unparsed text where tag attributes are thought to be
4051 * attributes Optional associative array of parsed attributes
4052 * inner Contents of extension element
4053 * noClose Original text did not have a close tag
4054 * @param PPFrame $frame
4056 * @throws MWException
4059 public function extensionSubstitution( $params, $frame ) {
4060 static $errorStr = '<span class="error">';
4061 static $errorLen = 20;
4063 $name = $frame->expand( $params['name'] );
4064 if ( substr( $name, 0, $errorLen ) === $errorStr ) {
4065 // Probably expansion depth or node count exceeded. Just punt the
4070 $attrText = !isset( $params['attr'] ) ?
null : $frame->expand( $params['attr'] );
4071 if ( substr( $attrText, 0, $errorLen ) === $errorStr ) {
4076 // We can't safely check if the expansion for $content resulted in an
4077 // error, because the content could happen to be the error string
4079 $content = !isset( $params['inner'] ) ?
null : $frame->expand( $params['inner'] );
4081 $marker = self
::MARKER_PREFIX
. "-$name-"
4082 . sprintf( '%08X', $this->mMarkerIndex++
) . self
::MARKER_SUFFIX
;
4084 $isFunctionTag = isset( $this->mFunctionTagHooks
[strtolower( $name )] ) &&
4085 ( $this->ot
['html'] ||
$this->ot
['pre'] );
4086 if ( $isFunctionTag ) {
4087 $markerType = 'none';
4089 $markerType = 'general';
4091 if ( $this->ot
['html'] ||
$isFunctionTag ) {
4092 $name = strtolower( $name );
4093 $attributes = Sanitizer
::decodeTagAttributes( $attrText );
4094 if ( isset( $params['attributes'] ) ) {
4095 $attributes +
= $params['attributes'];
4098 if ( isset( $this->mTagHooks
[$name] ) ) {
4099 $output = call_user_func_array( $this->mTagHooks
[$name],
4100 [ $content, $attributes, $this, $frame ] );
4101 } elseif ( isset( $this->mFunctionTagHooks
[$name] ) ) {
4102 list( $callback, ) = $this->mFunctionTagHooks
[$name];
4104 // Avoid PHP 7.1 warning from passing $this by reference
4106 $output = call_user_func_array( $callback, [ &$parser, $frame, $content, $attributes ] );
4108 $output = '<span class="error">Invalid tag extension name: ' .
4109 htmlspecialchars( $name ) . '</span>';
4112 if ( is_array( $output ) ) {
4115 $output = $flags[0];
4116 if ( isset( $flags['markerType'] ) ) {
4117 $markerType = $flags['markerType'];
4121 if ( is_null( $attrText ) ) {
4124 if ( isset( $params['attributes'] ) ) {
4125 foreach ( $params['attributes'] as $attrName => $attrValue ) {
4126 $attrText .= ' ' . htmlspecialchars( $attrName ) . '="' .
4127 htmlspecialchars( $attrValue ) . '"';
4130 if ( $content === null ) {
4131 $output = "<$name$attrText/>";
4133 $close = is_null( $params['close'] ) ?
'' : $frame->expand( $params['close'] );
4134 if ( substr( $close, 0, $errorLen ) === $errorStr ) {
4138 $output = "<$name$attrText>$content$close";
4142 if ( $markerType === 'none' ) {
4144 } elseif ( $markerType === 'nowiki' ) {
4145 $this->mStripState
->addNoWiki( $marker, $output );
4146 } elseif ( $markerType === 'general' ) {
4147 $this->mStripState
->addGeneral( $marker, $output );
4149 throw new MWException( __METHOD__
. ': invalid marker type' );
4155 * Increment an include size counter
4157 * @param string $type The type of expansion
4158 * @param int $size The size of the text
4159 * @return bool False if this inclusion would take it over the maximum, true otherwise
4161 public function incrementIncludeSize( $type, $size ) {
4162 if ( $this->mIncludeSizes
[$type] +
$size > $this->mOptions
->getMaxIncludeSize() ) {
4165 $this->mIncludeSizes
[$type] +
= $size;
4171 * Increment the expensive function count
4173 * @return bool False if the limit has been exceeded
4175 public function incrementExpensiveFunctionCount() {
4176 $this->mExpensiveFunctionCount++
;
4177 return $this->mExpensiveFunctionCount
<= $this->mOptions
->getExpensiveParserFunctionLimit();
4181 * Strip double-underscore items like __NOGALLERY__ and __NOTOC__
4182 * Fills $this->mDoubleUnderscores, returns the modified text
4184 * @param string $text
4188 public function doDoubleUnderscore( $text ) {
4189 # The position of __TOC__ needs to be recorded
4190 $mw = $this->magicWordFactory
->get( 'toc' );
4191 if ( $mw->match( $text ) ) {
4192 $this->mShowToc
= true;
4193 $this->mForceTocPosition
= true;
4195 # Set a placeholder. At the end we'll fill it in with the TOC.
4196 $text = $mw->replace( '<!--MWTOC\'"-->', $text, 1 );
4198 # Only keep the first one.
4199 $text = $mw->replace( '', $text );
4202 # Now match and remove the rest of them
4203 $mwa = $this->magicWordFactory
->getDoubleUnderscoreArray();
4204 $this->mDoubleUnderscores
= $mwa->matchAndRemove( $text );
4206 if ( isset( $this->mDoubleUnderscores
['nogallery'] ) ) {
4207 $this->mOutput
->mNoGallery
= true;
4209 if ( isset( $this->mDoubleUnderscores
['notoc'] ) && !$this->mForceTocPosition
) {
4210 $this->mShowToc
= false;
4212 if ( isset( $this->mDoubleUnderscores
['hiddencat'] )
4213 && $this->mTitle
->getNamespace() == NS_CATEGORY
4215 $this->addTrackingCategory( 'hidden-category-category' );
4217 # (T10068) Allow control over whether robots index a page.
4218 # __INDEX__ always overrides __NOINDEX__, see T16899
4219 if ( isset( $this->mDoubleUnderscores
['noindex'] ) && $this->mTitle
->canUseNoindex() ) {
4220 $this->mOutput
->setIndexPolicy( 'noindex' );
4221 $this->addTrackingCategory( 'noindex-category' );
4223 if ( isset( $this->mDoubleUnderscores
['index'] ) && $this->mTitle
->canUseNoindex() ) {
4224 $this->mOutput
->setIndexPolicy( 'index' );
4225 $this->addTrackingCategory( 'index-category' );
4228 # Cache all double underscores in the database
4229 foreach ( $this->mDoubleUnderscores
as $key => $val ) {
4230 $this->mOutput
->setProperty( $key, '' );
4237 * @see ParserOutput::addTrackingCategory()
4238 * @param string $msg Message key
4239 * @return bool Whether the addition was successful
4241 public function addTrackingCategory( $msg ) {
4242 return $this->mOutput
->addTrackingCategory( $msg, $this->mTitle
);
4246 * This function accomplishes several tasks:
4247 * 1) Auto-number headings if that option is enabled
4248 * 2) Add an [edit] link to sections for users who have enabled the option and can edit the page
4249 * 3) Add a Table of contents on the top for users who have enabled the option
4250 * 4) Auto-anchor headings
4252 * It loops through all headlines, collects the necessary data, then splits up the
4253 * string and re-inserts the newly formatted headlines.
4255 * @param string $text
4256 * @param string $origText Original, untouched wikitext
4257 * @param bool $isMain
4258 * @return mixed|string
4261 public function formatHeadings( $text, $origText, $isMain = true ) {
4262 # Inhibit editsection links if requested in the page
4263 if ( isset( $this->mDoubleUnderscores
['noeditsection'] ) ) {
4264 $maybeShowEditLink = false;
4266 $maybeShowEditLink = true; /* Actual presence will depend on post-cache transforms */
4269 # Get all headlines for numbering them and adding funky stuff like [edit]
4270 # links - this is for later, but we need the number of headlines right now
4271 # NOTE: white space in headings have been trimmed in doHeadings. They shouldn't
4272 # be trimmed here since whitespace in HTML headings is significant.
4274 $numMatches = preg_match_all(
4275 '/<H(?P<level>[1-6])(?P<attrib>.*?>)(?P<header>[\s\S]*?)<\/H[1-6] *>/i',
4280 # if there are fewer than 4 headlines in the article, do not show TOC
4281 # unless it's been explicitly enabled.
4282 $enoughToc = $this->mShowToc
&&
4283 ( ( $numMatches >= 4 ) ||
$this->mForceTocPosition
);
4285 # Allow user to stipulate that a page should have a "new section"
4286 # link added via __NEWSECTIONLINK__
4287 if ( isset( $this->mDoubleUnderscores
['newsectionlink'] ) ) {
4288 $this->mOutput
->setNewSection( true );
4291 # Allow user to remove the "new section"
4292 # link via __NONEWSECTIONLINK__
4293 if ( isset( $this->mDoubleUnderscores
['nonewsectionlink'] ) ) {
4294 $this->mOutput
->hideNewSection( true );
4297 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
4298 # override above conditions and always show TOC above first header
4299 if ( isset( $this->mDoubleUnderscores
['forcetoc'] ) ) {
4300 $this->mShowToc
= true;
4308 # Ugh .. the TOC should have neat indentation levels which can be
4309 # passed to the skin functions. These are determined here
4313 $sublevelCount = [];
4319 $markerRegex = self
::MARKER_PREFIX
. "-h-(\d+)-" . self
::MARKER_SUFFIX
;
4320 $baseTitleText = $this->mTitle
->getPrefixedDBkey();
4321 $oldType = $this->mOutputType
;
4322 $this->setOutputType( self
::OT_WIKI
);
4323 $frame = $this->getPreprocessor()->newFrame();
4324 $root = $this->preprocessToDom( $origText );
4325 $node = $root->getFirstChild();
4330 $headlines = $numMatches !== false ?
$matches[3] : [];
4332 $maxTocLevel = $this->svcOptions
->get( 'MaxTocLevel' );
4333 foreach ( $headlines as $headline ) {
4334 $isTemplate = false;
4336 $sectionIndex = false;
4338 $markerMatches = [];
4339 if ( preg_match( "/^$markerRegex/", $headline, $markerMatches ) ) {
4340 $serial = $markerMatches[1];
4341 list( $titleText, $sectionIndex ) = $this->mHeadings
[$serial];
4342 $isTemplate = ( $titleText != $baseTitleText );
4343 $headline = preg_replace( "/^$markerRegex\\s*/", "", $headline );
4347 $prevlevel = $level;
4349 $level = $matches[1][$headlineCount];
4351 if ( $level > $prevlevel ) {
4352 # Increase TOC level
4354 $sublevelCount[$toclevel] = 0;
4355 if ( $toclevel < $maxTocLevel ) {
4356 $prevtoclevel = $toclevel;
4357 $toc .= Linker
::tocIndent();
4360 } elseif ( $level < $prevlevel && $toclevel > 1 ) {
4361 # Decrease TOC level, find level to jump to
4363 for ( $i = $toclevel; $i > 0; $i-- ) {
4364 if ( $levelCount[$i] == $level ) {
4365 # Found last matching level
4368 } elseif ( $levelCount[$i] < $level ) {
4369 # Found first matching level below current level
4377 if ( $toclevel < $maxTocLevel ) {
4378 if ( $prevtoclevel < $maxTocLevel ) {
4379 # Unindent only if the previous toc level was shown :p
4380 $toc .= Linker
::tocUnindent( $prevtoclevel - $toclevel );
4381 $prevtoclevel = $toclevel;
4383 $toc .= Linker
::tocLineEnd();
4387 # No change in level, end TOC line
4388 if ( $toclevel < $maxTocLevel ) {
4389 $toc .= Linker
::tocLineEnd();
4393 $levelCount[$toclevel] = $level;
4395 # count number of headlines for each level
4396 $sublevelCount[$toclevel]++
;
4398 for ( $i = 1; $i <= $toclevel; $i++
) {
4399 if ( !empty( $sublevelCount[$i] ) ) {
4403 $numbering .= $this->getTargetLanguage()->formatNum( $sublevelCount[$i] );
4408 # The safe header is a version of the header text safe to use for links
4410 # Remove link placeholders by the link text.
4411 # <!--LINK number-->
4413 # link text with suffix
4414 # Do this before unstrip since link text can contain strip markers
4415 $safeHeadline = $this->replaceLinkHoldersText( $headline );
4417 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
4418 $safeHeadline = $this->mStripState
->unstripBoth( $safeHeadline );
4420 # Remove any <style> or <script> tags (T198618)
4421 $safeHeadline = preg_replace(
4422 '#<(style|script)(?: [^>]*[^>/])?>.*?</\1>#is',
4427 # Strip out HTML (first regex removes any tag not allowed)
4429 # * <sup> and <sub> (T10393)
4433 # * <span dir="rtl"> and <span dir="ltr"> (T37167)
4434 # * <s> and <strike> (T35715)
4435 # We strip any parameter from accepted tags (second regex), except dir="rtl|ltr" from <span>,
4436 # to allow setting directionality in toc items.
4437 $tocline = preg_replace(
4439 '#<(?!/?(span|sup|sub|bdi|i|b|s|strike)(?: [^>]*)?>).*?>#',
4440 '#<(/?(?:span(?: dir="(?:rtl|ltr)")?|sup|sub|bdi|i|b|s|strike))(?: .*?)?>#'
4446 # Strip '<span></span>', which is the result from the above if
4447 # <span id="foo"></span> is used to produce an additional anchor
4449 $tocline = str_replace( '<span></span>', '', $tocline );
4451 $tocline = trim( $tocline );
4453 # For the anchor, strip out HTML-y stuff period
4454 $safeHeadline = preg_replace( '/<.*?>/', '', $safeHeadline );
4455 $safeHeadline = Sanitizer
::normalizeSectionNameWhitespace( $safeHeadline );
4457 # Save headline for section edit hint before it's escaped
4458 $headlineHint = $safeHeadline;
4460 # Decode HTML entities
4461 $safeHeadline = Sanitizer
::decodeCharReferences( $safeHeadline );
4463 $safeHeadline = self
::normalizeSectionName( $safeHeadline );
4465 $fallbackHeadline = Sanitizer
::escapeIdForAttribute( $safeHeadline, Sanitizer
::ID_FALLBACK
);
4466 $linkAnchor = Sanitizer
::escapeIdForLink( $safeHeadline );
4467 $safeHeadline = Sanitizer
::escapeIdForAttribute( $safeHeadline, Sanitizer
::ID_PRIMARY
);
4468 if ( $fallbackHeadline === $safeHeadline ) {
4469 # No reason to have both (in fact, we can't)
4470 $fallbackHeadline = false;
4473 # HTML IDs must be case-insensitively unique for IE compatibility (T12721).
4474 # @todo FIXME: We may be changing them depending on the current locale.
4475 $arrayKey = strtolower( $safeHeadline );
4476 if ( $fallbackHeadline === false ) {
4477 $fallbackArrayKey = false;
4479 $fallbackArrayKey = strtolower( $fallbackHeadline );
4482 # Create the anchor for linking from the TOC to the section
4483 $anchor = $safeHeadline;
4484 $fallbackAnchor = $fallbackHeadline;
4485 if ( isset( $refers[$arrayKey] ) ) {
4486 // phpcs:ignore Generic.Formatting.DisallowMultipleStatements
4487 for ( $i = 2; isset( $refers["${arrayKey}_$i"] ); ++
$i );
4489 $linkAnchor .= "_$i";
4490 $refers["${arrayKey}_$i"] = true;
4492 $refers[$arrayKey] = true;
4494 if ( $fallbackHeadline !== false && isset( $refers[$fallbackArrayKey] ) ) {
4495 // phpcs:ignore Generic.Formatting.DisallowMultipleStatements
4496 for ( $i = 2; isset( $refers["${fallbackArrayKey}_$i"] ); ++
$i );
4497 $fallbackAnchor .= "_$i";
4498 $refers["${fallbackArrayKey}_$i"] = true;
4500 $refers[$fallbackArrayKey] = true;
4503 # Don't number the heading if it is the only one (looks silly)
4504 if ( count( $matches[3] ) > 1 && $this->mOptions
->getNumberHeadings() ) {
4505 # the two are different if the line contains a link
4506 $headline = Html
::element(
4508 [ 'class' => 'mw-headline-number' ],
4510 ) . ' ' . $headline;
4513 if ( $enoughToc && ( !isset( $maxTocLevel ) ||
$toclevel < $maxTocLevel ) ) {
4514 $toc .= Linker
::tocLine( $linkAnchor, $tocline,
4515 $numbering, $toclevel, ( $isTemplate ?
false : $sectionIndex ) );
4518 # Add the section to the section tree
4519 # Find the DOM node for this header
4520 $noOffset = ( $isTemplate ||
$sectionIndex === false );
4521 while ( $node && !$noOffset ) {
4522 if ( $node->getName() === 'h' ) {
4523 $bits = $node->splitHeading();
4524 if ( $bits['i'] == $sectionIndex ) {
4528 $byteOffset +
= mb_strlen( $this->mStripState
->unstripBoth(
4529 $frame->expand( $node, PPFrame
::RECOVER_ORIG
) ) );
4530 $node = $node->getNextSibling();
4533 'toclevel' => $toclevel,
4536 'number' => $numbering,
4537 'index' => ( $isTemplate ?
'T-' : '' ) . $sectionIndex,
4538 'fromtitle' => $titleText,
4539 'byteoffset' => ( $noOffset ?
null : $byteOffset ),
4540 'anchor' => $anchor,
4543 # give headline the correct <h#> tag
4544 if ( $maybeShowEditLink && $sectionIndex !== false ) {
4545 // Output edit section links as markers with styles that can be customized by skins
4546 if ( $isTemplate ) {
4547 # Put a T flag in the section identifier, to indicate to extractSections()
4548 # that sections inside <includeonly> should be counted.
4549 $editsectionPage = $titleText;
4550 $editsectionSection = "T-$sectionIndex";
4551 $editsectionContent = null;
4553 $editsectionPage = $this->mTitle
->getPrefixedText();
4554 $editsectionSection = $sectionIndex;
4555 $editsectionContent = $headlineHint;
4557 // We use a bit of pesudo-xml for editsection markers. The
4558 // language converter is run later on. Using a UNIQ style marker
4559 // leads to the converter screwing up the tokens when it
4560 // converts stuff. And trying to insert strip tags fails too. At
4561 // this point all real inputted tags have already been escaped,
4562 // so we don't have to worry about a user trying to input one of
4563 // these markers directly. We use a page and section attribute
4564 // to stop the language converter from converting these
4565 // important bits of data, but put the headline hint inside a
4566 // content block because the language converter is supposed to
4567 // be able to convert that piece of data.
4568 // Gets replaced with html in ParserOutput::getText
4569 $editlink = '<mw:editsection page="' . htmlspecialchars( $editsectionPage );
4570 $editlink .= '" section="' . htmlspecialchars( $editsectionSection ) . '"';
4571 if ( $editsectionContent !== null ) {
4572 $editlink .= '>' . $editsectionContent . '</mw:editsection>';
4579 $head[$headlineCount] = Linker
::makeHeadline( $level,
4580 $matches['attrib'][$headlineCount], $anchor, $headline,
4581 $editlink, $fallbackAnchor );
4586 $this->setOutputType( $oldType );
4588 # Never ever show TOC if no headers
4589 if ( $numVisible < 1 ) {
4594 if ( $prevtoclevel > 0 && $prevtoclevel < $maxTocLevel ) {
4595 $toc .= Linker
::tocUnindent( $prevtoclevel - 1 );
4597 $toc = Linker
::tocList( $toc, $this->mOptions
->getUserLangObj() );
4598 $this->mOutput
->setTOCHTML( $toc );
4599 $toc = self
::TOC_START
. $toc . self
::TOC_END
;
4603 $this->mOutput
->setSections( $tocraw );
4606 # split up and insert constructed headlines
4607 $blocks = preg_split( '/<H[1-6].*?>[\s\S]*?<\/H[1-6]>/i', $text );
4610 // build an array of document sections
4612 foreach ( $blocks as $block ) {
4613 // $head is zero-based, sections aren't.
4614 if ( empty( $head[$i - 1] ) ) {
4615 $sections[$i] = $block;
4617 $sections[$i] = $head[$i - 1] . $block;
4621 * Send a hook, one per section.
4622 * The idea here is to be able to make section-level DIVs, but to do so in a
4623 * lower-impact, more correct way than r50769
4626 * $section : the section number
4627 * &$sectionContent : ref to the content of the section
4628 * $maybeShowEditLinks : boolean describing whether this section has an edit link
4630 Hooks
::run( 'ParserSectionCreate', [ $this, $i, &$sections[$i], $maybeShowEditLink ] );
4635 if ( $enoughToc && $isMain && !$this->mForceTocPosition
) {
4636 // append the TOC at the beginning
4637 // Top anchor now in skin
4638 $sections[0] .= $toc . "\n";
4641 $full .= implode( '', $sections );
4643 if ( $this->mForceTocPosition
) {
4644 return str_replace( '<!--MWTOC\'"-->', $toc, $full );
4651 * Transform wiki markup when saving a page by doing "\r\n" -> "\n"
4652 * conversion, substituting signatures, {{subst:}} templates, etc.
4654 * @param string $text The text to transform
4655 * @param Title $title The Title object for the current article
4656 * @param User $user The User object describing the current user
4657 * @param ParserOptions $options Parsing options
4658 * @param bool $clearState Whether to clear the parser state first
4659 * @return string The altered wiki markup
4661 public function preSaveTransform( $text, Title
$title, User
$user,
4662 ParserOptions
$options, $clearState = true
4664 if ( $clearState ) {
4665 $magicScopeVariable = $this->lock();
4667 $this->startParse( $title, $options, self
::OT_WIKI
, $clearState );
4668 $this->setUser( $user );
4670 // Strip U+0000 NULL (T159174)
4671 $text = str_replace( "\000", '', $text );
4673 // We still normalize line endings for backwards-compatibility
4674 // with other code that just calls PST, but this should already
4675 // be handled in TextContent subclasses
4676 $text = TextContent
::normalizeLineEndings( $text );
4678 if ( $options->getPreSaveTransform() ) {
4679 $text = $this->pstPass2( $text, $user );
4681 $text = $this->mStripState
->unstripBoth( $text );
4683 $this->setUser( null ); # Reset
4689 * Pre-save transform helper function
4691 * @param string $text
4696 private function pstPass2( $text, $user ) {
4697 # Note: This is the timestamp saved as hardcoded wikitext to the database, we use
4698 # $this->contLang here in order to give everyone the same signature and use the default one
4699 # rather than the one selected in each user's preferences. (see also T14815)
4700 $ts = $this->mOptions
->getTimestamp();
4701 $timestamp = MWTimestamp
::getLocalInstance( $ts );
4702 $ts = $timestamp->format( 'YmdHis' );
4703 $tzMsg = $timestamp->getTimezoneMessage()->inContentLanguage()->text();
4705 $d = $this->contLang
->timeanddate( $ts, false, false ) . " ($tzMsg)";
4707 # Variable replacement
4708 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
4709 $text = $this->replaceVariables( $text );
4711 # This works almost by chance, as the replaceVariables are done before the getUserSig(),
4712 # which may corrupt this parser instance via its wfMessage()->text() call-
4715 if ( strpos( $text, '~~~' ) !== false ) {
4716 $sigText = $this->getUserSig( $user );
4717 $text = strtr( $text, [
4719 '~~~~' => "$sigText $d",
4722 # The main two signature forms used above are time-sensitive
4723 $this->setOutputFlag( 'user-signature', 'User signature detected' );
4726 # Context links ("pipe tricks"): [[|name]] and [[name (context)|]]
4727 $tc = '[' . Title
::legalChars() . ']';
4728 $nc = '[ _0-9A-Za-z\x80-\xff-]'; # Namespaces can use non-ascii!
4730 // [[ns:page (context)|]]
4731 $p1 = "/\[\[(:?$nc+:|:|)($tc+?)( ?\\($tc+\\))\\|]]/";
4732 // [[ns:page(context)|]] (double-width brackets, added in r40257)
4733 $p4 = "/\[\[(:?$nc+:|:|)($tc+?)( ?($tc+))\\|]]/";
4734 // [[ns:page (context), context|]] (using either single or double-width comma)
4735 $p3 = "/\[\[(:?$nc+:|:|)($tc+?)( ?\\($tc+\\)|)((?:, |,)$tc+|)\\|]]/";
4736 // [[|page]] (reverse pipe trick: add context from page title)
4737 $p2 = "/\[\[\\|($tc+)]]/";
4739 # try $p1 first, to turn "[[A, B (C)|]]" into "[[A, B (C)|A, B]]"
4740 $text = preg_replace( $p1, '[[\\1\\2\\3|\\2]]', $text );
4741 $text = preg_replace( $p4, '[[\\1\\2\\3|\\2]]', $text );
4742 $text = preg_replace( $p3, '[[\\1\\2\\3\\4|\\2]]', $text );
4744 $t = $this->mTitle
->getText();
4746 if ( preg_match( "/^($nc+:|)$tc+?( \\($tc+\\))$/", $t, $m ) ) {
4747 $text = preg_replace( $p2, "[[$m[1]\\1$m[2]|\\1]]", $text );
4748 } elseif ( preg_match( "/^($nc+:|)$tc+?(, $tc+|)$/", $t, $m ) && "$m[1]$m[2]" != '' ) {
4749 $text = preg_replace( $p2, "[[$m[1]\\1$m[2]|\\1]]", $text );
4751 # if there's no context, don't bother duplicating the title
4752 $text = preg_replace( $p2, '[[\\1]]', $text );
4759 * Fetch the user's signature text, if any, and normalize to
4760 * validated, ready-to-insert wikitext.
4761 * If you have pre-fetched the nickname or the fancySig option, you can
4762 * specify them here to save a database query.
4763 * Do not reuse this parser instance after calling getUserSig(),
4764 * as it may have changed.
4766 * @param User &$user
4767 * @param string|bool $nickname Nickname to use or false to use user's default nickname
4768 * @param bool|null $fancySig whether the nicknname is the complete signature
4769 * or null to use default value
4772 public function getUserSig( &$user, $nickname = false, $fancySig = null ) {
4773 $username = $user->getName();
4775 # If not given, retrieve from the user object.
4776 if ( $nickname === false ) {
4777 $nickname = $user->getOption( 'nickname' );
4780 if ( is_null( $fancySig ) ) {
4781 $fancySig = $user->getBoolOption( 'fancysig' );
4784 $nickname = $nickname == null ?
$username : $nickname;
4786 if ( mb_strlen( $nickname ) > $this->svcOptions
->get( 'MaxSigChars' ) ) {
4787 $nickname = $username;
4788 $this->logger
->debug( __METHOD__
. ": $username has overlong signature." );
4789 } elseif ( $fancySig !== false ) {
4790 # Sig. might contain markup; validate this
4791 if ( $this->validateSig( $nickname ) !== false ) {
4792 # Validated; clean up (if needed) and return it
4793 return $this->cleanSig( $nickname, true );
4795 # Failed to validate; fall back to the default
4796 $nickname = $username;
4797 $this->logger
->debug( __METHOD__
. ": $username has bad XML tags in signature." );
4801 # Make sure nickname doesnt get a sig in a sig
4802 $nickname = self
::cleanSigInSig( $nickname );
4804 # If we're still here, make it a link to the user page
4805 $userText = wfEscapeWikiText( $username );
4806 $nickText = wfEscapeWikiText( $nickname );
4807 $msgName = $user->isAnon() ?
'signature-anon' : 'signature';
4809 return wfMessage( $msgName, $userText, $nickText )->inContentLanguage()
4810 ->title( $this->getTitle() )->text();
4814 * Check that the user's signature contains no bad XML
4816 * @param string $text
4817 * @return string|bool An expanded string, or false if invalid.
4819 public function validateSig( $text ) {
4820 return Xml
::isWellFormedXmlFragment( $text ) ?
$text : false;
4824 * Clean up signature text
4826 * 1) Strip 3, 4 or 5 tildes out of signatures @see cleanSigInSig
4827 * 2) Substitute all transclusions
4829 * @param string $text
4830 * @param bool $parsing Whether we're cleaning (preferences save) or parsing
4831 * @return string Signature text
4833 public function cleanSig( $text, $parsing = false ) {
4836 $magicScopeVariable = $this->lock();
4837 $this->startParse( $wgTitle, new ParserOptions
, self
::OT_PREPROCESS
, true );
4840 # Option to disable this feature
4841 if ( !$this->mOptions
->getCleanSignatures() ) {
4845 # @todo FIXME: Regex doesn't respect extension tags or nowiki
4846 # => Move this logic to braceSubstitution()
4847 $substWord = $this->magicWordFactory
->get( 'subst' );
4848 $substRegex = '/\{\{(?!(?:' . $substWord->getBaseRegex() . '))/x' . $substWord->getRegexCase();
4849 $substText = '{{' . $substWord->getSynonym( 0 );
4851 $text = preg_replace( $substRegex, $substText, $text );
4852 $text = self
::cleanSigInSig( $text );
4853 $dom = $this->preprocessToDom( $text );
4854 $frame = $this->getPreprocessor()->newFrame();
4855 $text = $frame->expand( $dom );
4858 $text = $this->mStripState
->unstripBoth( $text );
4865 * Strip 3, 4 or 5 tildes out of signatures.
4867 * @param string $text
4868 * @return string Signature text with /~{3,5}/ removed
4870 public static function cleanSigInSig( $text ) {
4871 $text = preg_replace( '/~{3,5}/', '', $text );
4876 * Set up some variables which are usually set up in parse()
4877 * so that an external function can call some class members with confidence
4879 * @param Title|null $title
4880 * @param ParserOptions $options
4881 * @param int $outputType
4882 * @param bool $clearState
4883 * @param int|null $revId
4885 public function startExternalParse( Title
$title = null, ParserOptions
$options,
4886 $outputType, $clearState = true, $revId = null
4888 $this->startParse( $title, $options, $outputType, $clearState );
4889 if ( $revId !== null ) {
4890 $this->mRevisionId
= $revId;
4895 * @param Title|null $title
4896 * @param ParserOptions $options
4897 * @param int $outputType
4898 * @param bool $clearState
4900 private function startParse( Title
$title = null, ParserOptions
$options,
4901 $outputType, $clearState = true
4903 $this->setTitle( $title );
4904 $this->mOptions
= $options;
4905 $this->setOutputType( $outputType );
4906 if ( $clearState ) {
4907 $this->clearState();
4912 * Wrapper for preprocess()
4914 * @param string $text The text to preprocess
4915 * @param ParserOptions $options
4916 * @param Title|null $title Title object or null to use $wgTitle
4919 public function transformMsg( $text, $options, $title = null ) {
4920 static $executing = false;
4922 # Guard against infinite recursion
4933 $text = $this->preprocess( $text, $title, $options );
4940 * Create an HTML-style tag, e.g. "<yourtag>special text</yourtag>"
4941 * The callback should have the following form:
4942 * function myParserHook( $text, $params, $parser, $frame ) { ... }
4944 * Transform and return $text. Use $parser for any required context, e.g. use
4945 * $parser->getTitle() and $parser->getOptions() not $wgTitle or $wgOut->mParserOptions
4947 * Hooks may return extended information by returning an array, of which the
4948 * first numbered element (index 0) must be the return string, and all other
4949 * entries are extracted into local variables within an internal function
4950 * in the Parser class.
4952 * This interface (introduced r61913) appears to be undocumented, but
4953 * 'markerType' is used by some core tag hooks to override which strip
4954 * array their results are placed in. **Use great caution if attempting
4955 * this interface, as it is not documented and injudicious use could smash
4956 * private variables.**
4958 * @param string $tag The tag to use, e.g. 'hook' for "<hook>"
4959 * @param callable $callback The callback function (and object) to use for the tag
4960 * @throws MWException
4961 * @return callable|null The old value of the mTagHooks array associated with the hook
4963 public function setHook( $tag, callable
$callback ) {
4964 $tag = strtolower( $tag );
4965 if ( preg_match( '/[<>\r\n]/', $tag, $m ) ) {
4966 throw new MWException( "Invalid character {$m[0]} in setHook('$tag', ...) call" );
4968 $oldVal = $this->mTagHooks
[$tag] ??
null;
4969 $this->mTagHooks
[$tag] = $callback;
4970 if ( !in_array( $tag, $this->mStripList
) ) {
4971 $this->mStripList
[] = $tag;
4978 * As setHook(), but letting the contents be parsed.
4980 * Transparent tag hooks are like regular XML-style tag hooks, except they
4981 * operate late in the transformation sequence, on HTML instead of wikitext.
4983 * This is probably obsoleted by things dealing with parser frames?
4984 * The only extension currently using it is geoserver.
4987 * @todo better document or deprecate this
4989 * @param string $tag The tag to use, e.g. 'hook' for "<hook>"
4990 * @param callable $callback The callback function (and object) to use for the tag
4991 * @throws MWException
4992 * @return callable|null The old value of the mTagHooks array associated with the hook
4994 public function setTransparentTagHook( $tag, callable
$callback ) {
4995 $tag = strtolower( $tag );
4996 if ( preg_match( '/[<>\r\n]/', $tag, $m ) ) {
4997 throw new MWException( "Invalid character {$m[0]} in setTransparentHook('$tag', ...) call" );
4999 $oldVal = $this->mTransparentTagHooks
[$tag] ??
null;
5000 $this->mTransparentTagHooks
[$tag] = $callback;
5006 * Remove all tag hooks
5008 public function clearTagHooks() {
5009 $this->mTagHooks
= [];
5010 $this->mFunctionTagHooks
= [];
5011 $this->mStripList
= $this->mDefaultStripList
;
5015 * Create a function, e.g. {{sum:1|2|3}}
5016 * The callback function should have the form:
5017 * function myParserFunction( &$parser, $arg1, $arg2, $arg3 ) { ... }
5019 * Or with Parser::SFH_OBJECT_ARGS:
5020 * function myParserFunction( $parser, $frame, $args ) { ... }
5022 * The callback may either return the text result of the function, or an array with the text
5023 * in element 0, and a number of flags in the other elements. The names of the flags are
5024 * specified in the keys. Valid flags are:
5025 * found The text returned is valid, stop processing the template. This
5027 * nowiki Wiki markup in the return value should be escaped
5028 * isHTML The returned text is HTML, armour it against wikitext transformation
5030 * @param string $id The magic word ID
5031 * @param callable $callback The callback function (and object) to use
5032 * @param int $flags A combination of the following flags:
5033 * Parser::SFH_NO_HASH No leading hash, i.e. {{plural:...}} instead of {{#if:...}}
5035 * Parser::SFH_OBJECT_ARGS Pass the template arguments as PPNode objects instead of text.
5036 * This allows for conditional expansion of the parse tree, allowing you to eliminate dead
5037 * branches and thus speed up parsing. It is also possible to analyse the parse tree of
5038 * the arguments, and to control the way they are expanded.
5040 * The $frame parameter is a PPFrame. This can be used to produce expanded text from the
5041 * arguments, for instance:
5042 * $text = isset( $args[0] ) ? $frame->expand( $args[0] ) : '';
5044 * For technical reasons, $args[0] is pre-expanded and will be a string. This may change in
5045 * future versions. Please call $frame->expand() on it anyway so that your code keeps
5046 * working if/when this is changed.
5048 * If you want whitespace to be trimmed from $args, you need to do it yourself, post-
5051 * Please read the documentation in includes/parser/Preprocessor.php for more information
5052 * about the methods available in PPFrame and PPNode.
5054 * @throws MWException
5055 * @return string|callable The old callback function for this name, if any
5057 public function setFunctionHook( $id, callable
$callback, $flags = 0 ) {
5058 $oldVal = isset( $this->mFunctionHooks
[$id] ) ?
$this->mFunctionHooks
[$id][0] : null;
5059 $this->mFunctionHooks
[$id] = [ $callback, $flags ];
5061 # Add to function cache
5062 $mw = $this->magicWordFactory
->get( $id );
5064 throw new MWException( __METHOD__
. '() expecting a magic word identifier.' );
5067 $synonyms = $mw->getSynonyms();
5068 $sensitive = intval( $mw->isCaseSensitive() );
5070 foreach ( $synonyms as $syn ) {
5072 if ( !$sensitive ) {
5073 $syn = $this->contLang
->lc( $syn );
5076 if ( !( $flags & self
::SFH_NO_HASH
) ) {
5079 # Remove trailing colon
5080 if ( substr( $syn, -1, 1 ) === ':' ) {
5081 $syn = substr( $syn, 0, -1 );
5083 $this->mFunctionSynonyms
[$sensitive][$syn] = $id;
5089 * Get all registered function hook identifiers
5093 public function getFunctionHooks() {
5094 $this->firstCallInit();
5095 return array_keys( $this->mFunctionHooks
);
5099 * Create a tag function, e.g. "<test>some stuff</test>".
5100 * Unlike tag hooks, tag functions are parsed at preprocessor level.
5101 * Unlike parser functions, their content is not preprocessed.
5102 * @param string $tag
5103 * @param callable $callback
5105 * @throws MWException
5108 public function setFunctionTagHook( $tag, callable
$callback, $flags ) {
5109 $tag = strtolower( $tag );
5110 if ( preg_match( '/[<>\r\n]/', $tag, $m ) ) {
5111 throw new MWException( "Invalid character {$m[0]} in setFunctionTagHook('$tag', ...) call" );
5113 $old = $this->mFunctionTagHooks
[$tag] ??
null;
5114 $this->mFunctionTagHooks
[$tag] = [ $callback, $flags ];
5116 if ( !in_array( $tag, $this->mStripList
) ) {
5117 $this->mStripList
[] = $tag;
5124 * Replace "<!--LINK-->" link placeholders with actual links, in the buffer
5125 * Placeholders created in Linker::link()
5127 * @param string &$text
5128 * @param int $options
5130 public function replaceLinkHolders( &$text, $options = 0 ) {
5131 $this->mLinkHolders
->replace( $text );
5135 * Replace "<!--LINK-->" link placeholders with plain text of links
5136 * (not HTML-formatted).
5138 * @param string $text
5141 public function replaceLinkHoldersText( $text ) {
5142 return $this->mLinkHolders
->replaceText( $text );
5146 * Renders an image gallery from a text with one line per image.
5147 * text labels may be given by using |-style alternative text. E.g.
5148 * Image:one.jpg|The number "1"
5149 * Image:tree.jpg|A tree
5150 * given as text will return the HTML of a gallery with two images,
5151 * labeled 'The number "1"' and
5154 * @param string $text
5155 * @param array $params
5156 * @return string HTML
5158 public function renderImageGallery( $text, $params ) {
5160 if ( isset( $params['mode'] ) ) {
5161 $mode = $params['mode'];
5165 $ig = ImageGalleryBase
::factory( $mode );
5166 } catch ( Exception
$e ) {
5167 // If invalid type set, fallback to default.
5168 $ig = ImageGalleryBase
::factory( false );
5171 $ig->setContextTitle( $this->mTitle
);
5172 $ig->setShowBytes( false );
5173 $ig->setShowDimensions( false );
5174 $ig->setShowFilename( false );
5175 $ig->setParser( $this );
5176 $ig->setHideBadImages();
5177 $ig->setAttributes( Sanitizer
::validateTagAttributes( $params, 'ul' ) );
5179 if ( isset( $params['showfilename'] ) ) {
5180 $ig->setShowFilename( true );
5182 $ig->setShowFilename( false );
5184 if ( isset( $params['caption'] ) ) {
5185 // NOTE: We aren't passing a frame here or below. Frame info
5186 // is currently opaque to Parsoid, which acts on OT_PREPROCESS.
5187 // See T107332#4030581
5188 $caption = $this->recursiveTagParse( $params['caption'] );
5189 $ig->setCaptionHtml( $caption );
5191 if ( isset( $params['perrow'] ) ) {
5192 $ig->setPerRow( $params['perrow'] );
5194 if ( isset( $params['widths'] ) ) {
5195 $ig->setWidths( $params['widths'] );
5197 if ( isset( $params['heights'] ) ) {
5198 $ig->setHeights( $params['heights'] );
5200 $ig->setAdditionalOptions( $params );
5202 // Avoid PHP 7.1 warning from passing $this by reference
5204 Hooks
::run( 'BeforeParserrenderImageGallery', [ &$parser, &$ig ] );
5206 $lines = StringUtils
::explode( "\n", $text );
5207 foreach ( $lines as $line ) {
5208 # match lines like these:
5209 # Image:someimage.jpg|This is some image
5211 preg_match( "/^([^|]+)(\\|(.*))?$/", $line, $matches );
5213 if ( count( $matches ) == 0 ) {
5217 if ( strpos( $matches[0], '%' ) !== false ) {
5218 $matches[1] = rawurldecode( $matches[1] );
5220 $title = Title
::newFromText( $matches[1], NS_FILE
);
5221 if ( is_null( $title ) ) {
5222 # Bogus title. Ignore these so we don't bomb out later.
5226 # We need to get what handler the file uses, to figure out parameters.
5227 # Note, a hook can overide the file name, and chose an entirely different
5228 # file (which potentially could be of a different type and have different handler).
5231 Hooks
::run( 'BeforeParserFetchFileAndTitle',
5232 [ $this, $title, &$options, &$descQuery ] );
5233 # Don't register it now, as TraditionalImageGallery does that later.
5234 $file = $this->fetchFileNoRegister( $title, $options );
5235 $handler = $file ?
$file->getHandler() : false;
5238 'img_alt' => 'gallery-internal-alt',
5239 'img_link' => 'gallery-internal-link',
5242 $paramMap +
= $handler->getParamMap();
5243 // We don't want people to specify per-image widths.
5244 // Additionally the width parameter would need special casing anyhow.
5245 unset( $paramMap['img_width'] );
5248 $mwArray = $this->magicWordFactory
->newArray( array_keys( $paramMap ) );
5253 $handlerOptions = [];
5254 if ( isset( $matches[3] ) ) {
5255 // look for an |alt= definition while trying not to break existing
5256 // captions with multiple pipes (|) in it, until a more sensible grammar
5257 // is defined for images in galleries
5259 // FIXME: Doing recursiveTagParse at this stage, and the trim before
5260 // splitting on '|' is a bit odd, and different from makeImage.
5261 $matches[3] = $this->recursiveTagParse( trim( $matches[3] ) );
5262 // Protect LanguageConverter markup
5263 $parameterMatches = StringUtils
::delimiterExplode(
5264 '-{', '}-', '|', $matches[3], true /* nested */
5267 foreach ( $parameterMatches as $parameterMatch ) {
5268 list( $magicName, $match ) = $mwArray->matchVariableStartToEnd( $parameterMatch );
5270 $paramName = $paramMap[$magicName];
5272 switch ( $paramName ) {
5273 case 'gallery-internal-alt':
5274 $alt = $this->stripAltText( $match, false );
5276 case 'gallery-internal-link':
5277 $linkValue = $this->stripAltText( $match, false );
5278 if ( preg_match( '/^-{R|(.*)}-$/', $linkValue ) ) {
5279 // Result of LanguageConverter::markNoConversion
5280 // invoked on an external link.
5281 $linkValue = substr( $linkValue, 4, -2 );
5283 list( $type, $target ) = $this->parseLinkParameter( $linkValue );
5284 if ( $type === 'link-url' ) {
5286 $this->mOutput
->addExternalLink( $target );
5287 } elseif ( $type === 'link-title' ) {
5288 $link = $target->getLinkURL();
5289 $this->mOutput
->addLink( $target );
5293 // Must be a handler specific parameter.
5294 if ( $handler->validateParam( $paramName, $match ) ) {
5295 $handlerOptions[$paramName] = $match;
5297 // Guess not, consider it as caption.
5298 $this->logger
->debug(
5299 "$parameterMatch failed parameter validation" );
5300 $label = $parameterMatch;
5306 $label = $parameterMatch;
5311 $ig->add( $title, $label, $alt, $link, $handlerOptions );
5313 $html = $ig->toHTML();
5314 Hooks
::run( 'AfterParserFetchFileAndTitle', [ $this, $ig, &$html ] );
5319 * @param MediaHandler $handler
5322 public function getImageParams( $handler ) {
5324 $handlerClass = get_class( $handler );
5328 if ( !isset( $this->mImageParams
[$handlerClass] ) ) {
5329 # Initialise static lists
5330 static $internalParamNames = [
5331 'horizAlign' => [ 'left', 'right', 'center', 'none' ],
5332 'vertAlign' => [ 'baseline', 'sub', 'super', 'top', 'text-top', 'middle',
5333 'bottom', 'text-bottom' ],
5334 'frame' => [ 'thumbnail', 'manualthumb', 'framed', 'frameless',
5335 'upright', 'border', 'link', 'alt', 'class' ],
5337 static $internalParamMap;
5338 if ( !$internalParamMap ) {
5339 $internalParamMap = [];
5340 foreach ( $internalParamNames as $type => $names ) {
5341 foreach ( $names as $name ) {
5342 // For grep: img_left, img_right, img_center, img_none,
5343 // img_baseline, img_sub, img_super, img_top, img_text_top, img_middle,
5344 // img_bottom, img_text_bottom,
5345 // img_thumbnail, img_manualthumb, img_framed, img_frameless, img_upright,
5346 // img_border, img_link, img_alt, img_class
5347 $magicName = str_replace( '-', '_', "img_$name" );
5348 $internalParamMap[$magicName] = [ $type, $name ];
5353 # Add handler params
5354 $paramMap = $internalParamMap;
5356 $handlerParamMap = $handler->getParamMap();
5357 foreach ( $handlerParamMap as $magic => $paramName ) {
5358 $paramMap[$magic] = [ 'handler', $paramName ];
5361 $this->mImageParams
[$handlerClass] = $paramMap;
5362 $this->mImageParamsMagicArray
[$handlerClass] =
5363 $this->magicWordFactory
->newArray( array_keys( $paramMap ) );
5365 return [ $this->mImageParams
[$handlerClass], $this->mImageParamsMagicArray
[$handlerClass] ];
5369 * Parse image options text and use it to make an image
5371 * @param Title $title
5372 * @param string $options
5373 * @param LinkHolderArray|bool $holders
5374 * @return string HTML
5376 public function makeImage( $title, $options, $holders = false ) {
5377 # Check if the options text is of the form "options|alt text"
5379 # * thumbnail make a thumbnail with enlarge-icon and caption, alignment depends on lang
5380 # * left no resizing, just left align. label is used for alt= only
5381 # * right same, but right aligned
5382 # * none same, but not aligned
5383 # * ___px scale to ___ pixels width, no aligning. e.g. use in taxobox
5384 # * center center the image
5385 # * frame Keep original image size, no magnify-button.
5386 # * framed Same as "frame"
5387 # * frameless like 'thumb' but without a frame. Keeps user preferences for width
5388 # * upright reduce width for upright images, rounded to full __0 px
5389 # * border draw a 1px border around the image
5390 # * alt Text for HTML alt attribute (defaults to empty)
5391 # * class Set a class for img node
5392 # * link Set the target of the image link. Can be external, interwiki, or local
5393 # vertical-align values (no % or length right now):
5403 # Protect LanguageConverter markup when splitting into parts
5404 $parts = StringUtils
::delimiterExplode(
5405 '-{', '}-', '|', $options, true /* allow nesting */
5408 # Give extensions a chance to select the file revision for us
5411 Hooks
::run( 'BeforeParserFetchFileAndTitle',
5412 [ $this, $title, &$options, &$descQuery ] );
5413 # Fetch and register the file (file title may be different via hooks)
5414 list( $file, $title ) = $this->fetchFileAndTitle( $title, $options );
5417 $handler = $file ?
$file->getHandler() : false;
5419 list( $paramMap, $mwArray ) = $this->getImageParams( $handler );
5422 $this->addTrackingCategory( 'broken-file-category' );
5425 # Process the input parameters
5427 $params = [ 'frame' => [], 'handler' => [],
5428 'horizAlign' => [], 'vertAlign' => [] ];
5429 $seenformat = false;
5430 foreach ( $parts as $part ) {
5431 $part = trim( $part );
5432 list( $magicName, $value ) = $mwArray->matchVariableStartToEnd( $part );
5434 if ( isset( $paramMap[$magicName] ) ) {
5435 list( $type, $paramName ) = $paramMap[$magicName];
5437 # Special case; width and height come in one variable together
5438 if ( $type === 'handler' && $paramName === 'width' ) {
5439 $parsedWidthParam = self
::parseWidthParam( $value );
5440 if ( isset( $parsedWidthParam['width'] ) ) {
5441 $width = $parsedWidthParam['width'];
5442 if ( $handler->validateParam( 'width', $width ) ) {
5443 $params[$type]['width'] = $width;
5447 if ( isset( $parsedWidthParam['height'] ) ) {
5448 $height = $parsedWidthParam['height'];
5449 if ( $handler->validateParam( 'height', $height ) ) {
5450 $params[$type]['height'] = $height;
5454 # else no validation -- T15436
5456 if ( $type === 'handler' ) {
5457 # Validate handler parameter
5458 $validated = $handler->validateParam( $paramName, $value );
5460 # Validate internal parameters
5461 switch ( $paramName ) {
5465 # @todo FIXME: Possibly check validity here for
5466 # manualthumb? downstream behavior seems odd with
5467 # missing manual thumbs.
5469 $value = $this->stripAltText( $value, $holders );
5472 list( $paramName, $value ) =
5473 $this->parseLinkParameter(
5474 $this->stripAltText( $value, $holders )
5478 if ( $paramName === 'no-link' ) {
5481 if ( ( $paramName === 'link-url' ) && $this->mOptions
->getExternalLinkTarget() ) {
5482 $params[$type]['link-target'] = $this->mOptions
->getExternalLinkTarget();
5489 // use first appearing option, discard others.
5490 $validated = !$seenformat;
5494 # Most other things appear to be empty or numeric...
5495 $validated = ( $value === false ||
is_numeric( trim( $value ) ) );
5500 $params[$type][$paramName] = $value;
5504 if ( !$validated ) {
5509 # Process alignment parameters
5510 if ( $params['horizAlign'] ) {
5511 $params['frame']['align'] = key( $params['horizAlign'] );
5513 if ( $params['vertAlign'] ) {
5514 $params['frame']['valign'] = key( $params['vertAlign'] );
5517 $params['frame']['caption'] = $caption;
5519 # Will the image be presented in a frame, with the caption below?
5520 $imageIsFramed = isset( $params['frame']['frame'] )
5521 ||
isset( $params['frame']['framed'] )
5522 ||
isset( $params['frame']['thumbnail'] )
5523 ||
isset( $params['frame']['manualthumb'] );
5525 # In the old days, [[Image:Foo|text...]] would set alt text. Later it
5526 # came to also set the caption, ordinary text after the image -- which
5527 # makes no sense, because that just repeats the text multiple times in
5528 # screen readers. It *also* came to set the title attribute.
5529 # Now that we have an alt attribute, we should not set the alt text to
5530 # equal the caption: that's worse than useless, it just repeats the
5531 # text. This is the framed/thumbnail case. If there's no caption, we
5532 # use the unnamed parameter for alt text as well, just for the time be-
5533 # ing, if the unnamed param is set and the alt param is not.
5534 # For the future, we need to figure out if we want to tweak this more,
5535 # e.g., introducing a title= parameter for the title; ignoring the un-
5536 # named parameter entirely for images without a caption; adding an ex-
5537 # plicit caption= parameter and preserving the old magic unnamed para-
5539 if ( $imageIsFramed ) { # Framed image
5540 if ( $caption === '' && !isset( $params['frame']['alt'] ) ) {
5541 # No caption or alt text, add the filename as the alt text so
5542 # that screen readers at least get some description of the image
5543 $params['frame']['alt'] = $title->getText();
5545 # Do not set $params['frame']['title'] because tooltips don't make sense
5547 } else { # Inline image
5548 if ( !isset( $params['frame']['alt'] ) ) {
5549 # No alt text, use the "caption" for the alt text
5550 if ( $caption !== '' ) {
5551 $params['frame']['alt'] = $this->stripAltText( $caption, $holders );
5553 # No caption, fall back to using the filename for the
5555 $params['frame']['alt'] = $title->getText();
5558 # Use the "caption" for the tooltip text
5559 $params['frame']['title'] = $this->stripAltText( $caption, $holders );
5561 $params['handler']['targetlang'] = $this->getTargetLanguage()->getCode();
5563 Hooks
::run( 'ParserMakeImageParams', [ $title, $file, &$params, $this ] );
5565 # Linker does the rest
5566 $time = $options['time'] ??
false;
5567 $ret = Linker
::makeImageLink( $this, $title, $file, $params['frame'], $params['handler'],
5568 $time, $descQuery, $this->mOptions
->getThumbSize() );
5570 # Give the handler a chance to modify the parser object
5572 $handler->parserTransformHook( $this, $file );
5579 * Parse the value of 'link' parameter in image syntax (`[[File:Foo.jpg|link=<value>]]`).
5581 * Adds an entry to appropriate link tables.
5584 * @param string $value
5585 * @return array of `[ type, target ]`, where:
5586 * - `type` is one of:
5587 * - `null`: Given value is not a valid link target, use default
5588 * - `'no-link'`: Given value is empty, do not generate a link
5589 * - `'link-url'`: Given value is a valid external link
5590 * - `'link-title'`: Given value is a valid internal link
5592 * - When `type` is `null` or `'no-link'`: `false`
5593 * - When `type` is `'link-url'`: URL string corresponding to given value
5594 * - When `type` is `'link-title'`: Title object corresponding to given value
5596 public function parseLinkParameter( $value ) {
5597 $chars = self
::EXT_LINK_URL_CLASS
;
5598 $addr = self
::EXT_LINK_ADDR
;
5599 $prots = $this->mUrlProtocols
;
5602 if ( $value === '' ) {
5604 } elseif ( preg_match( "/^((?i)$prots)/", $value ) ) {
5605 if ( preg_match( "/^((?i)$prots)$addr$chars*$/u", $value, $m ) ) {
5606 $this->mOutput
->addExternalLink( $value );
5611 $linkTitle = Title
::newFromText( $value );
5613 $this->mOutput
->addLink( $linkTitle );
5614 $type = 'link-title';
5615 $target = $linkTitle;
5618 return [ $type, $target ];
5622 * @param string $caption
5623 * @param LinkHolderArray|bool $holders
5624 * @return mixed|string
5626 protected function stripAltText( $caption, $holders ) {
5627 # Strip bad stuff out of the title (tooltip). We can't just use
5628 # replaceLinkHoldersText() here, because if this function is called
5629 # from replaceInternalLinks2(), mLinkHolders won't be up-to-date.
5631 $tooltip = $holders->replaceText( $caption );
5633 $tooltip = $this->replaceLinkHoldersText( $caption );
5636 # make sure there are no placeholders in thumbnail attributes
5637 # that are later expanded to html- so expand them now and
5639 $tooltip = $this->mStripState
->unstripBoth( $tooltip );
5640 # Compatibility hack! In HTML certain entity references not terminated
5641 # by a semicolon are decoded (but not if we're in an attribute; that's
5642 # how link URLs get away without properly escaping & in queries).
5643 # But wikitext has always required semicolon-termination of entities,
5644 # so encode & where needed to avoid decode of semicolon-less entities.
5646 # https://www.w3.org/TR/html5/syntax.html#named-character-references
5647 # T210437 discusses moving this workaround to Sanitizer::stripAllTags.
5648 $tooltip = preg_replace( "/
5649 & # 1. entity prefix
5650 (?= # 2. followed by:
5651 (?: # a. one of the legacy semicolon-less named entities
5652 A(?:Elig|MP|acute|circ|grave|ring|tilde|uml)|
5653 C(?:OPY|cedil)|E(?:TH|acute|circ|grave|uml)|
5654 GT|I(?:acute|circ|grave|uml)|LT|Ntilde|
5655 O(?:acute|circ|grave|slash|tilde|uml)|QUOT|REG|THORN|
5656 U(?:acute|circ|grave|uml)|Yacute|
5657 a(?:acute|c(?:irc|ute)|elig|grave|mp|ring|tilde|uml)|brvbar|
5658 c(?:cedil|edil|urren)|cent(?!erdot;)|copy(?!sr;)|deg|
5659 divide(?!ontimes;)|e(?:acute|circ|grave|th|uml)|
5660 frac(?:1(?:2|4)|34)|
5661 gt(?!c(?:c|ir)|dot|lPar|quest|r(?:a(?:pprox|rr)|dot|eq(?:less|qless)|less|sim);)|
5662 i(?:acute|circ|excl|grave|quest|uml)|laquo|
5663 lt(?!c(?:c|ir)|dot|hree|imes|larr|quest|r(?:Par|i(?:e|f|));)|
5664 m(?:acr|i(?:cro|ddot))|n(?:bsp|tilde)|
5665 not(?!in(?:E|dot|v(?:a|b|c)|)|ni(?:v(?:a|b|c)|);)|
5666 o(?:acute|circ|grave|rd(?:f|m)|slash|tilde|uml)|
5667 p(?:lusmn|ound)|para(?!llel;)|quot|r(?:aquo|eg)|
5668 s(?:ect|hy|up(?:1|2|3)|zlig)|thorn|times(?!b(?:ar|)|d;)|
5669 u(?:acute|circ|grave|ml|uml)|y(?:acute|en|uml)
5671 (?:[^;]|$)) # b. and not followed by a semicolon
5672 # S = study, for efficiency
5673 /Sx", '&', $tooltip );
5674 $tooltip = Sanitizer
::stripAllTags( $tooltip );
5680 * Set a flag in the output object indicating that the content is dynamic and
5681 * shouldn't be cached.
5682 * @deprecated since 1.28; use getOutput()->updateCacheExpiry()
5684 public function disableCache() {
5685 $this->logger
->debug( "Parser output marked as uncacheable." );
5686 if ( !$this->mOutput
) {
5687 throw new MWException( __METHOD__
.
5688 " can only be called when actually parsing something" );
5690 $this->mOutput
->updateCacheExpiry( 0 ); // new style, for consistency
5694 * Callback from the Sanitizer for expanding items found in HTML attribute
5695 * values, so they can be safely tested and escaped.
5697 * @param string &$text
5698 * @param bool|PPFrame $frame
5701 public function attributeStripCallback( &$text, $frame = false ) {
5702 $text = $this->replaceVariables( $text, $frame );
5703 $text = $this->mStripState
->unstripBoth( $text );
5712 public function getTags() {
5713 $this->firstCallInit();
5715 array_keys( $this->mTransparentTagHooks
),
5716 array_keys( $this->mTagHooks
),
5717 array_keys( $this->mFunctionTagHooks
)
5725 public function getFunctionSynonyms() {
5726 $this->firstCallInit();
5727 return $this->mFunctionSynonyms
;
5734 public function getUrlProtocols() {
5735 return $this->mUrlProtocols
;
5739 * Replace transparent tags in $text with the values given by the callbacks.
5741 * Transparent tag hooks are like regular XML-style tag hooks, except they
5742 * operate late in the transformation sequence, on HTML instead of wikitext.
5744 * @param string $text
5748 public function replaceTransparentTags( $text ) {
5750 $elements = array_keys( $this->mTransparentTagHooks
);
5751 $text = self
::extractTagsAndParams( $elements, $text, $matches );
5754 foreach ( $matches as $marker => $data ) {
5755 list( $element, $content, $params, $tag ) = $data;
5756 $tagName = strtolower( $element );
5757 if ( isset( $this->mTransparentTagHooks
[$tagName] ) ) {
5758 $output = call_user_func_array(
5759 $this->mTransparentTagHooks
[$tagName],
5760 [ $content, $params, $this ]
5765 $replacements[$marker] = $output;
5767 return strtr( $text, $replacements );
5771 * Break wikitext input into sections, and either pull or replace
5772 * some particular section's text.
5774 * External callers should use the getSection and replaceSection methods.
5776 * @param string $text Page wikitext
5777 * @param string|int $sectionId A section identifier string of the form:
5778 * "<flag1> - <flag2> - ... - <section number>"
5780 * Currently the only recognised flag is "T", which means the target section number
5781 * was derived during a template inclusion parse, in other words this is a template
5782 * section edit link. If no flags are given, it was an ordinary section edit link.
5783 * This flag is required to avoid a section numbering mismatch when a section is
5784 * enclosed by "<includeonly>" (T8563).
5786 * The section number 0 pulls the text before the first heading; other numbers will
5787 * pull the given section along with its lower-level subsections. If the section is
5788 * not found, $mode=get will return $newtext, and $mode=replace will return $text.
5790 * Section 0 is always considered to exist, even if it only contains the empty
5791 * string. If $text is the empty string and section 0 is replaced, $newText is
5794 * @param string $mode One of "get" or "replace"
5795 * @param string $newText Replacement text for section data.
5796 * @return string For "get", the extracted section text.
5797 * for "replace", the whole page with the section replaced.
5799 private function extractSections( $text, $sectionId, $mode, $newText = '' ) {
5800 global $wgTitle; # not generally used but removes an ugly failure mode
5802 $magicScopeVariable = $this->lock();
5803 $this->startParse( $wgTitle, new ParserOptions
, self
::OT_PLAIN
, true );
5805 $frame = $this->getPreprocessor()->newFrame();
5807 # Process section extraction flags
5809 $sectionParts = explode( '-', $sectionId );
5810 $sectionIndex = array_pop( $sectionParts );
5811 foreach ( $sectionParts as $part ) {
5812 if ( $part === 'T' ) {
5813 $flags |
= self
::PTD_FOR_INCLUSION
;
5817 # Check for empty input
5818 if ( strval( $text ) === '' ) {
5819 # Only sections 0 and T-0 exist in an empty document
5820 if ( $sectionIndex == 0 ) {
5821 if ( $mode === 'get' ) {
5827 if ( $mode === 'get' ) {
5835 # Preprocess the text
5836 $root = $this->preprocessToDom( $text, $flags );
5838 # <h> nodes indicate section breaks
5839 # They can only occur at the top level, so we can find them by iterating the root's children
5840 $node = $root->getFirstChild();
5842 # Find the target section
5843 if ( $sectionIndex == 0 ) {
5844 # Section zero doesn't nest, level=big
5845 $targetLevel = 1000;
5848 if ( $node->getName() === 'h' ) {
5849 $bits = $node->splitHeading();
5850 if ( $bits['i'] == $sectionIndex ) {
5851 $targetLevel = $bits['level'];
5855 if ( $mode === 'replace' ) {
5856 $outText .= $frame->expand( $node, PPFrame
::RECOVER_ORIG
);
5858 $node = $node->getNextSibling();
5864 if ( $mode === 'get' ) {
5871 # Find the end of the section, including nested sections
5873 if ( $node->getName() === 'h' ) {
5874 $bits = $node->splitHeading();
5875 $curLevel = $bits['level'];
5876 if ( $bits['i'] != $sectionIndex && $curLevel <= $targetLevel ) {
5880 if ( $mode === 'get' ) {
5881 $outText .= $frame->expand( $node, PPFrame
::RECOVER_ORIG
);
5883 $node = $node->getNextSibling();
5886 # Write out the remainder (in replace mode only)
5887 if ( $mode === 'replace' ) {
5888 # Output the replacement text
5889 # Add two newlines on -- trailing whitespace in $newText is conventionally
5890 # stripped by the editor, so we need both newlines to restore the paragraph gap
5891 # Only add trailing whitespace if there is newText
5892 if ( $newText != "" ) {
5893 $outText .= $newText . "\n\n";
5897 $outText .= $frame->expand( $node, PPFrame
::RECOVER_ORIG
);
5898 $node = $node->getNextSibling();
5902 if ( is_string( $outText ) ) {
5903 # Re-insert stripped tags
5904 $outText = rtrim( $this->mStripState
->unstripBoth( $outText ) );
5911 * This function returns the text of a section, specified by a number ($section).
5912 * A section is text under a heading like == Heading == or \<h1\>Heading\</h1\>, or
5913 * the first section before any such heading (section 0).
5915 * If a section contains subsections, these are also returned.
5917 * @param string $text Text to look in
5918 * @param string|int $sectionId Section identifier as a number or string
5919 * (e.g. 0, 1 or 'T-1').
5920 * @param string $defaultText Default to return if section is not found
5922 * @return string Text of the requested section
5924 public function getSection( $text, $sectionId, $defaultText = '' ) {
5925 return $this->extractSections( $text, $sectionId, 'get', $defaultText );
5929 * This function returns $oldtext after the content of the section
5930 * specified by $section has been replaced with $text. If the target
5931 * section does not exist, $oldtext is returned unchanged.
5933 * @param string $oldText Former text of the article
5934 * @param string|int $sectionId Section identifier as a number or string
5935 * (e.g. 0, 1 or 'T-1').
5936 * @param string $newText Replacing text
5938 * @return string Modified text
5940 public function replaceSection( $oldText, $sectionId, $newText ) {
5941 return $this->extractSections( $oldText, $sectionId, 'replace', $newText );
5945 * Get the ID of the revision we are parsing
5947 * The return value will be either:
5948 * - a) Positive, indicating a specific revision ID (current or old)
5949 * - b) Zero, meaning the revision ID is specified by getCurrentRevisionCallback()
5950 * - c) Null, meaning the parse is for preview mode and there is no revision
5954 public function getRevisionId() {
5955 return $this->mRevisionId
;
5959 * Get the revision object for $this->mRevisionId
5961 * @return Revision|null Either a Revision object or null
5962 * @since 1.23 (public since 1.23)
5964 public function getRevisionObject() {
5965 if ( $this->mRevisionObject
) {
5966 return $this->mRevisionObject
;
5969 // NOTE: try to get the RevisionObject even if mRevisionId is null.
5970 // This is useful when parsing a revision that has not yet been saved.
5971 // However, if we get back a saved revision even though we are in
5972 // preview mode, we'll have to ignore it, see below.
5973 // NOTE: This callback may be used to inject an OLD revision that was
5974 // already loaded, so "current" is a bit of a misnomer. We can't just
5975 // skip it if mRevisionId is set.
5976 $rev = call_user_func(
5977 $this->mOptions
->getCurrentRevisionCallback(),
5982 if ( $this->mRevisionId
=== null && $rev && $rev->getId() ) {
5983 // We are in preview mode (mRevisionId is null), and the current revision callback
5984 // returned an existing revision. Ignore it and return null, it's probably the page's
5985 // current revision, which is not what we want here. Note that we do want to call the
5986 // callback to allow the unsaved revision to be injected here, e.g. for
5987 // self-transclusion previews.
5991 // If the parse is for a new revision, then the callback should have
5992 // already been set to force the object and should match mRevisionId.
5993 // If not, try to fetch by mRevisionId for sanity.
5994 if ( $this->mRevisionId
&& $rev && $rev->getId() != $this->mRevisionId
) {
5995 $rev = Revision
::newFromId( $this->mRevisionId
);
5998 $this->mRevisionObject
= $rev;
6000 return $this->mRevisionObject
;
6004 * Get the timestamp associated with the current revision, adjusted for
6005 * the default server-local timestamp
6006 * @return string TS_MW timestamp
6008 public function getRevisionTimestamp() {
6009 if ( $this->mRevisionTimestamp
!== null ) {
6010 return $this->mRevisionTimestamp
;
6013 # Use specified revision timestamp, falling back to the current timestamp
6014 $revObject = $this->getRevisionObject();
6015 $timestamp = $revObject ?
$revObject->getTimestamp() : $this->mOptions
->getTimestamp();
6016 $this->mOutput
->setRevisionTimestampUsed( $timestamp ); // unadjusted time zone
6018 # The cryptic '' timezone parameter tells to use the site-default
6019 # timezone offset instead of the user settings.
6020 # Since this value will be saved into the parser cache, served
6021 # to other users, and potentially even used inside links and such,
6022 # it needs to be consistent for all visitors.
6023 $this->mRevisionTimestamp
= $this->contLang
->userAdjust( $timestamp, '' );
6025 return $this->mRevisionTimestamp
;
6029 * Get the name of the user that edited the last revision
6031 * @return string User name
6033 public function getRevisionUser() {
6034 if ( is_null( $this->mRevisionUser
) ) {
6035 $revObject = $this->getRevisionObject();
6037 # if this template is subst: the revision id will be blank,
6038 # so just use the current user's name
6040 $this->mRevisionUser
= $revObject->getUserText();
6041 } elseif ( $this->ot
['wiki'] ||
$this->mOptions
->getIsPreview() ) {
6042 $this->mRevisionUser
= $this->getUser()->getName();
6045 return $this->mRevisionUser
;
6049 * Get the size of the revision
6051 * @return int|null Revision size
6053 public function getRevisionSize() {
6054 if ( is_null( $this->mRevisionSize
) ) {
6055 $revObject = $this->getRevisionObject();
6057 # if this variable is subst: the revision id will be blank,
6058 # so just use the parser input size, because the own substituation
6059 # will change the size.
6061 $this->mRevisionSize
= $revObject->getSize();
6063 $this->mRevisionSize
= $this->mInputSize
;
6066 return $this->mRevisionSize
;
6070 * Mutator for $mDefaultSort
6072 * @param string $sort New value
6074 public function setDefaultSort( $sort ) {
6075 $this->mDefaultSort
= $sort;
6076 $this->mOutput
->setProperty( 'defaultsort', $sort );
6080 * Accessor for $mDefaultSort
6081 * Will use the empty string if none is set.
6083 * This value is treated as a prefix, so the
6084 * empty string is equivalent to sorting by
6089 public function getDefaultSort() {
6090 if ( $this->mDefaultSort
!== false ) {
6091 return $this->mDefaultSort
;
6098 * Accessor for $mDefaultSort
6099 * Unlike getDefaultSort(), will return false if none is set
6101 * @return string|bool
6103 public function getCustomDefaultSort() {
6104 return $this->mDefaultSort
;
6107 private static function getSectionNameFromStrippedText( $text ) {
6108 $text = Sanitizer
::normalizeSectionNameWhitespace( $text );
6109 $text = Sanitizer
::decodeCharReferences( $text );
6110 $text = self
::normalizeSectionName( $text );
6114 private static function makeAnchor( $sectionName ) {
6115 return '#' . Sanitizer
::escapeIdForLink( $sectionName );
6118 private function makeLegacyAnchor( $sectionName ) {
6119 $fragmentMode = $this->svcOptions
->get( 'FragmentMode' );
6120 if ( isset( $fragmentMode[1] ) && $fragmentMode[1] === 'legacy' ) {
6121 // ForAttribute() and ForLink() are the same for legacy encoding
6122 $id = Sanitizer
::escapeIdForAttribute( $sectionName, Sanitizer
::ID_FALLBACK
);
6124 $id = Sanitizer
::escapeIdForLink( $sectionName );
6131 * Try to guess the section anchor name based on a wikitext fragment
6132 * presumably extracted from a heading, for example "Header" from
6135 * @param string $text
6136 * @return string Anchor (starting with '#')
6138 public function guessSectionNameFromWikiText( $text ) {
6139 # Strip out wikitext links(they break the anchor)
6140 $text = $this->stripSectionName( $text );
6141 $sectionName = self
::getSectionNameFromStrippedText( $text );
6142 return self
::makeAnchor( $sectionName );
6146 * Same as guessSectionNameFromWikiText(), but produces legacy anchors
6147 * instead, if possible. For use in redirects, since various versions
6148 * of Microsoft browsers interpret Location: headers as something other
6149 * than UTF-8, resulting in breakage.
6151 * @param string $text The section name
6152 * @return string Anchor (starting with '#')
6154 public function guessLegacySectionNameFromWikiText( $text ) {
6155 # Strip out wikitext links(they break the anchor)
6156 $text = $this->stripSectionName( $text );
6157 $sectionName = self
::getSectionNameFromStrippedText( $text );
6158 return $this->makeLegacyAnchor( $sectionName );
6162 * Like guessSectionNameFromWikiText(), but takes already-stripped text as input.
6163 * @param string $text Section name (plain text)
6164 * @return string Anchor (starting with '#')
6166 public static function guessSectionNameFromStrippedText( $text ) {
6167 $sectionName = self
::getSectionNameFromStrippedText( $text );
6168 return self
::makeAnchor( $sectionName );
6172 * Apply the same normalization as code making links to this section would
6174 * @param string $text
6177 private static function normalizeSectionName( $text ) {
6178 # T90902: ensure the same normalization is applied for IDs as to links
6179 $titleParser = MediaWikiServices
::getInstance()->getTitleParser();
6182 $parts = $titleParser->splitTitleString( "#$text" );
6183 } catch ( MalformedTitleException
$ex ) {
6186 return $parts['fragment'];
6190 * Strips a text string of wikitext for use in a section anchor
6192 * Accepts a text string and then removes all wikitext from the
6193 * string and leaves only the resultant text (i.e. the result of
6194 * [[User:WikiSysop|Sysop]] would be "Sysop" and the result of
6195 * [[User:WikiSysop]] would be "User:WikiSysop") - this is intended
6196 * to create valid section anchors by mimicing the output of the
6197 * parser when headings are parsed.
6199 * @param string $text Text string to be stripped of wikitext
6200 * for use in a Section anchor
6201 * @return string Filtered text string
6203 public function stripSectionName( $text ) {
6204 # Strip internal link markup
6205 $text = preg_replace( '/\[\[:?([^[|]+)\|([^[]+)\]\]/', '$2', $text );
6206 $text = preg_replace( '/\[\[:?([^[]+)\|?\]\]/', '$1', $text );
6208 # Strip external link markup
6209 # @todo FIXME: Not tolerant to blank link text
6210 # I.E. [https://www.mediawiki.org] will render as [1] or something depending
6211 # on how many empty links there are on the page - need to figure that out.
6212 $text = preg_replace( '/\[(?i:' . $this->mUrlProtocols
. ')([^ ]+?) ([^[]+)\]/', '$2', $text );
6214 # Parse wikitext quotes (italics & bold)
6215 $text = $this->doQuotes( $text );
6218 $text = StringUtils
::delimiterReplace( '<', '>', '', $text );
6223 * strip/replaceVariables/unstrip for preprocessor regression testing
6225 * @param string $text
6226 * @param Title $title
6227 * @param ParserOptions $options
6228 * @param int $outputType
6232 public function testSrvus( $text, Title
$title, ParserOptions
$options,
6233 $outputType = self
::OT_HTML
6235 $magicScopeVariable = $this->lock();
6236 $this->startParse( $title, $options, $outputType, true );
6238 $text = $this->replaceVariables( $text );
6239 $text = $this->mStripState
->unstripBoth( $text );
6240 $text = Sanitizer
::removeHTMLtags( $text );
6245 * @param string $text
6246 * @param Title $title
6247 * @param ParserOptions $options
6250 public function testPst( $text, Title
$title, ParserOptions
$options ) {
6251 return $this->preSaveTransform( $text, $title, $options->getUser(), $options );
6255 * @param string $text
6256 * @param Title $title
6257 * @param ParserOptions $options
6260 public function testPreprocess( $text, Title
$title, ParserOptions
$options ) {
6261 return $this->testSrvus( $text, $title, $options, self
::OT_PREPROCESS
);
6265 * Call a callback function on all regions of the given text that are not
6266 * inside strip markers, and replace those regions with the return value
6267 * of the callback. For example, with input:
6271 * This will call the callback function twice, with 'aaa' and 'bbb'. Those
6272 * two strings will be replaced with the value returned by the callback in
6276 * @param callable $callback
6280 public function markerSkipCallback( $s, $callback ) {
6283 while ( $i < strlen( $s ) ) {
6284 $markerStart = strpos( $s, self
::MARKER_PREFIX
, $i );
6285 if ( $markerStart === false ) {
6286 $out .= call_user_func( $callback, substr( $s, $i ) );
6289 $out .= call_user_func( $callback, substr( $s, $i, $markerStart - $i ) );
6290 $markerEnd = strpos( $s, self
::MARKER_SUFFIX
, $markerStart );
6291 if ( $markerEnd === false ) {
6292 $out .= substr( $s, $markerStart );
6295 $markerEnd +
= strlen( self
::MARKER_SUFFIX
);
6296 $out .= substr( $s, $markerStart, $markerEnd - $markerStart );
6305 * Remove any strip markers found in the given text.
6307 * @param string $text
6310 public function killMarkers( $text ) {
6311 return $this->mStripState
->killMarkers( $text );
6315 * Save the parser state required to convert the given half-parsed text to
6316 * HTML. "Half-parsed" in this context means the output of
6317 * recursiveTagParse() or internalParse(). This output has strip markers
6318 * from replaceVariables (extensionSubstitution() etc.), and link
6319 * placeholders from replaceLinkHolders().
6321 * Returns an array which can be serialized and stored persistently. This
6322 * array can later be loaded into another parser instance with
6323 * unserializeHalfParsedText(). The text can then be safely incorporated into
6324 * the return value of a parser hook.
6326 * @deprecated since 1.31
6327 * @param string $text
6331 public function serializeHalfParsedText( $text ) {
6332 wfDeprecated( __METHOD__
, '1.31' );
6335 'version' => self
::HALF_PARSED_VERSION
,
6336 'stripState' => $this->mStripState
->getSubState( $text ),
6337 'linkHolders' => $this->mLinkHolders
->getSubArray( $text )
6343 * Load the parser state given in the $data array, which is assumed to
6344 * have been generated by serializeHalfParsedText(). The text contents is
6345 * extracted from the array, and its markers are transformed into markers
6346 * appropriate for the current Parser instance. This transformed text is
6347 * returned, and can be safely included in the return value of a parser
6350 * If the $data array has been stored persistently, the caller should first
6351 * check whether it is still valid, by calling isValidHalfParsedText().
6353 * @deprecated since 1.31
6354 * @param array $data Serialized data
6355 * @throws MWException
6358 public function unserializeHalfParsedText( $data ) {
6359 wfDeprecated( __METHOD__
, '1.31' );
6360 if ( !isset( $data['version'] ) ||
$data['version'] != self
::HALF_PARSED_VERSION
) {
6361 throw new MWException( __METHOD__
. ': invalid version' );
6364 # First, extract the strip state.
6365 $texts = [ $data['text'] ];
6366 $texts = $this->mStripState
->merge( $data['stripState'], $texts );
6368 # Now renumber links
6369 $texts = $this->mLinkHolders
->mergeForeign( $data['linkHolders'], $texts );
6371 # Should be good to go.
6376 * Returns true if the given array, presumed to be generated by
6377 * serializeHalfParsedText(), is compatible with the current version of the
6380 * @deprecated since 1.31
6381 * @param array $data
6385 public function isValidHalfParsedText( $data ) {
6386 wfDeprecated( __METHOD__
, '1.31' );
6387 return isset( $data['version'] ) && $data['version'] == self
::HALF_PARSED_VERSION
;
6391 * Parsed a width param of imagelink like 300px or 200x300px
6393 * @param string $value
6394 * @param bool $parseHeight
6399 public static function parseWidthParam( $value, $parseHeight = true ) {
6400 $parsedWidthParam = [];
6401 if ( $value === '' ) {
6402 return $parsedWidthParam;
6405 # (T15500) In both cases (width/height and width only),
6406 # permit trailing "px" for backward compatibility.
6407 if ( $parseHeight && preg_match( '/^([0-9]*)x([0-9]*)\s*(?:px)?\s*$/', $value, $m ) ) {
6408 $width = intval( $m[1] );
6409 $height = intval( $m[2] );
6410 $parsedWidthParam['width'] = $width;
6411 $parsedWidthParam['height'] = $height;
6412 } elseif ( preg_match( '/^[0-9]*\s*(?:px)?\s*$/', $value ) ) {
6413 $width = intval( $value );
6414 $parsedWidthParam['width'] = $width;
6416 return $parsedWidthParam;
6420 * Lock the current instance of the parser.
6422 * This is meant to stop someone from calling the parser
6423 * recursively and messing up all the strip state.
6425 * @throws MWException If parser is in a parse
6426 * @return ScopedCallback The lock will be released once the return value goes out of scope.
6428 protected function lock() {
6429 if ( $this->mInParse
) {
6430 throw new MWException( "Parser state cleared while parsing. "
6431 . "Did you call Parser::parse recursively? Lock is held by: " . $this->mInParse
);
6434 // Save the backtrace when locking, so that if some code tries locking again,
6435 // we can print the lock owner's backtrace for easier debugging
6437 $this->mInParse
= $e->getTraceAsString();
6439 $recursiveCheck = new ScopedCallback( function () {
6440 $this->mInParse
= false;
6443 return $recursiveCheck;
6447 * Strip outer <p></p> tag from the HTML source of a single paragraph.
6449 * Returns original HTML if the <p/> tag has any attributes, if there's no wrapping <p/> tag,
6450 * or if there is more than one <p/> tag in the input HTML.
6452 * @param string $html
6456 public static function stripOuterParagraph( $html ) {
6458 if ( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $html, $m ) && strpos( $m[1], '</p>' ) === false ) {
6466 * Return this parser if it is not doing anything, otherwise
6467 * get a fresh parser. You can use this method by doing
6468 * $newParser = $oldParser->getFreshParser(), or more simply
6469 * $oldParser->getFreshParser()->parse( ... );
6470 * if you're unsure if $oldParser is safe to use.
6473 * @return Parser A parser object that is not parsing anything
6475 public function getFreshParser() {
6476 if ( $this->mInParse
) {
6477 return $this->factory
->create();
6484 * Set's up the PHP implementation of OOUI for use in this request
6485 * and instructs OutputPage to enable OOUI for itself.
6489 public function enableOOUI() {
6490 OutputPage
::setupOOUI();
6491 $this->mOutput
->setEnableOOUI( true );
6495 * @param string $flag
6496 * @param string $reason
6498 protected function setOutputFlag( $flag, $reason ) {
6499 $this->mOutput
->setFlag( $flag );
6500 $name = $this->mTitle
->getPrefixedText();
6501 $this->logger
->debug( __METHOD__
. ": set $flag flag on '$name'; $reason" );