3 * Parser functions provided by MediaWiki core
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Various core parser functions, registered in Parser::firstCallInit()
28 class CoreParserFunctions
{
30 * @param Parser $parser
33 public static function register( $parser ) {
34 global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
36 # Syntax for arguments (see Parser::setFunctionHook):
37 # "name for lookup in localized magic words array",
39 # optional Parser::SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}}
40 # instead of {{#int:...}})
41 $noHashFunctions = array(
42 'ns', 'nse', 'urlencode', 'lcfirst', 'ucfirst', 'lc', 'uc',
43 'localurl', 'localurle', 'fullurl', 'fullurle', 'canonicalurl',
44 'canonicalurle', 'formatnum', 'grammar', 'gender', 'plural', 'bidi',
45 'numberofpages', 'numberofusers', 'numberofactiveusers',
46 'numberofarticles', 'numberoffiles', 'numberofadmins',
47 'numberingroup', 'numberofedits', 'language',
48 'padleft', 'padright', 'anchorencode', 'defaultsort', 'filepath',
49 'pagesincategory', 'pagesize', 'protectionlevel',
50 'namespacee', 'namespacenumber', 'talkspace', 'talkspacee',
51 'subjectspace', 'subjectspacee', 'pagename', 'pagenamee',
52 'fullpagename', 'fullpagenamee', 'rootpagename', 'rootpagenamee',
53 'basepagename', 'basepagenamee', 'subpagename', 'subpagenamee',
54 'talkpagename', 'talkpagenamee', 'subjectpagename',
55 'subjectpagenamee', 'pageid', 'revisionid', 'revisionday',
56 'revisionday2', 'revisionmonth', 'revisionmonth1', 'revisionyear',
57 'revisiontimestamp', 'revisionuser', 'cascadingsources',
59 foreach ( $noHashFunctions as $func ) {
60 $parser->setFunctionHook( $func, array( __CLASS__
, $func ), Parser
::SFH_NO_HASH
);
63 $parser->setFunctionHook( 'namespace', array( __CLASS__
, 'mwnamespace' ), Parser
::SFH_NO_HASH
);
64 $parser->setFunctionHook( 'int', array( __CLASS__
, 'intFunction' ), Parser
::SFH_NO_HASH
);
65 $parser->setFunctionHook( 'special', array( __CLASS__
, 'special' ) );
66 $parser->setFunctionHook( 'speciale', array( __CLASS__
, 'speciale' ) );
67 $parser->setFunctionHook( 'tag', array( __CLASS__
, 'tagObj' ), Parser
::SFH_OBJECT_ARGS
);
68 $parser->setFunctionHook( 'formatdate', array( __CLASS__
, 'formatDate' ) );
70 if ( $wgAllowDisplayTitle ) {
71 $parser->setFunctionHook( 'displaytitle', array( __CLASS__
, 'displaytitle' ), Parser
::SFH_NO_HASH
);
73 if ( $wgAllowSlowParserFunctions ) {
74 $parser->setFunctionHook(
76 array( __CLASS__
, 'pagesinnamespace' ),
83 * @param Parser $parser
84 * @param string $part1
87 public static function intFunction( $parser, $part1 = '' /*, ... */ ) {
88 if ( strval( $part1 ) !== '' ) {
89 $args = array_slice( func_get_args(), 2 );
90 $message = wfMessage( $part1, $args )
91 ->inLanguage( $parser->getOptions()->getUserLangObj() )->plain();
93 return array( $message, 'noparse' => false );
95 return array( 'found' => false );
100 * @param Parser $parser
101 * @param string $date
102 * @param string $defaultPref
106 public static function formatDate( $parser, $date, $defaultPref = null ) {
107 $lang = $parser->getFunctionLang();
108 $df = DateFormatter
::getInstance( $lang );
110 $date = trim( $date );
112 $pref = $parser->getOptions()->getDateFormat();
114 // Specify a different default date format other than the normal default
115 // if the user has 'default' for their setting
116 if ( $pref == 'default' && $defaultPref ) {
117 $pref = $defaultPref;
120 $date = $df->reformat( $pref, $date, array( 'match-whole' ) );
124 public static function ns( $parser, $part1 = '' ) {
126 if ( intval( $part1 ) ||
$part1 == "0" ) {
127 $index = intval( $part1 );
129 $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
131 if ( $index !== false ) {
132 return $wgContLang->getFormattedNsText( $index );
134 return array( 'found' => false );
138 public static function nse( $parser, $part1 = '' ) {
139 $ret = self
::ns( $parser, $part1 );
140 if ( is_string( $ret ) ) {
141 $ret = wfUrlencode( str_replace( ' ', '_', $ret ) );
147 * urlencodes a string according to one of three patterns: (bug 22474)
149 * By default (for HTTP "query" strings), spaces are encoded as '+'.
150 * Or to encode a value for the HTTP "path", spaces are encoded as '%20'.
151 * For links to "wiki"s, or similar software, spaces are encoded as '_',
153 * @param Parser $parser
154 * @param string $s The text to encode.
155 * @param string $arg (optional): The type of encoding.
158 public static function urlencode( $parser, $s = '', $arg = null ) {
159 static $magicWords = null;
160 if ( is_null( $magicWords ) ) {
161 $magicWords = new MagicWordArray( array( 'url_path', 'url_query', 'url_wiki' ) );
163 switch ( $magicWords->matchStartToEnd( $arg ) ) {
165 // Encode as though it's a wiki page, '_' for ' '.
167 $func = 'wfUrlencode';
168 $s = str_replace( ' ', '_', $s );
171 // Encode for an HTTP Path, '%20' for ' '.
173 $func = 'rawurlencode';
176 // Encode for HTTP query, '+' for ' '.
181 // See T105242, where the choice to kill markers and various
182 // other options were discussed.
183 return $func( $parser->killMarkers( $s ) );
186 public static function lcfirst( $parser, $s = '' ) {
188 return $wgContLang->lcfirst( $s );
191 public static function ucfirst( $parser, $s = '' ) {
193 return $wgContLang->ucfirst( $s );
197 * @param Parser $parser
201 public static function lc( $parser, $s = '' ) {
203 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
207 * @param Parser $parser
211 public static function uc( $parser, $s = '' ) {
213 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
216 public static function localurl( $parser, $s = '', $arg = null ) {
217 return self
::urlFunction( 'getLocalURL', $s, $arg );
220 public static function localurle( $parser, $s = '', $arg = null ) {
221 $temp = self
::urlFunction( 'getLocalURL', $s, $arg );
222 if ( !is_string( $temp ) ) {
225 return htmlspecialchars( $temp );
229 public static function fullurl( $parser, $s = '', $arg = null ) {
230 return self
::urlFunction( 'getFullURL', $s, $arg );
233 public static function fullurle( $parser, $s = '', $arg = null ) {
234 $temp = self
::urlFunction( 'getFullURL', $s, $arg );
235 if ( !is_string( $temp ) ) {
238 return htmlspecialchars( $temp );
242 public static function canonicalurl( $parser, $s = '', $arg = null ) {
243 return self
::urlFunction( 'getCanonicalURL', $s, $arg );
246 public static function canonicalurle( $parser, $s = '', $arg = null ) {
247 $temp = self
::urlFunction( 'getCanonicalURL', $s, $arg );
248 if ( !is_string( $temp ) ) {
251 return htmlspecialchars( $temp );
255 public static function urlFunction( $func, $s = '', $arg = null ) {
256 $title = Title
::newFromText( $s );
257 # Due to order of execution of a lot of bits, the values might be encoded
258 # before arriving here; if that's true, then the title can't be created
259 # and the variable will fail. If we can't get a decent title from the first
260 # attempt, url-decode and try for a second.
261 if ( is_null( $title ) ) {
262 $title = Title
::newFromURL( urldecode( $s ) );
264 if ( !is_null( $title ) ) {
265 # Convert NS_MEDIA -> NS_FILE
266 if ( $title->getNamespace() == NS_MEDIA
) {
267 $title = Title
::makeTitle( NS_FILE
, $title->getDBkey() );
269 if ( !is_null( $arg ) ) {
270 $text = $title->$func( $arg );
272 $text = $title->$func();
276 return array( 'found' => false );
281 * @param Parser $parser
286 public static function formatnum( $parser, $num = '', $arg = null ) {
287 if ( self
::matchAgainstMagicword( 'rawsuffix', $arg ) ) {
288 $func = array( $parser->getFunctionLang(), 'parseFormattedNumber' );
289 } elseif ( self
::matchAgainstMagicword( 'nocommafysuffix', $arg ) ) {
290 $func = array( $parser->getFunctionLang(), 'formatNumNoSeparators' );
292 $func = array( $parser->getFunctionLang(), 'formatNum' );
294 return $parser->markerSkipCallback( $num, $func );
298 * @param Parser $parser
299 * @param string $case
300 * @param string $word
303 public static function grammar( $parser, $case = '', $word = '' ) {
304 $word = $parser->killMarkers( $word );
305 return $parser->getFunctionLang()->convertGrammar( $word, $case );
309 * @param Parser $parser
310 * @param string $username
313 public static function gender( $parser, $username ) {
314 $forms = array_slice( func_get_args(), 2 );
316 // Some shortcuts to avoid loading user data unnecessarily
317 if ( count( $forms ) === 0 ) {
319 } elseif ( count( $forms ) === 1 ) {
323 $username = trim( $username );
326 $gender = User
::getDefaultOption( 'gender' );
329 $title = Title
::newFromText( $username );
331 if ( $title && $title->getNamespace() == NS_USER
) {
332 $username = $title->getText();
335 // check parameter, or use the ParserOptions if in interface message
336 $user = User
::newFromName( $username );
338 $gender = GenderCache
::singleton()->getGenderOf( $user, __METHOD__
);
339 } elseif ( $username === '' && $parser->getOptions()->getInterfaceMessage() ) {
340 $gender = GenderCache
::singleton()->getGenderOf( $parser->getOptions()->getUser(), __METHOD__
);
342 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
347 * @param Parser $parser
348 * @param string $text
351 public static function plural( $parser, $text = '' ) {
352 $forms = array_slice( func_get_args(), 2 );
353 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
354 settype( $text, ctype_digit( $text ) ?
'int' : 'float' );
355 return $parser->getFunctionLang()->convertPlural( $text, $forms );
359 * @param Parser $parser
360 * @param string $text
363 public static function bidi( $parser, $text = '' ) {
364 return $parser->getFunctionLang()->embedBidi( $text );
368 * Override the title of the page when viewed, provided we've been given a
369 * title which will normalise to the canonical title
371 * @param Parser $parser Parent parser
372 * @param string $text Desired title text
373 * @param string $uarg
376 public static function displaytitle( $parser, $text = '', $uarg = '' ) {
377 global $wgRestrictDisplayTitle;
379 static $magicWords = null;
380 if ( is_null( $magicWords ) ) {
381 $magicWords = new MagicWordArray( array( 'displaytitle_noerror', 'displaytitle_noreplace' ) );
383 $arg = $magicWords->matchStartToEnd( $uarg );
385 // parse a limited subset of wiki markup (just the single quote items)
386 $text = $parser->doQuotes( $text );
388 // remove stripped text (e.g. the UNIQ-QINU stuff) that was generated by tag extensions/whatever
389 $text = $parser->killMarkers( $text );
391 // list of disallowed tags for DISPLAYTITLE
392 // these will be escaped even though they are allowed in normal wiki text
393 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr',
394 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rtc', 'rp', 'br' );
396 // disallow some styles that could be used to bypass $wgRestrictDisplayTitle
397 if ( $wgRestrictDisplayTitle ) {
398 $htmlTagsCallback = function ( &$params ) {
399 $decoded = Sanitizer
::decodeTagAttributes( $params );
401 if ( isset( $decoded['style'] ) ) {
402 // this is called later anyway, but we need it right now for the regexes below to be safe
403 // calling it twice doesn't hurt
404 $decoded['style'] = Sanitizer
::checkCss( $decoded['style'] );
406 if ( preg_match( '/(display|user-select|visibility)\s*:/i', $decoded['style'] ) ) {
407 $decoded['style'] = '/* attempt to bypass $wgRestrictDisplayTitle */';
411 $params = Sanitizer
::safeEncodeTagAttributes( $decoded );
414 $htmlTagsCallback = null;
417 // only requested titles that normalize to the actual title are allowed through
418 // if $wgRestrictDisplayTitle is true (it is by default)
419 // mimic the escaping process that occurs in OutputPage::setPageTitle
420 $text = Sanitizer
::normalizeCharReferences( Sanitizer
::removeHTMLtags(
427 $title = Title
::newFromText( Sanitizer
::stripAllTags( $text ) );
429 if ( !$wgRestrictDisplayTitle ||
430 ( $title instanceof Title
431 && !$title->hasFragment()
432 && $title->equals( $parser->mTitle
) )
434 $old = $parser->mOutput
->getProperty( 'displaytitle' );
435 if ( $old === false ||
$arg !== 'displaytitle_noreplace' ) {
436 $parser->mOutput
->setDisplayTitle( $text );
438 if ( $old !== false && $old !== $text && !$arg ) {
439 $converter = $parser->getConverterLanguage()->getConverter();
440 return '<span class="error">' .
441 wfMessage( 'duplicate-displaytitle',
442 // Message should be parsed, but these params should only be escaped.
443 $converter->markNoConversion( wfEscapeWikiText( $old ) ),
444 $converter->markNoConversion( wfEscapeWikiText( $text ) )
445 )->inContentLanguage()->text() .
454 * Matches the given value against the value of given magic word
456 * @param string $magicword Magic word key
457 * @param string $value Value to match
458 * @return bool True on successful match
460 private static function matchAgainstMagicword( $magicword, $value ) {
461 $value = trim( strval( $value ) );
462 if ( $value === '' ) {
465 $mwObject = MagicWord
::get( $magicword );
466 return $mwObject->matchStartToEnd( $value );
469 public static function formatRaw( $num, $raw ) {
470 if ( self
::matchAgainstMagicword( 'rawsuffix', $raw ) ) {
474 return $wgContLang->formatNum( $num );
477 public static function numberofpages( $parser, $raw = null ) {
478 return self
::formatRaw( SiteStats
::pages(), $raw );
480 public static function numberofusers( $parser, $raw = null ) {
481 return self
::formatRaw( SiteStats
::users(), $raw );
483 public static function numberofactiveusers( $parser, $raw = null ) {
484 return self
::formatRaw( SiteStats
::activeUsers(), $raw );
486 public static function numberofarticles( $parser, $raw = null ) {
487 return self
::formatRaw( SiteStats
::articles(), $raw );
489 public static function numberoffiles( $parser, $raw = null ) {
490 return self
::formatRaw( SiteStats
::images(), $raw );
492 public static function numberofadmins( $parser, $raw = null ) {
493 return self
::formatRaw( SiteStats
::numberingroup( 'sysop' ), $raw );
495 public static function numberofedits( $parser, $raw = null ) {
496 return self
::formatRaw( SiteStats
::edits(), $raw );
498 public static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
499 return self
::formatRaw( SiteStats
::pagesInNs( intval( $namespace ) ), $raw );
501 public static function numberingroup( $parser, $name = '', $raw = null ) {
502 return self
::formatRaw( SiteStats
::numberingroup( strtolower( $name ) ), $raw );
506 * Given a title, return the namespace name that would be given by the
507 * corresponding magic word
508 * Note: function name changed to "mwnamespace" rather than "namespace"
509 * to not break PHP 5.3
510 * @param Parser $parser
511 * @param string $title
512 * @return mixed|string
514 public static function mwnamespace( $parser, $title = null ) {
515 $t = Title
::newFromText( $title );
516 if ( is_null( $t ) ) {
519 return str_replace( '_', ' ', $t->getNsText() );
521 public static function namespacee( $parser, $title = null ) {
522 $t = Title
::newFromText( $title );
523 if ( is_null( $t ) ) {
526 return wfUrlencode( $t->getNsText() );
528 public static function namespacenumber( $parser, $title = null ) {
529 $t = Title
::newFromText( $title );
530 if ( is_null( $t ) ) {
533 return $t->getNamespace();
535 public static function talkspace( $parser, $title = null ) {
536 $t = Title
::newFromText( $title );
537 if ( is_null( $t ) ||
!$t->canTalk() ) {
540 return str_replace( '_', ' ', $t->getTalkNsText() );
542 public static function talkspacee( $parser, $title = null ) {
543 $t = Title
::newFromText( $title );
544 if ( is_null( $t ) ||
!$t->canTalk() ) {
547 return wfUrlencode( $t->getTalkNsText() );
549 public static function subjectspace( $parser, $title = null ) {
550 $t = Title
::newFromText( $title );
551 if ( is_null( $t ) ) {
554 return str_replace( '_', ' ', $t->getSubjectNsText() );
556 public static function subjectspacee( $parser, $title = null ) {
557 $t = Title
::newFromText( $title );
558 if ( is_null( $t ) ) {
561 return wfUrlencode( $t->getSubjectNsText() );
565 * Functions to get and normalize pagenames, corresponding to the magic words
567 * @param Parser $parser
568 * @param string $title
571 public static function pagename( $parser, $title = null ) {
572 $t = Title
::newFromText( $title );
573 if ( is_null( $t ) ) {
576 return wfEscapeWikiText( $t->getText() );
578 public static function pagenamee( $parser, $title = null ) {
579 $t = Title
::newFromText( $title );
580 if ( is_null( $t ) ) {
583 return wfEscapeWikiText( $t->getPartialURL() );
585 public static function fullpagename( $parser, $title = null ) {
586 $t = Title
::newFromText( $title );
587 if ( is_null( $t ) ||
!$t->canTalk() ) {
590 return wfEscapeWikiText( $t->getPrefixedText() );
592 public static function fullpagenamee( $parser, $title = null ) {
593 $t = Title
::newFromText( $title );
594 if ( is_null( $t ) ||
!$t->canTalk() ) {
597 return wfEscapeWikiText( $t->getPrefixedURL() );
599 public static function subpagename( $parser, $title = null ) {
600 $t = Title
::newFromText( $title );
601 if ( is_null( $t ) ) {
604 return wfEscapeWikiText( $t->getSubpageText() );
606 public static function subpagenamee( $parser, $title = null ) {
607 $t = Title
::newFromText( $title );
608 if ( is_null( $t ) ) {
611 return wfEscapeWikiText( $t->getSubpageUrlForm() );
613 public static function rootpagename( $parser, $title = null ) {
614 $t = Title
::newFromText( $title );
615 if ( is_null( $t ) ) {
618 return wfEscapeWikiText( $t->getRootText() );
620 public static function rootpagenamee( $parser, $title = null ) {
621 $t = Title
::newFromText( $title );
622 if ( is_null( $t ) ) {
625 return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', $t->getRootText() ) ) );
627 public static function basepagename( $parser, $title = null ) {
628 $t = Title
::newFromText( $title );
629 if ( is_null( $t ) ) {
632 return wfEscapeWikiText( $t->getBaseText() );
634 public static function basepagenamee( $parser, $title = null ) {
635 $t = Title
::newFromText( $title );
636 if ( is_null( $t ) ) {
639 return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) ) );
641 public static function talkpagename( $parser, $title = null ) {
642 $t = Title
::newFromText( $title );
643 if ( is_null( $t ) ||
!$t->canTalk() ) {
646 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
648 public static function talkpagenamee( $parser, $title = null ) {
649 $t = Title
::newFromText( $title );
650 if ( is_null( $t ) ||
!$t->canTalk() ) {
653 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedURL() );
655 public static function subjectpagename( $parser, $title = null ) {
656 $t = Title
::newFromText( $title );
657 if ( is_null( $t ) ) {
660 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
662 public static function subjectpagenamee( $parser, $title = null ) {
663 $t = Title
::newFromText( $title );
664 if ( is_null( $t ) ) {
667 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedURL() );
671 * Return the number of pages, files or subcats in the given category,
672 * or 0 if it's nonexistent. This is an expensive parser function and
673 * can't be called too many times per page.
674 * @param Parser $parser
675 * @param string $name
676 * @param string $arg1
677 * @param string $arg2
680 public static function pagesincategory( $parser, $name = '', $arg1 = null, $arg2 = null ) {
682 static $magicWords = null;
683 if ( is_null( $magicWords ) ) {
684 $magicWords = new MagicWordArray( array(
685 'pagesincategory_all',
686 'pagesincategory_pages',
687 'pagesincategory_subcats',
688 'pagesincategory_files'
691 static $cache = array();
693 // split the given option to its variable
694 if ( self
::matchAgainstMagicword( 'rawsuffix', $arg1 ) ) {
695 //{{pagesincategory:|raw[|type]}}
697 $type = $magicWords->matchStartToEnd( $arg2 );
699 //{{pagesincategory:[|type[|raw]]}}
700 $type = $magicWords->matchStartToEnd( $arg1 );
703 if ( !$type ) { //backward compatibility
704 $type = 'pagesincategory_all';
707 $title = Title
::makeTitleSafe( NS_CATEGORY
, $name );
708 if ( !$title ) { # invalid title
709 return self
::formatRaw( 0, $raw );
711 $wgContLang->findVariantLink( $name, $title, true );
713 // Normalize name for cache
714 $name = $title->getDBkey();
716 if ( !isset( $cache[$name] ) ) {
717 $category = Category
::newFromTitle( $title );
719 $allCount = $subcatCount = $fileCount = $pagesCount = 0;
720 if ( $parser->incrementExpensiveFunctionCount() ) {
721 // $allCount is the total number of cat members,
722 // not the count of how many members are normal pages.
723 $allCount = (int)$category->getPageCount();
724 $subcatCount = (int)$category->getSubcatCount();
725 $fileCount = (int)$category->getFileCount();
726 $pagesCount = $allCount - $subcatCount - $fileCount;
728 $cache[$name]['pagesincategory_all'] = $allCount;
729 $cache[$name]['pagesincategory_pages'] = $pagesCount;
730 $cache[$name]['pagesincategory_subcats'] = $subcatCount;
731 $cache[$name]['pagesincategory_files'] = $fileCount;
734 $count = $cache[$name][$type];
735 return self
::formatRaw( $count, $raw );
739 * Return the size of the given page, or 0 if it's nonexistent. This is an
740 * expensive parser function and can't be called too many times per page.
742 * @param Parser $parser
743 * @param string $page Name of page to check (Default: empty string)
744 * @param string $raw Should number be human readable with commas or just number
747 public static function pagesize( $parser, $page = '', $raw = null ) {
748 $title = Title
::newFromText( $page );
750 if ( !is_object( $title ) ) {
751 return self
::formatRaw( 0, $raw );
754 // fetch revision from cache/database and return the value
755 $rev = self
::getCachedRevisionObject( $parser, $title );
756 $length = $rev ?
$rev->getSize() : 0;
757 return self
::formatRaw( $length, $raw );
761 * Returns the requested protection level for the current page. This
762 * is an expensive parser function and can't be called too many times
763 * per page, unless the protection levels for the given title have
764 * already been retrieved
766 * @param Parser $parser
767 * @param string $type
768 * @param string $title
772 public static function protectionlevel( $parser, $type = '', $title = '' ) {
773 $titleObject = Title
::newFromText( $title );
774 if ( !( $titleObject instanceof Title
) ) {
775 $titleObject = $parser->mTitle
;
777 if ( $titleObject->areRestrictionsLoaded() ||
$parser->incrementExpensiveFunctionCount() ) {
778 $restrictions = $titleObject->getRestrictions( strtolower( $type ) );
779 # Title::getRestrictions returns an array, its possible it may have
780 # multiple values in the future
781 return implode( $restrictions, ',' );
787 * Gives language names.
788 * @param Parser $parser
789 * @param string $code Language code (of which to get name)
790 * @param string $inLanguage Language code (in which to get name)
793 public static function language( $parser, $code = '', $inLanguage = '' ) {
794 $code = strtolower( $code );
795 $inLanguage = strtolower( $inLanguage );
796 $lang = Language
::fetchLanguageName( $code, $inLanguage );
797 return $lang !== '' ?
$lang : wfBCP47( $code );
801 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
802 * @param Parser $parser
803 * @param string $string
805 * @param string $padding
806 * @param int $direction
809 public static function pad( $parser, $string, $length, $padding = '0', $direction = STR_PAD_RIGHT
) {
810 $padding = $parser->killMarkers( $padding );
811 $lengthOfPadding = mb_strlen( $padding );
812 if ( $lengthOfPadding == 0 ) {
816 # The remaining length to add counts down to 0 as padding is added
817 $length = min( $length, 500 ) - mb_strlen( $string );
818 # $finalPadding is just $padding repeated enough times so that
819 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
821 while ( $length > 0 ) {
822 # If $length < $lengthofPadding, truncate $padding so we get the
823 # exact length desired.
824 $finalPadding .= mb_substr( $padding, 0, $length );
825 $length -= $lengthOfPadding;
828 if ( $direction == STR_PAD_LEFT
) {
829 return $finalPadding . $string;
831 return $string . $finalPadding;
835 public static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
836 return self
::pad( $parser, $string, $length, $padding, STR_PAD_LEFT
);
839 public static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
840 return self
::pad( $parser, $string, $length, $padding );
844 * @param Parser $parser
845 * @param string $text
848 public static function anchorencode( $parser, $text ) {
849 $text = $parser->killMarkers( $text );
850 return (string)substr( $parser->guessSectionNameFromWikiText( $text ), 1 );
853 public static function special( $parser, $text ) {
854 list( $page, $subpage ) = SpecialPageFactory
::resolveAlias( $text );
856 $title = SpecialPage
::getTitleFor( $page, $subpage );
857 return $title->getPrefixedText();
859 // unknown special page, just use the given text as its title, if at all possible
860 $title = Title
::makeTitleSafe( NS_SPECIAL
, $text );
861 return $title ?
$title->getPrefixedText() : self
::special( $parser, 'Badtitle' );
865 public static function speciale( $parser, $text ) {
866 return wfUrlencode( str_replace( ' ', '_', self
::special( $parser, $text ) ) );
870 * @param Parser $parser
871 * @param string $text The sortkey to use
872 * @param string $uarg Either "noreplace" or "noerror" (in en)
873 * both suppress errors, and noreplace does nothing if
874 * a default sortkey already exists.
877 public static function defaultsort( $parser, $text, $uarg = '' ) {
878 static $magicWords = null;
879 if ( is_null( $magicWords ) ) {
880 $magicWords = new MagicWordArray( array( 'defaultsort_noerror', 'defaultsort_noreplace' ) );
882 $arg = $magicWords->matchStartToEnd( $uarg );
884 $text = trim( $text );
885 if ( strlen( $text ) == 0 ) {
888 $old = $parser->getCustomDefaultSort();
889 if ( $old === false ||
$arg !== 'defaultsort_noreplace' ) {
890 $parser->setDefaultSort( $text );
893 if ( $old === false ||
$old == $text ||
$arg ) {
896 $converter = $parser->getConverterLanguage()->getConverter();
897 return '<span class="error">' .
898 wfMessage( 'duplicate-defaultsort',
899 // Message should be parsed, but these params should only be escaped.
900 $converter->markNoConversion( wfEscapeWikiText( $old ) ),
901 $converter->markNoConversion( wfEscapeWikiText( $text ) )
902 )->inContentLanguage()->text() .
908 * Usage {{filepath|300}}, {{filepath|nowiki}}, {{filepath|nowiki|300}}
909 * or {{filepath|300|nowiki}} or {{filepath|300px}}, {{filepath|200x300px}},
910 * {{filepath|nowiki|200x300px}}, {{filepath|200x300px|nowiki}}.
912 * @param Parser $parser
913 * @param string $name
914 * @param string $argA
915 * @param string $argB
916 * @return array|string
918 public static function filepath( $parser, $name = '', $argA = '', $argB = '' ) {
919 $file = wfFindFile( $name );
921 if ( $argA == 'nowiki' ) {
922 // {{filepath: | option [| size] }}
924 $parsedWidthParam = $parser->parseWidthParam( $argB );
926 // {{filepath: [| size [|option]] }}
927 $parsedWidthParam = $parser->parseWidthParam( $argA );
928 $isNowiki = ( $argB == 'nowiki' );
932 $url = $file->getFullUrl();
934 // If a size is requested...
935 if ( count( $parsedWidthParam ) ) {
936 $mto = $file->transform( $parsedWidthParam );
938 if ( $mto && !$mto->isError() ) {
939 // ... change the URL to point to a thumbnail.
940 $url = wfExpandUrl( $mto->getUrl(), PROTO_RELATIVE
);
944 return array( $url, 'nowiki' => true );
953 * Parser function to extension tag adaptor
954 * @param Parser $parser
955 * @param PPFrame $frame
956 * @param PPNode[] $args
959 public static function tagObj( $parser, $frame, $args ) {
960 if ( !count( $args ) ) {
963 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
965 if ( count( $args ) ) {
966 $inner = $frame->expand( array_shift( $args ) );
971 $attributes = array();
972 foreach ( $args as $arg ) {
973 $bits = $arg->splitArg();
974 if ( strval( $bits['index'] ) === '' ) {
975 $name = trim( $frame->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
976 $value = trim( $frame->expand( $bits['value'] ) );
977 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
978 $value = isset( $m[1] ) ?
$m[1] : '';
980 $attributes[$name] = $value;
984 $stripList = $parser->getStripList();
985 if ( !in_array( $tagName, $stripList ) ) {
986 // we can't handle this tag (at least not now), so just re-emit it as an ordinary tag
988 foreach ( $attributes as $name => $value ) {
989 $attrText .= ' ' . htmlspecialchars( $name ) . '="' . htmlspecialchars( $value ) . '"';
991 if ( $inner === null ) {
992 return "<$tagName$attrText/>";
994 return "<$tagName$attrText>$inner</$tagName>";
1000 'attributes' => $attributes,
1001 'close' => "</$tagName>",
1003 return $parser->extensionSubstitution( $params, $frame );
1007 * Fetched the current revision of the given title and return this.
1008 * Will increment the expensive function count and
1009 * add a template link to get the value refreshed on changes.
1010 * For a given title, which is equal to the current parser title,
1011 * the revision object from the parser is used, when that is the current one
1013 * @param Parser $parser
1014 * @param Title $title
1018 private static function getCachedRevisionObject( $parser, $title = null ) {
1019 if ( is_null( $title ) ) {
1023 // Use the revision from the parser itself, when param is the current page
1024 // and the revision is the current one
1025 if ( $title->equals( $parser->getTitle() ) ) {
1026 $parserRev = $parser->getRevisionObject();
1027 if ( $parserRev && $parserRev->isCurrent() ) {
1028 // force reparse after edit with vary-revision flag
1029 $parser->getOutput()->setFlag( 'vary-revision' );
1030 wfDebug( __METHOD__
. ": use current revision from parser, setting vary-revision...\n" );
1035 // Normalize name for cache
1036 $page = $title->getPrefixedDBkey();
1038 if ( !( $parser->currentRevisionCache
&& $parser->currentRevisionCache
->has( $page ) )
1039 && !$parser->incrementExpensiveFunctionCount() ) {
1042 $rev = $parser->fetchCurrentRevisionOfTitle( $title );
1043 $pageID = $rev ?
$rev->getPage() : 0;
1044 $revID = $rev ?
$rev->getId() : 0;
1046 // Register dependency in templatelinks
1047 $parser->getOutput()->addTemplate( $title, $pageID, $revID );
1053 * Get the pageid of a specified page
1054 * @param Parser $parser
1055 * @param string $title Title to get the pageid from
1056 * @return int|null|string
1059 public static function pageid( $parser, $title = null ) {
1060 $t = Title
::newFromText( $title );
1061 if ( is_null( $t ) ) {
1064 // Use title from parser to have correct pageid after edit
1065 if ( $t->equals( $parser->getTitle() ) ) {
1066 $t = $parser->getTitle();
1067 return $t->getArticleID();
1070 // These can't have ids
1071 if ( !$t->canExist() ||
$t->isExternal() ) {
1075 // Check the link cache, maybe something already looked it up.
1076 $linkCache = LinkCache
::singleton();
1077 $pdbk = $t->getPrefixedDBkey();
1078 $id = $linkCache->getGoodLinkID( $pdbk );
1080 $parser->mOutput
->addLink( $t, $id );
1083 if ( $linkCache->isBadLink( $pdbk ) ) {
1084 $parser->mOutput
->addLink( $t, 0 );
1088 // We need to load it from the DB, so mark expensive
1089 if ( $parser->incrementExpensiveFunctionCount() ) {
1090 $id = $t->getArticleID();
1091 $parser->mOutput
->addLink( $t, $id );
1098 * Get the id from the last revision of a specified page.
1099 * @param Parser $parser
1100 * @param string $title Title to get the id from
1101 * @return int|null|string
1104 public static function revisionid( $parser, $title = null ) {
1105 $t = Title
::newFromText( $title );
1106 if ( is_null( $t ) ) {
1109 // fetch revision from cache/database and return the value
1110 $rev = self
::getCachedRevisionObject( $parser, $t );
1111 return $rev ?
$rev->getId() : '';
1115 * Get the day from the last revision of a specified page.
1116 * @param Parser $parser
1117 * @param string $title Title to get the day from
1121 public static function revisionday( $parser, $title = null ) {
1122 $t = Title
::newFromText( $title );
1123 if ( is_null( $t ) ) {
1126 // fetch revision from cache/database and return the value
1127 $rev = self
::getCachedRevisionObject( $parser, $t );
1128 return $rev ? MWTimestamp
::getLocalInstance( $rev->getTimestamp() )->format( 'j' ) : '';
1132 * Get the day with leading zeros from the last revision of a specified page.
1133 * @param Parser $parser
1134 * @param string $title Title to get the day from
1138 public static function revisionday2( $parser, $title = null ) {
1139 $t = Title
::newFromText( $title );
1140 if ( is_null( $t ) ) {
1143 // fetch revision from cache/database and return the value
1144 $rev = self
::getCachedRevisionObject( $parser, $t );
1145 return $rev ? MWTimestamp
::getLocalInstance( $rev->getTimestamp() )->format( 'd' ) : '';
1149 * Get the month with leading zeros from the last revision of a specified page.
1150 * @param Parser $parser
1151 * @param string $title Title to get the month from
1155 public static function revisionmonth( $parser, $title = null ) {
1156 $t = Title
::newFromText( $title );
1157 if ( is_null( $t ) ) {
1160 // fetch revision from cache/database and return the value
1161 $rev = self
::getCachedRevisionObject( $parser, $t );
1162 return $rev ? MWTimestamp
::getLocalInstance( $rev->getTimestamp() )->format( 'm' ) : '';
1166 * Get the month from the last revision of a specified page.
1167 * @param Parser $parser
1168 * @param string $title Title to get the month from
1172 public static function revisionmonth1( $parser, $title = null ) {
1173 $t = Title
::newFromText( $title );
1174 if ( is_null( $t ) ) {
1177 // fetch revision from cache/database and return the value
1178 $rev = self
::getCachedRevisionObject( $parser, $t );
1179 return $rev ? MWTimestamp
::getLocalInstance( $rev->getTimestamp() )->format( 'n' ) : '';
1183 * Get the year from the last revision of a specified page.
1184 * @param Parser $parser
1185 * @param string $title Title to get the year from
1189 public static function revisionyear( $parser, $title = null ) {
1190 $t = Title
::newFromText( $title );
1191 if ( is_null( $t ) ) {
1194 // fetch revision from cache/database and return the value
1195 $rev = self
::getCachedRevisionObject( $parser, $t );
1196 return $rev ? MWTimestamp
::getLocalInstance( $rev->getTimestamp() )->format( 'Y' ) : '';
1200 * Get the timestamp from the last revision of a specified page.
1201 * @param Parser $parser
1202 * @param string $title Title to get the timestamp from
1206 public static function revisiontimestamp( $parser, $title = null ) {
1207 $t = Title
::newFromText( $title );
1208 if ( is_null( $t ) ) {
1211 // fetch revision from cache/database and return the value
1212 $rev = self
::getCachedRevisionObject( $parser, $t );
1213 return $rev ? MWTimestamp
::getLocalInstance( $rev->getTimestamp() )->format( 'YmdHis' ) : '';
1217 * Get the user from the last revision of a specified page.
1218 * @param Parser $parser
1219 * @param string $title Title to get the user from
1223 public static function revisionuser( $parser, $title = null ) {
1224 $t = Title
::newFromText( $title );
1225 if ( is_null( $t ) ) {
1228 // fetch revision from cache/database and return the value
1229 $rev = self
::getCachedRevisionObject( $parser, $t );
1230 return $rev ?
$rev->getUserText() : '';
1234 * Returns the sources of any cascading protection acting on a specified page.
1235 * Pages will not return their own title unless they transclude themselves.
1236 * This is an expensive parser function and can't be called too many times per page,
1237 * unless cascading protection sources for the page have already been loaded.
1239 * @param Parser $parser
1240 * @param string $title
1245 public static function cascadingsources( $parser, $title = '' ) {
1246 $titleObject = Title
::newFromText( $title );
1247 if ( !( $titleObject instanceof Title
) ) {
1248 $titleObject = $parser->mTitle
;
1250 if ( $titleObject->areCascadeProtectionSourcesLoaded()
1251 ||
$parser->incrementExpensiveFunctionCount()
1254 $sources = $titleObject->getCascadeProtectionSources();
1255 foreach ( $sources[0] as $sourceTitle ) {
1256 $names[] = $sourceTitle->getPrefixedText();
1258 return implode( $names, '|' );