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