(bug 18317) Bolded selections in 1 | 3 | etc days on RecentChanges now use <strong...
[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( 'gender', array( __CLASS__, 'gender' ), SFH_NO_HASH );
31 $parser->setFunctionHook( 'plural', array( __CLASS__, 'plural' ), SFH_NO_HASH );
32 $parser->setFunctionHook( 'numberofpages', array( __CLASS__, 'numberofpages' ), SFH_NO_HASH );
33 $parser->setFunctionHook( 'numberofusers', array( __CLASS__, 'numberofusers' ), SFH_NO_HASH );
34 $parser->setFunctionHook( 'numberofactiveusers', array( __CLASS__, 'numberofactiveusers' ), SFH_NO_HASH );
35 $parser->setFunctionHook( 'numberofarticles', array( __CLASS__, 'numberofarticles' ), SFH_NO_HASH );
36 $parser->setFunctionHook( 'numberoffiles', array( __CLASS__, 'numberoffiles' ), SFH_NO_HASH );
37 $parser->setFunctionHook( 'numberofadmins', array( __CLASS__, 'numberofadmins' ), SFH_NO_HASH );
38 $parser->setFunctionHook( 'numberingroup', array( __CLASS__, 'numberingroup' ), SFH_NO_HASH );
39 $parser->setFunctionHook( 'numberofedits', array( __CLASS__, 'numberofedits' ), SFH_NO_HASH );
40 $parser->setFunctionHook( 'numberofviews', array( __CLASS__, 'numberofviews' ), SFH_NO_HASH );
41 $parser->setFunctionHook( 'language', array( __CLASS__, 'language' ), SFH_NO_HASH );
42 $parser->setFunctionHook( 'padleft', array( __CLASS__, 'padleft' ), SFH_NO_HASH );
43 $parser->setFunctionHook( 'padright', array( __CLASS__, 'padright' ), SFH_NO_HASH );
44 $parser->setFunctionHook( 'anchorencode', array( __CLASS__, 'anchorencode' ), SFH_NO_HASH );
45 $parser->setFunctionHook( 'special', array( __CLASS__, 'special' ) );
46 $parser->setFunctionHook( 'defaultsort', array( __CLASS__, 'defaultsort' ), SFH_NO_HASH );
47 $parser->setFunctionHook( 'filepath', array( __CLASS__, 'filepath' ), SFH_NO_HASH );
48 $parser->setFunctionHook( 'pagesincategory', array( __CLASS__, 'pagesincategory' ), SFH_NO_HASH );
49 $parser->setFunctionHook( 'pagesize', array( __CLASS__, 'pagesize' ), SFH_NO_HASH );
50 $parser->setFunctionHook( 'protectionlevel', array( __CLASS__, 'protectionlevel' ), SFH_NO_HASH );
51 $parser->setFunctionHook( 'namespace', array( __CLASS__, 'mwnamespace' ), SFH_NO_HASH );
52 $parser->setFunctionHook( 'namespacee', array( __CLASS__, 'namespacee' ), SFH_NO_HASH );
53 $parser->setFunctionHook( 'talkspace', array( __CLASS__, 'talkspace' ), SFH_NO_HASH );
54 $parser->setFunctionHook( 'talkspacee', array( __CLASS__, 'talkspacee' ), SFH_NO_HASH );
55 $parser->setFunctionHook( 'subjectspace', array( __CLASS__, 'subjectspace' ), SFH_NO_HASH );
56 $parser->setFunctionHook( 'subjectspacee', array( __CLASS__, 'subjectspacee' ), SFH_NO_HASH );
57 $parser->setFunctionHook( 'pagename', array( __CLASS__, 'pagename' ), SFH_NO_HASH );
58 $parser->setFunctionHook( 'pagenamee', array( __CLASS__, 'pagenamee' ), SFH_NO_HASH );
59 $parser->setFunctionHook( 'fullpagename', array( __CLASS__, 'fullpagename' ), SFH_NO_HASH );
60 $parser->setFunctionHook( 'fullpagenamee', array( __CLASS__, 'fullpagenamee' ), SFH_NO_HASH );
61 $parser->setFunctionHook( 'basepagename', array( __CLASS__, 'basepagename' ), SFH_NO_HASH );
62 $parser->setFunctionHook( 'basepagenamee', array( __CLASS__, 'basepagenamee' ), SFH_NO_HASH );
63 $parser->setFunctionHook( 'subpagename', array( __CLASS__, 'subpagename' ), SFH_NO_HASH );
64 $parser->setFunctionHook( 'subpagenamee', array( __CLASS__, 'subpagenamee' ), SFH_NO_HASH );
65 $parser->setFunctionHook( 'talkpagename', array( __CLASS__, 'talkpagename' ), SFH_NO_HASH );
66 $parser->setFunctionHook( 'talkpagenamee', array( __CLASS__, 'talkpagenamee' ), SFH_NO_HASH );
67 $parser->setFunctionHook( 'subjectpagename', array( __CLASS__, 'subjectpagename' ), SFH_NO_HASH );
68 $parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__, 'subjectpagenamee' ), SFH_NO_HASH );
69 $parser->setFunctionHook( 'tag', array( __CLASS__, 'tagObj' ), SFH_OBJECT_ARGS );
70 $parser->setFunctionHook( 'formatdate', array( __CLASS__, 'formatDate' ) );
71 $parser->setFunctionHook( 'groupconvert', array( __CLASS__, 'groupconvert' ), SFH_NO_HASH );
72
73 if ( $wgAllowDisplayTitle ) {
74 $parser->setFunctionHook( 'displaytitle', array( __CLASS__, 'displaytitle' ), SFH_NO_HASH );
75 }
76 if ( $wgAllowSlowParserFunctions ) {
77 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__, 'pagesinnamespace' ), SFH_NO_HASH );
78 }
79 }
80
81 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
82 if ( strval( $part1 ) !== '' ) {
83 $args = array_slice( func_get_args(), 2 );
84 $message = wfMsgGetKey( $part1, true, false, false );
85 $message = wfMsgReplaceArgs( $message, $args );
86 $message = $parser->replaceVariables( $message ); // like $wgMessageCache->transform()
87 return $message;
88 } else {
89 return array( 'found' => false );
90 }
91 }
92
93 static function formatDate( $parser, $date, $defaultPref = null ) {
94 $df = DateFormatter::getInstance();
95
96 $date = trim($date);
97
98 $pref = $parser->mOptions->getDateFormat();
99
100 // Specify a different default date format other than the the normal default
101 // iff the user has 'default' for their setting
102 if ($pref == 'default' && $defaultPref)
103 $pref = $defaultPref;
104
105 $date = $df->reformat( $pref, $date, array('match-whole') );
106 return $date;
107 }
108
109 static function ns( $parser, $part1 = '' ) {
110 global $wgContLang;
111 if ( intval( $part1 ) || $part1 == "0" ) {
112 $index = intval( $part1 );
113 } else {
114 $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
115 }
116 if ( $index !== false ) {
117 return $wgContLang->getFormattedNsText( $index );
118 } else {
119 return array( 'found' => false );
120 }
121 }
122
123 static function urlencode( $parser, $s = '' ) {
124 return urlencode( $s );
125 }
126
127 static function lcfirst( $parser, $s = '' ) {
128 global $wgContLang;
129 return $wgContLang->lcfirst( $s );
130 }
131
132 static function ucfirst( $parser, $s = '' ) {
133 global $wgContLang;
134 return $wgContLang->ucfirst( $s );
135 }
136
137 static function lc( $parser, $s = '' ) {
138 global $wgContLang;
139 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
140 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
141 } else {
142 return $wgContLang->lc( $s );
143 }
144 }
145
146 static function uc( $parser, $s = '' ) {
147 global $wgContLang;
148 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
149 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
150 } else {
151 return $wgContLang->uc( $s );
152 }
153 }
154
155 static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
156 static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
157 static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
158 static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
159
160 static function urlFunction( $func, $s = '', $arg = null ) {
161 $title = Title::newFromText( $s );
162 # Due to order of execution of a lot of bits, the values might be encoded
163 # before arriving here; if that's true, then the title can't be created
164 # and the variable will fail. If we can't get a decent title from the first
165 # attempt, url-decode and try for a second.
166 if( is_null( $title ) )
167 $title = Title::newFromUrl( urldecode( $s ) );
168 if( !is_null( $title ) ) {
169 # Convert NS_MEDIA -> NS_FILE
170 if( $title->getNamespace() == NS_MEDIA ) {
171 $title = Title::makeTitle( NS_FILE, $title->getDBKey() );
172 }
173 if( !is_null( $arg ) ) {
174 $text = $title->$func( $arg );
175 } else {
176 $text = $title->$func();
177 }
178 return $text;
179 } else {
180 return array( 'found' => false );
181 }
182 }
183
184 static function formatNum( $parser, $num = '', $raw = null) {
185 if ( self::israw( $raw ) ) {
186 return $parser->getFunctionLang()->parseFormattedNumber( $num );
187 } else {
188 return $parser->getFunctionLang()->formatNum( $num );
189 }
190 }
191
192 static function grammar( $parser, $case = '', $word = '' ) {
193 return $parser->getFunctionLang()->convertGrammar( $word, $case );
194 }
195
196 static function gender( $parser, $user ) {
197 $forms = array_slice( func_get_args(), 2);
198
199 // default
200 $gender = User::getDefaultOption( 'gender' );
201
202 // allow prefix.
203 $title = Title::newFromText( $user );
204
205 if (is_object( $title ) && $title->getNamespace() == NS_USER)
206 $user = $title->getText();
207
208 // check parameter, or use $wgUser if in interface message
209 $user = User::newFromName( $user );
210 if ( $user ) {
211 $gender = $user->getOption( 'gender' );
212 } elseif ( $parser->mOptions->getInterfaceMessage() ) {
213 global $wgUser;
214 $gender = $wgUser->getOption( 'gender' );
215 }
216 return $parser->getFunctionLang()->gender( $gender, $forms );
217 }
218 static function plural( $parser, $text = '') {
219 $forms = array_slice( func_get_args(), 2);
220 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
221 return $parser->getFunctionLang()->convertPlural( $text, $forms );
222 }
223
224 /**
225 * Override the title of the page when viewed, provided we've been given a
226 * title which will normalise to the canonical title
227 *
228 * @param Parser $parser Parent parser
229 * @param string $text Desired title text
230 * @return string
231 */
232 static function displaytitle( $parser, $displayTitle = '' ) {
233 #only requested titles that normalize to the actual title are allowed through
234 #mimic the escaping process that occurs in OutputPage::setPageTitle
235 $title = Title::newFromText( Sanitizer::stripAllTags( Sanitizer::normalizeCharReferences( Sanitizer::removeHTMLtags( $displayTitle ) ) ) );
236
237 if ( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) ) {
238 $parser->mOutput->setDisplayTitle( $displayTitle );
239 }
240
241 return '';
242 }
243
244 static function isRaw( $param ) {
245 static $mwRaw;
246 if ( !$mwRaw ) {
247 $mwRaw =& MagicWord::get( 'rawsuffix' );
248 }
249 if ( is_null( $param ) ) {
250 return false;
251 } else {
252 return $mwRaw->match( $param );
253 }
254 }
255
256 static function formatRaw( $num, $raw ) {
257 if( self::isRaw( $raw ) ) {
258 return $num;
259 } else {
260 global $wgContLang;
261 return $wgContLang->formatNum( $num );
262 }
263 }
264 static function numberofpages( $parser, $raw = null ) {
265 return self::formatRaw( SiteStats::pages(), $raw );
266 }
267 static function numberofusers( $parser, $raw = null ) {
268 return self::formatRaw( SiteStats::users(), $raw );
269 }
270 static function numberofactiveusers( $parser, $raw = null ) {
271 return self::formatRaw( SiteStats::activeUsers(), $raw );
272 }
273 static function numberofarticles( $parser, $raw = null ) {
274 return self::formatRaw( SiteStats::articles(), $raw );
275 }
276 static function numberoffiles( $parser, $raw = null ) {
277 return self::formatRaw( SiteStats::images(), $raw );
278 }
279 static function numberofadmins( $parser, $raw = null ) {
280 return self::formatRaw( SiteStats::numberingroup('sysop'), $raw );
281 }
282 static function numberofedits( $parser, $raw = null ) {
283 return self::formatRaw( SiteStats::edits(), $raw );
284 }
285 static function numberofviews( $parser, $raw = null ) {
286 return self::formatRaw( SiteStats::views(), $raw );
287 }
288 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
289 return self::formatRaw( SiteStats::pagesInNs( intval( $namespace ) ), $raw );
290 }
291 static function numberingroup( $parser, $name = '', $raw = null) {
292 return self::formatRaw( SiteStats::numberingroup( strtolower( $name ) ), $raw );
293 }
294
295
296 /**
297 * Given a title, return the namespace name that would be given by the
298 * corresponding magic word
299 * Note: function name changed to "mwnamespace" rather than "namespace"
300 * to not break PHP 5.3
301 */
302 static function mwnamespace( $parser, $title = null ) {
303 $t = Title::newFromText( $title );
304 if ( is_null($t) )
305 return '';
306 return str_replace( '_', ' ', $t->getNsText() );
307 }
308 static function namespacee( $parser, $title = null ) {
309 $t = Title::newFromText( $title );
310 if ( is_null($t) )
311 return '';
312 return wfUrlencode( $t->getNsText() );
313 }
314 static function talkspace( $parser, $title = null ) {
315 $t = Title::newFromText( $title );
316 if ( is_null($t) || !$t->canTalk() )
317 return '';
318 return str_replace( '_', ' ', $t->getTalkNsText() );
319 }
320 static function talkspacee( $parser, $title = null ) {
321 $t = Title::newFromText( $title );
322 if ( is_null($t) || !$t->canTalk() )
323 return '';
324 return wfUrlencode( $t->getTalkNsText() );
325 }
326 static function subjectspace( $parser, $title = null ) {
327 $t = Title::newFromText( $title );
328 if ( is_null($t) )
329 return '';
330 return str_replace( '_', ' ', $t->getSubjectNsText() );
331 }
332 static function subjectspacee( $parser, $title = null ) {
333 $t = Title::newFromText( $title );
334 if ( is_null($t) )
335 return '';
336 return wfUrlencode( $t->getSubjectNsText() );
337 }
338 /*
339 * Functions to get and normalize pagenames, corresponding to the magic words
340 * of the same names
341 */
342 static function pagename( $parser, $title = null ) {
343 $t = Title::newFromText( $title );
344 if ( is_null($t) )
345 return '';
346 return wfEscapeWikiText( $t->getText() );
347 }
348 static function pagenamee( $parser, $title = null ) {
349 $t = Title::newFromText( $title );
350 if ( is_null($t) )
351 return '';
352 return $t->getPartialURL();
353 }
354 static function fullpagename( $parser, $title = null ) {
355 $t = Title::newFromText( $title );
356 if ( is_null($t) || !$t->canTalk() )
357 return '';
358 return wfEscapeWikiText( $t->getPrefixedText() );
359 }
360 static function fullpagenamee( $parser, $title = null ) {
361 $t = Title::newFromText( $title );
362 if ( is_null($t) || !$t->canTalk() )
363 return '';
364 return $t->getPrefixedURL();
365 }
366 static function subpagename( $parser, $title = null ) {
367 $t = Title::newFromText( $title );
368 if ( is_null($t) )
369 return '';
370 return $t->getSubpageText();
371 }
372 static function subpagenamee( $parser, $title = null ) {
373 $t = Title::newFromText( $title );
374 if ( is_null($t) )
375 return '';
376 return $t->getSubpageUrlForm();
377 }
378 static function basepagename( $parser, $title = null ) {
379 $t = Title::newFromText( $title );
380 if ( is_null($t) )
381 return '';
382 return $t->getBaseText();
383 }
384 static function basepagenamee( $parser, $title = null ) {
385 $t = Title::newFromText( $title );
386 if ( is_null($t) )
387 return '';
388 return wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) );
389 }
390 static function talkpagename( $parser, $title = null ) {
391 $t = Title::newFromText( $title );
392 if ( is_null($t) || !$t->canTalk() )
393 return '';
394 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
395 }
396 static function talkpagenamee( $parser, $title = null ) {
397 $t = Title::newFromText( $title );
398 if ( is_null($t) || !$t->canTalk() )
399 return '';
400 return $t->getTalkPage()->getPrefixedUrl();
401 }
402 static function subjectpagename( $parser, $title = null ) {
403 $t = Title::newFromText( $title );
404 if ( is_null($t) )
405 return '';
406 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
407 }
408 static function subjectpagenamee( $parser, $title = null ) {
409 $t = Title::newFromText( $title );
410 if ( is_null($t) )
411 return '';
412 return $t->getSubjectPage()->getPrefixedUrl();
413 }
414
415 /**
416 * Return the number of pages in the given category, or 0 if it's nonexis-
417 * tent. This is an expensive parser function and can't be called too many
418 * times per page.
419 */
420 static function pagesincategory( $parser, $name = '', $raw = null ) {
421 static $cache = array();
422 $category = Category::newFromName( $name );
423
424 if( !is_object( $category ) ) {
425 $cache[$name] = 0;
426 return self::formatRaw( 0, $raw );
427 }
428
429 # Normalize name for cache
430 $name = $category->getName();
431
432 $count = 0;
433 if( isset( $cache[$name] ) ) {
434 $count = $cache[$name];
435 } elseif( $parser->incrementExpensiveFunctionCount() ) {
436 $count = $cache[$name] = (int)$category->getPageCount();
437 }
438 return self::formatRaw( $count, $raw );
439 }
440
441 /**
442 * Return the size of the given page, or 0 if it's nonexistent. This is an
443 * expensive parser function and can't be called too many times per page.
444 *
445 * @FIXME This doesn't work correctly on preview for getting the size of
446 * the current page.
447 * @FIXME Title::getLength() documentation claims that it adds things to
448 * the link cache, so the local cache here should be unnecessary, but in
449 * fact calling getLength() repeatedly for the same $page does seem to
450 * run one query for each call?
451 */
452 static function pagesize( $parser, $page = '', $raw = null ) {
453 static $cache = array();
454 $title = Title::newFromText($page);
455
456 if( !is_object( $title ) ) {
457 $cache[$page] = 0;
458 return self::formatRaw( 0, $raw );
459 }
460
461 # Normalize name for cache
462 $page = $title->getPrefixedText();
463
464 $length = 0;
465 if( isset( $cache[$page] ) ) {
466 $length = $cache[$page];
467 } elseif( $parser->incrementExpensiveFunctionCount() ) {
468 $rev = Revision::newFromTitle($title);
469 $id = $rev ? $rev->getPage() : 0;
470 $length = $cache[$page] = $rev ? $rev->getSize() : 0;
471
472 // Register dependency in templatelinks
473 $parser->mOutput->addTemplate( $title, $id, $rev ? $rev->getId() : 0 );
474 }
475 return self::formatRaw( $length, $raw );
476 }
477
478 /**
479 * Returns the requested protection level for the current page
480 */
481 static function protectionlevel( $parser, $type = '' ) {
482 $restrictions = $parser->mTitle->getRestrictions( strtolower( $type ) );
483 # Title::getRestrictions returns an array, its possible it may have
484 # multiple values in the future
485 return implode( $restrictions, ',' );
486 }
487
488 static function language( $parser, $arg = '' ) {
489 global $wgContLang;
490 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
491 return $lang != '' ? $lang : $arg;
492 }
493
494 /**
495 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
496 */
497 static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT ) {
498 $lengthOfPadding = mb_strlen( $padding );
499 if ( $lengthOfPadding == 0 ) return $string;
500
501 # The remaining length to add counts down to 0 as padding is added
502 $length = min( $length, 500 ) - mb_strlen( $string );
503 # $finalPadding is just $padding repeated enough times so that
504 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
505 $finalPadding = '';
506 while ( $length > 0 ) {
507 # If $length < $lengthofPadding, truncate $padding so we get the
508 # exact length desired.
509 $finalPadding .= mb_substr( $padding, 0, $length );
510 $length -= $lengthOfPadding;
511 }
512
513 if ( $direction == STR_PAD_LEFT ) {
514 return $finalPadding . $string;
515 } else {
516 return $string . $finalPadding;
517 }
518 }
519
520 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
521 return self::pad( $string, $length, $padding, STR_PAD_LEFT );
522 }
523
524 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
525 return self::pad( $string, $length, $padding );
526 }
527
528 static function anchorencode( $parser, $text ) {
529 $a = urlencode( $text );
530 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
531 # leave colons alone, however
532 $a = str_replace( '.3A', ':', $a );
533 return $a;
534 }
535
536 static function special( $parser, $text ) {
537 $title = SpecialPage::getTitleForAlias( $text );
538 if ( $title ) {
539 return $title->getPrefixedText();
540 } else {
541 return wfMsgForContent( 'nosuchspecialpage' );
542 }
543 }
544
545 public static function defaultsort( $parser, $text ) {
546 $text = trim( $text );
547 if( strlen( $text ) == 0 )
548 return '';
549 $old = $parser->getCustomDefaultSort();
550 $parser->setDefaultSort( $text );
551 if( $old === false || $old == $text )
552 return '';
553 else
554 return( '<span class="error">' .
555 wfMsg( 'duplicate-defaultsort',
556 htmlspecialchars( $old ),
557 htmlspecialchars( $text ) ) .
558 '</span>' );
559 }
560
561 public static function filepath( $parser, $name='', $option='' ) {
562 $file = wfFindFile( $name );
563 if( $file ) {
564 $url = $file->getFullUrl();
565 if( $option == 'nowiki' ) {
566 return array( $url, 'nowiki' => true );
567 }
568 return $url;
569 } else {
570 return '';
571 }
572 }
573
574 /**
575 * Parser function to extension tag adaptor
576 */
577 public static function tagObj( $parser, $frame, $args ) {
578 $xpath = false;
579 if ( !count( $args ) ) {
580 return '';
581 }
582 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
583
584 if ( count( $args ) ) {
585 $inner = $frame->expand( array_shift( $args ) );
586 } else {
587 $inner = null;
588 }
589
590 $stripList = $parser->getStripList();
591 if ( !in_array( $tagName, $stripList ) ) {
592 return '<span class="error">' .
593 wfMsg( 'unknown_extension_tag', $tagName ) .
594 '</span>';
595 }
596
597 $attributes = array();
598 foreach ( $args as $arg ) {
599 $bits = $arg->splitArg();
600 if ( strval( $bits['index'] ) === '' ) {
601 $name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
602 $value = trim( $frame->expand( $bits['value'] ) );
603 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
604 $value = isset( $m[1] ) ? $m[1] : '';
605 }
606 $attributes[$name] = $value;
607 }
608 }
609
610 $params = array(
611 'name' => $tagName,
612 'inner' => $inner,
613 'attributes' => $attributes,
614 'close' => "</$tagName>",
615 );
616 return $parser->extensionSubstitution( $params, $frame );
617 }
618
619 /**
620 * magic word call for a group convert from LanguageConverter.
621 */
622 public static function groupconvert( $parser, $group ) {
623 global $wgContLang;
624 return $wgContLang->groupConvert( $group );
625 }
626 }