* (bug 12294) Namespace class renamed to MWNamespace for PHP 5.3 compatibility
[lhc/web/wiklou.git] / includes / CoreParserFunctions.php
1 <?php
2
3 /**
4 * Various core parser functions, registered in Parser::firstCallInit()
5 * @addtogroup Parser
6 */
7 class CoreParserFunctions {
8 static function register( $parser ) {
9 global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
10
11 # Syntax for arguments (see self::setFunctionHook):
12 # "name for lookup in localized magic words array",
13 # function callback,
14 # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}
15 # instead of {{#int:...}})
16
17 $parser->setFunctionHook( 'int', array( __CLASS__, 'intFunction' ), SFH_NO_HASH );
18 $parser->setFunctionHook( 'ns', array( __CLASS__, 'ns' ), SFH_NO_HASH );
19 $parser->setFunctionHook( 'urlencode', array( __CLASS__, 'urlencode' ), SFH_NO_HASH );
20 $parser->setFunctionHook( 'lcfirst', array( __CLASS__, 'lcfirst' ), SFH_NO_HASH );
21 $parser->setFunctionHook( 'ucfirst', array( __CLASS__, 'ucfirst' ), SFH_NO_HASH );
22 $parser->setFunctionHook( 'lc', array( __CLASS__, 'lc' ), SFH_NO_HASH );
23 $parser->setFunctionHook( 'uc', array( __CLASS__, 'uc' ), SFH_NO_HASH );
24 $parser->setFunctionHook( 'localurl', array( __CLASS__, 'localurl' ), SFH_NO_HASH );
25 $parser->setFunctionHook( 'localurle', array( __CLASS__, 'localurle' ), SFH_NO_HASH );
26 $parser->setFunctionHook( 'fullurl', array( __CLASS__, 'fullurl' ), SFH_NO_HASH );
27 $parser->setFunctionHook( 'fullurle', array( __CLASS__, 'fullurle' ), SFH_NO_HASH );
28 $parser->setFunctionHook( 'formatnum', array( __CLASS__, 'formatnum' ), SFH_NO_HASH );
29 $parser->setFunctionHook( 'grammar', array( __CLASS__, 'grammar' ), SFH_NO_HASH );
30 $parser->setFunctionHook( 'plural', array( __CLASS__, 'plural' ), SFH_NO_HASH );
31 $parser->setFunctionHook( 'numberofpages', array( __CLASS__, 'numberofpages' ), SFH_NO_HASH );
32 $parser->setFunctionHook( 'numberofusers', array( __CLASS__, 'numberofusers' ), SFH_NO_HASH );
33 $parser->setFunctionHook( 'numberofarticles', array( __CLASS__, 'numberofarticles' ), SFH_NO_HASH );
34 $parser->setFunctionHook( 'numberoffiles', array( __CLASS__, 'numberoffiles' ), SFH_NO_HASH );
35 $parser->setFunctionHook( 'numberofadmins', array( __CLASS__, 'numberofadmins' ), SFH_NO_HASH );
36 $parser->setFunctionHook( 'numberofedits', array( __CLASS__, 'numberofedits' ), SFH_NO_HASH );
37 $parser->setFunctionHook( 'language', array( __CLASS__, 'language' ), SFH_NO_HASH );
38 $parser->setFunctionHook( 'padleft', array( __CLASS__, 'padleft' ), SFH_NO_HASH );
39 $parser->setFunctionHook( 'padright', array( __CLASS__, 'padright' ), SFH_NO_HASH );
40 $parser->setFunctionHook( 'anchorencode', array( __CLASS__, 'anchorencode' ), SFH_NO_HASH );
41 $parser->setFunctionHook( 'special', array( __CLASS__, 'special' ) );
42 $parser->setFunctionHook( 'defaultsort', array( __CLASS__, 'defaultsort' ), SFH_NO_HASH );
43 $parser->setFunctionHook( 'filepath', array( __CLASS__, 'filepath' ), SFH_NO_HASH );
44 $parser->setFunctionHook( 'tag', array( __CLASS__, 'tagObj' ), SFH_OBJECT_ARGS );
45
46 if ( $wgAllowDisplayTitle ) {
47 $parser->setFunctionHook( 'displaytitle', array( __CLASS__, 'displaytitle' ), SFH_NO_HASH );
48 }
49 if ( $wgAllowSlowParserFunctions ) {
50 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__, 'pagesinnamespace' ), SFH_NO_HASH );
51 }
52 }
53
54 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
55 if ( strval( $part1 ) !== '' ) {
56 $args = array_slice( func_get_args(), 2 );
57 return wfMsgReal( $part1, $args, true );
58 } else {
59 return array( 'found' => false );
60 }
61 }
62
63 static function ns( $parser, $part1 = '' ) {
64 global $wgContLang;
65 $found = false;
66 if ( intval( $part1 ) || $part1 == "0" ) {
67 $text = $wgContLang->getNsText( intval( $part1 ) );
68 $found = true;
69 } else {
70 $param = str_replace( ' ', '_', strtolower( $part1 ) );
71 $index = MWNamespace::getCanonicalIndex( strtolower( $param ) );
72 if ( !is_null( $index ) ) {
73 $text = $wgContLang->getNsText( $index );
74 $found = true;
75 }
76 }
77 if ( $found ) {
78 return $text;
79 } else {
80 return array( 'found' => false );
81 }
82 }
83
84 static function urlencode( $parser, $s = '' ) {
85 return urlencode( $s );
86 }
87
88 static function lcfirst( $parser, $s = '' ) {
89 global $wgContLang;
90 return $wgContLang->lcfirst( $s );
91 }
92
93 static function ucfirst( $parser, $s = '' ) {
94 global $wgContLang;
95 return $wgContLang->ucfirst( $s );
96 }
97
98 static function lc( $parser, $s = '' ) {
99 global $wgContLang;
100 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
101 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
102 } else {
103 return $wgContLang->lc( $s );
104 }
105 }
106
107 static function uc( $parser, $s = '' ) {
108 global $wgContLang;
109 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
110 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
111 } else {
112 return $wgContLang->uc( $s );
113 }
114 }
115
116 static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
117 static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
118 static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
119 static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
120
121 static function urlFunction( $func, $s = '', $arg = null ) {
122 $title = Title::newFromText( $s );
123 # Due to order of execution of a lot of bits, the values might be encoded
124 # before arriving here; if that's true, then the title can't be created
125 # and the variable will fail. If we can't get a decent title from the first
126 # attempt, url-decode and try for a second.
127 if( is_null( $title ) )
128 $title = Title::newFromUrl( urldecode( $s ) );
129 if ( !is_null( $title ) ) {
130 if ( !is_null( $arg ) ) {
131 $text = $title->$func( $arg );
132 } else {
133 $text = $title->$func();
134 }
135 return $text;
136 } else {
137 return array( 'found' => false );
138 }
139 }
140
141 static function formatNum( $parser, $num = '', $raw = null) {
142 if ( self::israw( $raw ) ) {
143 return $parser->getFunctionLang()->parseFormattedNumber( $num );
144 } else {
145 return $parser->getFunctionLang()->formatNum( $num );
146 }
147 }
148
149 static function grammar( $parser, $case = '', $word = '' ) {
150 return $parser->getFunctionLang()->convertGrammar( $word, $case );
151 }
152
153 static function plural( $parser, $text = '') {
154 $forms = array_slice( func_get_args(), 2);
155 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
156 return $parser->getFunctionLang()->convertPlural( $text, $forms );
157 }
158
159 /**
160 * Override the title of the page when viewed,
161 * provided we've been given a title which
162 * will normalise to the canonical title
163 *
164 * @param Parser $parser Parent parser
165 * @param string $text Desired title text
166 * @return string
167 */
168 static function displaytitle( $parser, $text = '' ) {
169 $text = trim( Sanitizer::decodeCharReferences( $text ) );
170 $title = Title::newFromText( $text );
171 if( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) )
172 $parser->mOutput->setDisplayTitle( $text );
173 return '';
174 }
175
176 static function isRaw( $param ) {
177 static $mwRaw;
178 if ( !$mwRaw ) {
179 $mwRaw =& MagicWord::get( 'rawsuffix' );
180 }
181 if ( is_null( $param ) ) {
182 return false;
183 } else {
184 return $mwRaw->match( $param );
185 }
186 }
187
188 static function formatRaw( $num, $raw ) {
189 if( self::isRaw( $raw ) ) {
190 return $num;
191 } else {
192 global $wgContLang;
193 return $wgContLang->formatNum( $num );
194 }
195 }
196 static function numberofpages( $parser, $raw = null ) {
197 return self::formatRaw( SiteStats::pages(), $raw );
198 }
199 static function numberofusers( $parser, $raw = null ) {
200 return self::formatRaw( SiteStats::users(), $raw );
201 }
202 static function numberofarticles( $parser, $raw = null ) {
203 return self::formatRaw( SiteStats::articles(), $raw );
204 }
205 static function numberoffiles( $parser, $raw = null ) {
206 return self::formatRaw( SiteStats::images(), $raw );
207 }
208 static function numberofadmins( $parser, $raw = null ) {
209 return self::formatRaw( SiteStats::admins(), $raw );
210 }
211 static function numberofedits( $parser, $raw = null ) {
212 return self::formatRaw( SiteStats::edits(), $raw );
213 }
214 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
215 return self::formatRaw( SiteStats::pagesInNs( intval( $namespace ) ), $raw );
216 }
217
218 static function language( $parser, $arg = '' ) {
219 global $wgContLang;
220 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
221 return $lang != '' ? $lang : $arg;
222 }
223
224 static function pad( $string = '', $length = 0, $char = 0, $direction = STR_PAD_RIGHT ) {
225 $length = min( max( $length, 0 ), 500 );
226 $char = substr( $char, 0, 1 );
227 return ( $string !== '' && (int)$length > 0 && strlen( trim( (string)$char ) ) > 0 )
228 ? str_pad( $string, $length, (string)$char, $direction )
229 : $string;
230 }
231
232 static function padleft( $parser, $string = '', $length = 0, $char = 0 ) {
233 return self::pad( $string, $length, $char, STR_PAD_LEFT );
234 }
235
236 static function padright( $parser, $string = '', $length = 0, $char = 0 ) {
237 return self::pad( $string, $length, $char );
238 }
239
240 static function anchorencode( $parser, $text ) {
241 $a = urlencode( $text );
242 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
243 # leave colons alone, however
244 $a = str_replace( '.3A', ':', $a );
245 return $a;
246 }
247
248 static function special( $parser, $text ) {
249 $title = SpecialPage::getTitleForAlias( $text );
250 if ( $title ) {
251 return $title->getPrefixedText();
252 } else {
253 return wfMsgForContent( 'nosuchspecialpage' );
254 }
255 }
256
257 public static function defaultsort( $parser, $text ) {
258 $text = trim( $text );
259 if( strlen( $text ) > 0 )
260 $parser->setDefaultSort( $text );
261 return '';
262 }
263
264 public static function filepath( $parser, $name='', $option='' ) {
265 $file = wfFindFile( $name );
266 if( $file ) {
267 $url = $file->getFullUrl();
268 if( $option == 'nowiki' ) {
269 return "<nowiki>$url</nowiki>";
270 }
271 return $url;
272 } else {
273 return '';
274 }
275 }
276
277 /**
278 * Parser function to extension tag adaptor
279 */
280 public static function tagObj( $parser, $frame, $args ) {
281 $xpath = false;
282 if ( !count( $args ) ) {
283 return '';
284 }
285 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
286
287 if ( count( $args ) ) {
288 $inner = $frame->expand( array_shift( $args ) );
289 } else {
290 $inner = null;
291 }
292
293 $stripList = $parser->getStripList();
294 if ( !in_array( $tagName, $stripList ) ) {
295 return '<span class="error">' .
296 wfMsg( 'unknown_extension_tag', $tagName ) .
297 '</span>';
298 }
299
300 $attributes = array();
301 foreach ( $args as $arg ) {
302 $bits = $arg->splitArg();
303 if ( strval( $bits['index'] ) === '' ) {
304 $name = $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS );
305 $value = trim( $frame->expand( $bits['value'] ) );
306 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
307 $value = isset( $m[1] ) ? $m[1] : '';
308 }
309 $attributes[$name] = $value;
310 }
311 }
312
313 $params = array(
314 'name' => $tagName,
315 'inner' => $inner,
316 'attributes' => $attributes,
317 'close' => "</$tagName>",
318 );
319 return $parser->extensionSubstitution( $params, $frame );
320 }
321 }
322