* Replaced $wgMessageCache by MessageCache::singleton(); since we only use one instan...
[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->getOptions()->getUserLang(), false );
90 $message = wfMsgReplaceArgs( $message, $args );
91 $message = $parser->replaceVariables( $message ); // like MessageCache::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->getOptions()->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 the ParserOptions if in interface message
248 $user = User::newFromName( $user );
249 if ( $user ) {
250 $gender = $user->getOption( 'gender' );
251 } elseif ( $parser->getOptions()->getInterfaceMessage() ) {
252 $gender = $parser->getOptions()->getUser()->getOption( 'gender' );
253 }
254 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
255 wfProfileOut( __METHOD__ );
256 return $ret;
257 }
258 static function plural( $parser, $text = '' ) {
259 $forms = array_slice( func_get_args(), 2 );
260 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
261 return $parser->getFunctionLang()->convertPlural( $text, $forms );
262 }
263
264 /**
265 * Override the title of the page when viewed, provided we've been given a
266 * title which will normalise to the canonical title
267 *
268 * @param $parser Parser: parent parser
269 * @param $text String: desired title text
270 * @return String
271 */
272 static function displaytitle( $parser, $text = '' ) {
273 global $wgRestrictDisplayTitle;
274
275 #parse a limited subset of wiki markup (just the single quote items)
276 $text = $parser->doQuotes( $text );
277
278 #remove stripped text (e.g. the UNIQ-QINU stuff) that was generated by tag extensions/whatever
279 $text = preg_replace( '/' . preg_quote( $parser->uniqPrefix(), '/' ) . '.*?'
280 . preg_quote( Parser::MARKER_SUFFIX, '/' ) . '/', '', $text );
281
282 #list of disallowed tags for DISPLAYTITLE
283 #these will be escaped even though they are allowed in normal wiki text
284 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr',
285 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp', 'br' );
286
287 #only requested titles that normalize to the actual title are allowed through
288 #if $wgRestrictDisplayTitle is true (it is by default)
289 #mimic the escaping process that occurs in OutputPage::setPageTitle
290 $text = Sanitizer::normalizeCharReferences( Sanitizer::removeHTMLtags( $text, null, array(), array(), $bad ) );
291 $title = Title::newFromText( Sanitizer::stripAllTags( $text ) );
292
293 if( !$wgRestrictDisplayTitle ) {
294 $parser->mOutput->setDisplayTitle( $text );
295 } else {
296 if ( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) ) {
297 $parser->mOutput->setDisplayTitle( $text );
298 }
299 }
300
301 return '';
302 }
303
304 static function isRaw( $param ) {
305 static $mwRaw;
306 if ( !$mwRaw ) {
307 $mwRaw =& MagicWord::get( 'rawsuffix' );
308 }
309 if ( is_null( $param ) ) {
310 return false;
311 } else {
312 return $mwRaw->match( $param );
313 }
314 }
315
316 static function formatRaw( $num, $raw ) {
317 if( self::isRaw( $raw ) ) {
318 return $num;
319 } else {
320 global $wgContLang;
321 return $wgContLang->formatNum( $num );
322 }
323 }
324 static function numberofpages( $parser, $raw = null ) {
325 return self::formatRaw( SiteStats::pages(), $raw );
326 }
327 static function numberofusers( $parser, $raw = null ) {
328 return self::formatRaw( SiteStats::users(), $raw );
329 }
330 static function numberofactiveusers( $parser, $raw = null ) {
331 return self::formatRaw( SiteStats::activeUsers(), $raw );
332 }
333 static function numberofarticles( $parser, $raw = null ) {
334 return self::formatRaw( SiteStats::articles(), $raw );
335 }
336 static function numberoffiles( $parser, $raw = null ) {
337 return self::formatRaw( SiteStats::images(), $raw );
338 }
339 static function numberofadmins( $parser, $raw = null ) {
340 return self::formatRaw( SiteStats::numberingroup('sysop'), $raw );
341 }
342 static function numberofedits( $parser, $raw = null ) {
343 return self::formatRaw( SiteStats::edits(), $raw );
344 }
345 static function numberofviews( $parser, $raw = null ) {
346 return self::formatRaw( SiteStats::views(), $raw );
347 }
348 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
349 return self::formatRaw( SiteStats::pagesInNs( intval( $namespace ) ), $raw );
350 }
351 static function numberingroup( $parser, $name = '', $raw = null) {
352 return self::formatRaw( SiteStats::numberingroup( strtolower( $name ) ), $raw );
353 }
354
355
356 /**
357 * Given a title, return the namespace name that would be given by the
358 * corresponding magic word
359 * Note: function name changed to "mwnamespace" rather than "namespace"
360 * to not break PHP 5.3
361 */
362 static function mwnamespace( $parser, $title = null ) {
363 $t = Title::newFromText( $title );
364 if ( is_null( $t ) )
365 return '';
366 return str_replace( '_', ' ', $t->getNsText() );
367 }
368 static function namespacee( $parser, $title = null ) {
369 $t = Title::newFromText( $title );
370 if ( is_null( $t ) )
371 return '';
372 return wfUrlencode( $t->getNsText() );
373 }
374 static function talkspace( $parser, $title = null ) {
375 $t = Title::newFromText( $title );
376 if ( is_null( $t ) || !$t->canTalk() )
377 return '';
378 return str_replace( '_', ' ', $t->getTalkNsText() );
379 }
380 static function talkspacee( $parser, $title = null ) {
381 $t = Title::newFromText( $title );
382 if ( is_null( $t ) || !$t->canTalk() )
383 return '';
384 return wfUrlencode( $t->getTalkNsText() );
385 }
386 static function subjectspace( $parser, $title = null ) {
387 $t = Title::newFromText( $title );
388 if ( is_null( $t ) )
389 return '';
390 return str_replace( '_', ' ', $t->getSubjectNsText() );
391 }
392 static function subjectspacee( $parser, $title = null ) {
393 $t = Title::newFromText( $title );
394 if ( is_null( $t ) )
395 return '';
396 return wfUrlencode( $t->getSubjectNsText() );
397 }
398 /*
399 * Functions to get and normalize pagenames, corresponding to the magic words
400 * of the same names
401 */
402 static function pagename( $parser, $title = null ) {
403 $t = Title::newFromText( $title );
404 if ( is_null( $t ) )
405 return '';
406 return wfEscapeWikiText( $t->getText() );
407 }
408 static function pagenamee( $parser, $title = null ) {
409 $t = Title::newFromText( $title );
410 if ( is_null( $t ) )
411 return '';
412 return wfEscapeWikiText( $t->getPartialURL() );
413 }
414 static function fullpagename( $parser, $title = null ) {
415 $t = Title::newFromText( $title );
416 if ( is_null( $t ) || !$t->canTalk() )
417 return '';
418 return wfEscapeWikiText( $t->getPrefixedText() );
419 }
420 static function fullpagenamee( $parser, $title = null ) {
421 $t = Title::newFromText( $title );
422 if ( is_null( $t ) || !$t->canTalk() )
423 return '';
424 return wfEscapeWikiText( $t->getPrefixedURL() );
425 }
426 static function subpagename( $parser, $title = null ) {
427 $t = Title::newFromText( $title );
428 if ( is_null( $t ) )
429 return '';
430 return wfEscapeWikiText( $t->getSubpageText() );
431 }
432 static function subpagenamee( $parser, $title = null ) {
433 $t = Title::newFromText( $title );
434 if ( is_null( $t ) )
435 return '';
436 return wfEscapeWikiText( $t->getSubpageUrlForm() );
437 }
438 static function basepagename( $parser, $title = null ) {
439 $t = Title::newFromText( $title );
440 if ( is_null( $t ) )
441 return '';
442 return wfEscapeWikiText( $t->getBaseText() );
443 }
444 static function basepagenamee( $parser, $title = null ) {
445 $t = Title::newFromText( $title );
446 if ( is_null( $t ) )
447 return '';
448 return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) ) );
449 }
450 static function talkpagename( $parser, $title = null ) {
451 $t = Title::newFromText( $title );
452 if ( is_null( $t ) || !$t->canTalk() )
453 return '';
454 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
455 }
456 static function talkpagenamee( $parser, $title = null ) {
457 $t = Title::newFromText( $title );
458 if ( is_null( $t ) || !$t->canTalk() )
459 return '';
460 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedUrl() );
461 }
462 static function subjectpagename( $parser, $title = null ) {
463 $t = Title::newFromText( $title );
464 if ( is_null( $t ) )
465 return '';
466 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
467 }
468 static function subjectpagenamee( $parser, $title = null ) {
469 $t = Title::newFromText( $title );
470 if ( is_null( $t ) )
471 return '';
472 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedUrl() );
473 }
474
475 /**
476 * Return the number of pages in the given category, or 0 if it's nonexis-
477 * tent. This is an expensive parser function and can't be called too many
478 * times per page.
479 */
480 static function pagesincategory( $parser, $name = '', $raw = null ) {
481 static $cache = array();
482 $category = Category::newFromName( $name );
483
484 if( !is_object( $category ) ) {
485 $cache[$name] = 0;
486 return self::formatRaw( 0, $raw );
487 }
488
489 # Normalize name for cache
490 $name = $category->getName();
491
492 $count = 0;
493 if( isset( $cache[$name] ) ) {
494 $count = $cache[$name];
495 } elseif( $parser->incrementExpensiveFunctionCount() ) {
496 $count = $cache[$name] = (int)$category->getPageCount();
497 }
498 return self::formatRaw( $count, $raw );
499 }
500
501 /**
502 * Return the size of the given page, or 0 if it's nonexistent. This is an
503 * expensive parser function and can't be called too many times per page.
504 *
505 * @todo Fixme: This doesn't work correctly on preview for getting the size
506 * of the current page.
507 * @todo Fixme: Title::getLength() documentation claims that it adds things
508 * to the link cache, so the local cache here should be unnecessary, but
509 * in fact calling getLength() repeatedly for the same $page does seem to
510 * run one query for each call?
511 */
512 static function pagesize( $parser, $page = '', $raw = null ) {
513 static $cache = array();
514 $title = Title::newFromText( $page );
515
516 if( !is_object( $title ) ) {
517 $cache[$page] = 0;
518 return self::formatRaw( 0, $raw );
519 }
520
521 # Normalize name for cache
522 $page = $title->getPrefixedText();
523
524 $length = 0;
525 if( isset( $cache[$page] ) ) {
526 $length = $cache[$page];
527 } elseif( $parser->incrementExpensiveFunctionCount() ) {
528 $rev = Revision::newFromTitle( $title );
529 $id = $rev ? $rev->getPage() : 0;
530 $length = $cache[$page] = $rev ? $rev->getSize() : 0;
531
532 // Register dependency in templatelinks
533 $parser->mOutput->addTemplate( $title, $id, $rev ? $rev->getId() : 0 );
534 }
535 return self::formatRaw( $length, $raw );
536 }
537
538 /**
539 * Returns the requested protection level for the current page
540 */
541 static function protectionlevel( $parser, $type = '' ) {
542 $restrictions = $parser->mTitle->getRestrictions( strtolower( $type ) );
543 # Title::getRestrictions returns an array, its possible it may have
544 # multiple values in the future
545 return implode( $restrictions, ',' );
546 }
547
548 static function language( $parser, $arg = '' ) {
549 global $wgContLang;
550 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
551 return $lang != '' ? $lang : $arg;
552 }
553
554 /**
555 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
556 */
557 static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT ) {
558 $lengthOfPadding = mb_strlen( $padding );
559 if ( $lengthOfPadding == 0 ) return $string;
560
561 # The remaining length to add counts down to 0 as padding is added
562 $length = min( $length, 500 ) - mb_strlen( $string );
563 # $finalPadding is just $padding repeated enough times so that
564 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
565 $finalPadding = '';
566 while ( $length > 0 ) {
567 # If $length < $lengthofPadding, truncate $padding so we get the
568 # exact length desired.
569 $finalPadding .= mb_substr( $padding, 0, $length );
570 $length -= $lengthOfPadding;
571 }
572
573 if ( $direction == STR_PAD_LEFT ) {
574 return $finalPadding . $string;
575 } else {
576 return $string . $finalPadding;
577 }
578 }
579
580 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
581 return self::pad( $string, $length, $padding, STR_PAD_LEFT );
582 }
583
584 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
585 return self::pad( $string, $length, $padding );
586 }
587
588 static function anchorencode( $parser, $text ) {
589 return substr( $parser->guessSectionNameFromWikiText( $text ), 1);
590 }
591
592 static function special( $parser, $text ) {
593 list( $page, $subpage ) = SpecialPage::resolveAliasWithSubpage( $text );
594 if ( $page ) {
595 $title = SpecialPage::getTitleFor( $page, $subpage );
596 return $title;
597 } else {
598 return wfMsgForContent( 'nosuchspecialpage' );
599 }
600 }
601
602 public static function defaultsort( $parser, $text ) {
603 $text = trim( $text );
604 if( strlen( $text ) == 0 )
605 return '';
606 $old = $parser->getCustomDefaultSort();
607 $parser->setDefaultSort( $text );
608 if( $old === false || $old == $text )
609 return '';
610 else
611 return( '<span class="error">' .
612 wfMsgForContent( 'duplicate-defaultsort',
613 htmlspecialchars( $old ),
614 htmlspecialchars( $text ) ) .
615 '</span>' );
616 }
617
618 // Usage {{filepath|300}}, {{filepath|nowiki}}, {{filepath|nowiki|300}} or {{filepath|300|nowiki}}
619 public static function filepath( $parser, $name='', $argA='', $argB='' ) {
620 $file = wfFindFile( $name );
621 $size = '';
622 $argA_int = intval( $argA );
623 $argB_int = intval( $argB );
624
625 if ( $argB_int > 0 ) {
626 // {{filepath: | option | size }}
627 $size = $argB_int;
628 $option = $argA;
629
630 } elseif ( $argA_int > 0 ) {
631 // {{filepath: | size [|option] }}
632 $size = $argA_int;
633 $option = $argB;
634
635 } else {
636 // {{filepath: [|option] }}
637 $option = $argA;
638 }
639
640 if ( $file ) {
641 $url = $file->getUrl();
642
643 // If a size is requested...
644 if ( is_integer( $size ) ) {
645 $mto = $file->transform( array( 'width' => $size ) );
646 // ... and we can
647 if ( $mto && !$mto->isError() ) {
648 // ... change the URL to point to a thumbnail.
649 $url = wfExpandUrl( $mto->getUrl() );
650 }
651 }
652 if ( $option == 'nowiki' ) {
653 return array( $url, 'nowiki' => true );
654 }
655 return $url;
656 } else {
657 return '';
658 }
659 }
660
661 /**
662 * Parser function to extension tag adaptor
663 */
664 public static function tagObj( $parser, $frame, $args ) {
665 if ( !count( $args ) ) {
666 return '';
667 }
668 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
669
670 if ( count( $args ) ) {
671 $inner = $frame->expand( array_shift( $args ) );
672 } else {
673 $inner = null;
674 }
675
676 $stripList = $parser->getStripList();
677 if ( !in_array( $tagName, $stripList ) ) {
678 return '<span class="error">' .
679 wfMsgForContent( 'unknown_extension_tag', $tagName ) .
680 '</span>';
681 }
682
683 $attributes = array();
684 foreach ( $args as $arg ) {
685 $bits = $arg->splitArg();
686 if ( strval( $bits['index'] ) === '' ) {
687 $name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
688 $value = trim( $frame->expand( $bits['value'] ) );
689 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
690 $value = isset( $m[1] ) ? $m[1] : '';
691 }
692 $attributes[$name] = $value;
693 }
694 }
695
696 $params = array(
697 'name' => $tagName,
698 'inner' => $inner,
699 'attributes' => $attributes,
700 'close' => "</$tagName>",
701 );
702 return $parser->extensionSubstitution( $params, $frame );
703 }
704 }