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