Merge "registration: Generalize CoreVersionChecker to VersionChecker"
[lhc/web/wiklou.git] / includes / registration / VersionChecker.php
1 <?php
2
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @author Legoktm
20 * @author Florian Schmidt
21 */
22
23 use Composer\Semver\VersionParser;
24 use Composer\Semver\Constraint\Constraint;
25
26 /**
27 * Provides functions to check a set of extensions with dependencies against
28 * a set of loaded extensions and given version information.
29 *
30 * @since 1.29
31 */
32 class VersionChecker {
33 /**
34 * @var Constraint|bool representing $wgVersion
35 */
36 private $coreVersion = false;
37
38 /**
39 * @var VersionParser
40 */
41 private $versionParser;
42
43 public function __construct() {
44 $this->versionParser = new VersionParser();
45 }
46
47 /**
48 * Set MediaWiki core version.
49 *
50 * @param string $coreVersion Current version of core
51 * @return VersionChecker $this
52 */
53 public function setCoreVersion( $coreVersion ) {
54 try {
55 $this->coreVersion = new Constraint(
56 '==',
57 $this->versionParser->normalize( $coreVersion )
58 );
59 $this->coreVersion->setPrettyString( $coreVersion );
60 } catch ( UnexpectedValueException $e ) {
61 // Non-parsable version, don't fatal.
62 }
63
64 return $this;
65 }
66
67 /**
68 * Check all given dependencies if they are compatible with the named
69 * installed extensions in the $credits array.
70 *
71 * Example $extDependencies:
72 * {
73 * 'GoogleAPIClient' => {
74 * 'MediaWiki' => '>= 1.25.0'
75 * }
76 * }
77 *
78 * @param array $extDependencies All extensions that depend on other ones
79 * @return array
80 */
81 public function checkArray( array $extDependencies ) {
82 $errors = [];
83 foreach ( $extDependencies as $extension => $dependencies ) {
84 foreach ( $dependencies as $dependencyType => $values ) {
85 switch ( $dependencyType ) {
86 case ExtensionRegistry::MEDIAWIKI_CORE:
87 $errors = array_merge(
88 $errors,
89 $this->handleMediaWikiDependency( $values, $extension )
90 );
91 break;
92 default:
93 throw new UnexpectedValueException( 'Dependency type ' . $dependencyType .
94 ' unknown in ' . $extension );
95 }
96 }
97 }
98
99 return $errors;
100 }
101
102 /**
103 * Handle a dependency to MediaWiki core. It will check, if a MediaWiki version constraint was
104 * set with self::setCoreVersion before this call (if not, it will return an empty array) and
105 * checks the version constraint given against it.
106 *
107 * @param string $constraint The required version constraint for this dependency
108 * @param string $checkedExt The Extension, which depends on this dependency
109 * @return array An empty array, if MediaWiki version is compatible with $constraint, an array
110 * with an error message, otherwise.
111 */
112 private function handleMediaWikiDependency( $constraint, $checkedExt ) {
113 if ( $this->coreVersion === false ) {
114 // Couldn't parse the core version, so we can't check anything
115 return [];
116 }
117
118 // if the installed and required version are compatible, return an empty array
119 if ( $this->versionParser->parseConstraints( $constraint )
120 ->matches( $this->coreVersion ) ) {
121 return [];
122 }
123 // otherwise mark this as incompatible.
124 return [ "{$checkedExt} is not compatible with the current "
125 . "MediaWiki core (version {$this->coreVersion->getPrettyString()}), it requires: "
126 . $constraint . '.' ];
127 }
128 }