308a4e8c44dda7863c9b8f281d374b2c6f10337b
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( 'numberofcontribs', array( __CLASS__
, 'numberofcontribs' ), 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( 'revisionid', array( __CLASS__
, 'revisionid' ), SFH_NO_HASH
);
71 $parser->setFunctionHook( 'revisiontimestamp',array( __CLASS__
, 'revisiontimestamp'), SFH_NO_HASH
);
72 $parser->setFunctionHook( 'revisionday', array( __CLASS__
, 'revisionday' ), SFH_NO_HASH
);
73 $parser->setFunctionHook( 'revisionday2', array( __CLASS__
, 'revisionday2' ), SFH_NO_HASH
);
74 $parser->setFunctionHook( 'revisionmonth', array( __CLASS__
, 'revisionmonth' ), SFH_NO_HASH
);
75 $parser->setFunctionHook( 'revisionyear', array( __CLASS__
, 'revisionyear' ), SFH_NO_HASH
);
76 $parser->setFunctionHook( 'revisionuser', array( __CLASS__
, 'revisionuser' ), SFH_NO_HASH
);
77 $parser->setFunctionHook( 'tag', array( __CLASS__
, 'tagObj' ), SFH_OBJECT_ARGS
);
78 $parser->setFunctionHook( 'formatdate', array( __CLASS__
, 'formatDate' ) );
79 $parser->setFunctionHook( 'groupconvert', array( __CLASS__
, 'groupconvert' ), SFH_NO_HASH
);
81 if ( $wgAllowDisplayTitle ) {
82 $parser->setFunctionHook( 'displaytitle', array( __CLASS__
, 'displaytitle' ), SFH_NO_HASH
);
84 if ( $wgAllowSlowParserFunctions ) {
85 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__
, 'pagesinnamespace' ), SFH_NO_HASH
);
89 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
90 if ( strval( $part1 ) !== '' ) {
91 $args = array_slice( func_get_args(), 2 );
92 $message = wfMsgGetKey( $part1, true, false, false );
93 $message = wfMsgReplaceArgs( $message, $args );
94 $message = $parser->replaceVariables( $message ); // like $wgMessageCache->transform()
97 return array( 'found' => false );
101 static function formatDate( $parser, $date, $defaultPref = null ) {
102 $df = DateFormatter
::getInstance();
106 $pref = $parser->mOptions
->getDateFormat();
108 // Specify a different default date format other than the the normal default
109 // iff the user has 'default' for their setting
110 if ($pref == 'default' && $defaultPref)
111 $pref = $defaultPref;
113 $date = $df->reformat( $pref, $date, array('match-whole') );
117 static function ns( $parser, $part1 = '' ) {
119 if ( intval( $part1 ) ||
$part1 == "0" ) {
120 $index = intval( $part1 );
122 $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
124 if ( $index !== false ) {
125 return $wgContLang->getFormattedNsText( $index );
127 return array( 'found' => false );
131 static function urlencode( $parser, $s = '' ) {
132 return urlencode( $s );
135 static function lcfirst( $parser, $s = '' ) {
137 return $wgContLang->lcfirst( $s );
140 static function ucfirst( $parser, $s = '' ) {
142 return $wgContLang->ucfirst( $s );
145 static function lc( $parser, $s = '' ) {
147 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
148 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
150 return $wgContLang->lc( $s );
154 static function uc( $parser, $s = '' ) {
156 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
157 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
159 return $wgContLang->uc( $s );
163 static function localurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getLocalURL', $s, $arg ); }
164 static function localurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeLocalURL', $s, $arg ); }
165 static function fullurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getFullURL', $s, $arg ); }
166 static function fullurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeFullURL', $s, $arg ); }
168 static function urlFunction( $func, $s = '', $arg = null ) {
169 $title = Title
::newFromText( $s );
170 # Due to order of execution of a lot of bits, the values might be encoded
171 # before arriving here; if that's true, then the title can't be created
172 # and the variable will fail. If we can't get a decent title from the first
173 # attempt, url-decode and try for a second.
174 if( is_null( $title ) )
175 $title = Title
::newFromUrl( urldecode( $s ) );
176 if( !is_null( $title ) ) {
177 # Convert NS_MEDIA -> NS_FILE
178 if( $title->getNamespace() == NS_MEDIA
) {
179 $title = Title
::makeTitle( NS_FILE
, $title->getDBkey() );
181 if( !is_null( $arg ) ) {
182 $text = $title->$func( $arg );
184 $text = $title->$func();
188 return array( 'found' => false );
192 static function formatNum( $parser, $num = '', $raw = null) {
193 if ( self
::israw( $raw ) ) {
194 return $parser->getFunctionLang()->parseFormattedNumber( $num );
196 return $parser->getFunctionLang()->formatNum( $num );
200 static function grammar( $parser, $case = '', $word = '' ) {
201 return $parser->getFunctionLang()->convertGrammar( $word, $case );
204 static function gender( $parser, $user ) {
205 wfProfileIn( __METHOD__
);
206 $forms = array_slice( func_get_args(), 2);
209 $gender = User
::getDefaultOption( 'gender' );
212 $title = Title
::newFromText( $user );
214 if (is_object( $title ) && $title->getNamespace() == NS_USER
)
215 $user = $title->getText();
217 // check parameter, or use $wgUser if in interface message
218 $user = User
::newFromName( $user );
220 $gender = $user->getOption( 'gender' );
221 } elseif ( $parser->mOptions
->getInterfaceMessage() ) {
223 $gender = $wgUser->getOption( 'gender' );
225 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
226 wfProfileOut( __METHOD__
);
229 static function plural( $parser, $text = '') {
230 $forms = array_slice( func_get_args(), 2);
231 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
232 return $parser->getFunctionLang()->convertPlural( $text, $forms );
236 * Override the title of the page when viewed, provided we've been given a
237 * title which will normalise to the canonical title
239 * @param Parser $parser Parent parser
240 * @param string $text Desired title text
243 static function displaytitle( $parser, $text = '' ) {
244 global $wgRestrictDisplayTitle;
246 #list of disallowed tags for DISPLAYTITLE
247 #these will be escaped even though they are allowed in normal wiki text
248 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li',
249 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp' );
251 #only requested titles that normalize to the actual title are allowed through
252 #mimic the escaping process that occurs in OutputPage::setPageTitle
253 $text = Sanitizer
::normalizeCharReferences( Sanitizer
::removeHTMLtags( $text, null, array(), array(), $bad ) );
254 $title = Title
::newFromText( Sanitizer
::stripAllTags( $text ) );
256 if( !$wgRestrictDisplayTitle ) {
257 $parser->mOutput
->setDisplayTitle( $text );
259 if ( $title instanceof Title
&& $title->getFragment() == '' && $title->equals( $parser->mTitle
) ) {
260 $parser->mOutput
->setDisplayTitle( $text );
267 static function isRaw( $param ) {
270 $mwRaw =& MagicWord
::get( 'rawsuffix' );
272 if ( is_null( $param ) ) {
275 return $mwRaw->match( $param );
279 static function formatRaw( $num, $raw ) {
280 if( self
::isRaw( $raw ) ) {
284 return $wgContLang->formatNum( $num );
287 static function numberofpages( $parser, $raw = null ) {
288 return self
::formatRaw( SiteStats
::pages(), $raw );
290 static function numberofusers( $parser, $raw = null ) {
291 return self
::formatRaw( SiteStats
::users(), $raw );
293 static function numberofactiveusers( $parser, $raw = null ) {
294 return self
::formatRaw( SiteStats
::activeUsers(), $raw );
296 static function numberofarticles( $parser, $raw = null ) {
297 return self
::formatRaw( SiteStats
::articles(), $raw );
299 static function numberoffiles( $parser, $raw = null ) {
300 return self
::formatRaw( SiteStats
::images(), $raw );
302 static function numberofadmins( $parser, $raw = null ) {
303 return self
::formatRaw( SiteStats
::numberingroup('sysop'), $raw );
305 static function numberofedits( $parser, $raw = null ) {
306 return self
::formatRaw( SiteStats
::edits(), $raw );
308 static function numberofviews( $parser, $raw = null ) {
309 return self
::formatRaw( SiteStats
::views(), $raw );
311 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
312 return self
::formatRaw( SiteStats
::pagesInNs( intval( $namespace ) ), $raw );
314 static function numberingroup( $parser, $name = '', $raw = null) {
315 return self
::formatRaw( SiteStats
::numberingroup( strtolower( $name ) ), $raw );
320 * Given a title, return the namespace name that would be given by the
321 * corresponding magic word
322 * Note: function name changed to "mwnamespace" rather than "namespace"
323 * to not break PHP 5.3
325 static function mwnamespace( $parser, $title = null ) {
326 $t = Title
::newFromText( $title );
329 return str_replace( '_', ' ', $t->getNsText() );
331 static function namespacee( $parser, $title = null ) {
332 $t = Title
::newFromText( $title );
335 return wfUrlencode( $t->getNsText() );
337 static function talkspace( $parser, $title = null ) {
338 $t = Title
::newFromText( $title );
339 if ( is_null($t) ||
!$t->canTalk() )
341 return str_replace( '_', ' ', $t->getTalkNsText() );
343 static function talkspacee( $parser, $title = null ) {
344 $t = Title
::newFromText( $title );
345 if ( is_null($t) ||
!$t->canTalk() )
347 return wfUrlencode( $t->getTalkNsText() );
349 static function subjectspace( $parser, $title = null ) {
350 $t = Title
::newFromText( $title );
353 return str_replace( '_', ' ', $t->getSubjectNsText() );
355 static function subjectspacee( $parser, $title = null ) {
356 $t = Title
::newFromText( $title );
359 return wfUrlencode( $t->getSubjectNsText() );
362 * Functions to get and normalize pagenames, corresponding to the magic words
365 static function pagename( $parser, $title = null ) {
366 $t = Title
::newFromText( $title );
369 return wfEscapeWikiText( $t->getText() );
371 static function pagenamee( $parser, $title = null ) {
372 $t = Title
::newFromText( $title );
375 return $t->getPartialURL();
377 static function fullpagename( $parser, $title = null ) {
378 $t = Title
::newFromText( $title );
379 if ( is_null($t) ||
!$t->canTalk() )
381 return wfEscapeWikiText( $t->getPrefixedText() );
383 static function fullpagenamee( $parser, $title = null ) {
384 $t = Title
::newFromText( $title );
385 if ( is_null($t) ||
!$t->canTalk() )
387 return $t->getPrefixedURL();
389 static function subpagename( $parser, $title = null ) {
390 $t = Title
::newFromText( $title );
393 return $t->getSubpageText();
395 static function subpagenamee( $parser, $title = null ) {
396 $t = Title
::newFromText( $title );
399 return $t->getSubpageUrlForm();
401 static function basepagename( $parser, $title = null ) {
402 $t = Title
::newFromText( $title );
405 return $t->getBaseText();
407 static function basepagenamee( $parser, $title = null ) {
408 $t = Title
::newFromText( $title );
411 return wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) );
413 static function talkpagename( $parser, $title = null ) {
414 $t = Title
::newFromText( $title );
415 if ( is_null($t) ||
!$t->canTalk() )
417 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
419 static function talkpagenamee( $parser, $title = null ) {
420 $t = Title
::newFromText( $title );
421 if ( is_null($t) ||
!$t->canTalk() )
423 return $t->getTalkPage()->getPrefixedUrl();
425 static function subjectpagename( $parser, $title = null ) {
426 $t = Title
::newFromText( $title );
429 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
431 static function subjectpagenamee( $parser, $title = null ) {
432 $t = Title
::newFromText( $title );
435 return $t->getSubjectPage()->getPrefixedUrl();
438 * Functions to get revision informations, corresponding to the magic words
441 static function revisionid( $parser, $title = null ) {
442 static $cache = array ();
443 $t = Title
::newFromText( $title );
446 if ( $t->equals( $parser->getTitle() ) ) {
447 // Let the edit saving system know we should parse the page
448 // *after* a revision ID has been assigned.
449 $parser->mOutput
->setFlag( 'vary-revision' );
450 wfDebug( __METHOD__
. ": {{REVISIONID}} used, setting vary-revision...\n" );
451 return $parser->getRevisionId();
453 if ( isset( $cache[$t->getPrefixedText()] ) )
454 return $cache[$t->getPrefixedText()];
455 elseif ( $parser->incrementExpensiveFunctionCount() ) {
456 $a = new Article( $t );
457 return $cache[$t->getPrefixedText()] = $a->getRevIdFetched();
461 static function revisiontimestamp( $parser, $title = null ) {
462 static $cache = array ();
463 $t = Title
::newFromText( $title );
466 if ( $t->equals( $parser->getTitle() ) ) {
467 // Let the edit saving system know we should parse the page
468 // *after* a revision ID has been assigned. This is for null edits.
469 $parser->mOutput
->setFlag( 'vary-revision' );
470 wfDebug( __METHOD__
. ": {{REVISIONTIMESTAMP}} or related parser function used, setting vary-revision...\n" );
471 return $parser->getRevisionTimestamp();
473 if ( isset( $cache[$t->getPrefixedText()] ) )
474 return $cache[$t->getPrefixedText()];
475 elseif ( $parser->incrementExpensiveFunctionCount() ) {
476 $a = new Article( $t );
477 return $cache[$t->getPrefixedText()] = $a->getTimestamp();
481 static function revisionday( $parser, $title = null ) {
482 $timestamp = self
::revisiontimestamp( $parser, $title );
483 if ( $timestamp == '' ) return '';
484 return intval( substr( $timestamp, 6, 2 ) );
486 static function revisionday2( $parser, $title = null ) {
487 $timestamp = self
::revisiontimestamp( $parser, $title );
488 if ( $timestamp == '' ) return '';
489 return substr( $timestamp, 6, 2 );
491 static function revisionmonth( $parser, $title = null ) {
492 $timestamp = self
::revisiontimestamp( $parser, $title );
493 if ( $timestamp == '' ) return '';
494 return intval( substr( $timestamp, 4, 2 ) );
496 static function revisionyear( $parser, $title = null ) {
497 $timestamp = self
::revisiontimestamp( $parser, $title );
498 if ( $timestamp == '' ) return '';
499 return substr( $timestamp, 0, 4 );
501 static function revisionuser( $parser, $title = null ) {
502 static $cache = array();
503 $t = Title
::newFromText( $title );
506 if ( $t->equals( $parser->getTitle() ) ) {
507 // Let the edit saving system know we should parse the page
508 // *after* a revision ID has been assigned. This is for null edits.
509 $parser->mOutput
->setFlag( 'vary-revision' );
510 wfDebug( __METHOD__
. ": {{REVISIONUSER}} used, setting vary-revision...\n" );
511 return $parser->getRevisionUser();
513 if ( isset( $cache[$t->getPrefixedText()] ) )
514 return $cache[$t->getPrefixedText()];
515 elseif ( $parser->incrementExpensiveFunctionCount() ) {
516 $a = new Article( $t );
517 return $cache[$t->getPrefixedText()] = $a->getUserText();
523 * Return the number of pages in the given category, or 0 if it's nonexis-
524 * tent. This is an expensive parser function and can't be called too many
527 static function pagesincategory( $parser, $name = '', $raw = null ) {
528 static $cache = array();
529 $category = Category
::newFromName( $name );
531 if( !is_object( $category ) ) {
533 return self
::formatRaw( 0, $raw );
536 # Normalize name for cache
537 $name = $category->getName();
540 if( isset( $cache[$name] ) ) {
541 $count = $cache[$name];
542 } elseif( $parser->incrementExpensiveFunctionCount() ) {
543 $count = $cache[$name] = (int)$category->getPageCount();
545 return self
::formatRaw( $count, $raw );
549 * Return the size of the given page, or 0 if it's nonexistent. This is an
550 * expensive parser function and can't be called too many times per page.
552 * @FIXME This doesn't work correctly on preview for getting the size of
554 * @FIXME Title::getLength() documentation claims that it adds things to
555 * the link cache, so the local cache here should be unnecessary, but in
556 * fact calling getLength() repeatedly for the same $page does seem to
557 * run one query for each call?
559 static function pagesize( $parser, $page = '', $raw = null ) {
560 static $cache = array();
561 $title = Title
::newFromText($page);
563 if( !is_object( $title ) ) {
565 return self
::formatRaw( 0, $raw );
568 # Normalize name for cache
569 $page = $title->getPrefixedText();
572 if( isset( $cache[$page] ) ) {
573 $length = $cache[$page];
574 } elseif( $parser->incrementExpensiveFunctionCount() ) {
575 $rev = Revision
::newFromTitle($title);
576 $id = $rev ?
$rev->getPage() : 0;
577 $length = $cache[$page] = $rev ?
$rev->getSize() : 0;
579 // Register dependency in templatelinks
580 $parser->mOutput
->addTemplate( $title, $id, $rev ?
$rev->getId() : 0 );
582 return self
::formatRaw( $length, $raw );
586 * Returns the requested protection level for the current page
588 static function protectionlevel( $parser, $type = '' ) {
589 $restrictions = $parser->mTitle
->getRestrictions( strtolower( $type ) );
590 # Title::getRestrictions returns an array, its possible it may have
591 # multiple values in the future
592 return implode( $restrictions, ',' );
595 static function language( $parser, $arg = '' ) {
597 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
598 return $lang != '' ?
$lang : $arg;
602 * Returns the number of contributions by a certain user. This is an
603 * expensive parser function and can't be called too many times per page
605 static function numberofcontribs( $parser, $user = null, $raw = null ) {
606 if ( is_null($user) ||
!User
::isValidUserName( $user ) ) {
609 if ( !$parser->incrementExpensiveFunctionCount() ) {
612 $u = User
::newFromName( $user );
613 $id = User
::idFromName( $u->mName
);
614 return self
::formatRaw( User
::edits( $id ), $raw );
618 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
620 static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT
) {
621 $lengthOfPadding = mb_strlen( $padding );
622 if ( $lengthOfPadding == 0 ) return $string;
624 # The remaining length to add counts down to 0 as padding is added
625 $length = min( $length, 500 ) - mb_strlen( $string );
626 # $finalPadding is just $padding repeated enough times so that
627 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
629 while ( $length > 0 ) {
630 # If $length < $lengthofPadding, truncate $padding so we get the
631 # exact length desired.
632 $finalPadding .= mb_substr( $padding, 0, $length );
633 $length -= $lengthOfPadding;
636 if ( $direction == STR_PAD_LEFT
) {
637 return $finalPadding . $string;
639 return $string . $finalPadding;
643 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
644 return self
::pad( $string, $length, $padding, STR_PAD_LEFT
);
647 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
648 return self
::pad( $string, $length, $padding );
651 static function anchorencode( $parser, $text ) {
652 $a = urlencode( $text );
653 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
654 # leave colons alone, however
655 $a = str_replace( '.3A', ':', $a );
659 static function special( $parser, $text ) {
660 $title = SpecialPage
::getTitleForAlias( $text );
662 return $title->getPrefixedText();
664 return wfMsgForContent( 'nosuchspecialpage' );
668 public static function defaultsort( $parser, $text ) {
669 $text = trim( $text );
670 if( strlen( $text ) == 0 )
672 $old = $parser->getCustomDefaultSort();
673 $parser->setDefaultSort( $text );
674 if( $old === false ||
$old == $text )
677 return( '<span class="error">' .
678 wfMsg( 'duplicate-defaultsort',
679 htmlspecialchars( $old ),
680 htmlspecialchars( $text ) ) .
684 public static function filepath( $parser, $name='', $option='' ) {
685 $file = wfFindFile( $name );
687 $url = $file->getFullUrl();
688 if( $option == 'nowiki' ) {
689 return array( $url, 'nowiki' => true );
698 * Parser function to extension tag adaptor
700 public static function tagObj( $parser, $frame, $args ) {
702 if ( !count( $args ) ) {
705 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
707 if ( count( $args ) ) {
708 $inner = $frame->expand( array_shift( $args ) );
713 $stripList = $parser->getStripList();
714 if ( !in_array( $tagName, $stripList ) ) {
715 return '<span class="error">' .
716 wfMsg( 'unknown_extension_tag', $tagName ) .
720 $attributes = array();
721 foreach ( $args as $arg ) {
722 $bits = $arg->splitArg();
723 if ( strval( $bits['index'] ) === '' ) {
724 $name = trim( $frame->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
725 $value = trim( $frame->expand( $bits['value'] ) );
726 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
727 $value = isset( $m[1] ) ?
$m[1] : '';
729 $attributes[$name] = $value;
736 'attributes' => $attributes,
737 'close' => "</$tagName>",
739 return $parser->extensionSubstitution( $params, $frame );
743 * magic word call for a group convert from LanguageConverter.
745 public static function groupconvert( $parser, $group ) {
747 return $wgContLang->groupConvert( $group );