3 * Parser functions provided by MediaWiki core
9 * Various core parser functions, registered in Parser::firstCallInit()
12 class CoreParserFunctions
{
14 * @param $parser Parser
17 static function register( $parser ) {
18 global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
20 # Syntax for arguments (see self::setFunctionHook):
21 # "name for lookup in localized magic words array",
23 # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}}
24 # instead of {{#int:...}})
26 $parser->setFunctionHook( 'int', array( __CLASS__
, 'intFunction' ), SFH_NO_HASH
);
27 $parser->setFunctionHook( 'ns', array( __CLASS__
, 'ns' ), SFH_NO_HASH
);
28 $parser->setFunctionHook( 'nse', array( __CLASS__
, 'nse' ), SFH_NO_HASH
);
29 $parser->setFunctionHook( 'urlencode', array( __CLASS__
, 'urlencode' ), SFH_NO_HASH
);
30 $parser->setFunctionHook( 'lcfirst', array( __CLASS__
, 'lcfirst' ), SFH_NO_HASH
);
31 $parser->setFunctionHook( 'ucfirst', array( __CLASS__
, 'ucfirst' ), SFH_NO_HASH
);
32 $parser->setFunctionHook( 'lc', array( __CLASS__
, 'lc' ), SFH_NO_HASH
);
33 $parser->setFunctionHook( 'uc', array( __CLASS__
, 'uc' ), SFH_NO_HASH
);
34 $parser->setFunctionHook( 'localurl', array( __CLASS__
, 'localurl' ), SFH_NO_HASH
);
35 $parser->setFunctionHook( 'localurle', array( __CLASS__
, 'localurle' ), SFH_NO_HASH
);
36 $parser->setFunctionHook( 'fullurl', array( __CLASS__
, 'fullurl' ), SFH_NO_HASH
);
37 $parser->setFunctionHook( 'fullurle', array( __CLASS__
, 'fullurle' ), SFH_NO_HASH
);
38 $parser->setFunctionHook( 'canonicalurl', array( __CLASS__
, 'canonicalurl' ), SFH_NO_HASH
);
39 $parser->setFunctionHook( 'canonicalurle', array( __CLASS__
, 'canonicalurle' ), SFH_NO_HASH
);
40 $parser->setFunctionHook( 'formatnum', array( __CLASS__
, 'formatnum' ), SFH_NO_HASH
);
41 $parser->setFunctionHook( 'grammar', array( __CLASS__
, 'grammar' ), SFH_NO_HASH
);
42 $parser->setFunctionHook( 'gender', array( __CLASS__
, 'gender' ), SFH_NO_HASH
);
43 $parser->setFunctionHook( 'plural', array( __CLASS__
, 'plural' ), SFH_NO_HASH
);
44 $parser->setFunctionHook( 'numberofpages', array( __CLASS__
, 'numberofpages' ), SFH_NO_HASH
);
45 $parser->setFunctionHook( 'numberofusers', array( __CLASS__
, 'numberofusers' ), SFH_NO_HASH
);
46 $parser->setFunctionHook( 'numberofactiveusers', array( __CLASS__
, 'numberofactiveusers' ), SFH_NO_HASH
);
47 $parser->setFunctionHook( 'numberofarticles', array( __CLASS__
, 'numberofarticles' ), SFH_NO_HASH
);
48 $parser->setFunctionHook( 'numberoffiles', array( __CLASS__
, 'numberoffiles' ), SFH_NO_HASH
);
49 $parser->setFunctionHook( 'numberofadmins', array( __CLASS__
, 'numberofadmins' ), SFH_NO_HASH
);
50 $parser->setFunctionHook( 'numberingroup', array( __CLASS__
, 'numberingroup' ), SFH_NO_HASH
);
51 $parser->setFunctionHook( 'numberofedits', array( __CLASS__
, 'numberofedits' ), SFH_NO_HASH
);
52 $parser->setFunctionHook( 'numberofviews', array( __CLASS__
, 'numberofviews' ), SFH_NO_HASH
);
53 $parser->setFunctionHook( 'language', array( __CLASS__
, 'language' ), SFH_NO_HASH
);
54 $parser->setFunctionHook( 'padleft', array( __CLASS__
, 'padleft' ), SFH_NO_HASH
);
55 $parser->setFunctionHook( 'padright', array( __CLASS__
, 'padright' ), SFH_NO_HASH
);
56 $parser->setFunctionHook( 'anchorencode', array( __CLASS__
, 'anchorencode' ), SFH_NO_HASH
);
57 $parser->setFunctionHook( 'special', array( __CLASS__
, 'special' ) );
58 $parser->setFunctionHook( 'defaultsort', array( __CLASS__
, 'defaultsort' ), SFH_NO_HASH
);
59 $parser->setFunctionHook( 'filepath', array( __CLASS__
, 'filepath' ), SFH_NO_HASH
);
60 $parser->setFunctionHook( 'pagesincategory', array( __CLASS__
, 'pagesincategory' ), SFH_NO_HASH
);
61 $parser->setFunctionHook( 'pagesize', array( __CLASS__
, 'pagesize' ), SFH_NO_HASH
);
62 $parser->setFunctionHook( 'protectionlevel', array( __CLASS__
, 'protectionlevel' ), SFH_NO_HASH
);
63 $parser->setFunctionHook( 'namespace', array( __CLASS__
, 'mwnamespace' ), SFH_NO_HASH
);
64 $parser->setFunctionHook( 'namespacee', array( __CLASS__
, 'namespacee' ), SFH_NO_HASH
);
65 $parser->setFunctionHook( 'talkspace', array( __CLASS__
, 'talkspace' ), SFH_NO_HASH
);
66 $parser->setFunctionHook( 'talkspacee', array( __CLASS__
, 'talkspacee' ), SFH_NO_HASH
);
67 $parser->setFunctionHook( 'subjectspace', array( __CLASS__
, 'subjectspace' ), SFH_NO_HASH
);
68 $parser->setFunctionHook( 'subjectspacee', array( __CLASS__
, 'subjectspacee' ), SFH_NO_HASH
);
69 $parser->setFunctionHook( 'pagename', array( __CLASS__
, 'pagename' ), SFH_NO_HASH
);
70 $parser->setFunctionHook( 'pagenamee', array( __CLASS__
, 'pagenamee' ), SFH_NO_HASH
);
71 $parser->setFunctionHook( 'fullpagename', array( __CLASS__
, 'fullpagename' ), SFH_NO_HASH
);
72 $parser->setFunctionHook( 'fullpagenamee', array( __CLASS__
, 'fullpagenamee' ), SFH_NO_HASH
);
73 $parser->setFunctionHook( 'basepagename', array( __CLASS__
, 'basepagename' ), SFH_NO_HASH
);
74 $parser->setFunctionHook( 'basepagenamee', array( __CLASS__
, 'basepagenamee' ), SFH_NO_HASH
);
75 $parser->setFunctionHook( 'subpagename', array( __CLASS__
, 'subpagename' ), SFH_NO_HASH
);
76 $parser->setFunctionHook( 'subpagenamee', array( __CLASS__
, 'subpagenamee' ), SFH_NO_HASH
);
77 $parser->setFunctionHook( 'talkpagename', array( __CLASS__
, 'talkpagename' ), SFH_NO_HASH
);
78 $parser->setFunctionHook( 'talkpagenamee', array( __CLASS__
, 'talkpagenamee' ), SFH_NO_HASH
);
79 $parser->setFunctionHook( 'subjectpagename', array( __CLASS__
, 'subjectpagename' ), SFH_NO_HASH
);
80 $parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__
, 'subjectpagenamee' ), SFH_NO_HASH
);
81 $parser->setFunctionHook( 'tag', array( __CLASS__
, 'tagObj' ), SFH_OBJECT_ARGS
);
82 $parser->setFunctionHook( 'formatdate', array( __CLASS__
, 'formatDate' ) );
84 if ( $wgAllowDisplayTitle ) {
85 $parser->setFunctionHook( 'displaytitle', array( __CLASS__
, 'displaytitle' ), SFH_NO_HASH
);
87 if ( $wgAllowSlowParserFunctions ) {
88 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__
, 'pagesinnamespace' ), SFH_NO_HASH
);
93 * @param $parser Parser
94 * @param string $part1
97 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
98 if ( strval( $part1 ) !== '' ) {
99 $args = array_slice( func_get_args(), 2 );
100 $message = wfMessage( $part1, $args )->inLanguage( $parser->getOptions()->getUserLangObj() )->plain();
101 return array( $message, 'noparse' => false );
103 return array( 'found' => false );
108 * @param $parser Parser
110 * @param null $defaultPref
111 * @return mixed|string
113 static function formatDate( $parser, $date, $defaultPref = null ) {
114 $df = DateFormatter
::getInstance();
116 $date = trim( $date );
118 $pref = $parser->getOptions()->getDateFormat();
120 // Specify a different default date format other than the the normal default
121 // iff the user has 'default' for their setting
122 if ( $pref == 'default' && $defaultPref )
123 $pref = $defaultPref;
125 $date = $df->reformat( $pref, $date, array( 'match-whole' ) );
129 static function ns( $parser, $part1 = '' ) {
131 if ( intval( $part1 ) ||
$part1 == "0" ) {
132 $index = intval( $part1 );
134 $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
136 if ( $index !== false ) {
137 return $wgContLang->getFormattedNsText( $index );
139 return array( 'found' => false );
143 static function nse( $parser, $part1 = '' ) {
144 return wfUrlencode( str_replace( ' ', '_', self
::ns( $parser, $part1 ) ) );
148 * urlencodes a string according to one of three patterns: (bug 22474)
150 * By default (for HTTP "query" strings), spaces are encoded as '+'.
151 * Or to encode a value for the HTTP "path", spaces are encoded as '%20'.
152 * For links to "wiki"s, or similar software, spaces are encoded as '_',
154 * @param $parser Parser object
155 * @param $s String: The text to encode.
156 * @param $arg String (optional): The type of encoding.
158 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 return wfUrlencode( str_replace( ' ', '_', $s ) );
169 // Encode for an HTTP Path, '%20' for ' '.
171 return rawurlencode( $s );
173 // Encode for HTTP query, '+' for ' '.
176 return urlencode( $s );
180 static function lcfirst( $parser, $s = '' ) {
182 return $wgContLang->lcfirst( $s );
185 static function ucfirst( $parser, $s = '' ) {
187 return $wgContLang->ucfirst( $s );
191 * @param $parser Parser
195 static function lc( $parser, $s = '' ) {
197 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
198 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
200 return $wgContLang->lc( $s );
205 * @param $parser Parser
209 static function uc( $parser, $s = '' ) {
211 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
212 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
214 return $wgContLang->uc( $s );
218 static function localurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getLocalURL', $s, $arg ); }
219 static function localurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeLocalURL', $s, $arg ); }
220 static function fullurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getFullURL', $s, $arg ); }
221 static function fullurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeFullURL', $s, $arg ); }
222 static function canonicalurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getCanonicalURL', $s, $arg ); }
223 static function canonicalurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeCanonicalURL', $s, $arg ); }
225 static function urlFunction( $func, $s = '', $arg = null ) {
226 $title = Title
::newFromText( $s );
227 # Due to order of execution of a lot of bits, the values might be encoded
228 # before arriving here; if that's true, then the title can't be created
229 # and the variable will fail. If we can't get a decent title from the first
230 # attempt, url-decode and try for a second.
231 if( is_null( $title ) )
232 $title = Title
::newFromURL( urldecode( $s ) );
233 if( !is_null( $title ) ) {
234 # Convert NS_MEDIA -> NS_FILE
235 if( $title->getNamespace() == NS_MEDIA
) {
236 $title = Title
::makeTitle( NS_FILE
, $title->getDBkey() );
238 if( !is_null( $arg ) ) {
239 $text = $title->$func( $arg );
241 $text = $title->$func();
245 return array( 'found' => false );
250 * @param $parser Parser
255 static function formatNum( $parser, $num = '', $raw = null) {
256 if ( self
::israw( $raw ) ) {
257 return $parser->getFunctionLang()->parseFormattedNumber( $num );
259 return $parser->getFunctionLang()->formatNum( $num );
264 * @param $parser Parser
265 * @param string $case
266 * @param string $word
269 static function grammar( $parser, $case = '', $word = '' ) {
270 return $parser->getFunctionLang()->convertGrammar( $word, $case );
274 * @param $parser Parser
275 * @param $username string
278 static function gender( $parser, $username ) {
279 wfProfileIn( __METHOD__
);
280 $forms = array_slice( func_get_args(), 2 );
282 // Some shortcuts to avoid loading user data unnecessarily
283 if ( count( $forms ) === 0 ) {
285 } elseif ( count( $forms ) === 1 ) {
289 $username = trim( $username );
292 $gender = User
::getDefaultOption( 'gender' );
295 $title = Title
::newFromText( $username );
297 if ( $title && $title->getNamespace() == NS_USER
) {
298 $username = $title->getText();
301 // check parameter, or use the ParserOptions if in interface message
302 $user = User
::newFromName( $username );
304 $gender = $user->getOption( 'gender' );
305 } elseif ( $username === '' && $parser->getOptions()->getInterfaceMessage() ) {
306 $gender = $parser->getOptions()->getUser()->getOption( 'gender' );
308 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
309 wfProfileOut( __METHOD__
);
314 * @param $parser Parser
315 * @param string $text
318 static function plural( $parser, $text = '' ) {
319 $forms = array_slice( func_get_args(), 2 );
320 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
321 return $parser->getFunctionLang()->convertPlural( $text, $forms );
325 * Override the title of the page when viewed, provided we've been given a
326 * title which will normalise to the canonical title
328 * @param $parser Parser: parent parser
329 * @param $text String: desired title text
332 static function displaytitle( $parser, $text = '' ) {
333 global $wgRestrictDisplayTitle;
335 #parse a limited subset of wiki markup (just the single quote items)
336 $text = $parser->doQuotes( $text );
338 #remove stripped text (e.g. the UNIQ-QINU stuff) that was generated by tag extensions/whatever
339 $text = preg_replace( '/' . preg_quote( $parser->uniqPrefix(), '/' ) . '.*?'
340 . preg_quote( Parser
::MARKER_SUFFIX
, '/' ) . '/', '', $text );
342 #list of disallowed tags for DISPLAYTITLE
343 #these will be escaped even though they are allowed in normal wiki text
344 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr',
345 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp', 'br' );
347 #only requested titles that normalize to the actual title are allowed through
348 #if $wgRestrictDisplayTitle is true (it is by default)
349 #mimic the escaping process that occurs in OutputPage::setPageTitle
350 $text = Sanitizer
::normalizeCharReferences( Sanitizer
::removeHTMLtags( $text, null, array(), array(), $bad ) );
351 $title = Title
::newFromText( Sanitizer
::stripAllTags( $text ) );
353 if( !$wgRestrictDisplayTitle ) {
354 $parser->mOutput
->setDisplayTitle( $text );
356 if ( $title instanceof Title
&& $title->getFragment() == '' && $title->equals( $parser->mTitle
) ) {
357 $parser->mOutput
->setDisplayTitle( $text );
364 static function isRaw( $param ) {
367 $mwRaw =& MagicWord
::get( 'rawsuffix' );
369 if ( is_null( $param ) ) {
372 return $mwRaw->match( $param );
376 static function formatRaw( $num, $raw ) {
377 if( self
::isRaw( $raw ) ) {
381 return $wgContLang->formatNum( $num );
384 static function numberofpages( $parser, $raw = null ) {
385 return self
::formatRaw( SiteStats
::pages(), $raw );
387 static function numberofusers( $parser, $raw = null ) {
388 return self
::formatRaw( SiteStats
::users(), $raw );
390 static function numberofactiveusers( $parser, $raw = null ) {
391 return self
::formatRaw( SiteStats
::activeUsers(), $raw );
393 static function numberofarticles( $parser, $raw = null ) {
394 return self
::formatRaw( SiteStats
::articles(), $raw );
396 static function numberoffiles( $parser, $raw = null ) {
397 return self
::formatRaw( SiteStats
::images(), $raw );
399 static function numberofadmins( $parser, $raw = null ) {
400 return self
::formatRaw( SiteStats
::numberingroup('sysop'), $raw );
402 static function numberofedits( $parser, $raw = null ) {
403 return self
::formatRaw( SiteStats
::edits(), $raw );
405 static function numberofviews( $parser, $raw = null ) {
406 return self
::formatRaw( SiteStats
::views(), $raw );
408 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
409 return self
::formatRaw( SiteStats
::pagesInNs( intval( $namespace ) ), $raw );
411 static function numberingroup( $parser, $name = '', $raw = null) {
412 return self
::formatRaw( SiteStats
::numberingroup( strtolower( $name ) ), $raw );
417 * Given a title, return the namespace name that would be given by the
418 * corresponding magic word
419 * Note: function name changed to "mwnamespace" rather than "namespace"
420 * to not break PHP 5.3
422 static function mwnamespace( $parser, $title = null ) {
423 $t = Title
::newFromText( $title );
426 return str_replace( '_', ' ', $t->getNsText() );
428 static function namespacee( $parser, $title = null ) {
429 $t = Title
::newFromText( $title );
432 return wfUrlencode( $t->getNsText() );
434 static function talkspace( $parser, $title = null ) {
435 $t = Title
::newFromText( $title );
436 if ( is_null( $t ) ||
!$t->canTalk() )
438 return str_replace( '_', ' ', $t->getTalkNsText() );
440 static function talkspacee( $parser, $title = null ) {
441 $t = Title
::newFromText( $title );
442 if ( is_null( $t ) ||
!$t->canTalk() )
444 return wfUrlencode( $t->getTalkNsText() );
446 static function subjectspace( $parser, $title = null ) {
447 $t = Title
::newFromText( $title );
450 return str_replace( '_', ' ', $t->getSubjectNsText() );
452 static function subjectspacee( $parser, $title = null ) {
453 $t = Title
::newFromText( $title );
456 return wfUrlencode( $t->getSubjectNsText() );
460 * Functions to get and normalize pagenames, corresponding to the magic words
463 static function pagename( $parser, $title = null ) {
464 $t = Title
::newFromText( $title );
467 return wfEscapeWikiText( $t->getText() );
469 static function pagenamee( $parser, $title = null ) {
470 $t = Title
::newFromText( $title );
473 return wfEscapeWikiText( $t->getPartialURL() );
475 static function fullpagename( $parser, $title = null ) {
476 $t = Title
::newFromText( $title );
477 if ( is_null( $t ) ||
!$t->canTalk() )
479 return wfEscapeWikiText( $t->getPrefixedText() );
481 static function fullpagenamee( $parser, $title = null ) {
482 $t = Title
::newFromText( $title );
483 if ( is_null( $t ) ||
!$t->canTalk() )
485 return wfEscapeWikiText( $t->getPrefixedURL() );
487 static function subpagename( $parser, $title = null ) {
488 $t = Title
::newFromText( $title );
491 return wfEscapeWikiText( $t->getSubpageText() );
493 static function subpagenamee( $parser, $title = null ) {
494 $t = Title
::newFromText( $title );
497 return wfEscapeWikiText( $t->getSubpageUrlForm() );
499 static function basepagename( $parser, $title = null ) {
500 $t = Title
::newFromText( $title );
503 return wfEscapeWikiText( $t->getBaseText() );
505 static function basepagenamee( $parser, $title = null ) {
506 $t = Title
::newFromText( $title );
509 return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) ) );
511 static function talkpagename( $parser, $title = null ) {
512 $t = Title
::newFromText( $title );
513 if ( is_null( $t ) ||
!$t->canTalk() )
515 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
517 static function talkpagenamee( $parser, $title = null ) {
518 $t = Title
::newFromText( $title );
519 if ( is_null( $t ) ||
!$t->canTalk() )
521 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedUrl() );
523 static function subjectpagename( $parser, $title = null ) {
524 $t = Title
::newFromText( $title );
527 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
529 static function subjectpagenamee( $parser, $title = null ) {
530 $t = Title
::newFromText( $title );
533 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedUrl() );
537 * Return the number of pages in the given category, or 0 if it's nonexis-
538 * tent. This is an expensive parser function and can't be called too many
541 static function pagesincategory( $parser, $name = '', $raw = null ) {
542 static $cache = array();
543 $category = Category
::newFromName( $name );
545 if( !is_object( $category ) ) {
547 return self
::formatRaw( 0, $raw );
550 # Normalize name for cache
551 $name = $category->getName();
554 if( isset( $cache[$name] ) ) {
555 $count = $cache[$name];
556 } elseif( $parser->incrementExpensiveFunctionCount() ) {
557 $count = $cache[$name] = (int)$category->getPageCount();
559 return self
::formatRaw( $count, $raw );
563 * Return the size of the given page, or 0 if it's nonexistent. This is an
564 * expensive parser function and can't be called too many times per page.
566 * @todo FIXME: This doesn't work correctly on preview for getting the size
567 * of the current page.
568 * @todo FIXME: Title::getLength() documentation claims that it adds things
569 * to the link cache, so the local cache here should be unnecessary, but
570 * in fact calling getLength() repeatedly for the same $page does seem to
571 * run one query for each call?
572 * @todo Document parameters
574 * @param $parser Parser
575 * @param $page String TODO DOCUMENT (Default: empty string)
576 * @param $raw TODO DOCUMENT (Default: null)
578 static function pagesize( $parser, $page = '', $raw = null ) {
579 static $cache = array();
580 $title = Title
::newFromText( $page );
582 if( !is_object( $title ) ) {
584 return self
::formatRaw( 0, $raw );
587 # Normalize name for cache
588 $page = $title->getPrefixedText();
591 if( isset( $cache[$page] ) ) {
592 $length = $cache[$page];
593 } elseif( $parser->incrementExpensiveFunctionCount() ) {
594 $rev = Revision
::newFromTitle( $title );
595 $id = $rev ?
$rev->getPage() : 0;
596 $length = $cache[$page] = $rev ?
$rev->getSize() : 0;
598 // Register dependency in templatelinks
599 $parser->mOutput
->addTemplate( $title, $id, $rev ?
$rev->getId() : 0 );
601 return self
::formatRaw( $length, $raw );
605 * Returns the requested protection level for the current page
607 static function protectionlevel( $parser, $type = '' ) {
608 $restrictions = $parser->mTitle
->getRestrictions( strtolower( $type ) );
609 # Title::getRestrictions returns an array, its possible it may have
610 # multiple values in the future
611 return implode( $restrictions, ',' );
615 * Gives language names.
616 * @param $parser Parser
617 * @param $code String Language code
618 * @param $language String Language code
621 static function language( $parser, $code = '', $language = '' ) {
623 $code = strtolower( $code );
624 $language = strtolower( $language );
626 if ( $language !== '' ) {
627 $names = Language
::getTranslatedLanguageNames( $language );
628 return isset( $names[$code] ) ?
$names[$code] : wfBCP47( $code );
631 $lang = $wgContLang->getLanguageName( $code );
632 return $lang !== '' ?
$lang : wfBCP47( $code );
636 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
638 static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT
) {
639 $lengthOfPadding = mb_strlen( $padding );
640 if ( $lengthOfPadding == 0 ) return $string;
642 # The remaining length to add counts down to 0 as padding is added
643 $length = min( $length, 500 ) - mb_strlen( $string );
644 # $finalPadding is just $padding repeated enough times so that
645 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
647 while ( $length > 0 ) {
648 # If $length < $lengthofPadding, truncate $padding so we get the
649 # exact length desired.
650 $finalPadding .= mb_substr( $padding, 0, $length );
651 $length -= $lengthOfPadding;
654 if ( $direction == STR_PAD_LEFT
) {
655 return $finalPadding . $string;
657 return $string . $finalPadding;
661 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
662 return self
::pad( $string, $length, $padding, STR_PAD_LEFT
);
665 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
666 return self
::pad( $string, $length, $padding );
670 * @param $parser Parser
674 static function anchorencode( $parser, $text ) {
675 return substr( $parser->guessSectionNameFromWikiText( $text ), 1);
678 static function special( $parser, $text ) {
679 list( $page, $subpage ) = SpecialPageFactory
::resolveAlias( $text );
681 $title = SpecialPage
::getTitleFor( $page, $subpage );
684 return wfMsgForContent( 'nosuchspecialpage' );
689 * @param $parser Parser
690 * @param $text String The sortkey to use
691 * @param $uarg String Either "noreplace" or "noerror" (in en)
692 * both suppress errors, and noreplace does nothing if
693 * a default sortkey already exists.
696 public static function defaultsort( $parser, $text, $uarg = '' ) {
697 static $magicWords = null;
698 if ( is_null( $magicWords ) ) {
699 $magicWords = new MagicWordArray( array( 'defaultsort_noerror', 'defaultsort_noreplace' ) );
701 $arg = $magicWords->matchStartToEnd( $uarg );
703 $text = trim( $text );
704 if( strlen( $text ) == 0 )
706 $old = $parser->getCustomDefaultSort();
707 if ( $old === false ||
$arg !== 'defaultsort_noreplace' ) {
708 $parser->setDefaultSort( $text );
711 if( $old === false ||
$old == $text ||
$arg ) {
714 return( '<span class="error">' .
715 wfMsgForContent( 'duplicate-defaultsort',
716 htmlspecialchars( $old ),
717 htmlspecialchars( $text ) ) .
722 // Usage {{filepath|300}}, {{filepath|nowiki}}, {{filepath|nowiki|300}} or {{filepath|300|nowiki}}
723 public static function filepath( $parser, $name='', $argA='', $argB='' ) {
724 $file = wfFindFile( $name );
726 $argA_int = intval( $argA );
727 $argB_int = intval( $argB );
729 if ( $argB_int > 0 ) {
730 // {{filepath: | option | size }}
734 } elseif ( $argA_int > 0 ) {
735 // {{filepath: | size [|option] }}
740 // {{filepath: [|option] }}
745 $url = $file->getFullUrl();
747 // If a size is requested...
748 if ( is_integer( $size ) ) {
749 $mto = $file->transform( array( 'width' => $size ) );
751 if ( $mto && !$mto->isError() ) {
752 // ... change the URL to point to a thumbnail.
753 $url = wfExpandUrl( $mto->getUrl(), PROTO_RELATIVE
);
756 if ( $option == 'nowiki' ) {
757 return array( $url, 'nowiki' => true );
766 * Parser function to extension tag adaptor
768 public static function tagObj( $parser, $frame, $args ) {
769 if ( !count( $args ) ) {
772 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
774 if ( count( $args ) ) {
775 $inner = $frame->expand( array_shift( $args ) );
780 $stripList = $parser->getStripList();
781 if ( !in_array( $tagName, $stripList ) ) {
782 return '<span class="error">' .
783 wfMsgForContent( 'unknown_extension_tag', $tagName ) .
787 $attributes = array();
788 foreach ( $args as $arg ) {
789 $bits = $arg->splitArg();
790 if ( strval( $bits['index'] ) === '' ) {
791 $name = trim( $frame->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
792 $value = trim( $frame->expand( $bits['value'] ) );
793 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
794 $value = isset( $m[1] ) ?
$m[1] : '';
796 $attributes[$name] = $value;
803 'attributes' => $attributes,
804 'close' => "</$tagName>",
806 return $parser->extensionSubstitution( $params, $frame );