Merge "ApiComparePages: Don't error with no prev/next rev"
[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 // default
331 $gender = User::getDefaultOption( 'gender' );
332
333 // allow prefix and normalize (e.g. "&#42;foo" -> "*foo" ).
334 $title = Title::newFromText( $username, NS_USER );
335
336 if ( $title && $title->inNamespace( NS_USER ) ) {
337 $username = $title->getText();
338 }
339
340 // check parameter, or use the ParserOptions if in interface message
341 $user = User::newFromName( $username );
342 $genderCache = MediaWikiServices::getInstance()->getGenderCache();
343 if ( $user ) {
344 $gender = $genderCache->getGenderOf( $user, __METHOD__ );
345 } elseif ( $username === '' && $parser->getOptions()->getInterfaceMessage() ) {
346 $gender = $genderCache->getGenderOf( $parser->getOptions()->getUser(), __METHOD__ );
347 }
348 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
349 return $ret;
350 }
351
352 /**
353 * @param Parser $parser
354 * @param string $text
355 * @return string
356 */
357 public static function plural( $parser, $text = '' ) {
358 $forms = array_slice( func_get_args(), 2 );
359 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
360 settype( $text, ctype_digit( $text ) ? 'int' : 'float' );
361 return $parser->getFunctionLang()->convertPlural( $text, $forms );
362 }
363
364 /**
365 * @param Parser $parser
366 * @param string $text
367 * @return string
368 */
369 public static function bidi( $parser, $text = '' ) {
370 return $parser->getFunctionLang()->embedBidi( $text );
371 }
372
373 /**
374 * Override the title of the page when viewed, provided we've been given a
375 * title which will normalise to the canonical title
376 *
377 * @param Parser $parser Parent parser
378 * @param string $text Desired title text
379 * @param string $uarg
380 * @return string
381 */
382 public static function displaytitle( $parser, $text = '', $uarg = '' ) {
383 global $wgRestrictDisplayTitle;
384
385 static $magicWords = null;
386 if ( is_null( $magicWords ) ) {
387 $magicWords = $parser->getMagicWordFactory()->newArray(
388 [ 'displaytitle_noerror', 'displaytitle_noreplace' ] );
389 }
390 $arg = $magicWords->matchStartToEnd( $uarg );
391
392 // parse a limited subset of wiki markup (just the single quote items)
393 $text = $parser->doQuotes( $text );
394
395 // remove stripped text (e.g. the UNIQ-QINU stuff) that was generated by tag extensions/whatever
396 $text = $parser->killMarkers( $text );
397
398 // list of disallowed tags for DISPLAYTITLE
399 // these will be escaped even though they are allowed in normal wiki text
400 $bad = [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr',
401 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rtc', 'rp', 'br' ];
402
403 // disallow some styles that could be used to bypass $wgRestrictDisplayTitle
404 if ( $wgRestrictDisplayTitle ) {
405 $htmlTagsCallback = function ( &$params ) {
406 $decoded = Sanitizer::decodeTagAttributes( $params );
407
408 if ( isset( $decoded['style'] ) ) {
409 // this is called later anyway, but we need it right now for the regexes below to be safe
410 // calling it twice doesn't hurt
411 $decoded['style'] = Sanitizer::checkCss( $decoded['style'] );
412
413 if ( preg_match( '/(display|user-select|visibility)\s*:/i', $decoded['style'] ) ) {
414 $decoded['style'] = '/* attempt to bypass $wgRestrictDisplayTitle */';
415 }
416 }
417
418 $params = Sanitizer::safeEncodeTagAttributes( $decoded );
419 };
420 } else {
421 $htmlTagsCallback = null;
422 }
423
424 // only requested titles that normalize to the actual title are allowed through
425 // if $wgRestrictDisplayTitle is true (it is by default)
426 // mimic the escaping process that occurs in OutputPage::setPageTitle
427 $text = Sanitizer::normalizeCharReferences( Sanitizer::removeHTMLtags(
428 $text,
429 $htmlTagsCallback,
430 [],
431 [],
432 $bad
433 ) );
434 $title = Title::newFromText( Sanitizer::stripAllTags( $text ) );
435
436 if ( !$wgRestrictDisplayTitle ||
437 ( $title instanceof Title
438 && !$title->hasFragment()
439 && $title->equals( $parser->mTitle ) )
440 ) {
441 $old = $parser->mOutput->getProperty( 'displaytitle' );
442 if ( $old === false || $arg !== 'displaytitle_noreplace' ) {
443 $parser->mOutput->setDisplayTitle( $text );
444 }
445 if ( $old !== false && $old !== $text && !$arg ) {
446 $converter = $parser->getTargetLanguage()->getConverter();
447 return '<span class="error">' .
448 wfMessage( 'duplicate-displaytitle',
449 // Message should be parsed, but these params should only be escaped.
450 $converter->markNoConversion( wfEscapeWikiText( $old ) ),
451 $converter->markNoConversion( wfEscapeWikiText( $text ) )
452 )->inContentLanguage()->text() .
453 '</span>';
454 } else {
455 return '';
456 }
457 } else {
458 $parser->getOutput()->addWarning(
459 wfMessage( 'restricted-displaytitle',
460 // Message should be parsed, but this param should only be escaped.
461 wfEscapeWikiText( $text )
462 )->text()
463 );
464 $parser->addTrackingCategory( 'restricted-displaytitle-ignored' );
465 }
466 }
467
468 /**
469 * Matches the given value against the value of given magic word
470 *
471 * @param MagicWordFactory $magicWordFactory A factory to get the word from, e.g., from
472 * $parser->getMagicWordFactory()
473 * @param string $magicword Magic word key
474 * @param string $value Value to match
475 * @return bool True on successful match
476 */
477 private static function matchAgainstMagicword(
478 MagicWordFactory $magicWordFactory, $magicword, $value
479 ) {
480 $value = trim( strval( $value ) );
481 if ( $value === '' ) {
482 return false;
483 }
484 $mwObject = $magicWordFactory->get( $magicword );
485 return $mwObject->matchStartToEnd( $value );
486 }
487
488 /**
489 * Formats a number according to a language.
490 *
491 * @param int|float $num
492 * @param string $raw
493 * @param Language|StubUserLang $language
494 * @param MagicWordFactory|null $magicWordFactory To evaluate $raw
495 * @return string
496 */
497 public static function formatRaw(
498 $num, $raw, $language, MagicWordFactory $magicWordFactory = null
499 ) {
500 if ( $raw !== null && !$magicWordFactory ) {
501 $magicWordFactory = MediaWikiServices::getInstance()->getMagicWordFactory();
502 }
503 if (
504 $raw !== null && self::matchAgainstMagicword( $magicWordFactory, 'rawsuffix', $raw )
505 ) {
506 return $num;
507 } else {
508 return $language->formatNum( $num );
509 }
510 }
511
512 public static function numberofpages( $parser, $raw = null ) {
513 return self::formatRaw( SiteStats::pages(), $raw, $parser->getFunctionLang() );
514 }
515
516 public static function numberofusers( $parser, $raw = null ) {
517 return self::formatRaw( SiteStats::users(), $raw, $parser->getFunctionLang() );
518 }
519 public static function numberofactiveusers( $parser, $raw = null ) {
520 return self::formatRaw( SiteStats::activeUsers(), $raw, $parser->getFunctionLang() );
521 }
522
523 public static function numberofarticles( $parser, $raw = null ) {
524 return self::formatRaw( SiteStats::articles(), $raw, $parser->getFunctionLang() );
525 }
526
527 public static function numberoffiles( $parser, $raw = null ) {
528 return self::formatRaw( SiteStats::images(), $raw, $parser->getFunctionLang() );
529 }
530
531 public static function numberofadmins( $parser, $raw = null ) {
532 return self::formatRaw(
533 SiteStats::numberingroup( 'sysop' ),
534 $raw,
535 $parser->getFunctionLang()
536 );
537 }
538
539 public static function numberofedits( $parser, $raw = null ) {
540 return self::formatRaw( SiteStats::edits(), $raw, $parser->getFunctionLang() );
541 }
542
543 public static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
544 return self::formatRaw(
545 SiteStats::pagesInNs( intval( $namespace ) ),
546 $raw,
547 $parser->getFunctionLang()
548 );
549 }
550 public static function numberingroup( $parser, $name = '', $raw = null ) {
551 return self::formatRaw(
552 SiteStats::numberingroup( strtolower( $name ) ),
553 $raw,
554 $parser->getFunctionLang()
555 );
556 }
557
558 /**
559 * Given a title, return the namespace name that would be given by the
560 * corresponding magic word
561 * Note: function name changed to "mwnamespace" rather than "namespace"
562 * to not break PHP 5.3
563 * @param Parser $parser
564 * @param string|null $title
565 * @return mixed|string
566 */
567 public static function mwnamespace( $parser, $title = null ) {
568 $t = Title::newFromText( $title );
569 if ( is_null( $t ) ) {
570 return '';
571 }
572 return str_replace( '_', ' ', $t->getNsText() );
573 }
574 public static function namespacee( $parser, $title = null ) {
575 $t = Title::newFromText( $title );
576 if ( is_null( $t ) ) {
577 return '';
578 }
579 return wfUrlencode( $t->getNsText() );
580 }
581 public static function namespacenumber( $parser, $title = null ) {
582 $t = Title::newFromText( $title );
583 if ( is_null( $t ) ) {
584 return '';
585 }
586 return $t->getNamespace();
587 }
588 public static function talkspace( $parser, $title = null ) {
589 $t = Title::newFromText( $title );
590 if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
591 return '';
592 }
593 return str_replace( '_', ' ', $t->getTalkNsText() );
594 }
595 public static function talkspacee( $parser, $title = null ) {
596 $t = Title::newFromText( $title );
597 if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
598 return '';
599 }
600 return wfUrlencode( $t->getTalkNsText() );
601 }
602 public static function subjectspace( $parser, $title = null ) {
603 $t = Title::newFromText( $title );
604 if ( is_null( $t ) ) {
605 return '';
606 }
607 return str_replace( '_', ' ', $t->getSubjectNsText() );
608 }
609 public static function subjectspacee( $parser, $title = null ) {
610 $t = Title::newFromText( $title );
611 if ( is_null( $t ) ) {
612 return '';
613 }
614 return wfUrlencode( $t->getSubjectNsText() );
615 }
616
617 /**
618 * Functions to get and normalize pagenames, corresponding to the magic words
619 * of the same names
620 * @param Parser $parser
621 * @param string|null $title
622 * @return string
623 */
624 public static function pagename( $parser, $title = null ) {
625 $t = Title::newFromText( $title );
626 if ( is_null( $t ) ) {
627 return '';
628 }
629 return wfEscapeWikiText( $t->getText() );
630 }
631 public static function pagenamee( $parser, $title = null ) {
632 $t = Title::newFromText( $title );
633 if ( is_null( $t ) ) {
634 return '';
635 }
636 return wfEscapeWikiText( $t->getPartialURL() );
637 }
638 public static function fullpagename( $parser, $title = null ) {
639 $t = Title::newFromText( $title );
640 if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
641 return '';
642 }
643 return wfEscapeWikiText( $t->getPrefixedText() );
644 }
645 public static function fullpagenamee( $parser, $title = null ) {
646 $t = Title::newFromText( $title );
647 if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
648 return '';
649 }
650 return wfEscapeWikiText( $t->getPrefixedURL() );
651 }
652 public static function subpagename( $parser, $title = null ) {
653 $t = Title::newFromText( $title );
654 if ( is_null( $t ) ) {
655 return '';
656 }
657 return wfEscapeWikiText( $t->getSubpageText() );
658 }
659 public static function subpagenamee( $parser, $title = null ) {
660 $t = Title::newFromText( $title );
661 if ( is_null( $t ) ) {
662 return '';
663 }
664 return wfEscapeWikiText( $t->getSubpageUrlForm() );
665 }
666 public static function rootpagename( $parser, $title = null ) {
667 $t = Title::newFromText( $title );
668 if ( is_null( $t ) ) {
669 return '';
670 }
671 return wfEscapeWikiText( $t->getRootText() );
672 }
673 public static function rootpagenamee( $parser, $title = null ) {
674 $t = Title::newFromText( $title );
675 if ( is_null( $t ) ) {
676 return '';
677 }
678 return wfEscapeWikiText( wfUrlencode( str_replace( ' ', '_', $t->getRootText() ) ) );
679 }
680 public static function basepagename( $parser, $title = null ) {
681 $t = Title::newFromText( $title );
682 if ( is_null( $t ) ) {
683 return '';
684 }
685 return wfEscapeWikiText( $t->getBaseText() );
686 }
687 public static function basepagenamee( $parser, $title = null ) {
688 $t = Title::newFromText( $title );
689 if ( is_null( $t ) ) {
690 return '';
691 }
692 return wfEscapeWikiText( wfUrlencode( str_replace( ' ', '_', $t->getBaseText() ) ) );
693 }
694 public static function talkpagename( $parser, $title = null ) {
695 $t = Title::newFromText( $title );
696 if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
697 return '';
698 }
699 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
700 }
701 public static function talkpagenamee( $parser, $title = null ) {
702 $t = Title::newFromText( $title );
703 if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
704 return '';
705 }
706 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedURL() );
707 }
708 public static function subjectpagename( $parser, $title = null ) {
709 $t = Title::newFromText( $title );
710 if ( is_null( $t ) ) {
711 return '';
712 }
713 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
714 }
715 public static function subjectpagenamee( $parser, $title = null ) {
716 $t = Title::newFromText( $title );
717 if ( is_null( $t ) ) {
718 return '';
719 }
720 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedURL() );
721 }
722
723 /**
724 * Return the number of pages, files or subcats in the given category,
725 * or 0 if it's nonexistent. This is an expensive parser function and
726 * can't be called too many times per page.
727 * @param Parser $parser
728 * @param string $name
729 * @param string|null $arg1
730 * @param string|null $arg2
731 * @return string
732 */
733 public static function pagesincategory( $parser, $name = '', $arg1 = null, $arg2 = null ) {
734 static $magicWords = null;
735 if ( is_null( $magicWords ) ) {
736 $magicWords = $parser->getMagicWordFactory()->newArray( [
737 'pagesincategory_all',
738 'pagesincategory_pages',
739 'pagesincategory_subcats',
740 'pagesincategory_files'
741 ] );
742 }
743 static $cache = [];
744
745 // split the given option to its variable
746 if ( self::matchAgainstMagicword( $parser->getMagicWordFactory(), 'rawsuffix', $arg1 ) ) {
747 // {{pagesincategory:|raw[|type]}}
748 $raw = $arg1;
749 $type = $magicWords->matchStartToEnd( $arg2 );
750 } else {
751 // {{pagesincategory:[|type[|raw]]}}
752 $type = $magicWords->matchStartToEnd( $arg1 );
753 $raw = $arg2;
754 }
755 if ( !$type ) { // backward compatibility
756 $type = 'pagesincategory_all';
757 }
758
759 $title = Title::makeTitleSafe( NS_CATEGORY, $name );
760 if ( !$title ) { # invalid title
761 return self::formatRaw( 0, $raw, $parser->getFunctionLang() );
762 }
763 $parser->getContentLanguage()->findVariantLink( $name, $title, true );
764
765 // Normalize name for cache
766 $name = $title->getDBkey();
767
768 if ( !isset( $cache[$name] ) ) {
769 $category = Category::newFromTitle( $title );
770
771 $allCount = $subcatCount = $fileCount = $pagesCount = 0;
772 if ( $parser->incrementExpensiveFunctionCount() ) {
773 // $allCount is the total number of cat members,
774 // not the count of how many members are normal pages.
775 $allCount = (int)$category->getPageCount();
776 $subcatCount = (int)$category->getSubcatCount();
777 $fileCount = (int)$category->getFileCount();
778 $pagesCount = $allCount - $subcatCount - $fileCount;
779 }
780 $cache[$name]['pagesincategory_all'] = $allCount;
781 $cache[$name]['pagesincategory_pages'] = $pagesCount;
782 $cache[$name]['pagesincategory_subcats'] = $subcatCount;
783 $cache[$name]['pagesincategory_files'] = $fileCount;
784 }
785
786 $count = $cache[$name][$type];
787 return self::formatRaw( $count, $raw, $parser->getFunctionLang() );
788 }
789
790 /**
791 * Return the size of the given page, or 0 if it's nonexistent. This is an
792 * expensive parser function and can't be called too many times per page.
793 *
794 * @param Parser $parser
795 * @param string $page Name of page to check (Default: empty string)
796 * @param string|null $raw Should number be human readable with commas or just number
797 * @return string
798 */
799 public static function pagesize( $parser, $page = '', $raw = null ) {
800 $title = Title::newFromText( $page );
801
802 if ( !is_object( $title ) ) {
803 return self::formatRaw( 0, $raw, $parser->getFunctionLang() );
804 }
805
806 // fetch revision from cache/database and return the value
807 $rev = self::getCachedRevisionObject( $parser, $title );
808 $length = $rev ? $rev->getSize() : 0;
809 if ( $length === null ) {
810 // We've had bugs where rev_len was not being recorded for empty pages, see T135414
811 $length = 0;
812 }
813 return self::formatRaw( $length, $raw, $parser->getFunctionLang() );
814 }
815
816 /**
817 * Returns the requested protection level for the current page. This
818 * is an expensive parser function and can't be called too many times
819 * per page, unless the protection levels/expiries for the given title
820 * have already been retrieved
821 *
822 * @param Parser $parser
823 * @param string $type
824 * @param string $title
825 *
826 * @return string
827 */
828 public static function protectionlevel( $parser, $type = '', $title = '' ) {
829 $titleObject = Title::newFromText( $title );
830 if ( !( $titleObject instanceof Title ) ) {
831 $titleObject = $parser->mTitle;
832 }
833 if ( $titleObject->areRestrictionsLoaded() || $parser->incrementExpensiveFunctionCount() ) {
834 $restrictions = $titleObject->getRestrictions( strtolower( $type ) );
835 # Title::getRestrictions returns an array, its possible it may have
836 # multiple values in the future
837 return implode( ',', $restrictions );
838 }
839 return '';
840 }
841
842 /**
843 * Returns the requested protection expiry for the current page. This
844 * is an expensive parser function and can't be called too many times
845 * per page, unless the protection levels/expiries for the given title
846 * have already been retrieved
847 *
848 * @param Parser $parser
849 * @param string $type
850 * @param string $title
851 *
852 * @return string
853 */
854 public static function protectionexpiry( $parser, $type = '', $title = '' ) {
855 $titleObject = Title::newFromText( $title );
856 if ( !( $titleObject instanceof Title ) ) {
857 $titleObject = $parser->mTitle;
858 }
859 if ( $titleObject->areRestrictionsLoaded() || $parser->incrementExpensiveFunctionCount() ) {
860 $expiry = $titleObject->getRestrictionExpiry( strtolower( $type ) );
861 // getRestrictionExpiry() returns false on invalid type; trying to
862 // match protectionlevel() function that returns empty string instead
863 if ( $expiry === false ) {
864 $expiry = '';
865 }
866 return $expiry;
867 }
868 return '';
869 }
870
871 /**
872 * Gives language names.
873 * @param Parser $parser
874 * @param string $code Language code (of which to get name)
875 * @param string $inLanguage Language code (in which to get name)
876 * @return string
877 */
878 public static function language( $parser, $code = '', $inLanguage = '' ) {
879 $code = strtolower( $code );
880 $inLanguage = strtolower( $inLanguage );
881 $lang = Language::fetchLanguageName( $code, $inLanguage );
882 return $lang !== '' ? $lang : LanguageCode::bcp47( $code );
883 }
884
885 /**
886 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
887 * @param Parser $parser
888 * @param string $string
889 * @param string $length
890 * @param string $padding
891 * @param int $direction
892 * @return string
893 */
894 public static function pad(
895 $parser, $string, $length, $padding = '0', $direction = STR_PAD_RIGHT
896 ) {
897 $padding = $parser->killMarkers( $padding );
898 $lengthOfPadding = mb_strlen( $padding );
899 if ( $lengthOfPadding == 0 ) {
900 return $string;
901 }
902
903 # The remaining length to add counts down to 0 as padding is added
904 $length = min( (int)$length, 500 ) - mb_strlen( $string );
905 if ( $length <= 0 ) {
906 // Nothing to add
907 return $string;
908 }
909
910 # $finalPadding is just $padding repeated enough times so that
911 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
912 $finalPadding = '';
913 while ( $length > 0 ) {
914 # If $length < $lengthofPadding, truncate $padding so we get the
915 # exact length desired.
916 $finalPadding .= mb_substr( $padding, 0, $length );
917 $length -= $lengthOfPadding;
918 }
919
920 if ( $direction == STR_PAD_LEFT ) {
921 return $finalPadding . $string;
922 } else {
923 return $string . $finalPadding;
924 }
925 }
926
927 public static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
928 return self::pad( $parser, $string, $length, $padding, STR_PAD_LEFT );
929 }
930
931 public static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
932 return self::pad( $parser, $string, $length, $padding );
933 }
934
935 /**
936 * @param Parser $parser
937 * @param string $text
938 * @return string
939 */
940 public static function anchorencode( $parser, $text ) {
941 $text = $parser->killMarkers( $text );
942 $section = (string)substr( $parser->guessSectionNameFromWikiText( $text ), 1 );
943 return Sanitizer::safeEncodeAttribute( $section );
944 }
945
946 public static function special( $parser, $text ) {
947 list( $page, $subpage ) = MediaWikiServices::getInstance()->getSpecialPageFactory()->
948 resolveAlias( $text );
949 if ( $page ) {
950 $title = SpecialPage::getTitleFor( $page, $subpage );
951 return $title->getPrefixedText();
952 } else {
953 // unknown special page, just use the given text as its title, if at all possible
954 $title = Title::makeTitleSafe( NS_SPECIAL, $text );
955 return $title ? $title->getPrefixedText() : self::special( $parser, 'Badtitle' );
956 }
957 }
958
959 public static function speciale( $parser, $text ) {
960 return wfUrlencode( str_replace( ' ', '_', self::special( $parser, $text ) ) );
961 }
962
963 /**
964 * @param Parser $parser
965 * @param string $text The sortkey to use
966 * @param string $uarg Either "noreplace" or "noerror" (in en)
967 * both suppress errors, and noreplace does nothing if
968 * a default sortkey already exists.
969 * @return string
970 */
971 public static function defaultsort( $parser, $text, $uarg = '' ) {
972 static $magicWords = null;
973 if ( is_null( $magicWords ) ) {
974 $magicWords = $parser->getMagicWordFactory()->newArray(
975 [ 'defaultsort_noerror', 'defaultsort_noreplace' ] );
976 }
977 $arg = $magicWords->matchStartToEnd( $uarg );
978
979 $text = trim( $text );
980 if ( strlen( $text ) == 0 ) {
981 return '';
982 }
983 $old = $parser->getCustomDefaultSort();
984 if ( $old === false || $arg !== 'defaultsort_noreplace' ) {
985 $parser->setDefaultSort( $text );
986 }
987
988 if ( $old === false || $old == $text || $arg ) {
989 return '';
990 } else {
991 $converter = $parser->getTargetLanguage()->getConverter();
992 return '<span class="error">' .
993 wfMessage( 'duplicate-defaultsort',
994 // Message should be parsed, but these params should only be escaped.
995 $converter->markNoConversion( wfEscapeWikiText( $old ) ),
996 $converter->markNoConversion( wfEscapeWikiText( $text ) )
997 )->inContentLanguage()->text() .
998 '</span>';
999 }
1000 }
1001
1002 /**
1003 * Usage {{filepath|300}}, {{filepath|nowiki}}, {{filepath|nowiki|300}}
1004 * or {{filepath|300|nowiki}} or {{filepath|300px}}, {{filepath|200x300px}},
1005 * {{filepath|nowiki|200x300px}}, {{filepath|200x300px|nowiki}}.
1006 *
1007 * @param Parser $parser
1008 * @param string $name
1009 * @param string $argA
1010 * @param string $argB
1011 * @return array|string
1012 */
1013 public static function filepath( $parser, $name = '', $argA = '', $argB = '' ) {
1014 $file = wfFindFile( $name );
1015
1016 if ( $argA == 'nowiki' ) {
1017 // {{filepath: | option [| size] }}
1018 $isNowiki = true;
1019 $parsedWidthParam = Parser::parseWidthParam( $argB );
1020 } else {
1021 // {{filepath: [| size [|option]] }}
1022 $parsedWidthParam = Parser::parseWidthParam( $argA );
1023 $isNowiki = ( $argB == 'nowiki' );
1024 }
1025
1026 if ( $file ) {
1027 $url = $file->getFullUrl();
1028
1029 // If a size is requested...
1030 if ( count( $parsedWidthParam ) ) {
1031 $mto = $file->transform( $parsedWidthParam );
1032 // ... and we can
1033 if ( $mto && !$mto->isError() ) {
1034 // ... change the URL to point to a thumbnail.
1035 $url = wfExpandUrl( $mto->getUrl(), PROTO_RELATIVE );
1036 }
1037 }
1038 if ( $isNowiki ) {
1039 return [ $url, 'nowiki' => true ];
1040 }
1041 return $url;
1042 } else {
1043 return '';
1044 }
1045 }
1046
1047 /**
1048 * Parser function to extension tag adaptor
1049 * @param Parser $parser
1050 * @param PPFrame $frame
1051 * @param PPNode[] $args
1052 * @return string
1053 */
1054 public static function tagObj( $parser, $frame, $args ) {
1055 if ( !count( $args ) ) {
1056 return '';
1057 }
1058 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
1059
1060 if ( count( $args ) ) {
1061 $inner = $frame->expand( array_shift( $args ) );
1062 } else {
1063 $inner = null;
1064 }
1065
1066 $attributes = [];
1067 foreach ( $args as $arg ) {
1068 $bits = $arg->splitArg();
1069 if ( strval( $bits['index'] ) === '' ) {
1070 $name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
1071 $value = trim( $frame->expand( $bits['value'] ) );
1072 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
1073 $value = $m[1] ?? '';
1074 }
1075 $attributes[$name] = $value;
1076 }
1077 }
1078
1079 $stripList = $parser->getStripList();
1080 if ( !in_array( $tagName, $stripList ) ) {
1081 // we can't handle this tag (at least not now), so just re-emit it as an ordinary tag
1082 $attrText = '';
1083 foreach ( $attributes as $name => $value ) {
1084 $attrText .= ' ' . htmlspecialchars( $name ) . '="' . htmlspecialchars( $value ) . '"';
1085 }
1086 if ( $inner === null ) {
1087 return "<$tagName$attrText/>";
1088 }
1089 return "<$tagName$attrText>$inner</$tagName>";
1090 }
1091
1092 $params = [
1093 'name' => $tagName,
1094 'inner' => $inner,
1095 'attributes' => $attributes,
1096 'close' => "</$tagName>",
1097 ];
1098 return $parser->extensionSubstitution( $params, $frame );
1099 }
1100
1101 /**
1102 * Fetched the current revision of the given title and return this.
1103 * Will increment the expensive function count and
1104 * add a template link to get the value refreshed on changes.
1105 * For a given title, which is equal to the current parser title,
1106 * the revision object from the parser is used, when that is the current one
1107 *
1108 * @param Parser $parser
1109 * @param Title $title
1110 * @return Revision
1111 * @since 1.23
1112 */
1113 private static function getCachedRevisionObject( $parser, $title = null ) {
1114 if ( is_null( $title ) ) {
1115 return null;
1116 }
1117
1118 // Use the revision from the parser itself, when param is the current page
1119 // and the revision is the current one
1120 if ( $title->equals( $parser->getTitle() ) ) {
1121 $parserRev = $parser->getRevisionObject();
1122 if ( $parserRev && $parserRev->isCurrent() ) {
1123 // force reparse after edit with vary-revision flag
1124 $parser->getOutput()->setFlag( 'vary-revision' );
1125 wfDebug( __METHOD__ . ": use current revision from parser, setting vary-revision...\n" );
1126 return $parserRev;
1127 }
1128 }
1129
1130 // Normalize name for cache
1131 $page = $title->getPrefixedDBkey();
1132
1133 if ( !( $parser->currentRevisionCache && $parser->currentRevisionCache->has( $page ) )
1134 && !$parser->incrementExpensiveFunctionCount() ) {
1135 return null;
1136 }
1137 $rev = $parser->fetchCurrentRevisionOfTitle( $title );
1138 $pageID = $rev ? $rev->getPage() : 0;
1139 $revID = $rev ? $rev->getId() : 0;
1140
1141 // Register dependency in templatelinks
1142 $parser->getOutput()->addTemplate( $title, $pageID, $revID );
1143
1144 return $rev;
1145 }
1146
1147 /**
1148 * Get the pageid of a specified page
1149 * @param Parser $parser
1150 * @param string|null $title Title to get the pageid from
1151 * @return int|null|string
1152 * @since 1.23
1153 */
1154 public static function pageid( $parser, $title = null ) {
1155 $t = Title::newFromText( $title );
1156 if ( is_null( $t ) ) {
1157 return '';
1158 }
1159 // Use title from parser to have correct pageid after edit
1160 if ( $t->equals( $parser->getTitle() ) ) {
1161 $t = $parser->getTitle();
1162 return $t->getArticleID();
1163 }
1164
1165 // These can't have ids
1166 if ( !$t->canExist() || $t->isExternal() ) {
1167 return 0;
1168 }
1169
1170 // Check the link cache, maybe something already looked it up.
1171 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
1172 $pdbk = $t->getPrefixedDBkey();
1173 $id = $linkCache->getGoodLinkID( $pdbk );
1174 if ( $id != 0 ) {
1175 $parser->mOutput->addLink( $t, $id );
1176 return $id;
1177 }
1178 if ( $linkCache->isBadLink( $pdbk ) ) {
1179 $parser->mOutput->addLink( $t, 0 );
1180 return $id;
1181 }
1182
1183 // We need to load it from the DB, so mark expensive
1184 if ( $parser->incrementExpensiveFunctionCount() ) {
1185 $id = $t->getArticleID();
1186 $parser->mOutput->addLink( $t, $id );
1187 return $id;
1188 }
1189 return null;
1190 }
1191
1192 /**
1193 * Get the id from the last revision of a specified page.
1194 * @param Parser $parser
1195 * @param string|null $title Title to get the id from
1196 * @return int|null|string
1197 * @since 1.23
1198 */
1199 public static function revisionid( $parser, $title = null ) {
1200 $t = Title::newFromText( $title );
1201 if ( is_null( $t ) ) {
1202 return '';
1203 }
1204 // fetch revision from cache/database and return the value
1205 $rev = self::getCachedRevisionObject( $parser, $t );
1206 return $rev ? $rev->getId() : '';
1207 }
1208
1209 /**
1210 * Get the day from the last revision of a specified page.
1211 * @param Parser $parser
1212 * @param string|null $title Title to get the day from
1213 * @return string
1214 * @since 1.23
1215 */
1216 public static function revisionday( $parser, $title = null ) {
1217 $t = Title::newFromText( $title );
1218 if ( is_null( $t ) ) {
1219 return '';
1220 }
1221 // fetch revision from cache/database and return the value
1222 $rev = self::getCachedRevisionObject( $parser, $t );
1223 return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'j' ) : '';
1224 }
1225
1226 /**
1227 * Get the day with leading zeros from the last revision of a specified page.
1228 * @param Parser $parser
1229 * @param string|null $title Title to get the day from
1230 * @return string
1231 * @since 1.23
1232 */
1233 public static function revisionday2( $parser, $title = null ) {
1234 $t = Title::newFromText( $title );
1235 if ( is_null( $t ) ) {
1236 return '';
1237 }
1238 // fetch revision from cache/database and return the value
1239 $rev = self::getCachedRevisionObject( $parser, $t );
1240 return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'd' ) : '';
1241 }
1242
1243 /**
1244 * Get the month with leading zeros from the last revision of a specified page.
1245 * @param Parser $parser
1246 * @param string|null $title Title to get the month from
1247 * @return string
1248 * @since 1.23
1249 */
1250 public static function revisionmonth( $parser, $title = null ) {
1251 $t = Title::newFromText( $title );
1252 if ( is_null( $t ) ) {
1253 return '';
1254 }
1255 // fetch revision from cache/database and return the value
1256 $rev = self::getCachedRevisionObject( $parser, $t );
1257 return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'm' ) : '';
1258 }
1259
1260 /**
1261 * Get the month from the last revision of a specified page.
1262 * @param Parser $parser
1263 * @param string|null $title Title to get the month from
1264 * @return string
1265 * @since 1.23
1266 */
1267 public static function revisionmonth1( $parser, $title = null ) {
1268 $t = Title::newFromText( $title );
1269 if ( is_null( $t ) ) {
1270 return '';
1271 }
1272 // fetch revision from cache/database and return the value
1273 $rev = self::getCachedRevisionObject( $parser, $t );
1274 return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'n' ) : '';
1275 }
1276
1277 /**
1278 * Get the year from the last revision of a specified page.
1279 * @param Parser $parser
1280 * @param string|null $title Title to get the year from
1281 * @return string
1282 * @since 1.23
1283 */
1284 public static function revisionyear( $parser, $title = null ) {
1285 $t = Title::newFromText( $title );
1286 if ( is_null( $t ) ) {
1287 return '';
1288 }
1289 // fetch revision from cache/database and return the value
1290 $rev = self::getCachedRevisionObject( $parser, $t );
1291 return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'Y' ) : '';
1292 }
1293
1294 /**
1295 * Get the timestamp from the last revision of a specified page.
1296 * @param Parser $parser
1297 * @param string|null $title Title to get the timestamp from
1298 * @return string
1299 * @since 1.23
1300 */
1301 public static function revisiontimestamp( $parser, $title = null ) {
1302 $t = Title::newFromText( $title );
1303 if ( is_null( $t ) ) {
1304 return '';
1305 }
1306 // fetch revision from cache/database and return the value
1307 $rev = self::getCachedRevisionObject( $parser, $t );
1308 return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'YmdHis' ) : '';
1309 }
1310
1311 /**
1312 * Get the user from the last revision of a specified page.
1313 * @param Parser $parser
1314 * @param string|null $title Title to get the user from
1315 * @return string
1316 * @since 1.23
1317 */
1318 public static function revisionuser( $parser, $title = null ) {
1319 $t = Title::newFromText( $title );
1320 if ( is_null( $t ) ) {
1321 return '';
1322 }
1323 // fetch revision from cache/database and return the value
1324 $rev = self::getCachedRevisionObject( $parser, $t );
1325 return $rev ? $rev->getUserText() : '';
1326 }
1327
1328 /**
1329 * Returns the sources of any cascading protection acting on a specified page.
1330 * Pages will not return their own title unless they transclude themselves.
1331 * This is an expensive parser function and can't be called too many times per page,
1332 * unless cascading protection sources for the page have already been loaded.
1333 *
1334 * @param Parser $parser
1335 * @param string $title
1336 *
1337 * @return string
1338 * @since 1.23
1339 */
1340 public static function cascadingsources( $parser, $title = '' ) {
1341 $titleObject = Title::newFromText( $title );
1342 if ( !( $titleObject instanceof Title ) ) {
1343 $titleObject = $parser->mTitle;
1344 }
1345 if ( $titleObject->areCascadeProtectionSourcesLoaded()
1346 || $parser->incrementExpensiveFunctionCount()
1347 ) {
1348 $names = [];
1349 $sources = $titleObject->getCascadeProtectionSources();
1350 foreach ( $sources[0] as $sourceTitle ) {
1351 $names[] = $sourceTitle->getPrefixedText();
1352 }
1353 return implode( '|', $names );
1354 }
1355 return '';
1356 }
1357
1358 }