Reverted r48984. Fragile, doesn't work if memcached is enabled. See CodeReview.
[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
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 wfProfileIn( __METHOD__ );
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 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
225 wfProfileOut( __METHOD__ );
226 return $ret;
227 }
228 static function plural( $parser, $text = '') {
229 $forms = array_slice( func_get_args(), 2);
230 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
231 return $parser->getFunctionLang()->convertPlural( $text, $forms );
232 }
233
234 /**
235 * Override the title of the page when viewed, provided we've been given a
236 * title which will normalise to the canonical title
237 *
238 * @param Parser $parser Parent parser
239 * @param string $text Desired title text
240 * @return string
241 */
242 static function displaytitle( $parser, $text = '' ) {
243 global $wgRestrictDisplayTitle;
244
245 #list of disallowed tags for DISPLAYTITLE
246 #these will be escaped even though they are allowed in normal wiki text
247 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li',
248 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp' );
249
250 #only requested titles that normalize to the actual title are allowed through
251 #mimic the escaping process that occurs in OutputPage::setPageTitle
252 $text = Sanitizer::normalizeCharReferences( Sanitizer::removeHTMLtags( $text, null, array(), array(), $bad ) );
253 $title = Title::newFromText( Sanitizer::stripAllTags( $text ) );
254
255 if( !$wgRestrictDisplayTitle ) {
256 $parser->mOutput->setDisplayTitle( $text );
257 } else {
258 if ( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) ) {
259 $parser->mOutput->setDisplayTitle( $text );
260 }
261 }
262
263 return '';
264 }
265
266 static function isRaw( $param ) {
267 static $mwRaw;
268 if ( !$mwRaw ) {
269 $mwRaw =& MagicWord::get( 'rawsuffix' );
270 }
271 if ( is_null( $param ) ) {
272 return false;
273 } else {
274 return $mwRaw->match( $param );
275 }
276 }
277
278 static function formatRaw( $num, $raw ) {
279 if( self::isRaw( $raw ) ) {
280 return $num;
281 } else {
282 global $wgContLang;
283 return $wgContLang->formatNum( $num );
284 }
285 }
286 static function numberofpages( $parser, $raw = null ) {
287 return self::formatRaw( SiteStats::pages(), $raw );
288 }
289 static function numberofusers( $parser, $raw = null ) {
290 return self::formatRaw( SiteStats::users(), $raw );
291 }
292 static function numberofactiveusers( $parser, $raw = null ) {
293 return self::formatRaw( SiteStats::activeUsers(), $raw );
294 }
295 static function numberofarticles( $parser, $raw = null ) {
296 return self::formatRaw( SiteStats::articles(), $raw );
297 }
298 static function numberoffiles( $parser, $raw = null ) {
299 return self::formatRaw( SiteStats::images(), $raw );
300 }
301 static function numberofadmins( $parser, $raw = null ) {
302 return self::formatRaw( SiteStats::numberingroup('sysop'), $raw );
303 }
304 static function numberofedits( $parser, $raw = null ) {
305 return self::formatRaw( SiteStats::edits(), $raw );
306 }
307 static function numberofviews( $parser, $raw = null ) {
308 return self::formatRaw( SiteStats::views(), $raw );
309 }
310 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
311 return self::formatRaw( SiteStats::pagesInNs( intval( $namespace ) ), $raw );
312 }
313 static function numberingroup( $parser, $name = '', $raw = null) {
314 return self::formatRaw( SiteStats::numberingroup( strtolower( $name ) ), $raw );
315 }
316
317
318 /**
319 * Given a title, return the namespace name that would be given by the
320 * corresponding magic word
321 * Note: function name changed to "mwnamespace" rather than "namespace"
322 * to not break PHP 5.3
323 */
324 static function mwnamespace( $parser, $title = null ) {
325 $t = Title::newFromText( $title );
326 if ( is_null($t) )
327 return '';
328 return str_replace( '_', ' ', $t->getNsText() );
329 }
330 static function namespacee( $parser, $title = null ) {
331 $t = Title::newFromText( $title );
332 if ( is_null($t) )
333 return '';
334 return wfUrlencode( $t->getNsText() );
335 }
336 static function talkspace( $parser, $title = null ) {
337 $t = Title::newFromText( $title );
338 if ( is_null($t) || !$t->canTalk() )
339 return '';
340 return str_replace( '_', ' ', $t->getTalkNsText() );
341 }
342 static function talkspacee( $parser, $title = null ) {
343 $t = Title::newFromText( $title );
344 if ( is_null($t) || !$t->canTalk() )
345 return '';
346 return wfUrlencode( $t->getTalkNsText() );
347 }
348 static function subjectspace( $parser, $title = null ) {
349 $t = Title::newFromText( $title );
350 if ( is_null($t) )
351 return '';
352 return str_replace( '_', ' ', $t->getSubjectNsText() );
353 }
354 static function subjectspacee( $parser, $title = null ) {
355 $t = Title::newFromText( $title );
356 if ( is_null($t) )
357 return '';
358 return wfUrlencode( $t->getSubjectNsText() );
359 }
360 /*
361 * Functions to get and normalize pagenames, corresponding to the magic words
362 * of the same names
363 */
364 static function pagename( $parser, $title = null ) {
365 $t = Title::newFromText( $title );
366 if ( is_null($t) )
367 return '';
368 return wfEscapeWikiText( $t->getText() );
369 }
370 static function pagenamee( $parser, $title = null ) {
371 $t = Title::newFromText( $title );
372 if ( is_null($t) )
373 return '';
374 return $t->getPartialURL();
375 }
376 static function fullpagename( $parser, $title = null ) {
377 $t = Title::newFromText( $title );
378 if ( is_null($t) || !$t->canTalk() )
379 return '';
380 return wfEscapeWikiText( $t->getPrefixedText() );
381 }
382 static function fullpagenamee( $parser, $title = null ) {
383 $t = Title::newFromText( $title );
384 if ( is_null($t) || !$t->canTalk() )
385 return '';
386 return $t->getPrefixedURL();
387 }
388 static function subpagename( $parser, $title = null ) {
389 $t = Title::newFromText( $title );
390 if ( is_null($t) )
391 return '';
392 return $t->getSubpageText();
393 }
394 static function subpagenamee( $parser, $title = null ) {
395 $t = Title::newFromText( $title );
396 if ( is_null($t) )
397 return '';
398 return $t->getSubpageUrlForm();
399 }
400 static function basepagename( $parser, $title = null ) {
401 $t = Title::newFromText( $title );
402 if ( is_null($t) )
403 return '';
404 return $t->getBaseText();
405 }
406 static function basepagenamee( $parser, $title = null ) {
407 $t = Title::newFromText( $title );
408 if ( is_null($t) )
409 return '';
410 return wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) );
411 }
412 static function talkpagename( $parser, $title = null ) {
413 $t = Title::newFromText( $title );
414 if ( is_null($t) || !$t->canTalk() )
415 return '';
416 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
417 }
418 static function talkpagenamee( $parser, $title = null ) {
419 $t = Title::newFromText( $title );
420 if ( is_null($t) || !$t->canTalk() )
421 return '';
422 return $t->getTalkPage()->getPrefixedUrl();
423 }
424 static function subjectpagename( $parser, $title = null ) {
425 $t = Title::newFromText( $title );
426 if ( is_null($t) )
427 return '';
428 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
429 }
430 static function subjectpagenamee( $parser, $title = null ) {
431 $t = Title::newFromText( $title );
432 if ( is_null($t) )
433 return '';
434 return $t->getSubjectPage()->getPrefixedUrl();
435 }
436 /*
437 * Functions to get revision informations, corresponding to the magic words
438 * of the same names
439 */
440 static function revisionid( $parser, $title = null ) {
441 static $cache = array ();
442 $t = Title::newFromText( $title );
443 if ( is_null( $t ) )
444 return '';
445 if ( $t->equals( $parser->getTitle() ) ) {
446 // Let the edit saving system know we should parse the page
447 // *after* a revision ID has been assigned.
448 $parser->mOutput->setFlag( 'vary-revision' );
449 wfDebug( __METHOD__ . ": {{REVISIONID}} used, setting vary-revision...\n" );
450 return $parser->getRevisionId();
451 }
452 if ( isset( $cache[$t->getPrefixedText()] ) )
453 return $cache[$t->getPrefixedText()];
454 elseif ( $parser->incrementExpensiveFunctionCount() ) {
455 $a = new Article( $t );
456 return $cache[$t->getPrefixedText()] = $a->getRevIdFetched();
457 }
458 return '';
459 }
460 static function revisiontimestamp( $parser, $title = null ) {
461 static $cache = array ();
462 $t = Title::newFromText( $title );
463 if ( is_null( $t ) )
464 return '';
465 if ( $t->equals( $parser->getTitle() ) ) {
466 // Let the edit saving system know we should parse the page
467 // *after* a revision ID has been assigned. This is for null edits.
468 $parser->mOutput->setFlag( 'vary-revision' );
469 wfDebug( __METHOD__ . ": {{REVISIONTIMESTAMP}} or related parser function used, setting vary-revision...\n" );
470 return $parser->getRevisionTimestamp();
471 }
472 if ( isset( $cache[$t->getPrefixedText()] ) )
473 return $cache[$t->getPrefixedText()];
474 elseif ( $parser->incrementExpensiveFunctionCount() ) {
475 $a = new Article( $t );
476 return $cache[$t->getPrefixedText()] = $a->getTimestamp();
477 }
478 return '';
479 }
480 static function revisionday( $parser, $title = null ) {
481 $timestamp = self::revisiontimestamp( $parser, $title );
482 if ( $timestamp == '' ) return '';
483 return intval( substr( $timestamp, 6, 2 ) );
484 }
485 static function revisionday2( $parser, $title = null ) {
486 $timestamp = self::revisiontimestamp( $parser, $title );
487 if ( $timestamp == '' ) return '';
488 return substr( $timestamp, 6, 2 );
489 }
490 static function revisionmonth( $parser, $title = null ) {
491 $timestamp = self::revisiontimestamp( $parser, $title );
492 if ( $timestamp == '' ) return '';
493 return intval( substr( $timestamp, 4, 2 ) );
494 }
495 static function revisionyear( $parser, $title = null ) {
496 $timestamp = self::revisiontimestamp( $parser, $title );
497 if ( $timestamp == '' ) return '';
498 return substr( $timestamp, 0, 4 );
499 }
500 static function revisionuser( $parser, $title = null ) {
501 static $cache = array();
502 $t = Title::newFromText( $title );
503 if ( is_null( $t ) )
504 return '';
505 if ( $t->equals( $parser->getTitle() ) ) {
506 // Let the edit saving system know we should parse the page
507 // *after* a revision ID has been assigned. This is for null edits.
508 $parser->mOutput->setFlag( 'vary-revision' );
509 wfDebug( __METHOD__ . ": {{REVISIONUSER}} used, setting vary-revision...\n" );
510 return $parser->getRevisionUser();
511 }
512 if ( isset( $cache[$t->getPrefixedText()] ) )
513 return $cache[$t->getPrefixedText()];
514 elseif ( $parser->incrementExpensiveFunctionCount() ) {
515 $a = new Article( $t );
516 return $cache[$t->getPrefixedText()] = $a->getUserText();
517 }
518 return '';
519 }
520
521 /**
522 * Return the number of pages in the given category, or 0 if it's nonexis-
523 * tent. This is an expensive parser function and can't be called too many
524 * times per page.
525 */
526 static function pagesincategory( $parser, $name = '', $raw = null ) {
527 static $cache = array();
528 $category = Category::newFromName( $name );
529
530 if( !is_object( $category ) ) {
531 $cache[$name] = 0;
532 return self::formatRaw( 0, $raw );
533 }
534
535 # Normalize name for cache
536 $name = $category->getName();
537
538 $count = 0;
539 if( isset( $cache[$name] ) ) {
540 $count = $cache[$name];
541 } elseif( $parser->incrementExpensiveFunctionCount() ) {
542 $count = $cache[$name] = (int)$category->getPageCount();
543 }
544 return self::formatRaw( $count, $raw );
545 }
546
547 /**
548 * Return the size of the given page, or 0 if it's nonexistent. This is an
549 * expensive parser function and can't be called too many times per page.
550 *
551 * @FIXME This doesn't work correctly on preview for getting the size of
552 * the current page.
553 * @FIXME Title::getLength() documentation claims that it adds things to
554 * the link cache, so the local cache here should be unnecessary, but in
555 * fact calling getLength() repeatedly for the same $page does seem to
556 * run one query for each call?
557 */
558 static function pagesize( $parser, $page = '', $raw = null ) {
559 static $cache = array();
560 $title = Title::newFromText($page);
561
562 if( !is_object( $title ) ) {
563 $cache[$page] = 0;
564 return self::formatRaw( 0, $raw );
565 }
566
567 # Normalize name for cache
568 $page = $title->getPrefixedText();
569
570 $length = 0;
571 if( isset( $cache[$page] ) ) {
572 $length = $cache[$page];
573 } elseif( $parser->incrementExpensiveFunctionCount() ) {
574 $rev = Revision::newFromTitle($title);
575 $id = $rev ? $rev->getPage() : 0;
576 $length = $cache[$page] = $rev ? $rev->getSize() : 0;
577
578 // Register dependency in templatelinks
579 $parser->mOutput->addTemplate( $title, $id, $rev ? $rev->getId() : 0 );
580 }
581 return self::formatRaw( $length, $raw );
582 }
583
584 /**
585 * Returns the requested protection level for the current page
586 */
587 static function protectionlevel( $parser, $type = '' ) {
588 $restrictions = $parser->mTitle->getRestrictions( strtolower( $type ) );
589 # Title::getRestrictions returns an array, its possible it may have
590 # multiple values in the future
591 return implode( $restrictions, ',' );
592 }
593
594 static function language( $parser, $arg = '' ) {
595 global $wgContLang;
596 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
597 return $lang != '' ? $lang : $arg;
598 }
599
600 /**
601 * Returns the number of contributions by a certain user. This is an
602 * expensive parser function and can't be called too many times per page
603 */
604 static function numberofcontribs( $parser, $user = null, $raw = null ) {
605 if ( is_null($user) || !User::isValidUserName( $user ) ) {
606 return '';
607 }
608 if ( !$parser->incrementExpensiveFunctionCount() ) {
609 return '';
610 }
611 $u = User::newFromName( $user );
612 $id = User::idFromName( $u->mName );
613 return self::formatRaw( User::edits( $id ), $raw );
614 }
615
616 /**
617 * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
618 */
619 static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT ) {
620 $lengthOfPadding = mb_strlen( $padding );
621 if ( $lengthOfPadding == 0 ) return $string;
622
623 # The remaining length to add counts down to 0 as padding is added
624 $length = min( $length, 500 ) - mb_strlen( $string );
625 # $finalPadding is just $padding repeated enough times so that
626 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
627 $finalPadding = '';
628 while ( $length > 0 ) {
629 # If $length < $lengthofPadding, truncate $padding so we get the
630 # exact length desired.
631 $finalPadding .= mb_substr( $padding, 0, $length );
632 $length -= $lengthOfPadding;
633 }
634
635 if ( $direction == STR_PAD_LEFT ) {
636 return $finalPadding . $string;
637 } else {
638 return $string . $finalPadding;
639 }
640 }
641
642 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
643 return self::pad( $string, $length, $padding, STR_PAD_LEFT );
644 }
645
646 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
647 return self::pad( $string, $length, $padding );
648 }
649
650 static function anchorencode( $parser, $text ) {
651 $a = urlencode( $text );
652 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
653 # leave colons alone, however
654 $a = str_replace( '.3A', ':', $a );
655 return $a;
656 }
657
658 static function special( $parser, $text ) {
659 $title = SpecialPage::getTitleForAlias( $text );
660 if ( $title ) {
661 return $title->getPrefixedText();
662 } else {
663 return wfMsgForContent( 'nosuchspecialpage' );
664 }
665 }
666
667 public static function defaultsort( $parser, $text ) {
668 $text = trim( $text );
669 if( strlen( $text ) == 0 )
670 return '';
671 $old = $parser->getCustomDefaultSort();
672 $parser->setDefaultSort( $text );
673 if( $old === false || $old == $text )
674 return '';
675 else
676 return( '<span class="error">' .
677 wfMsg( 'duplicate-defaultsort',
678 htmlspecialchars( $old ),
679 htmlspecialchars( $text ) ) .
680 '</span>' );
681 }
682
683 public static function filepath( $parser, $name='', $option='' ) {
684 $file = wfFindFile( $name );
685 if( $file ) {
686 $url = $file->getFullUrl();
687 if( $option == 'nowiki' ) {
688 return array( $url, 'nowiki' => true );
689 }
690 return $url;
691 } else {
692 return '';
693 }
694 }
695
696 /**
697 * Parser function to extension tag adaptor
698 */
699 public static function tagObj( $parser, $frame, $args ) {
700 $xpath = false;
701 if ( !count( $args ) ) {
702 return '';
703 }
704 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
705
706 if ( count( $args ) ) {
707 $inner = $frame->expand( array_shift( $args ) );
708 } else {
709 $inner = null;
710 }
711
712 $stripList = $parser->getStripList();
713 if ( !in_array( $tagName, $stripList ) ) {
714 return '<span class="error">' .
715 wfMsg( 'unknown_extension_tag', $tagName ) .
716 '</span>';
717 }
718
719 $attributes = array();
720 foreach ( $args as $arg ) {
721 $bits = $arg->splitArg();
722 if ( strval( $bits['index'] ) === '' ) {
723 $name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
724 $value = trim( $frame->expand( $bits['value'] ) );
725 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
726 $value = isset( $m[1] ) ? $m[1] : '';
727 }
728 $attributes[$name] = $value;
729 }
730 }
731
732 $params = array(
733 'name' => $tagName,
734 'inner' => $inner,
735 'attributes' => $attributes,
736 'close' => "</$tagName>",
737 );
738 return $parser->extensionSubstitution( $params, $frame );
739 }
740 }