Merge "Avoid use of deprecated ObjectCache::getMainWANInstance method"
[lhc/web/wiklou.git] / includes / parser / CoreParserFunctions.php
1 <?php
2 /**
3 * Parser functions provided by MediaWiki core
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\MediaWikiServices;
24
25 /**
26 * Various core parser functions, registered in Parser::firstCallInit()
27 * @ingroup Parser
28 */
29 class CoreParserFunctions {
30 /**
31 * @param Parser $parser
32 * @return void
33 */
34 public static function register( $parser ) {
35 global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
36
37 # Syntax for arguments (see Parser::setFunctionHook):
38 # "name for lookup in localized magic words array",
39 # function callback,
40 # optional Parser::SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}}
41 # instead of {{#int:...}})
42 $noHashFunctions = [
43 'ns', 'nse', 'urlencode', 'lcfirst', 'ucfirst', 'lc', 'uc',
44 'localurl', 'localurle', 'fullurl', 'fullurle', 'canonicalurl',
45 'canonicalurle', 'formatnum', 'grammar', 'gender', 'plural', 'bidi',
46 'numberofpages', 'numberofusers', 'numberofactiveusers',
47 'numberofarticles', 'numberoffiles', 'numberofadmins',
48 'numberingroup', 'numberofedits', 'language',
49 'padleft', 'padright', 'anchorencode', 'defaultsort', 'filepath',
50 'pagesincategory', 'pagesize', 'protectionlevel', 'protectionexpiry',
51 'namespacee', 'namespacenumber', 'talkspace', 'talkspacee',
52 'subjectspace', 'subjectspacee', 'pagename', 'pagenamee',
53 'fullpagename', 'fullpagenamee', 'rootpagename', 'rootpagenamee',
54 'basepagename', 'basepagenamee', 'subpagename', 'subpagenamee',
55 'talkpagename', 'talkpagenamee', 'subjectpagename',
56 'subjectpagenamee', 'pageid', 'revisionid', 'revisionday',
57 'revisionday2', 'revisionmonth', 'revisionmonth1', 'revisionyear',
58 'revisiontimestamp', 'revisionuser', 'cascadingsources',
59 ];
60 foreach ( $noHashFunctions as $func ) {
61 $parser->setFunctionHook( $func, [ __CLASS__, $func ], Parser::SFH_NO_HASH );
62 }
63
64 $parser->setFunctionHook(
65 'namespace',
66 [ __CLASS__, 'mwnamespace' ],
67 Parser::SFH_NO_HASH
68 );
69 $parser->setFunctionHook( 'int', [ __CLASS__, 'intFunction' ], Parser::SFH_NO_HASH );
70 $parser->setFunctionHook( 'special', [ __CLASS__, 'special' ] );
71 $parser->setFunctionHook( 'speciale', [ __CLASS__, 'speciale' ] );
72 $parser->setFunctionHook( 'tag', [ __CLASS__, 'tagObj' ], Parser::SFH_OBJECT_ARGS );
73 $parser->setFunctionHook( 'formatdate', [ __CLASS__, 'formatDate' ] );
74
75 if ( $wgAllowDisplayTitle ) {
76 $parser->setFunctionHook(
77 'displaytitle',
78 [ __CLASS__, 'displaytitle' ],
79 Parser::SFH_NO_HASH
80 );
81 }
82 if ( $wgAllowSlowParserFunctions ) {
83 $parser->setFunctionHook(
84 'pagesinnamespace',
85 [ __CLASS__, 'pagesinnamespace' ],
86 Parser::SFH_NO_HASH
87 );
88 }
89 }
90
91 /**
92 * @param Parser $parser
93 * @param string $part1
94 * @return array
95 */
96 public static function intFunction( $parser, $part1 = '' /*, ... */ ) {
97 if ( strval( $part1 ) !== '' ) {
98 $args = array_slice( func_get_args(), 2 );
99 $message = wfMessage( $part1, $args )
100 ->inLanguage( $parser->getOptions()->getUserLangObj() );
101 return [ $message->plain(), 'noparse' => false ];
102 } else {
103 return [ 'found' => false ];
104 }
105 }
106
107 /**
108 * @param Parser $parser
109 * @param string $date
110 * @param string|null $defaultPref
111 *
112 * @return string
113 */
114 public static function formatDate( $parser, $date, $defaultPref = null ) {
115 $lang = $parser->getFunctionLang();
116 $df = DateFormatter::getInstance( $lang );
117
118 $date = trim( $date );
119
120 $pref = $parser->getOptions()->getDateFormat();
121
122 // Specify a different default date format other than the normal default
123 // if the user has 'default' for their setting
124 if ( $pref == 'default' && $defaultPref ) {
125 $pref = $defaultPref;
126 }
127
128 $date = $df->reformat( $pref, $date, [ 'match-whole' ] );
129 return $date;
130 }
131
132 public static function ns( $parser, $part1 = '' ) {
133 if ( intval( $part1 ) || $part1 == "0" ) {
134 $index = intval( $part1 );
135 } else {
136 $index = $parser->getContentLanguage()->getNsIndex( str_replace( ' ', '_', $part1 ) );
137 }
138 if ( $index !== false ) {
139 return $parser->getContentLanguage()->getFormattedNsText( $index );
140 } else {
141 return [ 'found' => false ];
142 }
143 }
144
145 public static function nse( $parser, $part1 = '' ) {
146 $ret = self::ns( $parser, $part1 );
147 if ( is_string( $ret ) ) {
148 $ret = wfUrlencode( str_replace( ' ', '_', $ret ) );
149 }
150 return $ret;
151 }
152
153 /**
154 * urlencodes a string according to one of three patterns: (T24474)
155 *
156 * By default (for HTTP "query" strings), spaces are encoded as '+'.
157 * Or to encode a value for the HTTP "path", spaces are encoded as '%20'.
158 * For links to "wiki"s, or similar software, spaces are encoded as '_',
159 *
160 * @param Parser $parser
161 * @param string $s The text to encode.
162 * @param string|null $arg (optional): The type of encoding.
163 * @return string
164 */
165 public static function urlencode( $parser, $s = '', $arg = null ) {
166 static $magicWords = null;
167 if ( is_null( $magicWords ) ) {
168 $magicWords =
169 $parser->getMagicWordFactory()->newArray( [ 'url_path', 'url_query', 'url_wiki' ] );
170 }
171 switch ( $magicWords->matchStartToEnd( $arg ) ) {
172 // Encode as though it's a wiki page, '_' for ' '.
173 case 'url_wiki':
174 $func = 'wfUrlencode';
175 $s = str_replace( ' ', '_', $s );
176 break;
177
178 // Encode for an HTTP Path, '%20' for ' '.
179 case 'url_path':
180 $func = 'rawurlencode';
181 break;
182
183 // Encode for HTTP query, '+' for ' '.
184 case 'url_query':
185 default:
186 $func = 'urlencode';
187 }
188 // See T105242, where the choice to kill markers and various
189 // other options were discussed.
190 return $func( $parser->killMarkers( $s ) );
191 }
192
193 public static function lcfirst( $parser, $s = '' ) {
194 return $parser->getContentLanguage()->lcfirst( $s );
195 }
196
197 public static function ucfirst( $parser, $s = '' ) {
198 return $parser->getContentLanguage()->ucfirst( $s );
199 }
200
201 /**
202 * @param Parser $parser
203 * @param string $s
204 * @return string
205 */
206 public static function lc( $parser, $s = '' ) {
207 return $parser->markerSkipCallback( $s, [ $parser->getContentLanguage(), 'lc' ] );
208 }
209
210 /**
211 * @param Parser $parser
212 * @param string $s
213 * @return string
214 */
215 public static function uc( $parser, $s = '' ) {
216 return $parser->markerSkipCallback( $s, [ $parser->getContentLanguage(), 'uc' ] );
217 }
218
219 public static function localurl( $parser, $s = '', $arg = null ) {
220 return self::urlFunction( 'getLocalURL', $s, $arg );
221 }
222
223 public static function localurle( $parser, $s = '', $arg = null ) {
224 $temp = self::urlFunction( 'getLocalURL', $s, $arg );
225 if ( !is_string( $temp ) ) {
226 return $temp;
227 } else {
228 return htmlspecialchars( $temp );
229 }
230 }
231
232 public static function fullurl( $parser, $s = '', $arg = null ) {
233 return self::urlFunction( 'getFullURL', $s, $arg );
234 }
235
236 public static function fullurle( $parser, $s = '', $arg = null ) {
237 $temp = self::urlFunction( 'getFullURL', $s, $arg );
238 if ( !is_string( $temp ) ) {
239 return $temp;
240 } else {
241 return htmlspecialchars( $temp );
242 }
243 }
244
245 public static function canonicalurl( $parser, $s = '', $arg = null ) {
246 return self::urlFunction( 'getCanonicalURL', $s, $arg );
247 }
248
249 public static function canonicalurle( $parser, $s = '', $arg = null ) {
250 $temp = self::urlFunction( 'getCanonicalURL', $s, $arg );
251 if ( !is_string( $temp ) ) {
252 return $temp;
253 } else {
254 return htmlspecialchars( $temp );
255 }
256 }
257
258 public static function urlFunction( $func, $s = '', $arg = null ) {
259 $title = Title::newFromText( $s );
260 # Due to order of execution of a lot of bits, the values might be encoded
261 # before arriving here; if that's true, then the title can't be created
262 # and the variable will fail. If we can't get a decent title from the first
263 # attempt, url-decode and try for a second.
264 if ( is_null( $title ) ) {
265 $title = Title::newFromURL( urldecode( $s ) );
266 }
267 if ( !is_null( $title ) ) {
268 # Convert NS_MEDIA -> NS_FILE
269 if ( $title->inNamespace( NS_MEDIA ) ) {
270 $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
271 }
272 if ( !is_null( $arg ) ) {
273 $text = $title->$func( $arg );
274 } else {
275 $text = $title->$func();
276 }
277 return $text;
278 } else {
279 return [ 'found' => false ];
280 }
281 }
282
283 /**
284 * @param Parser $parser
285 * @param string $num
286 * @param string|null $arg
287 * @return string
288 */
289 public static function formatnum( $parser, $num = '', $arg = null ) {
290 if ( self::matchAgainstMagicword( $parser->getMagicWordFactory(), 'rawsuffix', $arg ) ) {
291 $func = [ $parser->getFunctionLang(), 'parseFormattedNumber' ];
292 } elseif (
293 self::matchAgainstMagicword( $parser->getMagicWordFactory(), 'nocommafysuffix', $arg )
294 ) {
295 $func = [ $parser->getFunctionLang(), 'formatNumNoSeparators' ];
296 } else {
297 $func = [ $parser->getFunctionLang(), 'formatNum' ];
298 }
299 return $parser->markerSkipCallback( $num, $func );
300 }
301
302 /**
303 * @param Parser $parser
304 * @param string $case
305 * @param string $word
306 * @return string
307 */
308 public static function grammar( $parser, $case = '', $word = '' ) {
309 $word = $parser->killMarkers( $word );
310 return $parser->getFunctionLang()->convertGrammar( $word, $case );
311 }
312
313 /**
314 * @param Parser $parser
315 * @param string $username
316 * @return string
317 */
318 public static function gender( $parser, $username ) {
319 $forms = array_slice( func_get_args(), 2 );
320
321 // Some shortcuts to avoid loading user data unnecessarily
322 if ( count( $forms ) === 0 ) {
323 return '';
324 } elseif ( count( $forms ) === 1 ) {
325 return $forms[0];
326 }
327
328 $username = trim( $username );
329
330 $gender = User::getDefaultOption( 'gender' );
331
332 // allow prefix and normalize (e.g. "&#42;foo" -> "*foo" ).
333 $title = Title::newFromText( $username, NS_USER );
334
335 if ( $title && $title->inNamespace( NS_USER ) ) {
336 $username = $title->getText();
337 }
338
339 // check parameter, or use the ParserOptions if in interface message
340 $user = User::newFromName( $username );
341 $genderCache = MediaWikiServices::getInstance()->getGenderCache();
342 if ( $user ) {
343 $gender = $genderCache->getGenderOf( $user, __METHOD__ );
344 } elseif ( $username === '' && $parser->getOptions()->getInterfaceMessage() ) {
345 $gender = $genderCache->getGenderOf( $parser->getOptions()->getUser(), __METHOD__ );
346 }
347 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
348 return $ret;
349 }
350
351 /**
352 * @param Parser $parser
353 * @param string $text
354 * @return string
355 */
356 public static function plural( $parser, $text = '' ) {
357 $forms = array_slice( func_get_args(), 2 );
358 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
359 settype( $text, ctype_digit( $text ) ? 'int' : 'float' );
360 return $parser->getFunctionLang()->convertPlural( $text, $forms );
361 }
362
363 /**
364 * @param Parser $parser
365 * @param string $text
366 * @return string
367 */
368 public static function bidi( $parser, $text = '' ) {
369 return $parser->getFunctionLang()->embedBidi( $text );
370 }
371
372 /**
373 * Override the title of the page when viewed, provided we've been given a
374 * title which will normalise to the canonical title
375 *
376 * @param Parser $parser Parent parser
377 * @param string $text Desired title text
378 * @param string $uarg
379 * @return string
380 */
381 public static function displaytitle( $parser, $text = '', $uarg = '' ) {
382 global $wgRestrictDisplayTitle;
383
384 static $magicWords = null;
385 if ( is_null( $magicWords ) ) {
386 $magicWords = $parser->getMagicWordFactory()->newArray(
387 [ 'displaytitle_noerror', 'displaytitle_noreplace' ] );
388 }
389 $arg = $magicWords->matchStartToEnd( $uarg );
390
391 // parse a limited subset of wiki markup (just the single quote items)
392 $text = $parser->doQuotes( $text );
393
394 // remove stripped text (e.g. the UNIQ-QINU stuff) that was generated by tag extensions/whatever
395 $text = $parser->killMarkers( $text );
396
397 // list of disallowed tags for DISPLAYTITLE
398 // these will be escaped even though they are allowed in normal wiki text
399 $bad = [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr',
400 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rtc', 'rp', 'br' ];
401
402 // disallow some styles that could be used to bypass $wgRestrictDisplayTitle
403 if ( $wgRestrictDisplayTitle ) {
404 $htmlTagsCallback = function ( &$params ) {
405 $decoded = Sanitizer::decodeTagAttributes( $params );
406
407 if ( isset( $decoded['style'] ) ) {
408 // this is called later anyway, but we need it right now for the regexes below to be safe
409 // calling it twice doesn't hurt
410 $decoded['style'] = Sanitizer::checkCss( $decoded['style'] );
411
412 if ( preg_match( '/(display|user-select|visibility)\s*:/i', $decoded['style'] ) ) {
413 $decoded['style'] = '/* attempt to bypass $wgRestrictDisplayTitle */';
414 }
415 }
416
417 $params = Sanitizer::safeEncodeTagAttributes( $decoded );
418 };
419 } else {
420 $htmlTagsCallback = null;
421 }
422
423 // only requested titles that normalize to the actual title are allowed through
424 // if $wgRestrictDisplayTitle is true (it is by default)
425 // mimic the escaping process that occurs in OutputPage::setPageTitle
426 $text = Sanitizer::normalizeCharReferences( Sanitizer::removeHTMLtags(
427 $text,
428 $htmlTagsCallback,
429 [],
430 [],
431 $bad
432 ) );
433 $title = Title::newFromText( Sanitizer::stripAllTags( $text ) );
434
435 if ( !$wgRestrictDisplayTitle ||
436 ( $title instanceof Title
437 && !$title->hasFragment()
438 && $title->equals( $parser->mTitle ) )
439 ) {
440 $old = $parser->mOutput->getProperty( 'displaytitle' );
441 if ( $old === false || $arg !== 'displaytitle_noreplace' ) {
442 $parser->mOutput->setDisplayTitle( $text );
443 }
444 if ( $old !== false && $old !== $text && !$arg ) {
445 $converter = $parser->getTargetLanguage()->getConverter();
446 return '<span class="error">' .
447 wfMessage( 'duplicate-displaytitle',
448 // Message should be parsed, but these params should only be escaped.
449 $converter->markNoConversion( wfEscapeWikiText( $old ) ),
450 $converter->markNoConversion( wfEscapeWikiText( $text ) )
451 )->inContentLanguage()->text() .
452 '</span>';
453 } else {
454 return '';
455 }
456 } else {
457 $parser->getOutput()->addWarning(
458 wfMessage( 'restricted-displaytitle',
459 // Message should be parsed, but this param should only be escaped.
460 wfEscapeWikiText( $text )
461 )->text()
462 );
463 $parser->addTrackingCategory( 'restricted-displaytitle-ignored' );
464 }
465 }
466
467 /**
468 * Matches the given value against the value of given magic word
469 *
470 * @param MagicWordFactory $magicWordFactory A factory to get the word from, e.g., from
471 * $parser->getMagicWordFactory()
472 * @param string $magicword Magic word key
473 * @param string $value Value to match
474 * @return bool True on successful match
475 */
476 private static function matchAgainstMagicword(
477 MagicWordFactory $magicWordFactory, $magicword, $value
478 ) {
479 $value = trim( strval( $value ) );
480 if ( $value === '' ) {
481 return false;
482 }
483 $mwObject = $magicWordFactory->get( $magicword );
484 return $mwObject->matchStartToEnd( $value );
485 }
486
487 /**
488 * Formats a number according to a language.
489 *
490 * @param int|float $num
491 * @param string $raw
492 * @param Language|StubUserLang $language
493 * @param MagicWordFactory|null $magicWordFactory To evaluate $raw
494 * @return string
495 */
496 public static function formatRaw(
497 $num, $raw, $language, MagicWordFactory $magicWordFactory = null
498 ) {
499 if ( $raw !== null && !$magicWordFactory ) {
500 $magicWordFactory = MediaWikiServices::getInstance()->getMagicWordFactory();
501 }
502 if (
503 $raw !== null && self::matchAgainstMagicword( $magicWordFactory, 'rawsuffix', $raw )
504 ) {
505 return $num;
506 } else {
507 return $language->formatNum( $num );
508 }
509 }
510
511 public static function numberofpages( $parser, $raw = null ) {
512 return self::formatRaw( SiteStats::pages(), $raw, $parser->getFunctionLang() );
513 }
514
515 public static function numberofusers( $parser, $raw = null ) {
516 return self::formatRaw( SiteStats::users(), $raw, $parser->getFunctionLang() );
517 }
518 public static function numberofactiveusers( $parser, $raw = null ) {
519 return self::formatRaw( SiteStats::activeUsers(), $raw, $parser->getFunctionLang() );
520 }
521
522 public static function numberofarticles( $parser, $raw = null ) {
523 return self::formatRaw( SiteStats::articles(), $raw, $parser->getFunctionLang() );
524 }
525
526 public static function numberoffiles( $parser, $raw = null ) {
527 return self::formatRaw( SiteStats::images(), $raw, $parser->getFunctionLang() );
528 }
529
530 public static function numberofadmins( $parser, $raw = null ) {
531 return self::formatRaw(
532 SiteStats::numberingroup( 'sysop' ),
533 $raw,
534 $parser->getFunctionLang()
535 );
536 }
537
538 public static function numberofedits( $parser, $raw = null ) {
539 return self::formatRaw( SiteStats::edits(), $raw, $parser->getFunctionLang() );
540 }
541
542 public static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
543 return self::formatRaw(
544 SiteStats::pagesInNs( intval( $namespace ) ),
545 $raw,
546 $parser->getFunctionLang()
547 );
548 }
549 public static function numberingroup( $parser, $name = '', $raw = null ) {
550 return self::formatRaw(
551 SiteStats::numberingroup( strtolower( $name ) ),
552 $raw,
553 $parser->getFunctionLang()
554 );
555 }
556
557 /**
558 * Given a title, return the namespace name that would be given by the
559 * corresponding magic word
560 * Note: function name changed to "mwnamespace" rather than "namespace"
561 * to not break PHP 5.3
562 * @param Parser $parser
563 * @param string|null $title
564 * @return mixed|string
565 */
566 public static function mwnamespace( $parser, $title = null ) {
567 $t = Title::newFromText( $title );
568 if ( is_null( $t ) ) {
569 return '';
570 }
571 return str_replace( '_', ' ', $t->getNsText() );
572 }
573 public static function namespacee( $parser, $title = null ) {
574 $t = Title::newFromText( $title );
575 if ( is_null( $t ) ) {
576 return '';
577 }
578 return wfUrlencode( $t->getNsText() );
579 }
580 public static function namespacenumber( $parser, $title = null ) {
581 $t = Title::newFromText( $title );
582 if ( is_null( $t ) ) {
583 return '';
584 }
585 return $t->getNamespace();
586 }
587 public static function talkspace( $parser, $title = null ) {
588 $t = Title::newFromText( $title );
589 if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
590 return '';
591 }
592 return str_replace( '_', ' ', $t->getTalkNsText() );
593 }
594 public static function talkspacee( $parser, $title = null ) {
595 $t = Title::newFromText( $title );
596 if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
597 return '';
598 }
599 return wfUrlencode( $t->getTalkNsText() );
600 }
601 public static function subjectspace( $parser, $title = null ) {
602 $t = Title::newFromText( $title );
603 if ( is_null( $t ) ) {
604 return '';
605 }
606 return str_replace( '_', ' ', $t->getSubjectNsText() );
607 }
608 public static function subjectspacee( $parser, $title = null ) {
609 $t = Title::newFromText( $title );
610 if ( is_null( $t ) ) {
611 return '';
612 }
613 return wfUrlencode( $t->getSubjectNsText() );
614 }
615
616 /**
617 * Functions to get and normalize pagenames, corresponding to the magic words
618 * of the same names
619 * @param Parser $parser
620 * @param string|null $title
621 * @return string
622 */
623 public static function pagename( $parser, $title = null ) {
624 $t = Title::newFromText( $title );
625 if ( is_null( $t ) ) {
626 return '';
627 }
628 return wfEscapeWikiText( $t->getText() );
629 }
630 public static function pagenamee( $parser, $title = null ) {
631 $t = Title::newFromText( $title );
632 if ( is_null( $t ) ) {
633 return '';
634 }
635 return wfEscapeWikiText( $t->getPartialURL() );
636 }
637 public static function fullpagename( $parser, $title = null ) {
638 $t = Title::newFromText( $title );
639 if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
640 return '';
641 }
642 return wfEscapeWikiText( $t->getPrefixedText() );
643 }
644 public static function fullpagenamee( $parser, $title = null ) {
645 $t = Title::newFromText( $title );
646 if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
647 return '';
648 }
649 return wfEscapeWikiText( $t->getPrefixedURL() );
650 }
651 public static function subpagename( $parser, $title = null ) {
652 $t = Title::newFromText( $title );
653 if ( is_null( $t ) ) {
654 return '';
655 }
656 return wfEscapeWikiText( $t->getSubpageText() );
657 }
658 public static function subpagenamee( $parser, $title = null ) {
659 $t = Title::newFromText( $title );
660 if ( is_null( $t ) ) {
661 return '';
662 }
663 return wfEscapeWikiText( $t->getSubpageUrlForm() );
664 }
665 public static function rootpagename( $parser, $title = null ) {
666 $t = Title::newFromText( $title );
667 if ( is_null( $t ) ) {
668 return '';
669 }
670 return wfEscapeWikiText( $t->getRootText() );
671 }
672 public static function rootpagenamee( $parser, $title = null ) {
673 $t = Title::newFromText( $title );
674 if ( is_null( $t ) ) {
675 return '';
676 }
677 return wfEscapeWikiText( wfUrlencode( str_replace( ' ', '_', $t->getRootText() ) ) );
678 }
679 public static function basepagename( $parser, $title = null ) {
680 $t = Title::newFromText( $title );
681 if ( is_null( $t ) ) {
682 return '';
683 }
684 return wfEscapeWikiText( $t->getBaseText() );
685 }
686 public static function basepagenamee( $parser, $title = null ) {
687 $t = Title::newFromText( $title );
688 if ( is_null( $t ) ) {
689 return '';
690 }
691 return wfEscapeWikiText( wfUrlencode( str_replace( ' ', '_', $t->getBaseText() ) ) );
692 }
693 public static function talkpagename( $parser, $title = null ) {
694 $t = Title::newFromText( $title );
695 if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
696 return '';
697 }
698 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
699 }
700 public static function talkpagenamee( $parser, $title = null ) {
701 $t = Title::newFromText( $title );
702 if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
703 return '';
704 }
705 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedURL() );
706 }
707 public static function subjectpagename( $parser, $title = null ) {
708 $t = Title::newFromText( $title );
709 if ( is_null( $t ) ) {
710 return '';
711 }
712 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
713 }
714 public static function subjectpagenamee( $parser, $title = null ) {
715 $t = Title::newFromText( $title );
716 if ( is_null( $t ) ) {
717 return '';
718 }
719 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedURL() );
720 }
721
722 /**
723 * Return the number of pages, files or subcats in the given category,
724 * or 0 if it's nonexistent. This is an expensive parser function and
725 * can't be called too many times per page.
726 * @param Parser $parser
727 * @param string $name
728 * @param string|null $arg1
729 * @param string|null $arg2
730 * @return string
731 */
732 public static function pagesincategory( $parser, $name = '', $arg1 = null, $arg2 = null ) {
733 static $magicWords = null;
734 if ( is_null( $magicWords ) ) {
735 $magicWords = $parser->getMagicWordFactory()->newArray( [
736 'pagesincategory_all',
737 'pagesincategory_pages',
738 'pagesincategory_subcats',
739 'pagesincategory_files'
740 ] );
741 }
742 static $cache = [];
743
744 // split the given option to its variable
745 if ( self::matchAgainstMagicword( $parser->getMagicWordFactory(), 'rawsuffix', $arg1 ) ) {
746 // {{pagesincategory:|raw[|type]}}
747 $raw = $arg1;
748 $type = $magicWords->matchStartToEnd( $arg2 );
749 } else {
750 // {{pagesincategory:[|type[|raw]]}}
751 $type = $magicWords->matchStartToEnd( $arg1 );
752 $raw = $arg2;
753 }
754 if ( !$type ) { // backward compatibility
755 $type = 'pagesincategory_all';
756 }
757
758 $title = Title::makeTitleSafe( NS_CATEGORY, $name );
759 if ( !$title ) { # invalid title
760 return self::formatRaw( 0, $raw, $parser->getFunctionLang() );
761 }
762 $parser->getContentLanguage()->findVariantLink( $name, $title, true );
763
764 // Normalize name for cache
765 $name = $title->getDBkey();
766
767 if ( !isset( $cache[$name] ) ) {
768 $category = Category::newFromTitle( $title );
769
770 $allCount = $subcatCount = $fileCount = $pagesCount = 0;
771 if ( $parser->incrementExpensiveFunctionCount() ) {
772 // $allCount is the total number of cat members,
773 // not the count of how many members are normal pages.
774 $allCount = (int)$category->getPageCount();
775 $subcatCount = (int)$category->getSubcatCount();
776 $fileCount = (int)$category->getFileCount();
777 $pagesCount = $allCount - $subcatCount - $fileCount;
778 }
779 $cache[$name]['pagesincategory_all'] = $allCount;
780 $cache[$name]['pagesincategory_pages'] = $pagesCount;
781 $cache[$name]['pagesincategory_subcats'] = $subcatCount;
782 $cache[$name]['pagesincategory_files'] = $fileCount;
783 }
784
785 $count = $cache[$name][$type];
786 return self::formatRaw( $count, $raw, $parser->getFunctionLang() );
787 }
788
789 /**
790 * Return the size of the given page, or 0 if it's nonexistent. This is an
791 * expensive parser function and can't be called too many times per page.
792 *
793 * @param Parser $parser
794 * @param string $page Name of page to check (Default: empty string)
795 * @param string|null $raw Should number be human readable with commas or just number
796 * @return string
797 */
798 public static function pagesize( $parser, $page = '', $raw = null ) {
799 $title = Title::newFromText( $page );
800
801 if ( !is_object( $title ) ) {
802 return self::formatRaw( 0, $raw, $parser->getFunctionLang() );
803 }
804
805 // fetch revision from cache/database and return the value
806 $rev = self::getCachedRevisionObject( $parser, $title );
807 $length = $rev ? $rev->getSize() : 0;
808 if ( $length === null ) {
809 // We've had bugs where rev_len was not being recorded for empty pages, see T135414
810 $length = 0;
811 }
812 return self::formatRaw( $length, $raw, $parser->getFunctionLang() );
813 }
814
815 /**
816 * Returns the requested protection level for the current page. This
817 * is an expensive parser function and can't be called too many times
818 * per page, unless the protection levels/expiries for the given title
819 * have already been retrieved
820 *
821 * @param Parser $parser
822 * @param string $type
823 * @param string $title
824 *
825 * @return string
826 */
827 public static function protectionlevel( $parser, $type = '', $title = '' ) {
828 $titleObject = Title::newFromText( $title );
829 if ( !( $titleObject instanceof Title ) ) {
830 $titleObject = $parser->mTitle;
831 }
832 if ( $titleObject->areRestrictionsLoaded() || $parser->incrementExpensiveFunctionCount() ) {
833 $restrictions = $titleObject->getRestrictions( strtolower( $type ) );
834 # Title::getRestrictions returns an array, its possible it may have
835 # multiple values in the future
836 return implode( ',', $restrictions );
837 }
838 return '';
839 }
840
841 /**
842 * Returns the requested protection expiry for the current page. This
843 * is an expensive parser function and can't be called too many times
844 * per page, unless the protection levels/expiries for the given title
845 * have already been retrieved
846 *
847 * @param Parser $parser
848 * @param string $type
849 * @param string $title
850 *
851 * @return string
852 */
853 public static function protectionexpiry( $parser, $type = '', $title = '' ) {
854 $titleObject = Title::newFromText( $title );
855 if ( !( $titleObject instanceof Title ) ) {
856 $titleObject = $parser->mTitle;
857 }
858 if ( $titleObject->areRestrictionsLoaded() || $parser->incrementExpensiveFunctionCount() ) {
859 $expiry = $titleObject->getRestrictionExpiry( strtolower( $type ) );
860 // getRestrictionExpiry() returns false on invalid type; trying to
861 // match protectionlevel() function that returns empty string instead
862 if ( $expiry === false ) {
863 $expiry = '';
864 }
865 return $expiry;
866 }
867 return '';
868 }
869
870 /**
871 * Gives language names.
872 * @param Parser $parser
873 * @param string $code Language code (of which to get name)
874 * @param string $inLanguage Language code (in which to get name)
875 * @return string
876 */
877 public static function language( $parser, $code = '', $inLanguage = '' ) {
878 $code = strtolower( $code );
879 $inLanguage = strtolower( $inLanguage );
880 $lang = Language::fetchLanguageName( $code, $inLanguage );
881 return $lang !== '' ? $lang : LanguageCode::bcp47( $code );
882 }
883
884 /**
885 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
886 * @param Parser $parser
887 * @param string $string
888 * @param string $length
889 * @param string $padding
890 * @param int $direction
891 * @return string
892 */
893 public static function pad(
894 $parser, $string, $length, $padding = '0', $direction = STR_PAD_RIGHT
895 ) {
896 $padding = $parser->killMarkers( $padding );
897 $lengthOfPadding = mb_strlen( $padding );
898 if ( $lengthOfPadding == 0 ) {
899 return $string;
900 }
901
902 # The remaining length to add counts down to 0 as padding is added
903 $length = min( (int)$length, 500 ) - mb_strlen( $string );
904 if ( $length <= 0 ) {
905 // Nothing to add
906 return $string;
907 }
908
909 # $finalPadding is just $padding repeated enough times so that
910 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
911 $finalPadding = '';
912 while ( $length > 0 ) {
913 # If $length < $lengthofPadding, truncate $padding so we get the
914 # exact length desired.
915 $finalPadding .= mb_substr( $padding, 0, $length );
916 $length -= $lengthOfPadding;
917 }
918
919 if ( $direction == STR_PAD_LEFT ) {
920 return $finalPadding . $string;
921 } else {
922 return $string . $finalPadding;
923 }
924 }
925
926 public static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
927 return self::pad( $parser, $string, $length, $padding, STR_PAD_LEFT );
928 }
929
930 public static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
931 return self::pad( $parser, $string, $length, $padding );
932 }
933
934 /**
935 * @param Parser $parser
936 * @param string $text
937 * @return string
938 */
939 public static function anchorencode( $parser, $text ) {
940 $text = $parser->killMarkers( $text );
941 $section = (string)substr( $parser->guessSectionNameFromWikiText( $text ), 1 );
942 return Sanitizer::safeEncodeAttribute( $section );
943 }
944
945 public static function special( $parser, $text ) {
946 list( $page, $subpage ) = MediaWikiServices::getInstance()->getSpecialPageFactory()->
947 resolveAlias( $text );
948 if ( $page ) {
949 $title = SpecialPage::getTitleFor( $page, $subpage );
950 return $title->getPrefixedText();
951 } else {
952 // unknown special page, just use the given text as its title, if at all possible
953 $title = Title::makeTitleSafe( NS_SPECIAL, $text );
954 return $title ? $title->getPrefixedText() : self::special( $parser, 'Badtitle' );
955 }
956 }
957
958 public static function speciale( $parser, $text ) {
959 return wfUrlencode( str_replace( ' ', '_', self::special( $parser, $text ) ) );
960 }
961
962 /**
963 * @param Parser $parser
964 * @param string $text The sortkey to use
965 * @param string $uarg Either "noreplace" or "noerror" (in en)
966 * both suppress errors, and noreplace does nothing if
967 * a default sortkey already exists.
968 * @return string
969 */
970 public static function defaultsort( $parser, $text, $uarg = '' ) {
971 static $magicWords = null;
972 if ( is_null( $magicWords ) ) {
973 $magicWords = $parser->getMagicWordFactory()->newArray(
974 [ 'defaultsort_noerror', 'defaultsort_noreplace' ] );
975 }
976 $arg = $magicWords->matchStartToEnd( $uarg );
977
978 $text = trim( $text );
979 if ( strlen( $text ) == 0 ) {
980 return '';
981 }
982 $old = $parser->getCustomDefaultSort();
983 if ( $old === false || $arg !== 'defaultsort_noreplace' ) {
984 $parser->setDefaultSort( $text );
985 }
986
987 if ( $old === false || $old == $text || $arg ) {
988 return '';
989 } else {
990 $converter = $parser->getTargetLanguage()->getConverter();
991 return '<span class="error">' .
992 wfMessage( 'duplicate-defaultsort',
993 // Message should be parsed, but these params should only be escaped.
994 $converter->markNoConversion( wfEscapeWikiText( $old ) ),
995 $converter->markNoConversion( wfEscapeWikiText( $text ) )
996 )->inContentLanguage()->text() .
997 '</span>';
998 }
999 }
1000
1001 /**
1002 * Usage {{filepath|300}}, {{filepath|nowiki}}, {{filepath|nowiki|300}}
1003 * or {{filepath|300|nowiki}} or {{filepath|300px}}, {{filepath|200x300px}},
1004 * {{filepath|nowiki|200x300px}}, {{filepath|200x300px|nowiki}}.
1005 *
1006 * @param Parser $parser
1007 * @param string $name
1008 * @param string $argA
1009 * @param string $argB
1010 * @return array|string
1011 */
1012 public static function filepath( $parser, $name = '', $argA = '', $argB = '' ) {
1013 $file = wfFindFile( $name );
1014
1015 if ( $argA == 'nowiki' ) {
1016 // {{filepath: | option [| size] }}
1017 $isNowiki = true;
1018 $parsedWidthParam = Parser::parseWidthParam( $argB );
1019 } else {
1020 // {{filepath: [| size [|option]] }}
1021 $parsedWidthParam = Parser::parseWidthParam( $argA );
1022 $isNowiki = ( $argB == 'nowiki' );
1023 }
1024
1025 if ( $file ) {
1026 $url = $file->getFullUrl();
1027
1028 // If a size is requested...
1029 if ( count( $parsedWidthParam ) ) {
1030 $mto = $file->transform( $parsedWidthParam );
1031 // ... and we can
1032 if ( $mto && !$mto->isError() ) {
1033 // ... change the URL to point to a thumbnail.
1034 $url = wfExpandUrl( $mto->getUrl(), PROTO_RELATIVE );
1035 }
1036 }
1037 if ( $isNowiki ) {
1038 return [ $url, 'nowiki' => true ];
1039 }
1040 return $url;
1041 } else {
1042 return '';
1043 }
1044 }
1045
1046 /**
1047 * Parser function to extension tag adaptor
1048 * @param Parser $parser
1049 * @param PPFrame $frame
1050 * @param PPNode[] $args
1051 * @return string
1052 */
1053 public static function tagObj( $parser, $frame, $args ) {
1054 if ( !count( $args ) ) {
1055 return '';
1056 }
1057 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
1058
1059 if ( count( $args ) ) {
1060 $inner = $frame->expand( array_shift( $args ) );
1061 } else {
1062 $inner = null;
1063 }
1064
1065 $attributes = [];
1066 foreach ( $args as $arg ) {
1067 $bits = $arg->splitArg();
1068 if ( strval( $bits['index'] ) === '' ) {
1069 $name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
1070 $value = trim( $frame->expand( $bits['value'] ) );
1071 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
1072 $value = $m[1] ?? '';
1073 }
1074 $attributes[$name] = $value;
1075 }
1076 }
1077
1078 $stripList = $parser->getStripList();
1079 if ( !in_array( $tagName, $stripList ) ) {
1080 // we can't handle this tag (at least not now), so just re-emit it as an ordinary tag
1081 $attrText = '';
1082 foreach ( $attributes as $name => $value ) {
1083 $attrText .= ' ' . htmlspecialchars( $name ) . '="' . htmlspecialchars( $value ) . '"';
1084 }
1085 if ( $inner === null ) {
1086 return "<$tagName$attrText/>";
1087 }
1088 return "<$tagName$attrText>$inner</$tagName>";
1089 }
1090
1091 $params = [
1092 'name' => $tagName,
1093 'inner' => $inner,
1094 'attributes' => $attributes,
1095 'close' => "</$tagName>",
1096 ];
1097 return $parser->extensionSubstitution( $params, $frame );
1098 }
1099
1100 /**
1101 * Fetched the current revision of the given title and return this.
1102 * Will increment the expensive function count and
1103 * add a template link to get the value refreshed on changes.
1104 * For a given title, which is equal to the current parser title,
1105 * the revision object from the parser is used, when that is the current one
1106 *
1107 * @param Parser $parser
1108 * @param Title $title
1109 * @return Revision
1110 * @since 1.23
1111 */
1112 private static function getCachedRevisionObject( $parser, $title = null ) {
1113 if ( is_null( $title ) ) {
1114 return null;
1115 }
1116
1117 // Use the revision from the parser itself, when param is the current page
1118 // and the revision is the current one
1119 if ( $title->equals( $parser->getTitle() ) ) {
1120 $parserRev = $parser->getRevisionObject();
1121 if ( $parserRev && $parserRev->isCurrent() ) {
1122 // force reparse after edit with vary-revision flag
1123 $parser->getOutput()->setFlag( 'vary-revision' );
1124 wfDebug( __METHOD__ . ": use current revision from parser, setting vary-revision...\n" );
1125 return $parserRev;
1126 }
1127 }
1128
1129 // Normalize name for cache
1130 $page = $title->getPrefixedDBkey();
1131
1132 if ( !( $parser->currentRevisionCache && $parser->currentRevisionCache->has( $page ) )
1133 && !$parser->incrementExpensiveFunctionCount() ) {
1134 return null;
1135 }
1136 $rev = $parser->fetchCurrentRevisionOfTitle( $title );
1137 $pageID = $rev ? $rev->getPage() : 0;
1138 $revID = $rev ? $rev->getId() : 0;
1139
1140 // Register dependency in templatelinks
1141 $parser->getOutput()->addTemplate( $title, $pageID, $revID );
1142
1143 return $rev;
1144 }
1145
1146 /**
1147 * Get the pageid of a specified page
1148 * @param Parser $parser
1149 * @param string|null $title Title to get the pageid from
1150 * @return int|null|string
1151 * @since 1.23
1152 */
1153 public static function pageid( $parser, $title = null ) {
1154 $t = Title::newFromText( $title );
1155 if ( is_null( $t ) ) {
1156 return '';
1157 }
1158 // Use title from parser to have correct pageid after edit
1159 if ( $t->equals( $parser->getTitle() ) ) {
1160 $t = $parser->getTitle();
1161 return $t->getArticleID();
1162 }
1163
1164 // These can't have ids
1165 if ( !$t->canExist() || $t->isExternal() ) {
1166 return 0;
1167 }
1168
1169 // Check the link cache, maybe something already looked it up.
1170 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
1171 $pdbk = $t->getPrefixedDBkey();
1172 $id = $linkCache->getGoodLinkID( $pdbk );
1173 if ( $id != 0 ) {
1174 $parser->mOutput->addLink( $t, $id );
1175 return $id;
1176 }
1177 if ( $linkCache->isBadLink( $pdbk ) ) {
1178 $parser->mOutput->addLink( $t, 0 );
1179 return $id;
1180 }
1181
1182 // We need to load it from the DB, so mark expensive
1183 if ( $parser->incrementExpensiveFunctionCount() ) {
1184 $id = $t->getArticleID();
1185 $parser->mOutput->addLink( $t, $id );
1186 return $id;
1187 }
1188 return null;
1189 }
1190
1191 /**
1192 * Get the id from the last revision of a specified page.
1193 * @param Parser $parser
1194 * @param string|null $title Title to get the id from
1195 * @return int|null|string
1196 * @since 1.23
1197 */
1198 public static function revisionid( $parser, $title = null ) {
1199 $t = Title::newFromText( $title );
1200 if ( is_null( $t ) ) {
1201 return '';
1202 }
1203 // fetch revision from cache/database and return the value
1204 $rev = self::getCachedRevisionObject( $parser, $t );
1205 return $rev ? $rev->getId() : '';
1206 }
1207
1208 /**
1209 * Get the day from the last revision of a specified page.
1210 * @param Parser $parser
1211 * @param string|null $title Title to get the day from
1212 * @return string
1213 * @since 1.23
1214 */
1215 public static function revisionday( $parser, $title = null ) {
1216 $t = Title::newFromText( $title );
1217 if ( is_null( $t ) ) {
1218 return '';
1219 }
1220 // fetch revision from cache/database and return the value
1221 $rev = self::getCachedRevisionObject( $parser, $t );
1222 return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'j' ) : '';
1223 }
1224
1225 /**
1226 * Get the day with leading zeros from the last revision of a specified page.
1227 * @param Parser $parser
1228 * @param string|null $title Title to get the day from
1229 * @return string
1230 * @since 1.23
1231 */
1232 public static function revisionday2( $parser, $title = null ) {
1233 $t = Title::newFromText( $title );
1234 if ( is_null( $t ) ) {
1235 return '';
1236 }
1237 // fetch revision from cache/database and return the value
1238 $rev = self::getCachedRevisionObject( $parser, $t );
1239 return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'd' ) : '';
1240 }
1241
1242 /**
1243 * Get the month with leading zeros from the last revision of a specified page.
1244 * @param Parser $parser
1245 * @param string|null $title Title to get the month from
1246 * @return string
1247 * @since 1.23
1248 */
1249 public static function revisionmonth( $parser, $title = null ) {
1250 $t = Title::newFromText( $title );
1251 if ( is_null( $t ) ) {
1252 return '';
1253 }
1254 // fetch revision from cache/database and return the value
1255 $rev = self::getCachedRevisionObject( $parser, $t );
1256 return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'm' ) : '';
1257 }
1258
1259 /**
1260 * Get the month from the last revision of a specified page.
1261 * @param Parser $parser
1262 * @param string|null $title Title to get the month from
1263 * @return string
1264 * @since 1.23
1265 */
1266 public static function revisionmonth1( $parser, $title = null ) {
1267 $t = Title::newFromText( $title );
1268 if ( is_null( $t ) ) {
1269 return '';
1270 }
1271 // fetch revision from cache/database and return the value
1272 $rev = self::getCachedRevisionObject( $parser, $t );
1273 return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'n' ) : '';
1274 }
1275
1276 /**
1277 * Get the year from the last revision of a specified page.
1278 * @param Parser $parser
1279 * @param string|null $title Title to get the year from
1280 * @return string
1281 * @since 1.23
1282 */
1283 public static function revisionyear( $parser, $title = null ) {
1284 $t = Title::newFromText( $title );
1285 if ( is_null( $t ) ) {
1286 return '';
1287 }
1288 // fetch revision from cache/database and return the value
1289 $rev = self::getCachedRevisionObject( $parser, $t );
1290 return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'Y' ) : '';
1291 }
1292
1293 /**
1294 * Get the timestamp from the last revision of a specified page.
1295 * @param Parser $parser
1296 * @param string|null $title Title to get the timestamp from
1297 * @return string
1298 * @since 1.23
1299 */
1300 public static function revisiontimestamp( $parser, $title = null ) {
1301 $t = Title::newFromText( $title );
1302 if ( is_null( $t ) ) {
1303 return '';
1304 }
1305 // fetch revision from cache/database and return the value
1306 $rev = self::getCachedRevisionObject( $parser, $t );
1307 return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'YmdHis' ) : '';
1308 }
1309
1310 /**
1311 * Get the user from the last revision of a specified page.
1312 * @param Parser $parser
1313 * @param string|null $title Title to get the user from
1314 * @return string
1315 * @since 1.23
1316 */
1317 public static function revisionuser( $parser, $title = null ) {
1318 $t = Title::newFromText( $title );
1319 if ( is_null( $t ) ) {
1320 return '';
1321 }
1322 // fetch revision from cache/database and return the value
1323 $rev = self::getCachedRevisionObject( $parser, $t );
1324 return $rev ? $rev->getUserText() : '';
1325 }
1326
1327 /**
1328 * Returns the sources of any cascading protection acting on a specified page.
1329 * Pages will not return their own title unless they transclude themselves.
1330 * This is an expensive parser function and can't be called too many times per page,
1331 * unless cascading protection sources for the page have already been loaded.
1332 *
1333 * @param Parser $parser
1334 * @param string $title
1335 *
1336 * @return string
1337 * @since 1.23
1338 */
1339 public static function cascadingsources( $parser, $title = '' ) {
1340 $titleObject = Title::newFromText( $title );
1341 if ( !( $titleObject instanceof Title ) ) {
1342 $titleObject = $parser->mTitle;
1343 }
1344 if ( $titleObject->areCascadeProtectionSourcesLoaded()
1345 || $parser->incrementExpensiveFunctionCount()
1346 ) {
1347 $names = [];
1348 $sources = $titleObject->getCascadeProtectionSources();
1349 foreach ( $sources[0] as $sourceTitle ) {
1350 $names[] = $sourceTitle->getPrefixedText();
1351 }
1352 return implode( '|', $names );
1353 }
1354 return '';
1355 }
1356
1357 }