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( 'formatnum', array( __CLASS__
, 'formatnum' ), SFH_NO_HASH
);
39 $parser->setFunctionHook( 'grammar', array( __CLASS__
, 'grammar' ), SFH_NO_HASH
);
40 $parser->setFunctionHook( 'gender', array( __CLASS__
, 'gender' ), SFH_NO_HASH
);
41 $parser->setFunctionHook( 'plural', array( __CLASS__
, 'plural' ), SFH_NO_HASH
);
42 $parser->setFunctionHook( 'numberofpages', array( __CLASS__
, 'numberofpages' ), SFH_NO_HASH
);
43 $parser->setFunctionHook( 'numberofusers', array( __CLASS__
, 'numberofusers' ), SFH_NO_HASH
);
44 $parser->setFunctionHook( 'numberofactiveusers', array( __CLASS__
, 'numberofactiveusers' ), SFH_NO_HASH
);
45 $parser->setFunctionHook( 'numberofarticles', array( __CLASS__
, 'numberofarticles' ), SFH_NO_HASH
);
46 $parser->setFunctionHook( 'numberoffiles', array( __CLASS__
, 'numberoffiles' ), SFH_NO_HASH
);
47 $parser->setFunctionHook( 'numberofadmins', array( __CLASS__
, 'numberofadmins' ), SFH_NO_HASH
);
48 $parser->setFunctionHook( 'numberingroup', array( __CLASS__
, 'numberingroup' ), SFH_NO_HASH
);
49 $parser->setFunctionHook( 'numberofedits', array( __CLASS__
, 'numberofedits' ), SFH_NO_HASH
);
50 $parser->setFunctionHook( 'numberofviews', array( __CLASS__
, 'numberofviews' ), SFH_NO_HASH
);
51 $parser->setFunctionHook( 'language', array( __CLASS__
, 'language' ), SFH_NO_HASH
);
52 $parser->setFunctionHook( 'padleft', array( __CLASS__
, 'padleft' ), SFH_NO_HASH
);
53 $parser->setFunctionHook( 'padright', array( __CLASS__
, 'padright' ), SFH_NO_HASH
);
54 $parser->setFunctionHook( 'anchorencode', array( __CLASS__
, 'anchorencode' ), SFH_NO_HASH
);
55 $parser->setFunctionHook( 'special', array( __CLASS__
, 'special' ) );
56 $parser->setFunctionHook( 'defaultsort', array( __CLASS__
, 'defaultsort' ), SFH_NO_HASH
);
57 $parser->setFunctionHook( 'filepath', array( __CLASS__
, 'filepath' ), SFH_NO_HASH
);
58 $parser->setFunctionHook( 'pagesincategory', array( __CLASS__
, 'pagesincategory' ), SFH_NO_HASH
);
59 $parser->setFunctionHook( 'pagesize', array( __CLASS__
, 'pagesize' ), SFH_NO_HASH
);
60 $parser->setFunctionHook( 'protectionlevel', array( __CLASS__
, 'protectionlevel' ), SFH_NO_HASH
);
61 $parser->setFunctionHook( 'namespace', array( __CLASS__
, 'mwnamespace' ), SFH_NO_HASH
);
62 $parser->setFunctionHook( 'namespacee', array( __CLASS__
, 'namespacee' ), SFH_NO_HASH
);
63 $parser->setFunctionHook( 'talkspace', array( __CLASS__
, 'talkspace' ), SFH_NO_HASH
);
64 $parser->setFunctionHook( 'talkspacee', array( __CLASS__
, 'talkspacee' ), SFH_NO_HASH
);
65 $parser->setFunctionHook( 'subjectspace', array( __CLASS__
, 'subjectspace' ), SFH_NO_HASH
);
66 $parser->setFunctionHook( 'subjectspacee', array( __CLASS__
, 'subjectspacee' ), SFH_NO_HASH
);
67 $parser->setFunctionHook( 'pagename', array( __CLASS__
, 'pagename' ), SFH_NO_HASH
);
68 $parser->setFunctionHook( 'pagenamee', array( __CLASS__
, 'pagenamee' ), SFH_NO_HASH
);
69 $parser->setFunctionHook( 'fullpagename', array( __CLASS__
, 'fullpagename' ), SFH_NO_HASH
);
70 $parser->setFunctionHook( 'fullpagenamee', array( __CLASS__
, 'fullpagenamee' ), SFH_NO_HASH
);
71 $parser->setFunctionHook( 'basepagename', array( __CLASS__
, 'basepagename' ), SFH_NO_HASH
);
72 $parser->setFunctionHook( 'basepagenamee', array( __CLASS__
, 'basepagenamee' ), SFH_NO_HASH
);
73 $parser->setFunctionHook( 'subpagename', array( __CLASS__
, 'subpagename' ), SFH_NO_HASH
);
74 $parser->setFunctionHook( 'subpagenamee', array( __CLASS__
, 'subpagenamee' ), SFH_NO_HASH
);
75 $parser->setFunctionHook( 'talkpagename', array( __CLASS__
, 'talkpagename' ), SFH_NO_HASH
);
76 $parser->setFunctionHook( 'talkpagenamee', array( __CLASS__
, 'talkpagenamee' ), SFH_NO_HASH
);
77 $parser->setFunctionHook( 'subjectpagename', array( __CLASS__
, 'subjectpagename' ), SFH_NO_HASH
);
78 $parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__
, 'subjectpagenamee' ), SFH_NO_HASH
);
79 $parser->setFunctionHook( 'tag', array( __CLASS__
, 'tagObj' ), SFH_OBJECT_ARGS
);
80 $parser->setFunctionHook( 'formatdate', array( __CLASS__
, 'formatDate' ) );
82 if ( $wgAllowDisplayTitle ) {
83 $parser->setFunctionHook( 'displaytitle', array( __CLASS__
, 'displaytitle' ), SFH_NO_HASH
);
85 if ( $wgAllowSlowParserFunctions ) {
86 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__
, 'pagesinnamespace' ), SFH_NO_HASH
);
91 * @param $parser Parser
92 * @param string $part1
95 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
96 if ( strval( $part1 ) !== '' ) {
97 $args = array_slice( func_get_args(), 2 );
98 $message = wfMessage( $part1, $args )->inLanguage( $parser->getOptions()->getUserLang() )->plain();
99 $message = $parser->replaceVariables( $message ); // like MessageCache::transform()
102 return array( 'found' => false );
107 * @param $parser Parser
109 * @param null $defaultPref
110 * @return mixed|string
112 static function formatDate( $parser, $date, $defaultPref = null ) {
113 $df = DateFormatter
::getInstance();
115 $date = trim( $date );
117 $pref = $parser->getOptions()->getDateFormat();
119 // Specify a different default date format other than the the normal default
120 // iff the user has 'default' for their setting
121 if ( $pref == 'default' && $defaultPref )
122 $pref = $defaultPref;
124 $date = $df->reformat( $pref, $date, array( 'match-whole' ) );
128 static function ns( $parser, $part1 = '' ) {
130 if ( intval( $part1 ) ||
$part1 == "0" ) {
131 $index = intval( $part1 );
133 $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
135 if ( $index !== false ) {
136 return $wgContLang->getFormattedNsText( $index );
138 return array( 'found' => false );
142 static function nse( $parser, $part1 = '' ) {
143 return wfUrlencode( str_replace( ' ', '_', self
::ns( $parser, $part1 ) ) );
147 * urlencodes a string according to one of three patterns: (bug 22474)
149 * By default (for HTTP "query" strings), spaces are encoded as '+'.
150 * Or to encode a value for the HTTP "path", spaces are encoded as '%20'.
151 * For links to "wiki"s, or similar software, spaces are encoded as '_',
153 * @param $parser Parser object
154 * @param $s String: The text to encode.
155 * @param $arg String (optional): The type of encoding.
157 static function urlencode( $parser, $s = '', $arg = null ) {
158 static $magicWords = null;
159 if ( is_null( $magicWords ) ) {
160 $magicWords = new MagicWordArray( array( 'url_path', 'url_query', 'url_wiki' ) );
162 switch( $magicWords->matchStartToEnd( $arg ) ) {
164 // Encode as though it's a wiki page, '_' for ' '.
166 return wfUrlencode( str_replace( ' ', '_', $s ) );
168 // Encode for an HTTP Path, '%20' for ' '.
170 return rawurlencode( $s );
172 // Encode for HTTP query, '+' for ' '.
175 return urlencode( $s );
179 static function lcfirst( $parser, $s = '' ) {
181 return $wgContLang->lcfirst( $s );
184 static function ucfirst( $parser, $s = '' ) {
186 return $wgContLang->ucfirst( $s );
190 * @param $parser Parser
194 static function lc( $parser, $s = '' ) {
196 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
197 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
199 return $wgContLang->lc( $s );
204 * @param $parser Parser
208 static function uc( $parser, $s = '' ) {
210 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
211 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
213 return $wgContLang->uc( $s );
217 static function localurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getLocalURL', $s, $arg ); }
218 static function localurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeLocalURL', $s, $arg ); }
219 static function fullurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getFullURL', $s, $arg ); }
220 static function fullurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeFullURL', $s, $arg ); }
222 static function urlFunction( $func, $s = '', $arg = null ) {
223 $title = Title
::newFromText( $s );
224 # Due to order of execution of a lot of bits, the values might be encoded
225 # before arriving here; if that's true, then the title can't be created
226 # and the variable will fail. If we can't get a decent title from the first
227 # attempt, url-decode and try for a second.
228 if( is_null( $title ) )
229 $title = Title
::newFromURL( urldecode( $s ) );
230 if( !is_null( $title ) ) {
231 # Convert NS_MEDIA -> NS_FILE
232 if( $title->getNamespace() == NS_MEDIA
) {
233 $title = Title
::makeTitle( NS_FILE
, $title->getDBkey() );
235 if( !is_null( $arg ) ) {
236 $text = $title->$func( $arg );
238 $text = $title->$func();
242 return array( 'found' => false );
247 * @param $parser Parser
252 static function formatNum( $parser, $num = '', $raw = null) {
253 if ( self
::israw( $raw ) ) {
254 return $parser->getFunctionLang()->parseFormattedNumber( $num );
256 return $parser->getFunctionLang()->formatNum( $num );
261 * @param $parser Parser
262 * @param string $case
263 * @param string $word
266 static function grammar( $parser, $case = '', $word = '' ) {
267 return $parser->getFunctionLang()->convertGrammar( $word, $case );
271 * @param $parser Parser
272 * @param $username string
275 static function gender( $parser, $username ) {
276 wfProfileIn( __METHOD__
);
277 $forms = array_slice( func_get_args(), 2);
279 $username = trim( $username );
282 $gender = User
::getDefaultOption( 'gender' );
285 $title = Title
::newFromText( $username );
287 if ( $title && $title->getNamespace() == NS_USER
) {
288 $username = $title->getText();
291 // check parameter, or use the ParserOptions if in interface message
292 $user = User
::newFromName( $username );
294 $gender = $user->getOption( 'gender' );
295 } elseif ( $username === '' && $parser->getOptions()->getInterfaceMessage() ) {
296 $gender = $parser->getOptions()->getUser()->getOption( 'gender' );
298 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
299 wfProfileOut( __METHOD__
);
304 * @param $parser Parser
305 * @param string $text
308 static function plural( $parser, $text = '' ) {
309 $forms = array_slice( func_get_args(), 2 );
310 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
311 return $parser->getFunctionLang()->convertPlural( $text, $forms );
315 * Override the title of the page when viewed, provided we've been given a
316 * title which will normalise to the canonical title
318 * @param $parser Parser: parent parser
319 * @param $text String: desired title text
322 static function displaytitle( $parser, $text = '' ) {
323 global $wgRestrictDisplayTitle;
325 #parse a limited subset of wiki markup (just the single quote items)
326 $text = $parser->doQuotes( $text );
328 #remove stripped text (e.g. the UNIQ-QINU stuff) that was generated by tag extensions/whatever
329 $text = preg_replace( '/' . preg_quote( $parser->uniqPrefix(), '/' ) . '.*?'
330 . preg_quote( Parser
::MARKER_SUFFIX
, '/' ) . '/', '', $text );
332 #list of disallowed tags for DISPLAYTITLE
333 #these will be escaped even though they are allowed in normal wiki text
334 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr',
335 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp', 'br' );
337 #only requested titles that normalize to the actual title are allowed through
338 #if $wgRestrictDisplayTitle is true (it is by default)
339 #mimic the escaping process that occurs in OutputPage::setPageTitle
340 $text = Sanitizer
::normalizeCharReferences( Sanitizer
::removeHTMLtags( $text, null, array(), array(), $bad ) );
341 $title = Title
::newFromText( Sanitizer
::stripAllTags( $text ) );
343 if( !$wgRestrictDisplayTitle ) {
344 $parser->mOutput
->setDisplayTitle( $text );
346 if ( $title instanceof Title
&& $title->getFragment() == '' && $title->equals( $parser->mTitle
) ) {
347 $parser->mOutput
->setDisplayTitle( $text );
354 static function isRaw( $param ) {
357 $mwRaw =& MagicWord
::get( 'rawsuffix' );
359 if ( is_null( $param ) ) {
362 return $mwRaw->match( $param );
366 static function formatRaw( $num, $raw ) {
367 if( self
::isRaw( $raw ) ) {
371 return $wgContLang->formatNum( $num );
374 static function numberofpages( $parser, $raw = null ) {
375 return self
::formatRaw( SiteStats
::pages(), $raw );
377 static function numberofusers( $parser, $raw = null ) {
378 return self
::formatRaw( SiteStats
::users(), $raw );
380 static function numberofactiveusers( $parser, $raw = null ) {
381 return self
::formatRaw( SiteStats
::activeUsers(), $raw );
383 static function numberofarticles( $parser, $raw = null ) {
384 return self
::formatRaw( SiteStats
::articles(), $raw );
386 static function numberoffiles( $parser, $raw = null ) {
387 return self
::formatRaw( SiteStats
::images(), $raw );
389 static function numberofadmins( $parser, $raw = null ) {
390 return self
::formatRaw( SiteStats
::numberingroup('sysop'), $raw );
392 static function numberofedits( $parser, $raw = null ) {
393 return self
::formatRaw( SiteStats
::edits(), $raw );
395 static function numberofviews( $parser, $raw = null ) {
396 return self
::formatRaw( SiteStats
::views(), $raw );
398 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
399 return self
::formatRaw( SiteStats
::pagesInNs( intval( $namespace ) ), $raw );
401 static function numberingroup( $parser, $name = '', $raw = null) {
402 return self
::formatRaw( SiteStats
::numberingroup( strtolower( $name ) ), $raw );
407 * Given a title, return the namespace name that would be given by the
408 * corresponding magic word
409 * Note: function name changed to "mwnamespace" rather than "namespace"
410 * to not break PHP 5.3
412 static function mwnamespace( $parser, $title = null ) {
413 $t = Title
::newFromText( $title );
416 return str_replace( '_', ' ', $t->getNsText() );
418 static function namespacee( $parser, $title = null ) {
419 $t = Title
::newFromText( $title );
422 return wfUrlencode( $t->getNsText() );
424 static function talkspace( $parser, $title = null ) {
425 $t = Title
::newFromText( $title );
426 if ( is_null( $t ) ||
!$t->canTalk() )
428 return str_replace( '_', ' ', $t->getTalkNsText() );
430 static function talkspacee( $parser, $title = null ) {
431 $t = Title
::newFromText( $title );
432 if ( is_null( $t ) ||
!$t->canTalk() )
434 return wfUrlencode( $t->getTalkNsText() );
436 static function subjectspace( $parser, $title = null ) {
437 $t = Title
::newFromText( $title );
440 return str_replace( '_', ' ', $t->getSubjectNsText() );
442 static function subjectspacee( $parser, $title = null ) {
443 $t = Title
::newFromText( $title );
446 return wfUrlencode( $t->getSubjectNsText() );
450 * Functions to get and normalize pagenames, corresponding to the magic words
453 static function pagename( $parser, $title = null ) {
454 $t = Title
::newFromText( $title );
457 return wfEscapeWikiText( $t->getText() );
459 static function pagenamee( $parser, $title = null ) {
460 $t = Title
::newFromText( $title );
463 return wfEscapeWikiText( $t->getPartialURL() );
465 static function fullpagename( $parser, $title = null ) {
466 $t = Title
::newFromText( $title );
467 if ( is_null( $t ) ||
!$t->canTalk() )
469 return wfEscapeWikiText( $t->getPrefixedText() );
471 static function fullpagenamee( $parser, $title = null ) {
472 $t = Title
::newFromText( $title );
473 if ( is_null( $t ) ||
!$t->canTalk() )
475 return wfEscapeWikiText( $t->getPrefixedURL() );
477 static function subpagename( $parser, $title = null ) {
478 $t = Title
::newFromText( $title );
481 return wfEscapeWikiText( $t->getSubpageText() );
483 static function subpagenamee( $parser, $title = null ) {
484 $t = Title
::newFromText( $title );
487 return wfEscapeWikiText( $t->getSubpageUrlForm() );
489 static function basepagename( $parser, $title = null ) {
490 $t = Title
::newFromText( $title );
493 return wfEscapeWikiText( $t->getBaseText() );
495 static function basepagenamee( $parser, $title = null ) {
496 $t = Title
::newFromText( $title );
499 return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) ) );
501 static function talkpagename( $parser, $title = null ) {
502 $t = Title
::newFromText( $title );
503 if ( is_null( $t ) ||
!$t->canTalk() )
505 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
507 static function talkpagenamee( $parser, $title = null ) {
508 $t = Title
::newFromText( $title );
509 if ( is_null( $t ) ||
!$t->canTalk() )
511 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedUrl() );
513 static function subjectpagename( $parser, $title = null ) {
514 $t = Title
::newFromText( $title );
517 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
519 static function subjectpagenamee( $parser, $title = null ) {
520 $t = Title
::newFromText( $title );
523 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedUrl() );
527 * Return the number of pages in the given category, or 0 if it's nonexis-
528 * tent. This is an expensive parser function and can't be called too many
531 static function pagesincategory( $parser, $name = '', $raw = null ) {
532 static $cache = array();
533 $category = Category
::newFromName( $name );
535 if( !is_object( $category ) ) {
537 return self
::formatRaw( 0, $raw );
540 # Normalize name for cache
541 $name = $category->getName();
544 if( isset( $cache[$name] ) ) {
545 $count = $cache[$name];
546 } elseif( $parser->incrementExpensiveFunctionCount() ) {
547 $count = $cache[$name] = (int)$category->getPageCount();
549 return self
::formatRaw( $count, $raw );
553 * Return the size of the given page, or 0 if it's nonexistent. This is an
554 * expensive parser function and can't be called too many times per page.
556 * @todo FIXME: This doesn't work correctly on preview for getting the size
557 * of the current page.
558 * @todo FIXME: Title::getLength() documentation claims that it adds things
559 * to the link cache, so the local cache here should be unnecessary, but
560 * in fact calling getLength() repeatedly for the same $page does seem to
561 * run one query for each call?
562 * @param $parser Parser
564 static function pagesize( $parser, $page = '', $raw = null ) {
565 static $cache = array();
566 $title = Title
::newFromText( $page );
568 if( !is_object( $title ) ) {
570 return self
::formatRaw( 0, $raw );
573 # Normalize name for cache
574 $page = $title->getPrefixedText();
577 if( isset( $cache[$page] ) ) {
578 $length = $cache[$page];
579 } elseif( $parser->incrementExpensiveFunctionCount() ) {
580 $rev = Revision
::newFromTitle( $title );
581 $id = $rev ?
$rev->getPage() : 0;
582 $length = $cache[$page] = $rev ?
$rev->getSize() : 0;
584 // Register dependency in templatelinks
585 $parser->mOutput
->addTemplate( $title, $id, $rev ?
$rev->getId() : 0 );
587 return self
::formatRaw( $length, $raw );
591 * Returns the requested protection level for the current page
593 static function protectionlevel( $parser, $type = '' ) {
594 $restrictions = $parser->mTitle
->getRestrictions( strtolower( $type ) );
595 # Title::getRestrictions returns an array, its possible it may have
596 # multiple values in the future
597 return implode( $restrictions, ',' );
601 * Gives language names.
602 * @param $parser Parser
603 * @param $code String Language code
604 * @param $language String Language code
607 static function language( $parser, $code = '', $language = '' ) {
609 $code = strtolower( $code );
610 $language = strtolower( $language );
612 if ( $language !== '' ) {
613 $names = Language
::getTranslatedLanguageNames( $language );
614 return isset( $names[$code] ) ?
$names[$code] : wfBCP47( $code );
617 $lang = $wgContLang->getLanguageName( $code );
618 return $lang !== '' ?
$lang : wfBCP47( $code );
622 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
624 static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT
) {
625 $lengthOfPadding = mb_strlen( $padding );
626 if ( $lengthOfPadding == 0 ) return $string;
628 # The remaining length to add counts down to 0 as padding is added
629 $length = min( $length, 500 ) - mb_strlen( $string );
630 # $finalPadding is just $padding repeated enough times so that
631 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
633 while ( $length > 0 ) {
634 # If $length < $lengthofPadding, truncate $padding so we get the
635 # exact length desired.
636 $finalPadding .= mb_substr( $padding, 0, $length );
637 $length -= $lengthOfPadding;
640 if ( $direction == STR_PAD_LEFT
) {
641 return $finalPadding . $string;
643 return $string . $finalPadding;
647 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
648 return self
::pad( $string, $length, $padding, STR_PAD_LEFT
);
651 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
652 return self
::pad( $string, $length, $padding );
656 * @param $parser Parser
660 static function anchorencode( $parser, $text ) {
661 return substr( $parser->guessSectionNameFromWikiText( $text ), 1);
664 static function special( $parser, $text ) {
665 list( $page, $subpage ) = SpecialPageFactory
::resolveAlias( $text );
667 $title = SpecialPage
::getTitleFor( $page, $subpage );
670 return wfMsgForContent( 'nosuchspecialpage' );
675 * @param $parser Parser
679 public static function defaultsort( $parser, $text ) {
680 $text = trim( $text );
681 if( strlen( $text ) == 0 )
683 $old = $parser->getCustomDefaultSort();
684 $parser->setDefaultSort( $text );
685 if( $old === false ||
$old == $text )
688 return( '<span class="error">' .
689 wfMsgForContent( 'duplicate-defaultsort',
690 htmlspecialchars( $old ),
691 htmlspecialchars( $text ) ) .
695 // Usage {{filepath|300}}, {{filepath|nowiki}}, {{filepath|nowiki|300}} or {{filepath|300|nowiki}}
696 public static function filepath( $parser, $name='', $argA='', $argB='' ) {
697 $file = wfFindFile( $name );
699 $argA_int = intval( $argA );
700 $argB_int = intval( $argB );
702 if ( $argB_int > 0 ) {
703 // {{filepath: | option | size }}
707 } elseif ( $argA_int > 0 ) {
708 // {{filepath: | size [|option] }}
713 // {{filepath: [|option] }}
718 $url = $file->getFullUrl();
720 // If a size is requested...
721 if ( is_integer( $size ) ) {
722 $mto = $file->transform( array( 'width' => $size ) );
724 if ( $mto && !$mto->isError() ) {
725 // ... change the URL to point to a thumbnail.
726 $url = wfExpandUrl( $mto->getUrl() );
729 if ( $option == 'nowiki' ) {
730 return array( $url, 'nowiki' => true );
739 * Parser function to extension tag adaptor
741 public static function tagObj( $parser, $frame, $args ) {
742 if ( !count( $args ) ) {
745 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
747 if ( count( $args ) ) {
748 $inner = $frame->expand( array_shift( $args ) );
753 $stripList = $parser->getStripList();
754 if ( !in_array( $tagName, $stripList ) ) {
755 return '<span class="error">' .
756 wfMsgForContent( 'unknown_extension_tag', $tagName ) .
760 $attributes = array();
761 foreach ( $args as $arg ) {
762 $bits = $arg->splitArg();
763 if ( strval( $bits['index'] ) === '' ) {
764 $name = trim( $frame->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
765 $value = trim( $frame->expand( $bits['value'] ) );
766 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
767 $value = isset( $m[1] ) ?
$m[1] : '';
769 $attributes[$name] = $value;
776 'attributes' => $attributes,
777 'close' => "</$tagName>",
779 return $parser->extensionSubstitution( $params, $frame );