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( 'tag', array( __CLASS__
, 'tagObj' ), SFH_OBJECT_ARGS
);
70 $parser->setFunctionHook( 'formatdate', array( __CLASS__
, 'formatDate' ) );
72 if ( $wgAllowDisplayTitle ) {
73 $parser->setFunctionHook( 'displaytitle', array( __CLASS__
, 'displaytitle' ), SFH_NO_HASH
);
75 if ( $wgAllowSlowParserFunctions ) {
76 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__
, 'pagesinnamespace' ), SFH_NO_HASH
);
80 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
81 if ( strval( $part1 ) !== '' ) {
82 $args = array_slice( func_get_args(), 2 );
83 $message = wfMsgGetKey( $part1, true, false, false );
84 $message = wfMsgReplaceArgs( $message, $args );
85 $message = $parser->replaceVariables( $message ); // like $wgMessageCache->transform()
88 return array( 'found' => false );
92 static function formatDate( $parser, $date, $defaultPref = null ) {
93 $df = DateFormatter
::getInstance();
97 $pref = $parser->mOptions
->getDateFormat();
99 // Specify a different default date format other than the the normal default
100 // iff the user has 'default' for their setting
101 if ($pref == 'default' && $defaultPref)
102 $pref = $defaultPref;
104 $date = $df->reformat( $pref, $date, array('match-whole') );
108 static function ns( $parser, $part1 = '' ) {
110 if ( intval( $part1 ) ||
$part1 == "0" ) {
111 $index = intval( $part1 );
113 $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
115 if ( $index !== false ) {
116 return $wgContLang->getFormattedNsText( $index );
118 return array( 'found' => false );
122 static function urlencode( $parser, $s = '' ) {
123 return urlencode( $s );
126 static function lcfirst( $parser, $s = '' ) {
128 return $wgContLang->lcfirst( $s );
131 static function ucfirst( $parser, $s = '' ) {
133 return $wgContLang->ucfirst( $s );
136 static function lc( $parser, $s = '' ) {
138 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
139 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
141 return $wgContLang->lc( $s );
145 static function uc( $parser, $s = '' ) {
147 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
148 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
150 return $wgContLang->uc( $s );
154 static function localurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getLocalURL', $s, $arg ); }
155 static function localurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeLocalURL', $s, $arg ); }
156 static function fullurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getFullURL', $s, $arg ); }
157 static function fullurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeFullURL', $s, $arg ); }
159 static function urlFunction( $func, $s = '', $arg = null ) {
160 $title = Title
::newFromText( $s );
161 # Due to order of execution of a lot of bits, the values might be encoded
162 # before arriving here; if that's true, then the title can't be created
163 # and the variable will fail. If we can't get a decent title from the first
164 # attempt, url-decode and try for a second.
165 if( is_null( $title ) )
166 $title = Title
::newFromUrl( urldecode( $s ) );
167 if( !is_null( $title ) ) {
168 # Convert NS_MEDIA -> NS_FILE
169 if( $title->getNamespace() == NS_MEDIA
) {
170 $title = Title
::makeTitle( NS_FILE
, $title->getDBKey() );
172 if( !is_null( $arg ) ) {
173 $text = $title->$func( $arg );
175 $text = $title->$func();
179 return array( 'found' => false );
183 static function formatNum( $parser, $num = '', $raw = null) {
184 if ( self
::israw( $raw ) ) {
185 return $parser->getFunctionLang()->parseFormattedNumber( $num );
187 return $parser->getFunctionLang()->formatNum( $num );
191 static function grammar( $parser, $case = '', $word = '' ) {
192 return $parser->getFunctionLang()->convertGrammar( $word, $case );
195 static function gender( $parser, $user ) {
196 $forms = array_slice( func_get_args(), 2);
199 $gender = User
::getDefaultOption( 'gender' );
202 $title = Title
::newFromText( $user );
204 if (is_object( $title ) && $title->getNamespace() == NS_USER
)
205 $user = $title->getText();
207 // check parameter, or use $wgUser if in interface message
208 $user = User
::newFromName( $user );
210 $gender = $user->getOption( 'gender' );
211 } elseif ( $parser->mOptions
->getInterfaceMessage() ) {
213 $gender = $wgUser->getOption( 'gender' );
215 return $parser->getFunctionLang()->gender( $gender, $forms );
217 static function plural( $parser, $text = '') {
218 $forms = array_slice( func_get_args(), 2);
219 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
220 return $parser->getFunctionLang()->convertPlural( $text, $forms );
224 * Override the title of the page when viewed, provided we've been given a
225 * title which will normalise to the canonical title
227 * @param Parser $parser Parent parser
228 * @param string $text Desired title text
231 static function displaytitle( $parser, $text = '' ) {
232 global $wgRestrictDisplayTitle;
233 $text = trim( Sanitizer
::decodeCharReferences( $text ) );
235 if ( !$wgRestrictDisplayTitle ) {
236 $parser->mOutput
->setDisplayTitle( $text );
238 $title = Title
::newFromText( $text );
239 if( $title instanceof Title
&& $title->getFragment() == '' && $title->equals( $parser->mTitle
) )
240 $parser->mOutput
->setDisplayTitle( $text );
245 static function isRaw( $param ) {
248 $mwRaw =& MagicWord
::get( 'rawsuffix' );
250 if ( is_null( $param ) ) {
253 return $mwRaw->match( $param );
257 static function formatRaw( $num, $raw ) {
258 if( self
::isRaw( $raw ) ) {
262 return $wgContLang->formatNum( $num );
265 static function numberofpages( $parser, $raw = null ) {
266 return self
::formatRaw( SiteStats
::pages(), $raw );
268 static function numberofusers( $parser, $raw = null ) {
269 return self
::formatRaw( SiteStats
::users(), $raw );
271 static function numberofactiveusers( $parser, $raw = null ) {
272 return self
::formatRaw( SiteStats
::activeUsers(), $raw );
274 static function numberofarticles( $parser, $raw = null ) {
275 return self
::formatRaw( SiteStats
::articles(), $raw );
277 static function numberoffiles( $parser, $raw = null ) {
278 return self
::formatRaw( SiteStats
::images(), $raw );
280 static function numberofadmins( $parser, $raw = null ) {
281 return self
::formatRaw( SiteStats
::numberingroup('sysop'), $raw );
283 static function numberofedits( $parser, $raw = null ) {
284 return self
::formatRaw( SiteStats
::edits(), $raw );
286 static function numberofviews( $parser, $raw = null ) {
287 return self
::formatRaw( SiteStats
::views(), $raw );
289 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
290 return self
::formatRaw( SiteStats
::pagesInNs( intval( $namespace ) ), $raw );
292 static function numberingroup( $parser, $name = '', $raw = null) {
293 return self
::formatRaw( SiteStats
::numberingroup( strtolower( $name ) ), $raw );
298 * Given a title, return the namespace name that would be given by the
299 * corresponding magic word
300 * Note: function name changed to "mwnamespace" rather than "namespace"
301 * to not break PHP 5.3
303 static function mwnamespace( $parser, $title = null ) {
304 $t = Title
::newFromText( $title );
307 return str_replace( '_', ' ', $t->getNsText() );
309 static function namespacee( $parser, $title = null ) {
310 $t = Title
::newFromText( $title );
313 return wfUrlencode( $t->getNsText() );
315 static function talkspace( $parser, $title = null ) {
316 $t = Title
::newFromText( $title );
317 if ( is_null($t) ||
!$t->canTalk() )
319 return str_replace( '_', ' ', $t->getTalkNsText() );
321 static function talkspacee( $parser, $title = null ) {
322 $t = Title
::newFromText( $title );
323 if ( is_null($t) ||
!$t->canTalk() )
325 return wfUrlencode( $t->getTalkNsText() );
327 static function subjectspace( $parser, $title = null ) {
328 $t = Title
::newFromText( $title );
331 return str_replace( '_', ' ', $t->getSubjectNsText() );
333 static function subjectspacee( $parser, $title = null ) {
334 $t = Title
::newFromText( $title );
337 return wfUrlencode( $t->getSubjectNsText() );
340 * Functions to get and normalize pagenames, corresponding to the magic words
343 static function pagename( $parser, $title = null ) {
344 $t = Title
::newFromText( $title );
347 return wfEscapeWikiText( $t->getText() );
349 static function pagenamee( $parser, $title = null ) {
350 $t = Title
::newFromText( $title );
353 return $t->getPartialURL();
355 static function fullpagename( $parser, $title = null ) {
356 $t = Title
::newFromText( $title );
357 if ( is_null($t) ||
!$t->canTalk() )
359 return wfEscapeWikiText( $t->getPrefixedText() );
361 static function fullpagenamee( $parser, $title = null ) {
362 $t = Title
::newFromText( $title );
363 if ( is_null($t) ||
!$t->canTalk() )
365 return $t->getPrefixedURL();
367 static function subpagename( $parser, $title = null ) {
368 $t = Title
::newFromText( $title );
371 return $t->getSubpageText();
373 static function subpagenamee( $parser, $title = null ) {
374 $t = Title
::newFromText( $title );
377 return $t->getSubpageUrlForm();
379 static function basepagename( $parser, $title = null ) {
380 $t = Title
::newFromText( $title );
383 return $t->getBaseText();
385 static function basepagenamee( $parser, $title = null ) {
386 $t = Title
::newFromText( $title );
389 return wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) );
391 static function talkpagename( $parser, $title = null ) {
392 $t = Title
::newFromText( $title );
393 if ( is_null($t) ||
!$t->canTalk() )
395 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
397 static function talkpagenamee( $parser, $title = null ) {
398 $t = Title
::newFromText( $title );
399 if ( is_null($t) ||
!$t->canTalk() )
401 return $t->getTalkPage()->getPrefixedUrl();
403 static function subjectpagename( $parser, $title = null ) {
404 $t = Title
::newFromText( $title );
407 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
409 static function subjectpagenamee( $parser, $title = null ) {
410 $t = Title
::newFromText( $title );
413 return $t->getSubjectPage()->getPrefixedUrl();
417 * Return the number of pages in the given category, or 0 if it's nonexis-
418 * tent. This is an expensive parser function and can't be called too many
421 static function pagesincategory( $parser, $name = '', $raw = null ) {
422 static $cache = array();
423 $category = Category
::newFromName( $name );
425 if( !is_object( $category ) ) {
427 return self
::formatRaw( 0, $raw );
430 # Normalize name for cache
431 $name = $category->getName();
434 if( isset( $cache[$name] ) ) {
435 $count = $cache[$name];
436 } elseif( $parser->incrementExpensiveFunctionCount() ) {
437 $count = $cache[$name] = (int)$category->getPageCount();
439 return self
::formatRaw( $count, $raw );
443 * Return the size of the given page, or 0 if it's nonexistent. This is an
444 * expensive parser function and can't be called too many times per page.
446 * @FIXME This doesn't work correctly on preview for getting the size of
448 * @FIXME Title::getLength() documentation claims that it adds things to
449 * the link cache, so the local cache here should be unnecessary, but in
450 * fact calling getLength() repeatedly for the same $page does seem to
451 * run one query for each call?
453 static function pagesize( $parser, $page = '', $raw = null ) {
454 static $cache = array();
455 $title = Title
::newFromText($page);
457 if( !is_object( $title ) ) {
459 return self
::formatRaw( 0, $raw );
462 # Normalize name for cache
463 $page = $title->getPrefixedText();
466 if( isset( $cache[$page] ) ) {
467 $length = $cache[$page];
468 } elseif( $parser->incrementExpensiveFunctionCount() ) {
469 $rev = Revision
::newFromTitle($title);
470 $id = $rev ?
$rev->getPage() : 0;
471 $length = $cache[$page] = $rev ?
$rev->getSize() : 0;
473 // Register dependency in templatelinks
474 $parser->mOutput
->addTemplate( $title, $id, $rev ?
$rev->getId() : 0 );
476 return self
::formatRaw( $length, $raw );
480 * Returns the requested protection level for the current page
482 static function protectionlevel( $parser, $type = '' ) {
483 $restrictions = $parser->mTitle
->getRestrictions( strtolower( $type ) );
484 # Title::getRestrictions returns an array, its possible it may have
485 # multiple values in the future
486 return implode( $restrictions, ',' );
489 static function language( $parser, $arg = '' ) {
491 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
492 return $lang != '' ?
$lang : $arg;
496 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
498 static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT
) {
499 $lengthOfPadding = mb_strlen( $padding );
500 if ( $lengthOfPadding == 0 ) return $string;
502 # The remaining length to add counts down to 0 as padding is added
503 $length = min( $length, 500 ) - mb_strlen( $string );
504 # $finalPadding is just $padding repeated enough times so that
505 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
507 while ( $length > 0 ) {
508 # If $length < $lengthofPadding, truncate $padding so we get the
509 # exact length desired.
510 $finalPadding .= mb_substr( $padding, 0, $length );
511 $length -= $lengthOfPadding;
514 if ( $direction == STR_PAD_LEFT
) {
515 return $finalPadding . $string;
517 return $string . $finalPadding;
521 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
522 return self
::pad( $string, $length, $padding, STR_PAD_LEFT
);
525 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
526 return self
::pad( $string, $length, $padding );
529 static function anchorencode( $parser, $text ) {
530 $a = urlencode( $text );
531 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
532 # leave colons alone, however
533 $a = str_replace( '.3A', ':', $a );
537 static function special( $parser, $text ) {
538 $title = SpecialPage
::getTitleForAlias( $text );
540 return $title->getPrefixedText();
542 return wfMsgForContent( 'nosuchspecialpage' );
546 public static function defaultsort( $parser, $text ) {
547 $text = trim( $text );
548 if( strlen( $text ) == 0 )
550 $old = $parser->getCustomDefaultSort();
551 $parser->setDefaultSort( $text );
552 if( $old === false ||
$old == $text )
555 return( '<span class="error">' .
556 wfMsg( 'duplicate-defaultsort',
557 htmlspecialchars( $old ),
558 htmlspecialchars( $text ) ) .
562 public static function filepath( $parser, $name='', $option='' ) {
563 $file = wfFindFile( $name );
565 $url = $file->getFullUrl();
566 if( $option == 'nowiki' ) {
567 return array( $url, 'nowiki' => true );
576 * Parser function to extension tag adaptor
578 public static function tagObj( $parser, $frame, $args ) {
580 if ( !count( $args ) ) {
583 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
585 if ( count( $args ) ) {
586 $inner = $frame->expand( array_shift( $args ) );
591 $stripList = $parser->getStripList();
592 if ( !in_array( $tagName, $stripList ) ) {
593 return '<span class="error">' .
594 wfMsg( 'unknown_extension_tag', $tagName ) .
598 $attributes = array();
599 foreach ( $args as $arg ) {
600 $bits = $arg->splitArg();
601 if ( strval( $bits['index'] ) === '' ) {
602 $name = trim( $frame->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
603 $value = trim( $frame->expand( $bits['value'] ) );
604 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
605 $value = isset( $m[1] ) ?
$m[1] : '';
607 $attributes[$name] = $value;
614 'attributes' => $attributes,
615 'close' => "</$tagName>",
617 return $parser->extensionSubstitution( $params, $frame );