1b08e82bcb0e2522e2428e59a6d1fbd66a08aa08
[lhc/web/wiklou.git] / includes / CoreParserFunctions.php
1 <?php
2
3 /**
4 * Various core parser functions, registered in Parser::firstCallInit()
5 */
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 );
14 }
15 }
16
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;
29 }
30 }
31 if ( $found ) {
32 return $text;
33 } else {
34 return array( 'found' => false );
35 }
36 }
37
38 static function urlencode( $parser, $s = '' ) {
39 return urlencode( $s );
40 }
41
42 static function lcfirst( $parser, $s = '' ) {
43 global $wgContLang;
44 return $wgContLang->lcfirst( $s );
45 }
46
47 static function ucfirst( $parser, $s = '' ) {
48 global $wgContLang;
49 return $wgContLang->ucfirst( $s );
50 }
51
52 static function lc( $parser, $s = '' ) {
53 global $wgContLang;
54 return $wgContLang->lc( $s );
55 }
56
57 static function uc( $parser, $s = '' ) {
58 global $wgContLang;
59 return $wgContLang->uc( $s );
60 }
61
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 ); }
66
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();
80 }
81 return $text;
82 } else {
83 return array( 'found' => false );
84 }
85 }
86
87 static function formatNum( $parser, $num = '' ) {
88 return $parser->getFunctionLang()->formatNum( $num );
89 }
90
91 static function grammar( $parser, $case = '', $word = '' ) {
92 return $parser->getFunctionLang()->convertGrammar( $word, $case );
93 }
94
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 );
98 }
99
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();
105
106 # Add subtitle
107 $t = $parser->mTitle->getPrefixedText();
108 $parser->mOutput->mSubtitle .= wfMsg('displaytitle', $t);
109 return '';
110 }
111
112 static function isRaw( $param ) {
113 static $mwRaw;
114 if ( !$mwRaw ) {
115 $mwRaw =& MagicWord::get( 'rawsuffix' );
116 }
117 if ( is_null( $param ) ) {
118 return false;
119 } else {
120 return $mwRaw->match( $param );
121 }
122 }
123
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 ) ) );
130 }
131 }
132
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
139 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
140 $count = SiteStats::pagesInNs( intval( $namespace ) );
141 if ( self::isRaw( $raw ) ) {
142 global $wgContLang;
143 return $wgContLang->formatNum( $count );
144 } else {
145 return $count;
146 }
147 }
148
149 static function language( $parser, $arg = '' ) {
150 global $wgContLang;
151 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
152 return $lang != '' ? $lang : $arg;
153 }
154
155 static function pad( $string = '', $length = 0, $char = 0, $direction = STR_PAD_RIGHT ) {
156 $length = min( max( $length, 0 ), 500 );
157 $char = substr( $char, 0, 1 );
158 return ( $string && (int)$length > 0 && strlen( trim( (string)$char ) ) > 0 )
159 ? str_pad( $string, $length, (string)$char, $direction )
160 : $string;
161 }
162
163 static function padleft( $parser, $string = '', $length = 0, $char = 0 ) {
164 return self::pad( $string, $length, $char, STR_PAD_LEFT );
165 }
166
167 static function padright( $parser, $string = '', $length = 0, $char = 0 ) {
168 return self::pad( $string, $length, $char );
169 }
170
171 static function anchorencode( $parser, $text ) {
172 return strtr( urlencode( $text ) , array( '%' => '.' , '+' => '_' ) );
173 }
174
175 static function special( $parser, $text ) {
176 $title = SpecialPage::getTitleForAlias( $text );
177 if ( $title ) {
178 return $title->getPrefixedText();
179 } else {
180 return wfMsgForContent( 'nosuchspecialpage' );
181 }
182 }
183
184 public static function defaultsort( $parser, $text ) {
185 $text = trim( $text );
186 if( strlen( $text ) > 0 )
187 $parser->setDefaultSort( $text );
188 return '';
189 }
190 }
191 ?>