diff options
Diffstat (limited to 'includes/registration')
-rw-r--r-- | includes/registration/ExtensionProcessor.php | 299 | ||||
-rw-r--r-- | includes/registration/ExtensionRegistry.php | 254 | ||||
-rw-r--r-- | includes/registration/Processor.php | 27 |
3 files changed, 580 insertions, 0 deletions
diff --git a/includes/registration/ExtensionProcessor.php b/includes/registration/ExtensionProcessor.php new file mode 100644 index 00000000..7f738661 --- /dev/null +++ b/includes/registration/ExtensionProcessor.php @@ -0,0 +1,299 @@ +<?php + +class ExtensionProcessor implements Processor { + + /** + * Keys that should be set to $GLOBALS + * + * @var array + */ + protected static $globalSettings = array( + 'ResourceLoaderSources', + 'ResourceLoaderLESSVars', + 'ResourceLoaderLESSImportPaths', + 'DefaultUserOptions', + 'HiddenPrefs', + 'GroupPermissions', + 'RevokePermissions', + 'ImplicitGroups', + 'GroupsAddToSelf', + 'GroupsRemoveFromSelf', + 'AddGroups', + 'RemoveGroups', + 'AvailableRights', + 'ContentHandlers', + 'ConfigRegistry', + 'RateLimits', + 'RecentChangesFlags', + 'MediaHandlers', + 'ExtensionFunctions', + 'ExtensionEntryPointListFiles', + 'SpecialPages', + 'JobClasses', + 'LogTypes', + 'LogRestrictions', + 'FilterLogTypes', + 'LogNames', + 'LogHeaders', + 'LogActions', + 'LogActionsHandlers', + 'Actions', + 'APIModules', + 'APIFormatModules', + 'APIMetaModules', + 'APIPropModules', + 'APIListModules', + 'ValidSkinNames', + ); + + /** + * Keys that are part of the extension credits + * + * @var array + */ + protected static $creditsAttributes = array( + 'name', + 'namemsg', + 'author', + 'version', + 'url', + 'description', + 'descriptionmsg', + 'license-name', + ); + + /** + * Things that are not 'attributes', but are not in + * $globalSettings or $creditsAttributes. + * + * @var array + */ + protected static $notAttributes = array( + 'callback', + 'Hooks', + 'namespaces', + 'ResourceFileModulePaths', + 'ResourceModules', + 'ResourceModuleSkinStyles', + 'ExtensionMessagesFiles', + 'MessagesDirs', + 'type', + 'config', + 'ParserTestFiles', + 'AutoloadClasses', + ); + + /** + * Stuff that is going to be set to $GLOBALS + * + * Some keys are pre-set to arrays so we can += to them + * + * @var array + */ + protected $globals = array( + 'wgExtensionMessagesFiles' => array(), + 'wgMessagesDirs' => array(), + ); + + /** + * Things that should be define()'d + * + * @var array + */ + protected $defines = array(); + + /** + * Things to be called once registration of these extensions are done + * + * @var callable[] + */ + protected $callbacks = array(); + + /** + * @var array + */ + protected $credits = array(); + + /** + * Any thing else in the $info that hasn't + * already been processed + * + * @var array + */ + protected $attributes = array(); + + /** + * @param string $path + * @param array $info + * @return array + */ + public function extractInfo( $path, array $info ) { + $this->extractConfig( $info ); + $this->extractHooks( $info ); + $dir = dirname( $path ); + $this->extractExtensionMessagesFiles( $dir, $info ); + $this->extractMessagesDirs( $dir, $info ); + $this->extractNamespaces( $info ); + $this->extractResourceLoaderModules( $dir, $info ); + $this->extractParserTestFiles( $dir, $info ); + if ( isset( $info['callback'] ) ) { + $this->callbacks[] = $info['callback']; + } + + $this->extractCredits( $path, $info ); + foreach ( $info as $key => $val ) { + if ( in_array( $key, self::$globalSettings ) ) { + $this->storeToArray( "wg$key", $val, $this->globals ); + // Ignore anything that starts with a @ + } elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes ) + && !in_array( $key, self::$creditsAttributes ) + ) { + $this->storeToArray( $key, $val, $this->attributes ); + } + } + } + + public function getExtractedInfo() { + return array( + 'globals' => $this->globals, + 'defines' => $this->defines, + 'callbacks' => $this->callbacks, + 'credits' => $this->credits, + 'attributes' => $this->attributes, + ); + } + + protected function extractHooks( array $info ) { + if ( isset( $info['Hooks'] ) ) { + foreach ( $info['Hooks'] as $name => $callable ) { + $this->globals['wgHooks'][$name][] = $callable; + } + } + } + + /** + * Register namespaces with the appropriate global settings + * + * @param array $info + */ + protected function extractNamespaces( array $info ) { + if ( isset( $info['namespaces'] ) ) { + foreach ( $info['namespaces'] as $ns ) { + $id = $ns['id']; + $this->defines[$ns['constant']] = $id; + $this->globals['wgExtraNamespaces'][$id] = $ns['name']; + if ( isset( $ns['gender'] ) ) { + $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender']; + } + if ( isset( $ns['subpages'] ) && $ns['subpages'] ) { + $this->globals['wgNamespacesWithSubpages'][$id] = true; + } + if ( isset( $ns['content'] ) && $ns['content'] ) { + $this->globals['wgContentNamespaces'][] = $id; + } + if ( isset( $ns['defaultcontentmodel'] ) ) { + $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel']; + } + } + } + } + + protected function extractResourceLoaderModules( $dir, array $info ) { + $defaultPaths = isset( $info['ResourceFileModulePaths'] ) + ? $info['ResourceFileModulePaths'] + : false; + if ( isset( $defaultPaths['localBasePath'] ) ) { + $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}"; + } + + foreach ( array( 'ResourceModules', 'ResourceModuleSkinStyles' ) as $setting ) { + if ( isset( $info[$setting] ) ) { + foreach ( $info[$setting] as $name => $data ) { + if ( isset( $data['localBasePath'] ) ) { + $data['localBasePath'] = "$dir/{$data['localBasePath']}"; + } + if ( $defaultPaths ) { + $data += $defaultPaths; + } + $this->globals["wg$setting"][$name] = $data; + } + } + } + } + + protected function extractExtensionMessagesFiles( $dir, array $info ) { + if ( isset( $info['ExtensionMessagesFiles'] ) ) { + $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) { + return "$dir/$file"; + }, $info['ExtensionMessagesFiles'] ); + } + } + + /** + * Set message-related settings, which need to be expanded to use + * absolute paths + * + * @param string $dir + * @param array $info + */ + protected function extractMessagesDirs( $dir, array $info ) { + if ( isset( $info['MessagesDirs'] ) ) { + foreach ( $info['MessagesDirs'] as $name => $files ) { + foreach ( (array)$files as $file ) { + $this->globals["wgMessagesDirs"][$name][] = "$dir/$file"; + } + } + } + } + + protected function extractCredits( $path, array $info ) { + $credits = array( + 'path' => $path, + 'type' => isset( $info['type'] ) ? $info['type'] : 'other', + ); + foreach ( self::$creditsAttributes as $attr ) { + if ( isset( $info[$attr] ) ) { + $credits[$attr] = $info[$attr]; + } + } + + $this->credits[$credits['name']] = $credits; + } + + /** + * Set configuration settings + * @todo In the future, this should be done via Config interfaces + * + * @param array $info + */ + protected function extractConfig( array $info ) { + if ( isset( $info['config'] ) ) { + foreach ( $info['config'] as $key => $val ) { + if ( $key[0] !== '@' ) { + $this->globals["wg$key"] = $val; + } + } + } + } + + protected function extractParserTestFiles( $dir, array $info ) { + if ( isset( $info['ParserTestFiles'] ) ) { + foreach ( $info['ParserTestFiles'] as $path ) { + $this->globals['wgParserTestFiles'][] = "$dir/$path"; + } + } + } + + /** + * @param string $name + * @param mixed $value + * @param array &$array + */ + protected function storeToArray( $name, $value, &$array ) { + if ( isset( $array[$name] ) ) { + $array[$name] = array_merge_recursive( $array[$name], $value ); + } else { + $array[$name] = $value; + } + } +} diff --git a/includes/registration/ExtensionRegistry.php b/includes/registration/ExtensionRegistry.php new file mode 100644 index 00000000..2558f7e2 --- /dev/null +++ b/includes/registration/ExtensionRegistry.php @@ -0,0 +1,254 @@ +<?php + +/** + * ExtensionRegistry class + * + * The Registry loads JSON files, and uses a Processor + * to extract information from them. It also registers + * classes with the autoloader. + * + * @since 1.25 + */ +class ExtensionRegistry { + + /** + * @var BagOStuff + */ + protected $cache; + + /** + * Array of loaded things, keyed by name, values are credits information + * + * @var array + */ + private $loaded = array(); + + /** + * List of paths that should be loaded + * + * @var array + */ + protected $queued = array(); + + /** + * Items in the JSON file that aren't being + * set as globals + * + * @var array + */ + protected $attributes = array(); + + /** + * @var ExtensionRegistry + */ + private static $instance; + + /** + * @return ExtensionRegistry + */ + public static function getInstance() { + if ( self::$instance === null ) { + self::$instance = new self(); + } + + return self::$instance; + } + + public function __construct() { + // We use a try/catch instead of the $fallback parameter because + // we don't want to fail here if $wgObjectCaches is not configured + // properly for APC setup + try { + $this->cache = ObjectCache::newAccelerator( array() ); + } catch ( MWException $e ) { + $this->cache = new EmptyBagOStuff(); + } + } + + /** + * @param string $path Absolute path to the JSON file + */ + public function queue( $path ) { + global $wgExtensionInfoMTime; + + $mtime = $wgExtensionInfoMTime; + if ( $mtime === false ) { + if ( file_exists( $path ) ) { + $mtime = filemtime( $path ); + } else { + throw new Exception( "$path does not exist!" ); + } + if ( !$mtime ) { + $err = error_get_last(); + throw new Exception( "Couldn't stat $path: {$err['message']}" ); + } + } + $this->queued[$path] = $mtime; + } + + public function loadFromQueue() { + if ( !$this->queued ) { + return; + } + + // See if this queue is in APC + $key = wfMemcKey( 'registration', md5( json_encode( $this->queued ) ) ); + $data = $this->cache->get( $key ); + if ( $data ) { + $this->exportExtractedData( $data ); + } else { + $data = $this->readFromQueue( $this->queued ); + $this->exportExtractedData( $data ); + // Do this late since we don't want to extract it since we already + // did that, but it should be cached + $data['globals']['wgAutoloadClasses'] += $data['autoload']; + unset( $data['autoload'] ); + $this->cache->set( $key, $data, 60 * 60 * 24 ); + } + $this->queued = array(); + } + + /** + * Process a queue of extensions and return their extracted data + * + * @param array $queue keys are filenames, values are ignored + * @return array extracted info + * @throws Exception + */ + public function readFromQueue( array $queue ) { + $data = array( 'globals' => array( 'wgAutoloadClasses' => array() ) ); + $autoloadClasses = array(); + $processor = new ExtensionProcessor(); + foreach ( $queue as $path => $mtime ) { + $json = file_get_contents( $path ); + $info = json_decode( $json, /* $assoc = */ true ); + if ( !is_array( $info ) ) { + throw new Exception( "$path is not a valid JSON file." ); + } + $autoload = $this->processAutoLoader( dirname( $path ), $info ); + // Set up the autoloader now so custom processors will work + $GLOBALS['wgAutoloadClasses'] += $autoload; + $autoloadClasses += $autoload; + $processor->extractInfo( $path, $info ); + } + $data = $processor->getExtractedInfo(); + // Need to set this so we can += to it later + $data['globals']['wgAutoloadClasses'] = array(); + foreach ( $data['credits'] as $credit ) { + $data['globals']['wgExtensionCredits'][$credit['type']][] = $credit; + } + $data['autoload'] = $autoloadClasses; + return $data; + } + + protected function exportExtractedData( array $info ) { + foreach ( $info['globals'] as $key => $val ) { + if ( !isset( $GLOBALS[$key] ) || !$GLOBALS[$key] ) { + $GLOBALS[$key] = $val; + } elseif ( $key === 'wgHooks' || $key === 'wgExtensionCredits' ) { + // Special case $wgHooks and $wgExtensionCredits, which require a recursive merge. + // Ideally it would have been taken care of in the first if block though. + $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val ); + } elseif ( $key === 'wgGroupPermissions' ) { + // First merge individual groups + foreach ( $GLOBALS[$key] as $name => &$groupVal ) { + if ( isset( $val[$name] ) ) { + $groupVal += $val[$name]; + } + } + // Now merge groups that didn't exist yet + $GLOBALS[$key] += $val; + } elseif ( is_array( $GLOBALS[$key] ) && is_array( $val ) ) { + $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] ); + } // else case is a config setting where it has already been overriden, so don't set it + } + foreach ( $info['defines'] as $name => $val ) { + define( $name, $val ); + } + foreach ( $info['callbacks'] as $cb ) { + call_user_func( $cb ); + } + + $this->loaded += $info['credits']; + + if ( $info['attributes'] ) { + if ( !$this->attributes ) { + $this->attributes = $info['attributes']; + } else { + $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] ); + } + } + } + + /** + * Loads and processes the given JSON file without delay + * + * If some extensions are already queued, this will load + * those as well. + * + * @param string $path Absolute path to the JSON file + */ + public function load( $path ) { + $this->loadFromQueue(); // First clear the queue + $this->queue( $path ); + $this->loadFromQueue(); + } + + /** + * Whether a thing has been loaded + * @param string $name + * @return bool + */ + public function isLoaded( $name ) { + return isset( $this->loaded[$name] ); + } + + /** + * @param string $name + * @return array + */ + public function getAttribute( $name ) { + if ( isset( $this->attributes[$name] ) ) { + return $this->attributes[$name]; + } else { + return array(); + } + } + + /** + * Get information about all things + * + * @return array + */ + public function getAllThings() { + return $this->loaded; + } + + /** + * Mark a thing as loaded + * + * @param string $name + * @param array $credits + */ + protected function markLoaded( $name, array $credits ) { + $this->loaded[$name] = $credits; + } + + /** + * Register classes with the autoloader + * + * @param string $dir + * @param array $info + * @return array + */ + protected function processAutoLoader( $dir, array $info ) { + if ( isset( $info['AutoloadClasses'] ) ) { + // Make paths absolute, relative to the JSON file + return array_map( function( $file ) use ( $dir ) { + return "$dir/$file"; + }, $info['AutoloadClasses'] ); + } else { + return array(); + } + } +} diff --git a/includes/registration/Processor.php b/includes/registration/Processor.php new file mode 100644 index 00000000..e930fd3e --- /dev/null +++ b/includes/registration/Processor.php @@ -0,0 +1,27 @@ +<?php + +/** + * Processors read associated arrays and register + * whatever is required + * + * @since 1.25 + */ +interface Processor { + + /** + * Main entry point, processes the information + * provided. + * Callers should call "callback" after calling + * this function. + * + * @param string $path Absolute path of JSON file + * @param array $info + * @return array "credits" information to store + */ + public function extractInfo( $path, array $info ); + + /** + * @return array With 'globals', 'defines', 'callbacks', 'credits' keys. + */ + public function getExtractedInfo(); +} |