Fully deprecate $wgProxyKey. Has been marked as deprecated since 1.4, but never seems...
[lhc/web/wiklou.git] / includes / parser / CoreParserFunctions.php
1 <?php
2
3 /**
4 * Various core parser functions, registered in Parser::firstCallInit()
5 * @ingroup 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( 'numberingroup', array( __CLASS__, 'numberingroup' ), SFH_NO_HASH );
37 $parser->setFunctionHook( 'numberofedits', array( __CLASS__, 'numberofedits' ), SFH_NO_HASH );
38 $parser->setFunctionHook( 'language', array( __CLASS__, 'language' ), SFH_NO_HASH );
39 $parser->setFunctionHook( 'padleft', array( __CLASS__, 'padleft' ), SFH_NO_HASH );
40 $parser->setFunctionHook( 'padright', array( __CLASS__, 'padright' ), SFH_NO_HASH );
41 $parser->setFunctionHook( 'anchorencode', array( __CLASS__, 'anchorencode' ), SFH_NO_HASH );
42 $parser->setFunctionHook( 'special', array( __CLASS__, 'special' ) );
43 $parser->setFunctionHook( 'defaultsort', array( __CLASS__, 'defaultsort' ), SFH_NO_HASH );
44 $parser->setFunctionHook( 'filepath', array( __CLASS__, 'filepath' ), SFH_NO_HASH );
45 $parser->setFunctionHook( 'pagesincategory', array( __CLASS__, 'pagesincategory' ), SFH_NO_HASH );
46 $parser->setFunctionHook( 'pagesize', array( __CLASS__, 'pagesize' ), SFH_NO_HASH );
47 $parser->setFunctionHook( 'tag', array( __CLASS__, 'tagObj' ), SFH_OBJECT_ARGS );
48
49 if ( $wgAllowDisplayTitle ) {
50 $parser->setFunctionHook( 'displaytitle', array( __CLASS__, 'displaytitle' ), SFH_NO_HASH );
51 }
52 if ( $wgAllowSlowParserFunctions ) {
53 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__, 'pagesinnamespace' ), SFH_NO_HASH );
54 }
55 }
56
57 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
58 if ( strval( $part1 ) !== '' ) {
59 $args = array_slice( func_get_args(), 2 );
60 $message = wfMsgGetKey( $part1, true, false, false );
61 $message = wfMsgReplaceArgs( $message, $args );
62 $message = $parser->replaceVariables( $message ); // like $wgMessageCache->transform()
63 return $message;
64 } else {
65 return array( 'found' => false );
66 }
67 }
68
69 static function ns( $parser, $part1 = '' ) {
70 global $wgContLang;
71 $found = false;
72 if ( intval( $part1 ) || $part1 == "0" ) {
73 $text = $wgContLang->getNsText( intval( $part1 ) );
74 $found = true;
75 } else {
76 $param = str_replace( ' ', '_', strtolower( $part1 ) );
77 $index = MWNamespace::getCanonicalIndex( strtolower( $param ) );
78 if ( !is_null( $index ) ) {
79 $text = $wgContLang->getNsText( $index );
80 $found = true;
81 }
82 }
83 if ( $found ) {
84 return $text;
85 } else {
86 return array( 'found' => false );
87 }
88 }
89
90 static function urlencode( $parser, $s = '' ) {
91 return urlencode( $s );
92 }
93
94 static function lcfirst( $parser, $s = '' ) {
95 global $wgContLang;
96 return $wgContLang->lcfirst( $s );
97 }
98
99 static function ucfirst( $parser, $s = '' ) {
100 global $wgContLang;
101 return $wgContLang->ucfirst( $s );
102 }
103
104 static function lc( $parser, $s = '' ) {
105 global $wgContLang;
106 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
107 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
108 } else {
109 return $wgContLang->lc( $s );
110 }
111 }
112
113 static function uc( $parser, $s = '' ) {
114 global $wgContLang;
115 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
116 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
117 } else {
118 return $wgContLang->uc( $s );
119 }
120 }
121
122 static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
123 static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
124 static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
125 static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
126
127 static function urlFunction( $func, $s = '', $arg = null ) {
128 $title = Title::newFromText( $s );
129 # Due to order of execution of a lot of bits, the values might be encoded
130 # before arriving here; if that's true, then the title can't be created
131 # and the variable will fail. If we can't get a decent title from the first
132 # attempt, url-decode and try for a second.
133 if( is_null( $title ) )
134 $title = Title::newFromUrl( urldecode( $s ) );
135 if ( !is_null( $title ) ) {
136 if ( !is_null( $arg ) ) {
137 $text = $title->$func( $arg );
138 } else {
139 $text = $title->$func();
140 }
141 return $text;
142 } else {
143 return array( 'found' => false );
144 }
145 }
146
147 static function formatNum( $parser, $num = '', $raw = null) {
148 if ( self::israw( $raw ) ) {
149 return $parser->getFunctionLang()->parseFormattedNumber( $num );
150 } else {
151 return $parser->getFunctionLang()->formatNum( $num );
152 }
153 }
154
155 static function grammar( $parser, $case = '', $word = '' ) {
156 return $parser->getFunctionLang()->convertGrammar( $word, $case );
157 }
158
159 static function plural( $parser, $text = '') {
160 $forms = array_slice( func_get_args(), 2);
161 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
162 return $parser->getFunctionLang()->convertPlural( $text, $forms );
163 }
164
165 /**
166 * Override the title of the page when viewed, provided we've been given a
167 * title which will normalise to the canonical title
168 *
169 * @param Parser $parser Parent parser
170 * @param string $text Desired title text
171 * @return string
172 */
173 static function displaytitle( $parser, $text = '' ) {
174 global $wgRestrictDisplayTitle;
175 $text = trim( Sanitizer::decodeCharReferences( $text ) );
176
177 if ( !$wgRestrictDisplayTitle ) {
178 $parser->mOutput->setDisplayTitle( $text );
179 } else {
180 $title = Title::newFromText( $text );
181 if( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) )
182 $parser->mOutput->setDisplayTitle( $text );
183 }
184 return '';
185 }
186
187 static function isRaw( $param ) {
188 static $mwRaw;
189 if ( !$mwRaw ) {
190 $mwRaw =& MagicWord::get( 'rawsuffix' );
191 }
192 if ( is_null( $param ) ) {
193 return false;
194 } else {
195 return $mwRaw->match( $param );
196 }
197 }
198
199 static function formatRaw( $num, $raw ) {
200 if( self::isRaw( $raw ) ) {
201 return $num;
202 } else {
203 global $wgContLang;
204 return $wgContLang->formatNum( $num );
205 }
206 }
207 static function numberofpages( $parser, $raw = null ) {
208 return self::formatRaw( SiteStats::pages(), $raw );
209 }
210 static function numberofusers( $parser, $raw = null ) {
211 return self::formatRaw( SiteStats::users(), $raw );
212 }
213 static function numberofarticles( $parser, $raw = null ) {
214 return self::formatRaw( SiteStats::articles(), $raw );
215 }
216 static function numberoffiles( $parser, $raw = null ) {
217 return self::formatRaw( SiteStats::images(), $raw );
218 }
219 static function numberofadmins( $parser, $raw = null ) {
220 return self::formatRaw( SiteStats::numberingroup('sysop'), $raw );
221 }
222 static function numberofedits( $parser, $raw = null ) {
223 return self::formatRaw( SiteStats::edits(), $raw );
224 }
225 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
226 return self::formatRaw( SiteStats::pagesInNs( intval( $namespace ) ), $raw );
227 }
228 static function numberingroup( $parser, $name = '', $raw = null) {
229 return self::formatRaw( SiteStats::numberingroup( strtolower( $name ) ), $raw );
230 }
231
232 /**
233 * Return the number of pages in the given category, or 0 if it's nonexis-
234 * tent. This is an expensive parser function and can't be called too many
235 * times per page.
236 */
237 static function pagesincategory( $parser, $name = '', $raw = null ) {
238 static $cache = array();
239 $category = Category::newFromName( $name );
240
241 if( !is_object( $category ) ) {
242 $cache[$name] = 0;
243 return self::formatRaw( 0, $raw );
244 }
245
246 # Normalize name for cache
247 $name = $category->getName();
248
249 $count = 0;
250 if( isset( $cache[$name] ) ) {
251 $count = $cache[$name];
252 } elseif( $parser->incrementExpensiveFunctionCount() ) {
253 $count = $cache[$name] = (int)$category->getPageCount();
254 }
255 return self::formatRaw( $count, $raw );
256 }
257
258 /**
259 * Return the size of the given page, or 0 if it's nonexistent. This is an
260 * expensive parser function and can't be called too many times per page.
261 *
262 * @FIXME This doesn't work correctly on preview for getting the size of
263 * the current page.
264 * @FIXME Title::getLength() documentation claims that it adds things to
265 * the link cache, so the local cache here should be unnecessary, but in
266 * fact calling getLength() repeatedly for the same $page does seem to
267 * run one query for each call?
268 */
269 static function pagesize( $parser, $page = '', $raw = null ) {
270 static $cache = array();
271 $title = Title::newFromText($page);
272
273 if( !is_object( $title ) ) {
274 $cache[$page] = 0;
275 return self::formatRaw( 0, $raw );
276 }
277
278 # Normalize name for cache
279 $page = $title->getPrefixedText();
280
281 $length = 0;
282 if( isset( $cache[$page] ) ) {
283 $length = $cache[$page];
284 } elseif( $parser->incrementExpensiveFunctionCount() ) {
285 $length = $cache[$page] = $title->getLength();
286
287 // Register dependency in templatelinks
288 $id = $title->getArticleId();
289 $revid = Revision::newFromTitle($title);
290 $parser->mOutput->addTemplate($title, $id, $revid);
291 }
292 return self::formatRaw( $length, $raw );
293 }
294
295 static function language( $parser, $arg = '' ) {
296 global $wgContLang;
297 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
298 return $lang != '' ? $lang : $arg;
299 }
300
301 static function pad( $string = '', $length = 0, $char = 0, $direction = STR_PAD_RIGHT ) {
302 $length = min( max( $length, 0 ), 500 );
303 $char = substr( $char, 0, 1 );
304 return ( $string !== '' && (int)$length > 0 && strlen( trim( (string)$char ) ) > 0 )
305 ? str_pad( $string, $length, (string)$char, $direction )
306 : $string;
307 }
308
309 static function padleft( $parser, $string = '', $length = 0, $char = 0 ) {
310 return self::pad( $string, $length, $char, STR_PAD_LEFT );
311 }
312
313 static function padright( $parser, $string = '', $length = 0, $char = 0 ) {
314 return self::pad( $string, $length, $char );
315 }
316
317 static function anchorencode( $parser, $text ) {
318 $a = urlencode( $text );
319 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
320 # leave colons alone, however
321 $a = str_replace( '.3A', ':', $a );
322 return $a;
323 }
324
325 static function special( $parser, $text ) {
326 $title = SpecialPage::getTitleForAlias( $text );
327 if ( $title ) {
328 return $title->getPrefixedText();
329 } else {
330 return wfMsgForContent( 'nosuchspecialpage' );
331 }
332 }
333
334 public static function defaultsort( $parser, $text ) {
335 $text = trim( $text );
336 if( strlen( $text ) > 0 )
337 $parser->setDefaultSort( $text );
338 return '';
339 }
340
341 public static function filepath( $parser, $name='', $option='' ) {
342 $file = wfFindFile( $name );
343 if( $file ) {
344 $url = $file->getFullUrl();
345 if( $option == 'nowiki' ) {
346 return "<nowiki>$url</nowiki>";
347 }
348 return $url;
349 } else {
350 return '';
351 }
352 }
353
354 /**
355 * Parser function to extension tag adaptor
356 */
357 public static function tagObj( $parser, $frame, $args ) {
358 $xpath = false;
359 if ( !count( $args ) ) {
360 return '';
361 }
362 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
363
364 if ( count( $args ) ) {
365 $inner = $frame->expand( array_shift( $args ) );
366 } else {
367 $inner = null;
368 }
369
370 $stripList = $parser->getStripList();
371 if ( !in_array( $tagName, $stripList ) ) {
372 return '<span class="error">' .
373 wfMsg( 'unknown_extension_tag', $tagName ) .
374 '</span>';
375 }
376
377 $attributes = array();
378 foreach ( $args as $arg ) {
379 $bits = $arg->splitArg();
380 if ( strval( $bits['index'] ) === '' ) {
381 $name = $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS );
382 $value = trim( $frame->expand( $bits['value'] ) );
383 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
384 $value = isset( $m[1] ) ? $m[1] : '';
385 }
386 $attributes[$name] = $value;
387 }
388 }
389
390 $params = array(
391 'name' => $tagName,
392 'inner' => $inner,
393 'attributes' => $attributes,
394 'close' => "</$tagName>",
395 );
396 return $parser->extensionSubstitution( $params, $frame );
397 }
398 }