Revert r29671, it was based on a misunderstanding of the purpose of the LoggedOut...
[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 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 = '') {
96 $forms = array_slice( func_get_args(), 2);
97 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
98 return $parser->getFunctionLang()->convertPlural( $text, $forms );
99 }
100
101 /**
102 * Override the title of the page when viewed,
103 * provided we've been given a title which
104 * will normalise to the canonical title
105 *
106 * @param Parser $parser Parent parser
107 * @param string $text Desired title text
108 * @return string
109 */
110 static function displaytitle( $parser, $text = '' ) {
111 $text = trim( Sanitizer::decodeCharReferences( $text ) );
112 $title = Title::newFromText( $text );
113 if( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) )
114 $parser->mOutput->setDisplayTitle( $text );
115 return '';
116 }
117
118 static function isRaw( $param ) {
119 static $mwRaw;
120 if ( !$mwRaw ) {
121 $mwRaw =& MagicWord::get( 'rawsuffix' );
122 }
123 if ( is_null( $param ) ) {
124 return false;
125 } else {
126 return $mwRaw->match( $param );
127 }
128 }
129
130 static function statisticsFunction( $func, $raw = null ) {
131 if ( self::isRaw( $raw ) ) {
132 return call_user_func( array( 'SiteStats', $func ) );
133 } else {
134 global $wgContLang;
135 return $wgContLang->formatNum( call_user_func( array( 'SiteStats', $func ) ) );
136 }
137 }
138
139 static function numberofpages( $parser, $raw = null ) { return self::statisticsFunction( 'pages', $raw ); }
140 static function numberofusers( $parser, $raw = null ) { return self::statisticsFunction( 'users', $raw ); }
141 static function numberofarticles( $parser, $raw = null ) { return self::statisticsFunction( 'articles', $raw ); }
142 static function numberoffiles( $parser, $raw = null ) { return self::statisticsFunction( 'images', $raw ); }
143 static function numberofadmins( $parser, $raw = null ) { return self::statisticsFunction( 'admins', $raw ); }
144 static function numberofedits( $parser, $raw = null ) { return self::statisticsFunction( 'edits', $raw ); }
145
146 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
147 $count = SiteStats::pagesInNs( intval( $namespace ) );
148 if ( self::isRaw( $raw ) ) {
149 global $wgContLang;
150 return $wgContLang->formatNum( $count );
151 } else {
152 return $count;
153 }
154 }
155
156 static function language( $parser, $arg = '' ) {
157 global $wgContLang;
158 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
159 return $lang != '' ? $lang : $arg;
160 }
161
162 static function pad( $string = '', $length = 0, $char = 0, $direction = STR_PAD_RIGHT ) {
163 $length = min( max( $length, 0 ), 500 );
164 $char = substr( $char, 0, 1 );
165 return ( $string !== '' && (int)$length > 0 && strlen( trim( (string)$char ) ) > 0 )
166 ? str_pad( $string, $length, (string)$char, $direction )
167 : $string;
168 }
169
170 static function padleft( $parser, $string = '', $length = 0, $char = 0 ) {
171 return self::pad( $string, $length, $char, STR_PAD_LEFT );
172 }
173
174 static function padright( $parser, $string = '', $length = 0, $char = 0 ) {
175 return self::pad( $string, $length, $char );
176 }
177
178 static function anchorencode( $parser, $text ) {
179 $a = urlencode( $text );
180 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
181 # leave colons alone, however
182 $a = str_replace( '.3A', ':', $a );
183 return $a;
184 }
185
186 static function special( $parser, $text ) {
187 $title = SpecialPage::getTitleForAlias( $text );
188 if ( $title ) {
189 return $title->getPrefixedText();
190 } else {
191 return wfMsgForContent( 'nosuchspecialpage' );
192 }
193 }
194
195 public static function defaultsort( $parser, $text ) {
196 $text = trim( $text );
197 if( strlen( $text ) > 0 )
198 $parser->setDefaultSort( $text );
199 return '';
200 }
201
202 public static function filepath( $parser, $name='', $option='' ) {
203 $file = wfFindFile( $name );
204 if( $file ) {
205 $url = $file->getFullUrl();
206 if( $option == 'nowiki' ) {
207 return "<nowiki>$url</nowiki>";
208 }
209 return $url;
210 } else {
211 return '';
212 }
213 }
214
215 /**
216 * Parser function to extension tag adaptor
217 */
218 public static function tagObj( $parser, $frame, $args ) {
219 $xpath = false;
220 if ( !count( $args ) ) {
221 return '';
222 }
223 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
224 $stripList = $parser->getStripList();
225 if ( !in_array( $tagName, $stripList ) ) {
226 return '<span class="error">' .
227 wfMsg( 'unknown_extension_tag', $tagName ) .
228 '</span>';
229 }
230
231 $lastNumberedNode = false;
232 $attributes = array();
233 foreach ( $args as $arg ) {
234 if ( !$xpath ) {
235 $xpath = new DOMXPath( $arg->ownerDocument );
236 }
237 $names = $xpath->query( 'name', $arg );
238 if ( $names->item( 0 )->hasAttributes() ) {
239 $lastNumberedNode = $arg;
240 } else {
241 $name = $frame->expand( $names->item( 0 ), PPFrame::STRIP_COMMENTS );
242 if ( preg_match( '/^\d+$/', $name ) ) {
243 // For = suppression syntax {{#tag|thing|1=2=3=4}}
244 $lastNumberedNode = $arg;
245 } else {
246 $values = $xpath->query( 'value', $arg );
247 $attributes[$name] = trim( $frame->expand( $values->item( 0 ) ) );
248 }
249 }
250 }
251
252 if ( !$lastNumberedNode ) {
253 $inner = null;
254 } else {
255 $values = $xpath->query( 'value', $lastNumberedNode );
256 $inner = $frame->expand( $values->item( 0 ) );
257 }
258 $params = array(
259 'name' => $tagName,
260 'inner' => $inner,
261 'attributes' => $attributes
262 );
263 return $parser->extensionSubstitution( $params, $frame );
264 }
265 }
266