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