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