4 * Various core parser functions, registered in Parser::firstCallInit()
7 class CoreParserFunctions
{
8 static function register( $parser ) {
9 global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
11 # Syntax for arguments (see self::setFunctionHook):
12 # "name for lookup in localized magic words array",
14 # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}
15 # instead of {{#int:...}})
17 $parser->setFunctionHook( 'int', array( __CLASS__
, 'intFunction' ), SFH_NO_HASH
);
18 $parser->setFunctionHook( 'ns', array( __CLASS__
, 'ns' ), SFH_NO_HASH
);
19 $parser->setFunctionHook( 'urlencode', array( __CLASS__
, 'urlencode' ), SFH_NO_HASH
);
20 $parser->setFunctionHook( 'lcfirst', array( __CLASS__
, 'lcfirst' ), SFH_NO_HASH
);
21 $parser->setFunctionHook( 'ucfirst', array( __CLASS__
, 'ucfirst' ), SFH_NO_HASH
);
22 $parser->setFunctionHook( 'lc', array( __CLASS__
, 'lc' ), SFH_NO_HASH
);
23 $parser->setFunctionHook( 'uc', array( __CLASS__
, 'uc' ), SFH_NO_HASH
);
24 $parser->setFunctionHook( 'localurl', array( __CLASS__
, 'localurl' ), SFH_NO_HASH
);
25 $parser->setFunctionHook( 'localurle', array( __CLASS__
, 'localurle' ), SFH_NO_HASH
);
26 $parser->setFunctionHook( 'fullurl', array( __CLASS__
, 'fullurl' ), SFH_NO_HASH
);
27 $parser->setFunctionHook( 'fullurle', array( __CLASS__
, 'fullurle' ), SFH_NO_HASH
);
28 $parser->setFunctionHook( 'formatnum', array( __CLASS__
, 'formatnum' ), SFH_NO_HASH
);
29 $parser->setFunctionHook( 'grammar', array( __CLASS__
, 'grammar' ), SFH_NO_HASH
);
30 $parser->setFunctionHook( 'plural', array( __CLASS__
, 'plural' ), SFH_NO_HASH
);
31 $parser->setFunctionHook( 'numberofpages', array( __CLASS__
, 'numberofpages' ), SFH_NO_HASH
);
32 $parser->setFunctionHook( 'numberofusers', array( __CLASS__
, 'numberofusers' ), SFH_NO_HASH
);
33 $parser->setFunctionHook( 'numberofarticles', array( __CLASS__
, 'numberofarticles' ), SFH_NO_HASH
);
34 $parser->setFunctionHook( 'numberoffiles', array( __CLASS__
, 'numberoffiles' ), SFH_NO_HASH
);
35 $parser->setFunctionHook( 'numberofadmins', array( __CLASS__
, 'numberofadmins' ), SFH_NO_HASH
);
36 $parser->setFunctionHook( 'numberofedits', array( __CLASS__
, 'numberofedits' ), SFH_NO_HASH
);
37 $parser->setFunctionHook( 'language', array( __CLASS__
, 'language' ), SFH_NO_HASH
);
38 $parser->setFunctionHook( 'padleft', array( __CLASS__
, 'padleft' ), SFH_NO_HASH
);
39 $parser->setFunctionHook( 'padright', array( __CLASS__
, 'padright' ), SFH_NO_HASH
);
40 $parser->setFunctionHook( 'anchorencode', array( __CLASS__
, 'anchorencode' ), SFH_NO_HASH
);
41 $parser->setFunctionHook( 'special', array( __CLASS__
, 'special' ) );
42 $parser->setFunctionHook( 'defaultsort', array( __CLASS__
, 'defaultsort' ), SFH_NO_HASH
);
43 $parser->setFunctionHook( 'filepath', array( __CLASS__
, 'filepath' ), SFH_NO_HASH
);
44 $parser->setFunctionHook( 'tag', array( __CLASS__
, 'tagObj' ), SFH_OBJECT_ARGS
);
46 if ( $wgAllowDisplayTitle ) {
47 $parser->setFunctionHook( 'displaytitle', array( __CLASS__
, 'displaytitle' ), SFH_NO_HASH
);
49 if ( $wgAllowSlowParserFunctions ) {
50 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__
, 'pagesinnamespace' ), SFH_NO_HASH
);
54 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
55 if ( strval( $part1 ) !== '' ) {
56 $args = array_slice( func_get_args(), 2 );
57 return wfMsgReal( $part1, $args, true );
59 return array( 'found' => false );
63 static function ns( $parser, $part1 = '' ) {
66 if ( intval( $part1 ) ||
$part1 == "0" ) {
67 $text = $wgContLang->getNsText( intval( $part1 ) );
70 $param = str_replace( ' ', '_', strtolower( $part1 ) );
71 $index = Namespace::getCanonicalIndex( strtolower( $param ) );
72 if ( !is_null( $index ) ) {
73 $text = $wgContLang->getNsText( $index );
80 return array( 'found' => false );
84 static function urlencode( $parser, $s = '' ) {
85 return urlencode( $s );
88 static function lcfirst( $parser, $s = '' ) {
90 return $wgContLang->lcfirst( $s );
93 static function ucfirst( $parser, $s = '' ) {
95 return $wgContLang->ucfirst( $s );
98 static function lc( $parser, $s = '' ) {
100 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
101 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
103 return $wgContLang->lc( $s );
107 static function uc( $parser, $s = '' ) {
109 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
110 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
112 return $wgContLang->uc( $s );
116 static function localurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getLocalURL', $s, $arg ); }
117 static function localurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeLocalURL', $s, $arg ); }
118 static function fullurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getFullURL', $s, $arg ); }
119 static function fullurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeFullURL', $s, $arg ); }
121 static function urlFunction( $func, $s = '', $arg = null ) {
122 $title = Title
::newFromText( $s );
123 # Due to order of execution of a lot of bits, the values might be encoded
124 # before arriving here; if that's true, then the title can't be created
125 # and the variable will fail. If we can't get a decent title from the first
126 # attempt, url-decode and try for a second.
127 if( is_null( $title ) )
128 $title = Title
::newFromUrl( urldecode( $s ) );
129 if ( !is_null( $title ) ) {
130 if ( !is_null( $arg ) ) {
131 $text = $title->$func( $arg );
133 $text = $title->$func();
137 return array( 'found' => false );
141 static function formatNum( $parser, $num = '' ) {
142 return $parser->getFunctionLang()->formatNum( $num );
145 static function grammar( $parser, $case = '', $word = '' ) {
146 return $parser->getFunctionLang()->convertGrammar( $word, $case );
149 static function plural( $parser, $text = '') {
150 $forms = array_slice( func_get_args(), 2);
151 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
152 return $parser->getFunctionLang()->convertPlural( $text, $forms );
156 * Override the title of the page when viewed,
157 * provided we've been given a title which
158 * will normalise to the canonical title
160 * @param Parser $parser Parent parser
161 * @param string $text Desired title text
164 static function displaytitle( $parser, $text = '' ) {
165 $text = trim( Sanitizer
::decodeCharReferences( $text ) );
166 $title = Title
::newFromText( $text );
167 if( $title instanceof Title
&& $title->getFragment() == '' && $title->equals( $parser->mTitle
) )
168 $parser->mOutput
->setDisplayTitle( $text );
172 static function isRaw( $param ) {
175 $mwRaw =& MagicWord
::get( 'rawsuffix' );
177 if ( is_null( $param ) ) {
180 return $mwRaw->match( $param );
184 static function statisticsFunction( $func, $raw = null ) {
185 if ( self
::isRaw( $raw ) ) {
186 return call_user_func( array( 'SiteStats', $func ) );
189 return $wgContLang->formatNum( call_user_func( array( 'SiteStats', $func ) ) );
193 static function numberofpages( $parser, $raw = null ) { return self
::statisticsFunction( 'pages', $raw ); }
194 static function numberofusers( $parser, $raw = null ) { return self
::statisticsFunction( 'users', $raw ); }
195 static function numberofarticles( $parser, $raw = null ) { return self
::statisticsFunction( 'articles', $raw ); }
196 static function numberoffiles( $parser, $raw = null ) { return self
::statisticsFunction( 'images', $raw ); }
197 static function numberofadmins( $parser, $raw = null ) { return self
::statisticsFunction( 'admins', $raw ); }
198 static function numberofedits( $parser, $raw = null ) { return self
::statisticsFunction( 'edits', $raw ); }
200 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
201 $count = SiteStats
::pagesInNs( intval( $namespace ) );
202 if ( self
::isRaw( $raw ) ) {
204 return $wgContLang->formatNum( $count );
210 static function language( $parser, $arg = '' ) {
212 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
213 return $lang != '' ?
$lang : $arg;
216 static function pad( $string = '', $length = 0, $char = 0, $direction = STR_PAD_RIGHT
) {
217 $length = min( max( $length, 0 ), 500 );
218 $char = substr( $char, 0, 1 );
219 return ( $string !== '' && (int)$length > 0 && strlen( trim( (string)$char ) ) > 0 )
220 ?
str_pad( $string, $length, (string)$char, $direction )
224 static function padleft( $parser, $string = '', $length = 0, $char = 0 ) {
225 return self
::pad( $string, $length, $char, STR_PAD_LEFT
);
228 static function padright( $parser, $string = '', $length = 0, $char = 0 ) {
229 return self
::pad( $string, $length, $char );
232 static function anchorencode( $parser, $text ) {
233 $a = urlencode( $text );
234 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
235 # leave colons alone, however
236 $a = str_replace( '.3A', ':', $a );
240 static function special( $parser, $text ) {
241 $title = SpecialPage
::getTitleForAlias( $text );
243 return $title->getPrefixedText();
245 return wfMsgForContent( 'nosuchspecialpage' );
249 public static function defaultsort( $parser, $text ) {
250 $text = trim( $text );
251 if( strlen( $text ) > 0 )
252 $parser->setDefaultSort( $text );
256 public static function filepath( $parser, $name='', $option='' ) {
257 $file = wfFindFile( $name );
259 $url = $file->getFullUrl();
260 if( $option == 'nowiki' ) {
261 return "<nowiki>$url</nowiki>";
270 * Parser function to extension tag adaptor
272 public static function tagObj( $parser, $frame, $args ) {
274 if ( !count( $args ) ) {
277 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
279 if ( count( $args ) ) {
280 $inner = $frame->expand( array_shift( $args ) );
285 $stripList = $parser->getStripList();
286 if ( !in_array( $tagName, $stripList ) ) {
287 return '<span class="error">' .
288 wfMsg( 'unknown_extension_tag', $tagName ) .
292 $attributes = array();
293 foreach ( $args as $arg ) {
294 $bits = $arg->splitArg();
295 if ( strval( $bits['index'] ) === '' ) {
296 $name = $frame->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
);
297 $value = trim( $frame->expand( $bits['value'] ) );
298 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
299 $value = isset( $m[1] ) ?
$m[1] : '';
301 $attributes[$name] = $value;
308 'attributes' => $attributes,
309 'close' => "</$tagName>",
311 return $parser->extensionSubstitution( $params, $frame );