Prettify upload form for RTL wikis
[lhc/web/wiklou.git] / includes / CoreParserFunctions.php
1 <?php
2
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 );
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 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
55 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
56 } else {
57 return $wgContLang->lc( $s );
58 }
59 }
60
61 static function uc( $parser, $s = '' ) {
62 global $wgContLang;
63 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
64 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
65 } else {
66 return $wgContLang->uc( $s );
67 }
68 }
69
70 static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
71 static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
72 static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
73 static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
74
75 static function urlFunction( $func, $s = '', $arg = null ) {
76 $title = Title::newFromText( $s );
77 # Due to order of execution of a lot of bits, the values might be encoded
78 # before arriving here; if that's true, then the title can't be created
79 # and the variable will fail. If we can't get a decent title from the first
80 # attempt, url-decode and try for a second.
81 if( is_null( $title ) )
82 $title = Title::newFromUrl( urldecode( $s ) );
83 if ( !is_null( $title ) ) {
84 if ( !is_null( $arg ) ) {
85 $text = $title->$func( $arg );
86 } else {
87 $text = $title->$func();
88 }
89 return $text;
90 } else {
91 return array( 'found' => false );
92 }
93 }
94
95 static function formatNum( $parser, $num = '' ) {
96 return $parser->getFunctionLang()->formatNum( $num );
97 }
98
99 static function grammar( $parser, $case = '', $word = '' ) {
100 return $parser->getFunctionLang()->convertGrammar( $word, $case );
101 }
102
103 static function plural( $parser, $text = '') {
104 $forms = array_slice( func_get_args(), 2);
105 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
106 return $parser->getFunctionLang()->convertPlural( $text, $forms );
107 }
108
109 /**
110 * Override the title of the page when viewed,
111 * provided we've been given a title which
112 * will normalise to the canonical title
113 *
114 * @param Parser $parser Parent parser
115 * @param string $text Desired title text
116 * @return string
117 */
118 static function displaytitle( $parser, $text = '' ) {
119 $text = trim( Sanitizer::decodeCharReferences( $text ) );
120 $title = Title::newFromText( $text );
121 if( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) )
122 $parser->mOutput->setDisplayTitle( $text );
123 return '';
124 }
125
126 static function isRaw( $param ) {
127 static $mwRaw;
128 if ( !$mwRaw ) {
129 $mwRaw =& MagicWord::get( 'rawsuffix' );
130 }
131 if ( is_null( $param ) ) {
132 return false;
133 } else {
134 return $mwRaw->match( $param );
135 }
136 }
137
138 static function statisticsFunction( $func, $raw = null ) {
139 if ( self::isRaw( $raw ) ) {
140 return call_user_func( array( 'SiteStats', $func ) );
141 } else {
142 global $wgContLang;
143 return $wgContLang->formatNum( call_user_func( array( 'SiteStats', $func ) ) );
144 }
145 }
146
147 static function numberofpages( $parser, $raw = null ) { return self::statisticsFunction( 'pages', $raw ); }
148 static function numberofusers( $parser, $raw = null ) { return self::statisticsFunction( 'users', $raw ); }
149 static function numberofarticles( $parser, $raw = null ) { return self::statisticsFunction( 'articles', $raw ); }
150 static function numberoffiles( $parser, $raw = null ) { return self::statisticsFunction( 'images', $raw ); }
151 static function numberofadmins( $parser, $raw = null ) { return self::statisticsFunction( 'admins', $raw ); }
152 static function numberofedits( $parser, $raw = null ) { return self::statisticsFunction( 'edits', $raw ); }
153
154 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
155 $count = SiteStats::pagesInNs( intval( $namespace ) );
156 if ( self::isRaw( $raw ) ) {
157 global $wgContLang;
158 return $wgContLang->formatNum( $count );
159 } else {
160 return $count;
161 }
162 }
163
164 static function language( $parser, $arg = '' ) {
165 global $wgContLang;
166 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
167 return $lang != '' ? $lang : $arg;
168 }
169
170 static function pad( $string = '', $length = 0, $char = 0, $direction = STR_PAD_RIGHT ) {
171 $length = min( max( $length, 0 ), 500 );
172 $char = substr( $char, 0, 1 );
173 return ( $string !== '' && (int)$length > 0 && strlen( trim( (string)$char ) ) > 0 )
174 ? str_pad( $string, $length, (string)$char, $direction )
175 : $string;
176 }
177
178 static function padleft( $parser, $string = '', $length = 0, $char = 0 ) {
179 return self::pad( $string, $length, $char, STR_PAD_LEFT );
180 }
181
182 static function padright( $parser, $string = '', $length = 0, $char = 0 ) {
183 return self::pad( $string, $length, $char );
184 }
185
186 static function anchorencode( $parser, $text ) {
187 $a = urlencode( $text );
188 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
189 # leave colons alone, however
190 $a = str_replace( '.3A', ':', $a );
191 return $a;
192 }
193
194 static function special( $parser, $text ) {
195 $title = SpecialPage::getTitleForAlias( $text );
196 if ( $title ) {
197 return $title->getPrefixedText();
198 } else {
199 return wfMsgForContent( 'nosuchspecialpage' );
200 }
201 }
202
203 public static function defaultsort( $parser, $text ) {
204 $text = trim( $text );
205 if( strlen( $text ) > 0 )
206 $parser->setDefaultSort( $text );
207 return '';
208 }
209
210 public static function filepath( $parser, $name='', $option='' ) {
211 $file = wfFindFile( $name );
212 if( $file ) {
213 $url = $file->getFullUrl();
214 if( $option == 'nowiki' ) {
215 return "<nowiki>$url</nowiki>";
216 }
217 return $url;
218 } else {
219 return '';
220 }
221 }
222
223 /**
224 * Parser function to extension tag adaptor
225 */
226 public static function tagObj( $parser, $frame, $args ) {
227 $xpath = false;
228 if ( !count( $args ) ) {
229 return '';
230 }
231 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
232
233 if ( count( $args ) ) {
234 $inner = $frame->expand( array_shift( $args ) );
235 } else {
236 $inner = null;
237 }
238
239 $stripList = $parser->getStripList();
240 if ( !in_array( $tagName, $stripList ) ) {
241 return '<span class="error">' .
242 wfMsg( 'unknown_extension_tag', $tagName ) .
243 '</span>';
244 }
245
246 $attributes = array();
247 foreach ( $args as $arg ) {
248 $bits = $arg->splitArg();
249 if ( strval( $bits['index'] ) === '' ) {
250 $name = $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS );
251 $value = trim( $frame->expand( $bits['value'] ) );
252 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
253 $value = isset( $m[1] ) ? $m[1] : '';
254 }
255 $attributes[$name] = $value;
256 }
257 }
258
259 $params = array(
260 'name' => $tagName,
261 'inner' => $inner,
262 'attributes' => $attributes,
263 'close' => "</$tagName>",
264 );
265 return $parser->extensionSubstitution( $params, $frame );
266 }
267 }
268