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