Sort the list of skins in My Preferences --> Skins by alphabetical order using asort...
[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 ic( $parser, $s = '' ) {
63 /*Testing showed that ucwords does NOT convert the rest of the words to lowercase
64 * Converting it all to lowercase first fixes this. */
65 $s = strtolower($s);
66 return ucwords($s);
67 }
68
69 static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
70 static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
71 static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
72 static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
73
74 static function urlFunction( $func, $s = '', $arg = null ) {
75 $found = false;
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 $found = true;
90 }
91 if ( $found ) {
92 return $text;
93 } else {
94 return array( 'found' => false );
95 }
96 }
97
98 function formatNum( $parser, $num = '' ) {
99 return $parser->getFunctionLang()->formatNum( $num );
100 }
101
102 function grammar( $parser, $case = '', $word = '' ) {
103 return $parser->getFunctionLang()->convertGrammar( $word, $case );
104 }
105
106 function plural( $parser, $text = '', $arg0 = null, $arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null ) {
107 return $parser->getFunctionLang()->convertPlural( $text, $arg0, $arg1, $arg2, $arg3, $arg4 );
108 }
109
110 function displaytitle( $parser, $param = '' ) {
111 $parserOptions = new ParserOptions;
112 $local_parser = clone $parser;
113 $t2 = $local_parser->parse ( $param, $parser->mTitle, $parserOptions, false );
114 $parser->mOutput->mHTMLtitle = $t2->GetText();
115
116 # Add subtitle
117 $t = $parser->mTitle->getPrefixedText();
118 $parser->mOutput->mSubtitle .= wfMsg('displaytitle', $t);
119 return '';
120 }
121
122 function isRaw( $param ) {
123 static $mwRaw;
124 if ( !$mwRaw ) {
125 $mwRaw =& MagicWord::get( 'rawsuffix' );
126 }
127 if ( is_null( $param ) ) {
128 return false;
129 } else {
130 return $mwRaw->match( $param );
131 }
132 }
133
134 function statisticsFunction( $func, $raw = null ) {
135 if ( self::isRaw( $raw ) ) {
136 return call_user_func( $func );
137 } else {
138 global $wgContLang;
139 return $wgContLang->formatNum( call_user_func( $func ) );
140 }
141 }
142
143 function numberofpages( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfPages', $raw ); }
144 function numberofusers( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfUsers', $raw ); }
145 function numberofarticles( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfArticles', $raw ); }
146 function numberoffiles( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfFiles', $raw ); }
147 function numberofadmins( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfAdmins', $raw ); }
148
149 function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
150 $count = wfPagesInNs( intval( $namespace ) );
151 if ( self::isRaw( $raw ) ) {
152 global $wgContLang;
153 return $wgContLang->formatNum( $count );
154 } else {
155 return $count;
156 }
157 }
158
159 function language( $parser, $arg = '' ) {
160 global $wgContLang;
161 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
162 return $lang != '' ? $lang : $arg;
163 }
164
165 function pad( $string = '', $length = 0, $char = 0, $direction = STR_PAD_RIGHT ) {
166 $length = min( max( $length, 0 ), 500 );
167 $char = substr( $char, 0, 1 );
168 return ( $string && (int)$length > 0 && strlen( trim( (string)$char ) ) > 0 )
169 ? str_pad( $string, $length, (string)$char, $direction )
170 : $string;
171 }
172
173 function padleft( $parser, $string = '', $length = 0, $char = 0 ) {
174 return self::pad( $string, $length, $char, STR_PAD_LEFT );
175 }
176
177 function padright( $parser, $string = '', $length = 0, $char = 0 ) {
178 return self::pad( $string, $length, $char );
179 }
180
181 function anchorencode( $parser, $text ) {
182 return str_replace( '%', '.', str_replace('+', '_', urlencode( $text ) ) );
183 }
184
185 function special( $parser, $text ) {
186 $title = SpecialPage::getTitleForAlias( $text );
187 if ( $title ) {
188 return $title->getPrefixedText();
189 } else {
190 return wfMsgForContent( 'nosuchspecialpage' );
191 }
192 }
193 }
194
195 ?>