MediaWiki REL1_28
ExtensionProcessor.php
Go to the documentation of this file.
1<?php
2
3class ExtensionProcessor implements Processor {
4
10 protected static $globalSettings = [
11 'ResourceLoaderSources',
12 'ResourceLoaderLESSVars',
13 'DefaultUserOptions',
14 'HiddenPrefs',
15 'GroupPermissions',
16 'RevokePermissions',
17 'GrantPermissions',
18 'GrantPermissionGroups',
19 'ImplicitGroups',
20 'GroupsAddToSelf',
21 'GroupsRemoveFromSelf',
22 'AddGroups',
23 'RemoveGroups',
24 'AvailableRights',
25 'ContentHandlers',
26 'ConfigRegistry',
27 'SessionProviders',
28 'AuthManagerAutoConfig',
29 'CentralIdLookupProviders',
30 'ChangeCredentialsBlacklist',
31 'RemoveCredentialsBlacklist',
32 'RateLimits',
33 'RecentChangesFlags',
34 'MediaHandlers',
35 'ExtensionFunctions',
36 'ExtensionEntryPointListFiles',
37 'SpecialPages',
38 'JobClasses',
39 'LogTypes',
40 'LogRestrictions',
41 'FilterLogTypes',
42 'ActionFilteredLogs',
43 'LogNames',
44 'LogHeaders',
45 'LogActions',
46 'LogActionsHandlers',
47 'Actions',
48 'APIModules',
49 'APIFormatModules',
50 'APIMetaModules',
51 'APIPropModules',
52 'APIListModules',
53 'ValidSkinNames',
54 'FeedClasses',
55 ];
56
64 protected static $mergeStrategies = [
65 'wgGroupPermissions' => 'array_plus_2d',
66 'wgRevokePermissions' => 'array_plus_2d',
67 'wgGrantPermissions' => 'array_plus_2d',
68 'wgHooks' => 'array_merge_recursive',
69 'wgExtensionCredits' => 'array_merge_recursive',
70 'wgExtraGenderNamespaces' => 'array_plus',
71 'wgNamespacesWithSubpages' => 'array_plus',
72 'wgNamespaceContentModels' => 'array_plus',
73 'wgNamespaceProtection' => 'array_plus',
74 'wgCapitalLinkOverrides' => 'array_plus',
75 'wgRateLimits' => 'array_plus_2d',
76 'wgAuthManagerAutoConfig' => 'array_plus_2d',
77 ];
78
84 protected static $creditsAttributes = [
85 'name',
86 'namemsg',
87 'author',
88 'version',
89 'url',
90 'description',
91 'descriptionmsg',
92 'license-name',
93 ];
94
101 protected static $notAttributes = [
102 'callback',
103 'Hooks',
104 'namespaces',
105 'ResourceFileModulePaths',
106 'ResourceModules',
107 'ResourceModuleSkinStyles',
108 'ExtensionMessagesFiles',
109 'MessagesDirs',
110 'type',
111 'config',
112 'config_prefix',
113 'ServiceWiringFiles',
114 'ParserTestFiles',
115 'AutoloadClasses',
116 'manifest_version',
117 'load_composer_autoloader',
118 ];
119
127 protected $globals = [
128 'wgExtensionMessagesFiles' => [],
129 'wgMessagesDirs' => [],
130 ];
131
137 protected $defines = [];
138
145 protected $callbacks = [];
146
150 protected $credits = [];
151
158 protected $attributes = [];
159
166 public function extractInfo( $path, array $info, $version ) {
167 $this->extractConfig( $info );
168 $this->extractHooks( $info );
169 $dir = dirname( $path );
170 $this->extractExtensionMessagesFiles( $dir, $info );
171 $this->extractMessagesDirs( $dir, $info );
172 $this->extractNamespaces( $info );
173 $this->extractResourceLoaderModules( $dir, $info );
174 $this->extractServiceWiringFiles( $dir, $info );
175 $this->extractParserTestFiles( $dir, $info );
176 $name = $this->extractCredits( $path, $info );
177 if ( isset( $info['callback'] ) ) {
178 $this->callbacks[$name] = $info['callback'];
179 }
180
181 foreach ( $info as $key => $val ) {
182 if ( in_array( $key, self::$globalSettings ) ) {
183 $this->storeToArray( $path, "wg$key", $val, $this->globals );
184 // Ignore anything that starts with a @
185 } elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes )
186 && !in_array( $key, self::$creditsAttributes )
187 ) {
188 $this->storeToArray( $path, $key, $val, $this->attributes );
189 }
190 }
191 }
192
193 public function getExtractedInfo() {
194 // Make sure the merge strategies are set
195 foreach ( $this->globals as $key => $val ) {
196 if ( isset( self::$mergeStrategies[$key] ) ) {
197 $this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::$mergeStrategies[$key];
198 }
199 }
200
201 return [
202 'globals' => $this->globals,
203 'defines' => $this->defines,
204 'callbacks' => $this->callbacks,
205 'credits' => $this->credits,
206 'attributes' => $this->attributes,
207 ];
208 }
209
210 public function getRequirements( array $info ) {
211 $requirements = [];
213 if ( isset( $info['requires'][$key] ) ) {
214 $requirements[$key] = $info['requires'][$key];
215 }
216
217 return $requirements;
218 }
219
220 protected function extractHooks( array $info ) {
221 if ( isset( $info['Hooks'] ) ) {
222 foreach ( $info['Hooks'] as $name => $value ) {
223 if ( is_array( $value ) ) {
224 foreach ( $value as $callback ) {
225 $this->globals['wgHooks'][$name][] = $callback;
226 }
227 } else {
228 $this->globals['wgHooks'][$name][] = $value;
229 }
230 }
231 }
232 }
233
239 protected function extractNamespaces( array $info ) {
240 if ( isset( $info['namespaces'] ) ) {
241 foreach ( $info['namespaces'] as $ns ) {
242 if ( defined( $ns['constant'] ) ) {
243 // If the namespace constant is already defined, use it.
244 // This allows namespace IDs to be overwritten locally.
245 $id = constant( $ns['constant'] );
246 } else {
247 $id = $ns['id'];
248 $this->defines[ $ns['constant'] ] = $id;
249 }
250
251 if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
252 // If it is not conditional, register it
253 $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
254 }
255 if ( isset( $ns['gender'] ) ) {
256 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
257 }
258 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
259 $this->globals['wgNamespacesWithSubpages'][$id] = true;
260 }
261 if ( isset( $ns['content'] ) && $ns['content'] ) {
262 $this->globals['wgContentNamespaces'][] = $id;
263 }
264 if ( isset( $ns['defaultcontentmodel'] ) ) {
265 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
266 }
267 if ( isset( $ns['protection'] ) ) {
268 $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
269 }
270 if ( isset( $ns['capitallinkoverride'] ) ) {
271 $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
272 }
273 }
274 }
275 }
276
277 protected function extractResourceLoaderModules( $dir, array $info ) {
278 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
279 ? $info['ResourceFileModulePaths']
280 : false;
281 if ( isset( $defaultPaths['localBasePath'] ) ) {
282 if ( $defaultPaths['localBasePath'] === '' ) {
283 // Avoid double slashes (e.g. /extensions/Example//path)
284 $defaultPaths['localBasePath'] = $dir;
285 } else {
286 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
287 }
288 }
289
290 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
291 if ( isset( $info[$setting] ) ) {
292 foreach ( $info[$setting] as $name => $data ) {
293 if ( isset( $data['localBasePath'] ) ) {
294 if ( $data['localBasePath'] === '' ) {
295 // Avoid double slashes (e.g. /extensions/Example//path)
296 $data['localBasePath'] = $dir;
297 } else {
298 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
299 }
300 }
301 if ( $defaultPaths ) {
302 $data += $defaultPaths;
303 }
304 $this->globals["wg$setting"][$name] = $data;
305 }
306 }
307 }
308 }
309
310 protected function extractExtensionMessagesFiles( $dir, array $info ) {
311 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
312 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
313 return "$dir/$file";
314 }, $info['ExtensionMessagesFiles'] );
315 }
316 }
317
325 protected function extractMessagesDirs( $dir, array $info ) {
326 if ( isset( $info['MessagesDirs'] ) ) {
327 foreach ( $info['MessagesDirs'] as $name => $files ) {
328 foreach ( (array)$files as $file ) {
329 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
330 }
331 }
332 }
333 }
334
341 protected function extractCredits( $path, array $info ) {
342 $credits = [
343 'path' => $path,
344 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
345 ];
346 foreach ( self::$creditsAttributes as $attr ) {
347 if ( isset( $info[$attr] ) ) {
348 $credits[$attr] = $info[$attr];
349 }
350 }
351
352 $name = $credits['name'];
353
354 // If someone is loading the same thing twice, throw
355 // a nice error (T121493)
356 if ( isset( $this->credits[$name] ) ) {
357 $firstPath = $this->credits[$name]['path'];
358 $secondPath = $credits['path'];
359 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
360 }
361
362 $this->credits[$name] = $credits;
363 $this->globals['wgExtensionCredits'][$credits['type']][] = $credits;
364
365 return $name;
366 }
367
374 protected function extractConfig( array $info ) {
375 if ( isset( $info['config'] ) ) {
376 if ( isset( $info['config']['_prefix'] ) ) {
377 $prefix = $info['config']['_prefix'];
378 unset( $info['config']['_prefix'] );
379 } else {
380 $prefix = 'wg';
381 }
382 foreach ( $info['config'] as $key => $val ) {
383 if ( $key[0] !== '@' ) {
384 $this->globals["$prefix$key"] = $val;
385 }
386 }
387 }
388 }
389
390 protected function extractServiceWiringFiles( $dir, array $info ) {
391 if ( isset( $info['ServiceWiringFiles'] ) ) {
392 foreach ( $info['ServiceWiringFiles'] as $path ) {
393 $this->globals['wgServiceWiringFiles'][] = "$dir/$path";
394 }
395 }
396 }
397
398 protected function extractParserTestFiles( $dir, array $info ) {
399 if ( isset( $info['ParserTestFiles'] ) ) {
400 foreach ( $info['ParserTestFiles'] as $path ) {
401 $this->globals['wgParserTestFiles'][] = "$dir/$path";
402 }
403 }
404 }
405
413 protected function storeToArray( $path, $name, $value, &$array ) {
414 if ( !is_array( $value ) ) {
415 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
416 }
417 if ( isset( $array[$name] ) ) {
418 $array[$name] = array_merge_recursive( $array[$name], $value );
419 } else {
420 $array[$name] = $value;
421 }
422 }
423
424 public function getExtraAutoloaderPaths( $dir, array $info ) {
425 $paths = [];
426 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
427 $path = "$dir/vendor/autoload.php";
428 if ( file_exists( $path ) ) {
429 $paths[] = $path;
430 }
431 }
432 return $paths;
433 }
434}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static array $mergeStrategies
Mapping of global settings to their specific merge strategies.
extractNamespaces(array $info)
Register namespaces with the appropriate global settings.
extractCredits( $path, array $info)
extractServiceWiringFiles( $dir, array $info)
array $attributes
Any thing else in the $info that hasn't already been processed.
callable[] $callbacks
Things to be called once registration of these extensions are done keyed by the name of the extension...
array $defines
Things that should be define()'d.
extractExtensionMessagesFiles( $dir, array $info)
array $globals
Stuff that is going to be set to $GLOBALS.
static array $notAttributes
Things that are not 'attributes', but are not in $globalSettings or $creditsAttributes.
extractMessagesDirs( $dir, array $info)
Set message-related settings, which need to be expanded to use absolute paths.
extractResourceLoaderModules( $dir, array $info)
getExtraAutoloaderPaths( $dir, array $info)
Get the path for additional autoloaders, e.g.
extractConfig(array $info)
Set configuration settings.
extractInfo( $path, array $info, $version)
getRequirements(array $info)
Get the requirements for the provided info.
storeToArray( $path, $name, $value, &$array)
extractParserTestFiles( $dir, array $info)
static array $creditsAttributes
Keys that are part of the extension credits.
static array $globalSettings
Keys that should be set to $GLOBALS.
const MERGE_STRATEGY
Special key that defines the merge strategy.
const MEDIAWIKI_CORE
"requires" key that applies to MediaWiki core/$wgVersion
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining globals
Definition globals.txt:39
the array() calling protocol came about after MediaWiki 1.4rc1.
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:304
if(count( $args)==0) $dir
$files
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Processors read associated arrays and register whatever is required.
Definition Processor.php:9