a98c5d3e446954540ba2a972f724e7edcbdcadda
3 * Parser functions provided by MediaWiki core
9 * Various core parser functions, registered in Parser::firstCallInit()
12 class CoreParserFunctions
{
13 static function register( $parser ) {
14 global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
16 # Syntax for arguments (see self::setFunctionHook):
17 # "name for lookup in localized magic words array",
19 # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}}
20 # instead of {{#int:...}})
22 $parser->setFunctionHook( 'int', array( __CLASS__
, 'intFunction' ), SFH_NO_HASH
);
23 $parser->setFunctionHook( 'ns', array( __CLASS__
, 'ns' ), SFH_NO_HASH
);
24 $parser->setFunctionHook( 'nse', array( __CLASS__
, 'nse' ), SFH_NO_HASH
);
25 $parser->setFunctionHook( 'urlencode', array( __CLASS__
, 'urlencode' ), SFH_NO_HASH
);
26 $parser->setFunctionHook( 'lcfirst', array( __CLASS__
, 'lcfirst' ), SFH_NO_HASH
);
27 $parser->setFunctionHook( 'ucfirst', array( __CLASS__
, 'ucfirst' ), SFH_NO_HASH
);
28 $parser->setFunctionHook( 'lc', array( __CLASS__
, 'lc' ), SFH_NO_HASH
);
29 $parser->setFunctionHook( 'uc', array( __CLASS__
, 'uc' ), SFH_NO_HASH
);
30 $parser->setFunctionHook( 'localurl', array( __CLASS__
, 'localurl' ), SFH_NO_HASH
);
31 $parser->setFunctionHook( 'localurle', array( __CLASS__
, 'localurle' ), SFH_NO_HASH
);
32 $parser->setFunctionHook( 'fullurl', array( __CLASS__
, 'fullurl' ), SFH_NO_HASH
);
33 $parser->setFunctionHook( 'fullurle', array( __CLASS__
, 'fullurle' ), SFH_NO_HASH
);
34 $parser->setFunctionHook( 'formatnum', array( __CLASS__
, 'formatnum' ), SFH_NO_HASH
);
35 $parser->setFunctionHook( 'grammar', array( __CLASS__
, 'grammar' ), SFH_NO_HASH
);
36 $parser->setFunctionHook( 'gender', array( __CLASS__
, 'gender' ), SFH_NO_HASH
);
37 $parser->setFunctionHook( 'plural', array( __CLASS__
, 'plural' ), SFH_NO_HASH
);
38 $parser->setFunctionHook( 'numberofpages', array( __CLASS__
, 'numberofpages' ), SFH_NO_HASH
);
39 $parser->setFunctionHook( 'numberofusers', array( __CLASS__
, 'numberofusers' ), SFH_NO_HASH
);
40 $parser->setFunctionHook( 'numberofactiveusers', array( __CLASS__
, 'numberofactiveusers' ), SFH_NO_HASH
);
41 $parser->setFunctionHook( 'numberofarticles', array( __CLASS__
, 'numberofarticles' ), SFH_NO_HASH
);
42 $parser->setFunctionHook( 'numberoffiles', array( __CLASS__
, 'numberoffiles' ), SFH_NO_HASH
);
43 $parser->setFunctionHook( 'numberofadmins', array( __CLASS__
, 'numberofadmins' ), SFH_NO_HASH
);
44 $parser->setFunctionHook( 'numberingroup', array( __CLASS__
, 'numberingroup' ), SFH_NO_HASH
);
45 $parser->setFunctionHook( 'numberofedits', array( __CLASS__
, 'numberofedits' ), SFH_NO_HASH
);
46 $parser->setFunctionHook( 'numberofviews', array( __CLASS__
, 'numberofviews' ), SFH_NO_HASH
);
47 $parser->setFunctionHook( 'language', array( __CLASS__
, 'language' ), SFH_NO_HASH
);
48 $parser->setFunctionHook( 'padleft', array( __CLASS__
, 'padleft' ), SFH_NO_HASH
);
49 $parser->setFunctionHook( 'padright', array( __CLASS__
, 'padright' ), SFH_NO_HASH
);
50 $parser->setFunctionHook( 'anchorencode', array( __CLASS__
, 'anchorencode' ), SFH_NO_HASH
);
51 $parser->setFunctionHook( 'special', array( __CLASS__
, 'special' ) );
52 $parser->setFunctionHook( 'defaultsort', array( __CLASS__
, 'defaultsort' ), SFH_NO_HASH
);
53 $parser->setFunctionHook( 'filepath', array( __CLASS__
, 'filepath' ), SFH_NO_HASH
);
54 $parser->setFunctionHook( 'pagesincategory', array( __CLASS__
, 'pagesincategory' ), SFH_NO_HASH
);
55 $parser->setFunctionHook( 'pagesize', array( __CLASS__
, 'pagesize' ), SFH_NO_HASH
);
56 $parser->setFunctionHook( 'protectionlevel', array( __CLASS__
, 'protectionlevel' ), SFH_NO_HASH
);
57 $parser->setFunctionHook( 'namespace', array( __CLASS__
, 'mwnamespace' ), SFH_NO_HASH
);
58 $parser->setFunctionHook( 'namespacee', array( __CLASS__
, 'namespacee' ), SFH_NO_HASH
);
59 $parser->setFunctionHook( 'talkspace', array( __CLASS__
, 'talkspace' ), SFH_NO_HASH
);
60 $parser->setFunctionHook( 'talkspacee', array( __CLASS__
, 'talkspacee' ), SFH_NO_HASH
);
61 $parser->setFunctionHook( 'subjectspace', array( __CLASS__
, 'subjectspace' ), SFH_NO_HASH
);
62 $parser->setFunctionHook( 'subjectspacee', array( __CLASS__
, 'subjectspacee' ), SFH_NO_HASH
);
63 $parser->setFunctionHook( 'pagename', array( __CLASS__
, 'pagename' ), SFH_NO_HASH
);
64 $parser->setFunctionHook( 'pagenamee', array( __CLASS__
, 'pagenamee' ), SFH_NO_HASH
);
65 $parser->setFunctionHook( 'fullpagename', array( __CLASS__
, 'fullpagename' ), SFH_NO_HASH
);
66 $parser->setFunctionHook( 'fullpagenamee', array( __CLASS__
, 'fullpagenamee' ), SFH_NO_HASH
);
67 $parser->setFunctionHook( 'basepagename', array( __CLASS__
, 'basepagename' ), SFH_NO_HASH
);
68 $parser->setFunctionHook( 'basepagenamee', array( __CLASS__
, 'basepagenamee' ), SFH_NO_HASH
);
69 $parser->setFunctionHook( 'subpagename', array( __CLASS__
, 'subpagename' ), SFH_NO_HASH
);
70 $parser->setFunctionHook( 'subpagenamee', array( __CLASS__
, 'subpagenamee' ), SFH_NO_HASH
);
71 $parser->setFunctionHook( 'talkpagename', array( __CLASS__
, 'talkpagename' ), SFH_NO_HASH
);
72 $parser->setFunctionHook( 'talkpagenamee', array( __CLASS__
, 'talkpagenamee' ), SFH_NO_HASH
);
73 $parser->setFunctionHook( 'subjectpagename', array( __CLASS__
, 'subjectpagename' ), SFH_NO_HASH
);
74 $parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__
, 'subjectpagenamee' ), SFH_NO_HASH
);
75 $parser->setFunctionHook( 'tag', array( __CLASS__
, 'tagObj' ), SFH_OBJECT_ARGS
);
76 $parser->setFunctionHook( 'formatdate', array( __CLASS__
, 'formatDate' ) );
78 if ( $wgAllowDisplayTitle ) {
79 $parser->setFunctionHook( 'displaytitle', array( __CLASS__
, 'displaytitle' ), SFH_NO_HASH
);
81 if ( $wgAllowSlowParserFunctions ) {
82 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__
, 'pagesinnamespace' ), SFH_NO_HASH
);
86 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
87 if ( strval( $part1 ) !== '' ) {
88 $args = array_slice( func_get_args(), 2 );
89 $message = wfMsgGetKey( $part1, true, $parser->getOptions()->getUserLang(), false );
90 $message = wfMsgReplaceArgs( $message, $args );
91 $message = $parser->replaceVariables( $message ); // like $wgMessageCache->transform()
94 return array( 'found' => false );
98 static function formatDate( $parser, $date, $defaultPref = null ) {
99 $df = DateFormatter
::getInstance();
101 $date = trim( $date );
103 $pref = $parser->getOptions()->getDateFormat();
105 // Specify a different default date format other than the the normal default
106 // iff the user has 'default' for their setting
107 if ( $pref == 'default' && $defaultPref )
108 $pref = $defaultPref;
110 $date = $df->reformat( $pref, $date, array( 'match-whole' ) );
114 static function ns( $parser, $part1 = '' ) {
116 if ( intval( $part1 ) ||
$part1 == "0" ) {
117 $index = intval( $part1 );
119 $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
121 if ( $index !== false ) {
122 return $wgContLang->getFormattedNsText( $index );
124 return array( 'found' => false );
128 static function nse( $parser, $part1 = '' ) {
129 return wfUrlencode( str_replace( ' ', '_', self
::ns( $parser, $part1 ) ) );
133 * urlencodes a string according to one of three patterns: (bug 22474)
135 * By default (for HTTP "query" strings), spaces are encoded as '+'.
136 * Or to encode a value for the HTTP "path", spaces are encoded as '%20'.
137 * For links to "wiki"s, or similar software, spaces are encoded as '_',
139 * @param $parser Parser object
140 * @param $s String: The text to encode.
141 * @param $arg String (optional): The type of encoding.
143 static function urlencode( $parser, $s = '', $arg = null ) {
144 static $magicWords = null;
145 if ( is_null( $magicWords ) ) {
146 $magicWords = new MagicWordArray( array( 'url_path', 'url_query', 'url_wiki' ) );
148 switch( $magicWords->matchStartToEnd( $arg ) ) {
150 // Encode as though it's a wiki page, '_' for ' '.
152 return wfUrlencode( str_replace( ' ', '_', $s ) );
154 // Encode for an HTTP Path, '%20' for ' '.
156 return rawurlencode( $s );
158 // Encode for HTTP query, '+' for ' '.
161 return urlencode( $s );
165 static function lcfirst( $parser, $s = '' ) {
167 return $wgContLang->lcfirst( $s );
170 static function ucfirst( $parser, $s = '' ) {
172 return $wgContLang->ucfirst( $s );
175 static function lc( $parser, $s = '' ) {
177 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
178 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
180 return $wgContLang->lc( $s );
184 static function uc( $parser, $s = '' ) {
186 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
187 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
189 return $wgContLang->uc( $s );
193 static function localurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getLocalURL', $s, $arg ); }
194 static function localurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeLocalURL', $s, $arg ); }
195 static function fullurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getFullURL', $s, $arg ); }
196 static function fullurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeFullURL', $s, $arg ); }
198 static function urlFunction( $func, $s = '', $arg = null ) {
199 $title = Title
::newFromText( $s );
200 # Due to order of execution of a lot of bits, the values might be encoded
201 # before arriving here; if that's true, then the title can't be created
202 # and the variable will fail. If we can't get a decent title from the first
203 # attempt, url-decode and try for a second.
204 if( is_null( $title ) )
205 $title = Title
::newFromURL( urldecode( $s ) );
206 if( !is_null( $title ) ) {
207 # Convert NS_MEDIA -> NS_FILE
208 if( $title->getNamespace() == NS_MEDIA
) {
209 $title = Title
::makeTitle( NS_FILE
, $title->getDBkey() );
211 if( !is_null( $arg ) ) {
212 $text = $title->$func( $arg );
214 $text = $title->$func();
218 return array( 'found' => false );
222 static function formatNum( $parser, $num = '', $raw = null) {
223 if ( self
::israw( $raw ) ) {
224 return $parser->getFunctionLang()->parseFormattedNumber( $num );
226 return $parser->getFunctionLang()->formatNum( $num );
230 static function grammar( $parser, $case = '', $word = '' ) {
231 return $parser->getFunctionLang()->convertGrammar( $word, $case );
234 static function gender( $parser, $user ) {
235 wfProfileIn( __METHOD__
);
236 $forms = array_slice( func_get_args(), 2);
239 $gender = User
::getDefaultOption( 'gender' );
242 $title = Title
::newFromText( $user );
244 if ( is_object( $title ) && $title->getNamespace() == NS_USER
)
245 $user = $title->getText();
247 // check parameter, or use the ParserOptions if in interface message
248 $user = User
::newFromName( $user );
250 $gender = $user->getOption( 'gender' );
251 } elseif ( $parser->getOptions()->getInterfaceMessage() ) {
252 $gender = $parser->getOptions()->getUser()->getOption( 'gender' );
254 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
255 wfProfileOut( __METHOD__
);
258 static function plural( $parser, $text = '' ) {
259 $forms = array_slice( func_get_args(), 2 );
260 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
261 return $parser->getFunctionLang()->convertPlural( $text, $forms );
265 * Override the title of the page when viewed, provided we've been given a
266 * title which will normalise to the canonical title
268 * @param $parser Parser: parent parser
269 * @param $text String: desired title text
272 static function displaytitle( $parser, $text = '' ) {
273 global $wgRestrictDisplayTitle;
275 #parse a limited subset of wiki markup (just the single quote items)
276 $text = $parser->doQuotes( $text );
278 #remove stripped text (e.g. the UNIQ-QINU stuff) that was generated by tag extensions/whatever
279 $text = preg_replace( '/' . preg_quote( $parser->uniqPrefix(), '/' ) . '.*?'
280 . preg_quote( Parser
::MARKER_SUFFIX
, '/' ) . '/', '', $text );
282 #list of disallowed tags for DISPLAYTITLE
283 #these will be escaped even though they are allowed in normal wiki text
284 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr',
285 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp', 'br' );
287 #only requested titles that normalize to the actual title are allowed through
288 #if $wgRestrictDisplayTitle is true (it is by default)
289 #mimic the escaping process that occurs in OutputPage::setPageTitle
290 $text = Sanitizer
::normalizeCharReferences( Sanitizer
::removeHTMLtags( $text, null, array(), array(), $bad ) );
291 $title = Title
::newFromText( Sanitizer
::stripAllTags( $text ) );
293 if( !$wgRestrictDisplayTitle ) {
294 $parser->mOutput
->setDisplayTitle( $text );
296 if ( $title instanceof Title
&& $title->getFragment() == '' && $title->equals( $parser->mTitle
) ) {
297 $parser->mOutput
->setDisplayTitle( $text );
304 static function isRaw( $param ) {
307 $mwRaw =& MagicWord
::get( 'rawsuffix' );
309 if ( is_null( $param ) ) {
312 return $mwRaw->match( $param );
316 static function formatRaw( $num, $raw ) {
317 if( self
::isRaw( $raw ) ) {
321 return $wgContLang->formatNum( $num );
324 static function numberofpages( $parser, $raw = null ) {
325 return self
::formatRaw( SiteStats
::pages(), $raw );
327 static function numberofusers( $parser, $raw = null ) {
328 return self
::formatRaw( SiteStats
::users(), $raw );
330 static function numberofactiveusers( $parser, $raw = null ) {
331 return self
::formatRaw( SiteStats
::activeUsers(), $raw );
333 static function numberofarticles( $parser, $raw = null ) {
334 return self
::formatRaw( SiteStats
::articles(), $raw );
336 static function numberoffiles( $parser, $raw = null ) {
337 return self
::formatRaw( SiteStats
::images(), $raw );
339 static function numberofadmins( $parser, $raw = null ) {
340 return self
::formatRaw( SiteStats
::numberingroup('sysop'), $raw );
342 static function numberofedits( $parser, $raw = null ) {
343 return self
::formatRaw( SiteStats
::edits(), $raw );
345 static function numberofviews( $parser, $raw = null ) {
346 return self
::formatRaw( SiteStats
::views(), $raw );
348 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
349 return self
::formatRaw( SiteStats
::pagesInNs( intval( $namespace ) ), $raw );
351 static function numberingroup( $parser, $name = '', $raw = null) {
352 return self
::formatRaw( SiteStats
::numberingroup( strtolower( $name ) ), $raw );
357 * Given a title, return the namespace name that would be given by the
358 * corresponding magic word
359 * Note: function name changed to "mwnamespace" rather than "namespace"
360 * to not break PHP 5.3
362 static function mwnamespace( $parser, $title = null ) {
363 $t = Title
::newFromText( $title );
366 return str_replace( '_', ' ', $t->getNsText() );
368 static function namespacee( $parser, $title = null ) {
369 $t = Title
::newFromText( $title );
372 return wfUrlencode( $t->getNsText() );
374 static function talkspace( $parser, $title = null ) {
375 $t = Title
::newFromText( $title );
376 if ( is_null( $t ) ||
!$t->canTalk() )
378 return str_replace( '_', ' ', $t->getTalkNsText() );
380 static function talkspacee( $parser, $title = null ) {
381 $t = Title
::newFromText( $title );
382 if ( is_null( $t ) ||
!$t->canTalk() )
384 return wfUrlencode( $t->getTalkNsText() );
386 static function subjectspace( $parser, $title = null ) {
387 $t = Title
::newFromText( $title );
390 return str_replace( '_', ' ', $t->getSubjectNsText() );
392 static function subjectspacee( $parser, $title = null ) {
393 $t = Title
::newFromText( $title );
396 return wfUrlencode( $t->getSubjectNsText() );
399 * Functions to get and normalize pagenames, corresponding to the magic words
402 static function pagename( $parser, $title = null ) {
403 $t = Title
::newFromText( $title );
406 return wfEscapeWikiText( $t->getText() );
408 static function pagenamee( $parser, $title = null ) {
409 $t = Title
::newFromText( $title );
412 return wfEscapeWikiText( $t->getPartialURL() );
414 static function fullpagename( $parser, $title = null ) {
415 $t = Title
::newFromText( $title );
416 if ( is_null( $t ) ||
!$t->canTalk() )
418 return wfEscapeWikiText( $t->getPrefixedText() );
420 static function fullpagenamee( $parser, $title = null ) {
421 $t = Title
::newFromText( $title );
422 if ( is_null( $t ) ||
!$t->canTalk() )
424 return wfEscapeWikiText( $t->getPrefixedURL() );
426 static function subpagename( $parser, $title = null ) {
427 $t = Title
::newFromText( $title );
430 return wfEscapeWikiText( $t->getSubpageText() );
432 static function subpagenamee( $parser, $title = null ) {
433 $t = Title
::newFromText( $title );
436 return wfEscapeWikiText( $t->getSubpageUrlForm() );
438 static function basepagename( $parser, $title = null ) {
439 $t = Title
::newFromText( $title );
442 return wfEscapeWikiText( $t->getBaseText() );
444 static function basepagenamee( $parser, $title = null ) {
445 $t = Title
::newFromText( $title );
448 return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) ) );
450 static function talkpagename( $parser, $title = null ) {
451 $t = Title
::newFromText( $title );
452 if ( is_null( $t ) ||
!$t->canTalk() )
454 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
456 static function talkpagenamee( $parser, $title = null ) {
457 $t = Title
::newFromText( $title );
458 if ( is_null( $t ) ||
!$t->canTalk() )
460 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedUrl() );
462 static function subjectpagename( $parser, $title = null ) {
463 $t = Title
::newFromText( $title );
466 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
468 static function subjectpagenamee( $parser, $title = null ) {
469 $t = Title
::newFromText( $title );
472 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedUrl() );
476 * Return the number of pages in the given category, or 0 if it's nonexis-
477 * tent. This is an expensive parser function and can't be called too many
480 static function pagesincategory( $parser, $name = '', $raw = null ) {
481 static $cache = array();
482 $category = Category
::newFromName( $name );
484 if( !is_object( $category ) ) {
486 return self
::formatRaw( 0, $raw );
489 # Normalize name for cache
490 $name = $category->getName();
493 if( isset( $cache[$name] ) ) {
494 $count = $cache[$name];
495 } elseif( $parser->incrementExpensiveFunctionCount() ) {
496 $count = $cache[$name] = (int)$category->getPageCount();
498 return self
::formatRaw( $count, $raw );
502 * Return the size of the given page, or 0 if it's nonexistent. This is an
503 * expensive parser function and can't be called too many times per page.
505 * @todo Fixme: This doesn't work correctly on preview for getting the size
506 * of the current page.
507 * @todo Fixme: Title::getLength() documentation claims that it adds things
508 * to the link cache, so the local cache here should be unnecessary, but
509 * in fact calling getLength() repeatedly for the same $page does seem to
510 * run one query for each call?
512 static function pagesize( $parser, $page = '', $raw = null ) {
513 static $cache = array();
514 $title = Title
::newFromText( $page );
516 if( !is_object( $title ) ) {
518 return self
::formatRaw( 0, $raw );
521 # Normalize name for cache
522 $page = $title->getPrefixedText();
525 if( isset( $cache[$page] ) ) {
526 $length = $cache[$page];
527 } elseif( $parser->incrementExpensiveFunctionCount() ) {
528 $rev = Revision
::newFromTitle( $title );
529 $id = $rev ?
$rev->getPage() : 0;
530 $length = $cache[$page] = $rev ?
$rev->getSize() : 0;
532 // Register dependency in templatelinks
533 $parser->mOutput
->addTemplate( $title, $id, $rev ?
$rev->getId() : 0 );
535 return self
::formatRaw( $length, $raw );
539 * Returns the requested protection level for the current page
541 static function protectionlevel( $parser, $type = '' ) {
542 $restrictions = $parser->mTitle
->getRestrictions( strtolower( $type ) );
543 # Title::getRestrictions returns an array, its possible it may have
544 # multiple values in the future
545 return implode( $restrictions, ',' );
548 static function language( $parser, $arg = '' ) {
550 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
551 return $lang != '' ?
$lang : $arg;
555 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
557 static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT
) {
558 $lengthOfPadding = mb_strlen( $padding );
559 if ( $lengthOfPadding == 0 ) return $string;
561 # The remaining length to add counts down to 0 as padding is added
562 $length = min( $length, 500 ) - mb_strlen( $string );
563 # $finalPadding is just $padding repeated enough times so that
564 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
566 while ( $length > 0 ) {
567 # If $length < $lengthofPadding, truncate $padding so we get the
568 # exact length desired.
569 $finalPadding .= mb_substr( $padding, 0, $length );
570 $length -= $lengthOfPadding;
573 if ( $direction == STR_PAD_LEFT
) {
574 return $finalPadding . $string;
576 return $string . $finalPadding;
580 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
581 return self
::pad( $string, $length, $padding, STR_PAD_LEFT
);
584 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
585 return self
::pad( $string, $length, $padding );
588 static function anchorencode( $parser, $text ) {
589 return substr( $parser->guessSectionNameFromWikiText( $text ), 1);
592 static function special( $parser, $text ) {
593 list( $page, $subpage ) = SpecialPage
::resolveAliasWithSubpage( $text );
595 $title = SpecialPage
::getTitleFor( $page, $subpage );
598 return wfMsgForContent( 'nosuchspecialpage' );
602 public static function defaultsort( $parser, $text ) {
603 $text = trim( $text );
604 if( strlen( $text ) == 0 )
606 $old = $parser->getCustomDefaultSort();
607 $parser->setDefaultSort( $text );
608 if( $old === false ||
$old == $text )
611 return( '<span class="error">' .
612 wfMsgForContent( 'duplicate-defaultsort',
613 htmlspecialchars( $old ),
614 htmlspecialchars( $text ) ) .
618 public static function filepath( $parser, $name='', $option='' ) {
619 $file = wfFindFile( $name );
621 $url = $file->getFullUrl();
622 if( $option == 'nowiki' ) {
623 return array( $url, 'nowiki' => true );
632 * Parser function to extension tag adaptor
634 public static function tagObj( $parser, $frame, $args ) {
635 if ( !count( $args ) ) {
638 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
640 if ( count( $args ) ) {
641 $inner = $frame->expand( array_shift( $args ) );
646 $stripList = $parser->getStripList();
647 if ( !in_array( $tagName, $stripList ) ) {
648 return '<span class="error">' .
649 wfMsgForContent( 'unknown_extension_tag', $tagName ) .
653 $attributes = array();
654 foreach ( $args as $arg ) {
655 $bits = $arg->splitArg();
656 if ( strval( $bits['index'] ) === '' ) {
657 $name = trim( $frame->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
658 $value = trim( $frame->expand( $bits['value'] ) );
659 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
660 $value = isset( $m[1] ) ?
$m[1] : '';
662 $attributes[$name] = $value;
669 'attributes' => $attributes,
670 'close' => "</$tagName>",
672 return $parser->extensionSubstitution( $params, $frame );