registration: Version the extension.json schema
[lhc/web/wiklou.git] / includes / registration / ExtensionRegistry.php
index d938f07..1c36407 100644 (file)
  */
 class ExtensionRegistry {
 
+       /**
+        * Version of the highest supported manifest version
+        */
+       const MANIFEST_VERSION = 1;
+
+       /**
+        * Version of the oldest supported manifest version
+        */
+       const OLDEST_MANIFEST_VERSION = 1;
+
        /**
         * @var BagOStuff
         */
@@ -38,13 +48,6 @@ class ExtensionRegistry {
         */
        protected $attributes = array();
 
-       /**
-        * Processors, 'default' should be set by subclasses in the constructor
-        *
-        * @var Processor[]
-        */
-       protected $processors = array();
-
        /**
         * @var ExtensionRegistry
         */
@@ -77,10 +80,18 @@ class ExtensionRegistry {
         */
        public function queue( $path ) {
                global $wgExtensionInfoMTime;
-               if ( $wgExtensionInfoMTime !== false ) {
-                       $mtime = $wgExtensionInfoMTime;
-               } else {
-                       $mtime = filemtime( $path );
+
+               $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;
        }
@@ -90,8 +101,6 @@ class ExtensionRegistry {
                        return;
                }
 
-               $this->queued = array_unique( $this->queued );
-
                // See if this queue is in APC
                $key = wfMemcKey( 'registration', md5( json_encode( $this->queued ) ) );
                $data = $this->cache->get( $key );
@@ -104,7 +113,7 @@ class ExtensionRegistry {
                        // did that, but it should be cached
                        $data['globals']['wgAutoloadClasses'] += $data['autoload'];
                        unset( $data['autoload'] );
-                       $this->cache->set( $key, $data );
+                       $this->cache->set( $key, $data, 60 * 60 * 24 );
                }
                $this->queued = array();
        }
@@ -119,6 +128,7 @@ class ExtensionRegistry {
        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 );
                        if ( $json === false ) {
@@ -128,40 +138,30 @@ class ExtensionRegistry {
                        if ( !is_array( $info ) ) {
                                throw new Exception( "$path is not a valid JSON file." );
                        }
+                       if ( !isset( $info['manifest_version' ] ) ) {
+                               // For backwards-compatability, assume a version of 1
+                               $info['manifest_version'] = 1;
+                       }
+                       $version = $info['manifest_version'];
+                       if ( $version < self::OLDEST_MANIFEST_VERSION || $version > self::MANIFEST_VERSION ) {
+                               throw new Exception( "$path: unsupported manifest_version: {$version}" );
+                       }
                        $autoload = $this->processAutoLoader( dirname( $path ), $info );
                        // Set up the autoloader now so custom processors will work
                        $GLOBALS['wgAutoloadClasses'] += $autoload;
                        $autoloadClasses += $autoload;
-                       if ( isset( $info['processor'] ) ) {
-                               $processor = $this->getProcessor( $info['processor'] );
-                       } else {
-                               $processor = $this->getProcessor( 'default' );
-                       }
-                       $processor->extractInfo( $path, $info );
-               }
-               foreach ( $this->processors as $processor ) {
-                       $data = array_merge_recursive( $data, $processor->getExtractedInfo() );
+                       $processor->extractInfo( $path, $info, $version );
                }
+               $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;
                }
-               $this->processors = array(); // Reset
                $data['autoload'] = $autoloadClasses;
                return $data;
        }
 
-       protected function getProcessor( $type ) {
-               if ( !isset( $this->processors[$type] ) ) {
-                       $processor = $type === 'default' ? new ExtensionProcessor() : new $type();
-                       if ( !$processor instanceof Processor ) {
-                               throw new Exception( "$type is not a Processor" );
-                       }
-                       $this->processors[$type] = $processor;
-               }
-
-               return $this->processors[$type];
-       }
-
        protected function exportExtractedData( array $info ) {
                foreach ( $info['globals'] as $key => $val ) {
                        if ( !isset( $GLOBALS[$key] ) || !$GLOBALS[$key] ) {