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