Revert r46628, r46639 for now "Don't allow padding parser functions to be used 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__, 'namespace' ), 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 */
278 static function namespace( $parser, $title = null ) {
279 $t = Title::newFromText( $title );
280 if ( is_null($t) )
281 return '';
282 return str_replace( '_', ' ', $t->getNsText() );
283 }
284 static function namespacee( $parser, $title = null ) {
285 $t = Title::newFromText( $title );
286 if ( is_null($t) )
287 return '';
288 return wfUrlencode( $t->getNsText() );
289 }
290 static function talkspace( $parser, $title = null ) {
291 $t = Title::newFromText( $title );
292 if ( is_null($t) || !$t->canTalk() )
293 return '';
294 return str_replace( '_', ' ', $t->getTalkNsText() );
295 }
296 static function talkspacee( $parser, $title = null ) {
297 $t = Title::newFromText( $title );
298 if ( is_null($t) || !$t->canTalk() )
299 return '';
300 return wfUrlencode( $t->getTalkNsText() );
301 }
302 static function subjectspace( $parser, $title = null ) {
303 $t = Title::newFromText( $title );
304 if ( is_null($t) )
305 return '';
306 return str_replace( '_', ' ', $t->getSubjectNsText() );
307 }
308 static function subjectspacee( $parser, $title = null ) {
309 $t = Title::newFromText( $title );
310 if ( is_null($t) )
311 return '';
312 return wfUrlencode( $t->getSubjectNsText() );
313 }
314 /*
315 * Functions to get and normalize pagenames, corresponding to the magic words
316 * of the same names
317 */
318 static function pagename( $parser, $title = null ) {
319 $t = Title::newFromText( $title );
320 if ( is_null($t) )
321 return '';
322 return wfEscapeWikiText( $t->getText() );
323 }
324 static function pagenamee( $parser, $title = null ) {
325 $t = Title::newFromText( $title );
326 if ( is_null($t) )
327 return '';
328 return $t->getPartialURL();
329 }
330 static function fullpagename( $parser, $title = null ) {
331 $t = Title::newFromText( $title );
332 if ( is_null($t) || !$t->canTalk() )
333 return '';
334 return wfEscapeWikiText( $t->getPrefixedText() );
335 }
336 static function fullpagenamee( $parser, $title = null ) {
337 $t = Title::newFromText( $title );
338 if ( is_null($t) || !$t->canTalk() )
339 return '';
340 return $t->getPrefixedURL();
341 }
342 static function subpagename( $parser, $title = null ) {
343 $t = Title::newFromText( $title );
344 if ( is_null($t) )
345 return '';
346 return $t->getSubpageText();
347 }
348 static function subpagenamee( $parser, $title = null ) {
349 $t = Title::newFromText( $title );
350 if ( is_null($t) )
351 return '';
352 return $t->getSubpageUrlForm();
353 }
354 static function basepagename( $parser, $title = null ) {
355 $t = Title::newFromText( $title );
356 if ( is_null($t) )
357 return '';
358 return $t->getBaseText();
359 }
360 static function basepagenamee( $parser, $title = null ) {
361 $t = Title::newFromText( $title );
362 if ( is_null($t) )
363 return '';
364 return wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) );
365 }
366 static function talkpagename( $parser, $title = null ) {
367 $t = Title::newFromText( $title );
368 if ( is_null($t) || !$t->canTalk() )
369 return '';
370 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
371 }
372 static function talkpagenamee( $parser, $title = null ) {
373 $t = Title::newFromText( $title );
374 if ( is_null($t) || !$t->canTalk() )
375 return '';
376 return $t->getTalkPage()->getPrefixedUrl();
377 }
378 static function subjectpagename( $parser, $title = null ) {
379 $t = Title::newFromText( $title );
380 if ( is_null($t) )
381 return '';
382 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
383 }
384 static function subjectpagenamee( $parser, $title = null ) {
385 $t = Title::newFromText( $title );
386 if ( is_null($t) )
387 return '';
388 return $t->getSubjectPage()->getPrefixedUrl();
389 }
390
391 /**
392 * Return the number of pages in the given category, or 0 if it's nonexis-
393 * tent. This is an expensive parser function and can't be called too many
394 * times per page.
395 */
396 static function pagesincategory( $parser, $name = '', $raw = null ) {
397 static $cache = array();
398 $category = Category::newFromName( $name );
399
400 if( !is_object( $category ) ) {
401 $cache[$name] = 0;
402 return self::formatRaw( 0, $raw );
403 }
404
405 # Normalize name for cache
406 $name = $category->getName();
407
408 $count = 0;
409 if( isset( $cache[$name] ) ) {
410 $count = $cache[$name];
411 } elseif( $parser->incrementExpensiveFunctionCount() ) {
412 $count = $cache[$name] = (int)$category->getPageCount();
413 }
414 return self::formatRaw( $count, $raw );
415 }
416
417 /**
418 * Return the size of the given page, or 0 if it's nonexistent. This is an
419 * expensive parser function and can't be called too many times per page.
420 *
421 * @FIXME This doesn't work correctly on preview for getting the size of
422 * the current page.
423 * @FIXME Title::getLength() documentation claims that it adds things to
424 * the link cache, so the local cache here should be unnecessary, but in
425 * fact calling getLength() repeatedly for the same $page does seem to
426 * run one query for each call?
427 */
428 static function pagesize( $parser, $page = '', $raw = null ) {
429 static $cache = array();
430 $title = Title::newFromText($page);
431
432 if( !is_object( $title ) ) {
433 $cache[$page] = 0;
434 return self::formatRaw( 0, $raw );
435 }
436
437 # Normalize name for cache
438 $page = $title->getPrefixedText();
439
440 $length = 0;
441 if( isset( $cache[$page] ) ) {
442 $length = $cache[$page];
443 } elseif( $parser->incrementExpensiveFunctionCount() ) {
444 $rev = Revision::newFromTitle($title);
445 $id = $rev ? $rev->getPage() : 0;
446 $length = $cache[$page] = $rev ? $rev->getSize() : 0;
447
448 // Register dependency in templatelinks
449 $parser->mOutput->addTemplate( $title, $id, $rev ? $rev->getId() : 0 );
450 }
451 return self::formatRaw( $length, $raw );
452 }
453
454 /**
455 * Returns the requested protection level for the current page
456 */
457 static function protectionlevel( $parser, $type = '' ) {
458 $restrictions = $parser->mTitle->getRestrictions( strtolower( $type ) );
459 # Title::getRestrictions returns an array, its possible it may have
460 # multiple values in the future
461 return implode( $restrictions, ',' );
462 }
463
464 static function language( $parser, $arg = '' ) {
465 global $wgContLang;
466 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
467 return $lang != '' ? $lang : $arg;
468 }
469
470 /**
471 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
472 */
473 static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT ) {
474 $lengthOfPadding = mb_strlen( $padding );
475 if ( $lengthOfPadding == 0 ) return $string;
476
477 # The remaining length to add counts down to 0 as padding is added
478 $length = min( $length, 500 ) - mb_strlen( $string );
479 # $finalPadding is just $padding repeated enough times so that
480 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
481 $finalPadding = '';
482 while ( $length > 0 ) {
483 # If $length < $lengthofPadding, truncate $padding so we get the
484 # exact length desired.
485 $finalPadding .= mb_substr( $padding, 0, $length );
486 $length -= $lengthOfPadding;
487 }
488
489 if ( $direction == STR_PAD_LEFT ) {
490 return $finalPadding . $string;
491 } else {
492 return $string . $finalPadding;
493 }
494 }
495
496 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
497 return self::pad( $string, $length, $padding, STR_PAD_LEFT );
498 }
499
500 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
501 return self::pad( $string, $length, $padding );
502 }
503
504 static function anchorencode( $parser, $text ) {
505 $a = urlencode( $text );
506 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
507 # leave colons alone, however
508 $a = str_replace( '.3A', ':', $a );
509 return $a;
510 }
511
512 static function special( $parser, $text ) {
513 $title = SpecialPage::getTitleForAlias( $text );
514 if ( $title ) {
515 return $title->getPrefixedText();
516 } else {
517 return wfMsgForContent( 'nosuchspecialpage' );
518 }
519 }
520
521 public static function defaultsort( $parser, $text ) {
522 $text = trim( $text );
523 if( strlen( $text ) == 0 )
524 return '';
525 $old = $parser->getCustomDefaultSort();
526 $parser->setDefaultSort( $text );
527 if( $old === false || $old == $text )
528 return '';
529 else
530 return( '<span class="error">' .
531 wfMsg( 'duplicate-defaultsort',
532 htmlspecialchars( $old ),
533 htmlspecialchars( $text ) ) .
534 '</span>' );
535 }
536
537 public static function filepath( $parser, $name='', $option='' ) {
538 $file = wfFindFile( $name );
539 if( $file ) {
540 $url = $file->getFullUrl();
541 if( $option == 'nowiki' ) {
542 return array( $url, 'nowiki' => true );
543 }
544 return $url;
545 } else {
546 return '';
547 }
548 }
549
550 /**
551 * Parser function to extension tag adaptor
552 */
553 public static function tagObj( $parser, $frame, $args ) {
554 $xpath = false;
555 if ( !count( $args ) ) {
556 return '';
557 }
558 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
559
560 if ( count( $args ) ) {
561 $inner = $frame->expand( array_shift( $args ) );
562 } else {
563 $inner = null;
564 }
565
566 $stripList = $parser->getStripList();
567 if ( !in_array( $tagName, $stripList ) ) {
568 return '<span class="error">' .
569 wfMsg( 'unknown_extension_tag', $tagName ) .
570 '</span>';
571 }
572
573 $attributes = array();
574 foreach ( $args as $arg ) {
575 $bits = $arg->splitArg();
576 if ( strval( $bits['index'] ) === '' ) {
577 $name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
578 $value = trim( $frame->expand( $bits['value'] ) );
579 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
580 $value = isset( $m[1] ) ? $m[1] : '';
581 }
582 $attributes[$name] = $value;
583 }
584 }
585
586 $params = array(
587 'name' => $tagName,
588 'inner' => $inner,
589 'attributes' => $attributes,
590 'close' => "</$tagName>",
591 );
592 return $parser->extensionSubstitution( $params, $frame );
593 }
594 }