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