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