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