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