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