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 $username = trim( $username );
285 $gender = User
::getDefaultOption( 'gender' );
288 $title = Title
::newFromText( $username );
290 if ( $title && $title->getNamespace() == NS_USER
) {
291 $username = $title->getText();
294 // check parameter, or use the ParserOptions if in interface message
295 $user = User
::newFromName( $username );
297 $gender = $user->getOption( 'gender' );
298 } elseif ( $username === '' && $parser->getOptions()->getInterfaceMessage() ) {
299 $gender = $parser->getOptions()->getUser()->getOption( 'gender' );
301 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
302 wfProfileOut( __METHOD__
);
307 * @param $parser Parser
308 * @param string $text
311 static function plural( $parser, $text = '' ) {
312 $forms = array_slice( func_get_args(), 2 );
313 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
314 return $parser->getFunctionLang()->convertPlural( $text, $forms );
318 * Override the title of the page when viewed, provided we've been given a
319 * title which will normalise to the canonical title
321 * @param $parser Parser: parent parser
322 * @param $text String: desired title text
325 static function displaytitle( $parser, $text = '' ) {
326 global $wgRestrictDisplayTitle;
328 #parse a limited subset of wiki markup (just the single quote items)
329 $text = $parser->doQuotes( $text );
331 #remove stripped text (e.g. the UNIQ-QINU stuff) that was generated by tag extensions/whatever
332 $text = preg_replace( '/' . preg_quote( $parser->uniqPrefix(), '/' ) . '.*?'
333 . preg_quote( Parser
::MARKER_SUFFIX
, '/' ) . '/', '', $text );
335 #list of disallowed tags for DISPLAYTITLE
336 #these will be escaped even though they are allowed in normal wiki text
337 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr',
338 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp', 'br' );
340 #only requested titles that normalize to the actual title are allowed through
341 #if $wgRestrictDisplayTitle is true (it is by default)
342 #mimic the escaping process that occurs in OutputPage::setPageTitle
343 $text = Sanitizer
::normalizeCharReferences( Sanitizer
::removeHTMLtags( $text, null, array(), array(), $bad ) );
344 $title = Title
::newFromText( Sanitizer
::stripAllTags( $text ) );
346 if( !$wgRestrictDisplayTitle ) {
347 $parser->mOutput
->setDisplayTitle( $text );
349 if ( $title instanceof Title
&& $title->getFragment() == '' && $title->equals( $parser->mTitle
) ) {
350 $parser->mOutput
->setDisplayTitle( $text );
357 static function isRaw( $param ) {
360 $mwRaw =& MagicWord
::get( 'rawsuffix' );
362 if ( is_null( $param ) ) {
365 return $mwRaw->match( $param );
369 static function formatRaw( $num, $raw ) {
370 if( self
::isRaw( $raw ) ) {
374 return $wgContLang->formatNum( $num );
377 static function numberofpages( $parser, $raw = null ) {
378 return self
::formatRaw( SiteStats
::pages(), $raw );
380 static function numberofusers( $parser, $raw = null ) {
381 return self
::formatRaw( SiteStats
::users(), $raw );
383 static function numberofactiveusers( $parser, $raw = null ) {
384 return self
::formatRaw( SiteStats
::activeUsers(), $raw );
386 static function numberofarticles( $parser, $raw = null ) {
387 return self
::formatRaw( SiteStats
::articles(), $raw );
389 static function numberoffiles( $parser, $raw = null ) {
390 return self
::formatRaw( SiteStats
::images(), $raw );
392 static function numberofadmins( $parser, $raw = null ) {
393 return self
::formatRaw( SiteStats
::numberingroup('sysop'), $raw );
395 static function numberofedits( $parser, $raw = null ) {
396 return self
::formatRaw( SiteStats
::edits(), $raw );
398 static function numberofviews( $parser, $raw = null ) {
399 return self
::formatRaw( SiteStats
::views(), $raw );
401 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
402 return self
::formatRaw( SiteStats
::pagesInNs( intval( $namespace ) ), $raw );
404 static function numberingroup( $parser, $name = '', $raw = null) {
405 return self
::formatRaw( SiteStats
::numberingroup( strtolower( $name ) ), $raw );
410 * Given a title, return the namespace name that would be given by the
411 * corresponding magic word
412 * Note: function name changed to "mwnamespace" rather than "namespace"
413 * to not break PHP 5.3
415 static function mwnamespace( $parser, $title = null ) {
416 $t = Title
::newFromText( $title );
419 return str_replace( '_', ' ', $t->getNsText() );
421 static function namespacee( $parser, $title = null ) {
422 $t = Title
::newFromText( $title );
425 return wfUrlencode( $t->getNsText() );
427 static function talkspace( $parser, $title = null ) {
428 $t = Title
::newFromText( $title );
429 if ( is_null( $t ) ||
!$t->canTalk() )
431 return str_replace( '_', ' ', $t->getTalkNsText() );
433 static function talkspacee( $parser, $title = null ) {
434 $t = Title
::newFromText( $title );
435 if ( is_null( $t ) ||
!$t->canTalk() )
437 return wfUrlencode( $t->getTalkNsText() );
439 static function subjectspace( $parser, $title = null ) {
440 $t = Title
::newFromText( $title );
443 return str_replace( '_', ' ', $t->getSubjectNsText() );
445 static function subjectspacee( $parser, $title = null ) {
446 $t = Title
::newFromText( $title );
449 return wfUrlencode( $t->getSubjectNsText() );
453 * Functions to get and normalize pagenames, corresponding to the magic words
456 static function pagename( $parser, $title = null ) {
457 $t = Title
::newFromText( $title );
460 return wfEscapeWikiText( $t->getText() );
462 static function pagenamee( $parser, $title = null ) {
463 $t = Title
::newFromText( $title );
466 return wfEscapeWikiText( $t->getPartialURL() );
468 static function fullpagename( $parser, $title = null ) {
469 $t = Title
::newFromText( $title );
470 if ( is_null( $t ) ||
!$t->canTalk() )
472 return wfEscapeWikiText( $t->getPrefixedText() );
474 static function fullpagenamee( $parser, $title = null ) {
475 $t = Title
::newFromText( $title );
476 if ( is_null( $t ) ||
!$t->canTalk() )
478 return wfEscapeWikiText( $t->getPrefixedURL() );
480 static function subpagename( $parser, $title = null ) {
481 $t = Title
::newFromText( $title );
484 return wfEscapeWikiText( $t->getSubpageText() );
486 static function subpagenamee( $parser, $title = null ) {
487 $t = Title
::newFromText( $title );
490 return wfEscapeWikiText( $t->getSubpageUrlForm() );
492 static function basepagename( $parser, $title = null ) {
493 $t = Title
::newFromText( $title );
496 return wfEscapeWikiText( $t->getBaseText() );
498 static function basepagenamee( $parser, $title = null ) {
499 $t = Title
::newFromText( $title );
502 return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) ) );
504 static function talkpagename( $parser, $title = null ) {
505 $t = Title
::newFromText( $title );
506 if ( is_null( $t ) ||
!$t->canTalk() )
508 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
510 static function talkpagenamee( $parser, $title = null ) {
511 $t = Title
::newFromText( $title );
512 if ( is_null( $t ) ||
!$t->canTalk() )
514 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedUrl() );
516 static function subjectpagename( $parser, $title = null ) {
517 $t = Title
::newFromText( $title );
520 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
522 static function subjectpagenamee( $parser, $title = null ) {
523 $t = Title
::newFromText( $title );
526 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedUrl() );
530 * Return the number of pages in the given category, or 0 if it's nonexis-
531 * tent. This is an expensive parser function and can't be called too many
534 static function pagesincategory( $parser, $name = '', $raw = null ) {
535 static $cache = array();
536 $category = Category
::newFromName( $name );
538 if( !is_object( $category ) ) {
540 return self
::formatRaw( 0, $raw );
543 # Normalize name for cache
544 $name = $category->getName();
547 if( isset( $cache[$name] ) ) {
548 $count = $cache[$name];
549 } elseif( $parser->incrementExpensiveFunctionCount() ) {
550 $count = $cache[$name] = (int)$category->getPageCount();
552 return self
::formatRaw( $count, $raw );
556 * Return the size of the given page, or 0 if it's nonexistent. This is an
557 * expensive parser function and can't be called too many times per page.
559 * @todo FIXME: This doesn't work correctly on preview for getting the size
560 * of the current page.
561 * @todo FIXME: Title::getLength() documentation claims that it adds things
562 * to the link cache, so the local cache here should be unnecessary, but
563 * in fact calling getLength() repeatedly for the same $page does seem to
564 * run one query for each call?
565 * @todo Document parameters
567 * @param $parser Parser
568 * @param $page String TODO DOCUMENT (Default: empty string)
569 * @param $raw TODO DOCUMENT (Default: null)
571 static function pagesize( $parser, $page = '', $raw = null ) {
572 static $cache = array();
573 $title = Title
::newFromText( $page );
575 if( !is_object( $title ) ) {
577 return self
::formatRaw( 0, $raw );
580 # Normalize name for cache
581 $page = $title->getPrefixedText();
584 if( isset( $cache[$page] ) ) {
585 $length = $cache[$page];
586 } elseif( $parser->incrementExpensiveFunctionCount() ) {
587 $rev = Revision
::newFromTitle( $title );
588 $id = $rev ?
$rev->getPage() : 0;
589 $length = $cache[$page] = $rev ?
$rev->getSize() : 0;
591 // Register dependency in templatelinks
592 $parser->mOutput
->addTemplate( $title, $id, $rev ?
$rev->getId() : 0 );
594 return self
::formatRaw( $length, $raw );
598 * Returns the requested protection level for the current page
600 static function protectionlevel( $parser, $type = '' ) {
601 $restrictions = $parser->mTitle
->getRestrictions( strtolower( $type ) );
602 # Title::getRestrictions returns an array, its possible it may have
603 # multiple values in the future
604 return implode( $restrictions, ',' );
608 * Gives language names.
609 * @param $parser Parser
610 * @param $code String Language code
611 * @param $language String Language code
614 static function language( $parser, $code = '', $language = '' ) {
616 $code = strtolower( $code );
617 $language = strtolower( $language );
619 if ( $language !== '' ) {
620 $names = Language
::getTranslatedLanguageNames( $language );
621 return isset( $names[$code] ) ?
$names[$code] : wfBCP47( $code );
624 $lang = $wgContLang->getLanguageName( $code );
625 return $lang !== '' ?
$lang : wfBCP47( $code );
629 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
631 static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT
) {
632 $lengthOfPadding = mb_strlen( $padding );
633 if ( $lengthOfPadding == 0 ) return $string;
635 # The remaining length to add counts down to 0 as padding is added
636 $length = min( $length, 500 ) - mb_strlen( $string );
637 # $finalPadding is just $padding repeated enough times so that
638 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
640 while ( $length > 0 ) {
641 # If $length < $lengthofPadding, truncate $padding so we get the
642 # exact length desired.
643 $finalPadding .= mb_substr( $padding, 0, $length );
644 $length -= $lengthOfPadding;
647 if ( $direction == STR_PAD_LEFT
) {
648 return $finalPadding . $string;
650 return $string . $finalPadding;
654 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
655 return self
::pad( $string, $length, $padding, STR_PAD_LEFT
);
658 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
659 return self
::pad( $string, $length, $padding );
663 * @param $parser Parser
667 static function anchorencode( $parser, $text ) {
668 return substr( $parser->guessSectionNameFromWikiText( $text ), 1);
671 static function special( $parser, $text ) {
672 list( $page, $subpage ) = SpecialPageFactory
::resolveAlias( $text );
674 $title = SpecialPage
::getTitleFor( $page, $subpage );
677 return wfMsgForContent( 'nosuchspecialpage' );
682 * @param $parser Parser
683 * @param $text String The sortkey to use
684 * @param $uarg String Either "noreplace" or "noerror" (in en)
685 * both suppress errors, and noreplace does nothing if
686 * a default sortkey already exists.
689 public static function defaultsort( $parser, $text, $uarg = '' ) {
690 static $magicWords = null;
691 if ( is_null( $magicWords ) ) {
692 $magicWords = new MagicWordArray( array( 'defaultsort_noerror', 'defaultsort_noreplace' ) );
694 $arg = $magicWords->matchStartToEnd( $uarg );
696 $text = trim( $text );
697 if( strlen( $text ) == 0 )
699 $old = $parser->getCustomDefaultSort();
700 if ( $old === false ||
$arg !== 'defaultsort_noreplace' ) {
701 $parser->setDefaultSort( $text );
704 if( $old === false ||
$old == $text ||
$arg ) {
707 return( '<span class="error">' .
708 wfMsgForContent( 'duplicate-defaultsort',
709 htmlspecialchars( $old ),
710 htmlspecialchars( $text ) ) .
715 // Usage {{filepath|300}}, {{filepath|nowiki}}, {{filepath|nowiki|300}} or {{filepath|300|nowiki}}
716 public static function filepath( $parser, $name='', $argA='', $argB='' ) {
717 $file = wfFindFile( $name );
719 $argA_int = intval( $argA );
720 $argB_int = intval( $argB );
722 if ( $argB_int > 0 ) {
723 // {{filepath: | option | size }}
727 } elseif ( $argA_int > 0 ) {
728 // {{filepath: | size [|option] }}
733 // {{filepath: [|option] }}
738 $url = $file->getFullUrl();
740 // If a size is requested...
741 if ( is_integer( $size ) ) {
742 $mto = $file->transform( array( 'width' => $size ) );
744 if ( $mto && !$mto->isError() ) {
745 // ... change the URL to point to a thumbnail.
746 $url = wfExpandUrl( $mto->getUrl(), PROTO_RELATIVE
);
749 if ( $option == 'nowiki' ) {
750 return array( $url, 'nowiki' => true );
759 * Parser function to extension tag adaptor
761 public static function tagObj( $parser, $frame, $args ) {
762 if ( !count( $args ) ) {
765 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
767 if ( count( $args ) ) {
768 $inner = $frame->expand( array_shift( $args ) );
773 $stripList = $parser->getStripList();
774 if ( !in_array( $tagName, $stripList ) ) {
775 return '<span class="error">' .
776 wfMsgForContent( 'unknown_extension_tag', $tagName ) .
780 $attributes = array();
781 foreach ( $args as $arg ) {
782 $bits = $arg->splitArg();
783 if ( strval( $bits['index'] ) === '' ) {
784 $name = trim( $frame->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
785 $value = trim( $frame->expand( $bits['value'] ) );
786 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
787 $value = isset( $m[1] ) ?
$m[1] : '';
789 $attributes[$name] = $value;
796 'attributes' => $attributes,
797 'close' => "</$tagName>",
799 return $parser->extensionSubstitution( $params, $frame );