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