Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / Init.php
blob66f9544d4736f98dd0a4ac10ac967b3366dc8f38
1 <?php
2 /**
3 * Some functions that are useful during startup.
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.
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.
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
20 * @file
23 /**
24 * Some functions that are useful during startup.
26 class MWInit {
27 static $compilerVersion;
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.
34 static function getCompilerVersion() {
35 if ( self::$compilerVersion === null ) {
36 if ( self::functionExists( 'wfHipHopCompilerVersion' ) ) {
37 self::$compilerVersion = wfHipHopCompilerVersion();
38 } else {
39 self::$compilerVersion = false;
42 return self::$compilerVersion;
45 /**
46 * Returns true if we are running under HipHop, whether in compiled or
47 * interpreted mode.
49 * @return bool
51 static function isHipHop() {
52 return function_exists( 'hphp_thread_set_warmup_enabled' );
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.
60 * @param $file string
62 * @return string
64 static function interpretedPath( $file ) {
65 global $IP;
66 return "$IP/$file";
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.
76 * @param $file string
78 * @return string
80 static function compiledPath( $file ) {
81 global $IP;
83 if ( defined( 'MW_COMPILED' ) ) {
84 return "phase3/$file";
85 } else {
86 return "$IP/$file";
90 /**
91 * The equivalent of MWInit::interpretedPath() but for files relative to the
92 * extensions directory.
94 * @param $file string
95 * @return string
97 static function extInterpretedPath( $file ) {
98 return self::getExtensionsDirectory() . '/' . $file;
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
108 static function extCompiledPath( $file ) {
109 if ( defined( 'MW_COMPILED' ) ) {
110 return "extensions/$file";
111 } else {
112 return self::getExtensionsDirectory() . '/' . $file;
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:
121 * require( MWInit::extSetupPath( 'ParserFunctions/ParserFunctions.php' ) );
123 * @param string $extRel The path relative to the extensions directory, as defined by
124 * $wgExtensionsDirectory.
126 * @return string
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;
140 * @return bool|string
142 static function getExtensionsDirectory() {
143 global $wgExtensionsDirectory, $IP;
144 if ( $wgExtensionsDirectory === false ) {
145 $wgExtensionsDirectory = "$IP/../extensions";
147 return $wgExtensionsDirectory;
151 * Determine whether a class exists, using a method which works under HipHop.
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.
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
162 * @param $class string
164 * @return bool
166 static function classExists( $class ) {
167 try {
168 $r = new ReflectionClass( $class );
169 } catch( ReflectionException $r ) {
170 $r = false;
172 return $r !== false;
176 * Determine whether a method exists within a class, using a method which works
177 * under HipHop.
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.
182 * @param $class string
183 * @param $method string
185 * @return bool
187 static function methodExists( $class, $method ) {
188 try {
189 $r = new ReflectionMethod( $class, $method );
190 } catch( ReflectionException $r ) {
191 $r = false;
193 return $r !== false;
197 * Determine whether a function exists, using a method which works under
198 * HipHop.
200 * @param $function string
202 * @return bool
204 static function functionExists( $function ) {
205 try {
206 $r = new ReflectionFunction( $function );
207 } catch( ReflectionException $r ) {
208 $r = false;
210 return $r !== false;
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
220 * @return mixed
222 static function callStaticMethod( $className, $methodName, $args ) {
223 $r = new ReflectionMethod( $className, $methodName );
224 return $r->invokeArgs( null, $args );