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