3 * Parser functions provided by MediaWiki core
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Various core parser functions, registered in Parser::firstCallInit()
28 class CoreParserFunctions
{
30 * @param $parser Parser
33 static function register( $parser ) {
34 global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
36 # Syntax for arguments (see self::setFunctionHook):
37 # "name for lookup in localized magic words array",
39 # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}}
40 # instead of {{#int:...}})
42 $parser->setFunctionHook( 'int', array( __CLASS__
, 'intFunction' ), SFH_NO_HASH
);
43 $parser->setFunctionHook( 'ns', array( __CLASS__
, 'ns' ), SFH_NO_HASH
);
44 $parser->setFunctionHook( 'nse', array( __CLASS__
, 'nse' ), SFH_NO_HASH
);
45 $parser->setFunctionHook( 'urlencode', array( __CLASS__
, 'urlencode' ), SFH_NO_HASH
);
46 $parser->setFunctionHook( 'lcfirst', array( __CLASS__
, 'lcfirst' ), SFH_NO_HASH
);
47 $parser->setFunctionHook( 'ucfirst', array( __CLASS__
, 'ucfirst' ), SFH_NO_HASH
);
48 $parser->setFunctionHook( 'lc', array( __CLASS__
, 'lc' ), SFH_NO_HASH
);
49 $parser->setFunctionHook( 'uc', array( __CLASS__
, 'uc' ), SFH_NO_HASH
);
50 $parser->setFunctionHook( 'localurl', array( __CLASS__
, 'localurl' ), SFH_NO_HASH
);
51 $parser->setFunctionHook( 'localurle', array( __CLASS__
, 'localurle' ), SFH_NO_HASH
);
52 $parser->setFunctionHook( 'fullurl', array( __CLASS__
, 'fullurl' ), SFH_NO_HASH
);
53 $parser->setFunctionHook( 'fullurle', array( __CLASS__
, 'fullurle' ), SFH_NO_HASH
);
54 $parser->setFunctionHook( 'canonicalurl', array( __CLASS__
, 'canonicalurl' ), SFH_NO_HASH
);
55 $parser->setFunctionHook( 'canonicalurle', array( __CLASS__
, 'canonicalurle' ), SFH_NO_HASH
);
56 $parser->setFunctionHook( 'formatnum', array( __CLASS__
, 'formatnum' ), SFH_NO_HASH
);
57 $parser->setFunctionHook( 'grammar', array( __CLASS__
, 'grammar' ), SFH_NO_HASH
);
58 $parser->setFunctionHook( 'gender', array( __CLASS__
, 'gender' ), SFH_NO_HASH
);
59 $parser->setFunctionHook( 'plural', array( __CLASS__
, 'plural' ), SFH_NO_HASH
);
60 $parser->setFunctionHook( 'numberofpages', array( __CLASS__
, 'numberofpages' ), SFH_NO_HASH
);
61 $parser->setFunctionHook( 'numberofusers', array( __CLASS__
, 'numberofusers' ), SFH_NO_HASH
);
62 $parser->setFunctionHook( 'numberofactiveusers', array( __CLASS__
, 'numberofactiveusers' ), SFH_NO_HASH
);
63 $parser->setFunctionHook( 'numberofarticles', array( __CLASS__
, 'numberofarticles' ), SFH_NO_HASH
);
64 $parser->setFunctionHook( 'numberoffiles', array( __CLASS__
, 'numberoffiles' ), SFH_NO_HASH
);
65 $parser->setFunctionHook( 'numberofadmins', array( __CLASS__
, 'numberofadmins' ), SFH_NO_HASH
);
66 $parser->setFunctionHook( 'numberingroup', array( __CLASS__
, 'numberingroup' ), SFH_NO_HASH
);
67 $parser->setFunctionHook( 'numberofedits', array( __CLASS__
, 'numberofedits' ), SFH_NO_HASH
);
68 $parser->setFunctionHook( 'numberofviews', array( __CLASS__
, 'numberofviews' ), SFH_NO_HASH
);
69 $parser->setFunctionHook( 'language', array( __CLASS__
, 'language' ), SFH_NO_HASH
);
70 $parser->setFunctionHook( 'padleft', array( __CLASS__
, 'padleft' ), SFH_NO_HASH
);
71 $parser->setFunctionHook( 'padright', array( __CLASS__
, 'padright' ), SFH_NO_HASH
);
72 $parser->setFunctionHook( 'anchorencode', array( __CLASS__
, 'anchorencode' ), SFH_NO_HASH
);
73 $parser->setFunctionHook( 'special', array( __CLASS__
, 'special' ) );
74 $parser->setFunctionHook( 'speciale', array( __CLASS__
, 'speciale' ) );
75 $parser->setFunctionHook( 'defaultsort', array( __CLASS__
, 'defaultsort' ), SFH_NO_HASH
);
76 $parser->setFunctionHook( 'filepath', array( __CLASS__
, 'filepath' ), SFH_NO_HASH
);
77 $parser->setFunctionHook( 'pagesincategory', array( __CLASS__
, 'pagesincategory' ), SFH_NO_HASH
);
78 $parser->setFunctionHook( 'pagesize', array( __CLASS__
, 'pagesize' ), SFH_NO_HASH
);
79 $parser->setFunctionHook( 'protectionlevel', array( __CLASS__
, 'protectionlevel' ), SFH_NO_HASH
);
80 $parser->setFunctionHook( 'namespace', array( __CLASS__
, 'mwnamespace' ), SFH_NO_HASH
);
81 $parser->setFunctionHook( 'namespacee', array( __CLASS__
, 'namespacee' ), SFH_NO_HASH
);
82 $parser->setFunctionHook( 'namespacenumber', array( __CLASS__
, 'namespacenumber' ), SFH_NO_HASH
);
83 $parser->setFunctionHook( 'talkspace', array( __CLASS__
, 'talkspace' ), SFH_NO_HASH
);
84 $parser->setFunctionHook( 'talkspacee', array( __CLASS__
, 'talkspacee' ), SFH_NO_HASH
);
85 $parser->setFunctionHook( 'subjectspace', array( __CLASS__
, 'subjectspace' ), SFH_NO_HASH
);
86 $parser->setFunctionHook( 'subjectspacee', array( __CLASS__
, 'subjectspacee' ), SFH_NO_HASH
);
87 $parser->setFunctionHook( 'pagename', array( __CLASS__
, 'pagename' ), SFH_NO_HASH
);
88 $parser->setFunctionHook( 'pagenamee', array( __CLASS__
, 'pagenamee' ), SFH_NO_HASH
);
89 $parser->setFunctionHook( 'fullpagename', array( __CLASS__
, 'fullpagename' ), SFH_NO_HASH
);
90 $parser->setFunctionHook( 'fullpagenamee', array( __CLASS__
, 'fullpagenamee' ), SFH_NO_HASH
);
91 $parser->setFunctionHook( 'basepagename', array( __CLASS__
, 'basepagename' ), SFH_NO_HASH
);
92 $parser->setFunctionHook( 'basepagenamee', array( __CLASS__
, 'basepagenamee' ), SFH_NO_HASH
);
93 $parser->setFunctionHook( 'subpagename', array( __CLASS__
, 'subpagename' ), SFH_NO_HASH
);
94 $parser->setFunctionHook( 'subpagenamee', array( __CLASS__
, 'subpagenamee' ), SFH_NO_HASH
);
95 $parser->setFunctionHook( 'talkpagename', array( __CLASS__
, 'talkpagename' ), SFH_NO_HASH
);
96 $parser->setFunctionHook( 'talkpagenamee', array( __CLASS__
, 'talkpagenamee' ), SFH_NO_HASH
);
97 $parser->setFunctionHook( 'subjectpagename', array( __CLASS__
, 'subjectpagename' ), SFH_NO_HASH
);
98 $parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__
, 'subjectpagenamee' ), SFH_NO_HASH
);
99 $parser->setFunctionHook( 'tag', array( __CLASS__
, 'tagObj' ), SFH_OBJECT_ARGS
);
100 $parser->setFunctionHook( 'formatdate', array( __CLASS__
, 'formatDate' ) );
102 if ( $wgAllowDisplayTitle ) {
103 $parser->setFunctionHook( 'displaytitle', array( __CLASS__
, 'displaytitle' ), SFH_NO_HASH
);
105 if ( $wgAllowSlowParserFunctions ) {
106 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__
, 'pagesinnamespace' ), SFH_NO_HASH
);
111 * @param $parser Parser
112 * @param string $part1
115 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
116 if ( strval( $part1 ) !== '' ) {
117 $args = array_slice( func_get_args(), 2 );
118 $message = wfMessage( $part1, $args )->inLanguage( $parser->getOptions()->getUserLangObj() )->plain();
119 return array( $message, 'noparse' => false );
121 return array( 'found' => false );
126 * @param $parser Parser
128 * @param null $defaultPref
129 * @return mixed|string
131 static function formatDate( $parser, $date, $defaultPref = null ) {
132 $df = DateFormatter
::getInstance();
134 $date = trim( $date );
136 $pref = $parser->getOptions()->getDateFormat();
138 // Specify a different default date format other than the the normal default
139 // iff the user has 'default' for their setting
140 if ( $pref == 'default' && $defaultPref )
141 $pref = $defaultPref;
143 $date = $df->reformat( $pref, $date, array( 'match-whole' ) );
147 static function ns( $parser, $part1 = '' ) {
149 if ( intval( $part1 ) ||
$part1 == "0" ) {
150 $index = intval( $part1 );
152 $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
154 if ( $index !== false ) {
155 return $wgContLang->getFormattedNsText( $index );
157 return array( 'found' => false );
161 static function nse( $parser, $part1 = '' ) {
162 $ret = self
::ns( $parser, $part1 );
163 if ( is_string( $ret ) ) {
164 $ret = wfUrlencode( str_replace( ' ', '_', $ret ) );
170 * urlencodes a string according to one of three patterns: (bug 22474)
172 * By default (for HTTP "query" strings), spaces are encoded as '+'.
173 * Or to encode a value for the HTTP "path", spaces are encoded as '%20'.
174 * For links to "wiki"s, or similar software, spaces are encoded as '_',
176 * @param $parser Parser object
177 * @param $s String: The text to encode.
178 * @param $arg String (optional): The type of encoding.
181 static function urlencode( $parser, $s = '', $arg = null ) {
182 static $magicWords = null;
183 if ( is_null( $magicWords ) ) {
184 $magicWords = new MagicWordArray( array( 'url_path', 'url_query', 'url_wiki' ) );
186 switch( $magicWords->matchStartToEnd( $arg ) ) {
188 // Encode as though it's a wiki page, '_' for ' '.
190 $func = 'wfUrlencode';
191 $s = str_replace( ' ', '_', $s );
194 // Encode for an HTTP Path, '%20' for ' '.
196 $func = 'rawurlencode';
199 // Encode for HTTP query, '+' for ' '.
204 return $parser->markerSkipCallback( $s, $func );
207 static function lcfirst( $parser, $s = '' ) {
209 return $wgContLang->lcfirst( $s );
212 static function ucfirst( $parser, $s = '' ) {
214 return $wgContLang->ucfirst( $s );
218 * @param $parser Parser
222 static function lc( $parser, $s = '' ) {
224 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
228 * @param $parser Parser
232 static function uc( $parser, $s = '' ) {
234 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
237 static function localurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getLocalURL', $s, $arg ); }
238 static function localurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeLocalURL', $s, $arg ); }
239 static function fullurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getFullURL', $s, $arg ); }
240 static function fullurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeFullURL', $s, $arg ); }
241 static function canonicalurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getCanonicalURL', $s, $arg ); }
242 static function canonicalurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeCanonicalURL', $s, $arg ); }
244 static function urlFunction( $func, $s = '', $arg = null ) {
245 $title = Title
::newFromText( $s );
246 # Due to order of execution of a lot of bits, the values might be encoded
247 # before arriving here; if that's true, then the title can't be created
248 # and the variable will fail. If we can't get a decent title from the first
249 # attempt, url-decode and try for a second.
250 if( is_null( $title ) )
251 $title = Title
::newFromURL( urldecode( $s ) );
252 if( !is_null( $title ) ) {
253 # Convert NS_MEDIA -> NS_FILE
254 if( $title->getNamespace() == NS_MEDIA
) {
255 $title = Title
::makeTitle( NS_FILE
, $title->getDBkey() );
257 if( !is_null( $arg ) ) {
258 $text = $title->$func( $arg );
260 $text = $title->$func();
264 return array( 'found' => false );
269 * @param $parser Parser
274 static function formatnum( $parser, $num = '', $raw = null) {
275 if ( self
::isRaw( $raw ) ) {
276 $func = array( $parser->getFunctionLang(), 'parseFormattedNumber' );
278 $func = array( $parser->getFunctionLang(), 'formatNum' );
280 return $parser->markerSkipCallback( $num, $func );
284 * @param $parser Parser
285 * @param string $case
286 * @param string $word
289 static function grammar( $parser, $case = '', $word = '' ) {
290 $word = $parser->killMarkers( $word );
291 return $parser->getFunctionLang()->convertGrammar( $word, $case );
295 * @param $parser Parser
296 * @param $username string
299 static function gender( $parser, $username ) {
300 wfProfileIn( __METHOD__
);
301 $forms = array_slice( func_get_args(), 2 );
303 // Some shortcuts to avoid loading user data unnecessarily
304 if ( count( $forms ) === 0 ) {
305 wfProfileOut( __METHOD__
);
307 } elseif ( count( $forms ) === 1 ) {
308 wfProfileOut( __METHOD__
);
312 $username = trim( $username );
315 $gender = User
::getDefaultOption( 'gender' );
318 $title = Title
::newFromText( $username );
320 if ( $title && $title->getNamespace() == NS_USER
) {
321 $username = $title->getText();
324 // check parameter, or use the ParserOptions if in interface message
325 $user = User
::newFromName( $username );
327 $gender = GenderCache
::singleton()->getGenderOf( $user, __METHOD__
);
328 } elseif ( $username === '' && $parser->getOptions()->getInterfaceMessage() ) {
329 $gender = GenderCache
::singleton()->getGenderOf( $parser->getOptions()->getUser(), __METHOD__
);
331 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
332 wfProfileOut( __METHOD__
);
337 * @param $parser Parser
338 * @param string $text
341 static function plural( $parser, $text = '' ) {
342 $forms = array_slice( func_get_args(), 2 );
343 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
344 return $parser->getFunctionLang()->convertPlural( $text, $forms );
348 * Override the title of the page when viewed, provided we've been given a
349 * title which will normalise to the canonical title
351 * @param $parser Parser: parent parser
352 * @param $text String: desired title text
355 static function displaytitle( $parser, $text = '' ) {
356 global $wgRestrictDisplayTitle;
358 #parse a limited subset of wiki markup (just the single quote items)
359 $text = $parser->doQuotes( $text );
361 #remove stripped text (e.g. the UNIQ-QINU stuff) that was generated by tag extensions/whatever
362 $text = preg_replace( '/' . preg_quote( $parser->uniqPrefix(), '/' ) . '.*?'
363 . preg_quote( Parser
::MARKER_SUFFIX
, '/' ) . '/', '', $text );
365 #list of disallowed tags for DISPLAYTITLE
366 #these will be escaped even though they are allowed in normal wiki text
367 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr',
368 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp', 'br' );
370 #only requested titles that normalize to the actual title are allowed through
371 #if $wgRestrictDisplayTitle is true (it is by default)
372 #mimic the escaping process that occurs in OutputPage::setPageTitle
373 $text = Sanitizer
::normalizeCharReferences( Sanitizer
::removeHTMLtags( $text, null, array(), array(), $bad ) );
374 $title = Title
::newFromText( Sanitizer
::stripAllTags( $text ) );
376 if( !$wgRestrictDisplayTitle ) {
377 $parser->mOutput
->setDisplayTitle( $text );
379 if ( $title instanceof Title
&& $title->getFragment() == '' && $title->equals( $parser->mTitle
) ) {
380 $parser->mOutput
->setDisplayTitle( $text );
387 static function isRaw( $param ) {
390 $mwRaw =& MagicWord
::get( 'rawsuffix' );
392 if ( is_null( $param ) ) {
395 return $mwRaw->match( $param );
399 static function formatRaw( $num, $raw ) {
400 if( self
::isRaw( $raw ) ) {
404 return $wgContLang->formatNum( $num );
407 static function numberofpages( $parser, $raw = null ) {
408 return self
::formatRaw( SiteStats
::pages(), $raw );
410 static function numberofusers( $parser, $raw = null ) {
411 return self
::formatRaw( SiteStats
::users(), $raw );
413 static function numberofactiveusers( $parser, $raw = null ) {
414 return self
::formatRaw( SiteStats
::activeUsers(), $raw );
416 static function numberofarticles( $parser, $raw = null ) {
417 return self
::formatRaw( SiteStats
::articles(), $raw );
419 static function numberoffiles( $parser, $raw = null ) {
420 return self
::formatRaw( SiteStats
::images(), $raw );
422 static function numberofadmins( $parser, $raw = null ) {
423 return self
::formatRaw( SiteStats
::numberingroup('sysop'), $raw );
425 static function numberofedits( $parser, $raw = null ) {
426 return self
::formatRaw( SiteStats
::edits(), $raw );
428 static function numberofviews( $parser, $raw = null ) {
429 return self
::formatRaw( SiteStats
::views(), $raw );
431 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
432 return self
::formatRaw( SiteStats
::pagesInNs( intval( $namespace ) ), $raw );
434 static function numberingroup( $parser, $name = '', $raw = null) {
435 return self
::formatRaw( SiteStats
::numberingroup( strtolower( $name ) ), $raw );
440 * Given a title, return the namespace name that would be given by the
441 * corresponding magic word
442 * Note: function name changed to "mwnamespace" rather than "namespace"
443 * to not break PHP 5.3
444 * @return mixed|string
446 static function mwnamespace( $parser, $title = null ) {
447 $t = Title
::newFromText( $title );
450 return str_replace( '_', ' ', $t->getNsText() );
452 static function namespacee( $parser, $title = null ) {
453 $t = Title
::newFromText( $title );
456 return wfUrlencode( $t->getNsText() );
458 static function namespacenumber( $parser, $title = null ) {
459 $t = Title
::newFromText( $title );
462 return $t->getNamespace();
464 static function talkspace( $parser, $title = null ) {
465 $t = Title
::newFromText( $title );
466 if ( is_null( $t ) ||
!$t->canTalk() )
468 return str_replace( '_', ' ', $t->getTalkNsText() );
470 static function talkspacee( $parser, $title = null ) {
471 $t = Title
::newFromText( $title );
472 if ( is_null( $t ) ||
!$t->canTalk() )
474 return wfUrlencode( $t->getTalkNsText() );
476 static function subjectspace( $parser, $title = null ) {
477 $t = Title
::newFromText( $title );
480 return str_replace( '_', ' ', $t->getSubjectNsText() );
482 static function subjectspacee( $parser, $title = null ) {
483 $t = Title
::newFromText( $title );
486 return wfUrlencode( $t->getSubjectNsText() );
490 * Functions to get and normalize pagenames, corresponding to the magic words
494 static function pagename( $parser, $title = null ) {
495 $t = Title
::newFromText( $title );
498 return wfEscapeWikiText( $t->getText() );
500 static function pagenamee( $parser, $title = null ) {
501 $t = Title
::newFromText( $title );
504 return wfEscapeWikiText( $t->getPartialURL() );
506 static function fullpagename( $parser, $title = null ) {
507 $t = Title
::newFromText( $title );
508 if ( is_null( $t ) ||
!$t->canTalk() )
510 return wfEscapeWikiText( $t->getPrefixedText() );
512 static function fullpagenamee( $parser, $title = null ) {
513 $t = Title
::newFromText( $title );
514 if ( is_null( $t ) ||
!$t->canTalk() )
516 return wfEscapeWikiText( $t->getPrefixedURL() );
518 static function subpagename( $parser, $title = null ) {
519 $t = Title
::newFromText( $title );
522 return wfEscapeWikiText( $t->getSubpageText() );
524 static function subpagenamee( $parser, $title = null ) {
525 $t = Title
::newFromText( $title );
528 return wfEscapeWikiText( $t->getSubpageUrlForm() );
530 static function basepagename( $parser, $title = null ) {
531 $t = Title
::newFromText( $title );
534 return wfEscapeWikiText( $t->getBaseText() );
536 static function basepagenamee( $parser, $title = null ) {
537 $t = Title
::newFromText( $title );
540 return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) ) );
542 static function talkpagename( $parser, $title = null ) {
543 $t = Title
::newFromText( $title );
544 if ( is_null( $t ) ||
!$t->canTalk() )
546 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
548 static function talkpagenamee( $parser, $title = null ) {
549 $t = Title
::newFromText( $title );
550 if ( is_null( $t ) ||
!$t->canTalk() )
552 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedUrl() );
554 static function subjectpagename( $parser, $title = null ) {
555 $t = Title
::newFromText( $title );
558 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
560 static function subjectpagenamee( $parser, $title = null ) {
561 $t = Title
::newFromText( $title );
564 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedUrl() );
568 * Return the number of pages in the given category, or 0 if it's nonexis-
569 * tent. This is an expensive parser function and can't be called too many
573 static function pagesincategory( $parser, $name = '', $raw = null ) {
574 static $cache = array();
575 $category = Category
::newFromName( $name );
577 if( !is_object( $category ) ) {
579 return self
::formatRaw( 0, $raw );
582 # Normalize name for cache
583 $name = $category->getName();
586 if( isset( $cache[$name] ) ) {
587 $count = $cache[$name];
588 } elseif( $parser->incrementExpensiveFunctionCount() ) {
589 $count = $cache[$name] = (int)$category->getPageCount();
591 return self
::formatRaw( $count, $raw );
595 * Return the size of the given page, or 0 if it's nonexistent. This is an
596 * expensive parser function and can't be called too many times per page.
598 * @todo FIXME: This doesn't work correctly on preview for getting the size
599 * of the current page.
600 * @todo FIXME: Title::getLength() documentation claims that it adds things
601 * to the link cache, so the local cache here should be unnecessary, but
602 * in fact calling getLength() repeatedly for the same $page does seem to
603 * run one query for each call?
604 * @todo Document parameters
606 * @param $parser Parser
607 * @param $page String TODO DOCUMENT (Default: empty string)
608 * @param $raw TODO DOCUMENT (Default: null)
611 static function pagesize( $parser, $page = '', $raw = null ) {
612 static $cache = array();
613 $title = Title
::newFromText( $page );
615 if( !is_object( $title ) ) {
617 return self
::formatRaw( 0, $raw );
620 # Normalize name for cache
621 $page = $title->getPrefixedText();
624 if( isset( $cache[$page] ) ) {
625 $length = $cache[$page];
626 } elseif( $parser->incrementExpensiveFunctionCount() ) {
627 $rev = Revision
::newFromTitle( $title );
628 $id = $rev ?
$rev->getPage() : 0;
629 $length = $cache[$page] = $rev ?
$rev->getSize() : 0;
631 // Register dependency in templatelinks
632 $parser->mOutput
->addTemplate( $title, $id, $rev ?
$rev->getId() : 0 );
634 return self
::formatRaw( $length, $raw );
638 * Returns the requested protection level for the current page
641 static function protectionlevel( $parser, $type = '' ) {
642 $restrictions = $parser->mTitle
->getRestrictions( strtolower( $type ) );
643 # Title::getRestrictions returns an array, its possible it may have
644 # multiple values in the future
645 return implode( $restrictions, ',' );
649 * Gives language names.
650 * @param $parser Parser
651 * @param $code String Language code (of which to get name)
652 * @param $inLanguage String Language code (in which to get name)
655 static function language( $parser, $code = '', $inLanguage = '' ) {
656 $code = strtolower( $code );
657 $inLanguage = strtolower( $inLanguage );
658 $lang = Language
::fetchLanguageName( $code, $inLanguage );
659 return $lang !== '' ?
$lang : wfBCP47( $code );
663 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
666 static function pad( $parser, $string, $length, $padding = '0', $direction = STR_PAD_RIGHT
) {
667 $padding = $parser->killMarkers( $padding );
668 $lengthOfPadding = mb_strlen( $padding );
669 if ( $lengthOfPadding == 0 ) return $string;
671 # The remaining length to add counts down to 0 as padding is added
672 $length = min( $length, 500 ) - mb_strlen( $string );
673 # $finalPadding is just $padding repeated enough times so that
674 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
676 while ( $length > 0 ) {
677 # If $length < $lengthofPadding, truncate $padding so we get the
678 # exact length desired.
679 $finalPadding .= mb_substr( $padding, 0, $length );
680 $length -= $lengthOfPadding;
683 if ( $direction == STR_PAD_LEFT
) {
684 return $finalPadding . $string;
686 return $string . $finalPadding;
690 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
691 return self
::pad( $parser, $string, $length, $padding, STR_PAD_LEFT
);
694 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
695 return self
::pad( $parser, $string, $length, $padding );
699 * @param $parser Parser
703 static function anchorencode( $parser, $text ) {
704 $text = $parser->killMarkers( $text );
705 return substr( $parser->guessSectionNameFromWikiText( $text ), 1);
708 static function special( $parser, $text ) {
709 list( $page, $subpage ) = SpecialPageFactory
::resolveAlias( $text );
711 $title = SpecialPage
::getTitleFor( $page, $subpage );
712 return $title->getPrefixedText();
714 return wfMsgForContent( 'nosuchspecialpage' );
718 static function speciale( $parser, $text ) {
719 return wfUrlencode( str_replace( ' ', '_', self
::special( $parser, $text ) ) );
723 * @param $parser Parser
724 * @param $text String The sortkey to use
725 * @param $uarg String Either "noreplace" or "noerror" (in en)
726 * both suppress errors, and noreplace does nothing if
727 * a default sortkey already exists.
730 public static function defaultsort( $parser, $text, $uarg = '' ) {
731 static $magicWords = null;
732 if ( is_null( $magicWords ) ) {
733 $magicWords = new MagicWordArray( array( 'defaultsort_noerror', 'defaultsort_noreplace' ) );
735 $arg = $magicWords->matchStartToEnd( $uarg );
737 $text = trim( $text );
738 if( strlen( $text ) == 0 )
740 $old = $parser->getCustomDefaultSort();
741 if ( $old === false ||
$arg !== 'defaultsort_noreplace' ) {
742 $parser->setDefaultSort( $text );
745 if( $old === false ||
$old == $text ||
$arg ) {
748 return( '<span class="error">' .
749 wfMsgForContent( 'duplicate-defaultsort',
750 htmlspecialchars( $old ),
751 htmlspecialchars( $text ) ) .
756 // Usage {{filepath|300}}, {{filepath|nowiki}}, {{filepath|nowiki|300}} or {{filepath|300|nowiki}}
757 // or {{filepath|300px}}, {{filepath|200x300px}}, {{filepath|nowiki|200x300px}}, {{filepath|200x300px|nowiki}}
758 public static function filepath( $parser, $name='', $argA='', $argB='' ) {
759 $file = wfFindFile( $name );
762 if( $argA == 'nowiki' ) {
763 // {{filepath: | option [| size] }}
765 $parsedWidthParam = $parser->parseWidthParam( $argB );
767 // {{filepath: [| size [|option]] }}
768 $parsedWidthParam = $parser->parseWidthParam( $argA );
769 $isNowiki = ($argB == 'nowiki');
773 $url = $file->getFullUrl();
775 // If a size is requested...
776 if ( count( $parsedWidthParam ) ) {
777 $mto = $file->transform( $parsedWidthParam );
779 if ( $mto && !$mto->isError() ) {
780 // ... change the URL to point to a thumbnail.
781 $url = wfExpandUrl( $mto->getUrl(), PROTO_RELATIVE
);
785 return array( $url, 'nowiki' => true );
794 * Parser function to extension tag adaptor
797 public static function tagObj( $parser, $frame, $args ) {
798 if ( !count( $args ) ) {
801 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
803 if ( count( $args ) ) {
804 $inner = $frame->expand( array_shift( $args ) );
809 $stripList = $parser->getStripList();
810 if ( !in_array( $tagName, $stripList ) ) {
811 return '<span class="error">' .
812 wfMsgForContent( 'unknown_extension_tag', $tagName ) .
816 $attributes = array();
817 foreach ( $args as $arg ) {
818 $bits = $arg->splitArg();
819 if ( strval( $bits['index'] ) === '' ) {
820 $name = trim( $frame->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
821 $value = trim( $frame->expand( $bits['value'] ) );
822 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
823 $value = isset( $m[1] ) ?
$m[1] : '';
825 $attributes[$name] = $value;
832 'attributes' => $attributes,
833 'close' => "</$tagName>",
835 return $parser->extensionSubstitution( $params, $frame );