registration: Generalize CoreVersionChecker to VersionChecker
authorFlorian Schmidt <florian.schmidt.stargatewissen@gmail.com>
Sat, 3 Dec 2016 18:06:46 +0000 (19:06 +0100)
committerFlorian Schmidt <florian.schmidt.stargatewissen@gmail.com>
Wed, 14 Dec 2016 18:18:09 +0000 (19:18 +0100)
This allows us to put other requirements more easily into extension
registration, such as skins and/or extensions.

Bug: T117277
Change-Id: I3ec1b28b6af380621585cd61b38e5ebb8be9f9c7

autoload.php
includes/registration/CoreVersionChecker.php [deleted file]
includes/registration/ExtensionProcessor.php
includes/registration/ExtensionRegistry.php
includes/registration/VersionChecker.php [new file with mode: 0644]
tests/phpunit/includes/registration/CoreVersionCheckerTest.php [deleted file]
tests/phpunit/includes/registration/VersionCheckerTest.php [new file with mode: 0644]

index e079686..91f7700 100644 (file)
@@ -293,7 +293,6 @@ $wgAutoloadLocalClasses = [
        'CopyJobQueue' => __DIR__ . '/maintenance/copyJobQueue.php',
        'CoreParserFunctions' => __DIR__ . '/includes/parser/CoreParserFunctions.php',
        'CoreTagHooks' => __DIR__ . '/includes/parser/CoreTagHooks.php',
-       'CoreVersionChecker' => __DIR__ . '/includes/registration/CoreVersionChecker.php',
        'CreateAndPromote' => __DIR__ . '/maintenance/createAndPromote.php',
        'CreateFileOp' => __DIR__ . '/includes/libs/filebackend/fileop/CreateFileOp.php',
        'CreditsAction' => __DIR__ . '/includes/actions/CreditsAction.php',
@@ -1523,6 +1522,7 @@ $wgAutoloadLocalClasses = [
        'UzConverter' => __DIR__ . '/languages/classes/LanguageUz.php',
        'VFormHTMLForm' => __DIR__ . '/includes/htmlform/VFormHTMLForm.php',
        'ValidateRegistrationFile' => __DIR__ . '/maintenance/validateRegistrationFile.php',
+       'VersionChecker' => __DIR__ . '/includes/registration/VersionChecker.php',
        'ViewAction' => __DIR__ . '/includes/actions/ViewAction.php',
        'VirtualRESTService' => __DIR__ . '/includes/libs/virtualrest/VirtualRESTService.php',
        'VirtualRESTServiceClient' => __DIR__ . '/includes/libs/virtualrest/VirtualRESTServiceClient.php',
diff --git a/includes/registration/CoreVersionChecker.php b/includes/registration/CoreVersionChecker.php
deleted file mode 100644 (file)
index f64d826..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-<?php
-
-/**
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- */
-
-use Composer\Semver\VersionParser;
-use Composer\Semver\Constraint\Constraint;
-
-/**
- * @since 1.26
- */
-class CoreVersionChecker {
-
-       /**
-        * @var Constraint|bool representing $wgVersion
-        */
-       private $coreVersion = false;
-
-       /**
-        * @var VersionParser
-        */
-       private $versionParser;
-
-       /**
-        * @param string $coreVersion Current version of core
-        */
-       public function __construct( $coreVersion ) {
-               $this->versionParser = new VersionParser();
-               try {
-                       $this->coreVersion = new Constraint(
-                               '==',
-                               $this->versionParser->normalize( $coreVersion )
-                       );
-               } catch ( UnexpectedValueException $e ) {
-                       // Non-parsable version, don't fatal.
-               }
-       }
-
-       /**
-        * Check that the provided constraint is compatible with the current version of core
-        *
-        * @param string $constraint Something like ">= 1.26"
-        * @return bool
-        */
-       public function check( $constraint ) {
-               if ( $this->coreVersion === false ) {
-                       // Couldn't parse the core version, so we can't check anything
-                       return true;
-               }
-
-               return $this->versionParser->parseConstraints( $constraint )
-                       ->matches( $this->coreVersion );
-       }
-}
index d967132..1212f99 100644 (file)
@@ -216,13 +216,7 @@ class ExtensionProcessor implements Processor {
        }
 
        public function getRequirements( array $info ) {
-               $requirements = [];
-               $key = ExtensionRegistry::MEDIAWIKI_CORE;
-               if ( isset( $info['requires'][$key] ) ) {
-                       $requirements[$key] = $info['requires'][$key];
-               }
-
-               return $requirements;
+               return isset( $info['requires'] ) ? $info['requires'] : [];
        }
 
        protected function extractHooks( array $info ) {
index 70dc624..0521f3b 100644 (file)
@@ -204,7 +204,8 @@ class ExtensionRegistry {
                $autoloaderPaths = [];
                $processor = new ExtensionProcessor();
                $incompatible = [];
-               $coreVersionParser = new CoreVersionChecker( $wgVersion );
+               $versionParser = new VersionChecker();
+               $versionParser->setCoreVersion( $wgVersion );
                foreach ( $queue as $path => $mtime ) {
                        $json = file_get_contents( $path );
                        if ( $json === false ) {
@@ -217,14 +218,14 @@ class ExtensionRegistry {
 
                        // Check any constraints against MediaWiki core
                        $requires = $processor->getRequirements( $info );
-                       if ( isset( $requires[self::MEDIAWIKI_CORE] )
-                               && !$coreVersionParser->check( $requires[self::MEDIAWIKI_CORE] )
-                       ) {
-                               // Doesn't match, mark it as incompatible.
-                               $incompatible[] = "{$info['name']} is not compatible with the current "
-                                       . "MediaWiki core (version {$wgVersion}), it requires: " . $requires[self::MEDIAWIKI_CORE]
-                                       . '.';
-                               continue;
+                       if ( $requires ) {
+                               $versionCheck = $versionParser->checkArray(
+                                       [ $info['name'] => $requires ]
+                               );
+                               $incompatible = array_merge( $incompatible, $versionCheck );
+                               if ( $versionCheck ) {
+                                       continue;
+                               }
                        }
 
                        if ( !isset( $info['manifest_version'] ) ) {
diff --git a/includes/registration/VersionChecker.php b/includes/registration/VersionChecker.php
new file mode 100644 (file)
index 0000000..b61a10e
--- /dev/null
@@ -0,0 +1,128 @@
+<?php
+
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @author Legoktm
+ * @author Florian Schmidt
+ */
+
+use Composer\Semver\VersionParser;
+use Composer\Semver\Constraint\Constraint;
+
+/**
+ * Provides functions to check a set of extensions with dependencies against
+ * a set of loaded extensions and given version information.
+ *
+ * @since 1.29
+ */
+class VersionChecker {
+       /**
+        * @var Constraint|bool representing $wgVersion
+        */
+       private $coreVersion = false;
+
+       /**
+        * @var VersionParser
+        */
+       private $versionParser;
+
+       public function __construct() {
+               $this->versionParser = new VersionParser();
+       }
+
+       /**
+        * Set MediaWiki core version.
+        *
+        * @param string $coreVersion Current version of core
+        * @return VersionChecker $this
+        */
+       public function setCoreVersion( $coreVersion ) {
+               try {
+                       $this->coreVersion = new Constraint(
+                               '==',
+                               $this->versionParser->normalize( $coreVersion )
+                       );
+                       $this->coreVersion->setPrettyString( $coreVersion );
+               } catch ( UnexpectedValueException $e ) {
+                       // Non-parsable version, don't fatal.
+               }
+
+               return $this;
+       }
+
+       /**
+        * Check all given dependencies if they are compatible with the named
+        * installed extensions in the $credits array.
+        *
+        * Example $extDependencies:
+        *      {
+        *              'GoogleAPIClient' => {
+        *                      'MediaWiki' => '>= 1.25.0'
+        *              }
+        *      }
+        *
+        * @param array $extDependencies All extensions that depend on other ones
+        * @return array
+        */
+       public function checkArray( array $extDependencies ) {
+               $errors = [];
+               foreach ( $extDependencies as $extension => $dependencies ) {
+                       foreach ( $dependencies as $dependencyType => $values ) {
+                               switch ( $dependencyType ) {
+                                       case ExtensionRegistry::MEDIAWIKI_CORE:
+                                               $errors = array_merge(
+                                                       $errors,
+                                                       $this->handleMediaWikiDependency( $values, $extension )
+                                               );
+                                               break;
+                                       default:
+                                               throw new UnexpectedValueException( 'Dependency type ' . $dependencyType .
+                                                       ' unknown in ' . $extension );
+                               }
+                       }
+               }
+
+               return $errors;
+       }
+
+       /**
+        * Handle a dependency to MediaWiki core. It will check, if a MediaWiki version constraint was
+        * set with self::setCoreVersion before this call (if not, it will return an empty array) and
+        * checks the version constraint given against it.
+        *
+        * @param string $constraint The required version constraint for this dependency
+        * @param string $checkedExt The Extension, which depends on this dependency
+        * @return array An empty array, if MediaWiki version is compatible with $constraint, an array
+        *  with an error message, otherwise.
+        */
+       private function handleMediaWikiDependency( $constraint, $checkedExt ) {
+               if ( $this->coreVersion === false ) {
+                       // Couldn't parse the core version, so we can't check anything
+                       return [];
+               }
+
+               // if the installed and required version are compatible, return an empty array
+               if ( $this->versionParser->parseConstraints( $constraint )
+                       ->matches( $this->coreVersion ) ) {
+                       return [];
+               }
+               // otherwise mark this as incompatible.
+               return [ "{$checkedExt} is not compatible with the current "
+                        . "MediaWiki core (version {$this->coreVersion->getPrettyString()}), it requires: "
+                        . $constraint . '.' ];
+       }
+}
diff --git a/tests/phpunit/includes/registration/CoreVersionCheckerTest.php b/tests/phpunit/includes/registration/CoreVersionCheckerTest.php
deleted file mode 100644 (file)
index 1dfcd82..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-<?php
-
-/**
- * @covers CoreVersionChecker
- */
-class CoreVersionCheckerTest extends PHPUnit_Framework_TestCase {
-       /**
-        * @dataProvider provideCheck
-        */
-       public function testCheck( $coreVersion, $constraint, $expected ) {
-               $checker = new CoreVersionChecker( $coreVersion );
-               $this->assertEquals( $expected, $checker->check( $constraint ) );
-       }
-
-       public static function provideCheck() {
-               return [
-                       // [ $wgVersion, constraint, expected ]
-                       [ '1.25alpha', '>= 1.26', false ],
-                       [ '1.25.0', '>= 1.26', false ],
-                       [ '1.26alpha', '>= 1.26', true ],
-                       [ '1.26alpha', '>= 1.26.0', true ],
-                       [ '1.26alpha', '>= 1.26.0-stable', false ],
-                       [ '1.26.0', '>= 1.26.0-stable', true ],
-                       [ '1.26.1', '>= 1.26.0-stable', true ],
-                       [ '1.27.1', '>= 1.26.0-stable', true ],
-                       [ '1.26alpha', '>= 1.26.1', false ],
-                       [ '1.26alpha', '>= 1.26alpha', true ],
-                       [ '1.26alpha', '>= 1.25', true ],
-                       [ '1.26.0-alpha.14', '>= 1.26.0-alpha.15', false ],
-                       [ '1.26.0-alpha.14', '>= 1.26.0-alpha.10', true ],
-                       [ '1.26.1', '>= 1.26.2, <=1.26.0', false ],
-                       [ '1.26.1', '^1.26.2', false ],
-                       // Accept anything for un-parsable version strings
-                       [ '1.26mwf14', '== 1.25alpha', true ],
-                       [ 'totallyinvalid', '== 1.0', true ],
-               ];
-       }
-}
diff --git a/tests/phpunit/includes/registration/VersionCheckerTest.php b/tests/phpunit/includes/registration/VersionCheckerTest.php
new file mode 100644 (file)
index 0000000..daa407f
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+/**
+ * @covers CoreVersionChecker
+ */
+class CoreVersionCheckerTest extends PHPUnit_Framework_TestCase {
+       /**
+        * @dataProvider provideCheck
+        */
+       public function testCheck( $coreVersion, $constraint, $expected ) {
+               $checker = new VersionChecker();
+               $checker->setCoreVersion( $coreVersion );
+               $this->assertEquals( $expected, !(bool)$checker->checkArray( [
+                       'FakeExtension' => [
+                               'MediaWiki' => $constraint,
+                       ],
+               ] )
+               );
+       }
+
+       public static function provideCheck() {
+               return [
+                       // [ $wgVersion, constraint, expected ]
+                       [ '1.25alpha', '>= 1.26', false ],
+                       [ '1.25.0', '>= 1.26', false ],
+                       [ '1.26alpha', '>= 1.26', true ],
+                       [ '1.26alpha', '>= 1.26.0', true ],
+                       [ '1.26alpha', '>= 1.26.0-stable', false ],
+                       [ '1.26.0', '>= 1.26.0-stable', true ],
+                       [ '1.26.1', '>= 1.26.0-stable', true ],
+                       [ '1.27.1', '>= 1.26.0-stable', true ],
+                       [ '1.26alpha', '>= 1.26.1', false ],
+                       [ '1.26alpha', '>= 1.26alpha', true ],
+                       [ '1.26alpha', '>= 1.25', true ],
+                       [ '1.26.0-alpha.14', '>= 1.26.0-alpha.15', false ],
+                       [ '1.26.0-alpha.14', '>= 1.26.0-alpha.10', true ],
+                       [ '1.26.1', '>= 1.26.2, <=1.26.0', false ],
+                       [ '1.26.1', '^1.26.2', false ],
+                       // Accept anything for un-parsable version strings
+                       [ '1.26mwf14', '== 1.25alpha', true ],
+                       [ 'totallyinvalid', '== 1.0', true ],
+               ];
+       }
+}