4 * Various core parser functions, registered in Parser::firstCallInit()
7 class CoreParserFunctions
{
8 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
9 if ( strval( $part1 ) !== '' ) {
10 $args = array_slice( func_get_args(), 2 );
11 return wfMsgReal( $part1, $args, true );
13 return array( 'found' => false );
17 static function ns( $parser, $part1 = '' ) {
20 if ( intval( $part1 ) ||
$part1 == "0" ) {
21 $text = $wgContLang->getNsText( intval( $part1 ) );
24 $param = str_replace( ' ', '_', strtolower( $part1 ) );
25 $index = Namespace::getCanonicalIndex( strtolower( $param ) );
26 if ( !is_null( $index ) ) {
27 $text = $wgContLang->getNsText( $index );
34 return array( 'found' => false );
38 static function urlencode( $parser, $s = '' ) {
39 return urlencode( $s );
42 static function lcfirst( $parser, $s = '' ) {
44 return $wgContLang->lcfirst( $s );
47 static function ucfirst( $parser, $s = '' ) {
49 return $wgContLang->ucfirst( $s );
52 static function lc( $parser, $s = '' ) {
54 return $wgContLang->lc( $s );
57 static function uc( $parser, $s = '' ) {
59 return $wgContLang->uc( $s );
62 static function localurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getLocalURL', $s, $arg ); }
63 static function localurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeLocalURL', $s, $arg ); }
64 static function fullurl( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'getFullURL', $s, $arg ); }
65 static function fullurle( $parser, $s = '', $arg = null ) { return self
::urlFunction( 'escapeFullURL', $s, $arg ); }
67 static function urlFunction( $func, $s = '', $arg = null ) {
68 $title = Title
::newFromText( $s );
69 # Due to order of execution of a lot of bits, the values might be encoded
70 # before arriving here; if that's true, then the title can't be created
71 # and the variable will fail. If we can't get a decent title from the first
72 # attempt, url-decode and try for a second.
73 if( is_null( $title ) )
74 $title = Title
::newFromUrl( urldecode( $s ) );
75 if ( !is_null( $title ) ) {
76 if ( !is_null( $arg ) ) {
77 $text = $title->$func( $arg );
79 $text = $title->$func();
83 return array( 'found' => false );
87 static function formatNum( $parser, $num = '' ) {
88 return $parser->getFunctionLang()->formatNum( $num );
91 static function grammar( $parser, $case = '', $word = '' ) {
92 return $parser->getFunctionLang()->convertGrammar( $word, $case );
95 static function plural( $parser, $text = '') {
96 $forms = array_slice( func_get_args(), 2);
97 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
98 return $parser->getFunctionLang()->convertPlural( $text, $forms );
102 * Override the title of the page when viewed,
103 * provided we've been given a title which
104 * will normalise to the canonical title
106 * @param Parser $parser Parent parser
107 * @param string $text Desired title text
110 static function displaytitle( $parser, $text = '' ) {
111 $text = trim( Sanitizer
::decodeCharReferences( $text ) );
112 $title = Title
::newFromText( $text );
113 if( $title instanceof Title
&& $title->getFragment() == '' && $title->equals( $parser->mTitle
) )
114 $parser->mOutput
->setDisplayTitle( $text );
118 static function isRaw( $param ) {
121 $mwRaw =& MagicWord
::get( 'rawsuffix' );
123 if ( is_null( $param ) ) {
126 return $mwRaw->match( $param );
130 static function statisticsFunction( $func, $raw = null ) {
131 if ( self
::isRaw( $raw ) ) {
132 return call_user_func( array( 'SiteStats', $func ) );
135 return $wgContLang->formatNum( call_user_func( array( 'SiteStats', $func ) ) );
139 static function numberofpages( $parser, $raw = null ) { return self
::statisticsFunction( 'pages', $raw ); }
140 static function numberofusers( $parser, $raw = null ) { return self
::statisticsFunction( 'users', $raw ); }
141 static function numberofarticles( $parser, $raw = null ) { return self
::statisticsFunction( 'articles', $raw ); }
142 static function numberoffiles( $parser, $raw = null ) { return self
::statisticsFunction( 'images', $raw ); }
143 static function numberofadmins( $parser, $raw = null ) { return self
::statisticsFunction( 'admins', $raw ); }
144 static function numberofedits( $parser, $raw = null ) { return self
::statisticsFunction( 'edits', $raw ); }
146 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
147 $count = SiteStats
::pagesInNs( intval( $namespace ) );
148 if ( self
::isRaw( $raw ) ) {
150 return $wgContLang->formatNum( $count );
156 static function language( $parser, $arg = '' ) {
158 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
159 return $lang != '' ?
$lang : $arg;
162 static function pad( $string = '', $length = 0, $char = 0, $direction = STR_PAD_RIGHT
) {
163 $length = min( max( $length, 0 ), 500 );
164 $char = substr( $char, 0, 1 );
165 return ( $string !== '' && (int)$length > 0 && strlen( trim( (string)$char ) ) > 0 )
166 ?
str_pad( $string, $length, (string)$char, $direction )
170 static function padleft( $parser, $string = '', $length = 0, $char = 0 ) {
171 return self
::pad( $string, $length, $char, STR_PAD_LEFT
);
174 static function padright( $parser, $string = '', $length = 0, $char = 0 ) {
175 return self
::pad( $string, $length, $char );
178 static function anchorencode( $parser, $text ) {
179 $a = urlencode( $text );
180 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
181 # leave colons alone, however
182 $a = str_replace( '.3A', ':', $a );
186 static function special( $parser, $text ) {
187 $title = SpecialPage
::getTitleForAlias( $text );
189 return $title->getPrefixedText();
191 return wfMsgForContent( 'nosuchspecialpage' );
195 public static function defaultsort( $parser, $text ) {
196 $text = trim( $text );
197 if( strlen( $text ) > 0 )
198 $parser->setDefaultSort( $text );
202 public static function filepath( $parser, $name='', $option='' ) {
203 $file = wfFindFile( $name );
205 $url = $file->getFullUrl();
206 if( $option == 'nowiki' ) {
207 return "<nowiki>$url</nowiki>";
216 * Parser function to extension tag adaptor
218 public static function tagObj( $parser, $frame, $args ) {
220 if ( !count( $args ) ) {
223 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
224 $stripList = $parser->getStripList();
225 if ( !in_array( $tagName, $stripList ) ) {
226 return '<span class="error">' .
227 wfMsg( 'unknown_extension_tag', $tagName ) .
231 $lastNumberedNode = false;
232 $attributes = array();
233 foreach ( $args as $arg ) {
235 $xpath = new DOMXPath( $arg->ownerDocument
);
237 $names = $xpath->query( 'name', $arg );
238 if ( $names->item( 0 )->hasAttributes() ) {
239 $lastNumberedNode = $arg;
241 $name = $frame->expand( $names->item( 0 ), PPFrame
::STRIP_COMMENTS
);
242 if ( preg_match( '/^\d+$/', $name ) ) {
243 // For = suppression syntax {{#tag|thing|1=2=3=4}}
244 $lastNumberedNode = $arg;
246 $values = $xpath->query( 'value', $arg );
247 $attributes[$name] = trim( $frame->expand( $values->item( 0 ) ) );
252 if ( !$lastNumberedNode ) {
255 $values = $xpath->query( 'value', $lastNumberedNode );
256 $inner = $frame->expand( $values->item( 0 ) );
261 'attributes' => $attributes
263 return $parser->extensionSubstitution( $params, $frame );