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