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