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