3 * Parser functions provided by MediaWiki core
9 * Various core parser functions, registered in Parser::firstCallInit()
12 class CoreParserFunctions
{
14 * @param $parser Parser
17 static function register( $parser ) {
18 global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
20 # Syntax for arguments (see self::setFunctionHook):
21 # "name for lookup in localized magic words array",
23 # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}}
24 # instead of {{#int:...}})
26 $parser->setFunctionHook( 'int', array( __CLASS__
, 'intFunction' ), SFH_NO_HASH
);
27 $parser->setFunctionHook( 'ns', array( __CLASS__
, 'ns' ), SFH_NO_HASH
);
28 $parser->setFunctionHook( 'nse', array( __CLASS__
, 'nse' ), SFH_NO_HASH
);
29 $parser->setFunctionHook( 'urlencode', array( __CLASS__
, 'urlencode' ), SFH_NO_HASH
);
30 $parser->setFunctionHook( 'lcfirst', array( __CLASS__
, 'lcfirst' ), SFH_NO_HASH
);
31 $parser->setFunctionHook( 'ucfirst', array( __CLASS__
, 'ucfirst' ), SFH_NO_HASH
);
32 $parser->setFunctionHook( 'lc', array( __CLASS__
, 'lc' ), SFH_NO_HASH
);
33 $parser->setFunctionHook( 'uc', array( __CLASS__
, 'uc' ), SFH_NO_HASH
);
34 $parser->setFunctionHook( 'localurl', array( __CLASS__
, 'localurl' ), SFH_NO_HASH
);
35 $parser->setFunctionHook( 'localurle', array( __CLASS__
, 'localurle' ), SFH_NO_HASH
);
36 $parser->setFunctionHook( 'fullurl', array( __CLASS__
, 'fullurl' ), SFH_NO_HASH
);
37 $parser->setFunctionHook( 'fullurle', array( __CLASS__
, 'fullurle' ), SFH_NO_HASH
);
38 $parser->setFunctionHook( 'canonicalurl', array( __CLASS__
, 'canonicalurl' ), SFH_NO_HASH
);
39 $parser->setFunctionHook( 'canonicalurle', array( __CLASS__
, 'canonicalurle' ), SFH_NO_HASH
);
40 $parser->setFunctionHook( 'formatnum', array( __CLASS__
, 'formatnum' ), SFH_NO_HASH
);
41 $parser->setFunctionHook( 'grammar', array( __CLASS__
, 'grammar' ), SFH_NO_HASH
);
42 $parser->setFunctionHook( 'gender', array( __CLASS__
, 'gender' ), SFH_NO_HASH
);
43 $parser->setFunctionHook( 'plural', array( __CLASS__
, 'plural' ), SFH_NO_HASH
);
44 $parser->setFunctionHook( 'numberofpages', array( __CLASS__
, 'numberofpages' ), SFH_NO_HASH
);
45 $parser->setFunctionHook( 'numberofusers', array( __CLASS__
, 'numberofusers' ), SFH_NO_HASH
);
46 $parser->setFunctionHook( 'numberofactiveusers', array( __CLASS__
, 'numberofactiveusers' ), SFH_NO_HASH
);
47 $parser->setFunctionHook( 'numberofarticles', array( __CLASS__
, 'numberofarticles' ), SFH_NO_HASH
);
48 $parser->setFunctionHook( 'numberoffiles', array( __CLASS__
, 'numberoffiles' ), SFH_NO_HASH
);
49 $parser->setFunctionHook( 'numberofadmins', array( __CLASS__
, 'numberofadmins' ), SFH_NO_HASH
);
50 $parser->setFunctionHook( 'numberingroup', array( __CLASS__
, 'numberingroup' ), SFH_NO_HASH
);
51 $parser->setFunctionHook( 'numberofedits', array( __CLASS__
, 'numberofedits' ), SFH_NO_HASH
);
52 $parser->setFunctionHook( 'numberofviews', array( __CLASS__
, 'numberofviews' ), SFH_NO_HASH
);
53 $parser->setFunctionHook( 'language', array( __CLASS__
, 'language' ), SFH_NO_HASH
);
54 $parser->setFunctionHook( 'padleft', array( __CLASS__
, 'padleft' ), SFH_NO_HASH
);
55 $parser->setFunctionHook( 'padright', array( __CLASS__
, 'padright' ), SFH_NO_HASH
);
56 $parser->setFunctionHook( 'anchorencode', array( __CLASS__
, 'anchorencode' ), SFH_NO_HASH
);
57 $parser->setFunctionHook( 'special', array( __CLASS__
, 'special' ) );
58 $parser->setFunctionHook( 'defaultsort', array( __CLASS__
, 'defaultsort' ), SFH_NO_HASH
);
59 $parser->setFunctionHook( 'filepath', array( __CLASS__
, 'filepath' ), SFH_NO_HASH
);
60 $parser->setFunctionHook( 'pagesincategory', array( __CLASS__
, 'pagesincategory' ), SFH_NO_HASH
);
61 $parser->setFunctionHook( 'pagesize', array( __CLASS__
, 'pagesize' ), SFH_NO_HASH
);
62 $parser->setFunctionHook( 'protectionlevel', array( __CLASS__
, 'protectionlevel' ), SFH_NO_HASH
);
63 $parser->setFunctionHook( 'namespace', array( __CLASS__
, 'mwnamespace' ), SFH_NO_HASH
);
64 $parser->setFunctionHook( 'namespacee', array( __CLASS__
, 'namespacee' ), SFH_NO_HASH
);
65 $parser->setFunctionHook( 'namespacenumber', array( __CLASS__
, 'namespacenumber' ), SFH_NO_HASH
);
66 $parser->setFunctionHook( 'talkspace', array( __CLASS__
, 'talkspace' ), SFH_NO_HASH
);
67 $parser->setFunctionHook( 'talkspacee', array( __CLASS__
, 'talkspacee' ), SFH_NO_HASH
);
68 $parser->setFunctionHook( 'subjectspace', array( __CLASS__
, 'subjectspace' ), SFH_NO_HASH
);
69 $parser->setFunctionHook( 'subjectspacee', array( __CLASS__
, 'subjectspacee' ), SFH_NO_HASH
);
70 $parser->setFunctionHook( 'pagename', array( __CLASS__
, 'pagename' ), SFH_NO_HASH
);
71 $parser->setFunctionHook( 'pagenamee', array( __CLASS__
, 'pagenamee' ), SFH_NO_HASH
);
72 $parser->setFunctionHook( 'fullpagename', array( __CLASS__
, 'fullpagename' ), SFH_NO_HASH
);
73 $parser->setFunctionHook( 'fullpagenamee', array( __CLASS__
, 'fullpagenamee' ), SFH_NO_HASH
);
74 $parser->setFunctionHook( 'basepagename', array( __CLASS__
, 'basepagename' ), SFH_NO_HASH
);
75 $parser->setFunctionHook( 'basepagenamee', array( __CLASS__
, 'basepagenamee' ), SFH_NO_HASH
);
76 $parser->setFunctionHook( 'subpagename', array( __CLASS__
, 'subpagename' ), SFH_NO_HASH
);
77 $parser->setFunctionHook( 'subpagenamee', array( __CLASS__
, 'subpagenamee' ), SFH_NO_HASH
);
78 $parser->setFunctionHook( 'talkpagename', array( __CLASS__
, 'talkpagename' ), SFH_NO_HASH
);
79 $parser->setFunctionHook( 'talkpagenamee', array( __CLASS__
, 'talkpagenamee' ), SFH_NO_HASH
);
80 $parser->setFunctionHook( 'subjectpagename', array( __CLASS__
, 'subjectpagename' ), SFH_NO_HASH
);
81 $parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__
, 'subjectpagenamee' ), SFH_NO_HASH
);
82 $parser->setFunctionHook( 'tag', array( __CLASS__
, 'tagObj' ), SFH_OBJECT_ARGS
);
83 $parser->setFunctionHook( 'formatdate', array( __CLASS__
, 'formatDate' ) );
85 if ( $wgAllowDisplayTitle ) {
86 $parser->setFunctionHook( 'displaytitle', array( __CLASS__
, 'displaytitle' ), SFH_NO_HASH
);
88 if ( $wgAllowSlowParserFunctions ) {
89 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__
, 'pagesinnamespace' ), SFH_NO_HASH
);
94 * @param $parser Parser
95 * @param string $part1
98 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
99 if ( strval( $part1 ) !== '' ) {
100 $args = array_slice( func_get_args(), 2 );
101 $message = wfMessage( $part1, $args )->inLanguage( $parser->getOptions()->getUserLangObj() )->plain();
102 return array( $message, 'noparse' => false );
104 return array( 'found' => false );
109 * @param $parser Parser
111 * @param null $defaultPref
112 * @return mixed|string
114 static function formatDate( $parser, $date, $defaultPref = null ) {
115 $df = DateFormatter
::getInstance();
117 $date = trim( $date );
119 $pref = $parser->getOptions()->getDateFormat();
121 // Specify a different default date format other than the the normal default
122 // iff the user has 'default' for their setting
123 if ( $pref == 'default' && $defaultPref )
124 $pref = $defaultPref;
126 $date = $df->reformat( $pref, $date, array( 'match-whole' ) );
130 static function ns( $parser, $part1 = '' ) {
132 if ( intval( $part1 ) ||
$part1 == "0" ) {
133 $index = intval( $part1 );
135 $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
137 if ( $index !== false ) {
138 return $wgContLang->getFormattedNsText( $index );
140 return array( 'found' => false );
144 static function nse( $parser, $part1 = '' ) {
145 $ret = self
::ns( $parser, $part1 );
146 if ( is_string( $ret ) ) {
147 $ret = wfUrlencode( str_replace( ' ', '_', $ret ) );
153 * urlencodes a string according to one of three patterns: (bug 22474)
155 * By default (for HTTP "query" strings), spaces are encoded as '+'.
156 * Or to encode a value for the HTTP "path", spaces are encoded as '%20'.
157 * For links to "wiki"s, or similar software, spaces are encoded as '_',
159 * @param $parser Parser object
160 * @param $s String: The text to encode.
161 * @param $arg String (optional): The type of encoding.
164 static function urlencode( $parser, $s = '', $arg = null ) {
165 static $magicWords = null;
166 if ( is_null( $magicWords ) ) {
167 $magicWords = new MagicWordArray( array( 'url_path', 'url_query', 'url_wiki' ) );
169 switch( $magicWords->matchStartToEnd( $arg ) ) {
171 // Encode as though it's a wiki page, '_' for ' '.
173 $func = 'wfUrlencode';
174 $s = str_replace( ' ', '_', $s );
177 // Encode for an HTTP Path, '%20' for ' '.
179 $func = 'rawurlencode';
182 // Encode for HTTP query, '+' for ' '.
187 return $parser->markerSkipCallback( $s, $func );
190 static function lcfirst( $parser, $s = '' ) {
192 return $wgContLang->lcfirst( $s );
195 static function ucfirst( $parser, $s = '' ) {
197 return $wgContLang->ucfirst( $s );
201 * @param $parser Parser
205 static function lc( $parser, $s = '' ) {
207 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
211 * @param $parser Parser
215 static function uc( $parser, $s = '' ) {
217 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
220 static function localurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getLocalURL', $s, $arg ); }
221 static function localurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeLocalURL', $s, $arg ); }
222 static function fullurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getFullURL', $s, $arg ); }
223 static function fullurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeFullURL', $s, $arg ); }
224 static function canonicalurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getCanonicalURL', $s, $arg ); }
225 static function canonicalurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeCanonicalURL', $s, $arg ); }
227 static function urlFunction( $func, $s = '', $arg = null ) {
228 $title = Title
::newFromText( $s );
229 # Due to order of execution of a lot of bits, the values might be encoded
230 # before arriving here; if that's true, then the title can't be created
231 # and the variable will fail. If we can't get a decent title from the first
232 # attempt, url-decode and try for a second.
233 if( is_null( $title ) )
234 $title = Title
::newFromURL( urldecode( $s ) );
235 if( !is_null( $title ) ) {
236 # Convert NS_MEDIA -> NS_FILE
237 if( $title->getNamespace() == NS_MEDIA
) {
238 $title = Title
::makeTitle( NS_FILE
, $title->getDBkey() );
240 if( !is_null( $arg ) ) {
241 $text = $title->$func( $arg );
243 $text = $title->$func();
247 return array( 'found' => false );
252 * @param $parser Parser
257 static function formatnum( $parser, $num = '', $raw = null) {
258 if ( self
::isRaw( $raw ) ) {
259 $func = array( $parser->getFunctionLang(), 'parseFormattedNumber' );
261 $func = array( $parser->getFunctionLang(), 'formatNum' );
263 return $parser->markerSkipCallback( $num, $func );
267 * @param $parser Parser
268 * @param string $case
269 * @param string $word
272 static function grammar( $parser, $case = '', $word = '' ) {
273 $word = $parser->killMarkers( $word );
274 return $parser->getFunctionLang()->convertGrammar( $word, $case );
278 * @param $parser Parser
279 * @param $username string
282 static function gender( $parser, $username ) {
283 wfProfileIn( __METHOD__
);
284 $forms = array_slice( func_get_args(), 2 );
286 // Some shortcuts to avoid loading user data unnecessarily
287 if ( count( $forms ) === 0 ) {
288 wfProfileOut( __METHOD__
);
290 } elseif ( count( $forms ) === 1 ) {
291 wfProfileOut( __METHOD__
);
295 $username = trim( $username );
298 $gender = User
::getDefaultOption( 'gender' );
301 $title = Title
::newFromText( $username );
303 if ( $title && $title->getNamespace() == NS_USER
) {
304 $username = $title->getText();
307 // check parameter, or use the ParserOptions if in interface message
308 $user = User
::newFromName( $username );
310 $gender = $user->getOption( 'gender' );
311 } elseif ( $username === '' && $parser->getOptions()->getInterfaceMessage() ) {
312 $gender = $parser->getOptions()->getUser()->getOption( 'gender' );
314 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
315 wfProfileOut( __METHOD__
);
320 * @param $parser Parser
321 * @param string $text
324 static function plural( $parser, $text = '' ) {
325 $forms = array_slice( func_get_args(), 2 );
326 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
327 return $parser->getFunctionLang()->convertPlural( $text, $forms );
331 * Override the title of the page when viewed, provided we've been given a
332 * title which will normalise to the canonical title
334 * @param $parser Parser: parent parser
335 * @param $text String: desired title text
338 static function displaytitle( $parser, $text = '' ) {
339 global $wgRestrictDisplayTitle;
341 #parse a limited subset of wiki markup (just the single quote items)
342 $text = $parser->doQuotes( $text );
344 #remove stripped text (e.g. the UNIQ-QINU stuff) that was generated by tag extensions/whatever
345 $text = preg_replace( '/' . preg_quote( $parser->uniqPrefix(), '/' ) . '.*?'
346 . preg_quote( Parser
::MARKER_SUFFIX
, '/' ) . '/', '', $text );
348 #list of disallowed tags for DISPLAYTITLE
349 #these will be escaped even though they are allowed in normal wiki text
350 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr',
351 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp', 'br' );
353 #only requested titles that normalize to the actual title are allowed through
354 #if $wgRestrictDisplayTitle is true (it is by default)
355 #mimic the escaping process that occurs in OutputPage::setPageTitle
356 $text = Sanitizer
::normalizeCharReferences( Sanitizer
::removeHTMLtags( $text, null, array(), array(), $bad ) );
357 $title = Title
::newFromText( Sanitizer
::stripAllTags( $text ) );
359 if( !$wgRestrictDisplayTitle ) {
360 $parser->mOutput
->setDisplayTitle( $text );
362 if ( $title instanceof Title
&& $title->getFragment() == '' && $title->equals( $parser->mTitle
) ) {
363 $parser->mOutput
->setDisplayTitle( $text );
370 static function isRaw( $param ) {
373 $mwRaw =& MagicWord
::get( 'rawsuffix' );
375 if ( is_null( $param ) ) {
378 return $mwRaw->match( $param );
382 static function formatRaw( $num, $raw ) {
383 if( self
::isRaw( $raw ) ) {
387 return $wgContLang->formatNum( $num );
390 static function numberofpages( $parser, $raw = null ) {
391 return self
::formatRaw( SiteStats
::pages(), $raw );
393 static function numberofusers( $parser, $raw = null ) {
394 return self
::formatRaw( SiteStats
::users(), $raw );
396 static function numberofactiveusers( $parser, $raw = null ) {
397 return self
::formatRaw( SiteStats
::activeUsers(), $raw );
399 static function numberofarticles( $parser, $raw = null ) {
400 return self
::formatRaw( SiteStats
::articles(), $raw );
402 static function numberoffiles( $parser, $raw = null ) {
403 return self
::formatRaw( SiteStats
::images(), $raw );
405 static function numberofadmins( $parser, $raw = null ) {
406 return self
::formatRaw( SiteStats
::numberingroup('sysop'), $raw );
408 static function numberofedits( $parser, $raw = null ) {
409 return self
::formatRaw( SiteStats
::edits(), $raw );
411 static function numberofviews( $parser, $raw = null ) {
412 return self
::formatRaw( SiteStats
::views(), $raw );
414 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
415 return self
::formatRaw( SiteStats
::pagesInNs( intval( $namespace ) ), $raw );
417 static function numberingroup( $parser, $name = '', $raw = null) {
418 return self
::formatRaw( SiteStats
::numberingroup( strtolower( $name ) ), $raw );
423 * Given a title, return the namespace name that would be given by the
424 * corresponding magic word
425 * Note: function name changed to "mwnamespace" rather than "namespace"
426 * to not break PHP 5.3
427 * @return mixed|string
429 static function mwnamespace( $parser, $title = null ) {
430 $t = Title
::newFromText( $title );
433 return str_replace( '_', ' ', $t->getNsText() );
435 static function namespacee( $parser, $title = null ) {
436 $t = Title
::newFromText( $title );
439 return wfUrlencode( $t->getNsText() );
441 static function namespacenumber( $parser, $title = null ) {
442 $t = Title
::newFromText( $title );
445 return $t->getNamespace();
447 static function talkspace( $parser, $title = null ) {
448 $t = Title
::newFromText( $title );
449 if ( is_null( $t ) ||
!$t->canTalk() )
451 return str_replace( '_', ' ', $t->getTalkNsText() );
453 static function talkspacee( $parser, $title = null ) {
454 $t = Title
::newFromText( $title );
455 if ( is_null( $t ) ||
!$t->canTalk() )
457 return wfUrlencode( $t->getTalkNsText() );
459 static function subjectspace( $parser, $title = null ) {
460 $t = Title
::newFromText( $title );
463 return str_replace( '_', ' ', $t->getSubjectNsText() );
465 static function subjectspacee( $parser, $title = null ) {
466 $t = Title
::newFromText( $title );
469 return wfUrlencode( $t->getSubjectNsText() );
473 * Functions to get and normalize pagenames, corresponding to the magic words
477 static function pagename( $parser, $title = null ) {
478 $t = Title
::newFromText( $title );
481 return wfEscapeWikiText( $t->getText() );
483 static function pagenamee( $parser, $title = null ) {
484 $t = Title
::newFromText( $title );
487 return wfEscapeWikiText( $t->getPartialURL() );
489 static function fullpagename( $parser, $title = null ) {
490 $t = Title
::newFromText( $title );
491 if ( is_null( $t ) ||
!$t->canTalk() )
493 return wfEscapeWikiText( $t->getPrefixedText() );
495 static function fullpagenamee( $parser, $title = null ) {
496 $t = Title
::newFromText( $title );
497 if ( is_null( $t ) ||
!$t->canTalk() )
499 return wfEscapeWikiText( $t->getPrefixedURL() );
501 static function subpagename( $parser, $title = null ) {
502 $t = Title
::newFromText( $title );
505 return wfEscapeWikiText( $t->getSubpageText() );
507 static function subpagenamee( $parser, $title = null ) {
508 $t = Title
::newFromText( $title );
511 return wfEscapeWikiText( $t->getSubpageUrlForm() );
513 static function basepagename( $parser, $title = null ) {
514 $t = Title
::newFromText( $title );
517 return wfEscapeWikiText( $t->getBaseText() );
519 static function basepagenamee( $parser, $title = null ) {
520 $t = Title
::newFromText( $title );
523 return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) ) );
525 static function talkpagename( $parser, $title = null ) {
526 $t = Title
::newFromText( $title );
527 if ( is_null( $t ) ||
!$t->canTalk() )
529 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
531 static function talkpagenamee( $parser, $title = null ) {
532 $t = Title
::newFromText( $title );
533 if ( is_null( $t ) ||
!$t->canTalk() )
535 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedUrl() );
537 static function subjectpagename( $parser, $title = null ) {
538 $t = Title
::newFromText( $title );
541 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
543 static function subjectpagenamee( $parser, $title = null ) {
544 $t = Title
::newFromText( $title );
547 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedUrl() );
551 * Return the number of pages in the given category, or 0 if it's nonexis-
552 * tent. This is an expensive parser function and can't be called too many
556 static function pagesincategory( $parser, $name = '', $raw = null ) {
557 static $cache = array();
558 $category = Category
::newFromName( $name );
560 if( !is_object( $category ) ) {
562 return self
::formatRaw( 0, $raw );
565 # Normalize name for cache
566 $name = $category->getName();
569 if( isset( $cache[$name] ) ) {
570 $count = $cache[$name];
571 } elseif( $parser->incrementExpensiveFunctionCount() ) {
572 $count = $cache[$name] = (int)$category->getPageCount();
574 return self
::formatRaw( $count, $raw );
578 * Return the size of the given page, or 0 if it's nonexistent. This is an
579 * expensive parser function and can't be called too many times per page.
581 * @todo FIXME: This doesn't work correctly on preview for getting the size
582 * of the current page.
583 * @todo FIXME: Title::getLength() documentation claims that it adds things
584 * to the link cache, so the local cache here should be unnecessary, but
585 * in fact calling getLength() repeatedly for the same $page does seem to
586 * run one query for each call?
587 * @todo Document parameters
589 * @param $parser Parser
590 * @param $page String TODO DOCUMENT (Default: empty string)
591 * @param $raw TODO DOCUMENT (Default: null)
594 static function pagesize( $parser, $page = '', $raw = null ) {
595 static $cache = array();
596 $title = Title
::newFromText( $page );
598 if( !is_object( $title ) ) {
600 return self
::formatRaw( 0, $raw );
603 # Normalize name for cache
604 $page = $title->getPrefixedText();
607 if( isset( $cache[$page] ) ) {
608 $length = $cache[$page];
609 } elseif( $parser->incrementExpensiveFunctionCount() ) {
610 $rev = Revision
::newFromTitle( $title );
611 $id = $rev ?
$rev->getPage() : 0;
612 $length = $cache[$page] = $rev ?
$rev->getSize() : 0;
614 // Register dependency in templatelinks
615 $parser->mOutput
->addTemplate( $title, $id, $rev ?
$rev->getId() : 0 );
617 return self
::formatRaw( $length, $raw );
621 * Returns the requested protection level for the current page
624 static function protectionlevel( $parser, $type = '' ) {
625 $restrictions = $parser->mTitle
->getRestrictions( strtolower( $type ) );
626 # Title::getRestrictions returns an array, its possible it may have
627 # multiple values in the future
628 return implode( $restrictions, ',' );
632 * Gives language names.
633 * @param $parser Parser
634 * @param $code String Language code (of which to get name)
635 * @param $inLanguage String Language code (in which to get name)
638 static function language( $parser, $code = '', $inLanguage = '' ) {
639 $code = strtolower( $code );
640 $inLanguage = strtolower( $inLanguage );
641 $lang = Language
::fetchLanguageName( $code, $inLanguage );
642 return $lang !== '' ?
$lang : wfBCP47( $code );
646 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
649 static function pad( $parser, $string, $length, $padding = '0', $direction = STR_PAD_RIGHT
) {
650 $padding = $parser->killMarkers( $padding );
651 $lengthOfPadding = mb_strlen( $padding );
652 if ( $lengthOfPadding == 0 ) return $string;
654 # The remaining length to add counts down to 0 as padding is added
655 $length = min( $length, 500 ) - mb_strlen( $string );
656 # $finalPadding is just $padding repeated enough times so that
657 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
659 while ( $length > 0 ) {
660 # If $length < $lengthofPadding, truncate $padding so we get the
661 # exact length desired.
662 $finalPadding .= mb_substr( $padding, 0, $length );
663 $length -= $lengthOfPadding;
666 if ( $direction == STR_PAD_LEFT
) {
667 return $finalPadding . $string;
669 return $string . $finalPadding;
673 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
674 return self
::pad( $parser, $string, $length, $padding, STR_PAD_LEFT
);
677 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
678 return self
::pad( $parser, $string, $length, $padding );
682 * @param $parser Parser
686 static function anchorencode( $parser, $text ) {
687 $text = $parser->killMarkers( $text );
688 return substr( $parser->guessSectionNameFromWikiText( $text ), 1);
691 static function special( $parser, $text ) {
692 list( $page, $subpage ) = SpecialPageFactory
::resolveAlias( $text );
694 $title = SpecialPage
::getTitleFor( $page, $subpage );
697 return wfMsgForContent( 'nosuchspecialpage' );
702 * @param $parser Parser
703 * @param $text String The sortkey to use
704 * @param $uarg String Either "noreplace" or "noerror" (in en)
705 * both suppress errors, and noreplace does nothing if
706 * a default sortkey already exists.
709 public static function defaultsort( $parser, $text, $uarg = '' ) {
710 static $magicWords = null;
711 if ( is_null( $magicWords ) ) {
712 $magicWords = new MagicWordArray( array( 'defaultsort_noerror', 'defaultsort_noreplace' ) );
714 $arg = $magicWords->matchStartToEnd( $uarg );
716 $text = trim( $text );
717 if( strlen( $text ) == 0 )
719 $old = $parser->getCustomDefaultSort();
720 if ( $old === false ||
$arg !== 'defaultsort_noreplace' ) {
721 $parser->setDefaultSort( $text );
724 if( $old === false ||
$old == $text ||
$arg ) {
727 return( '<span class="error">' .
728 wfMsgForContent( 'duplicate-defaultsort',
729 htmlspecialchars( $old ),
730 htmlspecialchars( $text ) ) .
735 // Usage {{filepath|300}}, {{filepath|nowiki}}, {{filepath|nowiki|300}} or {{filepath|300|nowiki}}
736 public static function filepath( $parser, $name='', $argA='', $argB='' ) {
737 $file = wfFindFile( $name );
739 $argA_int = intval( $argA );
740 $argB_int = intval( $argB );
742 if ( $argB_int > 0 ) {
743 // {{filepath: | option | size }}
747 } elseif ( $argA_int > 0 ) {
748 // {{filepath: | size [|option] }}
753 // {{filepath: [|option] }}
758 $url = $file->getFullUrl();
760 // If a size is requested...
761 if ( is_integer( $size ) ) {
762 $mto = $file->transform( array( 'width' => $size ) );
764 if ( $mto && !$mto->isError() ) {
765 // ... change the URL to point to a thumbnail.
766 $url = wfExpandUrl( $mto->getUrl(), PROTO_RELATIVE
);
769 if ( $option == 'nowiki' ) {
770 return array( $url, 'nowiki' => true );
779 * Parser function to extension tag adaptor
782 public static function tagObj( $parser, $frame, $args ) {
783 if ( !count( $args ) ) {
786 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
788 if ( count( $args ) ) {
789 $inner = $frame->expand( array_shift( $args ) );
794 $stripList = $parser->getStripList();
795 if ( !in_array( $tagName, $stripList ) ) {
796 return '<span class="error">' .
797 wfMsgForContent( 'unknown_extension_tag', $tagName ) .
801 $attributes = array();
802 foreach ( $args as $arg ) {
803 $bits = $arg->splitArg();
804 if ( strval( $bits['index'] ) === '' ) {
805 $name = trim( $frame->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
806 $value = trim( $frame->expand( $bits['value'] ) );
807 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
808 $value = isset( $m[1] ) ?
$m[1] : '';
810 $attributes[$name] = $value;
817 'attributes' => $attributes,
818 'close' => "</$tagName>",
820 return $parser->extensionSubstitution( $params, $frame );