registration: Let extensions add PHP extension requirements
authorMGChecker <hgasuser@gmail.com>
Mon, 24 Sep 2018 21:55:16 +0000 (23:55 +0200)
committerMGChecker <hgasuser@gmail.com>
Sun, 30 Sep 2018 17:55:57 +0000 (19:55 +0200)
This change adds the possibility to specify ext-* keys under the 'platform'
key introduced in I6744cc0be2 to require given PHP extensions. Note that
it's impossible to add constraints different from '*', as there is no universal
way to retrieve PHP extension versions.

Bug: T197535
Change-Id: I510de1e6d80f5c1d92dc1d1665aaa6c25bf28bf7

docs/extension.schema.v1.json
docs/extension.schema.v2.json
includes/registration/ExtensionDependencyError.php
includes/registration/ExtensionRegistry.php
includes/registration/VersionChecker.php
tests/phpunit/includes/registration/VersionCheckerTest.php

index e6ec971..f6f3b21 100644 (file)
                                                        "type": "string",
                                                        "description": "Version constraint string against PHP."
                                                }
+                                       },
+                                       "patternProprties": {
+                                               "^ext-": {
+                                                       "type": "string",
+                                                       "description": "Required PHP extension.",
+                                                       "const": "*"
+                                               }
                                        }
                                },
                                "extensions": {
index 93bf0d9..8ade991 100644 (file)
                                                        "type": "string",
                                                        "description": "Version constraint string against PHP."
                                                }
+                                       },
+                                       "patternProprties": {
+                                               "^ext-": {
+                                                       "type": "string",
+                                                       "description": "Required PHP extension.",
+                                                       "const": "*"
+                                               }
                                        }
                                },
                                "extensions": {
index dfd5985..c27cd2c 100644 (file)
@@ -53,6 +53,11 @@ class ExtensionDependencyError extends Exception {
         */
        public $incompatiblePhp = false;
 
+       /**
+        * @var string[]
+        */
+       public $missingPhpExtensions = [];
+
        /**
         * @param array $errors Each error has a 'msg' and 'type' key at minimum
         */
@@ -67,6 +72,9 @@ class ExtensionDependencyError extends Exception {
                                case 'incompatible-php':
                                        $this->incompatiblePhp = true;
                                        break;
+                               case 'missing-phpExtension':
+                                       $this->missingPhpExtensions[] = $info['missing'];
+                                       break;
                                case 'missing-skins':
                                        $this->missingSkins[] = $info['missing'];
                                        break;
index 3138b37..e462a0b 100644 (file)
@@ -213,8 +213,11 @@ class ExtensionRegistry {
                $autoloadNamespaces = [];
                $autoloaderPaths = [];
                $processor = new ExtensionProcessor();
-               $phpVersion = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION;
-               $versionChecker = new VersionChecker( $wgVersion, $phpVersion );
+               $versionChecker = new VersionChecker(
+                       $wgVersion,
+                       PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION,
+                       get_loaded_extensions()
+               );
                $extDependencies = [];
                $incompatible = [];
                $warnings = false;
index 93b4a14..586729d 100644 (file)
@@ -40,6 +40,11 @@ class VersionChecker {
         */
        private $phpVersion = false;
 
+       /**
+        * @var string[] List of installed PHP extensions
+        */
+       private $phpExtensions = [];
+
        /**
         * @var array Loaded extensions
         */
@@ -52,11 +57,14 @@ class VersionChecker {
 
        /**
         * @param string $coreVersion Current version of core
+        * @param string $phpVersion Current PHP version
+        * @param string[] $phpExtensions List of installed PHP extensions
         */
-       public function __construct( $coreVersion, $phpVersion ) {
+       public function __construct( $coreVersion, $phpVersion, array $phpExtensions ) {
                $this->versionParser = new VersionParser();
                $this->setCoreVersion( $coreVersion );
                $this->setPhpVersion( $phpVersion );
+               $this->phpExtensions = $phpExtensions;
        }
 
        /**
@@ -112,7 +120,8 @@ class VersionChecker {
         *       'FooBar' => {
         *         'MediaWiki' => '>= 1.25.0',
         *         'platform': {
-        *           'php': '>= 7.0.0'
+        *           'php': '>= 7.0.0',
+        *           'ext-foo': '*'
         *         },
         *         'extensions' => {
         *           'FooBaz' => '>= 1.25.0'
@@ -151,6 +160,7 @@ class VersionChecker {
                                        case 'platform':
                                                foreach ( $values as $dependency => $constraint ) {
                                                        if ( $dependency === 'php' ) {
+                                                               // PHP version
                                                                $phpError = $this->handleDependency(
                                                                        $this->phpVersion,
                                                                        $constraint,
@@ -166,6 +176,23 @@ class VersionChecker {
                                                                                'type' => 'incompatible-php',
                                                                        ];
                                                                }
+                                                       } elseif ( substr( $dependency, 0, 4 ) === 'ext-' ) {
+                                                               // PHP extensions
+                                                               $phpExtension = substr( $dependency, 4 );
+                                                               if ( $constraint !== '*' ) {
+                                                                       throw new UnexpectedValueException( 'Version constraints for '
+                                                                               . 'PHP extensions are not supported in ' . $extension );
+                                                               }
+                                                               if ( !in_array( $phpExtension, $this->phpExtensions, true ) ) {
+                                                                       $errors[] = [
+                                                                               'msg' =>
+                                                                                       "{$extension} requires {$phpExtension} PHP extension "
+                                                                                       . "to be installed."
+                                                                               ,
+                                                                               'type' => 'missing-phpExtension',
+                                                                               'missing' => $phpExtension,
+                                                                       ];
+                                                               }
                                                        } else {
                                                                // add other platform dependencies here
                                                                throw new UnexpectedValueException( 'Dependency type ' . $dependency .
index 20f97bf..6b92444 100644 (file)
@@ -12,7 +12,7 @@ class VersionCheckerTest extends PHPUnit\Framework\TestCase {
         * @dataProvider provideMediaWikiCheck
         */
        public function testMediaWikiCheck( $coreVersion, $constraint, $expected ) {
-               $checker = new VersionChecker( $coreVersion, '7.0.0' );
+               $checker = new VersionChecker( $coreVersion, '7.0.0', [] );
                $this->assertEquals( $expected, !(bool)$checker->checkArray( [
                        'FakeExtension' => [
                                'MediaWiki' => $constraint,
@@ -48,7 +48,7 @@ class VersionCheckerTest extends PHPUnit\Framework\TestCase {
         * @dataProvider providePhpValidCheck
         */
        public function testPhpValidCheck( $phpVersion, $constraint, $expected ) {
-               $checker = new VersionChecker( '1.0.0', $phpVersion );
+               $checker = new VersionChecker( '1.0.0', $phpVersion, [] );
                $this->assertEquals( $expected, !(bool)$checker->checkArray( [
                        'FakeExtension' => [
                                'platform' => [
@@ -71,7 +71,7 @@ class VersionCheckerTest extends PHPUnit\Framework\TestCase {
         * @expectedException UnexpectedValueException
         */
        public function testPhpInvalidConstraint() {
-               $checker = new VersionChecker( '1.0.0', '7.0.0' );
+               $checker = new VersionChecker( '1.0.0', '7.0.0', [] );
                $checker->checkArray( [
                        'FakeExtension' => [
                                'platform' => [
@@ -86,7 +86,7 @@ class VersionCheckerTest extends PHPUnit\Framework\TestCase {
         * @expectedException UnexpectedValueException
         */
        public function testPhpInvalidVersion( $phpVersion ) {
-                $checker = new VersionChecker( '1.0.0', $phpVersion );
+                $checker = new VersionChecker( '1.0.0', $phpVersion, [] );
        }
 
        public static function providePhpInvalidVersion() {
@@ -101,7 +101,7 @@ class VersionCheckerTest extends PHPUnit\Framework\TestCase {
         * @dataProvider provideType
         */
        public function testType( $given, $expected ) {
-               $checker = new VersionChecker( '1.0.0', '7.0.0' );
+               $checker = new VersionChecker( '1.0.0', '7.0.0', [ 'phpLoadedExtension' ] );
                $checker->setLoadedExtensionsAndSkins( [
                                'FakeDependency' => [
                                        'version' => '1.0.0',
@@ -195,6 +195,29 @@ class VersionCheckerTest extends PHPUnit\Framework\TestCase {
                                        ],
                                ],
                        ],
+                       [
+                               [
+                                       'platform' => [
+                                               'ext-phpLoadedExtension' => '*',
+                                       ],
+                               ],
+                               [],
+                       ],
+                       [
+                               [
+                                       'platform' => [
+                                               'ext-phpMissingExtension' => '*',
+                                       ],
+                               ],
+                               [
+                                       [
+                                               'missing' => 'phpMissingExtension',
+                                               'type' => 'missing-phpExtension',
+                                               // phpcs:ignore Generic.Files.LineLength.TooLong
+                                               'msg' => 'FakeExtension requires phpMissingExtension PHP extension to be installed.',
+                                       ],
+                               ],
+                       ],
                ];
        }
 
@@ -203,7 +226,7 @@ class VersionCheckerTest extends PHPUnit\Framework\TestCase {
         * returns any error message.
         */
        public function testInvalidConstraint() {
-               $checker = new VersionChecker( '1.0.0', '7.0.0' );
+               $checker = new VersionChecker( '1.0.0', '7.0.0', [] );
                $checker->setLoadedExtensionsAndSkins( [
                                'FakeDependency' => [
                                        'version' => 'not really valid',
@@ -222,7 +245,7 @@ class VersionCheckerTest extends PHPUnit\Framework\TestCase {
                        ],
                ] ) );
 
-               $checker = new VersionChecker( '1.0.0', '7.0.0' );
+               $checker = new VersionChecker( '1.0.0', '7.0.0', [] );
                $checker->setLoadedExtensionsAndSkins( [
                                'FakeDependency' => [
                                        'version' => '1.24.3',
@@ -249,6 +272,16 @@ class VersionCheckerTest extends PHPUnit\Framework\TestCase {
                                ],
                                'undefinedPlatformDependency',
                        ],
+                       [
+                               [
+                                       'FakeExtension' => [
+                                               'platform' => [
+                                                       'phpLoadedExtension' => '*',
+                                               ],
+                                       ],
+                               ],
+                               'phpLoadedExtension',
+                       ],
                        [
                                [
                                        'FakeExtension' => [
@@ -275,11 +308,26 @@ class VersionCheckerTest extends PHPUnit\Framework\TestCase {
         * @dataProvider provideInvalidDependency
         */
        public function testInvalidDependency( $depencency, $type ) {
-               $checker = new VersionChecker( '1.0.0', '7.0.0' );
+               $checker = new VersionChecker( '1.0.0', '7.0.0', [ 'phpLoadedExtension' ] );
                $this->setExpectedException(
                        UnexpectedValueException::class,
                        "Dependency type $type unknown in FakeExtension"
                );
                $checker->checkArray( $depencency );
        }
+
+       public function testInvalidPhpExtensionConstraint() {
+               $checker = new VersionChecker( '1.0.0', '7.0.0', [ 'phpLoadedExtension' ] );
+               $this->setExpectedException(
+                       UnexpectedValueException::class,
+                       'Version constraints for PHP extensions are not supported in FakeExtension'
+               );
+               $checker->checkArray( [
+                       'FakeExtension' => [
+                               'platform' => [
+                                       'ext-phpLoadedExtension' => '1.0.0',
+                               ],
+                       ],
+               ] );
+       }
 }