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