registration: Remove unused variable
[lhc/web/wiklou.git] / includes / registration / ExtensionRegistry.php
1 <?php
2
3 /**
4 * ExtensionRegistry class
5 *
6 * The Registry loads JSON files, and uses a Processor
7 * to extract information from them. It also registers
8 * classes with the autoloader.
9 *
10 * @since 1.25
11 */
12 class ExtensionRegistry {
13
14 /**
15 * @var BagOStuff
16 */
17 protected $cache;
18
19 /**
20 * Array of loaded things, keyed by name, values are credits information
21 *
22 * @var array
23 */
24 private $loaded = array();
25
26 /**
27 * List of paths that should be loaded
28 *
29 * @var array
30 */
31 protected $queued = array();
32
33 /**
34 * Items in the JSON file that aren't being
35 * set as globals
36 *
37 * @var array
38 */
39 protected $attributes = array();
40
41 /**
42 * @var ExtensionRegistry
43 */
44 private static $instance;
45
46 /**
47 * @return ExtensionRegistry
48 */
49 public static function getInstance() {
50 if ( self::$instance === null ) {
51 self::$instance = new self();
52 }
53
54 return self::$instance;
55 }
56
57 public function __construct() {
58 // We use a try/catch instead of the $fallback parameter because
59 // we don't want to fail here if $wgObjectCaches is not configured
60 // properly for APC setup
61 try {
62 $this->cache = ObjectCache::newAccelerator( array() );
63 } catch ( MWException $e ) {
64 $this->cache = new EmptyBagOStuff();
65 }
66 }
67
68 /**
69 * @param string $path Absolute path to the JSON file
70 */
71 public function queue( $path ) {
72 global $wgExtensionInfoMTime;
73
74 $mtime = $wgExtensionInfoMTime;
75 if ( $mtime === false ) {
76 if ( file_exists( $path ) ) {
77 $mtime = filemtime( $path );
78 } else {
79 throw new Exception( "$path does not exist!" );
80 }
81 if ( !$mtime ) {
82 $err = error_get_last();
83 throw new Exception( "Couldn't stat $path: {$err['message']}" );
84 }
85 }
86 $this->queued[$path] = $mtime;
87 }
88
89 public function loadFromQueue() {
90 if ( !$this->queued ) {
91 return;
92 }
93
94 // See if this queue is in APC
95 $key = wfMemcKey( 'registration', md5( json_encode( $this->queued ) ) );
96 $data = $this->cache->get( $key );
97 if ( $data ) {
98 $this->exportExtractedData( $data );
99 } else {
100 $data = $this->readFromQueue( $this->queued );
101 $this->exportExtractedData( $data );
102 // Do this late since we don't want to extract it since we already
103 // did that, but it should be cached
104 $data['globals']['wgAutoloadClasses'] += $data['autoload'];
105 unset( $data['autoload'] );
106 $this->cache->set( $key, $data, 60 * 60 * 24 );
107 }
108 $this->queued = array();
109 }
110
111 /**
112 * Process a queue of extensions and return their extracted data
113 *
114 * @param array $queue keys are filenames, values are ignored
115 * @return array extracted info
116 * @throws Exception
117 */
118 public function readFromQueue( array $queue ) {
119 $autoloadClasses = array();
120 $processor = new ExtensionProcessor();
121 foreach ( $queue as $path => $mtime ) {
122 $json = file_get_contents( $path );
123 if ( $json === false ) {
124 throw new Exception( "Unable to read $path, does it exist?" );
125 }
126 $info = json_decode( $json, /* $assoc = */ true );
127 if ( !is_array( $info ) ) {
128 throw new Exception( "$path is not a valid JSON file." );
129 }
130 $autoload = $this->processAutoLoader( dirname( $path ), $info );
131 // Set up the autoloader now so custom processors will work
132 $GLOBALS['wgAutoloadClasses'] += $autoload;
133 $autoloadClasses += $autoload;
134 $processor->extractInfo( $path, $info );
135 }
136 $data = $processor->getExtractedInfo();
137 // Need to set this so we can += to it later
138 $data['globals']['wgAutoloadClasses'] = array();
139 foreach ( $data['credits'] as $credit ) {
140 $data['globals']['wgExtensionCredits'][$credit['type']][] = $credit;
141 }
142 $data['autoload'] = $autoloadClasses;
143 return $data;
144 }
145
146 protected function exportExtractedData( array $info ) {
147 foreach ( $info['globals'] as $key => $val ) {
148 if ( !isset( $GLOBALS[$key] ) || !$GLOBALS[$key] ) {
149 $GLOBALS[$key] = $val;
150 } elseif ( $key === 'wgHooks' || $key === 'wgExtensionCredits' ) {
151 // Special case $wgHooks and $wgExtensionCredits, which require a recursive merge.
152 // Ideally it would have been taken care of in the first if block though.
153 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
154 } elseif ( $key === 'wgGroupPermissions' ) {
155 // First merge individual groups
156 foreach ( $GLOBALS[$key] as $name => &$groupVal ) {
157 if ( isset( $val[$name] ) ) {
158 $groupVal += $val[$name];
159 }
160 }
161 // Now merge groups that didn't exist yet
162 $GLOBALS[$key] += $val;
163 } elseif ( is_array( $GLOBALS[$key] ) && is_array( $val ) ) {
164 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
165 } // else case is a config setting where it has already been overriden, so don't set it
166 }
167 foreach ( $info['defines'] as $name => $val ) {
168 define( $name, $val );
169 }
170 foreach ( $info['callbacks'] as $cb ) {
171 call_user_func( $cb );
172 }
173
174 $this->loaded += $info['credits'];
175
176 if ( $info['attributes'] ) {
177 if ( !$this->attributes ) {
178 $this->attributes = $info['attributes'];
179 } else {
180 $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
181 }
182 }
183 }
184
185 /**
186 * Loads and processes the given JSON file without delay
187 *
188 * If some extensions are already queued, this will load
189 * those as well.
190 *
191 * @param string $path Absolute path to the JSON file
192 */
193 public function load( $path ) {
194 $this->loadFromQueue(); // First clear the queue
195 $this->queue( $path );
196 $this->loadFromQueue();
197 }
198
199 /**
200 * Whether a thing has been loaded
201 * @param string $name
202 * @return bool
203 */
204 public function isLoaded( $name ) {
205 return isset( $this->loaded[$name] );
206 }
207
208 /**
209 * @param string $name
210 * @return array
211 */
212 public function getAttribute( $name ) {
213 if ( isset( $this->attributes[$name] ) ) {
214 return $this->attributes[$name];
215 } else {
216 return array();
217 }
218 }
219
220 /**
221 * Get information about all things
222 *
223 * @return array
224 */
225 public function getAllThings() {
226 return $this->loaded;
227 }
228
229 /**
230 * Mark a thing as loaded
231 *
232 * @param string $name
233 * @param array $credits
234 */
235 protected function markLoaded( $name, array $credits ) {
236 $this->loaded[$name] = $credits;
237 }
238
239 /**
240 * Register classes with the autoloader
241 *
242 * @param string $dir
243 * @param array $info
244 * @return array
245 */
246 protected function processAutoLoader( $dir, array $info ) {
247 if ( isset( $info['AutoloadClasses'] ) ) {
248 // Make paths absolute, relative to the JSON file
249 return array_map( function( $file ) use ( $dir ) {
250 return "$dir/$file";
251 }, $info['AutoloadClasses'] );
252 } else {
253 return array();
254 }
255 }
256 }