Added {{#special:}} parser function, to give the local default title for special...
[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 intFunction( $parser, $part1 = '' /*, ... */ ) {
9 if ( strval( $part1 ) !== '' ) {
10 $args = array_slice( func_get_args(), 2 );
11 return wfMsgReal( $part1, $args, true );
12 } else {
13 return array( 'found' => false );
14 }
15 }
16
17 static function ns( $parser, $part1 = '' ) {
18 global $wgContLang;
19 $found = false;
20 if ( intval( $part1 ) || $part1 == "0" ) {
21 $text = $wgContLang->getNsText( intval( $part1 ) );
22 $found = true;
23 } else {
24 $param = str_replace( ' ', '_', strtolower( $part1 ) );
25 $index = Namespace::getCanonicalIndex( strtolower( $param ) );
26 if ( !is_null( $index ) ) {
27 $text = $wgContLang->getNsText( $index );
28 $found = true;
29 }
30 }
31 if ( $found ) {
32 return $text;
33 } else {
34 return array( 'found' => false );
35 }
36 }
37
38 static function urlencode( $parser, $s = '' ) {
39 return urlencode( $s );
40 }
41
42 static function lcfirst( $parser, $s = '' ) {
43 global $wgContLang;
44 return $wgContLang->lcfirst( $s );
45 }
46
47 static function ucfirst( $parser, $s = '' ) {
48 global $wgContLang;
49 return $wgContLang->ucfirst( $s );
50 }
51
52 static function lc( $parser, $s = '' ) {
53 global $wgContLang;
54 return $wgContLang->lc( $s );
55 }
56
57 static function uc( $parser, $s = '' ) {
58 global $wgContLang;
59 return $wgContLang->uc( $s );
60 }
61
62 static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
63 static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
64 static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
65 static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
66
67 static function urlFunction( $func, $s = '', $arg = null ) {
68 $found = false;
69 $title = Title::newFromText( $s );
70 # Due to order of execution of a lot of bits, the values might be encoded
71 # before arriving here; if that's true, then the title can't be created
72 # and the variable will fail. If we can't get a decent title from the first
73 # attempt, url-decode and try for a second.
74 if( is_null( $title ) )
75 $title = Title::newFromUrl( urldecode( $s ) );
76 if ( !is_null( $title ) ) {
77 if ( !is_null( $arg ) ) {
78 $text = $title->$func( $arg );
79 } else {
80 $text = $title->$func();
81 }
82 $found = true;
83 }
84 if ( $found ) {
85 return $text;
86 } else {
87 return array( 'found' => false );
88 }
89 }
90
91 function formatNum( $parser, $num = '' ) {
92 return $parser->getFunctionLang()->formatNum( $num );
93 }
94
95 function grammar( $parser, $case = '', $word = '' ) {
96 return $parser->getFunctionLang()->convertGrammar( $word, $case );
97 }
98
99 function plural( $parser, $text = '', $arg0 = null, $arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null ) {
100 return $parser->getFunctionLang()->convertPlural( $text, $arg0, $arg1, $arg2, $arg3, $arg4 );
101 }
102
103 function displaytitle( $parser, $param = '' ) {
104 $parserOptions = new ParserOptions;
105 $local_parser = clone $parser;
106 $t2 = $local_parser->parse ( $param, $parser->mTitle, $parserOptions, false );
107 $parser->mOutput->mHTMLtitle = $t2->GetText();
108
109 # Add subtitle
110 $t = $parser->mTitle->getPrefixedText();
111 $parser->mOutput->mSubtitle .= wfMsg('displaytitle', $t);
112 return '';
113 }
114
115 function isRaw( $param ) {
116 static $mwRaw;
117 if ( !$mwRaw ) {
118 $mwRaw =& MagicWord::get( 'rawsuffix' );
119 }
120 if ( is_null( $param ) ) {
121 return false;
122 } else {
123 return $mwRaw->match( $param );
124 }
125 }
126
127 function statisticsFunction( $func, $raw = null ) {
128 if ( self::isRaw( $raw ) ) {
129 return call_user_func( $func );
130 } else {
131 global $wgContLang;
132 return $wgContLang->formatNum( call_user_func( $func ) );
133 }
134 }
135
136 function numberofpages( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfPages', $raw ); }
137 function numberofusers( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfUsers', $raw ); }
138 function numberofarticles( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfArticles', $raw ); }
139 function numberoffiles( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfFiles', $raw ); }
140 function numberofadmins( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfAdmins', $raw ); }
141
142 function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
143 $count = wfPagesInNs( intval( $namespace ) );
144 if ( self::isRaw( $raw ) ) {
145 global $wgContLang;
146 return $wgContLang->formatNum( $count );
147 } else {
148 return $count;
149 }
150 }
151
152 function language( $parser, $arg = '' ) {
153 global $wgContLang;
154 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
155 return $lang != '' ? $lang : $arg;
156 }
157
158 function pad( $string = '', $length = 0, $char = 0, $direction = STR_PAD_RIGHT ) {
159 $length = min( max( $length, 0 ), 500 );
160 $char = substr( $char, 0, 1 );
161 return ( $string && (int)$length > 0 && strlen( trim( (string)$char ) ) > 0 )
162 ? str_pad( $string, $length, (string)$char, $direction )
163 : $string;
164 }
165
166 function padleft( $parser, $string = '', $length = 0, $char = 0 ) {
167 return self::pad( $string, $length, $char, STR_PAD_LEFT );
168 }
169
170 function padright( $parser, $string = '', $length = 0, $char = 0 ) {
171 return self::pad( $string, $length, $char );
172 }
173
174 function anchorencode( $parser, $text ) {
175 return str_replace( '%', '.', str_replace('+', '_', urlencode( $text ) ) );
176 }
177
178 function special( $parser, $text ) {
179 $title = SpecialPage::getTitleForAlias( $text );
180 if ( $title ) {
181 return $title->getPrefixedText();
182 } else {
183 return wfMsgForContent( 'nosuchspecialpage' );
184 }
185 }
186 }
187
188 ?>