Merge "Update namespace aliases for Luri (lrc) from translatewiki"
[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 $data = array( 'globals' => array( 'wgAutoloadClasses' => array() ) );
120 $autoloadClasses = array();
121 $processor = new ExtensionProcessor();
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 $processor->extractInfo( $path, $info );
136 }
137 $data = $processor->getExtractedInfo();
138 // Need to set this so we can += to it later
139 $data['globals']['wgAutoloadClasses'] = array();
140 foreach ( $data['credits'] as $credit ) {
141 $data['globals']['wgExtensionCredits'][$credit['type']][] = $credit;
142 }
143 $data['autoload'] = $autoloadClasses;
144 return $data;
145 }
146
147 protected function exportExtractedData( array $info ) {
148 foreach ( $info['globals'] as $key => $val ) {
149 if ( !isset( $GLOBALS[$key] ) || !$GLOBALS[$key] ) {
150 $GLOBALS[$key] = $val;
151 } elseif ( $key === 'wgHooks' || $key === 'wgExtensionCredits' ) {
152 // Special case $wgHooks and $wgExtensionCredits, which require a recursive merge.
153 // Ideally it would have been taken care of in the first if block though.
154 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
155 } elseif ( $key === 'wgGroupPermissions' ) {
156 // First merge individual groups
157 foreach ( $GLOBALS[$key] as $name => &$groupVal ) {
158 if ( isset( $val[$name] ) ) {
159 $groupVal += $val[$name];
160 }
161 }
162 // Now merge groups that didn't exist yet
163 $GLOBALS[$key] += $val;
164 } elseif ( is_array( $GLOBALS[$key] ) && is_array( $val ) ) {
165 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
166 } // else case is a config setting where it has already been overriden, so don't set it
167 }
168 foreach ( $info['defines'] as $name => $val ) {
169 define( $name, $val );
170 }
171 foreach ( $info['callbacks'] as $cb ) {
172 call_user_func( $cb );
173 }
174
175 $this->loaded += $info['credits'];
176
177 if ( $info['attributes'] ) {
178 if ( !$this->attributes ) {
179 $this->attributes = $info['attributes'];
180 } else {
181 $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
182 }
183 }
184 }
185
186 /**
187 * Loads and processes the given JSON file without delay
188 *
189 * If some extensions are already queued, this will load
190 * those as well.
191 *
192 * @param string $path Absolute path to the JSON file
193 */
194 public function load( $path ) {
195 $this->loadFromQueue(); // First clear the queue
196 $this->queue( $path );
197 $this->loadFromQueue();
198 }
199
200 /**
201 * Whether a thing has been loaded
202 * @param string $name
203 * @return bool
204 */
205 public function isLoaded( $name ) {
206 return isset( $this->loaded[$name] );
207 }
208
209 /**
210 * @param string $name
211 * @return array
212 */
213 public function getAttribute( $name ) {
214 if ( isset( $this->attributes[$name] ) ) {
215 return $this->attributes[$name];
216 } else {
217 return array();
218 }
219 }
220
221 /**
222 * Get information about all things
223 *
224 * @return array
225 */
226 public function getAllThings() {
227 return $this->loaded;
228 }
229
230 /**
231 * Mark a thing as loaded
232 *
233 * @param string $name
234 * @param array $credits
235 */
236 protected function markLoaded( $name, array $credits ) {
237 $this->loaded[$name] = $credits;
238 }
239
240 /**
241 * Register classes with the autoloader
242 *
243 * @param string $dir
244 * @param array $info
245 * @return array
246 */
247 protected function processAutoLoader( $dir, array $info ) {
248 if ( isset( $info['AutoloadClasses'] ) ) {
249 // Make paths absolute, relative to the JSON file
250 return array_map( function( $file ) use ( $dir ) {
251 return "$dir/$file";
252 }, $info['AutoloadClasses'] );
253 } else {
254 return array();
255 }
256 }
257 }