Merge "registration: Allow specifying extension dependencies"
[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 array Loaded extensions
40 */
41 private $loaded = [];
42
43 /**
44 * @var VersionParser
45 */
46 private $versionParser;
47
48 public function __construct() {
49 $this->versionParser = new VersionParser();
50 }
51
52 /**
53 * Set an array with credits of all loaded extensions and skins.
54 *
55 * @param array $credits An array of installed extensions with credits of them
56 * @return VersionChecker $this
57 */
58 public function setLoadedExtensionsAndSkins( array $credits ) {
59 $this->loaded = $credits;
60
61 return $this;
62 }
63
64 /**
65 * Set MediaWiki core version.
66 *
67 * @param string $coreVersion Current version of core
68 * @return VersionChecker $this
69 */
70 public function setCoreVersion( $coreVersion ) {
71 try {
72 $this->coreVersion = new Constraint(
73 '==',
74 $this->versionParser->normalize( $coreVersion )
75 );
76 $this->coreVersion->setPrettyString( $coreVersion );
77 } catch ( UnexpectedValueException $e ) {
78 // Non-parsable version, don't fatal.
79 }
80
81 return $this;
82 }
83
84 /**
85 * Check all given dependencies if they are compatible with the named
86 * installed extensions in the $credits array.
87 *
88 * Example $extDependencies:
89 * {
90 * 'GoogleAPIClient' => {
91 * 'MediaWiki' => '>= 1.25.0',
92 * 'extensions' => {
93 * 'FakeExtension' => '>= 1.25.0'
94 * },
95 * 'skins' => {
96 * 'FakeSkin' => '>= 1.0.0'
97 * }
98 * }
99 * }
100 *
101 * @param array $extDependencies All extensions that depend on other ones
102 * @return array
103 */
104 public function checkArray( array $extDependencies ) {
105 $errors = [];
106 foreach ( $extDependencies as $extension => $dependencies ) {
107 foreach ( $dependencies as $dependencyType => $values ) {
108 switch ( $dependencyType ) {
109 case ExtensionRegistry::MEDIAWIKI_CORE:
110 $errors = array_merge(
111 $errors,
112 $this->handleMediaWikiDependency( $values, $extension )
113 );
114 break;
115 case 'extensions':
116 case 'skin':
117 foreach ( $values as $dependency => $constraint ) {
118 $errors = array_merge(
119 $errors,
120 $this->handleExtensionDependency( $dependency, $constraint, $extension )
121 );
122 }
123 break;
124 default:
125 throw new UnexpectedValueException( 'Dependency type ' . $dependencyType .
126 ' unknown in ' . $extension );
127 }
128 }
129 }
130
131 return $errors;
132 }
133
134 /**
135 * Handle a dependency to MediaWiki core. It will check, if a MediaWiki version constraint was
136 * set with self::setCoreVersion before this call (if not, it will return an empty array) and
137 * checks the version constraint given against it.
138 *
139 * @param string $constraint The required version constraint for this dependency
140 * @param string $checkedExt The Extension, which depends on this dependency
141 * @return array An empty array, if MediaWiki version is compatible with $constraint, an array
142 * with an error message, otherwise.
143 */
144 private function handleMediaWikiDependency( $constraint, $checkedExt ) {
145 if ( $this->coreVersion === false ) {
146 // Couldn't parse the core version, so we can't check anything
147 return [];
148 }
149
150 // if the installed and required version are compatible, return an empty array
151 if ( $this->versionParser->parseConstraints( $constraint )
152 ->matches( $this->coreVersion ) ) {
153 return [];
154 }
155 // otherwise mark this as incompatible.
156 return [ "{$checkedExt} is not compatible with the current "
157 . "MediaWiki core (version {$this->coreVersion->getPrettyString()}), it requires: "
158 . $constraint . '.' ];
159 }
160
161 /**
162 * Handle a dependency to another extension.
163 *
164 * @param string $dependencyName The name of the dependency
165 * @param string $constraint The required version constraint for this dependency
166 * @param string $checkedExt The Extension, which depends on this dependency
167 * @return array An empty array, if installed version is compatible with $constraint, an array
168 * with an error message, otherwise.
169 */
170 private function handleExtensionDependency( $dependencyName, $constraint, $checkedExt ) {
171 $incompatible = [];
172 // Check if the dependency is even installed
173 if ( !isset( $this->loaded[$dependencyName] ) ) {
174 $incompatible[] = "{$checkedExt} requires {$dependencyName} to be installed.";
175 return $incompatible;
176 }
177 // Check if the dependency has specified a version
178 if ( !isset( $this->loaded[$dependencyName]['version'] ) ) {
179 // If we depend upon any version, and none is set, that's fine.
180 if ( $constraint === '*' ) {
181 wfDebug( "{$dependencyName} does not expose it's version, but {$checkedExt}
182 mentions it with constraint '*'. Assume it's ok so." );
183 } else {
184 // Otherwise, mark it as incompatible.
185 $incompatible[] = "{$dependencyName} does not expose it's version, but {$checkedExt}
186 requires: {$constraint}.";
187 }
188 } else {
189 // Try to get a constraint for the dependency version
190 try {
191 $installedVersion = new Constraint(
192 '==',
193 $this->versionParser->normalize( $this->loaded[$dependencyName]['version'] )
194 );
195 } catch ( UnexpectedValueException $e ) {
196 // Non-parsable version, don't fatal, output an error message that the version
197 // string is invalid
198 return [ "Dependency $dependencyName provides an invalid version string." ];
199 }
200 // Check if the constraint actually matches...
201 if (
202 isset( $installedVersion ) &&
203 !$this->versionParser->parseConstraints( $constraint )->matches( $installedVersion )
204 ) {
205 $incompatible[] = "{$checkedExt} is not compatible with the current "
206 . "installed version of {$dependencyName} "
207 . "({$this->loaded[$dependencyName]['version']}), "
208 . "it requires: " . $constraint . '.';
209 }
210 }
211
212 return $incompatible;
213 }
214 }