Set @param-taint for Parser::internalParse()
[lhc/web/wiklou.git] / includes / parser / Parser.php
1 <?php
2 /**
3 * PHP parser that converts wiki markup to HTML.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Parser
22 */
23 use MediaWiki\Linker\LinkRenderer;
24 use MediaWiki\MediaWikiServices;
25 use MediaWiki\Special\SpecialPageFactory;
26 use Wikimedia\ScopedCallback;
27
28 /**
29 * @defgroup Parser Parser
30 */
31
32 /**
33 * PHP Parser - Processes wiki markup (which uses a more user-friendly
34 * syntax, such as "[[link]]" for making links), and provides a one-way
35 * transformation of that wiki markup it into (X)HTML output / markup
36 * (which in turn the browser understands, and can display).
37 *
38 * There are seven main entry points into the Parser class:
39 *
40 * - Parser::parse()
41 * produces HTML output
42 * - Parser::preSaveTransform()
43 * produces altered wiki markup
44 * - Parser::preprocess()
45 * removes HTML comments and expands templates
46 * - Parser::cleanSig() and Parser::cleanSigInSig()
47 * cleans a signature before saving it to preferences
48 * - Parser::getSection()
49 * return the content of a section from an article for section editing
50 * - Parser::replaceSection()
51 * replaces a section by number inside an article
52 * - Parser::getPreloadText()
53 * removes <noinclude> sections and <includeonly> tags
54 *
55 * @warning $wgUser or $wgTitle or $wgRequest or $wgLang. Keep them away!
56 *
57 * @par Settings:
58 * $wgNamespacesWithSubpages
59 *
60 * @par Settings only within ParserOptions:
61 * $wgAllowExternalImages
62 * $wgAllowSpecialInclusion
63 * $wgInterwikiMagic
64 * $wgMaxArticleSize
65 *
66 * @ingroup Parser
67 */
68 class Parser {
69 /**
70 * Update this version number when the ParserOutput format
71 * changes in an incompatible way, so the parser cache
72 * can automatically discard old data.
73 */
74 const VERSION = '1.6.4';
75
76 /**
77 * Update this version number when the output of serialiseHalfParsedText()
78 * changes in an incompatible way
79 */
80 const HALF_PARSED_VERSION = 2;
81
82 # Flags for Parser::setFunctionHook
83 const SFH_NO_HASH = 1;
84 const SFH_OBJECT_ARGS = 2;
85
86 # Constants needed for external link processing
87 # Everything except bracket, space, or control characters
88 # \p{Zs} is unicode 'separator, space' category. It covers the space 0x20
89 # as well as U+3000 is IDEOGRAPHIC SPACE for T21052
90 # \x{FFFD} is the Unicode replacement character, which Preprocessor_DOM
91 # uses to replace invalid HTML characters.
92 const EXT_LINK_URL_CLASS = '[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}]';
93 # Simplified expression to match an IPv4 or IPv6 address, or
94 # at least one character of a host name (embeds EXT_LINK_URL_CLASS)
95 const EXT_LINK_ADDR = '(?:[0-9.]+|\\[(?i:[0-9a-f:.]+)\\]|[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}])';
96 # RegExp to make image URLs (embeds IPv6 part of EXT_LINK_ADDR)
97 // phpcs:ignore Generic.Files.LineLength
98 const EXT_IMAGE_REGEX = '/^(http:\/\/|https:\/\/)((?:\\[(?i:[0-9a-f:.]+)\\])?[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}]+)
99 \\/([A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]+)\\.((?i)gif|png|jpg|jpeg)$/Sxu';
100
101 # Regular expression for a non-newline space
102 const SPACE_NOT_NL = '(?:\t|&nbsp;|&\#0*160;|&\#[Xx]0*[Aa]0;|\p{Zs})';
103
104 # Flags for preprocessToDom
105 const PTD_FOR_INCLUSION = 1;
106
107 # Allowed values for $this->mOutputType
108 # Parameter to startExternalParse().
109 const OT_HTML = 1; # like parse()
110 const OT_WIKI = 2; # like preSaveTransform()
111 const OT_PREPROCESS = 3; # like preprocess()
112 const OT_MSG = 3;
113 const OT_PLAIN = 4; # like extractSections() - portions of the original are returned unchanged.
114
115 /**
116 * @var string Prefix and suffix for temporary replacement strings
117 * for the multipass parser.
118 *
119 * \x7f should never appear in input as it's disallowed in XML.
120 * Using it at the front also gives us a little extra robustness
121 * since it shouldn't match when butted up against identifier-like
122 * string constructs.
123 *
124 * Must not consist of all title characters, or else it will change
125 * the behavior of <nowiki> in a link.
126 *
127 * Must have a character that needs escaping in attributes, otherwise
128 * someone could put a strip marker in an attribute, to get around
129 * escaping quote marks, and break out of the attribute. Thus we add
130 * `'".
131 */
132 const MARKER_SUFFIX = "-QINU`\"'\x7f";
133 const MARKER_PREFIX = "\x7f'\"`UNIQ-";
134
135 # Markers used for wrapping the table of contents
136 const TOC_START = '<mw:toc>';
137 const TOC_END = '</mw:toc>';
138
139 /** @var int Assume that no output will later be saved this many seconds after parsing */
140 const MAX_TTS = 900;
141
142 # Persistent:
143 public $mTagHooks = [];
144 public $mTransparentTagHooks = [];
145 public $mFunctionHooks = [];
146 public $mFunctionSynonyms = [ 0 => [], 1 => [] ];
147 public $mFunctionTagHooks = [];
148 public $mStripList = [];
149 public $mDefaultStripList = [];
150 public $mVarCache = [];
151 public $mImageParams = [];
152 public $mImageParamsMagicArray = [];
153 public $mMarkerIndex = 0;
154 /**
155 * @var bool Whether firstCallInit still needs to be called
156 */
157 public $mFirstCall = true;
158
159 # Initialised by initialiseVariables()
160
161 /**
162 * @var MagicWordArray
163 */
164 public $mVariables;
165
166 /**
167 * @var MagicWordArray
168 */
169 public $mSubstWords;
170 # Initialised in constructor
171 public $mConf, $mExtLinkBracketedRegex, $mUrlProtocols;
172
173 # Initialized in getPreprocessor()
174 /** @var Preprocessor */
175 public $mPreprocessor;
176
177 # Cleared with clearState():
178 /**
179 * @var ParserOutput
180 */
181 public $mOutput;
182 public $mAutonumber;
183
184 /**
185 * @var StripState
186 */
187 public $mStripState;
188
189 public $mIncludeCount;
190 /**
191 * @var LinkHolderArray
192 */
193 public $mLinkHolders;
194
195 public $mLinkID;
196 public $mIncludeSizes, $mPPNodeCount, $mGeneratedPPNodeCount, $mHighestExpansionDepth;
197 public $mDefaultSort;
198 public $mTplRedirCache, $mTplDomCache, $mHeadings, $mDoubleUnderscores;
199 public $mExpensiveFunctionCount; # number of expensive parser function calls
200 public $mShowToc, $mForceTocPosition;
201
202 /**
203 * @var User
204 */
205 public $mUser; # User object; only used when doing pre-save transform
206
207 # Temporary
208 # These are variables reset at least once per parse regardless of $clearState
209
210 /**
211 * @var ParserOptions
212 */
213 public $mOptions;
214
215 /**
216 * @var Title
217 */
218 public $mTitle; # Title context, used for self-link rendering and similar things
219 public $mOutputType; # Output type, one of the OT_xxx constants
220 public $ot; # Shortcut alias, see setOutputType()
221 public $mRevisionObject; # The revision object of the specified revision ID
222 public $mRevisionId; # ID to display in {{REVISIONID}} tags
223 public $mRevisionTimestamp; # The timestamp of the specified revision ID
224 public $mRevisionUser; # User to display in {{REVISIONUSER}} tag
225 public $mRevisionSize; # Size to display in {{REVISIONSIZE}} variable
226 public $mRevIdForTs; # The revision ID which was used to fetch the timestamp
227 public $mInputSize = false; # For {{PAGESIZE}} on current page.
228
229 /**
230 * @var string Deprecated accessor for the strip marker prefix.
231 * @deprecated since 1.26; use Parser::MARKER_PREFIX instead.
232 */
233 public $mUniqPrefix = self::MARKER_PREFIX;
234
235 /**
236 * @var array Array with the language name of each language link (i.e. the
237 * interwiki prefix) in the key, value arbitrary. Used to avoid sending
238 * duplicate language links to the ParserOutput.
239 */
240 public $mLangLinkLanguages;
241
242 /**
243 * @var MapCacheLRU|null
244 * @since 1.24
245 *
246 * A cache of the current revisions of titles. Keys are $title->getPrefixedDbKey()
247 */
248 public $currentRevisionCache;
249
250 /**
251 * @var bool|string Recursive call protection.
252 * This variable should be treated as if it were private.
253 */
254 public $mInParse = false;
255
256 /** @var SectionProfiler */
257 protected $mProfiler;
258
259 /**
260 * @var LinkRenderer
261 */
262 protected $mLinkRenderer;
263
264 /** @var MagicWordFactory */
265 private $magicWordFactory;
266
267 /** @var Language */
268 private $contLang;
269
270 /** @var ParserFactory */
271 private $factory;
272
273 /** @var SpecialPageFactory */
274 private $specialPageFactory;
275
276 /**
277 * @param array $conf See $wgParserConf documentation
278 * @param MagicWordFactory|null $magicWordFactory
279 * @param Language|null $contLang Content language
280 * @param ParserFactory|null $factory
281 * @param string|null $urlProtocols As returned from wfUrlProtocols()
282 * @param SpecialPageFactory|null $spFactory
283 */
284 public function __construct(
285 array $conf = [], MagicWordFactory $magicWordFactory = null, Language $contLang = null,
286 ParserFactory $factory = null, $urlProtocols = null, SpecialPageFactory $spFactory = null
287 ) {
288 $this->mConf = $conf;
289 $this->mUrlProtocols = $urlProtocols ?? wfUrlProtocols();
290 $this->mExtLinkBracketedRegex = '/\[(((?i)' . $this->mUrlProtocols . ')' .
291 self::EXT_LINK_ADDR .
292 self::EXT_LINK_URL_CLASS . '*)\p{Zs}*([^\]\\x00-\\x08\\x0a-\\x1F\\x{FFFD}]*?)\]/Su';
293 if ( isset( $conf['preprocessorClass'] ) ) {
294 $this->mPreprocessorClass = $conf['preprocessorClass'];
295 } elseif ( defined( 'HPHP_VERSION' ) ) {
296 # Preprocessor_Hash is much faster than Preprocessor_DOM under HipHop
297 $this->mPreprocessorClass = Preprocessor_Hash::class;
298 } elseif ( extension_loaded( 'domxml' ) ) {
299 # PECL extension that conflicts with the core DOM extension (T15770)
300 wfDebug( "Warning: you have the obsolete domxml extension for PHP. Please remove it!\n" );
301 $this->mPreprocessorClass = Preprocessor_Hash::class;
302 } elseif ( extension_loaded( 'dom' ) ) {
303 $this->mPreprocessorClass = Preprocessor_DOM::class;
304 } else {
305 $this->mPreprocessorClass = Preprocessor_Hash::class;
306 }
307 wfDebug( __CLASS__ . ": using preprocessor: {$this->mPreprocessorClass}\n" );
308
309 $services = MediaWikiServices::getInstance();
310 $this->magicWordFactory = $magicWordFactory ??
311 $services->getMagicWordFactory();
312
313 $this->contLang = $contLang ?? $services->getContentLanguage();
314
315 $this->factory = $factory ?? $services->getParserFactory();
316 $this->specialPageFactory = $spFactory ?? $services->getSpecialPageFactory();
317 }
318
319 /**
320 * Reduce memory usage to reduce the impact of circular references
321 */
322 public function __destruct() {
323 if ( isset( $this->mLinkHolders ) ) {
324 unset( $this->mLinkHolders );
325 }
326 foreach ( $this as $name => $value ) {
327 unset( $this->$name );
328 }
329 }
330
331 /**
332 * Allow extensions to clean up when the parser is cloned
333 */
334 public function __clone() {
335 $this->mInParse = false;
336
337 // T58226: When you create a reference "to" an object field, that
338 // makes the object field itself be a reference too (until the other
339 // reference goes out of scope). When cloning, any field that's a
340 // reference is copied as a reference in the new object. Both of these
341 // are defined PHP5 behaviors, as inconvenient as it is for us when old
342 // hooks from PHP4 days are passing fields by reference.
343 foreach ( [ 'mStripState', 'mVarCache' ] as $k ) {
344 // Make a non-reference copy of the field, then rebind the field to
345 // reference the new copy.
346 $tmp = $this->$k;
347 $this->$k =& $tmp;
348 unset( $tmp );
349 }
350
351 Hooks::run( 'ParserCloned', [ $this ] );
352 }
353
354 /**
355 * Do various kinds of initialisation on the first call of the parser
356 */
357 public function firstCallInit() {
358 if ( !$this->mFirstCall ) {
359 return;
360 }
361 $this->mFirstCall = false;
362
363 CoreParserFunctions::register( $this );
364 CoreTagHooks::register( $this );
365 $this->initialiseVariables();
366
367 // Avoid PHP 7.1 warning from passing $this by reference
368 $parser = $this;
369 Hooks::run( 'ParserFirstCallInit', [ &$parser ] );
370 }
371
372 /**
373 * Clear Parser state
374 *
375 * @private
376 */
377 public function clearState() {
378 $this->firstCallInit();
379 $this->mOutput = new ParserOutput;
380 $this->mOptions->registerWatcher( [ $this->mOutput, 'recordOption' ] );
381 $this->mAutonumber = 0;
382 $this->mIncludeCount = [];
383 $this->mLinkHolders = new LinkHolderArray( $this );
384 $this->mLinkID = 0;
385 $this->mRevisionObject = $this->mRevisionTimestamp =
386 $this->mRevisionId = $this->mRevisionUser = $this->mRevisionSize = null;
387 $this->mVarCache = [];
388 $this->mUser = null;
389 $this->mLangLinkLanguages = [];
390 $this->currentRevisionCache = null;
391
392 $this->mStripState = new StripState( $this );
393
394 # Clear these on every parse, T6549
395 $this->mTplRedirCache = $this->mTplDomCache = [];
396
397 $this->mShowToc = true;
398 $this->mForceTocPosition = false;
399 $this->mIncludeSizes = [
400 'post-expand' => 0,
401 'arg' => 0,
402 ];
403 $this->mPPNodeCount = 0;
404 $this->mGeneratedPPNodeCount = 0;
405 $this->mHighestExpansionDepth = 0;
406 $this->mDefaultSort = false;
407 $this->mHeadings = [];
408 $this->mDoubleUnderscores = [];
409 $this->mExpensiveFunctionCount = 0;
410
411 # Fix cloning
412 if ( isset( $this->mPreprocessor ) && $this->mPreprocessor->parser !== $this ) {
413 $this->mPreprocessor = null;
414 }
415
416 $this->mProfiler = new SectionProfiler();
417
418 // Avoid PHP 7.1 warning from passing $this by reference
419 $parser = $this;
420 Hooks::run( 'ParserClearState', [ &$parser ] );
421 }
422
423 /**
424 * Convert wikitext to HTML
425 * Do not call this function recursively.
426 *
427 * @param string $text Text we want to parse
428 * @param Title $title
429 * @param ParserOptions $options
430 * @param bool $linestart
431 * @param bool $clearState
432 * @param int|null $revid Number to pass in {{REVISIONID}}
433 * @return ParserOutput A ParserOutput
434 */
435 public function parse(
436 $text, Title $title, ParserOptions $options,
437 $linestart = true, $clearState = true, $revid = null
438 ) {
439 if ( $clearState ) {
440 // We use U+007F DELETE to construct strip markers, so we have to make
441 // sure that this character does not occur in the input text.
442 $text = strtr( $text, "\x7f", "?" );
443 $magicScopeVariable = $this->lock();
444 }
445 // Strip U+0000 NULL (T159174)
446 $text = str_replace( "\000", '', $text );
447
448 $this->startParse( $title, $options, self::OT_HTML, $clearState );
449
450 $this->currentRevisionCache = null;
451 $this->mInputSize = strlen( $text );
452 if ( $this->mOptions->getEnableLimitReport() ) {
453 $this->mOutput->resetParseStartTime();
454 }
455
456 $oldRevisionId = $this->mRevisionId;
457 $oldRevisionObject = $this->mRevisionObject;
458 $oldRevisionTimestamp = $this->mRevisionTimestamp;
459 $oldRevisionUser = $this->mRevisionUser;
460 $oldRevisionSize = $this->mRevisionSize;
461 if ( $revid !== null ) {
462 $this->mRevisionId = $revid;
463 $this->mRevisionObject = null;
464 $this->mRevisionTimestamp = null;
465 $this->mRevisionUser = null;
466 $this->mRevisionSize = null;
467 }
468
469 // Avoid PHP 7.1 warning from passing $this by reference
470 $parser = $this;
471 Hooks::run( 'ParserBeforeStrip', [ &$parser, &$text, &$this->mStripState ] );
472 # No more strip!
473 Hooks::run( 'ParserAfterStrip', [ &$parser, &$text, &$this->mStripState ] );
474 $text = $this->internalParse( $text );
475 Hooks::run( 'ParserAfterParse', [ &$parser, &$text, &$this->mStripState ] );
476
477 $text = $this->internalParseHalfParsed( $text, true, $linestart );
478
479 /**
480 * A converted title will be provided in the output object if title and
481 * content conversion are enabled, the article text does not contain
482 * a conversion-suppressing double-underscore tag, and no
483 * {{DISPLAYTITLE:...}} is present. DISPLAYTITLE takes precedence over
484 * automatic link conversion.
485 */
486 if ( !( $options->getDisableTitleConversion()
487 || isset( $this->mDoubleUnderscores['nocontentconvert'] )
488 || isset( $this->mDoubleUnderscores['notitleconvert'] )
489 || $this->mOutput->getDisplayTitle() !== false )
490 ) {
491 $convruletitle = $this->getTargetLanguage()->getConvRuleTitle();
492 if ( $convruletitle ) {
493 $this->mOutput->setTitleText( $convruletitle );
494 } else {
495 $titleText = $this->getTargetLanguage()->convertTitle( $title );
496 $this->mOutput->setTitleText( $titleText );
497 }
498 }
499
500 # Compute runtime adaptive expiry if set
501 $this->mOutput->finalizeAdaptiveCacheExpiry();
502
503 # Warn if too many heavyweight parser functions were used
504 if ( $this->mExpensiveFunctionCount > $this->mOptions->getExpensiveParserFunctionLimit() ) {
505 $this->limitationWarn( 'expensive-parserfunction',
506 $this->mExpensiveFunctionCount,
507 $this->mOptions->getExpensiveParserFunctionLimit()
508 );
509 }
510
511 # Information on limits, for the benefit of users who try to skirt them
512 if ( $this->mOptions->getEnableLimitReport() ) {
513 $text .= $this->makeLimitReport();
514 }
515
516 # Wrap non-interface parser output in a <div> so it can be targeted
517 # with CSS (T37247)
518 $class = $this->mOptions->getWrapOutputClass();
519 if ( $class !== false && !$this->mOptions->getInterfaceMessage() ) {
520 $this->mOutput->addWrapperDivClass( $class );
521 }
522
523 $this->mOutput->setText( $text );
524
525 $this->mRevisionId = $oldRevisionId;
526 $this->mRevisionObject = $oldRevisionObject;
527 $this->mRevisionTimestamp = $oldRevisionTimestamp;
528 $this->mRevisionUser = $oldRevisionUser;
529 $this->mRevisionSize = $oldRevisionSize;
530 $this->mInputSize = false;
531 $this->currentRevisionCache = null;
532
533 return $this->mOutput;
534 }
535
536 /**
537 * Set the limit report data in the current ParserOutput, and return the
538 * limit report HTML comment.
539 *
540 * @return string
541 */
542 protected function makeLimitReport() {
543 global $wgShowHostnames;
544
545 $maxIncludeSize = $this->mOptions->getMaxIncludeSize();
546
547 $cpuTime = $this->mOutput->getTimeSinceStart( 'cpu' );
548 if ( $cpuTime !== null ) {
549 $this->mOutput->setLimitReportData( 'limitreport-cputime',
550 sprintf( "%.3f", $cpuTime )
551 );
552 }
553
554 $wallTime = $this->mOutput->getTimeSinceStart( 'wall' );
555 $this->mOutput->setLimitReportData( 'limitreport-walltime',
556 sprintf( "%.3f", $wallTime )
557 );
558
559 $this->mOutput->setLimitReportData( 'limitreport-ppvisitednodes',
560 [ $this->mPPNodeCount, $this->mOptions->getMaxPPNodeCount() ]
561 );
562 $this->mOutput->setLimitReportData( 'limitreport-ppgeneratednodes',
563 [ $this->mGeneratedPPNodeCount, $this->mOptions->getMaxGeneratedPPNodeCount() ]
564 );
565 $this->mOutput->setLimitReportData( 'limitreport-postexpandincludesize',
566 [ $this->mIncludeSizes['post-expand'], $maxIncludeSize ]
567 );
568 $this->mOutput->setLimitReportData( 'limitreport-templateargumentsize',
569 [ $this->mIncludeSizes['arg'], $maxIncludeSize ]
570 );
571 $this->mOutput->setLimitReportData( 'limitreport-expansiondepth',
572 [ $this->mHighestExpansionDepth, $this->mOptions->getMaxPPExpandDepth() ]
573 );
574 $this->mOutput->setLimitReportData( 'limitreport-expensivefunctioncount',
575 [ $this->mExpensiveFunctionCount, $this->mOptions->getExpensiveParserFunctionLimit() ]
576 );
577
578 foreach ( $this->mStripState->getLimitReport() as list( $key, $value ) ) {
579 $this->mOutput->setLimitReportData( $key, $value );
580 }
581
582 Hooks::run( 'ParserLimitReportPrepare', [ $this, $this->mOutput ] );
583
584 $limitReport = "NewPP limit report\n";
585 if ( $wgShowHostnames ) {
586 $limitReport .= 'Parsed by ' . wfHostname() . "\n";
587 }
588 $limitReport .= 'Cached time: ' . $this->mOutput->getCacheTime() . "\n";
589 $limitReport .= 'Cache expiry: ' . $this->mOutput->getCacheExpiry() . "\n";
590 $limitReport .= 'Dynamic content: ' .
591 ( $this->mOutput->hasDynamicContent() ? 'true' : 'false' ) .
592 "\n";
593
594 foreach ( $this->mOutput->getLimitReportData() as $key => $value ) {
595 if ( Hooks::run( 'ParserLimitReportFormat',
596 [ $key, &$value, &$limitReport, false, false ]
597 ) ) {
598 $keyMsg = wfMessage( $key )->inLanguage( 'en' )->useDatabase( false );
599 $valueMsg = wfMessage( [ "$key-value-text", "$key-value" ] )
600 ->inLanguage( 'en' )->useDatabase( false );
601 if ( !$valueMsg->exists() ) {
602 $valueMsg = new RawMessage( '$1' );
603 }
604 if ( !$keyMsg->isDisabled() && !$valueMsg->isDisabled() ) {
605 $valueMsg->params( $value );
606 $limitReport .= "{$keyMsg->text()}: {$valueMsg->text()}\n";
607 }
608 }
609 }
610 // Since we're not really outputting HTML, decode the entities and
611 // then re-encode the things that need hiding inside HTML comments.
612 $limitReport = htmlspecialchars_decode( $limitReport );
613 // Run deprecated hook
614 Hooks::run( 'ParserLimitReport', [ $this, &$limitReport ], '1.22' );
615
616 // Sanitize for comment. Note '‐' in the replacement is U+2010,
617 // which looks much like the problematic '-'.
618 $limitReport = str_replace( [ '-', '&' ], [ '‐', '&amp;' ], $limitReport );
619 $text = "\n<!-- \n$limitReport-->\n";
620
621 // Add on template profiling data in human/machine readable way
622 $dataByFunc = $this->mProfiler->getFunctionStats();
623 uasort( $dataByFunc, function ( $a, $b ) {
624 return $b['real'] <=> $a['real']; // descending order
625 } );
626 $profileReport = [];
627 foreach ( array_slice( $dataByFunc, 0, 10 ) as $item ) {
628 $profileReport[] = sprintf( "%6.2f%% %8.3f %6d %s",
629 $item['%real'], $item['real'], $item['calls'],
630 htmlspecialchars( $item['name'] ) );
631 }
632 $text .= "<!--\nTransclusion expansion time report (%,ms,calls,template)\n";
633 $text .= implode( "\n", $profileReport ) . "\n-->\n";
634
635 $this->mOutput->setLimitReportData( 'limitreport-timingprofile', $profileReport );
636
637 // Add other cache related metadata
638 if ( $wgShowHostnames ) {
639 $this->mOutput->setLimitReportData( 'cachereport-origin', wfHostname() );
640 }
641 $this->mOutput->setLimitReportData( 'cachereport-timestamp',
642 $this->mOutput->getCacheTime() );
643 $this->mOutput->setLimitReportData( 'cachereport-ttl',
644 $this->mOutput->getCacheExpiry() );
645 $this->mOutput->setLimitReportData( 'cachereport-transientcontent',
646 $this->mOutput->hasDynamicContent() );
647
648 if ( $this->mGeneratedPPNodeCount > $this->mOptions->getMaxGeneratedPPNodeCount() / 10 ) {
649 wfDebugLog( 'generated-pp-node-count', $this->mGeneratedPPNodeCount . ' ' .
650 $this->mTitle->getPrefixedDBkey() );
651 }
652 return $text;
653 }
654
655 /**
656 * Half-parse wikitext to half-parsed HTML. This recursive parser entry point
657 * can be called from an extension tag hook.
658 *
659 * The output of this function IS NOT SAFE PARSED HTML; it is "half-parsed"
660 * instead, which means that lists and links have not been fully parsed yet,
661 * and strip markers are still present.
662 *
663 * Use recursiveTagParseFully() to fully parse wikitext to output-safe HTML.
664 *
665 * Use this function if you're a parser tag hook and you want to parse
666 * wikitext before or after applying additional transformations, and you
667 * intend to *return the result as hook output*, which will cause it to go
668 * through the rest of parsing process automatically.
669 *
670 * If $frame is not provided, then template variables (e.g., {{{1}}}) within
671 * $text are not expanded
672 *
673 * @param string $text Text extension wants to have parsed
674 * @param bool|PPFrame $frame The frame to use for expanding any template variables
675 * @return string UNSAFE half-parsed HTML
676 */
677 public function recursiveTagParse( $text, $frame = false ) {
678 // Avoid PHP 7.1 warning from passing $this by reference
679 $parser = $this;
680 Hooks::run( 'ParserBeforeStrip', [ &$parser, &$text, &$this->mStripState ] );
681 Hooks::run( 'ParserAfterStrip', [ &$parser, &$text, &$this->mStripState ] );
682 $text = $this->internalParse( $text, false, $frame );
683 return $text;
684 }
685
686 /**
687 * Fully parse wikitext to fully parsed HTML. This recursive parser entry
688 * point can be called from an extension tag hook.
689 *
690 * The output of this function is fully-parsed HTML that is safe for output.
691 * If you're a parser tag hook, you might want to use recursiveTagParse()
692 * instead.
693 *
694 * If $frame is not provided, then template variables (e.g., {{{1}}}) within
695 * $text are not expanded
696 *
697 * @since 1.25
698 *
699 * @param string $text Text extension wants to have parsed
700 * @param bool|PPFrame $frame The frame to use for expanding any template variables
701 * @return string Fully parsed HTML
702 */
703 public function recursiveTagParseFully( $text, $frame = false ) {
704 $text = $this->recursiveTagParse( $text, $frame );
705 $text = $this->internalParseHalfParsed( $text, false );
706 return $text;
707 }
708
709 /**
710 * Expand templates and variables in the text, producing valid, static wikitext.
711 * Also removes comments.
712 * Do not call this function recursively.
713 * @param string $text
714 * @param Title|null $title
715 * @param ParserOptions $options
716 * @param int|null $revid
717 * @param bool|PPFrame $frame
718 * @return mixed|string
719 */
720 public function preprocess( $text, Title $title = null,
721 ParserOptions $options, $revid = null, $frame = false
722 ) {
723 $magicScopeVariable = $this->lock();
724 $this->startParse( $title, $options, self::OT_PREPROCESS, true );
725 if ( $revid !== null ) {
726 $this->mRevisionId = $revid;
727 }
728 // Avoid PHP 7.1 warning from passing $this by reference
729 $parser = $this;
730 Hooks::run( 'ParserBeforeStrip', [ &$parser, &$text, &$this->mStripState ] );
731 Hooks::run( 'ParserAfterStrip', [ &$parser, &$text, &$this->mStripState ] );
732 $text = $this->replaceVariables( $text, $frame );
733 $text = $this->mStripState->unstripBoth( $text );
734 return $text;
735 }
736
737 /**
738 * Recursive parser entry point that can be called from an extension tag
739 * hook.
740 *
741 * @param string $text Text to be expanded
742 * @param bool|PPFrame $frame The frame to use for expanding any template variables
743 * @return string
744 * @since 1.19
745 */
746 public function recursivePreprocess( $text, $frame = false ) {
747 $text = $this->replaceVariables( $text, $frame );
748 $text = $this->mStripState->unstripBoth( $text );
749 return $text;
750 }
751
752 /**
753 * Process the wikitext for the "?preload=" feature. (T7210)
754 *
755 * "<noinclude>", "<includeonly>" etc. are parsed as for template
756 * transclusion, comments, templates, arguments, tags hooks and parser
757 * functions are untouched.
758 *
759 * @param string $text
760 * @param Title $title
761 * @param ParserOptions $options
762 * @param array $params
763 * @return string
764 */
765 public function getPreloadText( $text, Title $title, ParserOptions $options, $params = [] ) {
766 $msg = new RawMessage( $text );
767 $text = $msg->params( $params )->plain();
768
769 # Parser (re)initialisation
770 $magicScopeVariable = $this->lock();
771 $this->startParse( $title, $options, self::OT_PLAIN, true );
772
773 $flags = PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES;
774 $dom = $this->preprocessToDom( $text, self::PTD_FOR_INCLUSION );
775 $text = $this->getPreprocessor()->newFrame()->expand( $dom, $flags );
776 $text = $this->mStripState->unstripBoth( $text );
777 return $text;
778 }
779
780 /**
781 * Set the current user.
782 * Should only be used when doing pre-save transform.
783 *
784 * @param User|null $user User object or null (to reset)
785 */
786 public function setUser( $user ) {
787 $this->mUser = $user;
788 }
789
790 /**
791 * Set the context title
792 *
793 * @param Title $t
794 */
795 public function setTitle( $t ) {
796 if ( !$t ) {
797 $t = Title::newFromText( 'NO TITLE' );
798 }
799
800 if ( $t->hasFragment() ) {
801 # Strip the fragment to avoid various odd effects
802 $this->mTitle = $t->createFragmentTarget( '' );
803 } else {
804 $this->mTitle = $t;
805 }
806 }
807
808 /**
809 * Accessor for the Title object
810 *
811 * @return Title
812 */
813 public function getTitle() {
814 return $this->mTitle;
815 }
816
817 /**
818 * Accessor/mutator for the Title object
819 *
820 * @param Title|null $x Title object or null to just get the current one
821 * @return Title
822 */
823 public function Title( $x = null ) {
824 return wfSetVar( $this->mTitle, $x );
825 }
826
827 /**
828 * Set the output type
829 *
830 * @param int $ot New value
831 */
832 public function setOutputType( $ot ) {
833 $this->mOutputType = $ot;
834 # Shortcut alias
835 $this->ot = [
836 'html' => $ot == self::OT_HTML,
837 'wiki' => $ot == self::OT_WIKI,
838 'pre' => $ot == self::OT_PREPROCESS,
839 'plain' => $ot == self::OT_PLAIN,
840 ];
841 }
842
843 /**
844 * Accessor/mutator for the output type
845 *
846 * @param int|null $x New value or null to just get the current one
847 * @return int
848 */
849 public function OutputType( $x = null ) {
850 return wfSetVar( $this->mOutputType, $x );
851 }
852
853 /**
854 * Get the ParserOutput object
855 *
856 * @return ParserOutput
857 */
858 public function getOutput() {
859 return $this->mOutput;
860 }
861
862 /**
863 * Get the ParserOptions object
864 *
865 * @return ParserOptions
866 */
867 public function getOptions() {
868 return $this->mOptions;
869 }
870
871 /**
872 * Accessor/mutator for the ParserOptions object
873 *
874 * @param ParserOptions|null $x New value or null to just get the current one
875 * @return ParserOptions Current ParserOptions object
876 */
877 public function Options( $x = null ) {
878 return wfSetVar( $this->mOptions, $x );
879 }
880
881 /**
882 * @return int
883 */
884 public function nextLinkID() {
885 return $this->mLinkID++;
886 }
887
888 /**
889 * @param int $id
890 */
891 public function setLinkID( $id ) {
892 $this->mLinkID = $id;
893 }
894
895 /**
896 * Get a language object for use in parser functions such as {{FORMATNUM:}}
897 * @return Language
898 */
899 public function getFunctionLang() {
900 return $this->getTargetLanguage();
901 }
902
903 /**
904 * Get the target language for the content being parsed. This is usually the
905 * language that the content is in.
906 *
907 * @since 1.19
908 *
909 * @throws MWException
910 * @return Language
911 */
912 public function getTargetLanguage() {
913 $target = $this->mOptions->getTargetLanguage();
914
915 if ( $target !== null ) {
916 return $target;
917 } elseif ( $this->mOptions->getInterfaceMessage() ) {
918 return $this->mOptions->getUserLangObj();
919 } elseif ( is_null( $this->mTitle ) ) {
920 throw new MWException( __METHOD__ . ': $this->mTitle is null' );
921 }
922
923 return $this->mTitle->getPageLanguage();
924 }
925
926 /**
927 * Get the language object for language conversion
928 * @deprecated since 1.32, just use getTargetLanguage()
929 * @return Language|null
930 */
931 public function getConverterLanguage() {
932 return $this->getTargetLanguage();
933 }
934
935 /**
936 * Get a User object either from $this->mUser, if set, or from the
937 * ParserOptions object otherwise
938 *
939 * @return User
940 */
941 public function getUser() {
942 if ( !is_null( $this->mUser ) ) {
943 return $this->mUser;
944 }
945 return $this->mOptions->getUser();
946 }
947
948 /**
949 * Get a preprocessor object
950 *
951 * @return Preprocessor
952 */
953 public function getPreprocessor() {
954 if ( !isset( $this->mPreprocessor ) ) {
955 $class = $this->mPreprocessorClass;
956 $this->mPreprocessor = new $class( $this );
957 }
958 return $this->mPreprocessor;
959 }
960
961 /**
962 * Get a LinkRenderer instance to make links with
963 *
964 * @since 1.28
965 * @return LinkRenderer
966 */
967 public function getLinkRenderer() {
968 if ( !$this->mLinkRenderer ) {
969 $this->mLinkRenderer = MediaWikiServices::getInstance()
970 ->getLinkRendererFactory()->create();
971 $this->mLinkRenderer->setStubThreshold(
972 $this->getOptions()->getStubThreshold()
973 );
974 }
975
976 return $this->mLinkRenderer;
977 }
978
979 /**
980 * Get the MagicWordFactory that this Parser is using
981 *
982 * @since 1.32
983 * @return MagicWordFactory
984 */
985 public function getMagicWordFactory() {
986 return $this->magicWordFactory;
987 }
988
989 /**
990 * Get the content language that this Parser is using
991 *
992 * @since 1.32
993 * @return Language
994 */
995 public function getContentLanguage() {
996 return $this->contLang;
997 }
998
999 /**
1000 * Replaces all occurrences of HTML-style comments and the given tags
1001 * in the text with a random marker and returns the next text. The output
1002 * parameter $matches will be an associative array filled with data in
1003 * the form:
1004 *
1005 * @code
1006 * 'UNIQ-xxxxx' => [
1007 * 'element',
1008 * 'tag content',
1009 * [ 'param' => 'x' ],
1010 * '<element param="x">tag content</element>' ]
1011 * @endcode
1012 *
1013 * @param array $elements List of element names. Comments are always extracted.
1014 * @param string $text Source text string.
1015 * @param array &$matches Out parameter, Array: extracted tags
1016 * @return string Stripped text
1017 */
1018 public static function extractTagsAndParams( $elements, $text, &$matches ) {
1019 static $n = 1;
1020 $stripped = '';
1021 $matches = [];
1022
1023 $taglist = implode( '|', $elements );
1024 $start = "/<($taglist)(\\s+[^>]*?|\\s*?)(\/?" . ">)|<(!--)/i";
1025
1026 while ( $text != '' ) {
1027 $p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE );
1028 $stripped .= $p[0];
1029 if ( count( $p ) < 5 ) {
1030 break;
1031 }
1032 if ( count( $p ) > 5 ) {
1033 # comment
1034 $element = $p[4];
1035 $attributes = '';
1036 $close = '';
1037 $inside = $p[5];
1038 } else {
1039 # tag
1040 $element = $p[1];
1041 $attributes = $p[2];
1042 $close = $p[3];
1043 $inside = $p[4];
1044 }
1045
1046 $marker = self::MARKER_PREFIX . "-$element-" . sprintf( '%08X', $n++ ) . self::MARKER_SUFFIX;
1047 $stripped .= $marker;
1048
1049 if ( $close === '/>' ) {
1050 # Empty element tag, <tag />
1051 $content = null;
1052 $text = $inside;
1053 $tail = null;
1054 } else {
1055 if ( $element === '!--' ) {
1056 $end = '/(-->)/';
1057 } else {
1058 $end = "/(<\\/$element\\s*>)/i";
1059 }
1060 $q = preg_split( $end, $inside, 2, PREG_SPLIT_DELIM_CAPTURE );
1061 $content = $q[0];
1062 if ( count( $q ) < 3 ) {
1063 # No end tag -- let it run out to the end of the text.
1064 $tail = '';
1065 $text = '';
1066 } else {
1067 $tail = $q[1];
1068 $text = $q[2];
1069 }
1070 }
1071
1072 $matches[$marker] = [ $element,
1073 $content,
1074 Sanitizer::decodeTagAttributes( $attributes ),
1075 "<$element$attributes$close$content$tail" ];
1076 }
1077 return $stripped;
1078 }
1079
1080 /**
1081 * Get a list of strippable XML-like elements
1082 *
1083 * @return array
1084 */
1085 public function getStripList() {
1086 return $this->mStripList;
1087 }
1088
1089 /**
1090 * Add an item to the strip state
1091 * Returns the unique tag which must be inserted into the stripped text
1092 * The tag will be replaced with the original text in unstrip()
1093 *
1094 * @param string $text
1095 *
1096 * @return string
1097 */
1098 public function insertStripItem( $text ) {
1099 $marker = self::MARKER_PREFIX . "-item-{$this->mMarkerIndex}-" . self::MARKER_SUFFIX;
1100 $this->mMarkerIndex++;
1101 $this->mStripState->addGeneral( $marker, $text );
1102 return $marker;
1103 }
1104
1105 /**
1106 * parse the wiki syntax used to render tables
1107 *
1108 * @private
1109 * @param string $text
1110 * @return string
1111 */
1112 public function doTableStuff( $text ) {
1113 $lines = StringUtils::explode( "\n", $text );
1114 $out = '';
1115 $td_history = []; # Is currently a td tag open?
1116 $last_tag_history = []; # Save history of last lag activated (td, th or caption)
1117 $tr_history = []; # Is currently a tr tag open?
1118 $tr_attributes = []; # history of tr attributes
1119 $has_opened_tr = []; # Did this table open a <tr> element?
1120 $indent_level = 0; # indent level of the table
1121
1122 foreach ( $lines as $outLine ) {
1123 $line = trim( $outLine );
1124
1125 if ( $line === '' ) { # empty line, go to next line
1126 $out .= $outLine . "\n";
1127 continue;
1128 }
1129
1130 $first_character = $line[0];
1131 $first_two = substr( $line, 0, 2 );
1132 $matches = [];
1133
1134 if ( preg_match( '/^(:*)\s*\{\|(.*)$/', $line, $matches ) ) {
1135 # First check if we are starting a new table
1136 $indent_level = strlen( $matches[1] );
1137
1138 $attributes = $this->mStripState->unstripBoth( $matches[2] );
1139 $attributes = Sanitizer::fixTagAttributes( $attributes, 'table' );
1140
1141 $outLine = str_repeat( '<dl><dd>', $indent_level ) . "<table{$attributes}>";
1142 array_push( $td_history, false );
1143 array_push( $last_tag_history, '' );
1144 array_push( $tr_history, false );
1145 array_push( $tr_attributes, '' );
1146 array_push( $has_opened_tr, false );
1147 } elseif ( count( $td_history ) == 0 ) {
1148 # Don't do any of the following
1149 $out .= $outLine . "\n";
1150 continue;
1151 } elseif ( $first_two === '|}' ) {
1152 # We are ending a table
1153 $line = '</table>' . substr( $line, 2 );
1154 $last_tag = array_pop( $last_tag_history );
1155
1156 if ( !array_pop( $has_opened_tr ) ) {
1157 $line = "<tr><td></td></tr>{$line}";
1158 }
1159
1160 if ( array_pop( $tr_history ) ) {
1161 $line = "</tr>{$line}";
1162 }
1163
1164 if ( array_pop( $td_history ) ) {
1165 $line = "</{$last_tag}>{$line}";
1166 }
1167 array_pop( $tr_attributes );
1168 if ( $indent_level > 0 ) {
1169 $outLine = rtrim( $line ) . str_repeat( '</dd></dl>', $indent_level );
1170 } else {
1171 $outLine = $line;
1172 }
1173 } elseif ( $first_two === '|-' ) {
1174 # Now we have a table row
1175 $line = preg_replace( '#^\|-+#', '', $line );
1176
1177 # Whats after the tag is now only attributes
1178 $attributes = $this->mStripState->unstripBoth( $line );
1179 $attributes = Sanitizer::fixTagAttributes( $attributes, 'tr' );
1180 array_pop( $tr_attributes );
1181 array_push( $tr_attributes, $attributes );
1182
1183 $line = '';
1184 $last_tag = array_pop( $last_tag_history );
1185 array_pop( $has_opened_tr );
1186 array_push( $has_opened_tr, true );
1187
1188 if ( array_pop( $tr_history ) ) {
1189 $line = '</tr>';
1190 }
1191
1192 if ( array_pop( $td_history ) ) {
1193 $line = "</{$last_tag}>{$line}";
1194 }
1195
1196 $outLine = $line;
1197 array_push( $tr_history, false );
1198 array_push( $td_history, false );
1199 array_push( $last_tag_history, '' );
1200 } elseif ( $first_character === '|'
1201 || $first_character === '!'
1202 || $first_two === '|+'
1203 ) {
1204 # This might be cell elements, td, th or captions
1205 if ( $first_two === '|+' ) {
1206 $first_character = '+';
1207 $line = substr( $line, 2 );
1208 } else {
1209 $line = substr( $line, 1 );
1210 }
1211
1212 // Implies both are valid for table headings.
1213 if ( $first_character === '!' ) {
1214 $line = StringUtils::replaceMarkup( '!!', '||', $line );
1215 }
1216
1217 # Split up multiple cells on the same line.
1218 # FIXME : This can result in improper nesting of tags processed
1219 # by earlier parser steps.
1220 $cells = explode( '||', $line );
1221
1222 $outLine = '';
1223
1224 # Loop through each table cell
1225 foreach ( $cells as $cell ) {
1226 $previous = '';
1227 if ( $first_character !== '+' ) {
1228 $tr_after = array_pop( $tr_attributes );
1229 if ( !array_pop( $tr_history ) ) {
1230 $previous = "<tr{$tr_after}>\n";
1231 }
1232 array_push( $tr_history, true );
1233 array_push( $tr_attributes, '' );
1234 array_pop( $has_opened_tr );
1235 array_push( $has_opened_tr, true );
1236 }
1237
1238 $last_tag = array_pop( $last_tag_history );
1239
1240 if ( array_pop( $td_history ) ) {
1241 $previous = "</{$last_tag}>\n{$previous}";
1242 }
1243
1244 if ( $first_character === '|' ) {
1245 $last_tag = 'td';
1246 } elseif ( $first_character === '!' ) {
1247 $last_tag = 'th';
1248 } elseif ( $first_character === '+' ) {
1249 $last_tag = 'caption';
1250 } else {
1251 $last_tag = '';
1252 }
1253
1254 array_push( $last_tag_history, $last_tag );
1255
1256 # A cell could contain both parameters and data
1257 $cell_data = explode( '|', $cell, 2 );
1258
1259 # T2553: Note that a '|' inside an invalid link should not
1260 # be mistaken as delimiting cell parameters
1261 # Bug T153140: Neither should language converter markup.
1262 if ( preg_match( '/\[\[|-\{/', $cell_data[0] ) === 1 ) {
1263 $cell = "{$previous}<{$last_tag}>" . trim( $cell );
1264 } elseif ( count( $cell_data ) == 1 ) {
1265 // Whitespace in cells is trimmed
1266 $cell = "{$previous}<{$last_tag}>" . trim( $cell_data[0] );
1267 } else {
1268 $attributes = $this->mStripState->unstripBoth( $cell_data[0] );
1269 $attributes = Sanitizer::fixTagAttributes( $attributes, $last_tag );
1270 // Whitespace in cells is trimmed
1271 $cell = "{$previous}<{$last_tag}{$attributes}>" . trim( $cell_data[1] );
1272 }
1273
1274 $outLine .= $cell;
1275 array_push( $td_history, true );
1276 }
1277 }
1278 $out .= $outLine . "\n";
1279 }
1280
1281 # Closing open td, tr && table
1282 while ( count( $td_history ) > 0 ) {
1283 if ( array_pop( $td_history ) ) {
1284 $out .= "</td>\n";
1285 }
1286 if ( array_pop( $tr_history ) ) {
1287 $out .= "</tr>\n";
1288 }
1289 if ( !array_pop( $has_opened_tr ) ) {
1290 $out .= "<tr><td></td></tr>\n";
1291 }
1292
1293 $out .= "</table>\n";
1294 }
1295
1296 # Remove trailing line-ending (b/c)
1297 if ( substr( $out, -1 ) === "\n" ) {
1298 $out = substr( $out, 0, -1 );
1299 }
1300
1301 # special case: don't return empty table
1302 if ( $out === "<table>\n<tr><td></td></tr>\n</table>" ) {
1303 $out = '';
1304 }
1305
1306 return $out;
1307 }
1308
1309 /**
1310 * Helper function for parse() that transforms wiki markup into half-parsed
1311 * HTML. Only called for $mOutputType == self::OT_HTML.
1312 *
1313 * @private
1314 *
1315 * @param string $text The text to parse
1316 * @param-taint $text escapes_html
1317 * @param bool $isMain Whether this is being called from the main parse() function
1318 * @param PPFrame|bool $frame A pre-processor frame
1319 *
1320 * @return string
1321 */
1322 public function internalParse( $text, $isMain = true, $frame = false ) {
1323 $origText = $text;
1324
1325 // Avoid PHP 7.1 warning from passing $this by reference
1326 $parser = $this;
1327
1328 # Hook to suspend the parser in this state
1329 if ( !Hooks::run( 'ParserBeforeInternalParse', [ &$parser, &$text, &$this->mStripState ] ) ) {
1330 return $text;
1331 }
1332
1333 # if $frame is provided, then use $frame for replacing any variables
1334 if ( $frame ) {
1335 # use frame depth to infer how include/noinclude tags should be handled
1336 # depth=0 means this is the top-level document; otherwise it's an included document
1337 if ( !$frame->depth ) {
1338 $flag = 0;
1339 } else {
1340 $flag = self::PTD_FOR_INCLUSION;
1341 }
1342 $dom = $this->preprocessToDom( $text, $flag );
1343 $text = $frame->expand( $dom );
1344 } else {
1345 # if $frame is not provided, then use old-style replaceVariables
1346 $text = $this->replaceVariables( $text );
1347 }
1348
1349 Hooks::run( 'InternalParseBeforeSanitize', [ &$parser, &$text, &$this->mStripState ] );
1350 $text = Sanitizer::removeHTMLtags(
1351 $text,
1352 [ $this, 'attributeStripCallback' ],
1353 false,
1354 array_keys( $this->mTransparentTagHooks ),
1355 [],
1356 [ $this, 'addTrackingCategory' ]
1357 );
1358 Hooks::run( 'InternalParseBeforeLinks', [ &$parser, &$text, &$this->mStripState ] );
1359
1360 # Tables need to come after variable replacement for things to work
1361 # properly; putting them before other transformations should keep
1362 # exciting things like link expansions from showing up in surprising
1363 # places.
1364 $text = $this->doTableStuff( $text );
1365
1366 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
1367
1368 $text = $this->doDoubleUnderscore( $text );
1369
1370 $text = $this->doHeadings( $text );
1371 $text = $this->replaceInternalLinks( $text );
1372 $text = $this->doAllQuotes( $text );
1373 $text = $this->replaceExternalLinks( $text );
1374
1375 # replaceInternalLinks may sometimes leave behind
1376 # absolute URLs, which have to be masked to hide them from replaceExternalLinks
1377 $text = str_replace( self::MARKER_PREFIX . 'NOPARSE', '', $text );
1378
1379 $text = $this->doMagicLinks( $text );
1380 $text = $this->formatHeadings( $text, $origText, $isMain );
1381
1382 return $text;
1383 }
1384
1385 /**
1386 * Helper function for parse() that transforms half-parsed HTML into fully
1387 * parsed HTML.
1388 *
1389 * @param string $text
1390 * @param bool $isMain
1391 * @param bool $linestart
1392 * @return string
1393 */
1394 private function internalParseHalfParsed( $text, $isMain = true, $linestart = true ) {
1395 $text = $this->mStripState->unstripGeneral( $text );
1396
1397 // Avoid PHP 7.1 warning from passing $this by reference
1398 $parser = $this;
1399
1400 if ( $isMain ) {
1401 Hooks::run( 'ParserAfterUnstrip', [ &$parser, &$text ] );
1402 }
1403
1404 # Clean up special characters, only run once, next-to-last before doBlockLevels
1405 $text = Sanitizer::armorFrenchSpaces( $text );
1406
1407 $text = $this->doBlockLevels( $text, $linestart );
1408
1409 $this->replaceLinkHolders( $text );
1410
1411 /**
1412 * The input doesn't get language converted if
1413 * a) It's disabled
1414 * b) Content isn't converted
1415 * c) It's a conversion table
1416 * d) it is an interface message (which is in the user language)
1417 */
1418 if ( !( $this->mOptions->getDisableContentConversion()
1419 || isset( $this->mDoubleUnderscores['nocontentconvert'] ) )
1420 ) {
1421 if ( !$this->mOptions->getInterfaceMessage() ) {
1422 # The position of the convert() call should not be changed. it
1423 # assumes that the links are all replaced and the only thing left
1424 # is the <nowiki> mark.
1425 $text = $this->getTargetLanguage()->convert( $text );
1426 }
1427 }
1428
1429 $text = $this->mStripState->unstripNoWiki( $text );
1430
1431 if ( $isMain ) {
1432 Hooks::run( 'ParserBeforeTidy', [ &$parser, &$text ] );
1433 }
1434
1435 $text = $this->replaceTransparentTags( $text );
1436 $text = $this->mStripState->unstripGeneral( $text );
1437
1438 $text = Sanitizer::normalizeCharReferences( $text );
1439
1440 if ( MWTidy::isEnabled() ) {
1441 if ( $this->mOptions->getTidy() ) {
1442 $text = MWTidy::tidy( $text );
1443 }
1444 } else {
1445 # attempt to sanitize at least some nesting problems
1446 # (T4702 and quite a few others)
1447 $tidyregs = [
1448 # ''Something [http://www.cool.com cool''] -->
1449 # <i>Something</i><a href="http://www.cool.com"..><i>cool></i></a>
1450 '/(<([bi])>)(<([bi])>)?([^<]*)(<\/?a[^<]*>)([^<]*)(<\/\\4>)?(<\/\\2>)/' =>
1451 '\\1\\3\\5\\8\\9\\6\\1\\3\\7\\8\\9',
1452 # fix up an anchor inside another anchor, only
1453 # at least for a single single nested link (T5695)
1454 '/(<a[^>]+>)([^<]*)(<a[^>]+>[^<]*)<\/a>(.*)<\/a>/' =>
1455 '\\1\\2</a>\\3</a>\\1\\4</a>',
1456 # fix div inside inline elements- doBlockLevels won't wrap a line which
1457 # contains a div, so fix it up here; replace
1458 # div with escaped text
1459 '/(<([aib]) [^>]+>)([^<]*)(<div([^>]*)>)(.*)(<\/div>)([^<]*)(<\/\\2>)/' =>
1460 '\\1\\3&lt;div\\5&gt;\\6&lt;/div&gt;\\8\\9',
1461 # remove empty italic or bold tag pairs, some
1462 # introduced by rules above
1463 '/<([bi])><\/\\1>/' => '',
1464 ];
1465
1466 $text = preg_replace(
1467 array_keys( $tidyregs ),
1468 array_values( $tidyregs ),
1469 $text );
1470 }
1471
1472 if ( $isMain ) {
1473 Hooks::run( 'ParserAfterTidy', [ &$parser, &$text ] );
1474 }
1475
1476 return $text;
1477 }
1478
1479 /**
1480 * Replace special strings like "ISBN xxx" and "RFC xxx" with
1481 * magic external links.
1482 *
1483 * DML
1484 * @private
1485 *
1486 * @param string $text
1487 *
1488 * @return string
1489 */
1490 public function doMagicLinks( $text ) {
1491 $prots = wfUrlProtocolsWithoutProtRel();
1492 $urlChar = self::EXT_LINK_URL_CLASS;
1493 $addr = self::EXT_LINK_ADDR;
1494 $space = self::SPACE_NOT_NL; # non-newline space
1495 $spdash = "(?:-|$space)"; # a dash or a non-newline space
1496 $spaces = "$space++"; # possessive match of 1 or more spaces
1497 $text = preg_replace_callback(
1498 '!(?: # Start cases
1499 (<a[ \t\r\n>].*?</a>) | # m[1]: Skip link text
1500 (<.*?>) | # m[2]: Skip stuff inside HTML elements' . "
1501 (\b # m[3]: Free external links
1502 (?i:$prots)
1503 ($addr$urlChar*) # m[4]: Post-protocol path
1504 ) |
1505 \b(?:RFC|PMID) $spaces # m[5]: RFC or PMID, capture number
1506 ([0-9]+)\b |
1507 \bISBN $spaces ( # m[6]: ISBN, capture number
1508 (?: 97[89] $spdash? )? # optional 13-digit ISBN prefix
1509 (?: [0-9] $spdash? ){9} # 9 digits with opt. delimiters
1510 [0-9Xx] # check digit
1511 )\b
1512 )!xu", [ $this, 'magicLinkCallback' ], $text );
1513 return $text;
1514 }
1515
1516 /**
1517 * @throws MWException
1518 * @param array $m
1519 * @return string HTML
1520 */
1521 public function magicLinkCallback( $m ) {
1522 if ( isset( $m[1] ) && $m[1] !== '' ) {
1523 # Skip anchor
1524 return $m[0];
1525 } elseif ( isset( $m[2] ) && $m[2] !== '' ) {
1526 # Skip HTML element
1527 return $m[0];
1528 } elseif ( isset( $m[3] ) && $m[3] !== '' ) {
1529 # Free external link
1530 return $this->makeFreeExternalLink( $m[0], strlen( $m[4] ) );
1531 } elseif ( isset( $m[5] ) && $m[5] !== '' ) {
1532 # RFC or PMID
1533 if ( substr( $m[0], 0, 3 ) === 'RFC' ) {
1534 if ( !$this->mOptions->getMagicRFCLinks() ) {
1535 return $m[0];
1536 }
1537 $keyword = 'RFC';
1538 $urlmsg = 'rfcurl';
1539 $cssClass = 'mw-magiclink-rfc';
1540 $trackingCat = 'magiclink-tracking-rfc';
1541 $id = $m[5];
1542 } elseif ( substr( $m[0], 0, 4 ) === 'PMID' ) {
1543 if ( !$this->mOptions->getMagicPMIDLinks() ) {
1544 return $m[0];
1545 }
1546 $keyword = 'PMID';
1547 $urlmsg = 'pubmedurl';
1548 $cssClass = 'mw-magiclink-pmid';
1549 $trackingCat = 'magiclink-tracking-pmid';
1550 $id = $m[5];
1551 } else {
1552 throw new MWException( __METHOD__ . ': unrecognised match type "' .
1553 substr( $m[0], 0, 20 ) . '"' );
1554 }
1555 $url = wfMessage( $urlmsg, $id )->inContentLanguage()->text();
1556 $this->addTrackingCategory( $trackingCat );
1557 return Linker::makeExternalLink( $url, "{$keyword} {$id}", true, $cssClass, [], $this->mTitle );
1558 } elseif ( isset( $m[6] ) && $m[6] !== ''
1559 && $this->mOptions->getMagicISBNLinks()
1560 ) {
1561 # ISBN
1562 $isbn = $m[6];
1563 $space = self::SPACE_NOT_NL; # non-newline space
1564 $isbn = preg_replace( "/$space/", ' ', $isbn );
1565 $num = strtr( $isbn, [
1566 '-' => '',
1567 ' ' => '',
1568 'x' => 'X',
1569 ] );
1570 $this->addTrackingCategory( 'magiclink-tracking-isbn' );
1571 return $this->getLinkRenderer()->makeKnownLink(
1572 SpecialPage::getTitleFor( 'Booksources', $num ),
1573 "ISBN $isbn",
1574 [
1575 'class' => 'internal mw-magiclink-isbn',
1576 'title' => false // suppress title attribute
1577 ]
1578 );
1579 } else {
1580 return $m[0];
1581 }
1582 }
1583
1584 /**
1585 * Make a free external link, given a user-supplied URL
1586 *
1587 * @param string $url
1588 * @param int $numPostProto
1589 * The number of characters after the protocol.
1590 * @return string HTML
1591 * @private
1592 */
1593 public function makeFreeExternalLink( $url, $numPostProto ) {
1594 $trail = '';
1595
1596 # The characters '<' and '>' (which were escaped by
1597 # removeHTMLtags()) should not be included in
1598 # URLs, per RFC 2396.
1599 # Make &nbsp; terminate a URL as well (bug T84937)
1600 $m2 = [];
1601 if ( preg_match(
1602 '/&(lt|gt|nbsp|#x0*(3[CcEe]|[Aa]0)|#0*(60|62|160));/',
1603 $url,
1604 $m2,
1605 PREG_OFFSET_CAPTURE
1606 ) ) {
1607 $trail = substr( $url, $m2[0][1] ) . $trail;
1608 $url = substr( $url, 0, $m2[0][1] );
1609 }
1610
1611 # Move trailing punctuation to $trail
1612 $sep = ',;\.:!?';
1613 # If there is no left bracket, then consider right brackets fair game too
1614 if ( strpos( $url, '(' ) === false ) {
1615 $sep .= ')';
1616 }
1617
1618 $urlRev = strrev( $url );
1619 $numSepChars = strspn( $urlRev, $sep );
1620 # Don't break a trailing HTML entity by moving the ; into $trail
1621 # This is in hot code, so use substr_compare to avoid having to
1622 # create a new string object for the comparison
1623 if ( $numSepChars && substr_compare( $url, ";", -$numSepChars, 1 ) === 0 ) {
1624 # more optimization: instead of running preg_match with a $
1625 # anchor, which can be slow, do the match on the reversed
1626 # string starting at the desired offset.
1627 # un-reversed regexp is: /&([a-z]+|#x[\da-f]+|#\d+)$/i
1628 if ( preg_match( '/\G([a-z]+|[\da-f]+x#|\d+#)&/i', $urlRev, $m2, 0, $numSepChars ) ) {
1629 $numSepChars--;
1630 }
1631 }
1632 if ( $numSepChars ) {
1633 $trail = substr( $url, -$numSepChars ) . $trail;
1634 $url = substr( $url, 0, -$numSepChars );
1635 }
1636
1637 # Verify that we still have a real URL after trail removal, and
1638 # not just lone protocol
1639 if ( strlen( $trail ) >= $numPostProto ) {
1640 return $url . $trail;
1641 }
1642
1643 $url = Sanitizer::cleanUrl( $url );
1644
1645 # Is this an external image?
1646 $text = $this->maybeMakeExternalImage( $url );
1647 if ( $text === false ) {
1648 # Not an image, make a link
1649 $text = Linker::makeExternalLink( $url,
1650 $this->getTargetLanguage()->getConverter()->markNoConversion( $url ),
1651 true, 'free',
1652 $this->getExternalLinkAttribs( $url ), $this->mTitle );
1653 # Register it in the output object...
1654 $this->mOutput->addExternalLink( $url );
1655 }
1656 return $text . $trail;
1657 }
1658
1659 /**
1660 * Parse headers and return html
1661 *
1662 * @private
1663 *
1664 * @param string $text
1665 *
1666 * @return string
1667 */
1668 public function doHeadings( $text ) {
1669 for ( $i = 6; $i >= 1; --$i ) {
1670 $h = str_repeat( '=', $i );
1671 // Trim non-newline whitespace from headings
1672 // Using \s* will break for: "==\n===\n" and parse as <h2>=</h2>
1673 $text = preg_replace( "/^(?:$h)[ \\t]*(.+?)[ \\t]*(?:$h)\\s*$/m", "<h$i>\\1</h$i>", $text );
1674 }
1675 return $text;
1676 }
1677
1678 /**
1679 * Replace single quotes with HTML markup
1680 * @private
1681 *
1682 * @param string $text
1683 *
1684 * @return string The altered text
1685 */
1686 public function doAllQuotes( $text ) {
1687 $outtext = '';
1688 $lines = StringUtils::explode( "\n", $text );
1689 foreach ( $lines as $line ) {
1690 $outtext .= $this->doQuotes( $line ) . "\n";
1691 }
1692 $outtext = substr( $outtext, 0, -1 );
1693 return $outtext;
1694 }
1695
1696 /**
1697 * Helper function for doAllQuotes()
1698 *
1699 * @param string $text
1700 *
1701 * @return string
1702 */
1703 public function doQuotes( $text ) {
1704 $arr = preg_split( "/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE );
1705 $countarr = count( $arr );
1706 if ( $countarr == 1 ) {
1707 return $text;
1708 }
1709
1710 // First, do some preliminary work. This may shift some apostrophes from
1711 // being mark-up to being text. It also counts the number of occurrences
1712 // of bold and italics mark-ups.
1713 $numbold = 0;
1714 $numitalics = 0;
1715 for ( $i = 1; $i < $countarr; $i += 2 ) {
1716 $thislen = strlen( $arr[$i] );
1717 // If there are ever four apostrophes, assume the first is supposed to
1718 // be text, and the remaining three constitute mark-up for bold text.
1719 // (T15227: ''''foo'''' turns into ' ''' foo ' ''')
1720 if ( $thislen == 4 ) {
1721 $arr[$i - 1] .= "'";
1722 $arr[$i] = "'''";
1723 $thislen = 3;
1724 } elseif ( $thislen > 5 ) {
1725 // If there are more than 5 apostrophes in a row, assume they're all
1726 // text except for the last 5.
1727 // (T15227: ''''''foo'''''' turns into ' ''''' foo ' ''''')
1728 $arr[$i - 1] .= str_repeat( "'", $thislen - 5 );
1729 $arr[$i] = "'''''";
1730 $thislen = 5;
1731 }
1732 // Count the number of occurrences of bold and italics mark-ups.
1733 if ( $thislen == 2 ) {
1734 $numitalics++;
1735 } elseif ( $thislen == 3 ) {
1736 $numbold++;
1737 } elseif ( $thislen == 5 ) {
1738 $numitalics++;
1739 $numbold++;
1740 }
1741 }
1742
1743 // If there is an odd number of both bold and italics, it is likely
1744 // that one of the bold ones was meant to be an apostrophe followed
1745 // by italics. Which one we cannot know for certain, but it is more
1746 // likely to be one that has a single-letter word before it.
1747 if ( ( $numbold % 2 == 1 ) && ( $numitalics % 2 == 1 ) ) {
1748 $firstsingleletterword = -1;
1749 $firstmultiletterword = -1;
1750 $firstspace = -1;
1751 for ( $i = 1; $i < $countarr; $i += 2 ) {
1752 if ( strlen( $arr[$i] ) == 3 ) {
1753 $x1 = substr( $arr[$i - 1], -1 );
1754 $x2 = substr( $arr[$i - 1], -2, 1 );
1755 if ( $x1 === ' ' ) {
1756 if ( $firstspace == -1 ) {
1757 $firstspace = $i;
1758 }
1759 } elseif ( $x2 === ' ' ) {
1760 $firstsingleletterword = $i;
1761 // if $firstsingleletterword is set, we don't
1762 // look at the other options, so we can bail early.
1763 break;
1764 } else {
1765 if ( $firstmultiletterword == -1 ) {
1766 $firstmultiletterword = $i;
1767 }
1768 }
1769 }
1770 }
1771
1772 // If there is a single-letter word, use it!
1773 if ( $firstsingleletterword > -1 ) {
1774 $arr[$firstsingleletterword] = "''";
1775 $arr[$firstsingleletterword - 1] .= "'";
1776 } elseif ( $firstmultiletterword > -1 ) {
1777 // If not, but there's a multi-letter word, use that one.
1778 $arr[$firstmultiletterword] = "''";
1779 $arr[$firstmultiletterword - 1] .= "'";
1780 } elseif ( $firstspace > -1 ) {
1781 // ... otherwise use the first one that has neither.
1782 // (notice that it is possible for all three to be -1 if, for example,
1783 // there is only one pentuple-apostrophe in the line)
1784 $arr[$firstspace] = "''";
1785 $arr[$firstspace - 1] .= "'";
1786 }
1787 }
1788
1789 // Now let's actually convert our apostrophic mush to HTML!
1790 $output = '';
1791 $buffer = '';
1792 $state = '';
1793 $i = 0;
1794 foreach ( $arr as $r ) {
1795 if ( ( $i % 2 ) == 0 ) {
1796 if ( $state === 'both' ) {
1797 $buffer .= $r;
1798 } else {
1799 $output .= $r;
1800 }
1801 } else {
1802 $thislen = strlen( $r );
1803 if ( $thislen == 2 ) {
1804 if ( $state === 'i' ) {
1805 $output .= '</i>';
1806 $state = '';
1807 } elseif ( $state === 'bi' ) {
1808 $output .= '</i>';
1809 $state = 'b';
1810 } elseif ( $state === 'ib' ) {
1811 $output .= '</b></i><b>';
1812 $state = 'b';
1813 } elseif ( $state === 'both' ) {
1814 $output .= '<b><i>' . $buffer . '</i>';
1815 $state = 'b';
1816 } else { // $state can be 'b' or ''
1817 $output .= '<i>';
1818 $state .= 'i';
1819 }
1820 } elseif ( $thislen == 3 ) {
1821 if ( $state === 'b' ) {
1822 $output .= '</b>';
1823 $state = '';
1824 } elseif ( $state === 'bi' ) {
1825 $output .= '</i></b><i>';
1826 $state = 'i';
1827 } elseif ( $state === 'ib' ) {
1828 $output .= '</b>';
1829 $state = 'i';
1830 } elseif ( $state === 'both' ) {
1831 $output .= '<i><b>' . $buffer . '</b>';
1832 $state = 'i';
1833 } else { // $state can be 'i' or ''
1834 $output .= '<b>';
1835 $state .= 'b';
1836 }
1837 } elseif ( $thislen == 5 ) {
1838 if ( $state === 'b' ) {
1839 $output .= '</b><i>';
1840 $state = 'i';
1841 } elseif ( $state === 'i' ) {
1842 $output .= '</i><b>';
1843 $state = 'b';
1844 } elseif ( $state === 'bi' ) {
1845 $output .= '</i></b>';
1846 $state = '';
1847 } elseif ( $state === 'ib' ) {
1848 $output .= '</b></i>';
1849 $state = '';
1850 } elseif ( $state === 'both' ) {
1851 $output .= '<i><b>' . $buffer . '</b></i>';
1852 $state = '';
1853 } else { // ($state == '')
1854 $buffer = '';
1855 $state = 'both';
1856 }
1857 }
1858 }
1859 $i++;
1860 }
1861 // Now close all remaining tags. Notice that the order is important.
1862 if ( $state === 'b' || $state === 'ib' ) {
1863 $output .= '</b>';
1864 }
1865 if ( $state === 'i' || $state === 'bi' || $state === 'ib' ) {
1866 $output .= '</i>';
1867 }
1868 if ( $state === 'bi' ) {
1869 $output .= '</b>';
1870 }
1871 // There might be lonely ''''', so make sure we have a buffer
1872 if ( $state === 'both' && $buffer ) {
1873 $output .= '<b><i>' . $buffer . '</i></b>';
1874 }
1875 return $output;
1876 }
1877
1878 /**
1879 * Replace external links (REL)
1880 *
1881 * Note: this is all very hackish and the order of execution matters a lot.
1882 * Make sure to run tests/parser/parserTests.php if you change this code.
1883 *
1884 * @private
1885 *
1886 * @param string $text
1887 *
1888 * @throws MWException
1889 * @return string
1890 */
1891 public function replaceExternalLinks( $text ) {
1892 $bits = preg_split( $this->mExtLinkBracketedRegex, $text, -1, PREG_SPLIT_DELIM_CAPTURE );
1893 if ( $bits === false ) {
1894 throw new MWException( "PCRE needs to be compiled with "
1895 . "--enable-unicode-properties in order for MediaWiki to function" );
1896 }
1897 $s = array_shift( $bits );
1898
1899 $i = 0;
1900 while ( $i < count( $bits ) ) {
1901 $url = $bits[$i++];
1902 $i++; // protocol
1903 $text = $bits[$i++];
1904 $trail = $bits[$i++];
1905
1906 # The characters '<' and '>' (which were escaped by
1907 # removeHTMLtags()) should not be included in
1908 # URLs, per RFC 2396.
1909 $m2 = [];
1910 if ( preg_match( '/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE ) ) {
1911 $text = substr( $url, $m2[0][1] ) . ' ' . $text;
1912 $url = substr( $url, 0, $m2[0][1] );
1913 }
1914
1915 # If the link text is an image URL, replace it with an <img> tag
1916 # This happened by accident in the original parser, but some people used it extensively
1917 $img = $this->maybeMakeExternalImage( $text );
1918 if ( $img !== false ) {
1919 $text = $img;
1920 }
1921
1922 $dtrail = '';
1923
1924 # Set linktype for CSS
1925 $linktype = 'text';
1926
1927 # No link text, e.g. [http://domain.tld/some.link]
1928 if ( $text == '' ) {
1929 # Autonumber
1930 $langObj = $this->getTargetLanguage();
1931 $text = '[' . $langObj->formatNum( ++$this->mAutonumber ) . ']';
1932 $linktype = 'autonumber';
1933 } else {
1934 # Have link text, e.g. [http://domain.tld/some.link text]s
1935 # Check for trail
1936 list( $dtrail, $trail ) = Linker::splitTrail( $trail );
1937 }
1938
1939 // Excluding protocol-relative URLs may avoid many false positives.
1940 if ( preg_match( '/^(?:' . wfUrlProtocolsWithoutProtRel() . ')/', $text ) ) {
1941 $text = $this->getTargetLanguage()->getConverter()->markNoConversion( $text );
1942 }
1943
1944 $url = Sanitizer::cleanUrl( $url );
1945
1946 # Use the encoded URL
1947 # This means that users can paste URLs directly into the text
1948 # Funny characters like ö aren't valid in URLs anyway
1949 # This was changed in August 2004
1950 $s .= Linker::makeExternalLink( $url, $text, false, $linktype,
1951 $this->getExternalLinkAttribs( $url ), $this->mTitle ) . $dtrail . $trail;
1952
1953 # Register link in the output object.
1954 $this->mOutput->addExternalLink( $url );
1955 }
1956
1957 return $s;
1958 }
1959
1960 /**
1961 * Get the rel attribute for a particular external link.
1962 *
1963 * @since 1.21
1964 * @param string|bool $url Optional URL, to extract the domain from for rel =>
1965 * nofollow if appropriate
1966 * @param Title|null $title Optional Title, for wgNoFollowNsExceptions lookups
1967 * @return string|null Rel attribute for $url
1968 */
1969 public static function getExternalLinkRel( $url = false, $title = null ) {
1970 global $wgNoFollowLinks, $wgNoFollowNsExceptions, $wgNoFollowDomainExceptions;
1971 $ns = $title ? $title->getNamespace() : false;
1972 if ( $wgNoFollowLinks && !in_array( $ns, $wgNoFollowNsExceptions )
1973 && !wfMatchesDomainList( $url, $wgNoFollowDomainExceptions )
1974 ) {
1975 return 'nofollow';
1976 }
1977 return null;
1978 }
1979
1980 /**
1981 * Get an associative array of additional HTML attributes appropriate for a
1982 * particular external link. This currently may include rel => nofollow
1983 * (depending on configuration, namespace, and the URL's domain) and/or a
1984 * target attribute (depending on configuration).
1985 *
1986 * @param string $url URL to extract the domain from for rel =>
1987 * nofollow if appropriate
1988 * @return array Associative array of HTML attributes
1989 */
1990 public function getExternalLinkAttribs( $url ) {
1991 $attribs = [];
1992 $rel = self::getExternalLinkRel( $url, $this->mTitle );
1993
1994 $target = $this->mOptions->getExternalLinkTarget();
1995 if ( $target ) {
1996 $attribs['target'] = $target;
1997 if ( !in_array( $target, [ '_self', '_parent', '_top' ] ) ) {
1998 // T133507. New windows can navigate parent cross-origin.
1999 // Including noreferrer due to lacking browser
2000 // support of noopener. Eventually noreferrer should be removed.
2001 if ( $rel !== '' ) {
2002 $rel .= ' ';
2003 }
2004 $rel .= 'noreferrer noopener';
2005 }
2006 }
2007 $attribs['rel'] = $rel;
2008 return $attribs;
2009 }
2010
2011 /**
2012 * Replace unusual escape codes in a URL with their equivalent characters
2013 *
2014 * This generally follows the syntax defined in RFC 3986, with special
2015 * consideration for HTTP query strings.
2016 *
2017 * @param string $url
2018 * @return string
2019 */
2020 public static function normalizeLinkUrl( $url ) {
2021 # First, make sure unsafe characters are encoded
2022 $url = preg_replace_callback( '/[\x00-\x20"<>\[\\\\\]^`{|}\x7F-\xFF]/',
2023 function ( $m ) {
2024 return rawurlencode( $m[0] );
2025 },
2026 $url
2027 );
2028
2029 $ret = '';
2030 $end = strlen( $url );
2031
2032 # Fragment part - 'fragment'
2033 $start = strpos( $url, '#' );
2034 if ( $start !== false && $start < $end ) {
2035 $ret = self::normalizeUrlComponent(
2036 substr( $url, $start, $end - $start ), '"#%<>[\]^`{|}' ) . $ret;
2037 $end = $start;
2038 }
2039
2040 # Query part - 'query' minus &=+;
2041 $start = strpos( $url, '?' );
2042 if ( $start !== false && $start < $end ) {
2043 $ret = self::normalizeUrlComponent(
2044 substr( $url, $start, $end - $start ), '"#%<>[\]^`{|}&=+;' ) . $ret;
2045 $end = $start;
2046 }
2047
2048 # Scheme and path part - 'pchar'
2049 # (we assume no userinfo or encoded colons in the host)
2050 $ret = self::normalizeUrlComponent(
2051 substr( $url, 0, $end ), '"#%<>[\]^`{|}/?' ) . $ret;
2052
2053 return $ret;
2054 }
2055
2056 private static function normalizeUrlComponent( $component, $unsafe ) {
2057 $callback = function ( $matches ) use ( $unsafe ) {
2058 $char = urldecode( $matches[0] );
2059 $ord = ord( $char );
2060 if ( $ord > 32 && $ord < 127 && strpos( $unsafe, $char ) === false ) {
2061 # Unescape it
2062 return $char;
2063 } else {
2064 # Leave it escaped, but use uppercase for a-f
2065 return strtoupper( $matches[0] );
2066 }
2067 };
2068 return preg_replace_callback( '/%[0-9A-Fa-f]{2}/', $callback, $component );
2069 }
2070
2071 /**
2072 * make an image if it's allowed, either through the global
2073 * option, through the exception, or through the on-wiki whitelist
2074 *
2075 * @param string $url
2076 *
2077 * @return string
2078 */
2079 private function maybeMakeExternalImage( $url ) {
2080 $imagesfrom = $this->mOptions->getAllowExternalImagesFrom();
2081 $imagesexception = !empty( $imagesfrom );
2082 $text = false;
2083 # $imagesfrom could be either a single string or an array of strings, parse out the latter
2084 if ( $imagesexception && is_array( $imagesfrom ) ) {
2085 $imagematch = false;
2086 foreach ( $imagesfrom as $match ) {
2087 if ( strpos( $url, $match ) === 0 ) {
2088 $imagematch = true;
2089 break;
2090 }
2091 }
2092 } elseif ( $imagesexception ) {
2093 $imagematch = ( strpos( $url, $imagesfrom ) === 0 );
2094 } else {
2095 $imagematch = false;
2096 }
2097
2098 if ( $this->mOptions->getAllowExternalImages()
2099 || ( $imagesexception && $imagematch )
2100 ) {
2101 if ( preg_match( self::EXT_IMAGE_REGEX, $url ) ) {
2102 # Image found
2103 $text = Linker::makeExternalImage( $url );
2104 }
2105 }
2106 if ( !$text && $this->mOptions->getEnableImageWhitelist()
2107 && preg_match( self::EXT_IMAGE_REGEX, $url )
2108 ) {
2109 $whitelist = explode(
2110 "\n",
2111 wfMessage( 'external_image_whitelist' )->inContentLanguage()->text()
2112 );
2113
2114 foreach ( $whitelist as $entry ) {
2115 # Sanitize the regex fragment, make it case-insensitive, ignore blank entries/comments
2116 if ( strpos( $entry, '#' ) === 0 || $entry === '' ) {
2117 continue;
2118 }
2119 if ( preg_match( '/' . str_replace( '/', '\\/', $entry ) . '/i', $url ) ) {
2120 # Image matches a whitelist entry
2121 $text = Linker::makeExternalImage( $url );
2122 break;
2123 }
2124 }
2125 }
2126 return $text;
2127 }
2128
2129 /**
2130 * Process [[ ]] wikilinks
2131 *
2132 * @param string $s
2133 *
2134 * @return string Processed text
2135 *
2136 * @private
2137 */
2138 public function replaceInternalLinks( $s ) {
2139 $this->mLinkHolders->merge( $this->replaceInternalLinks2( $s ) );
2140 return $s;
2141 }
2142
2143 /**
2144 * Process [[ ]] wikilinks (RIL)
2145 * @param string &$s
2146 * @throws MWException
2147 * @return LinkHolderArray
2148 *
2149 * @private
2150 */
2151 public function replaceInternalLinks2( &$s ) {
2152 global $wgExtraInterlanguageLinkPrefixes;
2153
2154 static $tc = false, $e1, $e1_img;
2155 # the % is needed to support urlencoded titles as well
2156 if ( !$tc ) {
2157 $tc = Title::legalChars() . '#%';
2158 # Match a link having the form [[namespace:link|alternate]]trail
2159 $e1 = "/^([{$tc}]+)(?:\\|(.+?))?]](.*)\$/sD";
2160 # Match cases where there is no "]]", which might still be images
2161 $e1_img = "/^([{$tc}]+)\\|(.*)\$/sD";
2162 }
2163
2164 $holders = new LinkHolderArray( $this );
2165
2166 # split the entire text string on occurrences of [[
2167 $a = StringUtils::explode( '[[', ' ' . $s );
2168 # get the first element (all text up to first [[), and remove the space we added
2169 $s = $a->current();
2170 $a->next();
2171 $line = $a->current(); # Workaround for broken ArrayIterator::next() that returns "void"
2172 $s = substr( $s, 1 );
2173
2174 $useLinkPrefixExtension = $this->getTargetLanguage()->linkPrefixExtension();
2175 $e2 = null;
2176 if ( $useLinkPrefixExtension ) {
2177 # Match the end of a line for a word that's not followed by whitespace,
2178 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
2179 $charset = $this->contLang->linkPrefixCharset();
2180 $e2 = "/^((?>.*[^$charset]|))(.+)$/sDu";
2181 }
2182
2183 if ( is_null( $this->mTitle ) ) {
2184 throw new MWException( __METHOD__ . ": \$this->mTitle is null\n" );
2185 }
2186 $nottalk = !$this->mTitle->isTalkPage();
2187
2188 if ( $useLinkPrefixExtension ) {
2189 $m = [];
2190 if ( preg_match( $e2, $s, $m ) ) {
2191 $first_prefix = $m[2];
2192 } else {
2193 $first_prefix = false;
2194 }
2195 } else {
2196 $prefix = '';
2197 }
2198
2199 $useSubpages = $this->areSubpagesAllowed();
2200
2201 # Loop for each link
2202 for ( ; $line !== false && $line !== null; $a->next(), $line = $a->current() ) {
2203 # Check for excessive memory usage
2204 if ( $holders->isBig() ) {
2205 # Too big
2206 # Do the existence check, replace the link holders and clear the array
2207 $holders->replace( $s );
2208 $holders->clear();
2209 }
2210
2211 if ( $useLinkPrefixExtension ) {
2212 if ( preg_match( $e2, $s, $m ) ) {
2213 $prefix = $m[2];
2214 $s = $m[1];
2215 } else {
2216 $prefix = '';
2217 }
2218 # first link
2219 if ( $first_prefix ) {
2220 $prefix = $first_prefix;
2221 $first_prefix = false;
2222 }
2223 }
2224
2225 $might_be_img = false;
2226
2227 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
2228 $text = $m[2];
2229 # If we get a ] at the beginning of $m[3] that means we have a link that's something like:
2230 # [[Image:Foo.jpg|[http://example.com desc]]] <- having three ] in a row fucks up,
2231 # the real problem is with the $e1 regex
2232 # See T1500.
2233 # Still some problems for cases where the ] is meant to be outside punctuation,
2234 # and no image is in sight. See T4095.
2235 if ( $text !== ''
2236 && substr( $m[3], 0, 1 ) === ']'
2237 && strpos( $text, '[' ) !== false
2238 ) {
2239 $text .= ']'; # so that replaceExternalLinks($text) works later
2240 $m[3] = substr( $m[3], 1 );
2241 }
2242 # fix up urlencoded title texts
2243 if ( strpos( $m[1], '%' ) !== false ) {
2244 # Should anchors '#' also be rejected?
2245 $m[1] = str_replace( [ '<', '>' ], [ '&lt;', '&gt;' ], rawurldecode( $m[1] ) );
2246 }
2247 $trail = $m[3];
2248 } elseif ( preg_match( $e1_img, $line, $m ) ) {
2249 # Invalid, but might be an image with a link in its caption
2250 $might_be_img = true;
2251 $text = $m[2];
2252 if ( strpos( $m[1], '%' ) !== false ) {
2253 $m[1] = str_replace( [ '<', '>' ], [ '&lt;', '&gt;' ], rawurldecode( $m[1] ) );
2254 }
2255 $trail = "";
2256 } else { # Invalid form; output directly
2257 $s .= $prefix . '[[' . $line;
2258 continue;
2259 }
2260
2261 $origLink = ltrim( $m[1], ' ' );
2262
2263 # Don't allow internal links to pages containing
2264 # PROTO: where PROTO is a valid URL protocol; these
2265 # should be external links.
2266 if ( preg_match( '/^(?i:' . $this->mUrlProtocols . ')/', $origLink ) ) {
2267 $s .= $prefix . '[[' . $line;
2268 continue;
2269 }
2270
2271 # Make subpage if necessary
2272 if ( $useSubpages ) {
2273 $link = $this->maybeDoSubpageLink( $origLink, $text );
2274 } else {
2275 $link = $origLink;
2276 }
2277
2278 // \x7f isn't a default legal title char, so most likely strip
2279 // markers will force us into the "invalid form" path above. But,
2280 // just in case, let's assert that xmlish tags aren't valid in
2281 // the title position.
2282 $unstrip = $this->mStripState->killMarkers( $link );
2283 $noMarkers = ( $unstrip === $link );
2284
2285 $nt = $noMarkers ? Title::newFromText( $link ) : null;
2286 if ( $nt === null ) {
2287 $s .= $prefix . '[[' . $line;
2288 continue;
2289 }
2290
2291 $ns = $nt->getNamespace();
2292 $iw = $nt->getInterwiki();
2293
2294 $noforce = ( substr( $origLink, 0, 1 ) !== ':' );
2295
2296 if ( $might_be_img ) { # if this is actually an invalid link
2297 if ( $ns == NS_FILE && $noforce ) { # but might be an image
2298 $found = false;
2299 while ( true ) {
2300 # look at the next 'line' to see if we can close it there
2301 $a->next();
2302 $next_line = $a->current();
2303 if ( $next_line === false || $next_line === null ) {
2304 break;
2305 }
2306 $m = explode( ']]', $next_line, 3 );
2307 if ( count( $m ) == 3 ) {
2308 # the first ]] closes the inner link, the second the image
2309 $found = true;
2310 $text .= "[[{$m[0]}]]{$m[1]}";
2311 $trail = $m[2];
2312 break;
2313 } elseif ( count( $m ) == 2 ) {
2314 # if there's exactly one ]] that's fine, we'll keep looking
2315 $text .= "[[{$m[0]}]]{$m[1]}";
2316 } else {
2317 # if $next_line is invalid too, we need look no further
2318 $text .= '[[' . $next_line;
2319 break;
2320 }
2321 }
2322 if ( !$found ) {
2323 # we couldn't find the end of this imageLink, so output it raw
2324 # but don't ignore what might be perfectly normal links in the text we've examined
2325 $holders->merge( $this->replaceInternalLinks2( $text ) );
2326 $s .= "{$prefix}[[$link|$text";
2327 # note: no $trail, because without an end, there *is* no trail
2328 continue;
2329 }
2330 } else { # it's not an image, so output it raw
2331 $s .= "{$prefix}[[$link|$text";
2332 # note: no $trail, because without an end, there *is* no trail
2333 continue;
2334 }
2335 }
2336
2337 $wasblank = ( $text == '' );
2338 if ( $wasblank ) {
2339 $text = $link;
2340 if ( !$noforce ) {
2341 # Strip off leading ':'
2342 $text = substr( $text, 1 );
2343 }
2344 } else {
2345 # T6598 madness. Handle the quotes only if they come from the alternate part
2346 # [[Lista d''e paise d''o munno]] -> <a href="...">Lista d''e paise d''o munno</a>
2347 # [[Criticism of Harry Potter|Criticism of ''Harry Potter'']]
2348 # -> <a href="Criticism of Harry Potter">Criticism of <i>Harry Potter</i></a>
2349 $text = $this->doQuotes( $text );
2350 }
2351
2352 # Link not escaped by : , create the various objects
2353 if ( $noforce && !$nt->wasLocalInterwiki() ) {
2354 # Interwikis
2355 if (
2356 $iw && $this->mOptions->getInterwikiMagic() && $nottalk && (
2357 Language::fetchLanguageName( $iw, null, 'mw' ) ||
2358 in_array( $iw, $wgExtraInterlanguageLinkPrefixes )
2359 )
2360 ) {
2361 # T26502: filter duplicates
2362 if ( !isset( $this->mLangLinkLanguages[$iw] ) ) {
2363 $this->mLangLinkLanguages[$iw] = true;
2364 $this->mOutput->addLanguageLink( $nt->getFullText() );
2365 }
2366
2367 /**
2368 * Strip the whitespace interwiki links produce, see T10897
2369 */
2370 $s = rtrim( $s . $prefix ) . $trail; # T175416
2371 continue;
2372 }
2373
2374 if ( $ns == NS_FILE ) {
2375 if ( !wfIsBadImage( $nt->getDBkey(), $this->mTitle ) ) {
2376 if ( $wasblank ) {
2377 # if no parameters were passed, $text
2378 # becomes something like "File:Foo.png",
2379 # which we don't want to pass on to the
2380 # image generator
2381 $text = '';
2382 } else {
2383 # recursively parse links inside the image caption
2384 # actually, this will parse them in any other parameters, too,
2385 # but it might be hard to fix that, and it doesn't matter ATM
2386 $text = $this->replaceExternalLinks( $text );
2387 $holders->merge( $this->replaceInternalLinks2( $text ) );
2388 }
2389 # cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them
2390 $s .= $prefix . $this->armorLinks(
2391 $this->makeImage( $nt, $text, $holders ) ) . $trail;
2392 continue;
2393 }
2394 } elseif ( $ns == NS_CATEGORY ) {
2395 /**
2396 * Strip the whitespace Category links produce, see T2087
2397 */
2398 $s = rtrim( $s . $prefix ) . $trail; # T2087, T87753
2399
2400 if ( $wasblank ) {
2401 $sortkey = $this->getDefaultSort();
2402 } else {
2403 $sortkey = $text;
2404 }
2405 $sortkey = Sanitizer::decodeCharReferences( $sortkey );
2406 $sortkey = str_replace( "\n", '', $sortkey );
2407 $sortkey = $this->getTargetLanguage()->convertCategoryKey( $sortkey );
2408 $this->mOutput->addCategory( $nt->getDBkey(), $sortkey );
2409
2410 continue;
2411 }
2412 }
2413
2414 # Self-link checking. For some languages, variants of the title are checked in
2415 # LinkHolderArray::doVariants() to allow batching the existence checks necessary
2416 # for linking to a different variant.
2417 if ( $ns != NS_SPECIAL && $nt->equals( $this->mTitle ) && !$nt->hasFragment() ) {
2418 $s .= $prefix . Linker::makeSelfLinkObj( $nt, $text, '', $trail );
2419 continue;
2420 }
2421
2422 # NS_MEDIA is a pseudo-namespace for linking directly to a file
2423 # @todo FIXME: Should do batch file existence checks, see comment below
2424 if ( $ns == NS_MEDIA ) {
2425 # Give extensions a chance to select the file revision for us
2426 $options = [];
2427 $descQuery = false;
2428 Hooks::run( 'BeforeParserFetchFileAndTitle',
2429 [ $this, $nt, &$options, &$descQuery ] );
2430 # Fetch and register the file (file title may be different via hooks)
2431 list( $file, $nt ) = $this->fetchFileAndTitle( $nt, $options );
2432 # Cloak with NOPARSE to avoid replacement in replaceExternalLinks
2433 $s .= $prefix . $this->armorLinks(
2434 Linker::makeMediaLinkFile( $nt, $file, $text ) ) . $trail;
2435 continue;
2436 }
2437
2438 # Some titles, such as valid special pages or files in foreign repos, should
2439 # be shown as bluelinks even though they're not included in the page table
2440 # @todo FIXME: isAlwaysKnown() can be expensive for file links; we should really do
2441 # batch file existence checks for NS_FILE and NS_MEDIA
2442 if ( $iw == '' && $nt->isAlwaysKnown() ) {
2443 $this->mOutput->addLink( $nt );
2444 $s .= $this->makeKnownLinkHolder( $nt, $text, $trail, $prefix );
2445 } else {
2446 # Links will be added to the output link list after checking
2447 $s .= $holders->makeHolder( $nt, $text, [], $trail, $prefix );
2448 }
2449 }
2450 return $holders;
2451 }
2452
2453 /**
2454 * Render a forced-blue link inline; protect against double expansion of
2455 * URLs if we're in a mode that prepends full URL prefixes to internal links.
2456 * Since this little disaster has to split off the trail text to avoid
2457 * breaking URLs in the following text without breaking trails on the
2458 * wiki links, it's been made into a horrible function.
2459 *
2460 * @param Title $nt
2461 * @param string $text
2462 * @param string $trail
2463 * @param string $prefix
2464 * @return string HTML-wikitext mix oh yuck
2465 */
2466 protected function makeKnownLinkHolder( $nt, $text = '', $trail = '', $prefix = '' ) {
2467 list( $inside, $trail ) = Linker::splitTrail( $trail );
2468
2469 if ( $text == '' ) {
2470 $text = htmlspecialchars( $nt->getPrefixedText() );
2471 }
2472
2473 $link = $this->getLinkRenderer()->makeKnownLink(
2474 $nt, new HtmlArmor( "$prefix$text$inside" )
2475 );
2476
2477 return $this->armorLinks( $link ) . $trail;
2478 }
2479
2480 /**
2481 * Insert a NOPARSE hacky thing into any inline links in a chunk that's
2482 * going to go through further parsing steps before inline URL expansion.
2483 *
2484 * Not needed quite as much as it used to be since free links are a bit
2485 * more sensible these days. But bracketed links are still an issue.
2486 *
2487 * @param string $text More-or-less HTML
2488 * @return string Less-or-more HTML with NOPARSE bits
2489 */
2490 public function armorLinks( $text ) {
2491 return preg_replace( '/\b((?i)' . $this->mUrlProtocols . ')/',
2492 self::MARKER_PREFIX . "NOPARSE$1", $text );
2493 }
2494
2495 /**
2496 * Return true if subpage links should be expanded on this page.
2497 * @return bool
2498 */
2499 public function areSubpagesAllowed() {
2500 # Some namespaces don't allow subpages
2501 return MWNamespace::hasSubpages( $this->mTitle->getNamespace() );
2502 }
2503
2504 /**
2505 * Handle link to subpage if necessary
2506 *
2507 * @param string $target The source of the link
2508 * @param string &$text The link text, modified as necessary
2509 * @return string The full name of the link
2510 * @private
2511 */
2512 public function maybeDoSubpageLink( $target, &$text ) {
2513 return Linker::normalizeSubpageLink( $this->mTitle, $target, $text );
2514 }
2515
2516 /**
2517 * Make lists from lines starting with ':', '*', '#', etc. (DBL)
2518 *
2519 * @param string $text
2520 * @param bool $linestart Whether or not this is at the start of a line.
2521 * @private
2522 * @return string The lists rendered as HTML
2523 */
2524 public function doBlockLevels( $text, $linestart ) {
2525 return BlockLevelPass::doBlockLevels( $text, $linestart );
2526 }
2527
2528 /**
2529 * Return value of a magic variable (like PAGENAME)
2530 *
2531 * @private
2532 *
2533 * @param string $index Magic variable identifier as mapped in MagicWordFactory::$mVariableIDs
2534 * @param bool|PPFrame $frame
2535 *
2536 * @throws MWException
2537 * @return string
2538 */
2539 public function getVariableValue( $index, $frame = false ) {
2540 global $wgSitename, $wgServer, $wgServerName;
2541 global $wgArticlePath, $wgScriptPath, $wgStylePath;
2542
2543 if ( is_null( $this->mTitle ) ) {
2544 // If no title set, bad things are going to happen
2545 // later. Title should always be set since this
2546 // should only be called in the middle of a parse
2547 // operation (but the unit-tests do funky stuff)
2548 throw new MWException( __METHOD__ . ' Should only be '
2549 . ' called while parsing (no title set)' );
2550 }
2551
2552 // Avoid PHP 7.1 warning from passing $this by reference
2553 $parser = $this;
2554
2555 /**
2556 * Some of these require message or data lookups and can be
2557 * expensive to check many times.
2558 */
2559 if ( Hooks::run( 'ParserGetVariableValueVarCache', [ &$parser, &$this->mVarCache ] ) ) {
2560 if ( isset( $this->mVarCache[$index] ) ) {
2561 return $this->mVarCache[$index];
2562 }
2563 }
2564
2565 $ts = wfTimestamp( TS_UNIX, $this->mOptions->getTimestamp() );
2566 Hooks::run( 'ParserGetVariableValueTs', [ &$parser, &$ts ] );
2567
2568 $pageLang = $this->getFunctionLang();
2569
2570 switch ( $index ) {
2571 case '!':
2572 $value = '|';
2573 break;
2574 case 'currentmonth':
2575 $value = $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'm' ), true );
2576 break;
2577 case 'currentmonth1':
2578 $value = $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'n' ), true );
2579 break;
2580 case 'currentmonthname':
2581 $value = $pageLang->getMonthName( MWTimestamp::getInstance( $ts )->format( 'n' ) );
2582 break;
2583 case 'currentmonthnamegen':
2584 $value = $pageLang->getMonthNameGen( MWTimestamp::getInstance( $ts )->format( 'n' ) );
2585 break;
2586 case 'currentmonthabbrev':
2587 $value = $pageLang->getMonthAbbreviation( MWTimestamp::getInstance( $ts )->format( 'n' ) );
2588 break;
2589 case 'currentday':
2590 $value = $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'j' ), true );
2591 break;
2592 case 'currentday2':
2593 $value = $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'd' ), true );
2594 break;
2595 case 'localmonth':
2596 $value = $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'm' ), true );
2597 break;
2598 case 'localmonth1':
2599 $value = $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'n' ), true );
2600 break;
2601 case 'localmonthname':
2602 $value = $pageLang->getMonthName( MWTimestamp::getLocalInstance( $ts )->format( 'n' ) );
2603 break;
2604 case 'localmonthnamegen':
2605 $value = $pageLang->getMonthNameGen( MWTimestamp::getLocalInstance( $ts )->format( 'n' ) );
2606 break;
2607 case 'localmonthabbrev':
2608 $value = $pageLang->getMonthAbbreviation( MWTimestamp::getLocalInstance( $ts )->format( 'n' ) );
2609 break;
2610 case 'localday':
2611 $value = $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'j' ), true );
2612 break;
2613 case 'localday2':
2614 $value = $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'd' ), true );
2615 break;
2616 case 'pagename':
2617 $value = wfEscapeWikiText( $this->mTitle->getText() );
2618 break;
2619 case 'pagenamee':
2620 $value = wfEscapeWikiText( $this->mTitle->getPartialURL() );
2621 break;
2622 case 'fullpagename':
2623 $value = wfEscapeWikiText( $this->mTitle->getPrefixedText() );
2624 break;
2625 case 'fullpagenamee':
2626 $value = wfEscapeWikiText( $this->mTitle->getPrefixedURL() );
2627 break;
2628 case 'subpagename':
2629 $value = wfEscapeWikiText( $this->mTitle->getSubpageText() );
2630 break;
2631 case 'subpagenamee':
2632 $value = wfEscapeWikiText( $this->mTitle->getSubpageUrlForm() );
2633 break;
2634 case 'rootpagename':
2635 $value = wfEscapeWikiText( $this->mTitle->getRootText() );
2636 break;
2637 case 'rootpagenamee':
2638 $value = wfEscapeWikiText( wfUrlencode( str_replace(
2639 ' ',
2640 '_',
2641 $this->mTitle->getRootText()
2642 ) ) );
2643 break;
2644 case 'basepagename':
2645 $value = wfEscapeWikiText( $this->mTitle->getBaseText() );
2646 break;
2647 case 'basepagenamee':
2648 $value = wfEscapeWikiText( wfUrlencode( str_replace(
2649 ' ',
2650 '_',
2651 $this->mTitle->getBaseText()
2652 ) ) );
2653 break;
2654 case 'talkpagename':
2655 if ( $this->mTitle->canHaveTalkPage() ) {
2656 $talkPage = $this->mTitle->getTalkPage();
2657 $value = wfEscapeWikiText( $talkPage->getPrefixedText() );
2658 } else {
2659 $value = '';
2660 }
2661 break;
2662 case 'talkpagenamee':
2663 if ( $this->mTitle->canHaveTalkPage() ) {
2664 $talkPage = $this->mTitle->getTalkPage();
2665 $value = wfEscapeWikiText( $talkPage->getPrefixedURL() );
2666 } else {
2667 $value = '';
2668 }
2669 break;
2670 case 'subjectpagename':
2671 $subjPage = $this->mTitle->getSubjectPage();
2672 $value = wfEscapeWikiText( $subjPage->getPrefixedText() );
2673 break;
2674 case 'subjectpagenamee':
2675 $subjPage = $this->mTitle->getSubjectPage();
2676 $value = wfEscapeWikiText( $subjPage->getPrefixedURL() );
2677 break;
2678 case 'pageid': // requested in T25427
2679 $pageid = $this->getTitle()->getArticleID();
2680 if ( $pageid == 0 ) {
2681 # 0 means the page doesn't exist in the database,
2682 # which means the user is previewing a new page.
2683 # The vary-revision flag must be set, because the magic word
2684 # will have a different value once the page is saved.
2685 $this->mOutput->setFlag( 'vary-revision' );
2686 wfDebug( __METHOD__ . ": {{PAGEID}} used in a new page, setting vary-revision...\n" );
2687 }
2688 $value = $pageid ?: null;
2689 break;
2690 case 'revisionid':
2691 # Let the edit saving system know we should parse the page
2692 # *after* a revision ID has been assigned.
2693 $this->mOutput->setFlag( 'vary-revision-id' );
2694 wfDebug( __METHOD__ . ": {{REVISIONID}} used, setting vary-revision-id...\n" );
2695 $value = $this->mRevisionId;
2696 if ( !$value && $this->mOptions->getSpeculativeRevIdCallback() ) {
2697 $value = call_user_func( $this->mOptions->getSpeculativeRevIdCallback() );
2698 $this->mOutput->setSpeculativeRevIdUsed( $value );
2699 }
2700 break;
2701 case 'revisionday':
2702 $value = (int)$this->getRevisionTimestampSubstring( 6, 2, self::MAX_TTS, $index );
2703 break;
2704 case 'revisionday2':
2705 $value = $this->getRevisionTimestampSubstring( 6, 2, self::MAX_TTS, $index );
2706 break;
2707 case 'revisionmonth':
2708 $value = $this->getRevisionTimestampSubstring( 4, 2, self::MAX_TTS, $index );
2709 break;
2710 case 'revisionmonth1':
2711 $value = (int)$this->getRevisionTimestampSubstring( 4, 2, self::MAX_TTS, $index );
2712 break;
2713 case 'revisionyear':
2714 $value = $this->getRevisionTimestampSubstring( 0, 4, self::MAX_TTS, $index );
2715 break;
2716 case 'revisiontimestamp':
2717 # Let the edit saving system know we should parse the page
2718 # *after* a revision ID has been assigned. This is for null edits.
2719 $this->mOutput->setFlag( 'vary-revision' );
2720 wfDebug( __METHOD__ . ": {{REVISIONTIMESTAMP}} used, setting vary-revision...\n" );
2721 $value = $this->getRevisionTimestamp();
2722 break;
2723 case 'revisionuser':
2724 # Let the edit saving system know we should parse the page
2725 # *after* a revision ID has been assigned for null edits.
2726 $this->mOutput->setFlag( 'vary-user' );
2727 wfDebug( __METHOD__ . ": {{REVISIONUSER}} used, setting vary-user...\n" );
2728 $value = $this->getRevisionUser();
2729 break;
2730 case 'revisionsize':
2731 $value = $this->getRevisionSize();
2732 break;
2733 case 'namespace':
2734 $value = str_replace( '_', ' ',
2735 $this->contLang->getNsText( $this->mTitle->getNamespace() ) );
2736 break;
2737 case 'namespacee':
2738 $value = wfUrlencode( $this->contLang->getNsText( $this->mTitle->getNamespace() ) );
2739 break;
2740 case 'namespacenumber':
2741 $value = $this->mTitle->getNamespace();
2742 break;
2743 case 'talkspace':
2744 $value = $this->mTitle->canHaveTalkPage()
2745 ? str_replace( '_', ' ', $this->mTitle->getTalkNsText() )
2746 : '';
2747 break;
2748 case 'talkspacee':
2749 $value = $this->mTitle->canHaveTalkPage() ? wfUrlencode( $this->mTitle->getTalkNsText() ) : '';
2750 break;
2751 case 'subjectspace':
2752 $value = str_replace( '_', ' ', $this->mTitle->getSubjectNsText() );
2753 break;
2754 case 'subjectspacee':
2755 $value = ( wfUrlencode( $this->mTitle->getSubjectNsText() ) );
2756 break;
2757 case 'currentdayname':
2758 $value = $pageLang->getWeekdayName( (int)MWTimestamp::getInstance( $ts )->format( 'w' ) + 1 );
2759 break;
2760 case 'currentyear':
2761 $value = $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'Y' ), true );
2762 break;
2763 case 'currenttime':
2764 $value = $pageLang->time( wfTimestamp( TS_MW, $ts ), false, false );
2765 break;
2766 case 'currenthour':
2767 $value = $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'H' ), true );
2768 break;
2769 case 'currentweek':
2770 # @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
2771 # int to remove the padding
2772 $value = $pageLang->formatNum( (int)MWTimestamp::getInstance( $ts )->format( 'W' ) );
2773 break;
2774 case 'currentdow':
2775 $value = $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'w' ) );
2776 break;
2777 case 'localdayname':
2778 $value = $pageLang->getWeekdayName(
2779 (int)MWTimestamp::getLocalInstance( $ts )->format( 'w' ) + 1
2780 );
2781 break;
2782 case 'localyear':
2783 $value = $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'Y' ), true );
2784 break;
2785 case 'localtime':
2786 $value = $pageLang->time(
2787 MWTimestamp::getLocalInstance( $ts )->format( 'YmdHis' ),
2788 false,
2789 false
2790 );
2791 break;
2792 case 'localhour':
2793 $value = $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'H' ), true );
2794 break;
2795 case 'localweek':
2796 # @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
2797 # int to remove the padding
2798 $value = $pageLang->formatNum( (int)MWTimestamp::getLocalInstance( $ts )->format( 'W' ) );
2799 break;
2800 case 'localdow':
2801 $value = $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'w' ) );
2802 break;
2803 case 'numberofarticles':
2804 $value = $pageLang->formatNum( SiteStats::articles() );
2805 break;
2806 case 'numberoffiles':
2807 $value = $pageLang->formatNum( SiteStats::images() );
2808 break;
2809 case 'numberofusers':
2810 $value = $pageLang->formatNum( SiteStats::users() );
2811 break;
2812 case 'numberofactiveusers':
2813 $value = $pageLang->formatNum( SiteStats::activeUsers() );
2814 break;
2815 case 'numberofpages':
2816 $value = $pageLang->formatNum( SiteStats::pages() );
2817 break;
2818 case 'numberofadmins':
2819 $value = $pageLang->formatNum( SiteStats::numberingroup( 'sysop' ) );
2820 break;
2821 case 'numberofedits':
2822 $value = $pageLang->formatNum( SiteStats::edits() );
2823 break;
2824 case 'currenttimestamp':
2825 $value = wfTimestamp( TS_MW, $ts );
2826 break;
2827 case 'localtimestamp':
2828 $value = MWTimestamp::getLocalInstance( $ts )->format( 'YmdHis' );
2829 break;
2830 case 'currentversion':
2831 $value = SpecialVersion::getVersion();
2832 break;
2833 case 'articlepath':
2834 return $wgArticlePath;
2835 case 'sitename':
2836 return $wgSitename;
2837 case 'server':
2838 return $wgServer;
2839 case 'servername':
2840 return $wgServerName;
2841 case 'scriptpath':
2842 return $wgScriptPath;
2843 case 'stylepath':
2844 return $wgStylePath;
2845 case 'directionmark':
2846 return $pageLang->getDirMark();
2847 case 'contentlanguage':
2848 global $wgLanguageCode;
2849 return $wgLanguageCode;
2850 case 'pagelanguage':
2851 $value = $pageLang->getCode();
2852 break;
2853 case 'cascadingsources':
2854 $value = CoreParserFunctions::cascadingsources( $this );
2855 break;
2856 default:
2857 $ret = null;
2858 Hooks::run(
2859 'ParserGetVariableValueSwitch',
2860 [ &$parser, &$this->mVarCache, &$index, &$ret, &$frame ]
2861 );
2862
2863 return $ret;
2864 }
2865
2866 if ( $index ) {
2867 $this->mVarCache[$index] = $value;
2868 }
2869
2870 return $value;
2871 }
2872
2873 /**
2874 * @param int $start
2875 * @param int $len
2876 * @param int $mtts Max time-till-save; sets vary-revision if result might change by then
2877 * @param string $variable Parser variable name
2878 * @return string
2879 */
2880 private function getRevisionTimestampSubstring( $start, $len, $mtts, $variable ) {
2881 # Get the timezone-adjusted timestamp to be used for this revision
2882 $resNow = substr( $this->getRevisionTimestamp(), $start, $len );
2883 # Possibly set vary-revision if there is not yet an associated revision
2884 if ( !$this->getRevisionObject() ) {
2885 # Get the timezone-adjusted timestamp $mtts seconds in the future
2886 $resThen = substr(
2887 $this->contLang->userAdjust( wfTimestamp( TS_MW, time() + $mtts ), '' ),
2888 $start,
2889 $len
2890 );
2891
2892 if ( $resNow !== $resThen ) {
2893 # Let the edit saving system know we should parse the page
2894 # *after* a revision ID has been assigned. This is for null edits.
2895 $this->mOutput->setFlag( 'vary-revision' );
2896 wfDebug( __METHOD__ . ": $variable used, setting vary-revision...\n" );
2897 }
2898 }
2899
2900 return $resNow;
2901 }
2902
2903 /**
2904 * initialise the magic variables (like CURRENTMONTHNAME) and substitution modifiers
2905 *
2906 * @private
2907 */
2908 public function initialiseVariables() {
2909 $variableIDs = $this->magicWordFactory->getVariableIDs();
2910 $substIDs = $this->magicWordFactory->getSubstIDs();
2911
2912 $this->mVariables = $this->magicWordFactory->newArray( $variableIDs );
2913 $this->mSubstWords = $this->magicWordFactory->newArray( $substIDs );
2914 }
2915
2916 /**
2917 * Preprocess some wikitext and return the document tree.
2918 * This is the ghost of replace_variables().
2919 *
2920 * @param string $text The text to parse
2921 * @param int $flags Bitwise combination of:
2922 * - self::PTD_FOR_INCLUSION: Handle "<noinclude>" and "<includeonly>" as if the text is being
2923 * included. Default is to assume a direct page view.
2924 *
2925 * The generated DOM tree must depend only on the input text and the flags.
2926 * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of T6899.
2927 *
2928 * Any flag added to the $flags parameter here, or any other parameter liable to cause a
2929 * change in the DOM tree for a given text, must be passed through the section identifier
2930 * in the section edit link and thus back to extractSections().
2931 *
2932 * The output of this function is currently only cached in process memory, but a persistent
2933 * cache may be implemented at a later date which takes further advantage of these strict
2934 * dependency requirements.
2935 *
2936 * @return PPNode
2937 */
2938 public function preprocessToDom( $text, $flags = 0 ) {
2939 $dom = $this->getPreprocessor()->preprocessToObj( $text, $flags );
2940 return $dom;
2941 }
2942
2943 /**
2944 * Return a three-element array: leading whitespace, string contents, trailing whitespace
2945 *
2946 * @param string $s
2947 *
2948 * @return array
2949 */
2950 public static function splitWhitespace( $s ) {
2951 $ltrimmed = ltrim( $s );
2952 $w1 = substr( $s, 0, strlen( $s ) - strlen( $ltrimmed ) );
2953 $trimmed = rtrim( $ltrimmed );
2954 $diff = strlen( $ltrimmed ) - strlen( $trimmed );
2955 if ( $diff > 0 ) {
2956 $w2 = substr( $ltrimmed, -$diff );
2957 } else {
2958 $w2 = '';
2959 }
2960 return [ $w1, $trimmed, $w2 ];
2961 }
2962
2963 /**
2964 * Replace magic variables, templates, and template arguments
2965 * with the appropriate text. Templates are substituted recursively,
2966 * taking care to avoid infinite loops.
2967 *
2968 * Note that the substitution depends on value of $mOutputType:
2969 * self::OT_WIKI: only {{subst:}} templates
2970 * self::OT_PREPROCESS: templates but not extension tags
2971 * self::OT_HTML: all templates and extension tags
2972 *
2973 * @param string $text The text to transform
2974 * @param bool|PPFrame $frame Object describing the arguments passed to the
2975 * template. Arguments may also be provided as an associative array, as
2976 * was the usual case before MW1.12. Providing arguments this way may be
2977 * useful for extensions wishing to perform variable replacement
2978 * explicitly.
2979 * @param bool $argsOnly Only do argument (triple-brace) expansion, not
2980 * double-brace expansion.
2981 * @return string
2982 */
2983 public function replaceVariables( $text, $frame = false, $argsOnly = false ) {
2984 # Is there any text? Also, Prevent too big inclusions!
2985 $textSize = strlen( $text );
2986 if ( $textSize < 1 || $textSize > $this->mOptions->getMaxIncludeSize() ) {
2987 return $text;
2988 }
2989
2990 if ( $frame === false ) {
2991 $frame = $this->getPreprocessor()->newFrame();
2992 } elseif ( !( $frame instanceof PPFrame ) ) {
2993 wfDebug( __METHOD__ . " called using plain parameters instead of "
2994 . "a PPFrame instance. Creating custom frame.\n" );
2995 $frame = $this->getPreprocessor()->newCustomFrame( $frame );
2996 }
2997
2998 $dom = $this->preprocessToDom( $text );
2999 $flags = $argsOnly ? PPFrame::NO_TEMPLATES : 0;
3000 $text = $frame->expand( $dom, $flags );
3001
3002 return $text;
3003 }
3004
3005 /**
3006 * Clean up argument array - refactored in 1.9 so parserfunctions can use it, too.
3007 *
3008 * @param array $args
3009 *
3010 * @return array
3011 */
3012 public static function createAssocArgs( $args ) {
3013 $assocArgs = [];
3014 $index = 1;
3015 foreach ( $args as $arg ) {
3016 $eqpos = strpos( $arg, '=' );
3017 if ( $eqpos === false ) {
3018 $assocArgs[$index++] = $arg;
3019 } else {
3020 $name = trim( substr( $arg, 0, $eqpos ) );
3021 $value = trim( substr( $arg, $eqpos + 1 ) );
3022 if ( $value === false ) {
3023 $value = '';
3024 }
3025 if ( $name !== false ) {
3026 $assocArgs[$name] = $value;
3027 }
3028 }
3029 }
3030
3031 return $assocArgs;
3032 }
3033
3034 /**
3035 * Warn the user when a parser limitation is reached
3036 * Will warn at most once the user per limitation type
3037 *
3038 * The results are shown during preview and run through the Parser (See EditPage.php)
3039 *
3040 * @param string $limitationType Should be one of:
3041 * 'expensive-parserfunction' (corresponding messages:
3042 * 'expensive-parserfunction-warning',
3043 * 'expensive-parserfunction-category')
3044 * 'post-expand-template-argument' (corresponding messages:
3045 * 'post-expand-template-argument-warning',
3046 * 'post-expand-template-argument-category')
3047 * 'post-expand-template-inclusion' (corresponding messages:
3048 * 'post-expand-template-inclusion-warning',
3049 * 'post-expand-template-inclusion-category')
3050 * 'node-count-exceeded' (corresponding messages:
3051 * 'node-count-exceeded-warning',
3052 * 'node-count-exceeded-category')
3053 * 'expansion-depth-exceeded' (corresponding messages:
3054 * 'expansion-depth-exceeded-warning',
3055 * 'expansion-depth-exceeded-category')
3056 * @param string|int|null $current Current value
3057 * @param string|int|null $max Maximum allowed, when an explicit limit has been
3058 * exceeded, provide the values (optional)
3059 */
3060 public function limitationWarn( $limitationType, $current = '', $max = '' ) {
3061 # does no harm if $current and $max are present but are unnecessary for the message
3062 # Not doing ->inLanguage( $this->mOptions->getUserLangObj() ), since this is shown
3063 # only during preview, and that would split the parser cache unnecessarily.
3064 $warning = wfMessage( "$limitationType-warning" )->numParams( $current, $max )
3065 ->text();
3066 $this->mOutput->addWarning( $warning );
3067 $this->addTrackingCategory( "$limitationType-category" );
3068 }
3069
3070 /**
3071 * Return the text of a template, after recursively
3072 * replacing any variables or templates within the template.
3073 *
3074 * @param array $piece The parts of the template
3075 * $piece['title']: the title, i.e. the part before the |
3076 * $piece['parts']: the parameter array
3077 * $piece['lineStart']: whether the brace was at the start of a line
3078 * @param PPFrame $frame The current frame, contains template arguments
3079 * @throws Exception
3080 * @return string The text of the template
3081 */
3082 public function braceSubstitution( $piece, $frame ) {
3083 // Flags
3084
3085 // $text has been filled
3086 $found = false;
3087 // wiki markup in $text should be escaped
3088 $nowiki = false;
3089 // $text is HTML, armour it against wikitext transformation
3090 $isHTML = false;
3091 // Force interwiki transclusion to be done in raw mode not rendered
3092 $forceRawInterwiki = false;
3093 // $text is a DOM node needing expansion in a child frame
3094 $isChildObj = false;
3095 // $text is a DOM node needing expansion in the current frame
3096 $isLocalObj = false;
3097
3098 # Title object, where $text came from
3099 $title = false;
3100
3101 # $part1 is the bit before the first |, and must contain only title characters.
3102 # Various prefixes will be stripped from it later.
3103 $titleWithSpaces = $frame->expand( $piece['title'] );
3104 $part1 = trim( $titleWithSpaces );
3105 $titleText = false;
3106
3107 # Original title text preserved for various purposes
3108 $originalTitle = $part1;
3109
3110 # $args is a list of argument nodes, starting from index 0, not including $part1
3111 # @todo FIXME: If piece['parts'] is null then the call to getLength()
3112 # below won't work b/c this $args isn't an object
3113 $args = ( null == $piece['parts'] ) ? [] : $piece['parts'];
3114
3115 $profileSection = null; // profile templates
3116
3117 # SUBST
3118 if ( !$found ) {
3119 $substMatch = $this->mSubstWords->matchStartAndRemove( $part1 );
3120
3121 # Possibilities for substMatch: "subst", "safesubst" or FALSE
3122 # Decide whether to expand template or keep wikitext as-is.
3123 if ( $this->ot['wiki'] ) {
3124 if ( $substMatch === false ) {
3125 $literal = true; # literal when in PST with no prefix
3126 } else {
3127 $literal = false; # expand when in PST with subst: or safesubst:
3128 }
3129 } else {
3130 if ( $substMatch == 'subst' ) {
3131 $literal = true; # literal when not in PST with plain subst:
3132 } else {
3133 $literal = false; # expand when not in PST with safesubst: or no prefix
3134 }
3135 }
3136 if ( $literal ) {
3137 $text = $frame->virtualBracketedImplode( '{{', '|', '}}', $titleWithSpaces, $args );
3138 $isLocalObj = true;
3139 $found = true;
3140 }
3141 }
3142
3143 # Variables
3144 if ( !$found && $args->getLength() == 0 ) {
3145 $id = $this->mVariables->matchStartToEnd( $part1 );
3146 if ( $id !== false ) {
3147 $text = $this->getVariableValue( $id, $frame );
3148 if ( $this->magicWordFactory->getCacheTTL( $id ) > -1 ) {
3149 $this->mOutput->updateCacheExpiry(
3150 $this->magicWordFactory->getCacheTTL( $id ) );
3151 }
3152 $found = true;
3153 }
3154 }
3155
3156 # MSG, MSGNW and RAW
3157 if ( !$found ) {
3158 # Check for MSGNW:
3159 $mwMsgnw = $this->magicWordFactory->get( 'msgnw' );
3160 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
3161 $nowiki = true;
3162 } else {
3163 # Remove obsolete MSG:
3164 $mwMsg = $this->magicWordFactory->get( 'msg' );
3165 $mwMsg->matchStartAndRemove( $part1 );
3166 }
3167
3168 # Check for RAW:
3169 $mwRaw = $this->magicWordFactory->get( 'raw' );
3170 if ( $mwRaw->matchStartAndRemove( $part1 ) ) {
3171 $forceRawInterwiki = true;
3172 }
3173 }
3174
3175 # Parser functions
3176 if ( !$found ) {
3177 $colonPos = strpos( $part1, ':' );
3178 if ( $colonPos !== false ) {
3179 $func = substr( $part1, 0, $colonPos );
3180 $funcArgs = [ trim( substr( $part1, $colonPos + 1 ) ) ];
3181 $argsLength = $args->getLength();
3182 for ( $i = 0; $i < $argsLength; $i++ ) {
3183 $funcArgs[] = $args->item( $i );
3184 }
3185
3186 $result = $this->callParserFunction( $frame, $func, $funcArgs );
3187
3188 // Extract any forwarded flags
3189 if ( isset( $result['title'] ) ) {
3190 $title = $result['title'];
3191 }
3192 if ( isset( $result['found'] ) ) {
3193 $found = $result['found'];
3194 }
3195 if ( array_key_exists( 'text', $result ) ) {
3196 // a string or null
3197 $text = $result['text'];
3198 }
3199 if ( isset( $result['nowiki'] ) ) {
3200 $nowiki = $result['nowiki'];
3201 }
3202 if ( isset( $result['isHTML'] ) ) {
3203 $isHTML = $result['isHTML'];
3204 }
3205 if ( isset( $result['forceRawInterwiki'] ) ) {
3206 $forceRawInterwiki = $result['forceRawInterwiki'];
3207 }
3208 if ( isset( $result['isChildObj'] ) ) {
3209 $isChildObj = $result['isChildObj'];
3210 }
3211 if ( isset( $result['isLocalObj'] ) ) {
3212 $isLocalObj = $result['isLocalObj'];
3213 }
3214 }
3215 }
3216
3217 # Finish mangling title and then check for loops.
3218 # Set $title to a Title object and $titleText to the PDBK
3219 if ( !$found ) {
3220 $ns = NS_TEMPLATE;
3221 # Split the title into page and subpage
3222 $subpage = '';
3223 $relative = $this->maybeDoSubpageLink( $part1, $subpage );
3224 if ( $part1 !== $relative ) {
3225 $part1 = $relative;
3226 $ns = $this->mTitle->getNamespace();
3227 }
3228 $title = Title::newFromText( $part1, $ns );
3229 if ( $title ) {
3230 $titleText = $title->getPrefixedText();
3231 # Check for language variants if the template is not found
3232 if ( $this->getTargetLanguage()->hasVariants() && $title->getArticleID() == 0 ) {
3233 $this->getTargetLanguage()->findVariantLink( $part1, $title, true );
3234 }
3235 # Do recursion depth check
3236 $limit = $this->mOptions->getMaxTemplateDepth();
3237 if ( $frame->depth >= $limit ) {
3238 $found = true;
3239 $text = '<span class="error">'
3240 . wfMessage( 'parser-template-recursion-depth-warning' )
3241 ->numParams( $limit )->inContentLanguage()->text()
3242 . '</span>';
3243 }
3244 }
3245 }
3246
3247 # Load from database
3248 if ( !$found && $title ) {
3249 $profileSection = $this->mProfiler->scopedProfileIn( $title->getPrefixedDBkey() );
3250 if ( !$title->isExternal() ) {
3251 if ( $title->isSpecialPage()
3252 && $this->mOptions->getAllowSpecialInclusion()
3253 && $this->ot['html']
3254 ) {
3255 $specialPage = $this->specialPageFactory->getPage( $title->getDBkey() );
3256 // Pass the template arguments as URL parameters.
3257 // "uselang" will have no effect since the Language object
3258 // is forced to the one defined in ParserOptions.
3259 $pageArgs = [];
3260 $argsLength = $args->getLength();
3261 for ( $i = 0; $i < $argsLength; $i++ ) {
3262 $bits = $args->item( $i )->splitArg();
3263 if ( strval( $bits['index'] ) === '' ) {
3264 $name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
3265 $value = trim( $frame->expand( $bits['value'] ) );
3266 $pageArgs[$name] = $value;
3267 }
3268 }
3269
3270 // Create a new context to execute the special page
3271 $context = new RequestContext;
3272 $context->setTitle( $title );
3273 $context->setRequest( new FauxRequest( $pageArgs ) );
3274 if ( $specialPage && $specialPage->maxIncludeCacheTime() === 0 ) {
3275 $context->setUser( $this->getUser() );
3276 } else {
3277 // If this page is cached, then we better not be per user.
3278 $context->setUser( User::newFromName( '127.0.0.1', false ) );
3279 }
3280 $context->setLanguage( $this->mOptions->getUserLangObj() );
3281 $ret = $this->specialPageFactory->capturePath( $title, $context, $this->getLinkRenderer() );
3282 if ( $ret ) {
3283 $text = $context->getOutput()->getHTML();
3284 $this->mOutput->addOutputPageMetadata( $context->getOutput() );
3285 $found = true;
3286 $isHTML = true;
3287 if ( $specialPage && $specialPage->maxIncludeCacheTime() !== false ) {
3288 $this->mOutput->updateRuntimeAdaptiveExpiry(
3289 $specialPage->maxIncludeCacheTime()
3290 );
3291 }
3292 }
3293 } elseif ( MWNamespace::isNonincludable( $title->getNamespace() ) ) {
3294 $found = false; # access denied
3295 wfDebug( __METHOD__ . ": template inclusion denied for " .
3296 $title->getPrefixedDBkey() . "\n" );
3297 } else {
3298 list( $text, $title ) = $this->getTemplateDom( $title );
3299 if ( $text !== false ) {
3300 $found = true;
3301 $isChildObj = true;
3302 }
3303 }
3304
3305 # If the title is valid but undisplayable, make a link to it
3306 if ( !$found && ( $this->ot['html'] || $this->ot['pre'] ) ) {
3307 $text = "[[:$titleText]]";
3308 $found = true;
3309 }
3310 } elseif ( $title->isTrans() ) {
3311 # Interwiki transclusion
3312 if ( $this->ot['html'] && !$forceRawInterwiki ) {
3313 $text = $this->interwikiTransclude( $title, 'render' );
3314 $isHTML = true;
3315 } else {
3316 $text = $this->interwikiTransclude( $title, 'raw' );
3317 # Preprocess it like a template
3318 $text = $this->preprocessToDom( $text, self::PTD_FOR_INCLUSION );
3319 $isChildObj = true;
3320 }
3321 $found = true;
3322 }
3323
3324 # Do infinite loop check
3325 # This has to be done after redirect resolution to avoid infinite loops via redirects
3326 if ( !$frame->loopCheck( $title ) ) {
3327 $found = true;
3328 $text = '<span class="error">'
3329 . wfMessage( 'parser-template-loop-warning', $titleText )->inContentLanguage()->text()
3330 . '</span>';
3331 $this->addTrackingCategory( 'template-loop-category' );
3332 $this->mOutput->addWarning( wfMessage( 'template-loop-warning',
3333 wfEscapeWikiText( $titleText ) )->text() );
3334 wfDebug( __METHOD__ . ": template loop broken at '$titleText'\n" );
3335 }
3336 }
3337
3338 # If we haven't found text to substitute by now, we're done
3339 # Recover the source wikitext and return it
3340 if ( !$found ) {
3341 $text = $frame->virtualBracketedImplode( '{{', '|', '}}', $titleWithSpaces, $args );
3342 if ( $profileSection ) {
3343 $this->mProfiler->scopedProfileOut( $profileSection );
3344 }
3345 return [ 'object' => $text ];
3346 }
3347
3348 # Expand DOM-style return values in a child frame
3349 if ( $isChildObj ) {
3350 # Clean up argument array
3351 $newFrame = $frame->newChild( $args, $title );
3352
3353 if ( $nowiki ) {
3354 $text = $newFrame->expand( $text, PPFrame::RECOVER_ORIG );
3355 } elseif ( $titleText !== false && $newFrame->isEmpty() ) {
3356 # Expansion is eligible for the empty-frame cache
3357 $text = $newFrame->cachedExpand( $titleText, $text );
3358 } else {
3359 # Uncached expansion
3360 $text = $newFrame->expand( $text );
3361 }
3362 }
3363 if ( $isLocalObj && $nowiki ) {
3364 $text = $frame->expand( $text, PPFrame::RECOVER_ORIG );
3365 $isLocalObj = false;
3366 }
3367
3368 if ( $profileSection ) {
3369 $this->mProfiler->scopedProfileOut( $profileSection );
3370 }
3371
3372 # Replace raw HTML by a placeholder
3373 if ( $isHTML ) {
3374 $text = $this->insertStripItem( $text );
3375 } elseif ( $nowiki && ( $this->ot['html'] || $this->ot['pre'] ) ) {
3376 # Escape nowiki-style return values
3377 $text = wfEscapeWikiText( $text );
3378 } elseif ( is_string( $text )
3379 && !$piece['lineStart']
3380 && preg_match( '/^(?:{\\||:|;|#|\*)/', $text )
3381 ) {
3382 # T2529: if the template begins with a table or block-level
3383 # element, it should be treated as beginning a new line.
3384 # This behavior is somewhat controversial.
3385 $text = "\n" . $text;
3386 }
3387
3388 if ( is_string( $text ) && !$this->incrementIncludeSize( 'post-expand', strlen( $text ) ) ) {
3389 # Error, oversize inclusion
3390 if ( $titleText !== false ) {
3391 # Make a working, properly escaped link if possible (T25588)
3392 $text = "[[:$titleText]]";
3393 } else {
3394 # This will probably not be a working link, but at least it may
3395 # provide some hint of where the problem is
3396 preg_replace( '/^:/', '', $originalTitle );
3397 $text = "[[:$originalTitle]]";
3398 }
3399 $text .= $this->insertStripItem( '<!-- WARNING: template omitted, '
3400 . 'post-expand include size too large -->' );
3401 $this->limitationWarn( 'post-expand-template-inclusion' );
3402 }
3403
3404 if ( $isLocalObj ) {
3405 $ret = [ 'object' => $text ];
3406 } else {
3407 $ret = [ 'text' => $text ];
3408 }
3409
3410 return $ret;
3411 }
3412
3413 /**
3414 * Call a parser function and return an array with text and flags.
3415 *
3416 * The returned array will always contain a boolean 'found', indicating
3417 * whether the parser function was found or not. It may also contain the
3418 * following:
3419 * text: string|object, resulting wikitext or PP DOM object
3420 * isHTML: bool, $text is HTML, armour it against wikitext transformation
3421 * isChildObj: bool, $text is a DOM node needing expansion in a child frame
3422 * isLocalObj: bool, $text is a DOM node needing expansion in the current frame
3423 * nowiki: bool, wiki markup in $text should be escaped
3424 *
3425 * @since 1.21
3426 * @param PPFrame $frame The current frame, contains template arguments
3427 * @param string $function Function name
3428 * @param array $args Arguments to the function
3429 * @throws MWException
3430 * @return array
3431 */
3432 public function callParserFunction( $frame, $function, array $args = [] ) {
3433 # Case sensitive functions
3434 if ( isset( $this->mFunctionSynonyms[1][$function] ) ) {
3435 $function = $this->mFunctionSynonyms[1][$function];
3436 } else {
3437 # Case insensitive functions
3438 $function = $this->contLang->lc( $function );
3439 if ( isset( $this->mFunctionSynonyms[0][$function] ) ) {
3440 $function = $this->mFunctionSynonyms[0][$function];
3441 } else {
3442 return [ 'found' => false ];
3443 }
3444 }
3445
3446 list( $callback, $flags ) = $this->mFunctionHooks[$function];
3447
3448 // Avoid PHP 7.1 warning from passing $this by reference
3449 $parser = $this;
3450
3451 $allArgs = [ &$parser ];
3452 if ( $flags & self::SFH_OBJECT_ARGS ) {
3453 # Convert arguments to PPNodes and collect for appending to $allArgs
3454 $funcArgs = [];
3455 foreach ( $args as $k => $v ) {
3456 if ( $v instanceof PPNode || $k === 0 ) {
3457 $funcArgs[] = $v;
3458 } else {
3459 $funcArgs[] = $this->mPreprocessor->newPartNodeArray( [ $k => $v ] )->item( 0 );
3460 }
3461 }
3462
3463 # Add a frame parameter, and pass the arguments as an array
3464 $allArgs[] = $frame;
3465 $allArgs[] = $funcArgs;
3466 } else {
3467 # Convert arguments to plain text and append to $allArgs
3468 foreach ( $args as $k => $v ) {
3469 if ( $v instanceof PPNode ) {
3470 $allArgs[] = trim( $frame->expand( $v ) );
3471 } elseif ( is_int( $k ) && $k >= 0 ) {
3472 $allArgs[] = trim( $v );
3473 } else {
3474 $allArgs[] = trim( "$k=$v" );
3475 }
3476 }
3477 }
3478
3479 $result = $callback( ...$allArgs );
3480
3481 # The interface for function hooks allows them to return a wikitext
3482 # string or an array containing the string and any flags. This mungs
3483 # things around to match what this method should return.
3484 if ( !is_array( $result ) ) {
3485 $result = [
3486 'found' => true,
3487 'text' => $result,
3488 ];
3489 } else {
3490 if ( isset( $result[0] ) && !isset( $result['text'] ) ) {
3491 $result['text'] = $result[0];
3492 }
3493 unset( $result[0] );
3494 $result += [
3495 'found' => true,
3496 ];
3497 }
3498
3499 $noparse = true;
3500 $preprocessFlags = 0;
3501 if ( isset( $result['noparse'] ) ) {
3502 $noparse = $result['noparse'];
3503 }
3504 if ( isset( $result['preprocessFlags'] ) ) {
3505 $preprocessFlags = $result['preprocessFlags'];
3506 }
3507
3508 if ( !$noparse ) {
3509 $result['text'] = $this->preprocessToDom( $result['text'], $preprocessFlags );
3510 $result['isChildObj'] = true;
3511 }
3512
3513 return $result;
3514 }
3515
3516 /**
3517 * Get the semi-parsed DOM representation of a template with a given title,
3518 * and its redirect destination title. Cached.
3519 *
3520 * @param Title $title
3521 *
3522 * @return array
3523 */
3524 public function getTemplateDom( $title ) {
3525 $cacheTitle = $title;
3526 $titleText = $title->getPrefixedDBkey();
3527
3528 if ( isset( $this->mTplRedirCache[$titleText] ) ) {
3529 list( $ns, $dbk ) = $this->mTplRedirCache[$titleText];
3530 $title = Title::makeTitle( $ns, $dbk );
3531 $titleText = $title->getPrefixedDBkey();
3532 }
3533 if ( isset( $this->mTplDomCache[$titleText] ) ) {
3534 return [ $this->mTplDomCache[$titleText], $title ];
3535 }
3536
3537 # Cache miss, go to the database
3538 list( $text, $title ) = $this->fetchTemplateAndTitle( $title );
3539
3540 if ( $text === false ) {
3541 $this->mTplDomCache[$titleText] = false;
3542 return [ false, $title ];
3543 }
3544
3545 $dom = $this->preprocessToDom( $text, self::PTD_FOR_INCLUSION );
3546 $this->mTplDomCache[$titleText] = $dom;
3547
3548 if ( !$title->equals( $cacheTitle ) ) {
3549 $this->mTplRedirCache[$cacheTitle->getPrefixedDBkey()] =
3550 [ $title->getNamespace(), $title->getDBkey() ];
3551 }
3552
3553 return [ $dom, $title ];
3554 }
3555
3556 /**
3557 * Fetch the current revision of a given title. Note that the revision
3558 * (and even the title) may not exist in the database, so everything
3559 * contributing to the output of the parser should use this method
3560 * where possible, rather than getting the revisions themselves. This
3561 * method also caches its results, so using it benefits performance.
3562 *
3563 * @since 1.24
3564 * @param Title $title
3565 * @return Revision
3566 */
3567 public function fetchCurrentRevisionOfTitle( $title ) {
3568 $cacheKey = $title->getPrefixedDBkey();
3569 if ( !$this->currentRevisionCache ) {
3570 $this->currentRevisionCache = new MapCacheLRU( 100 );
3571 }
3572 if ( !$this->currentRevisionCache->has( $cacheKey ) ) {
3573 $this->currentRevisionCache->set( $cacheKey,
3574 // Defaults to Parser::statelessFetchRevision()
3575 call_user_func( $this->mOptions->getCurrentRevisionCallback(), $title, $this )
3576 );
3577 }
3578 return $this->currentRevisionCache->get( $cacheKey );
3579 }
3580
3581 /**
3582 * Wrapper around Revision::newFromTitle to allow passing additional parameters
3583 * without passing them on to it.
3584 *
3585 * @since 1.24
3586 * @param Title $title
3587 * @param Parser|bool $parser
3588 * @return Revision|bool False if missing
3589 */
3590 public static function statelessFetchRevision( Title $title, $parser = false ) {
3591 $rev = Revision::newKnownCurrent( wfGetDB( DB_REPLICA ), $title );
3592
3593 return $rev;
3594 }
3595
3596 /**
3597 * Fetch the unparsed text of a template and register a reference to it.
3598 * @param Title $title
3599 * @return array ( string or false, Title )
3600 */
3601 public function fetchTemplateAndTitle( $title ) {
3602 // Defaults to Parser::statelessFetchTemplate()
3603 $templateCb = $this->mOptions->getTemplateCallback();
3604 $stuff = call_user_func( $templateCb, $title, $this );
3605 // We use U+007F DELETE to distinguish strip markers from regular text.
3606 $text = $stuff['text'];
3607 if ( is_string( $stuff['text'] ) ) {
3608 $text = strtr( $text, "\x7f", "?" );
3609 }
3610 $finalTitle = $stuff['finalTitle'] ?? $title;
3611 if ( isset( $stuff['deps'] ) ) {
3612 foreach ( $stuff['deps'] as $dep ) {
3613 $this->mOutput->addTemplate( $dep['title'], $dep['page_id'], $dep['rev_id'] );
3614 if ( $dep['title']->equals( $this->getTitle() ) ) {
3615 // If we transclude ourselves, the final result
3616 // will change based on the new version of the page
3617 $this->mOutput->setFlag( 'vary-revision' );
3618 }
3619 }
3620 }
3621 return [ $text, $finalTitle ];
3622 }
3623
3624 /**
3625 * Fetch the unparsed text of a template and register a reference to it.
3626 * @param Title $title
3627 * @return string|bool
3628 */
3629 public function fetchTemplate( $title ) {
3630 return $this->fetchTemplateAndTitle( $title )[0];
3631 }
3632
3633 /**
3634 * Static function to get a template
3635 * Can be overridden via ParserOptions::setTemplateCallback().
3636 *
3637 * @param Title $title
3638 * @param bool|Parser $parser
3639 *
3640 * @return array
3641 */
3642 public static function statelessFetchTemplate( $title, $parser = false ) {
3643 $text = $skip = false;
3644 $finalTitle = $title;
3645 $deps = [];
3646
3647 # Loop to fetch the article, with up to 1 redirect
3648 // phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall
3649 for ( $i = 0; $i < 2 && is_object( $title ); $i++ ) {
3650 # Give extensions a chance to select the revision instead
3651 $id = false; # Assume current
3652 Hooks::run( 'BeforeParserFetchTemplateAndtitle',
3653 [ $parser, $title, &$skip, &$id ] );
3654
3655 if ( $skip ) {
3656 $text = false;
3657 $deps[] = [
3658 'title' => $title,
3659 'page_id' => $title->getArticleID(),
3660 'rev_id' => null
3661 ];
3662 break;
3663 }
3664 # Get the revision
3665 if ( $id ) {
3666 $rev = Revision::newFromId( $id );
3667 } elseif ( $parser ) {
3668 $rev = $parser->fetchCurrentRevisionOfTitle( $title );
3669 } else {
3670 $rev = Revision::newFromTitle( $title );
3671 }
3672 $rev_id = $rev ? $rev->getId() : 0;
3673 # If there is no current revision, there is no page
3674 if ( $id === false && !$rev ) {
3675 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
3676 $linkCache->addBadLinkObj( $title );
3677 }
3678
3679 $deps[] = [
3680 'title' => $title,
3681 'page_id' => $title->getArticleID(),
3682 'rev_id' => $rev_id ];
3683 if ( $rev && !$title->equals( $rev->getTitle() ) ) {
3684 # We fetched a rev from a different title; register it too...
3685 $deps[] = [
3686 'title' => $rev->getTitle(),
3687 'page_id' => $rev->getPage(),
3688 'rev_id' => $rev_id ];
3689 }
3690
3691 if ( $rev ) {
3692 $content = $rev->getContent();
3693 $text = $content ? $content->getWikitextForTransclusion() : null;
3694
3695 Hooks::run( 'ParserFetchTemplate',
3696 [ $parser, $title, $rev, &$text, &$deps ] );
3697
3698 if ( $text === false || $text === null ) {
3699 $text = false;
3700 break;
3701 }
3702 } elseif ( $title->getNamespace() == NS_MEDIAWIKI ) {
3703 $message = wfMessage( MediaWikiServices::getInstance()->getContentLanguage()->
3704 lcfirst( $title->getText() ) )->inContentLanguage();
3705 if ( !$message->exists() ) {
3706 $text = false;
3707 break;
3708 }
3709 $content = $message->content();
3710 $text = $message->plain();
3711 } else {
3712 break;
3713 }
3714 if ( !$content ) {
3715 break;
3716 }
3717 # Redirect?
3718 $finalTitle = $title;
3719 $title = $content->getRedirectTarget();
3720 }
3721 return [
3722 'text' => $text,
3723 'finalTitle' => $finalTitle,
3724 'deps' => $deps ];
3725 }
3726
3727 /**
3728 * Fetch a file and its title and register a reference to it.
3729 * If 'broken' is a key in $options then the file will appear as a broken thumbnail.
3730 * @param Title $title
3731 * @param array $options Array of options to RepoGroup::findFile
3732 * @return File|bool
3733 * @deprecated since 1.32, use fetchFileAndTitle instead
3734 */
3735 public function fetchFile( $title, $options = [] ) {
3736 wfDeprecated( __METHOD__, '1.32' );
3737 return $this->fetchFileAndTitle( $title, $options )[0];
3738 }
3739
3740 /**
3741 * Fetch a file and its title and register a reference to it.
3742 * If 'broken' is a key in $options then the file will appear as a broken thumbnail.
3743 * @param Title $title
3744 * @param array $options Array of options to RepoGroup::findFile
3745 * @return array ( File or false, Title of file )
3746 */
3747 public function fetchFileAndTitle( $title, $options = [] ) {
3748 $file = $this->fetchFileNoRegister( $title, $options );
3749
3750 $time = $file ? $file->getTimestamp() : false;
3751 $sha1 = $file ? $file->getSha1() : false;
3752 # Register the file as a dependency...
3753 $this->mOutput->addImage( $title->getDBkey(), $time, $sha1 );
3754 if ( $file && !$title->equals( $file->getTitle() ) ) {
3755 # Update fetched file title
3756 $title = $file->getTitle();
3757 $this->mOutput->addImage( $title->getDBkey(), $time, $sha1 );
3758 }
3759 return [ $file, $title ];
3760 }
3761
3762 /**
3763 * Helper function for fetchFileAndTitle.
3764 *
3765 * Also useful if you need to fetch a file but not use it yet,
3766 * for example to get the file's handler.
3767 *
3768 * @param Title $title
3769 * @param array $options Array of options to RepoGroup::findFile
3770 * @return File|bool
3771 */
3772 protected function fetchFileNoRegister( $title, $options = [] ) {
3773 if ( isset( $options['broken'] ) ) {
3774 $file = false; // broken thumbnail forced by hook
3775 } elseif ( isset( $options['sha1'] ) ) { // get by (sha1,timestamp)
3776 $file = RepoGroup::singleton()->findFileFromKey( $options['sha1'], $options );
3777 } else { // get by (name,timestamp)
3778 $file = wfFindFile( $title, $options );
3779 }
3780 return $file;
3781 }
3782
3783 /**
3784 * Transclude an interwiki link.
3785 *
3786 * @param Title $title
3787 * @param string $action Usually one of (raw, render)
3788 *
3789 * @return string
3790 */
3791 public function interwikiTransclude( $title, $action ) {
3792 global $wgEnableScaryTranscluding, $wgTranscludeCacheExpiry;
3793
3794 if ( !$wgEnableScaryTranscluding ) {
3795 return wfMessage( 'scarytranscludedisabled' )->inContentLanguage()->text();
3796 }
3797
3798 $url = $title->getFullURL( [ 'action' => $action ] );
3799 if ( strlen( $url ) > 1024 ) {
3800 return wfMessage( 'scarytranscludetoolong' )->inContentLanguage()->text();
3801 }
3802
3803 $wikiId = $title->getTransWikiID(); // remote wiki ID or false
3804
3805 $fname = __METHOD__;
3806 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
3807
3808 $data = $cache->getWithSetCallback(
3809 $cache->makeGlobalKey(
3810 'interwiki-transclude',
3811 ( $wikiId !== false ) ? $wikiId : 'external',
3812 sha1( $url )
3813 ),
3814 $wgTranscludeCacheExpiry,
3815 function ( $oldValue, &$ttl ) use ( $url, $fname, $cache ) {
3816 $req = MWHttpRequest::factory( $url, [], $fname );
3817
3818 $status = $req->execute(); // Status object
3819 if ( !$status->isOK() ) {
3820 $ttl = $cache::TTL_UNCACHEABLE;
3821 } elseif ( $req->getResponseHeader( 'X-Database-Lagged' ) !== null ) {
3822 $ttl = min( $cache::TTL_LAGGED, $ttl );
3823 }
3824
3825 return [
3826 'text' => $status->isOK() ? $req->getContent() : null,
3827 'code' => $req->getStatus()
3828 ];
3829 },
3830 [
3831 'checkKeys' => ( $wikiId !== false )
3832 ? [ $cache->makeGlobalKey( 'interwiki-page', $wikiId, $title->getDBkey() ) ]
3833 : [],
3834 'pcGroup' => 'interwiki-transclude:5',
3835 'pcTTL' => $cache::TTL_PROC_LONG
3836 ]
3837 );
3838
3839 if ( is_string( $data['text'] ) ) {
3840 $text = $data['text'];
3841 } elseif ( $data['code'] != 200 ) {
3842 // Though we failed to fetch the content, this status is useless.
3843 $text = wfMessage( 'scarytranscludefailed-httpstatus' )
3844 ->params( $url, $data['code'] )->inContentLanguage()->text();
3845 } else {
3846 $text = wfMessage( 'scarytranscludefailed', $url )->inContentLanguage()->text();
3847 }
3848
3849 return $text;
3850 }
3851
3852 /**
3853 * Triple brace replacement -- used for template arguments
3854 * @private
3855 *
3856 * @param array $piece
3857 * @param PPFrame $frame
3858 *
3859 * @return array
3860 */
3861 public function argSubstitution( $piece, $frame ) {
3862 $error = false;
3863 $parts = $piece['parts'];
3864 $nameWithSpaces = $frame->expand( $piece['title'] );
3865 $argName = trim( $nameWithSpaces );
3866 $object = false;
3867 $text = $frame->getArgument( $argName );
3868 if ( $text === false && $parts->getLength() > 0
3869 && ( $this->ot['html']
3870 || $this->ot['pre']
3871 || ( $this->ot['wiki'] && $frame->isTemplate() )
3872 )
3873 ) {
3874 # No match in frame, use the supplied default
3875 $object = $parts->item( 0 )->getChildren();
3876 }
3877 if ( !$this->incrementIncludeSize( 'arg', strlen( $text ) ) ) {
3878 $error = '<!-- WARNING: argument omitted, expansion size too large -->';
3879 $this->limitationWarn( 'post-expand-template-argument' );
3880 }
3881
3882 if ( $text === false && $object === false ) {
3883 # No match anywhere
3884 $object = $frame->virtualBracketedImplode( '{{{', '|', '}}}', $nameWithSpaces, $parts );
3885 }
3886 if ( $error !== false ) {
3887 $text .= $error;
3888 }
3889 if ( $object !== false ) {
3890 $ret = [ 'object' => $object ];
3891 } else {
3892 $ret = [ 'text' => $text ];
3893 }
3894
3895 return $ret;
3896 }
3897
3898 /**
3899 * Return the text to be used for a given extension tag.
3900 * This is the ghost of strip().
3901 *
3902 * @param array $params Associative array of parameters:
3903 * name PPNode for the tag name
3904 * attr PPNode for unparsed text where tag attributes are thought to be
3905 * attributes Optional associative array of parsed attributes
3906 * inner Contents of extension element
3907 * noClose Original text did not have a close tag
3908 * @param PPFrame $frame
3909 *
3910 * @throws MWException
3911 * @return string
3912 */
3913 public function extensionSubstitution( $params, $frame ) {
3914 static $errorStr = '<span class="error">';
3915 static $errorLen = 20;
3916
3917 $name = $frame->expand( $params['name'] );
3918 if ( substr( $name, 0, $errorLen ) === $errorStr ) {
3919 // Probably expansion depth or node count exceeded. Just punt the
3920 // error up.
3921 return $name;
3922 }
3923
3924 $attrText = !isset( $params['attr'] ) ? null : $frame->expand( $params['attr'] );
3925 if ( substr( $attrText, 0, $errorLen ) === $errorStr ) {
3926 // See above
3927 return $attrText;
3928 }
3929
3930 // We can't safely check if the expansion for $content resulted in an
3931 // error, because the content could happen to be the error string
3932 // (T149622).
3933 $content = !isset( $params['inner'] ) ? null : $frame->expand( $params['inner'] );
3934
3935 $marker = self::MARKER_PREFIX . "-$name-"
3936 . sprintf( '%08X', $this->mMarkerIndex++ ) . self::MARKER_SUFFIX;
3937
3938 $isFunctionTag = isset( $this->mFunctionTagHooks[strtolower( $name )] ) &&
3939 ( $this->ot['html'] || $this->ot['pre'] );
3940 if ( $isFunctionTag ) {
3941 $markerType = 'none';
3942 } else {
3943 $markerType = 'general';
3944 }
3945 if ( $this->ot['html'] || $isFunctionTag ) {
3946 $name = strtolower( $name );
3947 $attributes = Sanitizer::decodeTagAttributes( $attrText );
3948 if ( isset( $params['attributes'] ) ) {
3949 $attributes = $attributes + $params['attributes'];
3950 }
3951
3952 if ( isset( $this->mTagHooks[$name] ) ) {
3953 $output = call_user_func_array( $this->mTagHooks[$name],
3954 [ $content, $attributes, $this, $frame ] );
3955 } elseif ( isset( $this->mFunctionTagHooks[$name] ) ) {
3956 list( $callback, ) = $this->mFunctionTagHooks[$name];
3957
3958 // Avoid PHP 7.1 warning from passing $this by reference
3959 $parser = $this;
3960 $output = call_user_func_array( $callback, [ &$parser, $frame, $content, $attributes ] );
3961 } else {
3962 $output = '<span class="error">Invalid tag extension name: ' .
3963 htmlspecialchars( $name ) . '</span>';
3964 }
3965
3966 if ( is_array( $output ) ) {
3967 // Extract flags
3968 $flags = $output;
3969 $output = $flags[0];
3970 if ( isset( $flags['markerType'] ) ) {
3971 $markerType = $flags['markerType'];
3972 }
3973 }
3974 } else {
3975 if ( is_null( $attrText ) ) {
3976 $attrText = '';
3977 }
3978 if ( isset( $params['attributes'] ) ) {
3979 foreach ( $params['attributes'] as $attrName => $attrValue ) {
3980 $attrText .= ' ' . htmlspecialchars( $attrName ) . '="' .
3981 htmlspecialchars( $attrValue ) . '"';
3982 }
3983 }
3984 if ( $content === null ) {
3985 $output = "<$name$attrText/>";
3986 } else {
3987 $close = is_null( $params['close'] ) ? '' : $frame->expand( $params['close'] );
3988 if ( substr( $close, 0, $errorLen ) === $errorStr ) {
3989 // See above
3990 return $close;
3991 }
3992 $output = "<$name$attrText>$content$close";
3993 }
3994 }
3995
3996 if ( $markerType === 'none' ) {
3997 return $output;
3998 } elseif ( $markerType === 'nowiki' ) {
3999 $this->mStripState->addNoWiki( $marker, $output );
4000 } elseif ( $markerType === 'general' ) {
4001 $this->mStripState->addGeneral( $marker, $output );
4002 } else {
4003 throw new MWException( __METHOD__ . ': invalid marker type' );
4004 }
4005 return $marker;
4006 }
4007
4008 /**
4009 * Increment an include size counter
4010 *
4011 * @param string $type The type of expansion
4012 * @param int $size The size of the text
4013 * @return bool False if this inclusion would take it over the maximum, true otherwise
4014 */
4015 public function incrementIncludeSize( $type, $size ) {
4016 if ( $this->mIncludeSizes[$type] + $size > $this->mOptions->getMaxIncludeSize() ) {
4017 return false;
4018 } else {
4019 $this->mIncludeSizes[$type] += $size;
4020 return true;
4021 }
4022 }
4023
4024 /**
4025 * Increment the expensive function count
4026 *
4027 * @return bool False if the limit has been exceeded
4028 */
4029 public function incrementExpensiveFunctionCount() {
4030 $this->mExpensiveFunctionCount++;
4031 return $this->mExpensiveFunctionCount <= $this->mOptions->getExpensiveParserFunctionLimit();
4032 }
4033
4034 /**
4035 * Strip double-underscore items like __NOGALLERY__ and __NOTOC__
4036 * Fills $this->mDoubleUnderscores, returns the modified text
4037 *
4038 * @param string $text
4039 *
4040 * @return string
4041 */
4042 public function doDoubleUnderscore( $text ) {
4043 # The position of __TOC__ needs to be recorded
4044 $mw = $this->magicWordFactory->get( 'toc' );
4045 if ( $mw->match( $text ) ) {
4046 $this->mShowToc = true;
4047 $this->mForceTocPosition = true;
4048
4049 # Set a placeholder. At the end we'll fill it in with the TOC.
4050 $text = $mw->replace( '<!--MWTOC\'"-->', $text, 1 );
4051
4052 # Only keep the first one.
4053 $text = $mw->replace( '', $text );
4054 }
4055
4056 # Now match and remove the rest of them
4057 $mwa = $this->magicWordFactory->getDoubleUnderscoreArray();
4058 $this->mDoubleUnderscores = $mwa->matchAndRemove( $text );
4059
4060 if ( isset( $this->mDoubleUnderscores['nogallery'] ) ) {
4061 $this->mOutput->mNoGallery = true;
4062 }
4063 if ( isset( $this->mDoubleUnderscores['notoc'] ) && !$this->mForceTocPosition ) {
4064 $this->mShowToc = false;
4065 }
4066 if ( isset( $this->mDoubleUnderscores['hiddencat'] )
4067 && $this->mTitle->getNamespace() == NS_CATEGORY
4068 ) {
4069 $this->addTrackingCategory( 'hidden-category-category' );
4070 }
4071 # (T10068) Allow control over whether robots index a page.
4072 # __INDEX__ always overrides __NOINDEX__, see T16899
4073 if ( isset( $this->mDoubleUnderscores['noindex'] ) && $this->mTitle->canUseNoindex() ) {
4074 $this->mOutput->setIndexPolicy( 'noindex' );
4075 $this->addTrackingCategory( 'noindex-category' );
4076 }
4077 if ( isset( $this->mDoubleUnderscores['index'] ) && $this->mTitle->canUseNoindex() ) {
4078 $this->mOutput->setIndexPolicy( 'index' );
4079 $this->addTrackingCategory( 'index-category' );
4080 }
4081
4082 # Cache all double underscores in the database
4083 foreach ( $this->mDoubleUnderscores as $key => $val ) {
4084 $this->mOutput->setProperty( $key, '' );
4085 }
4086
4087 return $text;
4088 }
4089
4090 /**
4091 * @see ParserOutput::addTrackingCategory()
4092 * @param string $msg Message key
4093 * @return bool Whether the addition was successful
4094 */
4095 public function addTrackingCategory( $msg ) {
4096 return $this->mOutput->addTrackingCategory( $msg, $this->mTitle );
4097 }
4098
4099 /**
4100 * This function accomplishes several tasks:
4101 * 1) Auto-number headings if that option is enabled
4102 * 2) Add an [edit] link to sections for users who have enabled the option and can edit the page
4103 * 3) Add a Table of contents on the top for users who have enabled the option
4104 * 4) Auto-anchor headings
4105 *
4106 * It loops through all headlines, collects the necessary data, then splits up the
4107 * string and re-inserts the newly formatted headlines.
4108 *
4109 * @param string $text
4110 * @param string $origText Original, untouched wikitext
4111 * @param bool $isMain
4112 * @return mixed|string
4113 * @private
4114 */
4115 public function formatHeadings( $text, $origText, $isMain = true ) {
4116 global $wgMaxTocLevel;
4117
4118 # Inhibit editsection links if requested in the page
4119 if ( isset( $this->mDoubleUnderscores['noeditsection'] ) ) {
4120 $maybeShowEditLink = false;
4121 } else {
4122 $maybeShowEditLink = true; /* Actual presence will depend on post-cache transforms */
4123 }
4124
4125 # Get all headlines for numbering them and adding funky stuff like [edit]
4126 # links - this is for later, but we need the number of headlines right now
4127 # NOTE: white space in headings have been trimmed in doHeadings. They shouldn't
4128 # be trimmed here since whitespace in HTML headings is significant.
4129 $matches = [];
4130 $numMatches = preg_match_all(
4131 '/<H(?P<level>[1-6])(?P<attrib>.*?>)(?P<header>[\s\S]*?)<\/H[1-6] *>/i',
4132 $text,
4133 $matches
4134 );
4135
4136 # if there are fewer than 4 headlines in the article, do not show TOC
4137 # unless it's been explicitly enabled.
4138 $enoughToc = $this->mShowToc &&
4139 ( ( $numMatches >= 4 ) || $this->mForceTocPosition );
4140
4141 # Allow user to stipulate that a page should have a "new section"
4142 # link added via __NEWSECTIONLINK__
4143 if ( isset( $this->mDoubleUnderscores['newsectionlink'] ) ) {
4144 $this->mOutput->setNewSection( true );
4145 }
4146
4147 # Allow user to remove the "new section"
4148 # link via __NONEWSECTIONLINK__
4149 if ( isset( $this->mDoubleUnderscores['nonewsectionlink'] ) ) {
4150 $this->mOutput->hideNewSection( true );
4151 }
4152
4153 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
4154 # override above conditions and always show TOC above first header
4155 if ( isset( $this->mDoubleUnderscores['forcetoc'] ) ) {
4156 $this->mShowToc = true;
4157 $enoughToc = true;
4158 }
4159
4160 # headline counter
4161 $headlineCount = 0;
4162 $numVisible = 0;
4163
4164 # Ugh .. the TOC should have neat indentation levels which can be
4165 # passed to the skin functions. These are determined here
4166 $toc = '';
4167 $full = '';
4168 $head = [];
4169 $sublevelCount = [];
4170 $levelCount = [];
4171 $level = 0;
4172 $prevlevel = 0;
4173 $toclevel = 0;
4174 $prevtoclevel = 0;
4175 $markerRegex = self::MARKER_PREFIX . "-h-(\d+)-" . self::MARKER_SUFFIX;
4176 $baseTitleText = $this->mTitle->getPrefixedDBkey();
4177 $oldType = $this->mOutputType;
4178 $this->setOutputType( self::OT_WIKI );
4179 $frame = $this->getPreprocessor()->newFrame();
4180 $root = $this->preprocessToDom( $origText );
4181 $node = $root->getFirstChild();
4182 $byteOffset = 0;
4183 $tocraw = [];
4184 $refers = [];
4185
4186 $headlines = $numMatches !== false ? $matches[3] : [];
4187
4188 foreach ( $headlines as $headline ) {
4189 $isTemplate = false;
4190 $titleText = false;
4191 $sectionIndex = false;
4192 $numbering = '';
4193 $markerMatches = [];
4194 if ( preg_match( "/^$markerRegex/", $headline, $markerMatches ) ) {
4195 $serial = $markerMatches[1];
4196 list( $titleText, $sectionIndex ) = $this->mHeadings[$serial];
4197 $isTemplate = ( $titleText != $baseTitleText );
4198 $headline = preg_replace( "/^$markerRegex\\s*/", "", $headline );
4199 }
4200
4201 if ( $toclevel ) {
4202 $prevlevel = $level;
4203 }
4204 $level = $matches[1][$headlineCount];
4205
4206 if ( $level > $prevlevel ) {
4207 # Increase TOC level
4208 $toclevel++;
4209 $sublevelCount[$toclevel] = 0;
4210 if ( $toclevel < $wgMaxTocLevel ) {
4211 $prevtoclevel = $toclevel;
4212 $toc .= Linker::tocIndent();
4213 $numVisible++;
4214 }
4215 } elseif ( $level < $prevlevel && $toclevel > 1 ) {
4216 # Decrease TOC level, find level to jump to
4217
4218 for ( $i = $toclevel; $i > 0; $i-- ) {
4219 if ( $levelCount[$i] == $level ) {
4220 # Found last matching level
4221 $toclevel = $i;
4222 break;
4223 } elseif ( $levelCount[$i] < $level ) {
4224 # Found first matching level below current level
4225 $toclevel = $i + 1;
4226 break;
4227 }
4228 }
4229 if ( $i == 0 ) {
4230 $toclevel = 1;
4231 }
4232 if ( $toclevel < $wgMaxTocLevel ) {
4233 if ( $prevtoclevel < $wgMaxTocLevel ) {
4234 # Unindent only if the previous toc level was shown :p
4235 $toc .= Linker::tocUnindent( $prevtoclevel - $toclevel );
4236 $prevtoclevel = $toclevel;
4237 } else {
4238 $toc .= Linker::tocLineEnd();
4239 }
4240 }
4241 } else {
4242 # No change in level, end TOC line
4243 if ( $toclevel < $wgMaxTocLevel ) {
4244 $toc .= Linker::tocLineEnd();
4245 }
4246 }
4247
4248 $levelCount[$toclevel] = $level;
4249
4250 # count number of headlines for each level
4251 $sublevelCount[$toclevel]++;
4252 $dot = 0;
4253 for ( $i = 1; $i <= $toclevel; $i++ ) {
4254 if ( !empty( $sublevelCount[$i] ) ) {
4255 if ( $dot ) {
4256 $numbering .= '.';
4257 }
4258 $numbering .= $this->getTargetLanguage()->formatNum( $sublevelCount[$i] );
4259 $dot = 1;
4260 }
4261 }
4262
4263 # The safe header is a version of the header text safe to use for links
4264
4265 # Remove link placeholders by the link text.
4266 # <!--LINK number-->
4267 # turns into
4268 # link text with suffix
4269 # Do this before unstrip since link text can contain strip markers
4270 $safeHeadline = $this->replaceLinkHoldersText( $headline );
4271
4272 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
4273 $safeHeadline = $this->mStripState->unstripBoth( $safeHeadline );
4274
4275 # Remove any <style> or <script> tags (T198618)
4276 $safeHeadline = preg_replace(
4277 '#<(style|script)(?: [^>]*[^>/])?>.*?</\1>#is',
4278 '',
4279 $safeHeadline
4280 );
4281
4282 # Strip out HTML (first regex removes any tag not allowed)
4283 # Allowed tags are:
4284 # * <sup> and <sub> (T10393)
4285 # * <i> (T28375)
4286 # * <b> (r105284)
4287 # * <bdi> (T74884)
4288 # * <span dir="rtl"> and <span dir="ltr"> (T37167)
4289 # * <s> and <strike> (T35715)
4290 # We strip any parameter from accepted tags (second regex), except dir="rtl|ltr" from <span>,
4291 # to allow setting directionality in toc items.
4292 $tocline = preg_replace(
4293 [
4294 '#<(?!/?(span|sup|sub|bdi|i|b|s|strike)(?: [^>]*)?>).*?>#',
4295 '#<(/?(?:span(?: dir="(?:rtl|ltr)")?|sup|sub|bdi|i|b|s|strike))(?: .*?)?>#'
4296 ],
4297 [ '', '<$1>' ],
4298 $safeHeadline
4299 );
4300
4301 # Strip '<span></span>', which is the result from the above if
4302 # <span id="foo"></span> is used to produce an additional anchor
4303 # for a section.
4304 $tocline = str_replace( '<span></span>', '', $tocline );
4305
4306 $tocline = trim( $tocline );
4307
4308 # For the anchor, strip out HTML-y stuff period
4309 $safeHeadline = preg_replace( '/<.*?>/', '', $safeHeadline );
4310 $safeHeadline = Sanitizer::normalizeSectionNameWhitespace( $safeHeadline );
4311
4312 # Save headline for section edit hint before it's escaped
4313 $headlineHint = $safeHeadline;
4314
4315 # Decode HTML entities
4316 $safeHeadline = Sanitizer::decodeCharReferences( $safeHeadline );
4317
4318 $safeHeadline = self::normalizeSectionName( $safeHeadline );
4319
4320 $fallbackHeadline = Sanitizer::escapeIdForAttribute( $safeHeadline, Sanitizer::ID_FALLBACK );
4321 $linkAnchor = Sanitizer::escapeIdForLink( $safeHeadline );
4322 $safeHeadline = Sanitizer::escapeIdForAttribute( $safeHeadline, Sanitizer::ID_PRIMARY );
4323 if ( $fallbackHeadline === $safeHeadline ) {
4324 # No reason to have both (in fact, we can't)
4325 $fallbackHeadline = false;
4326 }
4327
4328 # HTML IDs must be case-insensitively unique for IE compatibility (T12721).
4329 # @todo FIXME: We may be changing them depending on the current locale.
4330 $arrayKey = strtolower( $safeHeadline );
4331 if ( $fallbackHeadline === false ) {
4332 $fallbackArrayKey = false;
4333 } else {
4334 $fallbackArrayKey = strtolower( $fallbackHeadline );
4335 }
4336
4337 # Create the anchor for linking from the TOC to the section
4338 $anchor = $safeHeadline;
4339 $fallbackAnchor = $fallbackHeadline;
4340 if ( isset( $refers[$arrayKey] ) ) {
4341 // phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall,Generic.Formatting.DisallowMultipleStatements
4342 for ( $i = 2; isset( $refers["${arrayKey}_$i"] ); ++$i );
4343 $anchor .= "_$i";
4344 $linkAnchor .= "_$i";
4345 $refers["${arrayKey}_$i"] = true;
4346 } else {
4347 $refers[$arrayKey] = true;
4348 }
4349 if ( $fallbackHeadline !== false && isset( $refers[$fallbackArrayKey] ) ) {
4350 // phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall,Generic.Formatting.DisallowMultipleStatements
4351 for ( $i = 2; isset( $refers["${fallbackArrayKey}_$i"] ); ++$i );
4352 $fallbackAnchor .= "_$i";
4353 $refers["${fallbackArrayKey}_$i"] = true;
4354 } else {
4355 $refers[$fallbackArrayKey] = true;
4356 }
4357
4358 # Don't number the heading if it is the only one (looks silly)
4359 if ( count( $matches[3] ) > 1 && $this->mOptions->getNumberHeadings() ) {
4360 # the two are different if the line contains a link
4361 $headline = Html::element(
4362 'span',
4363 [ 'class' => 'mw-headline-number' ],
4364 $numbering
4365 ) . ' ' . $headline;
4366 }
4367
4368 if ( $enoughToc && ( !isset( $wgMaxTocLevel ) || $toclevel < $wgMaxTocLevel ) ) {
4369 $toc .= Linker::tocLine( $linkAnchor, $tocline,
4370 $numbering, $toclevel, ( $isTemplate ? false : $sectionIndex ) );
4371 }
4372
4373 # Add the section to the section tree
4374 # Find the DOM node for this header
4375 $noOffset = ( $isTemplate || $sectionIndex === false );
4376 while ( $node && !$noOffset ) {
4377 if ( $node->getName() === 'h' ) {
4378 $bits = $node->splitHeading();
4379 if ( $bits['i'] == $sectionIndex ) {
4380 break;
4381 }
4382 }
4383 $byteOffset += mb_strlen( $this->mStripState->unstripBoth(
4384 $frame->expand( $node, PPFrame::RECOVER_ORIG ) ) );
4385 $node = $node->getNextSibling();
4386 }
4387 $tocraw[] = [
4388 'toclevel' => $toclevel,
4389 'level' => $level,
4390 'line' => $tocline,
4391 'number' => $numbering,
4392 'index' => ( $isTemplate ? 'T-' : '' ) . $sectionIndex,
4393 'fromtitle' => $titleText,
4394 'byteoffset' => ( $noOffset ? null : $byteOffset ),
4395 'anchor' => $anchor,
4396 ];
4397
4398 # give headline the correct <h#> tag
4399 if ( $maybeShowEditLink && $sectionIndex !== false ) {
4400 // Output edit section links as markers with styles that can be customized by skins
4401 if ( $isTemplate ) {
4402 # Put a T flag in the section identifier, to indicate to extractSections()
4403 # that sections inside <includeonly> should be counted.
4404 $editsectionPage = $titleText;
4405 $editsectionSection = "T-$sectionIndex";
4406 $editsectionContent = null;
4407 } else {
4408 $editsectionPage = $this->mTitle->getPrefixedText();
4409 $editsectionSection = $sectionIndex;
4410 $editsectionContent = $headlineHint;
4411 }
4412 // We use a bit of pesudo-xml for editsection markers. The
4413 // language converter is run later on. Using a UNIQ style marker
4414 // leads to the converter screwing up the tokens when it
4415 // converts stuff. And trying to insert strip tags fails too. At
4416 // this point all real inputted tags have already been escaped,
4417 // so we don't have to worry about a user trying to input one of
4418 // these markers directly. We use a page and section attribute
4419 // to stop the language converter from converting these
4420 // important bits of data, but put the headline hint inside a
4421 // content block because the language converter is supposed to
4422 // be able to convert that piece of data.
4423 // Gets replaced with html in ParserOutput::getText
4424 $editlink = '<mw:editsection page="' . htmlspecialchars( $editsectionPage );
4425 $editlink .= '" section="' . htmlspecialchars( $editsectionSection ) . '"';
4426 if ( $editsectionContent !== null ) {
4427 $editlink .= '>' . $editsectionContent . '</mw:editsection>';
4428 } else {
4429 $editlink .= '/>';
4430 }
4431 } else {
4432 $editlink = '';
4433 }
4434 $head[$headlineCount] = Linker::makeHeadline( $level,
4435 $matches['attrib'][$headlineCount], $anchor, $headline,
4436 $editlink, $fallbackAnchor );
4437
4438 $headlineCount++;
4439 }
4440
4441 $this->setOutputType( $oldType );
4442
4443 # Never ever show TOC if no headers
4444 if ( $numVisible < 1 ) {
4445 $enoughToc = false;
4446 }
4447
4448 if ( $enoughToc ) {
4449 if ( $prevtoclevel > 0 && $prevtoclevel < $wgMaxTocLevel ) {
4450 $toc .= Linker::tocUnindent( $prevtoclevel - 1 );
4451 }
4452 $toc = Linker::tocList( $toc, $this->mOptions->getUserLangObj() );
4453 $this->mOutput->setTOCHTML( $toc );
4454 $toc = self::TOC_START . $toc . self::TOC_END;
4455 }
4456
4457 if ( $isMain ) {
4458 $this->mOutput->setSections( $tocraw );
4459 }
4460
4461 # split up and insert constructed headlines
4462 $blocks = preg_split( '/<H[1-6].*?>[\s\S]*?<\/H[1-6]>/i', $text );
4463 $i = 0;
4464
4465 // build an array of document sections
4466 $sections = [];
4467 foreach ( $blocks as $block ) {
4468 // $head is zero-based, sections aren't.
4469 if ( empty( $head[$i - 1] ) ) {
4470 $sections[$i] = $block;
4471 } else {
4472 $sections[$i] = $head[$i - 1] . $block;
4473 }
4474
4475 /**
4476 * Send a hook, one per section.
4477 * The idea here is to be able to make section-level DIVs, but to do so in a
4478 * lower-impact, more correct way than r50769
4479 *
4480 * $this : caller
4481 * $section : the section number
4482 * &$sectionContent : ref to the content of the section
4483 * $maybeShowEditLinks : boolean describing whether this section has an edit link
4484 */
4485 Hooks::run( 'ParserSectionCreate', [ $this, $i, &$sections[$i], $maybeShowEditLink ] );
4486
4487 $i++;
4488 }
4489
4490 if ( $enoughToc && $isMain && !$this->mForceTocPosition ) {
4491 // append the TOC at the beginning
4492 // Top anchor now in skin
4493 $sections[0] = $sections[0] . $toc . "\n";
4494 }
4495
4496 $full .= implode( '', $sections );
4497
4498 if ( $this->mForceTocPosition ) {
4499 return str_replace( '<!--MWTOC\'"-->', $toc, $full );
4500 } else {
4501 return $full;
4502 }
4503 }
4504
4505 /**
4506 * Transform wiki markup when saving a page by doing "\r\n" -> "\n"
4507 * conversion, substituting signatures, {{subst:}} templates, etc.
4508 *
4509 * @param string $text The text to transform
4510 * @param Title $title The Title object for the current article
4511 * @param User $user The User object describing the current user
4512 * @param ParserOptions $options Parsing options
4513 * @param bool $clearState Whether to clear the parser state first
4514 * @return string The altered wiki markup
4515 */
4516 public function preSaveTransform( $text, Title $title, User $user,
4517 ParserOptions $options, $clearState = true
4518 ) {
4519 if ( $clearState ) {
4520 $magicScopeVariable = $this->lock();
4521 }
4522 $this->startParse( $title, $options, self::OT_WIKI, $clearState );
4523 $this->setUser( $user );
4524
4525 // Strip U+0000 NULL (T159174)
4526 $text = str_replace( "\000", '', $text );
4527
4528 // We still normalize line endings for backwards-compatibility
4529 // with other code that just calls PST, but this should already
4530 // be handled in TextContent subclasses
4531 $text = TextContent::normalizeLineEndings( $text );
4532
4533 if ( $options->getPreSaveTransform() ) {
4534 $text = $this->pstPass2( $text, $user );
4535 }
4536 $text = $this->mStripState->unstripBoth( $text );
4537
4538 $this->setUser( null ); # Reset
4539
4540 return $text;
4541 }
4542
4543 /**
4544 * Pre-save transform helper function
4545 *
4546 * @param string $text
4547 * @param User $user
4548 *
4549 * @return string
4550 */
4551 private function pstPass2( $text, $user ) {
4552 # Note: This is the timestamp saved as hardcoded wikitext to the database, we use
4553 # $this->contLang here in order to give everyone the same signature and use the default one
4554 # rather than the one selected in each user's preferences. (see also T14815)
4555 $ts = $this->mOptions->getTimestamp();
4556 $timestamp = MWTimestamp::getLocalInstance( $ts );
4557 $ts = $timestamp->format( 'YmdHis' );
4558 $tzMsg = $timestamp->getTimezoneMessage()->inContentLanguage()->text();
4559
4560 $d = $this->contLang->timeanddate( $ts, false, false ) . " ($tzMsg)";
4561
4562 # Variable replacement
4563 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
4564 $text = $this->replaceVariables( $text );
4565
4566 # This works almost by chance, as the replaceVariables are done before the getUserSig(),
4567 # which may corrupt this parser instance via its wfMessage()->text() call-
4568
4569 # Signatures
4570 if ( strpos( $text, '~~~' ) !== false ) {
4571 $sigText = $this->getUserSig( $user );
4572 $text = strtr( $text, [
4573 '~~~~~' => $d,
4574 '~~~~' => "$sigText $d",
4575 '~~~' => $sigText
4576 ] );
4577 # The main two signature forms used above are time-sensitive
4578 $this->mOutput->setFlag( 'user-signature' );
4579 }
4580
4581 # Context links ("pipe tricks"): [[|name]] and [[name (context)|]]
4582 $tc = '[' . Title::legalChars() . ']';
4583 $nc = '[ _0-9A-Za-z\x80-\xff-]'; # Namespaces can use non-ascii!
4584
4585 // [[ns:page (context)|]]
4586 $p1 = "/\[\[(:?$nc+:|:|)($tc+?)( ?\\($tc+\\))\\|]]/";
4587 // [[ns:page(context)|]] (double-width brackets, added in r40257)
4588 $p4 = "/\[\[(:?$nc+:|:|)($tc+?)( ?($tc+))\\|]]/";
4589 // [[ns:page (context), context|]] (using either single or double-width comma)
4590 $p3 = "/\[\[(:?$nc+:|:|)($tc+?)( ?\\($tc+\\)|)((?:, |,)$tc+|)\\|]]/";
4591 // [[|page]] (reverse pipe trick: add context from page title)
4592 $p2 = "/\[\[\\|($tc+)]]/";
4593
4594 # try $p1 first, to turn "[[A, B (C)|]]" into "[[A, B (C)|A, B]]"
4595 $text = preg_replace( $p1, '[[\\1\\2\\3|\\2]]', $text );
4596 $text = preg_replace( $p4, '[[\\1\\2\\3|\\2]]', $text );
4597 $text = preg_replace( $p3, '[[\\1\\2\\3\\4|\\2]]', $text );
4598
4599 $t = $this->mTitle->getText();
4600 $m = [];
4601 if ( preg_match( "/^($nc+:|)$tc+?( \\($tc+\\))$/", $t, $m ) ) {
4602 $text = preg_replace( $p2, "[[$m[1]\\1$m[2]|\\1]]", $text );
4603 } elseif ( preg_match( "/^($nc+:|)$tc+?(, $tc+|)$/", $t, $m ) && "$m[1]$m[2]" != '' ) {
4604 $text = preg_replace( $p2, "[[$m[1]\\1$m[2]|\\1]]", $text );
4605 } else {
4606 # if there's no context, don't bother duplicating the title
4607 $text = preg_replace( $p2, '[[\\1]]', $text );
4608 }
4609
4610 return $text;
4611 }
4612
4613 /**
4614 * Fetch the user's signature text, if any, and normalize to
4615 * validated, ready-to-insert wikitext.
4616 * If you have pre-fetched the nickname or the fancySig option, you can
4617 * specify them here to save a database query.
4618 * Do not reuse this parser instance after calling getUserSig(),
4619 * as it may have changed if it's the $wgParser.
4620 *
4621 * @param User &$user
4622 * @param string|bool $nickname Nickname to use or false to use user's default nickname
4623 * @param bool|null $fancySig whether the nicknname is the complete signature
4624 * or null to use default value
4625 * @return string
4626 */
4627 public function getUserSig( &$user, $nickname = false, $fancySig = null ) {
4628 global $wgMaxSigChars;
4629
4630 $username = $user->getName();
4631
4632 # If not given, retrieve from the user object.
4633 if ( $nickname === false ) {
4634 $nickname = $user->getOption( 'nickname' );
4635 }
4636
4637 if ( is_null( $fancySig ) ) {
4638 $fancySig = $user->getBoolOption( 'fancysig' );
4639 }
4640
4641 $nickname = $nickname == null ? $username : $nickname;
4642
4643 if ( mb_strlen( $nickname ) > $wgMaxSigChars ) {
4644 $nickname = $username;
4645 wfDebug( __METHOD__ . ": $username has overlong signature.\n" );
4646 } elseif ( $fancySig !== false ) {
4647 # Sig. might contain markup; validate this
4648 if ( $this->validateSig( $nickname ) !== false ) {
4649 # Validated; clean up (if needed) and return it
4650 return $this->cleanSig( $nickname, true );
4651 } else {
4652 # Failed to validate; fall back to the default
4653 $nickname = $username;
4654 wfDebug( __METHOD__ . ": $username has bad XML tags in signature.\n" );
4655 }
4656 }
4657
4658 # Make sure nickname doesnt get a sig in a sig
4659 $nickname = self::cleanSigInSig( $nickname );
4660
4661 # If we're still here, make it a link to the user page
4662 $userText = wfEscapeWikiText( $username );
4663 $nickText = wfEscapeWikiText( $nickname );
4664 $msgName = $user->isAnon() ? 'signature-anon' : 'signature';
4665
4666 return wfMessage( $msgName, $userText, $nickText )->inContentLanguage()
4667 ->title( $this->getTitle() )->text();
4668 }
4669
4670 /**
4671 * Check that the user's signature contains no bad XML
4672 *
4673 * @param string $text
4674 * @return string|bool An expanded string, or false if invalid.
4675 */
4676 public function validateSig( $text ) {
4677 return Xml::isWellFormedXmlFragment( $text ) ? $text : false;
4678 }
4679
4680 /**
4681 * Clean up signature text
4682 *
4683 * 1) Strip 3, 4 or 5 tildes out of signatures @see cleanSigInSig
4684 * 2) Substitute all transclusions
4685 *
4686 * @param string $text
4687 * @param bool $parsing Whether we're cleaning (preferences save) or parsing
4688 * @return string Signature text
4689 */
4690 public function cleanSig( $text, $parsing = false ) {
4691 if ( !$parsing ) {
4692 global $wgTitle;
4693 $magicScopeVariable = $this->lock();
4694 $this->startParse( $wgTitle, new ParserOptions, self::OT_PREPROCESS, true );
4695 }
4696
4697 # Option to disable this feature
4698 if ( !$this->mOptions->getCleanSignatures() ) {
4699 return $text;
4700 }
4701
4702 # @todo FIXME: Regex doesn't respect extension tags or nowiki
4703 # => Move this logic to braceSubstitution()
4704 $substWord = $this->magicWordFactory->get( 'subst' );
4705 $substRegex = '/\{\{(?!(?:' . $substWord->getBaseRegex() . '))/x' . $substWord->getRegexCase();
4706 $substText = '{{' . $substWord->getSynonym( 0 );
4707
4708 $text = preg_replace( $substRegex, $substText, $text );
4709 $text = self::cleanSigInSig( $text );
4710 $dom = $this->preprocessToDom( $text );
4711 $frame = $this->getPreprocessor()->newFrame();
4712 $text = $frame->expand( $dom );
4713
4714 if ( !$parsing ) {
4715 $text = $this->mStripState->unstripBoth( $text );
4716 }
4717
4718 return $text;
4719 }
4720
4721 /**
4722 * Strip 3, 4 or 5 tildes out of signatures.
4723 *
4724 * @param string $text
4725 * @return string Signature text with /~{3,5}/ removed
4726 */
4727 public static function cleanSigInSig( $text ) {
4728 $text = preg_replace( '/~{3,5}/', '', $text );
4729 return $text;
4730 }
4731
4732 /**
4733 * Set up some variables which are usually set up in parse()
4734 * so that an external function can call some class members with confidence
4735 *
4736 * @param Title|null $title
4737 * @param ParserOptions $options
4738 * @param int $outputType
4739 * @param bool $clearState
4740 */
4741 public function startExternalParse( Title $title = null, ParserOptions $options,
4742 $outputType, $clearState = true
4743 ) {
4744 $this->startParse( $title, $options, $outputType, $clearState );
4745 }
4746
4747 /**
4748 * @param Title|null $title
4749 * @param ParserOptions $options
4750 * @param int $outputType
4751 * @param bool $clearState
4752 */
4753 private function startParse( Title $title = null, ParserOptions $options,
4754 $outputType, $clearState = true
4755 ) {
4756 $this->setTitle( $title );
4757 $this->mOptions = $options;
4758 $this->setOutputType( $outputType );
4759 if ( $clearState ) {
4760 $this->clearState();
4761 }
4762 }
4763
4764 /**
4765 * Wrapper for preprocess()
4766 *
4767 * @param string $text The text to preprocess
4768 * @param ParserOptions $options
4769 * @param Title|null $title Title object or null to use $wgTitle
4770 * @return string
4771 */
4772 public function transformMsg( $text, $options, $title = null ) {
4773 static $executing = false;
4774
4775 # Guard against infinite recursion
4776 if ( $executing ) {
4777 return $text;
4778 }
4779 $executing = true;
4780
4781 if ( !$title ) {
4782 global $wgTitle;
4783 $title = $wgTitle;
4784 }
4785
4786 $text = $this->preprocess( $text, $title, $options );
4787
4788 $executing = false;
4789 return $text;
4790 }
4791
4792 /**
4793 * Create an HTML-style tag, e.g. "<yourtag>special text</yourtag>"
4794 * The callback should have the following form:
4795 * function myParserHook( $text, $params, $parser, $frame ) { ... }
4796 *
4797 * Transform and return $text. Use $parser for any required context, e.g. use
4798 * $parser->getTitle() and $parser->getOptions() not $wgTitle or $wgOut->mParserOptions
4799 *
4800 * Hooks may return extended information by returning an array, of which the
4801 * first numbered element (index 0) must be the return string, and all other
4802 * entries are extracted into local variables within an internal function
4803 * in the Parser class.
4804 *
4805 * This interface (introduced r61913) appears to be undocumented, but
4806 * 'markerType' is used by some core tag hooks to override which strip
4807 * array their results are placed in. **Use great caution if attempting
4808 * this interface, as it is not documented and injudicious use could smash
4809 * private variables.**
4810 *
4811 * @param string $tag The tag to use, e.g. 'hook' for "<hook>"
4812 * @param callable $callback The callback function (and object) to use for the tag
4813 * @throws MWException
4814 * @return callable|null The old value of the mTagHooks array associated with the hook
4815 */
4816 public function setHook( $tag, callable $callback ) {
4817 $tag = strtolower( $tag );
4818 if ( preg_match( '/[<>\r\n]/', $tag, $m ) ) {
4819 throw new MWException( "Invalid character {$m[0]} in setHook('$tag', ...) call" );
4820 }
4821 $oldVal = $this->mTagHooks[$tag] ?? null;
4822 $this->mTagHooks[$tag] = $callback;
4823 if ( !in_array( $tag, $this->mStripList ) ) {
4824 $this->mStripList[] = $tag;
4825 }
4826
4827 return $oldVal;
4828 }
4829
4830 /**
4831 * As setHook(), but letting the contents be parsed.
4832 *
4833 * Transparent tag hooks are like regular XML-style tag hooks, except they
4834 * operate late in the transformation sequence, on HTML instead of wikitext.
4835 *
4836 * This is probably obsoleted by things dealing with parser frames?
4837 * The only extension currently using it is geoserver.
4838 *
4839 * @since 1.10
4840 * @todo better document or deprecate this
4841 *
4842 * @param string $tag The tag to use, e.g. 'hook' for "<hook>"
4843 * @param callable $callback The callback function (and object) to use for the tag
4844 * @throws MWException
4845 * @return callable|null The old value of the mTagHooks array associated with the hook
4846 */
4847 public function setTransparentTagHook( $tag, callable $callback ) {
4848 $tag = strtolower( $tag );
4849 if ( preg_match( '/[<>\r\n]/', $tag, $m ) ) {
4850 throw new MWException( "Invalid character {$m[0]} in setTransparentHook('$tag', ...) call" );
4851 }
4852 $oldVal = $this->mTransparentTagHooks[$tag] ?? null;
4853 $this->mTransparentTagHooks[$tag] = $callback;
4854
4855 return $oldVal;
4856 }
4857
4858 /**
4859 * Remove all tag hooks
4860 */
4861 public function clearTagHooks() {
4862 $this->mTagHooks = [];
4863 $this->mFunctionTagHooks = [];
4864 $this->mStripList = $this->mDefaultStripList;
4865 }
4866
4867 /**
4868 * Create a function, e.g. {{sum:1|2|3}}
4869 * The callback function should have the form:
4870 * function myParserFunction( &$parser, $arg1, $arg2, $arg3 ) { ... }
4871 *
4872 * Or with Parser::SFH_OBJECT_ARGS:
4873 * function myParserFunction( $parser, $frame, $args ) { ... }
4874 *
4875 * The callback may either return the text result of the function, or an array with the text
4876 * in element 0, and a number of flags in the other elements. The names of the flags are
4877 * specified in the keys. Valid flags are:
4878 * found The text returned is valid, stop processing the template. This
4879 * is on by default.
4880 * nowiki Wiki markup in the return value should be escaped
4881 * isHTML The returned text is HTML, armour it against wikitext transformation
4882 *
4883 * @param string $id The magic word ID
4884 * @param callable $callback The callback function (and object) to use
4885 * @param int $flags A combination of the following flags:
4886 * Parser::SFH_NO_HASH No leading hash, i.e. {{plural:...}} instead of {{#if:...}}
4887 *
4888 * Parser::SFH_OBJECT_ARGS Pass the template arguments as PPNode objects instead of text.
4889 * This allows for conditional expansion of the parse tree, allowing you to eliminate dead
4890 * branches and thus speed up parsing. It is also possible to analyse the parse tree of
4891 * the arguments, and to control the way they are expanded.
4892 *
4893 * The $frame parameter is a PPFrame. This can be used to produce expanded text from the
4894 * arguments, for instance:
4895 * $text = isset( $args[0] ) ? $frame->expand( $args[0] ) : '';
4896 *
4897 * For technical reasons, $args[0] is pre-expanded and will be a string. This may change in
4898 * future versions. Please call $frame->expand() on it anyway so that your code keeps
4899 * working if/when this is changed.
4900 *
4901 * If you want whitespace to be trimmed from $args, you need to do it yourself, post-
4902 * expansion.
4903 *
4904 * Please read the documentation in includes/parser/Preprocessor.php for more information
4905 * about the methods available in PPFrame and PPNode.
4906 *
4907 * @throws MWException
4908 * @return string|callable The old callback function for this name, if any
4909 */
4910 public function setFunctionHook( $id, callable $callback, $flags = 0 ) {
4911 $oldVal = isset( $this->mFunctionHooks[$id] ) ? $this->mFunctionHooks[$id][0] : null;
4912 $this->mFunctionHooks[$id] = [ $callback, $flags ];
4913
4914 # Add to function cache
4915 $mw = $this->magicWordFactory->get( $id );
4916 if ( !$mw ) {
4917 throw new MWException( __METHOD__ . '() expecting a magic word identifier.' );
4918 }
4919
4920 $synonyms = $mw->getSynonyms();
4921 $sensitive = intval( $mw->isCaseSensitive() );
4922
4923 foreach ( $synonyms as $syn ) {
4924 # Case
4925 if ( !$sensitive ) {
4926 $syn = $this->contLang->lc( $syn );
4927 }
4928 # Add leading hash
4929 if ( !( $flags & self::SFH_NO_HASH ) ) {
4930 $syn = '#' . $syn;
4931 }
4932 # Remove trailing colon
4933 if ( substr( $syn, -1, 1 ) === ':' ) {
4934 $syn = substr( $syn, 0, -1 );
4935 }
4936 $this->mFunctionSynonyms[$sensitive][$syn] = $id;
4937 }
4938 return $oldVal;
4939 }
4940
4941 /**
4942 * Get all registered function hook identifiers
4943 *
4944 * @return array
4945 */
4946 public function getFunctionHooks() {
4947 $this->firstCallInit();
4948 return array_keys( $this->mFunctionHooks );
4949 }
4950
4951 /**
4952 * Create a tag function, e.g. "<test>some stuff</test>".
4953 * Unlike tag hooks, tag functions are parsed at preprocessor level.
4954 * Unlike parser functions, their content is not preprocessed.
4955 * @param string $tag
4956 * @param callable $callback
4957 * @param int $flags
4958 * @throws MWException
4959 * @return null
4960 */
4961 public function setFunctionTagHook( $tag, callable $callback, $flags ) {
4962 $tag = strtolower( $tag );
4963 if ( preg_match( '/[<>\r\n]/', $tag, $m ) ) {
4964 throw new MWException( "Invalid character {$m[0]} in setFunctionTagHook('$tag', ...) call" );
4965 }
4966 $old = $this->mFunctionTagHooks[$tag] ?? null;
4967 $this->mFunctionTagHooks[$tag] = [ $callback, $flags ];
4968
4969 if ( !in_array( $tag, $this->mStripList ) ) {
4970 $this->mStripList[] = $tag;
4971 }
4972
4973 return $old;
4974 }
4975
4976 /**
4977 * Replace "<!--LINK-->" link placeholders with actual links, in the buffer
4978 * Placeholders created in Linker::link()
4979 *
4980 * @param string &$text
4981 * @param int $options
4982 */
4983 public function replaceLinkHolders( &$text, $options = 0 ) {
4984 $this->mLinkHolders->replace( $text );
4985 }
4986
4987 /**
4988 * Replace "<!--LINK-->" link placeholders with plain text of links
4989 * (not HTML-formatted).
4990 *
4991 * @param string $text
4992 * @return string
4993 */
4994 public function replaceLinkHoldersText( $text ) {
4995 return $this->mLinkHolders->replaceText( $text );
4996 }
4997
4998 /**
4999 * Renders an image gallery from a text with one line per image.
5000 * text labels may be given by using |-style alternative text. E.g.
5001 * Image:one.jpg|The number "1"
5002 * Image:tree.jpg|A tree
5003 * given as text will return the HTML of a gallery with two images,
5004 * labeled 'The number "1"' and
5005 * 'A tree'.
5006 *
5007 * @param string $text
5008 * @param array $params
5009 * @return string HTML
5010 */
5011 public function renderImageGallery( $text, $params ) {
5012 $mode = false;
5013 if ( isset( $params['mode'] ) ) {
5014 $mode = $params['mode'];
5015 }
5016
5017 try {
5018 $ig = ImageGalleryBase::factory( $mode );
5019 } catch ( Exception $e ) {
5020 // If invalid type set, fallback to default.
5021 $ig = ImageGalleryBase::factory( false );
5022 }
5023
5024 $ig->setContextTitle( $this->mTitle );
5025 $ig->setShowBytes( false );
5026 $ig->setShowDimensions( false );
5027 $ig->setShowFilename( false );
5028 $ig->setParser( $this );
5029 $ig->setHideBadImages();
5030 $ig->setAttributes( Sanitizer::validateTagAttributes( $params, 'ul' ) );
5031
5032 if ( isset( $params['showfilename'] ) ) {
5033 $ig->setShowFilename( true );
5034 } else {
5035 $ig->setShowFilename( false );
5036 }
5037 if ( isset( $params['caption'] ) ) {
5038 $caption = $params['caption'];
5039 $caption = htmlspecialchars( $caption );
5040 $caption = $this->replaceInternalLinks( $caption );
5041 $ig->setCaptionHtml( $caption );
5042 }
5043 if ( isset( $params['perrow'] ) ) {
5044 $ig->setPerRow( $params['perrow'] );
5045 }
5046 if ( isset( $params['widths'] ) ) {
5047 $ig->setWidths( $params['widths'] );
5048 }
5049 if ( isset( $params['heights'] ) ) {
5050 $ig->setHeights( $params['heights'] );
5051 }
5052 $ig->setAdditionalOptions( $params );
5053
5054 // Avoid PHP 7.1 warning from passing $this by reference
5055 $parser = $this;
5056 Hooks::run( 'BeforeParserrenderImageGallery', [ &$parser, &$ig ] );
5057
5058 $lines = StringUtils::explode( "\n", $text );
5059 foreach ( $lines as $line ) {
5060 # match lines like these:
5061 # Image:someimage.jpg|This is some image
5062 $matches = [];
5063 preg_match( "/^([^|]+)(\\|(.*))?$/", $line, $matches );
5064 # Skip empty lines
5065 if ( count( $matches ) == 0 ) {
5066 continue;
5067 }
5068
5069 if ( strpos( $matches[0], '%' ) !== false ) {
5070 $matches[1] = rawurldecode( $matches[1] );
5071 }
5072 $title = Title::newFromText( $matches[1], NS_FILE );
5073 if ( is_null( $title ) ) {
5074 # Bogus title. Ignore these so we don't bomb out later.
5075 continue;
5076 }
5077
5078 # We need to get what handler the file uses, to figure out parameters.
5079 # Note, a hook can overide the file name, and chose an entirely different
5080 # file (which potentially could be of a different type and have different handler).
5081 $options = [];
5082 $descQuery = false;
5083 Hooks::run( 'BeforeParserFetchFileAndTitle',
5084 [ $this, $title, &$options, &$descQuery ] );
5085 # Don't register it now, as TraditionalImageGallery does that later.
5086 $file = $this->fetchFileNoRegister( $title, $options );
5087 $handler = $file ? $file->getHandler() : false;
5088
5089 $paramMap = [
5090 'img_alt' => 'gallery-internal-alt',
5091 'img_link' => 'gallery-internal-link',
5092 ];
5093 if ( $handler ) {
5094 $paramMap = $paramMap + $handler->getParamMap();
5095 // We don't want people to specify per-image widths.
5096 // Additionally the width parameter would need special casing anyhow.
5097 unset( $paramMap['img_width'] );
5098 }
5099
5100 $mwArray = $this->magicWordFactory->newArray( array_keys( $paramMap ) );
5101
5102 $label = '';
5103 $alt = '';
5104 $link = '';
5105 $handlerOptions = [];
5106 if ( isset( $matches[3] ) ) {
5107 // look for an |alt= definition while trying not to break existing
5108 // captions with multiple pipes (|) in it, until a more sensible grammar
5109 // is defined for images in galleries
5110
5111 // FIXME: Doing recursiveTagParse at this stage, and the trim before
5112 // splitting on '|' is a bit odd, and different from makeImage.
5113 $matches[3] = $this->recursiveTagParse( trim( $matches[3] ) );
5114 // Protect LanguageConverter markup
5115 $parameterMatches = StringUtils::delimiterExplode(
5116 '-{', '}-', '|', $matches[3], true /* nested */
5117 );
5118
5119 foreach ( $parameterMatches as $parameterMatch ) {
5120 list( $magicName, $match ) = $mwArray->matchVariableStartToEnd( $parameterMatch );
5121 if ( $magicName ) {
5122 $paramName = $paramMap[$magicName];
5123
5124 switch ( $paramName ) {
5125 case 'gallery-internal-alt':
5126 $alt = $this->stripAltText( $match, false );
5127 break;
5128 case 'gallery-internal-link':
5129 $linkValue = strip_tags( $this->replaceLinkHoldersText( $match ) );
5130 $chars = self::EXT_LINK_URL_CLASS;
5131 $addr = self::EXT_LINK_ADDR;
5132 $prots = $this->mUrlProtocols;
5133 // check to see if link matches an absolute url, if not then it must be a wiki link.
5134 if ( preg_match( '/^-{R|(.*)}-$/', $linkValue ) ) {
5135 // Result of LanguageConverter::markNoConversion
5136 // invoked on an external link.
5137 $linkValue = substr( $linkValue, 4, -2 );
5138 }
5139 if ( preg_match( "/^($prots)$addr$chars*$/u", $linkValue ) ) {
5140 $link = $linkValue;
5141 $this->mOutput->addExternalLink( $link );
5142 } else {
5143 $localLinkTitle = Title::newFromText( $linkValue );
5144 if ( $localLinkTitle !== null ) {
5145 $this->mOutput->addLink( $localLinkTitle );
5146 $link = $localLinkTitle->getLinkURL();
5147 }
5148 }
5149 break;
5150 default:
5151 // Must be a handler specific parameter.
5152 if ( $handler->validateParam( $paramName, $match ) ) {
5153 $handlerOptions[$paramName] = $match;
5154 } else {
5155 // Guess not, consider it as caption.
5156 wfDebug( "$parameterMatch failed parameter validation\n" );
5157 $label = $parameterMatch;
5158 }
5159 }
5160
5161 } else {
5162 // Last pipe wins.
5163 $label = $parameterMatch;
5164 }
5165 }
5166 }
5167
5168 $ig->add( $title, $label, $alt, $link, $handlerOptions );
5169 }
5170 $html = $ig->toHTML();
5171 Hooks::run( 'AfterParserFetchFileAndTitle', [ $this, $ig, &$html ] );
5172 return $html;
5173 }
5174
5175 /**
5176 * @param MediaHandler $handler
5177 * @return array
5178 */
5179 public function getImageParams( $handler ) {
5180 if ( $handler ) {
5181 $handlerClass = get_class( $handler );
5182 } else {
5183 $handlerClass = '';
5184 }
5185 if ( !isset( $this->mImageParams[$handlerClass] ) ) {
5186 # Initialise static lists
5187 static $internalParamNames = [
5188 'horizAlign' => [ 'left', 'right', 'center', 'none' ],
5189 'vertAlign' => [ 'baseline', 'sub', 'super', 'top', 'text-top', 'middle',
5190 'bottom', 'text-bottom' ],
5191 'frame' => [ 'thumbnail', 'manualthumb', 'framed', 'frameless',
5192 'upright', 'border', 'link', 'alt', 'class' ],
5193 ];
5194 static $internalParamMap;
5195 if ( !$internalParamMap ) {
5196 $internalParamMap = [];
5197 foreach ( $internalParamNames as $type => $names ) {
5198 foreach ( $names as $name ) {
5199 // For grep: img_left, img_right, img_center, img_none,
5200 // img_baseline, img_sub, img_super, img_top, img_text_top, img_middle,
5201 // img_bottom, img_text_bottom,
5202 // img_thumbnail, img_manualthumb, img_framed, img_frameless, img_upright,
5203 // img_border, img_link, img_alt, img_class
5204 $magicName = str_replace( '-', '_', "img_$name" );
5205 $internalParamMap[$magicName] = [ $type, $name ];
5206 }
5207 }
5208 }
5209
5210 # Add handler params
5211 $paramMap = $internalParamMap;
5212 if ( $handler ) {
5213 $handlerParamMap = $handler->getParamMap();
5214 foreach ( $handlerParamMap as $magic => $paramName ) {
5215 $paramMap[$magic] = [ 'handler', $paramName ];
5216 }
5217 }
5218 $this->mImageParams[$handlerClass] = $paramMap;
5219 $this->mImageParamsMagicArray[$handlerClass] =
5220 $this->magicWordFactory->newArray( array_keys( $paramMap ) );
5221 }
5222 return [ $this->mImageParams[$handlerClass], $this->mImageParamsMagicArray[$handlerClass] ];
5223 }
5224
5225 /**
5226 * Parse image options text and use it to make an image
5227 *
5228 * @param Title $title
5229 * @param string $options
5230 * @param LinkHolderArray|bool $holders
5231 * @return string HTML
5232 */
5233 public function makeImage( $title, $options, $holders = false ) {
5234 # Check if the options text is of the form "options|alt text"
5235 # Options are:
5236 # * thumbnail make a thumbnail with enlarge-icon and caption, alignment depends on lang
5237 # * left no resizing, just left align. label is used for alt= only
5238 # * right same, but right aligned
5239 # * none same, but not aligned
5240 # * ___px scale to ___ pixels width, no aligning. e.g. use in taxobox
5241 # * center center the image
5242 # * frame Keep original image size, no magnify-button.
5243 # * framed Same as "frame"
5244 # * frameless like 'thumb' but without a frame. Keeps user preferences for width
5245 # * upright reduce width for upright images, rounded to full __0 px
5246 # * border draw a 1px border around the image
5247 # * alt Text for HTML alt attribute (defaults to empty)
5248 # * class Set a class for img node
5249 # * link Set the target of the image link. Can be external, interwiki, or local
5250 # vertical-align values (no % or length right now):
5251 # * baseline
5252 # * sub
5253 # * super
5254 # * top
5255 # * text-top
5256 # * middle
5257 # * bottom
5258 # * text-bottom
5259
5260 # Protect LanguageConverter markup when splitting into parts
5261 $parts = StringUtils::delimiterExplode(
5262 '-{', '}-', '|', $options, true /* allow nesting */
5263 );
5264
5265 # Give extensions a chance to select the file revision for us
5266 $options = [];
5267 $descQuery = false;
5268 Hooks::run( 'BeforeParserFetchFileAndTitle',
5269 [ $this, $title, &$options, &$descQuery ] );
5270 # Fetch and register the file (file title may be different via hooks)
5271 list( $file, $title ) = $this->fetchFileAndTitle( $title, $options );
5272
5273 # Get parameter map
5274 $handler = $file ? $file->getHandler() : false;
5275
5276 list( $paramMap, $mwArray ) = $this->getImageParams( $handler );
5277
5278 if ( !$file ) {
5279 $this->addTrackingCategory( 'broken-file-category' );
5280 }
5281
5282 # Process the input parameters
5283 $caption = '';
5284 $params = [ 'frame' => [], 'handler' => [],
5285 'horizAlign' => [], 'vertAlign' => [] ];
5286 $seenformat = false;
5287 foreach ( $parts as $part ) {
5288 $part = trim( $part );
5289 list( $magicName, $value ) = $mwArray->matchVariableStartToEnd( $part );
5290 $validated = false;
5291 if ( isset( $paramMap[$magicName] ) ) {
5292 list( $type, $paramName ) = $paramMap[$magicName];
5293
5294 # Special case; width and height come in one variable together
5295 if ( $type === 'handler' && $paramName === 'width' ) {
5296 $parsedWidthParam = self::parseWidthParam( $value );
5297 if ( isset( $parsedWidthParam['width'] ) ) {
5298 $width = $parsedWidthParam['width'];
5299 if ( $handler->validateParam( 'width', $width ) ) {
5300 $params[$type]['width'] = $width;
5301 $validated = true;
5302 }
5303 }
5304 if ( isset( $parsedWidthParam['height'] ) ) {
5305 $height = $parsedWidthParam['height'];
5306 if ( $handler->validateParam( 'height', $height ) ) {
5307 $params[$type]['height'] = $height;
5308 $validated = true;
5309 }
5310 }
5311 # else no validation -- T15436
5312 } else {
5313 if ( $type === 'handler' ) {
5314 # Validate handler parameter
5315 $validated = $handler->validateParam( $paramName, $value );
5316 } else {
5317 # Validate internal parameters
5318 switch ( $paramName ) {
5319 case 'manualthumb':
5320 case 'alt':
5321 case 'class':
5322 # @todo FIXME: Possibly check validity here for
5323 # manualthumb? downstream behavior seems odd with
5324 # missing manual thumbs.
5325 $validated = true;
5326 $value = $this->stripAltText( $value, $holders );
5327 break;
5328 case 'link':
5329 $chars = self::EXT_LINK_URL_CLASS;
5330 $addr = self::EXT_LINK_ADDR;
5331 $prots = $this->mUrlProtocols;
5332 if ( $value === '' ) {
5333 $paramName = 'no-link';
5334 $value = true;
5335 $validated = true;
5336 } elseif ( preg_match( "/^((?i)$prots)/", $value ) ) {
5337 if ( preg_match( "/^((?i)$prots)$addr$chars*$/u", $value, $m ) ) {
5338 $paramName = 'link-url';
5339 $this->mOutput->addExternalLink( $value );
5340 if ( $this->mOptions->getExternalLinkTarget() ) {
5341 $params[$type]['link-target'] = $this->mOptions->getExternalLinkTarget();
5342 }
5343 $validated = true;
5344 }
5345 } else {
5346 $linkTitle = Title::newFromText( $value );
5347 if ( $linkTitle ) {
5348 $paramName = 'link-title';
5349 $value = $linkTitle;
5350 $this->mOutput->addLink( $linkTitle );
5351 $validated = true;
5352 }
5353 }
5354 break;
5355 case 'frameless':
5356 case 'framed':
5357 case 'thumbnail':
5358 // use first appearing option, discard others.
5359 $validated = !$seenformat;
5360 $seenformat = true;
5361 break;
5362 default:
5363 # Most other things appear to be empty or numeric...
5364 $validated = ( $value === false || is_numeric( trim( $value ) ) );
5365 }
5366 }
5367
5368 if ( $validated ) {
5369 $params[$type][$paramName] = $value;
5370 }
5371 }
5372 }
5373 if ( !$validated ) {
5374 $caption = $part;
5375 }
5376 }
5377
5378 # Process alignment parameters
5379 if ( $params['horizAlign'] ) {
5380 $params['frame']['align'] = key( $params['horizAlign'] );
5381 }
5382 if ( $params['vertAlign'] ) {
5383 $params['frame']['valign'] = key( $params['vertAlign'] );
5384 }
5385
5386 $params['frame']['caption'] = $caption;
5387
5388 # Will the image be presented in a frame, with the caption below?
5389 $imageIsFramed = isset( $params['frame']['frame'] )
5390 || isset( $params['frame']['framed'] )
5391 || isset( $params['frame']['thumbnail'] )
5392 || isset( $params['frame']['manualthumb'] );
5393
5394 # In the old days, [[Image:Foo|text...]] would set alt text. Later it
5395 # came to also set the caption, ordinary text after the image -- which
5396 # makes no sense, because that just repeats the text multiple times in
5397 # screen readers. It *also* came to set the title attribute.
5398 # Now that we have an alt attribute, we should not set the alt text to
5399 # equal the caption: that's worse than useless, it just repeats the
5400 # text. This is the framed/thumbnail case. If there's no caption, we
5401 # use the unnamed parameter for alt text as well, just for the time be-
5402 # ing, if the unnamed param is set and the alt param is not.
5403 # For the future, we need to figure out if we want to tweak this more,
5404 # e.g., introducing a title= parameter for the title; ignoring the un-
5405 # named parameter entirely for images without a caption; adding an ex-
5406 # plicit caption= parameter and preserving the old magic unnamed para-
5407 # meter for BC; ...
5408 if ( $imageIsFramed ) { # Framed image
5409 if ( $caption === '' && !isset( $params['frame']['alt'] ) ) {
5410 # No caption or alt text, add the filename as the alt text so
5411 # that screen readers at least get some description of the image
5412 $params['frame']['alt'] = $title->getText();
5413 }
5414 # Do not set $params['frame']['title'] because tooltips don't make sense
5415 # for framed images
5416 } else { # Inline image
5417 if ( !isset( $params['frame']['alt'] ) ) {
5418 # No alt text, use the "caption" for the alt text
5419 if ( $caption !== '' ) {
5420 $params['frame']['alt'] = $this->stripAltText( $caption, $holders );
5421 } else {
5422 # No caption, fall back to using the filename for the
5423 # alt text
5424 $params['frame']['alt'] = $title->getText();
5425 }
5426 }
5427 # Use the "caption" for the tooltip text
5428 $params['frame']['title'] = $this->stripAltText( $caption, $holders );
5429 }
5430
5431 Hooks::run( 'ParserMakeImageParams', [ $title, $file, &$params, $this ] );
5432
5433 # Linker does the rest
5434 $time = $options['time'] ?? false;
5435 $ret = Linker::makeImageLink( $this, $title, $file, $params['frame'], $params['handler'],
5436 $time, $descQuery, $this->mOptions->getThumbSize() );
5437
5438 # Give the handler a chance to modify the parser object
5439 if ( $handler ) {
5440 $handler->parserTransformHook( $this, $file );
5441 }
5442
5443 return $ret;
5444 }
5445
5446 /**
5447 * @param string $caption
5448 * @param LinkHolderArray|bool $holders
5449 * @return mixed|string
5450 */
5451 protected function stripAltText( $caption, $holders ) {
5452 # Strip bad stuff out of the title (tooltip). We can't just use
5453 # replaceLinkHoldersText() here, because if this function is called
5454 # from replaceInternalLinks2(), mLinkHolders won't be up-to-date.
5455 if ( $holders ) {
5456 $tooltip = $holders->replaceText( $caption );
5457 } else {
5458 $tooltip = $this->replaceLinkHoldersText( $caption );
5459 }
5460
5461 # make sure there are no placeholders in thumbnail attributes
5462 # that are later expanded to html- so expand them now and
5463 # remove the tags
5464 $tooltip = $this->mStripState->unstripBoth( $tooltip );
5465 $tooltip = Sanitizer::stripAllTags( $tooltip );
5466
5467 return $tooltip;
5468 }
5469
5470 /**
5471 * Set a flag in the output object indicating that the content is dynamic and
5472 * shouldn't be cached.
5473 * @deprecated since 1.28; use getOutput()->updateCacheExpiry()
5474 */
5475 public function disableCache() {
5476 wfDebug( "Parser output marked as uncacheable.\n" );
5477 if ( !$this->mOutput ) {
5478 throw new MWException( __METHOD__ .
5479 " can only be called when actually parsing something" );
5480 }
5481 $this->mOutput->updateCacheExpiry( 0 ); // new style, for consistency
5482 }
5483
5484 /**
5485 * Callback from the Sanitizer for expanding items found in HTML attribute
5486 * values, so they can be safely tested and escaped.
5487 *
5488 * @param string &$text
5489 * @param bool|PPFrame $frame
5490 * @return string
5491 */
5492 public function attributeStripCallback( &$text, $frame = false ) {
5493 $text = $this->replaceVariables( $text, $frame );
5494 $text = $this->mStripState->unstripBoth( $text );
5495 return $text;
5496 }
5497
5498 /**
5499 * Accessor
5500 *
5501 * @return array
5502 */
5503 public function getTags() {
5504 $this->firstCallInit();
5505 return array_merge(
5506 array_keys( $this->mTransparentTagHooks ),
5507 array_keys( $this->mTagHooks ),
5508 array_keys( $this->mFunctionTagHooks )
5509 );
5510 }
5511
5512 /**
5513 * @since 1.32
5514 * @return array
5515 */
5516 public function getFunctionSynonyms() {
5517 $this->firstCallInit();
5518 return $this->mFunctionSynonyms;
5519 }
5520
5521 /**
5522 * @since 1.32
5523 * @return string
5524 */
5525 public function getUrlProtocols() {
5526 return $this->mUrlProtocols;
5527 }
5528
5529 /**
5530 * Replace transparent tags in $text with the values given by the callbacks.
5531 *
5532 * Transparent tag hooks are like regular XML-style tag hooks, except they
5533 * operate late in the transformation sequence, on HTML instead of wikitext.
5534 *
5535 * @param string $text
5536 *
5537 * @return string
5538 */
5539 public function replaceTransparentTags( $text ) {
5540 $matches = [];
5541 $elements = array_keys( $this->mTransparentTagHooks );
5542 $text = self::extractTagsAndParams( $elements, $text, $matches );
5543 $replacements = [];
5544
5545 foreach ( $matches as $marker => $data ) {
5546 list( $element, $content, $params, $tag ) = $data;
5547 $tagName = strtolower( $element );
5548 if ( isset( $this->mTransparentTagHooks[$tagName] ) ) {
5549 $output = call_user_func_array(
5550 $this->mTransparentTagHooks[$tagName],
5551 [ $content, $params, $this ]
5552 );
5553 } else {
5554 $output = $tag;
5555 }
5556 $replacements[$marker] = $output;
5557 }
5558 return strtr( $text, $replacements );
5559 }
5560
5561 /**
5562 * Break wikitext input into sections, and either pull or replace
5563 * some particular section's text.
5564 *
5565 * External callers should use the getSection and replaceSection methods.
5566 *
5567 * @param string $text Page wikitext
5568 * @param string|int $sectionId A section identifier string of the form:
5569 * "<flag1> - <flag2> - ... - <section number>"
5570 *
5571 * Currently the only recognised flag is "T", which means the target section number
5572 * was derived during a template inclusion parse, in other words this is a template
5573 * section edit link. If no flags are given, it was an ordinary section edit link.
5574 * This flag is required to avoid a section numbering mismatch when a section is
5575 * enclosed by "<includeonly>" (T8563).
5576 *
5577 * The section number 0 pulls the text before the first heading; other numbers will
5578 * pull the given section along with its lower-level subsections. If the section is
5579 * not found, $mode=get will return $newtext, and $mode=replace will return $text.
5580 *
5581 * Section 0 is always considered to exist, even if it only contains the empty
5582 * string. If $text is the empty string and section 0 is replaced, $newText is
5583 * returned.
5584 *
5585 * @param string $mode One of "get" or "replace"
5586 * @param string $newText Replacement text for section data.
5587 * @return string For "get", the extracted section text.
5588 * for "replace", the whole page with the section replaced.
5589 */
5590 private function extractSections( $text, $sectionId, $mode, $newText = '' ) {
5591 global $wgTitle; # not generally used but removes an ugly failure mode
5592
5593 $magicScopeVariable = $this->lock();
5594 $this->startParse( $wgTitle, new ParserOptions, self::OT_PLAIN, true );
5595 $outText = '';
5596 $frame = $this->getPreprocessor()->newFrame();
5597
5598 # Process section extraction flags
5599 $flags = 0;
5600 $sectionParts = explode( '-', $sectionId );
5601 $sectionIndex = array_pop( $sectionParts );
5602 foreach ( $sectionParts as $part ) {
5603 if ( $part === 'T' ) {
5604 $flags |= self::PTD_FOR_INCLUSION;
5605 }
5606 }
5607
5608 # Check for empty input
5609 if ( strval( $text ) === '' ) {
5610 # Only sections 0 and T-0 exist in an empty document
5611 if ( $sectionIndex == 0 ) {
5612 if ( $mode === 'get' ) {
5613 return '';
5614 } else {
5615 return $newText;
5616 }
5617 } else {
5618 if ( $mode === 'get' ) {
5619 return $newText;
5620 } else {
5621 return $text;
5622 }
5623 }
5624 }
5625
5626 # Preprocess the text
5627 $root = $this->preprocessToDom( $text, $flags );
5628
5629 # <h> nodes indicate section breaks
5630 # They can only occur at the top level, so we can find them by iterating the root's children
5631 $node = $root->getFirstChild();
5632
5633 # Find the target section
5634 if ( $sectionIndex == 0 ) {
5635 # Section zero doesn't nest, level=big
5636 $targetLevel = 1000;
5637 } else {
5638 while ( $node ) {
5639 if ( $node->getName() === 'h' ) {
5640 $bits = $node->splitHeading();
5641 if ( $bits['i'] == $sectionIndex ) {
5642 $targetLevel = $bits['level'];
5643 break;
5644 }
5645 }
5646 if ( $mode === 'replace' ) {
5647 $outText .= $frame->expand( $node, PPFrame::RECOVER_ORIG );
5648 }
5649 $node = $node->getNextSibling();
5650 }
5651 }
5652
5653 if ( !$node ) {
5654 # Not found
5655 if ( $mode === 'get' ) {
5656 return $newText;
5657 } else {
5658 return $text;
5659 }
5660 }
5661
5662 # Find the end of the section, including nested sections
5663 do {
5664 if ( $node->getName() === 'h' ) {
5665 $bits = $node->splitHeading();
5666 $curLevel = $bits['level'];
5667 if ( $bits['i'] != $sectionIndex && $curLevel <= $targetLevel ) {
5668 break;
5669 }
5670 }
5671 if ( $mode === 'get' ) {
5672 $outText .= $frame->expand( $node, PPFrame::RECOVER_ORIG );
5673 }
5674 $node = $node->getNextSibling();
5675 } while ( $node );
5676
5677 # Write out the remainder (in replace mode only)
5678 if ( $mode === 'replace' ) {
5679 # Output the replacement text
5680 # Add two newlines on -- trailing whitespace in $newText is conventionally
5681 # stripped by the editor, so we need both newlines to restore the paragraph gap
5682 # Only add trailing whitespace if there is newText
5683 if ( $newText != "" ) {
5684 $outText .= $newText . "\n\n";
5685 }
5686
5687 while ( $node ) {
5688 $outText .= $frame->expand( $node, PPFrame::RECOVER_ORIG );
5689 $node = $node->getNextSibling();
5690 }
5691 }
5692
5693 if ( is_string( $outText ) ) {
5694 # Re-insert stripped tags
5695 $outText = rtrim( $this->mStripState->unstripBoth( $outText ) );
5696 }
5697
5698 return $outText;
5699 }
5700
5701 /**
5702 * This function returns the text of a section, specified by a number ($section).
5703 * A section is text under a heading like == Heading == or \<h1\>Heading\</h1\>, or
5704 * the first section before any such heading (section 0).
5705 *
5706 * If a section contains subsections, these are also returned.
5707 *
5708 * @param string $text Text to look in
5709 * @param string|int $sectionId Section identifier as a number or string
5710 * (e.g. 0, 1 or 'T-1').
5711 * @param string $defaultText Default to return if section is not found
5712 *
5713 * @return string Text of the requested section
5714 */
5715 public function getSection( $text, $sectionId, $defaultText = '' ) {
5716 return $this->extractSections( $text, $sectionId, 'get', $defaultText );
5717 }
5718
5719 /**
5720 * This function returns $oldtext after the content of the section
5721 * specified by $section has been replaced with $text. If the target
5722 * section does not exist, $oldtext is returned unchanged.
5723 *
5724 * @param string $oldText Former text of the article
5725 * @param string|int $sectionId Section identifier as a number or string
5726 * (e.g. 0, 1 or 'T-1').
5727 * @param string $newText Replacing text
5728 *
5729 * @return string Modified text
5730 */
5731 public function replaceSection( $oldText, $sectionId, $newText ) {
5732 return $this->extractSections( $oldText, $sectionId, 'replace', $newText );
5733 }
5734
5735 /**
5736 * Get the ID of the revision we are parsing
5737 *
5738 * @return int|null
5739 */
5740 public function getRevisionId() {
5741 return $this->mRevisionId;
5742 }
5743
5744 /**
5745 * Get the revision object for $this->mRevisionId
5746 *
5747 * @return Revision|null Either a Revision object or null
5748 * @since 1.23 (public since 1.23)
5749 */
5750 public function getRevisionObject() {
5751 if ( !is_null( $this->mRevisionObject ) ) {
5752 return $this->mRevisionObject;
5753 }
5754 if ( is_null( $this->mRevisionId ) ) {
5755 return null;
5756 }
5757
5758 $rev = call_user_func(
5759 $this->mOptions->getCurrentRevisionCallback(), $this->getTitle(), $this
5760 );
5761
5762 # If the parse is for a new revision, then the callback should have
5763 # already been set to force the object and should match mRevisionId.
5764 # If not, try to fetch by mRevisionId for sanity.
5765 if ( $rev && $rev->getId() != $this->mRevisionId ) {
5766 $rev = Revision::newFromId( $this->mRevisionId );
5767 }
5768
5769 $this->mRevisionObject = $rev;
5770
5771 return $this->mRevisionObject;
5772 }
5773
5774 /**
5775 * Get the timestamp associated with the current revision, adjusted for
5776 * the default server-local timestamp
5777 * @return string
5778 */
5779 public function getRevisionTimestamp() {
5780 if ( is_null( $this->mRevisionTimestamp ) ) {
5781 $revObject = $this->getRevisionObject();
5782 $timestamp = $revObject ? $revObject->getTimestamp() : wfTimestampNow();
5783
5784 # The cryptic '' timezone parameter tells to use the site-default
5785 # timezone offset instead of the user settings.
5786 # Since this value will be saved into the parser cache, served
5787 # to other users, and potentially even used inside links and such,
5788 # it needs to be consistent for all visitors.
5789 $this->mRevisionTimestamp = $this->contLang->userAdjust( $timestamp, '' );
5790 }
5791 return $this->mRevisionTimestamp;
5792 }
5793
5794 /**
5795 * Get the name of the user that edited the last revision
5796 *
5797 * @return string User name
5798 */
5799 public function getRevisionUser() {
5800 if ( is_null( $this->mRevisionUser ) ) {
5801 $revObject = $this->getRevisionObject();
5802
5803 # if this template is subst: the revision id will be blank,
5804 # so just use the current user's name
5805 if ( $revObject ) {
5806 $this->mRevisionUser = $revObject->getUserText();
5807 } elseif ( $this->ot['wiki'] || $this->mOptions->getIsPreview() ) {
5808 $this->mRevisionUser = $this->getUser()->getName();
5809 }
5810 }
5811 return $this->mRevisionUser;
5812 }
5813
5814 /**
5815 * Get the size of the revision
5816 *
5817 * @return int|null Revision size
5818 */
5819 public function getRevisionSize() {
5820 if ( is_null( $this->mRevisionSize ) ) {
5821 $revObject = $this->getRevisionObject();
5822
5823 # if this variable is subst: the revision id will be blank,
5824 # so just use the parser input size, because the own substituation
5825 # will change the size.
5826 if ( $revObject ) {
5827 $this->mRevisionSize = $revObject->getSize();
5828 } else {
5829 $this->mRevisionSize = $this->mInputSize;
5830 }
5831 }
5832 return $this->mRevisionSize;
5833 }
5834
5835 /**
5836 * Mutator for $mDefaultSort
5837 *
5838 * @param string $sort New value
5839 */
5840 public function setDefaultSort( $sort ) {
5841 $this->mDefaultSort = $sort;
5842 $this->mOutput->setProperty( 'defaultsort', $sort );
5843 }
5844
5845 /**
5846 * Accessor for $mDefaultSort
5847 * Will use the empty string if none is set.
5848 *
5849 * This value is treated as a prefix, so the
5850 * empty string is equivalent to sorting by
5851 * page name.
5852 *
5853 * @return string
5854 */
5855 public function getDefaultSort() {
5856 if ( $this->mDefaultSort !== false ) {
5857 return $this->mDefaultSort;
5858 } else {
5859 return '';
5860 }
5861 }
5862
5863 /**
5864 * Accessor for $mDefaultSort
5865 * Unlike getDefaultSort(), will return false if none is set
5866 *
5867 * @return string|bool
5868 */
5869 public function getCustomDefaultSort() {
5870 return $this->mDefaultSort;
5871 }
5872
5873 private static function getSectionNameFromStrippedText( $text ) {
5874 $text = Sanitizer::normalizeSectionNameWhitespace( $text );
5875 $text = Sanitizer::decodeCharReferences( $text );
5876 $text = self::normalizeSectionName( $text );
5877 return $text;
5878 }
5879
5880 private static function makeAnchor( $sectionName ) {
5881 return '#' . Sanitizer::escapeIdForLink( $sectionName );
5882 }
5883
5884 private static function makeLegacyAnchor( $sectionName ) {
5885 global $wgFragmentMode;
5886 if ( isset( $wgFragmentMode[1] ) && $wgFragmentMode[1] === 'legacy' ) {
5887 // ForAttribute() and ForLink() are the same for legacy encoding
5888 $id = Sanitizer::escapeIdForAttribute( $sectionName, Sanitizer::ID_FALLBACK );
5889 } else {
5890 $id = Sanitizer::escapeIdForLink( $sectionName );
5891 }
5892
5893 return "#$id";
5894 }
5895
5896 /**
5897 * Try to guess the section anchor name based on a wikitext fragment
5898 * presumably extracted from a heading, for example "Header" from
5899 * "== Header ==".
5900 *
5901 * @param string $text
5902 * @return string Anchor (starting with '#')
5903 */
5904 public function guessSectionNameFromWikiText( $text ) {
5905 # Strip out wikitext links(they break the anchor)
5906 $text = $this->stripSectionName( $text );
5907 $sectionName = self::getSectionNameFromStrippedText( $text );
5908 return self::makeAnchor( $sectionName );
5909 }
5910
5911 /**
5912 * Same as guessSectionNameFromWikiText(), but produces legacy anchors
5913 * instead, if possible. For use in redirects, since various versions
5914 * of Microsoft browsers interpret Location: headers as something other
5915 * than UTF-8, resulting in breakage.
5916 *
5917 * @param string $text The section name
5918 * @return string Anchor (starting with '#')
5919 */
5920 public function guessLegacySectionNameFromWikiText( $text ) {
5921 # Strip out wikitext links(they break the anchor)
5922 $text = $this->stripSectionName( $text );
5923 $sectionName = self::getSectionNameFromStrippedText( $text );
5924 return self::makeLegacyAnchor( $sectionName );
5925 }
5926
5927 /**
5928 * Like guessSectionNameFromWikiText(), but takes already-stripped text as input.
5929 * @param string $text Section name (plain text)
5930 * @return string Anchor (starting with '#')
5931 */
5932 public static function guessSectionNameFromStrippedText( $text ) {
5933 $sectionName = self::getSectionNameFromStrippedText( $text );
5934 return self::makeAnchor( $sectionName );
5935 }
5936
5937 /**
5938 * Apply the same normalization as code making links to this section would
5939 *
5940 * @param string $text
5941 * @return string
5942 */
5943 private static function normalizeSectionName( $text ) {
5944 # T90902: ensure the same normalization is applied for IDs as to links
5945 $titleParser = MediaWikiServices::getInstance()->getTitleParser();
5946 try {
5947
5948 $parts = $titleParser->splitTitleString( "#$text" );
5949 } catch ( MalformedTitleException $ex ) {
5950 return $text;
5951 }
5952 return $parts['fragment'];
5953 }
5954
5955 /**
5956 * Strips a text string of wikitext for use in a section anchor
5957 *
5958 * Accepts a text string and then removes all wikitext from the
5959 * string and leaves only the resultant text (i.e. the result of
5960 * [[User:WikiSysop|Sysop]] would be "Sysop" and the result of
5961 * [[User:WikiSysop]] would be "User:WikiSysop") - this is intended
5962 * to create valid section anchors by mimicing the output of the
5963 * parser when headings are parsed.
5964 *
5965 * @param string $text Text string to be stripped of wikitext
5966 * for use in a Section anchor
5967 * @return string Filtered text string
5968 */
5969 public function stripSectionName( $text ) {
5970 # Strip internal link markup
5971 $text = preg_replace( '/\[\[:?([^[|]+)\|([^[]+)\]\]/', '$2', $text );
5972 $text = preg_replace( '/\[\[:?([^[]+)\|?\]\]/', '$1', $text );
5973
5974 # Strip external link markup
5975 # @todo FIXME: Not tolerant to blank link text
5976 # I.E. [https://www.mediawiki.org] will render as [1] or something depending
5977 # on how many empty links there are on the page - need to figure that out.
5978 $text = preg_replace( '/\[(?i:' . $this->mUrlProtocols . ')([^ ]+?) ([^[]+)\]/', '$2', $text );
5979
5980 # Parse wikitext quotes (italics & bold)
5981 $text = $this->doQuotes( $text );
5982
5983 # Strip HTML tags
5984 $text = StringUtils::delimiterReplace( '<', '>', '', $text );
5985 return $text;
5986 }
5987
5988 /**
5989 * strip/replaceVariables/unstrip for preprocessor regression testing
5990 *
5991 * @param string $text
5992 * @param Title $title
5993 * @param ParserOptions $options
5994 * @param int $outputType
5995 *
5996 * @return string
5997 */
5998 public function testSrvus( $text, Title $title, ParserOptions $options,
5999 $outputType = self::OT_HTML
6000 ) {
6001 $magicScopeVariable = $this->lock();
6002 $this->startParse( $title, $options, $outputType, true );
6003
6004 $text = $this->replaceVariables( $text );
6005 $text = $this->mStripState->unstripBoth( $text );
6006 $text = Sanitizer::removeHTMLtags( $text );
6007 return $text;
6008 }
6009
6010 /**
6011 * @param string $text
6012 * @param Title $title
6013 * @param ParserOptions $options
6014 * @return string
6015 */
6016 public function testPst( $text, Title $title, ParserOptions $options ) {
6017 return $this->preSaveTransform( $text, $title, $options->getUser(), $options );
6018 }
6019
6020 /**
6021 * @param string $text
6022 * @param Title $title
6023 * @param ParserOptions $options
6024 * @return string
6025 */
6026 public function testPreprocess( $text, Title $title, ParserOptions $options ) {
6027 return $this->testSrvus( $text, $title, $options, self::OT_PREPROCESS );
6028 }
6029
6030 /**
6031 * Call a callback function on all regions of the given text that are not
6032 * inside strip markers, and replace those regions with the return value
6033 * of the callback. For example, with input:
6034 *
6035 * aaa<MARKER>bbb
6036 *
6037 * This will call the callback function twice, with 'aaa' and 'bbb'. Those
6038 * two strings will be replaced with the value returned by the callback in
6039 * each case.
6040 *
6041 * @param string $s
6042 * @param callable $callback
6043 *
6044 * @return string
6045 */
6046 public function markerSkipCallback( $s, $callback ) {
6047 $i = 0;
6048 $out = '';
6049 while ( $i < strlen( $s ) ) {
6050 $markerStart = strpos( $s, self::MARKER_PREFIX, $i );
6051 if ( $markerStart === false ) {
6052 $out .= call_user_func( $callback, substr( $s, $i ) );
6053 break;
6054 } else {
6055 $out .= call_user_func( $callback, substr( $s, $i, $markerStart - $i ) );
6056 $markerEnd = strpos( $s, self::MARKER_SUFFIX, $markerStart );
6057 if ( $markerEnd === false ) {
6058 $out .= substr( $s, $markerStart );
6059 break;
6060 } else {
6061 $markerEnd += strlen( self::MARKER_SUFFIX );
6062 $out .= substr( $s, $markerStart, $markerEnd - $markerStart );
6063 $i = $markerEnd;
6064 }
6065 }
6066 }
6067 return $out;
6068 }
6069
6070 /**
6071 * Remove any strip markers found in the given text.
6072 *
6073 * @param string $text
6074 * @return string
6075 */
6076 public function killMarkers( $text ) {
6077 return $this->mStripState->killMarkers( $text );
6078 }
6079
6080 /**
6081 * Save the parser state required to convert the given half-parsed text to
6082 * HTML. "Half-parsed" in this context means the output of
6083 * recursiveTagParse() or internalParse(). This output has strip markers
6084 * from replaceVariables (extensionSubstitution() etc.), and link
6085 * placeholders from replaceLinkHolders().
6086 *
6087 * Returns an array which can be serialized and stored persistently. This
6088 * array can later be loaded into another parser instance with
6089 * unserializeHalfParsedText(). The text can then be safely incorporated into
6090 * the return value of a parser hook.
6091 *
6092 * @deprecated since 1.31
6093 * @param string $text
6094 *
6095 * @return array
6096 */
6097 public function serializeHalfParsedText( $text ) {
6098 wfDeprecated( __METHOD__, '1.31' );
6099 $data = [
6100 'text' => $text,
6101 'version' => self::HALF_PARSED_VERSION,
6102 'stripState' => $this->mStripState->getSubState( $text ),
6103 'linkHolders' => $this->mLinkHolders->getSubArray( $text )
6104 ];
6105 return $data;
6106 }
6107
6108 /**
6109 * Load the parser state given in the $data array, which is assumed to
6110 * have been generated by serializeHalfParsedText(). The text contents is
6111 * extracted from the array, and its markers are transformed into markers
6112 * appropriate for the current Parser instance. This transformed text is
6113 * returned, and can be safely included in the return value of a parser
6114 * hook.
6115 *
6116 * If the $data array has been stored persistently, the caller should first
6117 * check whether it is still valid, by calling isValidHalfParsedText().
6118 *
6119 * @deprecated since 1.31
6120 * @param array $data Serialized data
6121 * @throws MWException
6122 * @return string
6123 */
6124 public function unserializeHalfParsedText( $data ) {
6125 wfDeprecated( __METHOD__, '1.31' );
6126 if ( !isset( $data['version'] ) || $data['version'] != self::HALF_PARSED_VERSION ) {
6127 throw new MWException( __METHOD__ . ': invalid version' );
6128 }
6129
6130 # First, extract the strip state.
6131 $texts = [ $data['text'] ];
6132 $texts = $this->mStripState->merge( $data['stripState'], $texts );
6133
6134 # Now renumber links
6135 $texts = $this->mLinkHolders->mergeForeign( $data['linkHolders'], $texts );
6136
6137 # Should be good to go.
6138 return $texts[0];
6139 }
6140
6141 /**
6142 * Returns true if the given array, presumed to be generated by
6143 * serializeHalfParsedText(), is compatible with the current version of the
6144 * parser.
6145 *
6146 * @deprecated since 1.31
6147 * @param array $data
6148 *
6149 * @return bool
6150 */
6151 public function isValidHalfParsedText( $data ) {
6152 wfDeprecated( __METHOD__, '1.31' );
6153 return isset( $data['version'] ) && $data['version'] == self::HALF_PARSED_VERSION;
6154 }
6155
6156 /**
6157 * Parsed a width param of imagelink like 300px or 200x300px
6158 *
6159 * @param string $value
6160 * @param bool $parseHeight
6161 *
6162 * @return array
6163 * @since 1.20
6164 */
6165 public static function parseWidthParam( $value, $parseHeight = true ) {
6166 $parsedWidthParam = [];
6167 if ( $value === '' ) {
6168 return $parsedWidthParam;
6169 }
6170 $m = [];
6171 # (T15500) In both cases (width/height and width only),
6172 # permit trailing "px" for backward compatibility.
6173 if ( $parseHeight && preg_match( '/^([0-9]*)x([0-9]*)\s*(?:px)?\s*$/', $value, $m ) ) {
6174 $width = intval( $m[1] );
6175 $height = intval( $m[2] );
6176 $parsedWidthParam['width'] = $width;
6177 $parsedWidthParam['height'] = $height;
6178 } elseif ( preg_match( '/^[0-9]*\s*(?:px)?\s*$/', $value ) ) {
6179 $width = intval( $value );
6180 $parsedWidthParam['width'] = $width;
6181 }
6182 return $parsedWidthParam;
6183 }
6184
6185 /**
6186 * Lock the current instance of the parser.
6187 *
6188 * This is meant to stop someone from calling the parser
6189 * recursively and messing up all the strip state.
6190 *
6191 * @throws MWException If parser is in a parse
6192 * @return ScopedCallback The lock will be released once the return value goes out of scope.
6193 */
6194 protected function lock() {
6195 if ( $this->mInParse ) {
6196 throw new MWException( "Parser state cleared while parsing. "
6197 . "Did you call Parser::parse recursively? Lock is held by: " . $this->mInParse );
6198 }
6199
6200 // Save the backtrace when locking, so that if some code tries locking again,
6201 // we can print the lock owner's backtrace for easier debugging
6202 $e = new Exception;
6203 $this->mInParse = $e->getTraceAsString();
6204
6205 $recursiveCheck = new ScopedCallback( function () {
6206 $this->mInParse = false;
6207 } );
6208
6209 return $recursiveCheck;
6210 }
6211
6212 /**
6213 * Strip outer <p></p> tag from the HTML source of a single paragraph.
6214 *
6215 * Returns original HTML if the <p/> tag has any attributes, if there's no wrapping <p/> tag,
6216 * or if there is more than one <p/> tag in the input HTML.
6217 *
6218 * @param string $html
6219 * @return string
6220 * @since 1.24
6221 */
6222 public static function stripOuterParagraph( $html ) {
6223 $m = [];
6224 if ( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $html, $m ) ) {
6225 if ( strpos( $m[1], '</p>' ) === false ) {
6226 $html = $m[1];
6227 }
6228 }
6229
6230 return $html;
6231 }
6232
6233 /**
6234 * Return this parser if it is not doing anything, otherwise
6235 * get a fresh parser. You can use this method by doing
6236 * $myParser = $wgParser->getFreshParser(), or more simply
6237 * $wgParser->getFreshParser()->parse( ... );
6238 * if you're unsure if $wgParser is safe to use.
6239 *
6240 * @since 1.24
6241 * @return Parser A parser object that is not parsing anything
6242 */
6243 public function getFreshParser() {
6244 if ( $this->mInParse ) {
6245 return $this->factory->create();
6246 } else {
6247 return $this;
6248 }
6249 }
6250
6251 /**
6252 * Set's up the PHP implementation of OOUI for use in this request
6253 * and instructs OutputPage to enable OOUI for itself.
6254 *
6255 * @since 1.26
6256 */
6257 public function enableOOUI() {
6258 OutputPage::setupOOUI();
6259 $this->mOutput->setEnableOOUI( true );
6260 }
6261 }