Fix regression in even sizing of diff columns; forgot to restore a couple bits I...
[mediawiki.git] / includes / CoreParserFunctions.php
blob72ceb45f60aab49b16565832277b63d5543416e1
1 <?php
3 /**
4 * Various core parser functions, registered in Parser::firstCallInit()
5 * @addtogroup Parser
6 */
7 class CoreParserFunctions {
8 static function intFunction( $parser, $part1 = '' /*, ... */ ) {
9 if ( strval( $part1 ) !== '' ) {
10 $args = array_slice( func_get_args(), 2 );
11 return wfMsgReal( $part1, $args, true );
12 } else {
13 return array( 'found' => false );
17 static function ns( $parser, $part1 = '' ) {
18 global $wgContLang;
19 $found = false;
20 if ( intval( $part1 ) || $part1 == "0" ) {
21 $text = $wgContLang->getNsText( intval( $part1 ) );
22 $found = true;
23 } else {
24 $param = str_replace( ' ', '_', strtolower( $part1 ) );
25 $index = Namespace::getCanonicalIndex( strtolower( $param ) );
26 if ( !is_null( $index ) ) {
27 $text = $wgContLang->getNsText( $index );
28 $found = true;
31 if ( $found ) {
32 return $text;
33 } else {
34 return array( 'found' => false );
38 static function urlencode( $parser, $s = '' ) {
39 return urlencode( $s );
42 static function lcfirst( $parser, $s = '' ) {
43 global $wgContLang;
44 return $wgContLang->lcfirst( $s );
47 static function ucfirst( $parser, $s = '' ) {
48 global $wgContLang;
49 return $wgContLang->ucfirst( $s );
52 static function lc( $parser, $s = '' ) {
53 global $wgContLang;
54 return $wgContLang->lc( $s );
57 static function uc( $parser, $s = '' ) {
58 global $wgContLang;
59 return $wgContLang->uc( $s );
62 static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
63 static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
64 static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
65 static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
67 static function urlFunction( $func, $s = '', $arg = null ) {
68 $title = Title::newFromText( $s );
69 # Due to order of execution of a lot of bits, the values might be encoded
70 # before arriving here; if that's true, then the title can't be created
71 # and the variable will fail. If we can't get a decent title from the first
72 # attempt, url-decode and try for a second.
73 if( is_null( $title ) )
74 $title = Title::newFromUrl( urldecode( $s ) );
75 if ( !is_null( $title ) ) {
76 if ( !is_null( $arg ) ) {
77 $text = $title->$func( $arg );
78 } else {
79 $text = $title->$func();
81 return $text;
82 } else {
83 return array( 'found' => false );
87 static function formatNum( $parser, $num = '' ) {
88 return $parser->getFunctionLang()->formatNum( $num );
91 static function grammar( $parser, $case = '', $word = '' ) {
92 return $parser->getFunctionLang()->convertGrammar( $word, $case );
95 static function plural( $parser, $text = '', $arg0 = null, $arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null ) {
96 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
97 return $parser->getFunctionLang()->convertPlural( $text, $arg0, $arg1, $arg2, $arg3, $arg4 );
100 static function displaytitle( $parser, $param = '' ) {
101 $parserOptions = new ParserOptions;
102 $local_parser = clone $parser;
103 $t2 = $local_parser->parse ( $param, $parser->mTitle, $parserOptions, false );
104 $parser->mOutput->mHTMLtitle = $t2->GetText();
106 # Add subtitle
107 $t = $parser->mTitle->getPrefixedText();
108 $parser->mOutput->mSubtitle .= wfMsg('displaytitle', $t);
109 return '';
112 static function isRaw( $param ) {
113 static $mwRaw;
114 if ( !$mwRaw ) {
115 $mwRaw =& MagicWord::get( 'rawsuffix' );
117 if ( is_null( $param ) ) {
118 return false;
119 } else {
120 return $mwRaw->match( $param );
124 static function statisticsFunction( $func, $raw = null ) {
125 if ( self::isRaw( $raw ) ) {
126 return call_user_func( array( 'SiteStats', $func ) );
127 } else {
128 global $wgContLang;
129 return $wgContLang->formatNum( call_user_func( array( 'SiteStats', $func ) ) );
133 static function numberofpages( $parser, $raw = null ) { return self::statisticsFunction( 'pages', $raw ); }
134 static function numberofusers( $parser, $raw = null ) { return self::statisticsFunction( 'users', $raw ); }
135 static function numberofarticles( $parser, $raw = null ) { return self::statisticsFunction( 'articles', $raw ); }
136 static function numberoffiles( $parser, $raw = null ) { return self::statisticsFunction( 'images', $raw ); }
137 static function numberofadmins( $parser, $raw = null ) { return self::statisticsFunction( 'admins', $raw ); }
138 static function numberofedits( $parser, $raw = null ) { return self::statisticsFunction( 'edits', $raw ); }
140 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
141 $count = SiteStats::pagesInNs( intval( $namespace ) );
142 if ( self::isRaw( $raw ) ) {
143 global $wgContLang;
144 return $wgContLang->formatNum( $count );
145 } else {
146 return $count;
150 static function language( $parser, $arg = '' ) {
151 global $wgContLang;
152 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
153 return $lang != '' ? $lang : $arg;
156 static function pad( $string = '', $length = 0, $char = 0, $direction = STR_PAD_RIGHT ) {
157 $length = min( max( $length, 0 ), 500 );
158 $char = substr( $char, 0, 1 );
159 return ( $string && (int)$length > 0 && strlen( trim( (string)$char ) ) > 0 )
160 ? str_pad( $string, $length, (string)$char, $direction )
161 : $string;
164 static function padleft( $parser, $string = '', $length = 0, $char = 0 ) {
165 return self::pad( $string, $length, $char, STR_PAD_LEFT );
168 static function padright( $parser, $string = '', $length = 0, $char = 0 ) {
169 return self::pad( $string, $length, $char );
172 static function anchorencode( $parser, $text ) {
173 $a = urlencode( $text );
174 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
175 # leave colons alone, however
176 $a = str_replace( '.3A', ':', $a );
177 return $a;
180 static function special( $parser, $text ) {
181 $title = SpecialPage::getTitleForAlias( $text );
182 if ( $title ) {
183 return $title->getPrefixedText();
184 } else {
185 return wfMsgForContent( 'nosuchspecialpage' );
189 public static function defaultsort( $parser, $text ) {
190 $text = trim( $text );
191 if( strlen( $text ) > 0 )
192 $parser->setDefaultSort( $text );
193 return '';