f7368f398825c6511150cf714309fb64725953f8
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' ) );
71 $parser->setFunctionHook( 'groupconvert', array( __CLASS__
, 'groupconvert' ), SFH_NO_HASH
);
73 if ( $wgAllowDisplayTitle ) {
74 $parser->setFunctionHook( 'displaytitle', array( __CLASS__
, 'displaytitle' ), SFH_NO_HASH
);
76 if ( $wgAllowSlowParserFunctions ) {
77 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__
, 'pagesinnamespace' ), SFH_NO_HASH
);
81 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
82 if ( strval( $part1 ) !== '' ) {
83 $args = array_slice( func_get_args(), 2 );
84 $message = wfMsgGetKey( $part1, true, false, false );
85 $message = wfMsgReplaceArgs( $message, $args );
86 $message = $parser->replaceVariables( $message ); // like $wgMessageCache->transform()
89 return array( 'found' => false );
93 static function formatDate( $parser, $date, $defaultPref = null ) {
94 $df = DateFormatter
::getInstance();
98 $pref = $parser->mOptions
->getDateFormat();
100 // Specify a different default date format other than the the normal default
101 // iff the user has 'default' for their setting
102 if ($pref == 'default' && $defaultPref)
103 $pref = $defaultPref;
105 $date = $df->reformat( $pref, $date, array('match-whole') );
109 static function ns( $parser, $part1 = '' ) {
111 if ( intval( $part1 ) ||
$part1 == "0" ) {
112 $index = intval( $part1 );
114 $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
116 if ( $index !== false ) {
117 return $wgContLang->getFormattedNsText( $index );
119 return array( 'found' => false );
123 static function urlencode( $parser, $s = '' ) {
124 return urlencode( $s );
127 static function lcfirst( $parser, $s = '' ) {
129 return $wgContLang->lcfirst( $s );
132 static function ucfirst( $parser, $s = '' ) {
134 return $wgContLang->ucfirst( $s );
137 static function lc( $parser, $s = '' ) {
139 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
140 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
142 return $wgContLang->lc( $s );
146 static function uc( $parser, $s = '' ) {
148 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
149 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
151 return $wgContLang->uc( $s );
155 static function localurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getLocalURL', $s, $arg ); }
156 static function localurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeLocalURL', $s, $arg ); }
157 static function fullurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getFullURL', $s, $arg ); }
158 static function fullurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeFullURL', $s, $arg ); }
160 static function urlFunction( $func, $s = '', $arg = null ) {
161 $title = Title
::newFromText( $s );
162 # Due to order of execution of a lot of bits, the values might be encoded
163 # before arriving here; if that's true, then the title can't be created
164 # and the variable will fail. If we can't get a decent title from the first
165 # attempt, url-decode and try for a second.
166 if( is_null( $title ) )
167 $title = Title
::newFromUrl( urldecode( $s ) );
168 if( !is_null( $title ) ) {
169 # Convert NS_MEDIA -> NS_FILE
170 if( $title->getNamespace() == NS_MEDIA
) {
171 $title = Title
::makeTitle( NS_FILE
, $title->getDBKey() );
173 if( !is_null( $arg ) ) {
174 $text = $title->$func( $arg );
176 $text = $title->$func();
180 return array( 'found' => false );
184 static function formatNum( $parser, $num = '', $raw = null) {
185 if ( self
::israw( $raw ) ) {
186 return $parser->getFunctionLang()->parseFormattedNumber( $num );
188 return $parser->getFunctionLang()->formatNum( $num );
192 static function grammar( $parser, $case = '', $word = '' ) {
193 return $parser->getFunctionLang()->convertGrammar( $word, $case );
196 static function gender( $parser, $user ) {
197 $forms = array_slice( func_get_args(), 2);
200 $gender = User
::getDefaultOption( 'gender' );
203 $title = Title
::newFromText( $user );
205 if (is_object( $title ) && $title->getNamespace() == NS_USER
)
206 $user = $title->getText();
208 // check parameter, or use $wgUser if in interface message
209 $user = User
::newFromName( $user );
211 $gender = $user->getOption( 'gender' );
212 } elseif ( $parser->mOptions
->getInterfaceMessage() ) {
214 $gender = $wgUser->getOption( 'gender' );
216 return $parser->getFunctionLang()->gender( $gender, $forms );
218 static function plural( $parser, $text = '') {
219 $forms = array_slice( func_get_args(), 2);
220 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
221 return $parser->getFunctionLang()->convertPlural( $text, $forms );
225 * Override the title of the page when viewed, provided we've been given a
226 * title which will normalise to the canonical title
228 * @param Parser $parser Parent parser
229 * @param string $text Desired title text
232 static function displaytitle( $parser, $text = '' ) {
233 global $wgRestrictDisplayTitle;
234 $text = trim( Sanitizer
::decodeCharReferences( $text ) );
236 if ( !$wgRestrictDisplayTitle ) {
237 $parser->mOutput
->setDisplayTitle( $text );
239 $title = Title
::newFromText( $text );
240 if( $title instanceof Title
&& $title->getFragment() == '' && $title->equals( $parser->mTitle
) )
241 $parser->mOutput
->setDisplayTitle( $text );
246 static function isRaw( $param ) {
249 $mwRaw =& MagicWord
::get( 'rawsuffix' );
251 if ( is_null( $param ) ) {
254 return $mwRaw->match( $param );
258 static function formatRaw( $num, $raw ) {
259 if( self
::isRaw( $raw ) ) {
263 return $wgContLang->formatNum( $num );
266 static function numberofpages( $parser, $raw = null ) {
267 return self
::formatRaw( SiteStats
::pages(), $raw );
269 static function numberofusers( $parser, $raw = null ) {
270 return self
::formatRaw( SiteStats
::users(), $raw );
272 static function numberofactiveusers( $parser, $raw = null ) {
273 return self
::formatRaw( SiteStats
::activeUsers(), $raw );
275 static function numberofarticles( $parser, $raw = null ) {
276 return self
::formatRaw( SiteStats
::articles(), $raw );
278 static function numberoffiles( $parser, $raw = null ) {
279 return self
::formatRaw( SiteStats
::images(), $raw );
281 static function numberofadmins( $parser, $raw = null ) {
282 return self
::formatRaw( SiteStats
::numberingroup('sysop'), $raw );
284 static function numberofedits( $parser, $raw = null ) {
285 return self
::formatRaw( SiteStats
::edits(), $raw );
287 static function numberofviews( $parser, $raw = null ) {
288 return self
::formatRaw( SiteStats
::views(), $raw );
290 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
291 return self
::formatRaw( SiteStats
::pagesInNs( intval( $namespace ) ), $raw );
293 static function numberingroup( $parser, $name = '', $raw = null) {
294 return self
::formatRaw( SiteStats
::numberingroup( strtolower( $name ) ), $raw );
299 * Given a title, return the namespace name that would be given by the
300 * corresponding magic word
301 * Note: function name changed to "mwnamespace" rather than "namespace"
302 * to not break PHP 5.3
304 static function mwnamespace( $parser, $title = null ) {
305 $t = Title
::newFromText( $title );
308 return str_replace( '_', ' ', $t->getNsText() );
310 static function namespacee( $parser, $title = null ) {
311 $t = Title
::newFromText( $title );
314 return wfUrlencode( $t->getNsText() );
316 static function talkspace( $parser, $title = null ) {
317 $t = Title
::newFromText( $title );
318 if ( is_null($t) ||
!$t->canTalk() )
320 return str_replace( '_', ' ', $t->getTalkNsText() );
322 static function talkspacee( $parser, $title = null ) {
323 $t = Title
::newFromText( $title );
324 if ( is_null($t) ||
!$t->canTalk() )
326 return wfUrlencode( $t->getTalkNsText() );
328 static function subjectspace( $parser, $title = null ) {
329 $t = Title
::newFromText( $title );
332 return str_replace( '_', ' ', $t->getSubjectNsText() );
334 static function subjectspacee( $parser, $title = null ) {
335 $t = Title
::newFromText( $title );
338 return wfUrlencode( $t->getSubjectNsText() );
341 * Functions to get and normalize pagenames, corresponding to the magic words
344 static function pagename( $parser, $title = null ) {
345 $t = Title
::newFromText( $title );
348 return wfEscapeWikiText( $t->getText() );
350 static function pagenamee( $parser, $title = null ) {
351 $t = Title
::newFromText( $title );
354 return $t->getPartialURL();
356 static function fullpagename( $parser, $title = null ) {
357 $t = Title
::newFromText( $title );
358 if ( is_null($t) ||
!$t->canTalk() )
360 return wfEscapeWikiText( $t->getPrefixedText() );
362 static function fullpagenamee( $parser, $title = null ) {
363 $t = Title
::newFromText( $title );
364 if ( is_null($t) ||
!$t->canTalk() )
366 return $t->getPrefixedURL();
368 static function subpagename( $parser, $title = null ) {
369 $t = Title
::newFromText( $title );
372 return $t->getSubpageText();
374 static function subpagenamee( $parser, $title = null ) {
375 $t = Title
::newFromText( $title );
378 return $t->getSubpageUrlForm();
380 static function basepagename( $parser, $title = null ) {
381 $t = Title
::newFromText( $title );
384 return $t->getBaseText();
386 static function basepagenamee( $parser, $title = null ) {
387 $t = Title
::newFromText( $title );
390 return wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) );
392 static function talkpagename( $parser, $title = null ) {
393 $t = Title
::newFromText( $title );
394 if ( is_null($t) ||
!$t->canTalk() )
396 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
398 static function talkpagenamee( $parser, $title = null ) {
399 $t = Title
::newFromText( $title );
400 if ( is_null($t) ||
!$t->canTalk() )
402 return $t->getTalkPage()->getPrefixedUrl();
404 static function subjectpagename( $parser, $title = null ) {
405 $t = Title
::newFromText( $title );
408 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
410 static function subjectpagenamee( $parser, $title = null ) {
411 $t = Title
::newFromText( $title );
414 return $t->getSubjectPage()->getPrefixedUrl();
418 * Return the number of pages in the given category, or 0 if it's nonexis-
419 * tent. This is an expensive parser function and can't be called too many
422 static function pagesincategory( $parser, $name = '', $raw = null ) {
423 static $cache = array();
424 $category = Category
::newFromName( $name );
426 if( !is_object( $category ) ) {
428 return self
::formatRaw( 0, $raw );
431 # Normalize name for cache
432 $name = $category->getName();
435 if( isset( $cache[$name] ) ) {
436 $count = $cache[$name];
437 } elseif( $parser->incrementExpensiveFunctionCount() ) {
438 $count = $cache[$name] = (int)$category->getPageCount();
440 return self
::formatRaw( $count, $raw );
444 * Return the size of the given page, or 0 if it's nonexistent. This is an
445 * expensive parser function and can't be called too many times per page.
447 * @FIXME This doesn't work correctly on preview for getting the size of
449 * @FIXME Title::getLength() documentation claims that it adds things to
450 * the link cache, so the local cache here should be unnecessary, but in
451 * fact calling getLength() repeatedly for the same $page does seem to
452 * run one query for each call?
454 static function pagesize( $parser, $page = '', $raw = null ) {
455 static $cache = array();
456 $title = Title
::newFromText($page);
458 if( !is_object( $title ) ) {
460 return self
::formatRaw( 0, $raw );
463 # Normalize name for cache
464 $page = $title->getPrefixedText();
467 if( isset( $cache[$page] ) ) {
468 $length = $cache[$page];
469 } elseif( $parser->incrementExpensiveFunctionCount() ) {
470 $rev = Revision
::newFromTitle($title);
471 $id = $rev ?
$rev->getPage() : 0;
472 $length = $cache[$page] = $rev ?
$rev->getSize() : 0;
474 // Register dependency in templatelinks
475 $parser->mOutput
->addTemplate( $title, $id, $rev ?
$rev->getId() : 0 );
477 return self
::formatRaw( $length, $raw );
481 * Returns the requested protection level for the current page
483 static function protectionlevel( $parser, $type = '' ) {
484 $restrictions = $parser->mTitle
->getRestrictions( strtolower( $type ) );
485 # Title::getRestrictions returns an array, its possible it may have
486 # multiple values in the future
487 return implode( $restrictions, ',' );
490 static function language( $parser, $arg = '' ) {
492 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
493 return $lang != '' ?
$lang : $arg;
497 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
499 static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT
) {
500 $lengthOfPadding = mb_strlen( $padding );
501 if ( $lengthOfPadding == 0 ) return $string;
503 # The remaining length to add counts down to 0 as padding is added
504 $length = min( $length, 500 ) - mb_strlen( $string );
505 # $finalPadding is just $padding repeated enough times so that
506 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
508 while ( $length > 0 ) {
509 # If $length < $lengthofPadding, truncate $padding so we get the
510 # exact length desired.
511 $finalPadding .= mb_substr( $padding, 0, $length );
512 $length -= $lengthOfPadding;
515 if ( $direction == STR_PAD_LEFT
) {
516 return $finalPadding . $string;
518 return $string . $finalPadding;
522 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
523 return self
::pad( $string, $length, $padding, STR_PAD_LEFT
);
526 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
527 return self
::pad( $string, $length, $padding );
530 static function anchorencode( $parser, $text ) {
531 $a = urlencode( $text );
532 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
533 # leave colons alone, however
534 $a = str_replace( '.3A', ':', $a );
538 static function special( $parser, $text ) {
539 $title = SpecialPage
::getTitleForAlias( $text );
541 return $title->getPrefixedText();
543 return wfMsgForContent( 'nosuchspecialpage' );
547 public static function defaultsort( $parser, $text ) {
548 $text = trim( $text );
549 if( strlen( $text ) == 0 )
551 $old = $parser->getCustomDefaultSort();
552 $parser->setDefaultSort( $text );
553 if( $old === false ||
$old == $text )
556 return( '<span class="error">' .
557 wfMsg( 'duplicate-defaultsort',
558 htmlspecialchars( $old ),
559 htmlspecialchars( $text ) ) .
563 public static function filepath( $parser, $name='', $option='' ) {
564 $file = wfFindFile( $name );
566 $url = $file->getFullUrl();
567 if( $option == 'nowiki' ) {
568 return array( $url, 'nowiki' => true );
577 * Parser function to extension tag adaptor
579 public static function tagObj( $parser, $frame, $args ) {
581 if ( !count( $args ) ) {
584 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
586 if ( count( $args ) ) {
587 $inner = $frame->expand( array_shift( $args ) );
592 $stripList = $parser->getStripList();
593 if ( !in_array( $tagName, $stripList ) ) {
594 return '<span class="error">' .
595 wfMsg( 'unknown_extension_tag', $tagName ) .
599 $attributes = array();
600 foreach ( $args as $arg ) {
601 $bits = $arg->splitArg();
602 if ( strval( $bits['index'] ) === '' ) {
603 $name = trim( $frame->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
604 $value = trim( $frame->expand( $bits['value'] ) );
605 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
606 $value = isset( $m[1] ) ?
$m[1] : '';
608 $attributes[$name] = $value;
615 'attributes' => $attributes,
616 'close' => "</$tagName>",
618 return $parser->extensionSubstitution( $params, $frame );
622 * magic word call for a group convert from LanguageConverter.
624 public static function groupconvert( $parser, $group ) {
626 return $wgContLang->groupConvert( $group );