(bug 37183) Removed hard coded parentheses in SpecialListfiles.php
[lhc/web/wiklou.git] / includes / Init.php
1 <?php
2 /**
3 * Some functions that are useful during startup.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Some functions that are useful during startup.
25 */
26 class MWInit {
27 static $compilerVersion;
28
29 /**
30 * Get the version of HipHop used to compile, or false if MediaWiki was not
31 * compiled. This works by having our build script insert a special function
32 * into the compiled code.
33 */
34 static function getCompilerVersion() {
35 if ( self::$compilerVersion === null ) {
36 if ( self::functionExists( 'wfHipHopCompilerVersion' ) ) {
37 self::$compilerVersion = wfHipHopCompilerVersion();
38 } else {
39 self::$compilerVersion = false;
40 }
41 }
42 return self::$compilerVersion;
43 }
44
45 /**
46 * Returns true if we are running under HipHop, whether in compiled or
47 * interpreted mode.
48 *
49 * @return bool
50 */
51 static function isHipHop() {
52 return function_exists( 'hphp_thread_set_warmup_enabled' );
53 }
54
55 /**
56 * Get a fully-qualified path for a source file relative to $IP. Including
57 * such a path under HipHop will force the file to be interpreted. This is
58 * useful for configuration files.
59 *
60 * @param $file string
61 *
62 * @return string
63 */
64 static function interpretedPath( $file ) {
65 global $IP;
66 return "$IP/$file";
67 }
68
69 /**
70 * If we are running code compiled by HipHop, this will pass through the
71 * input path, assumed to be relative to $IP. If the code is interpreted,
72 * it will converted to a fully qualified path. It is necessary to use a
73 * path which is relative to $IP in order to make HipHop use its compiled
74 * code.
75 *
76 * @param $file string
77 *
78 * @return string
79 */
80 static function compiledPath( $file ) {
81 global $IP;
82
83 if ( defined( 'MW_COMPILED' ) ) {
84 return "phase3/$file";
85 } else {
86 return "$IP/$file";
87 }
88 }
89
90 /**
91 * The equivalent of MWInit::interpretedPath() but for files relative to the
92 * extensions directory.
93 *
94 * @param $file string
95 * @return string
96 */
97 static function extInterpretedPath( $file ) {
98 return self::getExtensionsDirectory() . '/' . $file;
99 }
100
101 /**
102 * The equivalent of MWInit::compiledPath() but for files relative to the
103 * extensions directory. Any files referenced in this way must be registered
104 * for compilation by including them in $wgCompiledFiles.
105 * @param $file string
106 * @return string
107 */
108 static function extCompiledPath( $file ) {
109 if ( defined( 'MW_COMPILED' ) ) {
110 return "extensions/$file";
111 } else {
112 return self::getExtensionsDirectory() . '/' . $file;
113 }
114 }
115
116 /**
117 * Register an extension setup file and return its path for compiled
118 * inclusion. Use this function in LocalSettings.php to add extensions
119 * to the build. For example:
120 *
121 * require( MWInit::extSetupPath( 'ParserFunctions/ParserFunctions.php' ) );
122 *
123 * @param $extRel string The path relative to the extensions directory, as defined by
124 * $wgExtensionsDirectory.
125 *
126 * @return string
127 */
128 static function extSetupPath( $extRel ) {
129 $baseRel = "extensions/$extRel";
130 if ( defined( 'MW_COMPILED' ) ) {
131 return $baseRel;
132 } else {
133 global $wgCompiledFiles;
134 $wgCompiledFiles[] = $baseRel;
135 return self::getExtensionsDirectory() . '/' . $extRel;
136 }
137 }
138
139 /**
140 * @return bool|string
141 */
142 static function getExtensionsDirectory() {
143 global $wgExtensionsDirectory, $IP;
144 if ( $wgExtensionsDirectory === false ) {
145 $wgExtensionsDirectory = "$IP/../extensions";
146 }
147 return $wgExtensionsDirectory;
148 }
149
150 /**
151 * Determine whether a class exists, using a method which works under HipHop.
152 *
153 * Note that it's not possible to implement this with any variant of
154 * class_exists(), because class_exists() returns false for classes which
155 * are compiled in.
156 *
157 * Calling class_exists() on a literal string causes the class to be made
158 * "volatile", which means (as of March 2011) that the class is broken and
159 * can't be used at all. So don't do that. See
160 * https://github.com/facebook/hiphop-php/issues/314
161 *
162 * @param $class string
163 *
164 * @return bool
165 */
166 static function classExists( $class ) {
167 try {
168 $r = new ReflectionClass( $class );
169 } catch( ReflectionException $r ) {
170 $r = false;
171 }
172 return $r !== false;
173 }
174
175 /**
176 * Determine wether a method exists within a class, using a method which works
177 * under HipHop.
178 *
179 * Note that under HipHop when method_exists is given a string for it's class
180 * such as to test for a static method has the same issues as class_exists does.
181 *
182 * @param $class string
183 * @param $method string
184 *
185 * @return bool
186 */
187 static function methodExists( $class, $method ) {
188 try {
189 $r = new ReflectionMethod( $class, $method );
190 } catch( ReflectionException $r ) {
191 $r = false;
192 }
193 return $r !== false;
194 }
195
196 /**
197 * Determine whether a function exists, using a method which works under
198 * HipHop.
199 *
200 * @param $function string
201 *
202 * @return bool
203 */
204 static function functionExists( $function ) {
205 try {
206 $r = new ReflectionFunction( $function );
207 } catch( ReflectionException $r ) {
208 $r = false;
209 }
210 return $r !== false;
211 }
212
213 /**
214 * Call a static method of a class with variable arguments without causing
215 * it to become volatile.
216 * @param $className string
217 * @param $methodName string
218 * @param $args array
219 *
220 * @return mixed
221 */
222 static function callStaticMethod( $className, $methodName, $args ) {
223 $r = new ReflectionMethod( $className, $methodName );
224 return $r->invokeArgs( null, $args );
225 }
226 }