Revert r51200, r51253, r51254, addition of {{NUMBEROFCONTRIBS:...}} parser function...
[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' ) );
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 wfProfileIn( __METHOD__ );
197 $forms = array_slice( func_get_args(), 2);
198
199 // default
200 $gender = User::getDefaultOption( 'gender' );
201
202 // allow prefix.
203 $title = Title::newFromText( $user );
204
205 if (is_object( $title ) && $title->getNamespace() == NS_USER)
206 $user = $title->getText();
207
208 // check parameter, or use $wgUser if in interface message
209 $user = User::newFromName( $user );
210 if ( $user ) {
211 $gender = $user->getOption( 'gender' );
212 } elseif ( $parser->mOptions->getInterfaceMessage() ) {
213 global $wgUser;
214 $gender = $wgUser->getOption( 'gender' );
215 }
216 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
217 wfProfileOut( __METHOD__ );
218 return $ret;
219 }
220 static function plural( $parser, $text = '') {
221 $forms = array_slice( func_get_args(), 2);
222 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
223 return $parser->getFunctionLang()->convertPlural( $text, $forms );
224 }
225
226 /**
227 * Override the title of the page when viewed, provided we've been given a
228 * title which will normalise to the canonical title
229 *
230 * @param Parser $parser Parent parser
231 * @param string $text Desired title text
232 * @return string
233 */
234 static function displaytitle( $parser, $text = '' ) {
235 global $wgRestrictDisplayTitle;
236
237 #list of disallowed tags for DISPLAYTITLE
238 #these will be escaped even though they are allowed in normal wiki text
239 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li',
240 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp' );
241
242 #only requested titles that normalize to the actual title are allowed through
243 #mimic the escaping process that occurs in OutputPage::setPageTitle
244 $text = Sanitizer::normalizeCharReferences( Sanitizer::removeHTMLtags( $text, null, array(), array(), $bad ) );
245 $title = Title::newFromText( Sanitizer::stripAllTags( $text ) );
246
247 if( !$wgRestrictDisplayTitle ) {
248 $parser->mOutput->setDisplayTitle( $text );
249 } else {
250 if ( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) ) {
251 $parser->mOutput->setDisplayTitle( $text );
252 }
253 }
254
255 return '';
256 }
257
258 static function isRaw( $param ) {
259 static $mwRaw;
260 if ( !$mwRaw ) {
261 $mwRaw =& MagicWord::get( 'rawsuffix' );
262 }
263 if ( is_null( $param ) ) {
264 return false;
265 } else {
266 return $mwRaw->match( $param );
267 }
268 }
269
270 static function formatRaw( $num, $raw ) {
271 if( self::isRaw( $raw ) ) {
272 return $num;
273 } else {
274 global $wgContLang;
275 return $wgContLang->formatNum( $num );
276 }
277 }
278 static function numberofpages( $parser, $raw = null ) {
279 return self::formatRaw( SiteStats::pages(), $raw );
280 }
281 static function numberofusers( $parser, $raw = null ) {
282 return self::formatRaw( SiteStats::users(), $raw );
283 }
284 static function numberofactiveusers( $parser, $raw = null ) {
285 return self::formatRaw( SiteStats::activeUsers(), $raw );
286 }
287 static function numberofarticles( $parser, $raw = null ) {
288 return self::formatRaw( SiteStats::articles(), $raw );
289 }
290 static function numberoffiles( $parser, $raw = null ) {
291 return self::formatRaw( SiteStats::images(), $raw );
292 }
293 static function numberofadmins( $parser, $raw = null ) {
294 return self::formatRaw( SiteStats::numberingroup('sysop'), $raw );
295 }
296 static function numberofedits( $parser, $raw = null ) {
297 return self::formatRaw( SiteStats::edits(), $raw );
298 }
299 static function numberofviews( $parser, $raw = null ) {
300 return self::formatRaw( SiteStats::views(), $raw );
301 }
302 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
303 return self::formatRaw( SiteStats::pagesInNs( intval( $namespace ) ), $raw );
304 }
305 static function numberingroup( $parser, $name = '', $raw = null) {
306 return self::formatRaw( SiteStats::numberingroup( strtolower( $name ) ), $raw );
307 }
308
309
310 /**
311 * Given a title, return the namespace name that would be given by the
312 * corresponding magic word
313 * Note: function name changed to "mwnamespace" rather than "namespace"
314 * to not break PHP 5.3
315 */
316 static function mwnamespace( $parser, $title = null ) {
317 $t = Title::newFromText( $title );
318 if ( is_null($t) )
319 return '';
320 return str_replace( '_', ' ', $t->getNsText() );
321 }
322 static function namespacee( $parser, $title = null ) {
323 $t = Title::newFromText( $title );
324 if ( is_null($t) )
325 return '';
326 return wfUrlencode( $t->getNsText() );
327 }
328 static function talkspace( $parser, $title = null ) {
329 $t = Title::newFromText( $title );
330 if ( is_null($t) || !$t->canTalk() )
331 return '';
332 return str_replace( '_', ' ', $t->getTalkNsText() );
333 }
334 static function talkspacee( $parser, $title = null ) {
335 $t = Title::newFromText( $title );
336 if ( is_null($t) || !$t->canTalk() )
337 return '';
338 return wfUrlencode( $t->getTalkNsText() );
339 }
340 static function subjectspace( $parser, $title = null ) {
341 $t = Title::newFromText( $title );
342 if ( is_null($t) )
343 return '';
344 return str_replace( '_', ' ', $t->getSubjectNsText() );
345 }
346 static function subjectspacee( $parser, $title = null ) {
347 $t = Title::newFromText( $title );
348 if ( is_null($t) )
349 return '';
350 return wfUrlencode( $t->getSubjectNsText() );
351 }
352 /*
353 * Functions to get and normalize pagenames, corresponding to the magic words
354 * of the same names
355 */
356 static function pagename( $parser, $title = null ) {
357 $t = Title::newFromText( $title );
358 if ( is_null($t) )
359 return '';
360 return wfEscapeWikiText( $t->getText() );
361 }
362 static function pagenamee( $parser, $title = null ) {
363 $t = Title::newFromText( $title );
364 if ( is_null($t) )
365 return '';
366 return $t->getPartialURL();
367 }
368 static function fullpagename( $parser, $title = null ) {
369 $t = Title::newFromText( $title );
370 if ( is_null($t) || !$t->canTalk() )
371 return '';
372 return wfEscapeWikiText( $t->getPrefixedText() );
373 }
374 static function fullpagenamee( $parser, $title = null ) {
375 $t = Title::newFromText( $title );
376 if ( is_null($t) || !$t->canTalk() )
377 return '';
378 return $t->getPrefixedURL();
379 }
380 static function subpagename( $parser, $title = null ) {
381 $t = Title::newFromText( $title );
382 if ( is_null($t) )
383 return '';
384 return $t->getSubpageText();
385 }
386 static function subpagenamee( $parser, $title = null ) {
387 $t = Title::newFromText( $title );
388 if ( is_null($t) )
389 return '';
390 return $t->getSubpageUrlForm();
391 }
392 static function basepagename( $parser, $title = null ) {
393 $t = Title::newFromText( $title );
394 if ( is_null($t) )
395 return '';
396 return $t->getBaseText();
397 }
398 static function basepagenamee( $parser, $title = null ) {
399 $t = Title::newFromText( $title );
400 if ( is_null($t) )
401 return '';
402 return wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) );
403 }
404 static function talkpagename( $parser, $title = null ) {
405 $t = Title::newFromText( $title );
406 if ( is_null($t) || !$t->canTalk() )
407 return '';
408 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
409 }
410 static function talkpagenamee( $parser, $title = null ) {
411 $t = Title::newFromText( $title );
412 if ( is_null($t) || !$t->canTalk() )
413 return '';
414 return $t->getTalkPage()->getPrefixedUrl();
415 }
416 static function subjectpagename( $parser, $title = null ) {
417 $t = Title::newFromText( $title );
418 if ( is_null($t) )
419 return '';
420 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
421 }
422 static function subjectpagenamee( $parser, $title = null ) {
423 $t = Title::newFromText( $title );
424 if ( is_null($t) )
425 return '';
426 return $t->getSubjectPage()->getPrefixedUrl();
427 }
428
429 /**
430 * Return the number of pages in the given category, or 0 if it's nonexis-
431 * tent. This is an expensive parser function and can't be called too many
432 * times per page.
433 */
434 static function pagesincategory( $parser, $name = '', $raw = null ) {
435 static $cache = array();
436 $category = Category::newFromName( $name );
437
438 if( !is_object( $category ) ) {
439 $cache[$name] = 0;
440 return self::formatRaw( 0, $raw );
441 }
442
443 # Normalize name for cache
444 $name = $category->getName();
445
446 $count = 0;
447 if( isset( $cache[$name] ) ) {
448 $count = $cache[$name];
449 } elseif( $parser->incrementExpensiveFunctionCount() ) {
450 $count = $cache[$name] = (int)$category->getPageCount();
451 }
452 return self::formatRaw( $count, $raw );
453 }
454
455 /**
456 * Return the size of the given page, or 0 if it's nonexistent. This is an
457 * expensive parser function and can't be called too many times per page.
458 *
459 * @FIXME This doesn't work correctly on preview for getting the size of
460 * the current page.
461 * @FIXME Title::getLength() documentation claims that it adds things to
462 * the link cache, so the local cache here should be unnecessary, but in
463 * fact calling getLength() repeatedly for the same $page does seem to
464 * run one query for each call?
465 */
466 static function pagesize( $parser, $page = '', $raw = null ) {
467 static $cache = array();
468 $title = Title::newFromText($page);
469
470 if( !is_object( $title ) ) {
471 $cache[$page] = 0;
472 return self::formatRaw( 0, $raw );
473 }
474
475 # Normalize name for cache
476 $page = $title->getPrefixedText();
477
478 $length = 0;
479 if( isset( $cache[$page] ) ) {
480 $length = $cache[$page];
481 } elseif( $parser->incrementExpensiveFunctionCount() ) {
482 $rev = Revision::newFromTitle($title);
483 $id = $rev ? $rev->getPage() : 0;
484 $length = $cache[$page] = $rev ? $rev->getSize() : 0;
485
486 // Register dependency in templatelinks
487 $parser->mOutput->addTemplate( $title, $id, $rev ? $rev->getId() : 0 );
488 }
489 return self::formatRaw( $length, $raw );
490 }
491
492 /**
493 * Returns the requested protection level for the current page
494 */
495 static function protectionlevel( $parser, $type = '' ) {
496 $restrictions = $parser->mTitle->getRestrictions( strtolower( $type ) );
497 # Title::getRestrictions returns an array, its possible it may have
498 # multiple values in the future
499 return implode( $restrictions, ',' );
500 }
501
502 static function language( $parser, $arg = '' ) {
503 global $wgContLang;
504 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
505 return $lang != '' ? $lang : $arg;
506 }
507
508 /**
509 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
510 */
511 static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT ) {
512 $lengthOfPadding = mb_strlen( $padding );
513 if ( $lengthOfPadding == 0 ) return $string;
514
515 # The remaining length to add counts down to 0 as padding is added
516 $length = min( $length, 500 ) - mb_strlen( $string );
517 # $finalPadding is just $padding repeated enough times so that
518 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
519 $finalPadding = '';
520 while ( $length > 0 ) {
521 # If $length < $lengthofPadding, truncate $padding so we get the
522 # exact length desired.
523 $finalPadding .= mb_substr( $padding, 0, $length );
524 $length -= $lengthOfPadding;
525 }
526
527 if ( $direction == STR_PAD_LEFT ) {
528 return $finalPadding . $string;
529 } else {
530 return $string . $finalPadding;
531 }
532 }
533
534 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
535 return self::pad( $string, $length, $padding, STR_PAD_LEFT );
536 }
537
538 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
539 return self::pad( $string, $length, $padding );
540 }
541
542 static function anchorencode( $parser, $text ) {
543 $a = urlencode( $text );
544 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
545 # leave colons alone, however
546 $a = str_replace( '.3A', ':', $a );
547 return $a;
548 }
549
550 static function special( $parser, $text ) {
551 $title = SpecialPage::getTitleForAlias( $text );
552 if ( $title ) {
553 return $title->getPrefixedText();
554 } else {
555 return wfMsgForContent( 'nosuchspecialpage' );
556 }
557 }
558
559 public static function defaultsort( $parser, $text ) {
560 $text = trim( $text );
561 if( strlen( $text ) == 0 )
562 return '';
563 $old = $parser->getCustomDefaultSort();
564 $parser->setDefaultSort( $text );
565 if( $old === false || $old == $text )
566 return '';
567 else
568 return( '<span class="error">' .
569 wfMsg( 'duplicate-defaultsort',
570 htmlspecialchars( $old ),
571 htmlspecialchars( $text ) ) .
572 '</span>' );
573 }
574
575 public static function filepath( $parser, $name='', $option='' ) {
576 $file = wfFindFile( $name );
577 if( $file ) {
578 $url = $file->getFullUrl();
579 if( $option == 'nowiki' ) {
580 return array( $url, 'nowiki' => true );
581 }
582 return $url;
583 } else {
584 return '';
585 }
586 }
587
588 /**
589 * Parser function to extension tag adaptor
590 */
591 public static function tagObj( $parser, $frame, $args ) {
592 $xpath = false;
593 if ( !count( $args ) ) {
594 return '';
595 }
596 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
597
598 if ( count( $args ) ) {
599 $inner = $frame->expand( array_shift( $args ) );
600 } else {
601 $inner = null;
602 }
603
604 $stripList = $parser->getStripList();
605 if ( !in_array( $tagName, $stripList ) ) {
606 return '<span class="error">' .
607 wfMsg( 'unknown_extension_tag', $tagName ) .
608 '</span>';
609 }
610
611 $attributes = array();
612 foreach ( $args as $arg ) {
613 $bits = $arg->splitArg();
614 if ( strval( $bits['index'] ) === '' ) {
615 $name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
616 $value = trim( $frame->expand( $bits['value'] ) );
617 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
618 $value = isset( $m[1] ) ? $m[1] : '';
619 }
620 $attributes[$name] = $value;
621 }
622 }
623
624 $params = array(
625 'name' => $tagName,
626 'inner' => $inner,
627 'attributes' => $attributes,
628 'close' => "</$tagName>",
629 );
630 return $parser->extensionSubstitution( $params, $frame );
631 }
632 }