(bug 8247) Minor tweaks to includes/CoreParserFunctions.php and parser test for ANCHO...
[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 return $parser->getFunctionLang()->convertPlural( $text, $arg0, $arg1, $arg2, $arg3, $arg4 );
97 }
98
99 static function displaytitle( $parser, $param = '' ) {
100 $parserOptions = new ParserOptions;
101 $local_parser = clone $parser;
102 $t2 = $local_parser->parse ( $param, $parser->mTitle, $parserOptions, false );
103 $parser->mOutput->mHTMLtitle = $t2->GetText();
104
105 # Add subtitle
106 $t = $parser->mTitle->getPrefixedText();
107 $parser->mOutput->mSubtitle .= wfMsg('displaytitle', $t);
108 return '';
109 }
110
111 static function isRaw( $param ) {
112 static $mwRaw;
113 if ( !$mwRaw ) {
114 $mwRaw =& MagicWord::get( 'rawsuffix' );
115 }
116 if ( is_null( $param ) ) {
117 return false;
118 } else {
119 return $mwRaw->match( $param );
120 }
121 }
122
123 static function statisticsFunction( $func, $raw = null ) {
124 if ( self::isRaw( $raw ) ) {
125 return call_user_func( array( 'SiteStats', $func ) );
126 } else {
127 global $wgContLang;
128 return $wgContLang->formatNum( call_user_func( array( 'SiteStats', $func ) ) );
129 }
130 }
131
132 static function numberofpages( $parser, $raw = null ) { return self::statisticsFunction( 'pages', $raw ); }
133 static function numberofusers( $parser, $raw = null ) { return self::statisticsFunction( 'users', $raw ); }
134 static function numberofarticles( $parser, $raw = null ) { return self::statisticsFunction( 'articles', $raw ); }
135 static function numberoffiles( $parser, $raw = null ) { return self::statisticsFunction( 'images', $raw ); }
136 static function numberofadmins( $parser, $raw = null ) { return self::statisticsFunction( 'admins', $raw ); }
137
138 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
139 $count = SiteStats::pagesInNs( intval( $namespace ) );
140 if ( self::isRaw( $raw ) ) {
141 global $wgContLang;
142 return $wgContLang->formatNum( $count );
143 } else {
144 return $count;
145 }
146 }
147
148 static function language( $parser, $arg = '' ) {
149 global $wgContLang;
150 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
151 return $lang != '' ? $lang : $arg;
152 }
153
154 static function pad( $string = '', $length = 0, $char = 0, $direction = STR_PAD_RIGHT ) {
155 $length = min( max( $length, 0 ), 500 );
156 $char = substr( $char, 0, 1 );
157 return ( $string && (int)$length > 0 && strlen( trim( (string)$char ) ) > 0 )
158 ? str_pad( $string, $length, (string)$char, $direction )
159 : $string;
160 }
161
162 static function padleft( $parser, $string = '', $length = 0, $char = 0 ) {
163 return self::pad( $string, $length, $char, STR_PAD_LEFT );
164 }
165
166 static function padright( $parser, $string = '', $length = 0, $char = 0 ) {
167 return self::pad( $string, $length, $char );
168 }
169
170 static function anchorencode( $parser, $text ) {
171 return strtr( urlencode( $text ) , array( '%' => '.' , '+' => '_' ) );
172 }
173
174 static function special( $parser, $text ) {
175 $title = SpecialPage::getTitleForAlias( $text );
176 if ( $title ) {
177 return $title->getPrefixedText();
178 } else {
179 return wfMsgForContent( 'nosuchspecialpage' );
180 }
181 }
182 }
183
184 ?>