4 * Various core parser functions, registered in Parser::firstCallInit()
7 class CoreParserFunctions
{
9 static function ns( $parser, $part1 = '' ) {
12 if ( intval( $part1 ) ||
$part1 == "0" ) {
13 $text = $wgContLang->getNsText( intval( $part1 ) );
16 $param = str_replace( ' ', '_', strtolower( $part1 ) );
17 $index = Namespace::getCanonicalIndex( strtolower( $param ) );
18 if ( !is_null( $index ) ) {
19 $text = $wgContLang->getNsText( $index );
26 return array( 'found' => false );
30 static function urlencode( $parser, $s = '' ) {
31 return urlencode( $s );
34 static function lcfirst( $parser, $s = '' ) {
36 return $wgContLang->lcfirst( $s );
39 static function ucfirst( $parser, $s = '' ) {
41 return $wgContLang->ucfirst( $s );
44 static function lc( $parser, $s = '' ) {
46 return $wgContLang->lc( $s );
49 static function uc( $parser, $s = '' ) {
51 return $wgContLang->uc( $s );
54 static function localurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getLocalURL', $s, $arg ); }
55 static function localurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeLocalURL', $s, $arg ); }
56 static function fullurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getFullURL', $s, $arg ); }
57 static function fullurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeFullURL', $s, $arg ); }
59 static function urlFunction( $func, $s = '', $arg = null ) {
61 $title = Title
::newFromText( $s );
62 # Due to order of execution of a lot of bits, the values might be encoded
63 # before arriving here; if that's true, then the title can't be created
64 # and the variable will fail. If we can't get a decent title from the first
65 # attempt, url-decode and try for a second.
66 if( is_null( $title ) )
67 $title = Title
::newFromUrl( urldecode( $s ) );
68 if ( !is_null( $title ) ) {
69 if ( !is_null( $arg ) ) {
70 $text = $title->$func( $arg );
72 $text = $title->$func();
79 return array( 'found' => false );
83 function formatNum( $parser, $num = '' ) {
84 return $parser->getFunctionLang()->formatNum( $num );
87 function grammar( $parser, $case = '', $word = '' ) {
88 return $parser->getFunctionLang()->convertGrammar( $word, $case );
91 function plural( $parser, $text = '', $arg0 = null, $arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null ) {
92 return $parser->getFunctionLang()->convertPlural( $text, $arg0, $arg1, $arg2, $arg3, $arg4 );
95 function displaytitle( $parser, $param = '' ) {
96 $parserOptions = new ParserOptions
;
97 $local_parser = clone $parser;
98 $t2 = $local_parser->parse ( $param, $parser->mTitle
, $parserOptions, false );
99 $parser->mOutput
->mHTMLtitle
= $t2->GetText();
102 $t = $parser->mTitle
->getPrefixedText();
103 $parser->mOutput
->mSubtitle
.= wfMsg('displaytitle', $t);
107 function isRaw( $param ) {
110 $mwRaw =& MagicWord
::get( 'rawsuffix' );
112 if ( is_null( $param ) ) {
115 return $mwRaw->match( $param );
119 function statisticsFunction( $func, $raw = null ) {
120 if ( self
::isRaw( $raw ) ) {
121 return call_user_func( $func );
124 return $wgContLang->formatNum( call_user_func( $func ) );
128 function numberofpages( $parser, $raw = null ) { return self
::statisticsFunction( 'wfNumberOfPages', $raw ); }
129 function numberofusers( $parser, $raw = null ) { return self
::statisticsFunction( 'wfNumberOfUsers', $raw ); }
130 function numberofarticles( $parser, $raw = null ) { return self
::statisticsFunction( 'wfNumberOfArticles', $raw ); }
131 function numberoffiles( $parser, $raw = null ) { return self
::statisticsFunction( 'wfNumberOfFiles', $raw ); }
132 function numberofadmins( $parser, $raw = null ) { return self
::statisticsFunction( 'wfNumberOfAdmins', $raw ); }
134 function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
135 $count = wfPagesInNs( intval( $namespace ) );
136 if ( self
::isRaw( $raw ) ) {
138 return $wgContLang->formatNum( $count );
144 function language( $parser, $arg = '' ) {
146 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
147 return $lang != '' ?
$lang : $arg;
150 function padleft( $parser, $string, $length, $char = 0 ) {
151 return str_pad( $string, $length, (string)$char, STR_PAD_LEFT
);
154 function padright( $parser, $string, $length, $char = 0 ) {
155 return str_pad( $string, $length, (string)$char, STR_PAD_RIGHT
);