f22f61ecafd2b9563e75d6c0d798086be907571c
[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
9 static function ns( $parser, $part1 = '' ) {
10 global $wgContLang;
11 $found = false;
12 if ( intval( $part1 ) || $part1 == "0" ) {
13 $text = $wgContLang->getNsText( intval( $part1 ) );
14 $found = true;
15 } else {
16 $param = str_replace( ' ', '_', strtolower( $part1 ) );
17 $index = Namespace::getCanonicalIndex( strtolower( $param ) );
18 if ( !is_null( $index ) ) {
19 $text = $wgContLang->getNsText( $index );
20 $found = true;
21 }
22 }
23 if ( $found ) {
24 return $text;
25 } else {
26 return array( 'found' => false );
27 }
28 }
29
30 static function urlencode( $parser, $s = '' ) {
31 return urlencode( $s );
32 }
33
34 static function lcfirst( $parser, $s = '' ) {
35 global $wgContLang;
36 return $wgContLang->lcfirst( $s );
37 }
38
39 static function ucfirst( $parser, $s = '' ) {
40 global $wgContLang;
41 return $wgContLang->ucfirst( $s );
42 }
43
44 static function lc( $parser, $s = '' ) {
45 global $wgContLang;
46 return $wgContLang->lc( $s );
47 }
48
49 static function uc( $parser, $s = '' ) {
50 global $wgContLang;
51 return $wgContLang->uc( $s );
52 }
53
54 static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
55 static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
56 static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
57 static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
58
59 static function urlFunction( $func, $s = '', $arg = null ) {
60 $found = false;
61 $title = Title::newFromText( $s );
62 # Due to order of execution of a lot of bits, the values might be encoded
63 # before arriving here; if that's true, then the title can't be created
64 # and the variable will fail. If we can't get a decent title from the first
65 # attempt, url-decode and try for a second.
66 if( is_null( $title ) )
67 $title = Title::newFromUrl( urldecode( $s ) );
68 if ( !is_null( $title ) ) {
69 if ( !is_null( $arg ) ) {
70 $text = $title->$func( $arg );
71 } else {
72 $text = $title->$func();
73 }
74 $found = true;
75 }
76 if ( $found ) {
77 return $text;
78 } else {
79 return array( 'found' => false );
80 }
81 }
82
83 function formatNum( $parser, $num = '' ) {
84 return $parser->getFunctionLang()->formatNum( $num );
85 }
86
87 function grammar( $parser, $case = '', $word = '' ) {
88 return $parser->getFunctionLang()->convertGrammar( $word, $case );
89 }
90
91 function plural( $parser, $text = '', $arg0 = null, $arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null ) {
92 return $parser->getFunctionLang()->convertPlural( $text, $arg0, $arg1, $arg2, $arg3, $arg4 );
93 }
94
95 function displaytitle( $parser, $param = '' ) {
96 $parserOptions = new ParserOptions;
97 $local_parser = clone $parser;
98 $t2 = $local_parser->parse ( $param, $parser->mTitle, $parserOptions, false );
99 $parser->mOutput->mHTMLtitle = $t2->GetText();
100
101 # Add subtitle
102 $t = $parser->mTitle->getPrefixedText();
103 $parser->mOutput->mSubtitle .= wfMsg('displaytitle', $t);
104 return '';
105 }
106
107 function isRaw( $param ) {
108 static $mwRaw;
109 if ( !$mwRaw ) {
110 $mwRaw =& MagicWord::get( 'rawsuffix' );
111 }
112 if ( is_null( $param ) ) {
113 return false;
114 } else {
115 return $mwRaw->match( $param );
116 }
117 }
118
119 function statisticsFunction( $func, $raw = null ) {
120 if ( self::isRaw( $raw ) ) {
121 return call_user_func( $func );
122 } else {
123 global $wgContLang;
124 return $wgContLang->formatNum( call_user_func( $func ) );
125 }
126 }
127
128 function numberofpages( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfPages', $raw ); }
129 function numberofusers( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfUsers', $raw ); }
130 function numberofarticles( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfArticles', $raw ); }
131 function numberoffiles( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfFiles', $raw ); }
132 function numberofadmins( $parser, $raw = null ) { return self::statisticsFunction( 'wfNumberOfAdmins', $raw ); }
133
134 function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
135 $count = wfPagesInNs( intval( $namespace ) );
136 if ( self::isRaw( $raw ) ) {
137 global $wgContLang;
138 return $wgContLang->formatNum( $count );
139 } else {
140 return $count;
141 }
142 }
143
144 function language( $parser, $arg = '' ) {
145 global $wgContLang;
146 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
147 return $lang != '' ? $lang : $arg;
148 }
149
150 function padleft( $parser, $string = '', $length = 0, $char = 0 ) {
151 return str_pad( $string, $length, (string)$char, STR_PAD_LEFT );
152 }
153
154 function padright( $parser, $string = '', $length = 0, $char = 0 ) {
155 return str_pad( $string, $length, (string)$char, STR_PAD_RIGHT );
156 }
157
158 }
159
160 ?>