Got rid of the MagicWord indexing constants (MAG_xxx), replaced them by string indexi...
[lhc/web/wiklou.git] / includes / CoreParserFunctions.php
1 <?php
2
3 /**
4 * Various core parser functions, registered in Parser::firstCallInit()
5 */
6
7 class CoreParserFunctions {
8 static function ns( $parser, $part1 = '' ) {
9 global $wgContLang;
10 $found = false;
11 if ( intval( $part1 ) || $part1 == "0" ) {
12 $text = $wgContLang->getNsText( intval( $part1 ) );
13 $found = true;
14 } else {
15 $param = str_replace( ' ', '_', strtolower( $part1 ) );
16 $index = Namespace::getCanonicalIndex( strtolower( $param ) );
17 if ( !is_null( $index ) ) {
18 $text = $wgContLang->getNsText( $index );
19 $found = true;
20 }
21 }
22 if ( $found ) {
23 return $text;
24 } else {
25 return array( 'found' => false );
26 }
27 }
28
29 static function urlencode( $parser, $s = '' ) {
30 return urlencode( $s );
31 }
32
33 static function lcfirst( $parser, $s = '' ) {
34 global $wgContLang;
35 return $wgContLang->lcfirst( $s );
36 }
37
38 static function ucfirst( $parser, $s = '' ) {
39 global $wgContLang;
40 return $wgContLang->ucfirst( $s );
41 }
42
43 static function lc( $parser, $s = '' ) {
44 global $wgContLang;
45 return $wgContLang->lc( $s );
46 }
47
48 static function uc( $parser, $s = '' ) {
49 global $wgContLang;
50 return $wgContLang->uc( $s );
51 }
52
53 static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
54 static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
55 static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
56 static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
57
58 static function urlFunction( $func, $s = '', $arg = null ) {
59 $found = false;
60 $title = Title::newFromText( $s );
61 # Due to order of execution of a lot of bits, the values might be encoded
62 # before arriving here; if that's true, then the title can't be created
63 # and the variable will fail. If we can't get a decent title from the first
64 # attempt, url-decode and try for a second.
65 if( is_null( $title ) )
66 $title = Title::newFromUrl( urldecode( $s ) );
67 if ( !is_null( $title ) ) {
68 if ( !is_null( $arg ) ) {
69 $text = $title->$func( $arg );
70 } else {
71 $text = $title->$func();
72 }
73 $found = true;
74 }
75 if ( $found ) {
76 return $text;
77 } else {
78 return array( 'found' => false );
79 }
80 }
81
82 function formatNum( $parser, $num = '' ) {
83 return $parser->getFunctionLang()->formatNum( $num );
84 }
85
86 function grammar( $parser, $case = '', $word = '' ) {
87 return $parser->getFunctionLang()->convertGrammar( $word, $case );
88 }
89
90 function plural( $parser, $text = '', $arg0 = null, $arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null ) {
91 return $parser->getFunctionLang()->convertPlural( $text, $arg0, $arg1, $arg2, $arg3, $arg4 );
92 }
93
94 function displaytitle( $parser, $param = '' ) {
95 $parserOptions = new ParserOptions;
96 $local_parser = clone $parser;
97 $t2 = $local_parser->parse ( $param, $parser->mTitle, $parserOptions, false );
98 $parser->mOutput->mHTMLtitle = $t2->GetText();
99
100 # Add subtitle
101 $t = $parser->mTitle->getPrefixedText();
102 $parser->mOutput->mSubtitle .= wfMsg('displaytitle', $t);
103 return '';
104 }
105
106 function isRaw( $param ) {
107 static $mwRaw;
108 if ( !$mwRaw ) {
109 $mwRaw =& MagicWord::get( 'rawsuffix' );
110 }
111 if ( is_null( $param ) ) {
112 return false;
113 } else {
114 return $mwRaw->match( $param );
115 }
116 }
117
118 function statisticsFunction( $func, $raw = null ) {
119 if ( self::isRaw( $raw ) ) {
120 return call_user_func( $func );
121 } else {
122 global $wgContLang;
123 return $wgContLang->formatNum( call_user_func( $func ) );
124 }
125 }
126
127 function numberofpages( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfPages', $raw ); }
128 function numberofusers( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfUsers', $raw ); }
129 function numberofarticles( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfArticles', $raw ); }
130 function numberoffiles( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfFiles', $raw ); }
131 function numberofadmins( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfAdmins', $raw ); }
132
133 function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
134 $count = wfPagesInNs( intval( $namespace ) );
135 if ( self::isRaw( $raw ) ) {
136 global $wgContLang;
137 return $wgContLang->formatNum( $count );
138 } else {
139 return $count;
140 }
141 }
142
143 function language( $parser, $arg = '' ) {
144 global $wgContLang;
145 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
146 return $lang != '' ? $lang : $arg;
147 }
148 }
149
150 ?>