Same for $this->user
[lhc/web/wiklou.git] / includes / CoreParserFunctions.php
1 <?php
2
3 /**
4 * Various core parser functions, registered in Parser::firstCallInit()
5 * @addtogroup Parser
6 */
7 class CoreParserFunctions {
8 static function register( $parser ) {
9 global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
10
11 # Syntax for arguments (see self::setFunctionHook):
12 # "name for lookup in localized magic words array",
13 # function callback,
14 # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}
15 # instead of {{#int:...}})
16
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( 'pagesincategory', array( __CLASS__, 'pagesincategory' ), SFH_NO_HASH );
45 $parser->setFunctionHook( 'tag', array( __CLASS__, 'tagObj' ), SFH_OBJECT_ARGS );
46
47 if ( $wgAllowDisplayTitle ) {
48 $parser->setFunctionHook( 'displaytitle', array( __CLASS__, 'displaytitle' ), SFH_NO_HASH );
49 }
50 if ( $wgAllowSlowParserFunctions ) {
51 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__, 'pagesinnamespace' ), SFH_NO_HASH );
52 }
53 }
54
55 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
56 if ( strval( $part1 ) !== '' ) {
57 $args = array_slice( func_get_args(), 2 );
58 return wfMsgReal( $part1, $args, true );
59 } else {
60 return array( 'found' => false );
61 }
62 }
63
64 static function ns( $parser, $part1 = '' ) {
65 global $wgContLang;
66 $found = false;
67 if ( intval( $part1 ) || $part1 == "0" ) {
68 $text = $wgContLang->getNsText( intval( $part1 ) );
69 $found = true;
70 } else {
71 $param = str_replace( ' ', '_', strtolower( $part1 ) );
72 $index = MWNamespace::getCanonicalIndex( strtolower( $param ) );
73 if ( !is_null( $index ) ) {
74 $text = $wgContLang->getNsText( $index );
75 $found = true;
76 }
77 }
78 if ( $found ) {
79 return $text;
80 } else {
81 return array( 'found' => false );
82 }
83 }
84
85 static function urlencode( $parser, $s = '' ) {
86 return urlencode( $s );
87 }
88
89 static function lcfirst( $parser, $s = '' ) {
90 global $wgContLang;
91 return $wgContLang->lcfirst( $s );
92 }
93
94 static function ucfirst( $parser, $s = '' ) {
95 global $wgContLang;
96 return $wgContLang->ucfirst( $s );
97 }
98
99 static function lc( $parser, $s = '' ) {
100 global $wgContLang;
101 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
102 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
103 } else {
104 return $wgContLang->lc( $s );
105 }
106 }
107
108 static function uc( $parser, $s = '' ) {
109 global $wgContLang;
110 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
111 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
112 } else {
113 return $wgContLang->uc( $s );
114 }
115 }
116
117 static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
118 static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
119 static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
120 static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
121
122 static function urlFunction( $func, $s = '', $arg = null ) {
123 $title = Title::newFromText( $s );
124 # Due to order of execution of a lot of bits, the values might be encoded
125 # before arriving here; if that's true, then the title can't be created
126 # and the variable will fail. If we can't get a decent title from the first
127 # attempt, url-decode and try for a second.
128 if( is_null( $title ) )
129 $title = Title::newFromUrl( urldecode( $s ) );
130 if ( !is_null( $title ) ) {
131 if ( !is_null( $arg ) ) {
132 $text = $title->$func( $arg );
133 } else {
134 $text = $title->$func();
135 }
136 return $text;
137 } else {
138 return array( 'found' => false );
139 }
140 }
141
142 static function formatNum( $parser, $num = '', $raw = null) {
143 if ( self::israw( $raw ) ) {
144 return $parser->getFunctionLang()->parseFormattedNumber( $num );
145 } else {
146 return $parser->getFunctionLang()->formatNum( $num );
147 }
148 }
149
150 static function grammar( $parser, $case = '', $word = '' ) {
151 return $parser->getFunctionLang()->convertGrammar( $word, $case );
152 }
153
154 static function plural( $parser, $text = '') {
155 $forms = array_slice( func_get_args(), 2);
156 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
157 return $parser->getFunctionLang()->convertPlural( $text, $forms );
158 }
159
160 /**
161 * Override the title of the page when viewed,
162 * provided we've been given a title which
163 * will normalise to the canonical title
164 *
165 * @param Parser $parser Parent parser
166 * @param string $text Desired title text
167 * @return string
168 */
169 static function displaytitle( $parser, $text = '' ) {
170 $text = trim( Sanitizer::decodeCharReferences( $text ) );
171 $title = Title::newFromText( $text );
172 if( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) )
173 $parser->mOutput->setDisplayTitle( $text );
174 return '';
175 }
176
177 static function isRaw( $param ) {
178 static $mwRaw;
179 if ( !$mwRaw ) {
180 $mwRaw =& MagicWord::get( 'rawsuffix' );
181 }
182 if ( is_null( $param ) ) {
183 return false;
184 } else {
185 return $mwRaw->match( $param );
186 }
187 }
188
189 static function formatRaw( $num, $raw ) {
190 if( self::isRaw( $raw ) ) {
191 return $num;
192 } else {
193 global $wgContLang;
194 return $wgContLang->formatNum( $num );
195 }
196 }
197 static function numberofpages( $parser, $raw = null ) {
198 return self::formatRaw( SiteStats::pages(), $raw );
199 }
200 static function numberofusers( $parser, $raw = null ) {
201 return self::formatRaw( SiteStats::users(), $raw );
202 }
203 static function numberofarticles( $parser, $raw = null ) {
204 return self::formatRaw( SiteStats::articles(), $raw );
205 }
206 static function numberoffiles( $parser, $raw = null ) {
207 return self::formatRaw( SiteStats::images(), $raw );
208 }
209 static function numberofadmins( $parser, $raw = null ) {
210 return self::formatRaw( SiteStats::admins(), $raw );
211 }
212 static function numberofedits( $parser, $raw = null ) {
213 return self::formatRaw( SiteStats::edits(), $raw );
214 }
215 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
216 return self::formatRaw( SiteStats::pagesInNs( intval( $namespace ) ), $raw );
217 }
218
219 static function pagesincategory( $parser, $category = '', $raw = null ) {
220 global $wgExpensiveParserFunctionLimit;
221 if ($category == '') {
222 return 0;
223 }
224 $parser->mExpensiveFunctionCount++;
225 if ($parser->mExpensiveFunctionCount <= $wgExpensiveParserFunctionLimit) {
226 $category = Category::newFromName($category);
227 $count = $category->getPageCount();
228 if ( !$count ) {
229 $count = 0;
230 }
231 return self::formatRaw( $count, $raw );
232 }
233 return 0;
234 }
235
236 static function language( $parser, $arg = '' ) {
237 global $wgContLang;
238 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
239 return $lang != '' ? $lang : $arg;
240 }
241
242 static function pad( $string = '', $length = 0, $char = 0, $direction = STR_PAD_RIGHT ) {
243 $length = min( max( $length, 0 ), 500 );
244 $char = substr( $char, 0, 1 );
245 return ( $string !== '' && (int)$length > 0 && strlen( trim( (string)$char ) ) > 0 )
246 ? str_pad( $string, $length, (string)$char, $direction )
247 : $string;
248 }
249
250 static function padleft( $parser, $string = '', $length = 0, $char = 0 ) {
251 return self::pad( $string, $length, $char, STR_PAD_LEFT );
252 }
253
254 static function padright( $parser, $string = '', $length = 0, $char = 0 ) {
255 return self::pad( $string, $length, $char );
256 }
257
258 static function anchorencode( $parser, $text ) {
259 $a = urlencode( $text );
260 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
261 # leave colons alone, however
262 $a = str_replace( '.3A', ':', $a );
263 return $a;
264 }
265
266 static function special( $parser, $text ) {
267 $title = SpecialPage::getTitleForAlias( $text );
268 if ( $title ) {
269 return $title->getPrefixedText();
270 } else {
271 return wfMsgForContent( 'nosuchspecialpage' );
272 }
273 }
274
275 public static function defaultsort( $parser, $text ) {
276 $text = trim( $text );
277 if( strlen( $text ) > 0 )
278 $parser->setDefaultSort( $text );
279 return '';
280 }
281
282 public static function filepath( $parser, $name='', $option='' ) {
283 $file = wfFindFile( $name );
284 if( $file ) {
285 $url = $file->getFullUrl();
286 if( $option == 'nowiki' ) {
287 return "<nowiki>$url</nowiki>";
288 }
289 return $url;
290 } else {
291 return '';
292 }
293 }
294
295 /**
296 * Parser function to extension tag adaptor
297 */
298 public static function tagObj( $parser, $frame, $args ) {
299 $xpath = false;
300 if ( !count( $args ) ) {
301 return '';
302 }
303 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
304
305 if ( count( $args ) ) {
306 $inner = $frame->expand( array_shift( $args ) );
307 } else {
308 $inner = null;
309 }
310
311 $stripList = $parser->getStripList();
312 if ( !in_array( $tagName, $stripList ) ) {
313 return '<span class="error">' .
314 wfMsg( 'unknown_extension_tag', $tagName ) .
315 '</span>';
316 }
317
318 $attributes = array();
319 foreach ( $args as $arg ) {
320 $bits = $arg->splitArg();
321 if ( strval( $bits['index'] ) === '' ) {
322 $name = $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS );
323 $value = trim( $frame->expand( $bits['value'] ) );
324 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
325 $value = isset( $m[1] ) ? $m[1] : '';
326 }
327 $attributes[$name] = $value;
328 }
329 }
330
331 $params = array(
332 'name' => $tagName,
333 'inner' => $inner,
334 'attributes' => $attributes,
335 'close' => "</$tagName>",
336 );
337 return $parser->extensionSubstitution( $params, $frame );
338 }
339 }
340