I love case-sensitivity. Don't you?
[lhc/web/wiklou.git] / includes / parser / CoreParserFunctions.php
1 <?php
2
3 /**
4 * Various core parser functions, registered in Parser::firstCallInit()
5 * @ingroup 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( 'apiurl', array( __CLASS__, 'apiurl' ), SFH_NO_HASH );
29 $parser->setFunctionHook( 'formatnum', array( __CLASS__, 'formatnum' ), SFH_NO_HASH );
30 $parser->setFunctionHook( 'grammar', array( __CLASS__, 'grammar' ), SFH_NO_HASH );
31 $parser->setFunctionHook( 'plural', array( __CLASS__, 'plural' ), SFH_NO_HASH );
32 $parser->setFunctionHook( 'numberofpages', array( __CLASS__, 'numberofpages' ), SFH_NO_HASH );
33 $parser->setFunctionHook( 'numberofusers', array( __CLASS__, 'numberofusers' ), SFH_NO_HASH );
34 $parser->setFunctionHook( 'numberofarticles', array( __CLASS__, 'numberofarticles' ), SFH_NO_HASH );
35 $parser->setFunctionHook( 'numberoffiles', array( __CLASS__, 'numberoffiles' ), SFH_NO_HASH );
36 $parser->setFunctionHook( 'numberofadmins', array( __CLASS__, 'numberofadmins' ), SFH_NO_HASH );
37 $parser->setFunctionHook( 'numberofedits', array( __CLASS__, 'numberofedits' ), SFH_NO_HASH );
38 $parser->setFunctionHook( 'language', array( __CLASS__, 'language' ), SFH_NO_HASH );
39 $parser->setFunctionHook( 'padleft', array( __CLASS__, 'padleft' ), SFH_NO_HASH );
40 $parser->setFunctionHook( 'padright', array( __CLASS__, 'padright' ), SFH_NO_HASH );
41 $parser->setFunctionHook( 'anchorencode', array( __CLASS__, 'anchorencode' ), SFH_NO_HASH );
42 $parser->setFunctionHook( 'special', array( __CLASS__, 'special' ) );
43 $parser->setFunctionHook( 'defaultsort', array( __CLASS__, 'defaultsort' ), SFH_NO_HASH );
44 $parser->setFunctionHook( 'filepath', array( __CLASS__, 'filepath' ), SFH_NO_HASH );
45 $parser->setFunctionHook( 'pagesincategory', array( __CLASS__, 'pagesincategory' ), SFH_NO_HASH );
46 $parser->setFunctionHook( 'pagesize', array( __CLASS__, 'pagesize' ), SFH_NO_HASH );
47 $parser->setFunctionHook( 'tag', array( __CLASS__, 'tagObj' ), SFH_OBJECT_ARGS );
48
49 if ( $wgAllowDisplayTitle ) {
50 $parser->setFunctionHook( 'displaytitle', array( __CLASS__, 'displaytitle' ), SFH_NO_HASH );
51 }
52 if ( $wgAllowSlowParserFunctions ) {
53 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__, 'pagesinnamespace' ), SFH_NO_HASH );
54 }
55 }
56
57 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
58 if ( strval( $part1 ) !== '' ) {
59 $args = array_slice( func_get_args(), 2 );
60 return wfMsgReal( $part1, $args, true );
61 } else {
62 return array( 'found' => false );
63 }
64 }
65
66 static function ns( $parser, $part1 = '' ) {
67 global $wgContLang;
68 $found = false;
69 if ( intval( $part1 ) || $part1 == "0" ) {
70 $text = $wgContLang->getNsText( intval( $part1 ) );
71 $found = true;
72 } else {
73 $param = str_replace( ' ', '_', strtolower( $part1 ) );
74 $index = MWNamespace::getCanonicalIndex( strtolower( $param ) );
75 if ( !is_null( $index ) ) {
76 $text = $wgContLang->getNsText( $index );
77 $found = true;
78 }
79 }
80 if ( $found ) {
81 return $text;
82 } else {
83 return array( 'found' => false );
84 }
85 }
86
87 static function urlencode( $parser, $s = '' ) {
88 return urlencode( $s );
89 }
90
91 static function lcfirst( $parser, $s = '' ) {
92 global $wgContLang;
93 return $wgContLang->lcfirst( $s );
94 }
95
96 static function ucfirst( $parser, $s = '' ) {
97 global $wgContLang;
98 return $wgContLang->ucfirst( $s );
99 }
100
101 static function lc( $parser, $s = '' ) {
102 global $wgContLang;
103 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
104 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
105 } else {
106 return $wgContLang->lc( $s );
107 }
108 }
109
110 static function uc( $parser, $s = '' ) {
111 global $wgContLang;
112 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
113 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
114 } else {
115 return $wgContLang->uc( $s );
116 }
117 }
118
119 static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
120 static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
121 static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
122 static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
123 static function apiurl( $parser, $s = '', $arg = null ) {
124 global $wgEnableAPI;
125 # Don't bother linking to the API if they can't use it.
126 if ( $wgEnableAPI === false ) {
127 return array( 'found' => false );
128 }
129 global $wgServer, $wgApiScript;
130 $path = $wgServer . $wgApiScript . '?action=' . $s;
131 if ( !is_null($arg ) ) {
132 $path .= '&' . $arg;
133 }
134 return $path;
135 }
136
137 static function urlFunction( $func, $s = '', $arg = null ) {
138 $title = Title::newFromText( $s );
139 # Due to order of execution of a lot of bits, the values might be encoded
140 # before arriving here; if that's true, then the title can't be created
141 # and the variable will fail. If we can't get a decent title from the first
142 # attempt, url-decode and try for a second.
143 if( is_null( $title ) )
144 $title = Title::newFromUrl( urldecode( $s ) );
145 if ( !is_null( $title ) ) {
146 if ( !is_null( $arg ) ) {
147 $text = $title->$func( $arg );
148 } else {
149 $text = $title->$func();
150 }
151 return $text;
152 } else {
153 return array( 'found' => false );
154 }
155 }
156
157 static function formatNum( $parser, $num = '', $raw = null) {
158 if ( self::israw( $raw ) ) {
159 return $parser->getFunctionLang()->parseFormattedNumber( $num );
160 } else {
161 return $parser->getFunctionLang()->formatNum( $num );
162 }
163 }
164
165 static function grammar( $parser, $case = '', $word = '' ) {
166 return $parser->getFunctionLang()->convertGrammar( $word, $case );
167 }
168
169 static function plural( $parser, $text = '') {
170 $forms = array_slice( func_get_args(), 2);
171 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
172 return $parser->getFunctionLang()->convertPlural( $text, $forms );
173 }
174
175 /**
176 * Override the title of the page when viewed, provided we've been given a
177 * title which will normalise to the canonical title
178 *
179 * @param Parser $parser Parent parser
180 * @param string $text Desired title text
181 * @return string
182 */
183 static function displaytitle( $parser, $text = '' ) {
184 $text = trim( Sanitizer::decodeCharReferences( $text ) );
185 $title = Title::newFromText( $text );
186 if( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) )
187 $parser->mOutput->setDisplayTitle( $text );
188 return '';
189 }
190
191 static function isRaw( $param ) {
192 static $mwRaw;
193 if ( !$mwRaw ) {
194 $mwRaw =& MagicWord::get( 'rawsuffix' );
195 }
196 if ( is_null( $param ) ) {
197 return false;
198 } else {
199 return $mwRaw->match( $param );
200 }
201 }
202
203 static function formatRaw( $num, $raw ) {
204 if( self::isRaw( $raw ) ) {
205 return $num;
206 } else {
207 global $wgContLang;
208 return $wgContLang->formatNum( $num );
209 }
210 }
211 static function numberofpages( $parser, $raw = null ) {
212 return self::formatRaw( SiteStats::pages(), $raw );
213 }
214 static function numberofusers( $parser, $raw = null ) {
215 return self::formatRaw( SiteStats::users(), $raw );
216 }
217 static function numberofarticles( $parser, $raw = null ) {
218 return self::formatRaw( SiteStats::articles(), $raw );
219 }
220 static function numberoffiles( $parser, $raw = null ) {
221 return self::formatRaw( SiteStats::images(), $raw );
222 }
223 static function numberofadmins( $parser, $raw = null ) {
224 return self::formatRaw( SiteStats::numberingroup('sysop'), $raw );
225 }
226 static function numberofedits( $parser, $raw = null ) {
227 return self::formatRaw( SiteStats::edits(), $raw );
228 }
229 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
230 return self::formatRaw( SiteStats::pagesInNs( intval( $namespace ) ), $raw );
231 }
232
233 /**
234 * Return the number of pages in the given category, or 0 if it's nonexis-
235 * tent. This is an expensive parser function and can't be called too many
236 * times per page.
237 */
238 static function pagesincategory( $parser, $name = '', $raw = null ) {
239 static $cache = array();
240 $category = Category::newFromName( $name );
241
242 if( !is_object( $category ) ) {
243 $cache[$name] = 0;
244 return self::formatRaw( 0, $raw );
245 }
246
247 # Normalize name for cache
248 $name = $category->getName();
249
250 $count = 0;
251 if( isset( $cache[$name] ) ) {
252 $count = $cache[$name];
253 } elseif( $parser->incrementExpensiveFunctionCount() ) {
254 $count = $cache[$name] = (int)$category->getPageCount();
255 }
256 return self::formatRaw( $count, $raw );
257 }
258
259 /**
260 * Return the size of the given page, or 0 if it's nonexistent. This is an
261 * expensive parser function and can't be called too many times per page.
262 *
263 * @FIXME This doesn't work correctly on preview for getting the size of
264 * the current page.
265 * @FIXME Title::getLength() documentation claims that it adds things to
266 * the link cache, so the local cache here should be unnecessary, but in
267 * fact calling getLength() repeatedly for the same $page does seem to
268 * run one query for each call?
269 */
270 static function pagesize( $parser, $page = '', $raw = null ) {
271 static $cache = array();
272 $title = Title::newFromText($page);
273
274 if( !is_object( $title ) ) {
275 $cache[$page] = 0;
276 return self::formatRaw( 0, $raw );
277 }
278
279 # Normalize name for cache
280 $page = $title->getPrefixedText();
281
282 $length = 0;
283 if( isset( $cache[$page] ) ) {
284 $length = $cache[$page];
285 } elseif( $parser->incrementExpensiveFunctionCount() ) {
286 $length = $cache[$page] = $title->getLength();
287
288 // Register dependency in templatelinks
289 $id = $title->getArticleId();
290 $revid = Revision::newFromTitle($title);
291 $parser->mOutput->addTemplate($title, $id, $revid);
292 }
293 return self::formatRaw( $length, $raw );
294 }
295
296 static function language( $parser, $arg = '' ) {
297 global $wgContLang;
298 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
299 return $lang != '' ? $lang : $arg;
300 }
301
302 static function pad( $string = '', $length = 0, $char = 0, $direction = STR_PAD_RIGHT ) {
303 $length = min( max( $length, 0 ), 500 );
304 $char = substr( $char, 0, 1 );
305 return ( $string !== '' && (int)$length > 0 && strlen( trim( (string)$char ) ) > 0 )
306 ? str_pad( $string, $length, (string)$char, $direction )
307 : $string;
308 }
309
310 static function padleft( $parser, $string = '', $length = 0, $char = 0 ) {
311 return self::pad( $string, $length, $char, STR_PAD_LEFT );
312 }
313
314 static function padright( $parser, $string = '', $length = 0, $char = 0 ) {
315 return self::pad( $string, $length, $char );
316 }
317
318 static function anchorencode( $parser, $text ) {
319 $a = urlencode( $text );
320 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
321 # leave colons alone, however
322 $a = str_replace( '.3A', ':', $a );
323 return $a;
324 }
325
326 static function special( $parser, $text ) {
327 $title = SpecialPage::getTitleForAlias( $text );
328 if ( $title ) {
329 return $title->getPrefixedText();
330 } else {
331 return wfMsgForContent( 'nosuchspecialpage' );
332 }
333 }
334
335 public static function defaultsort( $parser, $text ) {
336 $text = trim( $text );
337 if( strlen( $text ) > 0 )
338 $parser->setDefaultSort( $text );
339 return '';
340 }
341
342 public static function filepath( $parser, $name='', $option='' ) {
343 $file = wfFindFile( $name );
344 if( $file ) {
345 $url = $file->getFullUrl();
346 if( $option == 'nowiki' ) {
347 return "<nowiki>$url</nowiki>";
348 }
349 return $url;
350 } else {
351 return '';
352 }
353 }
354
355 /**
356 * Parser function to extension tag adaptor
357 */
358 public static function tagObj( $parser, $frame, $args ) {
359 $xpath = false;
360 if ( !count( $args ) ) {
361 return '';
362 }
363 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
364
365 if ( count( $args ) ) {
366 $inner = $frame->expand( array_shift( $args ) );
367 } else {
368 $inner = null;
369 }
370
371 $stripList = $parser->getStripList();
372 if ( !in_array( $tagName, $stripList ) ) {
373 return '<span class="error">' .
374 wfMsg( 'unknown_extension_tag', $tagName ) .
375 '</span>';
376 }
377
378 $attributes = array();
379 foreach ( $args as $arg ) {
380 $bits = $arg->splitArg();
381 if ( strval( $bits['index'] ) === '' ) {
382 $name = $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS );
383 $value = trim( $frame->expand( $bits['value'] ) );
384 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
385 $value = isset( $m[1] ) ? $m[1] : '';
386 }
387 $attributes[$name] = $value;
388 }
389 }
390
391 $params = array(
392 'name' => $tagName,
393 'inner' => $inner,
394 'attributes' => $attributes,
395 'close' => "</$tagName>",
396 );
397 return $parser->extensionSubstitution( $params, $frame );
398 }
399 }