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