4 * Various core parser functions, registered in Parser::firstCallInit()
7 class CoreParserFunctions
{
8 static function register( $parser ) {
9 global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
11 # Syntax for arguments (see self::setFunctionHook):
12 # "name for lookup in localized magic words array",
14 # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}
15 # instead of {{#int:...}})
17 $parser->setFunctionHook( 'int', array( __CLASS__
, 'intFunction' ), SFH_NO_HASH
);
18 $parser->setFunctionHook( 'ns', array( __CLASS__
, 'ns' ), SFH_NO_HASH
);
19 $parser->setFunctionHook( 'urlencode', array( __CLASS__
, 'urlencode' ), SFH_NO_HASH
);
20 $parser->setFunctionHook( 'lcfirst', array( __CLASS__
, 'lcfirst' ), SFH_NO_HASH
);
21 $parser->setFunctionHook( 'ucfirst', array( __CLASS__
, 'ucfirst' ), SFH_NO_HASH
);
22 $parser->setFunctionHook( 'lc', array( __CLASS__
, 'lc' ), SFH_NO_HASH
);
23 $parser->setFunctionHook( 'uc', array( __CLASS__
, 'uc' ), SFH_NO_HASH
);
24 $parser->setFunctionHook( 'localurl', array( __CLASS__
, 'localurl' ), SFH_NO_HASH
);
25 $parser->setFunctionHook( 'localurle', array( __CLASS__
, 'localurle' ), SFH_NO_HASH
);
26 $parser->setFunctionHook( 'fullurl', array( __CLASS__
, 'fullurl' ), SFH_NO_HASH
);
27 $parser->setFunctionHook( 'fullurle', array( __CLASS__
, 'fullurle' ), SFH_NO_HASH
);
28 $parser->setFunctionHook( 'formatnum', array( __CLASS__
, 'formatnum' ), SFH_NO_HASH
);
29 $parser->setFunctionHook( 'grammar', array( __CLASS__
, 'grammar' ), SFH_NO_HASH
);
30 $parser->setFunctionHook( 'gender', array( __CLASS__
, 'gender' ), SFH_NO_HASH
);
31 $parser->setFunctionHook( 'plural', array( __CLASS__
, 'plural' ), SFH_NO_HASH
);
32 $parser->setFunctionHook( 'numberofpages', array( __CLASS__
, 'numberofpages' ), SFH_NO_HASH
);
33 $parser->setFunctionHook( 'numberofusers', array( __CLASS__
, 'numberofusers' ), SFH_NO_HASH
);
34 $parser->setFunctionHook( 'numberofactiveusers', array( __CLASS__
, 'numberofactiveusers' ), SFH_NO_HASH
);
35 $parser->setFunctionHook( 'numberofarticles', array( __CLASS__
, 'numberofarticles' ), SFH_NO_HASH
);
36 $parser->setFunctionHook( 'numberoffiles', array( __CLASS__
, 'numberoffiles' ), SFH_NO_HASH
);
37 $parser->setFunctionHook( 'numberofadmins', array( __CLASS__
, 'numberofadmins' ), SFH_NO_HASH
);
38 $parser->setFunctionHook( 'numberingroup', array( __CLASS__
, 'numberingroup' ), SFH_NO_HASH
);
39 $parser->setFunctionHook( 'numberofedits', array( __CLASS__
, 'numberofedits' ), SFH_NO_HASH
);
40 $parser->setFunctionHook( 'numberofviews', array( __CLASS__
, 'numberofviews' ), SFH_NO_HASH
);
41 $parser->setFunctionHook( 'language', array( __CLASS__
, 'language' ), SFH_NO_HASH
);
42 $parser->setFunctionHook( 'padleft', array( __CLASS__
, 'padleft' ), SFH_NO_HASH
);
43 $parser->setFunctionHook( 'padright', array( __CLASS__
, 'padright' ), SFH_NO_HASH
);
44 $parser->setFunctionHook( 'anchorencode', array( __CLASS__
, 'anchorencode' ), SFH_NO_HASH
);
45 $parser->setFunctionHook( 'special', array( __CLASS__
, 'special' ) );
46 $parser->setFunctionHook( 'defaultsort', array( __CLASS__
, 'defaultsort' ), SFH_NO_HASH
);
47 $parser->setFunctionHook( 'filepath', array( __CLASS__
, 'filepath' ), SFH_NO_HASH
);
48 $parser->setFunctionHook( 'pagesincategory', array( __CLASS__
, 'pagesincategory' ), SFH_NO_HASH
);
49 $parser->setFunctionHook( 'pagesize', array( __CLASS__
, 'pagesize' ), SFH_NO_HASH
);
50 $parser->setFunctionHook( 'protectionlevel', array( __CLASS__
, 'protectionlevel' ), SFH_NO_HASH
);
51 $parser->setFunctionHook( 'namespace', array( __CLASS__
, 'mwnamespace' ), SFH_NO_HASH
);
52 $parser->setFunctionHook( 'namespacee', array( __CLASS__
, 'namespacee' ), SFH_NO_HASH
);
53 $parser->setFunctionHook( 'talkspace', array( __CLASS__
, 'talkspace' ), SFH_NO_HASH
);
54 $parser->setFunctionHook( 'talkspacee', array( __CLASS__
, 'talkspacee' ), SFH_NO_HASH
);
55 $parser->setFunctionHook( 'subjectspace', array( __CLASS__
, 'subjectspace' ), SFH_NO_HASH
);
56 $parser->setFunctionHook( 'subjectspacee', array( __CLASS__
, 'subjectspacee' ), SFH_NO_HASH
);
57 $parser->setFunctionHook( 'pagename', array( __CLASS__
, 'pagename' ), SFH_NO_HASH
);
58 $parser->setFunctionHook( 'pagenamee', array( __CLASS__
, 'pagenamee' ), SFH_NO_HASH
);
59 $parser->setFunctionHook( 'fullpagename', array( __CLASS__
, 'fullpagename' ), SFH_NO_HASH
);
60 $parser->setFunctionHook( 'fullpagenamee', array( __CLASS__
, 'fullpagenamee' ), SFH_NO_HASH
);
61 $parser->setFunctionHook( 'basepagename', array( __CLASS__
, 'basepagename' ), SFH_NO_HASH
);
62 $parser->setFunctionHook( 'basepagenamee', array( __CLASS__
, 'basepagenamee' ), SFH_NO_HASH
);
63 $parser->setFunctionHook( 'subpagename', array( __CLASS__
, 'subpagename' ), SFH_NO_HASH
);
64 $parser->setFunctionHook( 'subpagenamee', array( __CLASS__
, 'subpagenamee' ), SFH_NO_HASH
);
65 $parser->setFunctionHook( 'talkpagename', array( __CLASS__
, 'talkpagename' ), SFH_NO_HASH
);
66 $parser->setFunctionHook( 'talkpagenamee', array( __CLASS__
, 'talkpagenamee' ), SFH_NO_HASH
);
67 $parser->setFunctionHook( 'subjectpagename', array( __CLASS__
, 'subjectpagename' ), SFH_NO_HASH
);
68 $parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__
, 'subjectpagenamee' ), SFH_NO_HASH
);
69 $parser->setFunctionHook( 'revisionid', array( __CLASS__
, 'revisionid' ), SFH_NO_HASH
);
70 $parser->setFunctionHook( 'revisiontimestamp',array( __CLASS__
, 'revisiontimestamp'), SFH_NO_HASH
);
71 $parser->setFunctionHook( 'revisionday', array( __CLASS__
, 'revisionday' ), SFH_NO_HASH
);
72 $parser->setFunctionHook( 'revisionday2', array( __CLASS__
, 'revisionday2' ), SFH_NO_HASH
);
73 $parser->setFunctionHook( 'revisionmonth', array( __CLASS__
, 'revisionmonth' ), SFH_NO_HASH
);
74 $parser->setFunctionHook( 'revisionyear', array( __CLASS__
, 'revisionyear' ), SFH_NO_HASH
);
75 $parser->setFunctionHook( 'revisionuser', array( __CLASS__
, 'revisionuser' ), SFH_NO_HASH
);
76 $parser->setFunctionHook( 'tag', array( __CLASS__
, 'tagObj' ), SFH_OBJECT_ARGS
);
77 $parser->setFunctionHook( 'formatdate', array( __CLASS__
, 'formatDate' ) );
78 $parser->setFunctionHook( 'groupconvert', array( __CLASS__
, 'groupconvert' ), SFH_NO_HASH
);
80 if ( $wgAllowDisplayTitle ) {
81 $parser->setFunctionHook( 'displaytitle', array( __CLASS__
, 'displaytitle' ), SFH_NO_HASH
);
83 if ( $wgAllowSlowParserFunctions ) {
84 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__
, 'pagesinnamespace' ), SFH_NO_HASH
);
88 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
89 if ( strval( $part1 ) !== '' ) {
90 $args = array_slice( func_get_args(), 2 );
91 $message = wfMsgGetKey( $part1, true, false, false );
92 $message = wfMsgReplaceArgs( $message, $args );
93 $message = $parser->replaceVariables( $message ); // like $wgMessageCache->transform()
96 return array( 'found' => false );
100 static function formatDate( $parser, $date, $defaultPref = null ) {
101 $df = DateFormatter
::getInstance();
105 $pref = $parser->mOptions
->getDateFormat();
107 // Specify a different default date format other than the the normal default
108 // iff the user has 'default' for their setting
109 if ($pref == 'default' && $defaultPref)
110 $pref = $defaultPref;
112 $date = $df->reformat( $pref, $date, array('match-whole') );
116 static function ns( $parser, $part1 = '' ) {
118 if ( intval( $part1 ) ||
$part1 == "0" ) {
119 $index = intval( $part1 );
121 $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
123 if ( $index !== false ) {
124 return $wgContLang->getFormattedNsText( $index );
126 return array( 'found' => false );
130 static function urlencode( $parser, $s = '' ) {
131 return urlencode( $s );
134 static function lcfirst( $parser, $s = '' ) {
136 return $wgContLang->lcfirst( $s );
139 static function ucfirst( $parser, $s = '' ) {
141 return $wgContLang->ucfirst( $s );
144 static function lc( $parser, $s = '' ) {
146 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
147 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
149 return $wgContLang->lc( $s );
153 static function uc( $parser, $s = '' ) {
155 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
156 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
158 return $wgContLang->uc( $s );
162 static function localurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getLocalURL', $s, $arg ); }
163 static function localurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeLocalURL', $s, $arg ); }
164 static function fullurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getFullURL', $s, $arg ); }
165 static function fullurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeFullURL', $s, $arg ); }
167 static function urlFunction( $func, $s = '', $arg = null ) {
168 $title = Title
::newFromText( $s );
169 # Due to order of execution of a lot of bits, the values might be encoded
170 # before arriving here; if that's true, then the title can't be created
171 # and the variable will fail. If we can't get a decent title from the first
172 # attempt, url-decode and try for a second.
173 if( is_null( $title ) )
174 $title = Title
::newFromUrl( urldecode( $s ) );
175 if( !is_null( $title ) ) {
176 # Convert NS_MEDIA -> NS_FILE
177 if( $title->getNamespace() == NS_MEDIA
) {
178 $title = Title
::makeTitle( NS_FILE
, $title->getDBKey() );
180 if( !is_null( $arg ) ) {
181 $text = $title->$func( $arg );
183 $text = $title->$func();
187 return array( 'found' => false );
191 static function formatNum( $parser, $num = '', $raw = null) {
192 if ( self
::israw( $raw ) ) {
193 return $parser->getFunctionLang()->parseFormattedNumber( $num );
195 return $parser->getFunctionLang()->formatNum( $num );
199 static function grammar( $parser, $case = '', $word = '' ) {
200 return $parser->getFunctionLang()->convertGrammar( $word, $case );
203 static function gender( $parser, $user ) {
204 $forms = array_slice( func_get_args(), 2);
207 $gender = User
::getDefaultOption( 'gender' );
210 $title = Title
::newFromText( $user );
212 if (is_object( $title ) && $title->getNamespace() == NS_USER
)
213 $user = $title->getText();
215 // check parameter, or use $wgUser if in interface message
216 $user = User
::newFromName( $user );
218 $gender = $user->getOption( 'gender' );
219 } elseif ( $parser->mOptions
->getInterfaceMessage() ) {
221 $gender = $wgUser->getOption( 'gender' );
223 return $parser->getFunctionLang()->gender( $gender, $forms );
225 static function plural( $parser, $text = '') {
226 $forms = array_slice( func_get_args(), 2);
227 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
228 return $parser->getFunctionLang()->convertPlural( $text, $forms );
232 * Override the title of the page when viewed, provided we've been given a
233 * title which will normalise to the canonical title
235 * @param Parser $parser Parent parser
236 * @param string $text Desired title text
239 static function displaytitle( $parser, $text = '' ) {
240 global $wgRestrictDisplayTitle;
242 #list of disallowed tags for DISPLAYTITLE
243 #these will be escaped even though they are allowed in normal wiki text
244 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li',
245 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp' );
247 #only requested titles that normalize to the actual title are allowed through
248 #mimic the escaping process that occurs in OutputPage::setPageTitle
249 $text = Sanitizer
::normalizeCharReferences( Sanitizer
::removeHTMLtags( $text, null, array(), array(), $bad ) );
250 $title = Title
::newFromText( Sanitizer
::stripAllTags( $text ) );
252 if( !$wgRestrictDisplayTitle ) {
253 $parser->mOutput
->setDisplayTitle( $text );
255 if ( $title instanceof Title
&& $title->getFragment() == '' && $title->equals( $parser->mTitle
) ) {
256 $parser->mOutput
->setDisplayTitle( $text );
263 static function isRaw( $param ) {
266 $mwRaw =& MagicWord
::get( 'rawsuffix' );
268 if ( is_null( $param ) ) {
271 return $mwRaw->match( $param );
275 static function formatRaw( $num, $raw ) {
276 if( self
::isRaw( $raw ) ) {
280 return $wgContLang->formatNum( $num );
283 static function numberofpages( $parser, $raw = null ) {
284 return self
::formatRaw( SiteStats
::pages(), $raw );
286 static function numberofusers( $parser, $raw = null ) {
287 return self
::formatRaw( SiteStats
::users(), $raw );
289 static function numberofactiveusers( $parser, $raw = null ) {
290 return self
::formatRaw( SiteStats
::activeUsers(), $raw );
292 static function numberofarticles( $parser, $raw = null ) {
293 return self
::formatRaw( SiteStats
::articles(), $raw );
295 static function numberoffiles( $parser, $raw = null ) {
296 return self
::formatRaw( SiteStats
::images(), $raw );
298 static function numberofadmins( $parser, $raw = null ) {
299 return self
::formatRaw( SiteStats
::numberingroup('sysop'), $raw );
301 static function numberofedits( $parser, $raw = null ) {
302 return self
::formatRaw( SiteStats
::edits(), $raw );
304 static function numberofviews( $parser, $raw = null ) {
305 return self
::formatRaw( SiteStats
::views(), $raw );
307 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
308 return self
::formatRaw( SiteStats
::pagesInNs( intval( $namespace ) ), $raw );
310 static function numberingroup( $parser, $name = '', $raw = null) {
311 return self
::formatRaw( SiteStats
::numberingroup( strtolower( $name ) ), $raw );
316 * Given a title, return the namespace name that would be given by the
317 * corresponding magic word
318 * Note: function name changed to "mwnamespace" rather than "namespace"
319 * to not break PHP 5.3
321 static function mwnamespace( $parser, $title = null ) {
322 $t = Title
::newFromText( $title );
325 return str_replace( '_', ' ', $t->getNsText() );
327 static function namespacee( $parser, $title = null ) {
328 $t = Title
::newFromText( $title );
331 return wfUrlencode( $t->getNsText() );
333 static function talkspace( $parser, $title = null ) {
334 $t = Title
::newFromText( $title );
335 if ( is_null($t) ||
!$t->canTalk() )
337 return str_replace( '_', ' ', $t->getTalkNsText() );
339 static function talkspacee( $parser, $title = null ) {
340 $t = Title
::newFromText( $title );
341 if ( is_null($t) ||
!$t->canTalk() )
343 return wfUrlencode( $t->getTalkNsText() );
345 static function subjectspace( $parser, $title = null ) {
346 $t = Title
::newFromText( $title );
349 return str_replace( '_', ' ', $t->getSubjectNsText() );
351 static function subjectspacee( $parser, $title = null ) {
352 $t = Title
::newFromText( $title );
355 return wfUrlencode( $t->getSubjectNsText() );
358 * Functions to get and normalize pagenames, corresponding to the magic words
361 static function pagename( $parser, $title = null ) {
362 $t = Title
::newFromText( $title );
365 return wfEscapeWikiText( $t->getText() );
367 static function pagenamee( $parser, $title = null ) {
368 $t = Title
::newFromText( $title );
371 return $t->getPartialURL();
373 static function fullpagename( $parser, $title = null ) {
374 $t = Title
::newFromText( $title );
375 if ( is_null($t) ||
!$t->canTalk() )
377 return wfEscapeWikiText( $t->getPrefixedText() );
379 static function fullpagenamee( $parser, $title = null ) {
380 $t = Title
::newFromText( $title );
381 if ( is_null($t) ||
!$t->canTalk() )
383 return $t->getPrefixedURL();
385 static function subpagename( $parser, $title = null ) {
386 $t = Title
::newFromText( $title );
389 return $t->getSubpageText();
391 static function subpagenamee( $parser, $title = null ) {
392 $t = Title
::newFromText( $title );
395 return $t->getSubpageUrlForm();
397 static function basepagename( $parser, $title = null ) {
398 $t = Title
::newFromText( $title );
401 return $t->getBaseText();
403 static function basepagenamee( $parser, $title = null ) {
404 $t = Title
::newFromText( $title );
407 return wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) );
409 static function talkpagename( $parser, $title = null ) {
410 $t = Title
::newFromText( $title );
411 if ( is_null($t) ||
!$t->canTalk() )
413 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
415 static function talkpagenamee( $parser, $title = null ) {
416 $t = Title
::newFromText( $title );
417 if ( is_null($t) ||
!$t->canTalk() )
419 return $t->getTalkPage()->getPrefixedUrl();
421 static function subjectpagename( $parser, $title = null ) {
422 $t = Title
::newFromText( $title );
425 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
427 static function subjectpagenamee( $parser, $title = null ) {
428 $t = Title
::newFromText( $title );
431 return $t->getSubjectPage()->getPrefixedUrl();
434 * Functions to get revision informations, corresponding to the magic words
437 static function revisionid( $parser, $title = null ) {
438 static $cache = array ();
439 $t = Title
::newFromText( $title );
442 if ( $t->equals( $parser->getTitle() ) ) {
443 // Let the edit saving system know we should parse the page
444 // *after* a revision ID has been assigned.
445 $parser->mOutput
->setFlag( 'vary-revision' );
446 wfDebug( __METHOD__
. ": {{REVISIONID}} used, setting vary-revision...\n" );
447 return $parser->getRevisionId();
449 if ( isset( $cache[$t->getPrefixedText()] ) )
450 return $cache[$t->getPrefixedText()];
451 elseif ( $parser->incrementExpensiveFunctionCount() ) {
452 $a = new Article( $t );
453 return $cache[$t->getPrefixedText()] = $a->getRevIdFetched();
457 static function revisiontimestamp( $parser, $title = null ) {
458 static $cache = array ();
459 $t = Title
::newFromText( $title );
462 if ( $t->equals( $parser->getTitle() ) ) {
463 // Let the edit saving system know we should parse the page
464 // *after* a revision ID has been assigned. This is for null edits.
465 $parser->mOutput
->setFlag( 'vary-revision' );
466 wfDebug( __METHOD__
. ": {{REVISIONTIMESTAMP}} or related parser function used, setting vary-revision...\n" );
467 return $parser->getRevisionTimestamp();
469 if ( isset( $cache[$t->getPrefixedText()] ) )
470 return $cache[$t->getPrefixedText()];
471 elseif ( $parser->incrementExpensiveFunctionCount() ) {
472 $a = new Article( $t );
473 return $cache[$t->getPrefixedText()] = $a->getTimestamp();
477 static function revisionday( $parser, $title = null ) {
478 $timestamp = self
::revisiontimestamp( $parser, $title );
479 if ( $timestamp == '' ) return '';
480 return intval( substr( $timestamp, 6, 2 ) );
482 static function revisionday2( $parser, $title = null ) {
483 $timestamp = self
::revisiontimestamp( $parser, $title );
484 if ( $timestamp == '' ) return '';
485 return substr( $timestamp, 6, 2 );
487 static function revisionmonth( $parser, $title = null ) {
488 $timestamp = self
::revisiontimestamp( $parser, $title );
489 if ( $timestamp == '' ) return '';
490 return intval( substr( $timestamp, 4, 2 ) );
492 static function revisionyear( $parser, $title = null ) {
493 $timestamp = self
::revisiontimestamp( $parser, $title );
494 if ( $timestamp == '' ) return '';
495 return substr( $timestamp, 0, 4 );
497 static function revisionuser( $parser, $title = null ) {
498 static $cache = array();
499 $t = Title
::newFromText( $title );
502 if ( $t->equals( $parser->getTitle() ) ) {
503 // Let the edit saving system know we should parse the page
504 // *after* a revision ID has been assigned. This is for null edits.
505 $parser->mOutput
->setFlag( 'vary-revision' );
506 wfDebug( __METHOD__
. ": {{REVISIONUSER}} used, setting vary-revision...\n" );
507 return $parser->getRevisionUser();
509 if ( isset( $cache[$t->getPrefixedText()] ) )
510 return $cache[$t->getPrefixedText()];
511 elseif ( $parser->incrementExpensiveFunctionCount() ) {
512 $a = new Article( $t );
513 return $cache[$t->getPrefixedText()] = $a->getUserText();
519 * Return the number of pages in the given category, or 0 if it's nonexis-
520 * tent. This is an expensive parser function and can't be called too many
523 static function pagesincategory( $parser, $name = '', $raw = null ) {
524 static $cache = array();
525 $category = Category
::newFromName( $name );
527 if( !is_object( $category ) ) {
529 return self
::formatRaw( 0, $raw );
532 # Normalize name for cache
533 $name = $category->getName();
536 if( isset( $cache[$name] ) ) {
537 $count = $cache[$name];
538 } elseif( $parser->incrementExpensiveFunctionCount() ) {
539 $count = $cache[$name] = (int)$category->getPageCount();
541 return self
::formatRaw( $count, $raw );
545 * Return the size of the given page, or 0 if it's nonexistent. This is an
546 * expensive parser function and can't be called too many times per page.
548 * @FIXME This doesn't work correctly on preview for getting the size of
550 * @FIXME Title::getLength() documentation claims that it adds things to
551 * the link cache, so the local cache here should be unnecessary, but in
552 * fact calling getLength() repeatedly for the same $page does seem to
553 * run one query for each call?
555 static function pagesize( $parser, $page = '', $raw = null ) {
556 static $cache = array();
557 $title = Title
::newFromText($page);
559 if( !is_object( $title ) ) {
561 return self
::formatRaw( 0, $raw );
564 # Normalize name for cache
565 $page = $title->getPrefixedText();
568 if( isset( $cache[$page] ) ) {
569 $length = $cache[$page];
570 } elseif( $parser->incrementExpensiveFunctionCount() ) {
571 $rev = Revision
::newFromTitle($title);
572 $id = $rev ?
$rev->getPage() : 0;
573 $length = $cache[$page] = $rev ?
$rev->getSize() : 0;
575 // Register dependency in templatelinks
576 $parser->mOutput
->addTemplate( $title, $id, $rev ?
$rev->getId() : 0 );
578 return self
::formatRaw( $length, $raw );
582 * Returns the requested protection level for the current page
584 static function protectionlevel( $parser, $type = '' ) {
585 $restrictions = $parser->mTitle
->getRestrictions( strtolower( $type ) );
586 # Title::getRestrictions returns an array, its possible it may have
587 # multiple values in the future
588 return implode( $restrictions, ',' );
591 static function language( $parser, $arg = '' ) {
593 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
594 return $lang != '' ?
$lang : $arg;
598 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
600 static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT
) {
601 $lengthOfPadding = mb_strlen( $padding );
602 if ( $lengthOfPadding == 0 ) return $string;
604 # The remaining length to add counts down to 0 as padding is added
605 $length = min( $length, 500 ) - mb_strlen( $string );
606 # $finalPadding is just $padding repeated enough times so that
607 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
609 while ( $length > 0 ) {
610 # If $length < $lengthofPadding, truncate $padding so we get the
611 # exact length desired.
612 $finalPadding .= mb_substr( $padding, 0, $length );
613 $length -= $lengthOfPadding;
616 if ( $direction == STR_PAD_LEFT
) {
617 return $finalPadding . $string;
619 return $string . $finalPadding;
623 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
624 return self
::pad( $string, $length, $padding, STR_PAD_LEFT
);
627 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
628 return self
::pad( $string, $length, $padding );
631 static function anchorencode( $parser, $text ) {
632 $a = urlencode( $text );
633 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
634 # leave colons alone, however
635 $a = str_replace( '.3A', ':', $a );
639 static function special( $parser, $text ) {
640 $title = SpecialPage
::getTitleForAlias( $text );
642 return $title->getPrefixedText();
644 return wfMsgForContent( 'nosuchspecialpage' );
648 public static function defaultsort( $parser, $text ) {
649 $text = trim( $text );
650 if( strlen( $text ) == 0 )
652 $old = $parser->getCustomDefaultSort();
653 $parser->setDefaultSort( $text );
654 if( $old === false ||
$old == $text )
657 return( '<span class="error">' .
658 wfMsg( 'duplicate-defaultsort',
659 htmlspecialchars( $old ),
660 htmlspecialchars( $text ) ) .
664 public static function filepath( $parser, $name='', $option='' ) {
665 $file = wfFindFile( $name );
667 $url = $file->getFullUrl();
668 if( $option == 'nowiki' ) {
669 return array( $url, 'nowiki' => true );
678 * Parser function to extension tag adaptor
680 public static function tagObj( $parser, $frame, $args ) {
682 if ( !count( $args ) ) {
685 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
687 if ( count( $args ) ) {
688 $inner = $frame->expand( array_shift( $args ) );
693 $stripList = $parser->getStripList();
694 if ( !in_array( $tagName, $stripList ) ) {
695 return '<span class="error">' .
696 wfMsg( 'unknown_extension_tag', $tagName ) .
700 $attributes = array();
701 foreach ( $args as $arg ) {
702 $bits = $arg->splitArg();
703 if ( strval( $bits['index'] ) === '' ) {
704 $name = trim( $frame->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
705 $value = trim( $frame->expand( $bits['value'] ) );
706 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
707 $value = isset( $m[1] ) ?
$m[1] : '';
709 $attributes[$name] = $value;
716 'attributes' => $attributes,
717 'close' => "</$tagName>",
719 return $parser->extensionSubstitution( $params, $frame );
723 * magic word call for a group convert from LanguageConverter.
725 public static function groupconvert( $parser, $group ) {
727 return $wgContLang->groupConvert( $group );