f35d020d889e03bcb05ab2f4abf16940e5190e01
[lhc/web/wiklou.git] / includes / parser / CoreParserFunctions.php
1 <?php
2 /**
3 * Parser functions provided by MediaWiki core
4 *
5 * @file
6 */
7
8 /**
9 * Various core parser functions, registered in Parser::firstCallInit()
10 * @ingroup Parser
11 */
12 class CoreParserFunctions {
13 static function register( $parser ) {
14 global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
15
16 # Syntax for arguments (see self::setFunctionHook):
17 # "name for lookup in localized magic words array",
18 # function callback,
19 # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}}
20 # instead of {{#int:...}})
21
22 $parser->setFunctionHook( 'int', array( __CLASS__, 'intFunction' ), SFH_NO_HASH );
23 $parser->setFunctionHook( 'ns', array( __CLASS__, 'ns' ), SFH_NO_HASH );
24 $parser->setFunctionHook( 'nse', array( __CLASS__, 'nse' ), SFH_NO_HASH );
25 $parser->setFunctionHook( 'urlencode', array( __CLASS__, 'urlencode' ), SFH_NO_HASH );
26 $parser->setFunctionHook( 'lcfirst', array( __CLASS__, 'lcfirst' ), SFH_NO_HASH );
27 $parser->setFunctionHook( 'ucfirst', array( __CLASS__, 'ucfirst' ), SFH_NO_HASH );
28 $parser->setFunctionHook( 'lc', array( __CLASS__, 'lc' ), SFH_NO_HASH );
29 $parser->setFunctionHook( 'uc', array( __CLASS__, 'uc' ), SFH_NO_HASH );
30 $parser->setFunctionHook( 'localurl', array( __CLASS__, 'localurl' ), SFH_NO_HASH );
31 $parser->setFunctionHook( 'localurle', array( __CLASS__, 'localurle' ), SFH_NO_HASH );
32 $parser->setFunctionHook( 'fullurl', array( __CLASS__, 'fullurl' ), SFH_NO_HASH );
33 $parser->setFunctionHook( 'fullurle', array( __CLASS__, 'fullurle' ), SFH_NO_HASH );
34 $parser->setFunctionHook( 'formatnum', array( __CLASS__, 'formatnum' ), SFH_NO_HASH );
35 $parser->setFunctionHook( 'grammar', array( __CLASS__, 'grammar' ), SFH_NO_HASH );
36 $parser->setFunctionHook( 'gender', array( __CLASS__, 'gender' ), SFH_NO_HASH );
37 $parser->setFunctionHook( 'plural', array( __CLASS__, 'plural' ), SFH_NO_HASH );
38 $parser->setFunctionHook( 'numberofpages', array( __CLASS__, 'numberofpages' ), SFH_NO_HASH );
39 $parser->setFunctionHook( 'numberofusers', array( __CLASS__, 'numberofusers' ), SFH_NO_HASH );
40 $parser->setFunctionHook( 'numberofactiveusers', array( __CLASS__, 'numberofactiveusers' ), SFH_NO_HASH );
41 $parser->setFunctionHook( 'numberofarticles', array( __CLASS__, 'numberofarticles' ), SFH_NO_HASH );
42 $parser->setFunctionHook( 'numberoffiles', array( __CLASS__, 'numberoffiles' ), SFH_NO_HASH );
43 $parser->setFunctionHook( 'numberofadmins', array( __CLASS__, 'numberofadmins' ), SFH_NO_HASH );
44 $parser->setFunctionHook( 'numberingroup', array( __CLASS__, 'numberingroup' ), SFH_NO_HASH );
45 $parser->setFunctionHook( 'numberofedits', array( __CLASS__, 'numberofedits' ), SFH_NO_HASH );
46 $parser->setFunctionHook( 'numberofviews', array( __CLASS__, 'numberofviews' ), SFH_NO_HASH );
47 $parser->setFunctionHook( 'language', array( __CLASS__, 'language' ), SFH_NO_HASH );
48 $parser->setFunctionHook( 'padleft', array( __CLASS__, 'padleft' ), SFH_NO_HASH );
49 $parser->setFunctionHook( 'padright', array( __CLASS__, 'padright' ), SFH_NO_HASH );
50 $parser->setFunctionHook( 'anchorencode', array( __CLASS__, 'anchorencode' ), SFH_NO_HASH );
51 $parser->setFunctionHook( 'special', array( __CLASS__, 'special' ) );
52 $parser->setFunctionHook( 'defaultsort', array( __CLASS__, 'defaultsort' ), SFH_NO_HASH );
53 $parser->setFunctionHook( 'filepath', array( __CLASS__, 'filepath' ), SFH_NO_HASH );
54 $parser->setFunctionHook( 'pagesincategory', array( __CLASS__, 'pagesincategory' ), SFH_NO_HASH );
55 $parser->setFunctionHook( 'pagesize', array( __CLASS__, 'pagesize' ), SFH_NO_HASH );
56 $parser->setFunctionHook( 'protectionlevel', array( __CLASS__, 'protectionlevel' ), SFH_NO_HASH );
57 $parser->setFunctionHook( 'namespace', array( __CLASS__, 'mwnamespace' ), SFH_NO_HASH );
58 $parser->setFunctionHook( 'namespacee', array( __CLASS__, 'namespacee' ), SFH_NO_HASH );
59 $parser->setFunctionHook( 'talkspace', array( __CLASS__, 'talkspace' ), SFH_NO_HASH );
60 $parser->setFunctionHook( 'talkspacee', array( __CLASS__, 'talkspacee' ), SFH_NO_HASH );
61 $parser->setFunctionHook( 'subjectspace', array( __CLASS__, 'subjectspace' ), SFH_NO_HASH );
62 $parser->setFunctionHook( 'subjectspacee', array( __CLASS__, 'subjectspacee' ), SFH_NO_HASH );
63 $parser->setFunctionHook( 'pagename', array( __CLASS__, 'pagename' ), SFH_NO_HASH );
64 $parser->setFunctionHook( 'pagenamee', array( __CLASS__, 'pagenamee' ), SFH_NO_HASH );
65 $parser->setFunctionHook( 'fullpagename', array( __CLASS__, 'fullpagename' ), SFH_NO_HASH );
66 $parser->setFunctionHook( 'fullpagenamee', array( __CLASS__, 'fullpagenamee' ), SFH_NO_HASH );
67 $parser->setFunctionHook( 'basepagename', array( __CLASS__, 'basepagename' ), SFH_NO_HASH );
68 $parser->setFunctionHook( 'basepagenamee', array( __CLASS__, 'basepagenamee' ), SFH_NO_HASH );
69 $parser->setFunctionHook( 'subpagename', array( __CLASS__, 'subpagename' ), SFH_NO_HASH );
70 $parser->setFunctionHook( 'subpagenamee', array( __CLASS__, 'subpagenamee' ), SFH_NO_HASH );
71 $parser->setFunctionHook( 'talkpagename', array( __CLASS__, 'talkpagename' ), SFH_NO_HASH );
72 $parser->setFunctionHook( 'talkpagenamee', array( __CLASS__, 'talkpagenamee' ), SFH_NO_HASH );
73 $parser->setFunctionHook( 'subjectpagename', array( __CLASS__, 'subjectpagename' ), SFH_NO_HASH );
74 $parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__, 'subjectpagenamee' ), SFH_NO_HASH );
75 $parser->setFunctionHook( 'tag', array( __CLASS__, 'tagObj' ), SFH_OBJECT_ARGS );
76 $parser->setFunctionHook( 'formatdate', array( __CLASS__, 'formatDate' ) );
77
78 if ( $wgAllowDisplayTitle ) {
79 $parser->setFunctionHook( 'displaytitle', array( __CLASS__, 'displaytitle' ), SFH_NO_HASH );
80 }
81 if ( $wgAllowSlowParserFunctions ) {
82 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__, 'pagesinnamespace' ), SFH_NO_HASH );
83 }
84 }
85
86 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
87 if ( strval( $part1 ) !== '' ) {
88 $args = array_slice( func_get_args(), 2 );
89 $message = wfMsgGetKey( $part1, true, $parser->mOptions->getUserLang(), false );
90 $message = wfMsgReplaceArgs( $message, $args );
91 $message = $parser->replaceVariables( $message ); // like $wgMessageCache->transform()
92 return $message;
93 } else {
94 return array( 'found' => false );
95 }
96 }
97
98 static function formatDate( $parser, $date, $defaultPref = null ) {
99 $df = DateFormatter::getInstance();
100
101 $date = trim( $date );
102
103 $pref = $parser->mOptions->getDateFormat();
104
105 // Specify a different default date format other than the the normal default
106 // iff the user has 'default' for their setting
107 if ( $pref == 'default' && $defaultPref )
108 $pref = $defaultPref;
109
110 $date = $df->reformat( $pref, $date, array( 'match-whole' ) );
111 return $date;
112 }
113
114 static function ns( $parser, $part1 = '' ) {
115 global $wgContLang;
116 if ( intval( $part1 ) || $part1 == "0" ) {
117 $index = intval( $part1 );
118 } else {
119 $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
120 }
121 if ( $index !== false ) {
122 return $wgContLang->getFormattedNsText( $index );
123 } else {
124 return array( 'found' => false );
125 }
126 }
127
128 static function nse( $parser, $part1 = '' ) {
129 return wfUrlencode( str_replace( ' ', '_', self::ns( $parser, $part1 ) ) );
130 }
131
132 /**
133 * urlencodes a string according to one of three patterns: (bug 22474)
134 *
135 * By default (for HTTP "query" strings), spaces are encoded as '+'.
136 * Or to encode a value for the HTTP "path", spaces are encoded as '%20'.
137 * For links to "wiki"s, or similar software, spaces are encoded as '_',
138 *
139 * @param $parser Parser object
140 * @param $s String: The text to encode.
141 * @param $arg String (optional): The type of encoding.
142 */
143 static function urlencode( $parser, $s = '', $arg = null ) {
144 static $magicWords = null;
145 if ( is_null( $magicWords ) ) {
146 $magicWords = new MagicWordArray( array( 'url_path', 'url_query', 'url_wiki' ) );
147 }
148 switch( $magicWords->matchStartToEnd( $arg ) ) {
149
150 // Encode as though it's a wiki page, '_' for ' '.
151 case 'url_wiki':
152 return wfUrlencode( str_replace( ' ', '_', $s ) );
153
154 // Encode for an HTTP Path, '%20' for ' '.
155 case 'url_path':
156 return rawurlencode( $s );
157
158 // Encode for HTTP query, '+' for ' '.
159 case 'url_query':
160 default:
161 return urlencode( $s );
162 }
163 }
164
165 static function lcfirst( $parser, $s = '' ) {
166 global $wgContLang;
167 return $wgContLang->lcfirst( $s );
168 }
169
170 static function ucfirst( $parser, $s = '' ) {
171 global $wgContLang;
172 return $wgContLang->ucfirst( $s );
173 }
174
175 static function lc( $parser, $s = '' ) {
176 global $wgContLang;
177 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
178 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
179 } else {
180 return $wgContLang->lc( $s );
181 }
182 }
183
184 static function uc( $parser, $s = '' ) {
185 global $wgContLang;
186 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
187 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
188 } else {
189 return $wgContLang->uc( $s );
190 }
191 }
192
193 static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
194 static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
195 static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
196 static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
197
198 static function urlFunction( $func, $s = '', $arg = null ) {
199 $title = Title::newFromText( $s );
200 # Due to order of execution of a lot of bits, the values might be encoded
201 # before arriving here; if that's true, then the title can't be created
202 # and the variable will fail. If we can't get a decent title from the first
203 # attempt, url-decode and try for a second.
204 if( is_null( $title ) )
205 $title = Title::newFromURL( urldecode( $s ) );
206 if( !is_null( $title ) ) {
207 # Convert NS_MEDIA -> NS_FILE
208 if( $title->getNamespace() == NS_MEDIA ) {
209 $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
210 }
211 if( !is_null( $arg ) ) {
212 $text = $title->$func( $arg );
213 } else {
214 $text = $title->$func();
215 }
216 return $text;
217 } else {
218 return array( 'found' => false );
219 }
220 }
221
222 static function formatNum( $parser, $num = '', $raw = null) {
223 if ( self::israw( $raw ) ) {
224 return $parser->getFunctionLang()->parseFormattedNumber( $num );
225 } else {
226 return $parser->getFunctionLang()->formatNum( $num );
227 }
228 }
229
230 static function grammar( $parser, $case = '', $word = '' ) {
231 return $parser->getFunctionLang()->convertGrammar( $word, $case );
232 }
233
234 static function gender( $parser, $user ) {
235 wfProfileIn( __METHOD__ );
236 $forms = array_slice( func_get_args(), 2);
237
238 // default
239 $gender = User::getDefaultOption( 'gender' );
240
241 // allow prefix.
242 $title = Title::newFromText( $user );
243
244 if ( is_object( $title ) && $title->getNamespace() == NS_USER )
245 $user = $title->getText();
246
247 // check parameter, or use $wgUser if in interface message
248 $user = User::newFromName( $user );
249 if ( $user ) {
250 $gender = $user->getOption( 'gender' );
251 } elseif ( $parser->mOptions->getInterfaceMessage() ) {
252 global $wgUser;
253 $gender = $wgUser->getOption( 'gender' );
254 }
255 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
256 wfProfileOut( __METHOD__ );
257 return $ret;
258 }
259 static function plural( $parser, $text = '' ) {
260 $forms = array_slice( func_get_args(), 2 );
261 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
262 return $parser->getFunctionLang()->convertPlural( $text, $forms );
263 }
264
265 /**
266 * Override the title of the page when viewed, provided we've been given a
267 * title which will normalise to the canonical title
268 *
269 * @param $parser Parser: parent parser
270 * @param $text String: desired title text
271 * @return String
272 */
273 static function displaytitle( $parser, $text = '' ) {
274 global $wgRestrictDisplayTitle;
275
276 #parse a limited subset of wiki markup (just the single quote items)
277 $text = $parser->doQuotes( $text );
278
279 #remove stripped text (e.g. the UNIQ-QINU stuff) that was generated by tag extensions/whatever
280 $text = preg_replace( '/' . preg_quote( $parser->uniqPrefix(), '/' ) . '.*?'
281 . preg_quote( Parser::MARKER_SUFFIX, '/' ) . '/', '', $text );
282
283 #list of disallowed tags for DISPLAYTITLE
284 #these will be escaped even though they are allowed in normal wiki text
285 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr',
286 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp', 'br' );
287
288 #only requested titles that normalize to the actual title are allowed through
289 #if $wgRestrictDisplayTitle is true (it is by default)
290 #mimic the escaping process that occurs in OutputPage::setPageTitle
291 $text = Sanitizer::normalizeCharReferences( Sanitizer::removeHTMLtags( $text, null, array(), array(), $bad ) );
292 $title = Title::newFromText( Sanitizer::stripAllTags( $text ) );
293
294 if( !$wgRestrictDisplayTitle ) {
295 $parser->mOutput->setDisplayTitle( $text );
296 } else {
297 if ( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) ) {
298 $parser->mOutput->setDisplayTitle( $text );
299 }
300 }
301
302 return '';
303 }
304
305 static function isRaw( $param ) {
306 static $mwRaw;
307 if ( !$mwRaw ) {
308 $mwRaw =& MagicWord::get( 'rawsuffix' );
309 }
310 if ( is_null( $param ) ) {
311 return false;
312 } else {
313 return $mwRaw->match( $param );
314 }
315 }
316
317 static function formatRaw( $num, $raw ) {
318 if( self::isRaw( $raw ) ) {
319 return $num;
320 } else {
321 global $wgContLang;
322 return $wgContLang->formatNum( $num );
323 }
324 }
325 static function numberofpages( $parser, $raw = null ) {
326 return self::formatRaw( SiteStats::pages(), $raw );
327 }
328 static function numberofusers( $parser, $raw = null ) {
329 return self::formatRaw( SiteStats::users(), $raw );
330 }
331 static function numberofactiveusers( $parser, $raw = null ) {
332 return self::formatRaw( SiteStats::activeUsers(), $raw );
333 }
334 static function numberofarticles( $parser, $raw = null ) {
335 return self::formatRaw( SiteStats::articles(), $raw );
336 }
337 static function numberoffiles( $parser, $raw = null ) {
338 return self::formatRaw( SiteStats::images(), $raw );
339 }
340 static function numberofadmins( $parser, $raw = null ) {
341 return self::formatRaw( SiteStats::numberingroup('sysop'), $raw );
342 }
343 static function numberofedits( $parser, $raw = null ) {
344 return self::formatRaw( SiteStats::edits(), $raw );
345 }
346 static function numberofviews( $parser, $raw = null ) {
347 return self::formatRaw( SiteStats::views(), $raw );
348 }
349 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
350 return self::formatRaw( SiteStats::pagesInNs( intval( $namespace ) ), $raw );
351 }
352 static function numberingroup( $parser, $name = '', $raw = null) {
353 return self::formatRaw( SiteStats::numberingroup( strtolower( $name ) ), $raw );
354 }
355
356
357 /**
358 * Given a title, return the namespace name that would be given by the
359 * corresponding magic word
360 * Note: function name changed to "mwnamespace" rather than "namespace"
361 * to not break PHP 5.3
362 */
363 static function mwnamespace( $parser, $title = null ) {
364 $t = Title::newFromText( $title );
365 if ( is_null( $t ) )
366 return '';
367 return str_replace( '_', ' ', $t->getNsText() );
368 }
369 static function namespacee( $parser, $title = null ) {
370 $t = Title::newFromText( $title );
371 if ( is_null( $t ) )
372 return '';
373 return wfUrlencode( $t->getNsText() );
374 }
375 static function talkspace( $parser, $title = null ) {
376 $t = Title::newFromText( $title );
377 if ( is_null( $t ) || !$t->canTalk() )
378 return '';
379 return str_replace( '_', ' ', $t->getTalkNsText() );
380 }
381 static function talkspacee( $parser, $title = null ) {
382 $t = Title::newFromText( $title );
383 if ( is_null( $t ) || !$t->canTalk() )
384 return '';
385 return wfUrlencode( $t->getTalkNsText() );
386 }
387 static function subjectspace( $parser, $title = null ) {
388 $t = Title::newFromText( $title );
389 if ( is_null( $t ) )
390 return '';
391 return str_replace( '_', ' ', $t->getSubjectNsText() );
392 }
393 static function subjectspacee( $parser, $title = null ) {
394 $t = Title::newFromText( $title );
395 if ( is_null( $t ) )
396 return '';
397 return wfUrlencode( $t->getSubjectNsText() );
398 }
399 /*
400 * Functions to get and normalize pagenames, corresponding to the magic words
401 * of the same names
402 */
403 static function pagename( $parser, $title = null ) {
404 $t = Title::newFromText( $title );
405 if ( is_null( $t ) )
406 return '';
407 return wfEscapeWikiText( $t->getText() );
408 }
409 static function pagenamee( $parser, $title = null ) {
410 $t = Title::newFromText( $title );
411 if ( is_null( $t ) )
412 return '';
413 return $t->getPartialURL();
414 }
415 static function fullpagename( $parser, $title = null ) {
416 $t = Title::newFromText( $title );
417 if ( is_null( $t ) || !$t->canTalk() )
418 return '';
419 return wfEscapeWikiText( $t->getPrefixedText() );
420 }
421 static function fullpagenamee( $parser, $title = null ) {
422 $t = Title::newFromText( $title );
423 if ( is_null( $t ) || !$t->canTalk() )
424 return '';
425 return $t->getPrefixedURL();
426 }
427 static function subpagename( $parser, $title = null ) {
428 $t = Title::newFromText( $title );
429 if ( is_null( $t ) )
430 return '';
431 return $t->getSubpageText();
432 }
433 static function subpagenamee( $parser, $title = null ) {
434 $t = Title::newFromText( $title );
435 if ( is_null( $t ) )
436 return '';
437 return $t->getSubpageUrlForm();
438 }
439 static function basepagename( $parser, $title = null ) {
440 $t = Title::newFromText( $title );
441 if ( is_null( $t ) )
442 return '';
443 return $t->getBaseText();
444 }
445 static function basepagenamee( $parser, $title = null ) {
446 $t = Title::newFromText( $title );
447 if ( is_null( $t ) )
448 return '';
449 return wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) );
450 }
451 static function talkpagename( $parser, $title = null ) {
452 $t = Title::newFromText( $title );
453 if ( is_null( $t ) || !$t->canTalk() )
454 return '';
455 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
456 }
457 static function talkpagenamee( $parser, $title = null ) {
458 $t = Title::newFromText( $title );
459 if ( is_null( $t ) || !$t->canTalk() )
460 return '';
461 return $t->getTalkPage()->getPrefixedUrl();
462 }
463 static function subjectpagename( $parser, $title = null ) {
464 $t = Title::newFromText( $title );
465 if ( is_null( $t ) )
466 return '';
467 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
468 }
469 static function subjectpagenamee( $parser, $title = null ) {
470 $t = Title::newFromText( $title );
471 if ( is_null( $t ) )
472 return '';
473 return $t->getSubjectPage()->getPrefixedUrl();
474 }
475
476 /**
477 * Return the number of pages in the given category, or 0 if it's nonexis-
478 * tent. This is an expensive parser function and can't be called too many
479 * times per page.
480 */
481 static function pagesincategory( $parser, $name = '', $raw = null ) {
482 static $cache = array();
483 $category = Category::newFromName( $name );
484
485 if( !is_object( $category ) ) {
486 $cache[$name] = 0;
487 return self::formatRaw( 0, $raw );
488 }
489
490 # Normalize name for cache
491 $name = $category->getName();
492
493 $count = 0;
494 if( isset( $cache[$name] ) ) {
495 $count = $cache[$name];
496 } elseif( $parser->incrementExpensiveFunctionCount() ) {
497 $count = $cache[$name] = (int)$category->getPageCount();
498 }
499 return self::formatRaw( $count, $raw );
500 }
501
502 /**
503 * Return the size of the given page, or 0 if it's nonexistent. This is an
504 * expensive parser function and can't be called too many times per page.
505 *
506 * @todo Fixme: This doesn't work correctly on preview for getting the size
507 * of the current page.
508 * @todo Fixme: Title::getLength() documentation claims that it adds things
509 * to the link cache, so the local cache here should be unnecessary, but
510 * in fact calling getLength() repeatedly for the same $page does seem to
511 * run one query for each call?
512 */
513 static function pagesize( $parser, $page = '', $raw = null ) {
514 static $cache = array();
515 $title = Title::newFromText( $page );
516
517 if( !is_object( $title ) ) {
518 $cache[$page] = 0;
519 return self::formatRaw( 0, $raw );
520 }
521
522 # Normalize name for cache
523 $page = $title->getPrefixedText();
524
525 $length = 0;
526 if( isset( $cache[$page] ) ) {
527 $length = $cache[$page];
528 } elseif( $parser->incrementExpensiveFunctionCount() ) {
529 $rev = Revision::newFromTitle( $title );
530 $id = $rev ? $rev->getPage() : 0;
531 $length = $cache[$page] = $rev ? $rev->getSize() : 0;
532
533 // Register dependency in templatelinks
534 $parser->mOutput->addTemplate( $title, $id, $rev ? $rev->getId() : 0 );
535 }
536 return self::formatRaw( $length, $raw );
537 }
538
539 /**
540 * Returns the requested protection level for the current page
541 */
542 static function protectionlevel( $parser, $type = '' ) {
543 $restrictions = $parser->mTitle->getRestrictions( strtolower( $type ) );
544 # Title::getRestrictions returns an array, its possible it may have
545 # multiple values in the future
546 return implode( $restrictions, ',' );
547 }
548
549 static function language( $parser, $arg = '' ) {
550 global $wgContLang;
551 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
552 return $lang != '' ? $lang : $arg;
553 }
554
555 /**
556 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
557 */
558 static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT ) {
559 $lengthOfPadding = mb_strlen( $padding );
560 if ( $lengthOfPadding == 0 ) return $string;
561
562 # The remaining length to add counts down to 0 as padding is added
563 $length = min( $length, 500 ) - mb_strlen( $string );
564 # $finalPadding is just $padding repeated enough times so that
565 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
566 $finalPadding = '';
567 while ( $length > 0 ) {
568 # If $length < $lengthofPadding, truncate $padding so we get the
569 # exact length desired.
570 $finalPadding .= mb_substr( $padding, 0, $length );
571 $length -= $lengthOfPadding;
572 }
573
574 if ( $direction == STR_PAD_LEFT ) {
575 return $finalPadding . $string;
576 } else {
577 return $string . $finalPadding;
578 }
579 }
580
581 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
582 return self::pad( $string, $length, $padding, STR_PAD_LEFT );
583 }
584
585 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
586 return self::pad( $string, $length, $padding );
587 }
588
589 static function anchorencode( $parser, $text ) {
590 return substr( $parser->guessSectionNameFromWikiText( $text ), 1);
591 }
592
593 static function special( $parser, $text ) {
594 list( $page, $subpage ) = SpecialPage::resolveAliasWithSubpage( $text );
595 if ( $page ) {
596 $title = SpecialPage::getTitleFor( $page, $subpage );
597 return $title;
598 } else {
599 return wfMsgForContent( 'nosuchspecialpage' );
600 }
601 }
602
603 public static function defaultsort( $parser, $text ) {
604 $text = trim( $text );
605 if( strlen( $text ) == 0 )
606 return '';
607 $old = $parser->getCustomDefaultSort();
608 $parser->setDefaultSort( $text );
609 if( $old === false || $old == $text )
610 return '';
611 else
612 return( '<span class="error">' .
613 wfMsgForContent( 'duplicate-defaultsort',
614 htmlspecialchars( $old ),
615 htmlspecialchars( $text ) ) .
616 '</span>' );
617 }
618
619 public static function filepath( $parser, $name='', $option='' ) {
620 $file = wfFindFile( $name );
621 if( $file ) {
622 $url = $file->getFullUrl();
623 if( $option == 'nowiki' ) {
624 return array( $url, 'nowiki' => true );
625 }
626 return $url;
627 } else {
628 return '';
629 }
630 }
631
632 /**
633 * Parser function to extension tag adaptor
634 */
635 public static function tagObj( $parser, $frame, $args ) {
636 if ( !count( $args ) ) {
637 return '';
638 }
639 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
640
641 if ( count( $args ) ) {
642 $inner = $frame->expand( array_shift( $args ) );
643 } else {
644 $inner = null;
645 }
646
647 $stripList = $parser->getStripList();
648 if ( !in_array( $tagName, $stripList ) ) {
649 return '<span class="error">' .
650 wfMsgForContent( 'unknown_extension_tag', $tagName ) .
651 '</span>';
652 }
653
654 $attributes = array();
655 foreach ( $args as $arg ) {
656 $bits = $arg->splitArg();
657 if ( strval( $bits['index'] ) === '' ) {
658 $name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
659 $value = trim( $frame->expand( $bits['value'] ) );
660 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
661 $value = isset( $m[1] ) ? $m[1] : '';
662 }
663 $attributes[$name] = $value;
664 }
665 }
666
667 $params = array(
668 'name' => $tagName,
669 'inner' => $inner,
670 'attributes' => $attributes,
671 'close' => "</$tagName>",
672 );
673 return $parser->extensionSubstitution( $params, $frame );
674 }
675 }