HipHop improvements:
[lhc/web/wiklou.git] / includes / Init.php
1 <?php
2
3 /**
4 * Some functions that are useful during startup.
5 */
6 class MWInit {
7 static $compilerVersion;
8
9 /**
10 * Get the version of HipHop used to compile, or false if MediaWiki was not
11 * compiled. This works by having our build script insert a special function
12 * into the compiled code.
13 */
14 static function getCompilerVersion() {
15 if ( self::$compilerVersion === null ) {
16 if ( self::functionExists( 'wfHipHopCompilerVersion' ) ) {
17 self::$compilerVersion = wfHipHopCompilerVersion();
18 } else {
19 self::$compilerVersion = false;
20 }
21 }
22 return self::$compilerVersion;
23 }
24
25 /**
26 * Returns true if we are running under HipHop, whether in compiled or
27 * interpreted mode.
28 *
29 * @return bool
30 */
31 static function isHipHop() {
32 return function_exists( 'hphp_thread_set_warmup_enabled' );
33 }
34
35 /**
36 * Get a fully-qualified path for a source file relative to $IP. Including
37 * such a path under HipHop will force the file to be interpreted. This is
38 * useful for configuration files.
39 *
40 * @param $file string
41 *
42 * @return string
43 */
44 static function interpretedPath( $file ) {
45 global $IP;
46 return "$IP/$file";
47 }
48
49 /**
50 * If we are running code compiled by HipHop, this will pass through the
51 * input path, assumed to be relative to $IP. If the code is interpreted,
52 * it will converted to a fully qualified path. It is necessary to use a
53 * path which is relative to $IP in order to make HipHop use its compiled
54 * code.
55 *
56 * @param $file string
57 *
58 * @return string
59 */
60 static function compiledPath( $file ) {
61 global $IP;
62
63 if ( defined( 'MW_COMPILED' ) ) {
64 return "phase3/$file";
65 } else {
66 return "$IP/$file";
67 }
68 }
69
70 /**
71 * The equivalent of MWInit::interpretedPath() but for files relative to the
72 * extensions directory.
73 */
74 static function extInterpretedPath( $file ) {
75 return getExtensionsDirectory() . '/' . $file;
76 }
77
78 /**
79 * The equivalent of MWInit::compiledPath() but for files relative to the
80 * extensions directory. Any files referenced in this way must be registered
81 * for compilation by including them in $wgCompiledFiles.
82 */
83 static function extCompiledPath( $file ) {
84 if ( defined( 'MW_COMPILED' ) ) {
85 return "extensions/$file";
86 } else {
87 return getExtensionsDirectory() . '/' . $file;
88 }
89 }
90
91 /**
92 * Register an extension setup file and return its path for compiled
93 * inclusion. Use this function in LocalSettings.php to add extensions
94 * to the build. For example:
95 *
96 * require( MWInit::extSetupPath( 'ParserFunctions/ParserFunctions.php' ) );
97 *
98 * @param $path The path relative to the extensions directory, as defined by
99 * $wgExtensionsDirectory.
100 */
101 static function extSetupPath( $extRel ) {
102 $baseRel = "extensions/$extRel";
103 if ( defined( 'MW_COMPILED' ) ) {
104 return $baseRel;
105 } else {
106 global $wgCompiledFiles;
107 $wgCompiledFiles[] = $baseRel;
108 return self::getExtensionsDirectory() . '/' . $extRel;
109 }
110 }
111
112 static function getExtensionsDirectory() {
113 global $wgExtensionsDirectory, $IP;
114 if ( $wgExtensionsDirectory === false ) {
115 $wgExtensionsDirectory = "$IP/../extensions";
116 }
117 return $wgExtensionsDirectory;
118 }
119
120 /**
121 * Determine whether a class exists, using a method which works under HipHop.
122 *
123 * Note that it's not possible to implement this with any variant of
124 * class_exists(), because class_exists() returns false for classes which
125 * are compiled in.
126 *
127 * Calling class_exists() on a literal string causes the class to be made
128 * "volatile", which means (as of March 2011) that the class is broken and
129 * can't be used at all. So don't do that. See
130 * https://github.com/facebook/hiphop-php/issues/314
131 *
132 * @param $class string
133 *
134 * @return bool
135 */
136 static function classExists( $class ) {
137 try {
138 $r = new ReflectionClass( $class );
139 } catch( ReflectionException $r ) {
140 $r = false;
141 }
142 return $r !== false;
143 }
144
145 /**
146 * Determine whether a function exists, using a method which works under
147 * HipHop.
148 *
149 * @param $function string
150 *
151 * @return bool
152 */
153 static function functionExists( $function ) {
154 try {
155 $r = new ReflectionFunction( $function );
156 } catch( ReflectionException $r ) {
157 $r = false;
158 }
159 return $r !== false;
160 }
161
162 /**
163 * Call a static method of a class with variable arguments without causing
164 * it to become volatile.
165 */
166 static function callStaticMethod( $className, $methodName, $args ) {
167 $r = new ReflectionMethod( $className, $methodName );
168 return $r->invokeArgs( null, $args );
169 }
170 }