60413be52a36e1605251f80eb53d753f828d4b02
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / package / validation / PackageValidationException.class.php
1 <?php
2
3 namespace wcf\system\package\validation;
4
5 use wcf\system\exception\SystemException;
6 use wcf\system\package\PackageArchive;
7 use wcf\system\WCF;
8
9 /**
10 * Represents exceptions occurred during validation of a package archive. This exception
11 * does not cause the details to be logged.
12 *
13 * @author Alexander Ebert
14 * @copyright 2001-2019 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 */
17 class PackageValidationException extends SystemException
18 {
19 /**
20 * list of additional details for each subtype
21 * @var string[]
22 */
23 protected $details = [];
24
25 /**
26 * missing archive, expects the detail 'archive' and optionally 'targetArchive'
27 * (extracting archive from the archive)
28 * @var int
29 */
30 const FILE_NOT_FOUND = 1;
31
32 /**
33 * missing package.xml, expects the detail 'archive'
34 * @var int
35 */
36 const MISSING_PACKAGE_XML = 2;
37
38 /**
39 * package name violates WCF's schema, expects the detail 'packageName'
40 * @var int
41 */
42 const INVALID_PACKAGE_NAME = 3;
43
44 /**
45 * package version violates WCF's schema, expects the detail 'packageVersion'
46 * @var int
47 */
48 const INVALID_PACKAGE_VERSION = 4;
49
50 /**
51 * package contains no install instructions and an update is not possible, expects the detail 'packageName'
52 * @var int
53 */
54 const NO_INSTALL_PATH = 5;
55
56 /**
57 * package is already installed and cannot be updated using current archive, expects the
58 * details 'packageName', 'packageVersion' and 'deliveredPackageVersion'
59 * @var int
60 */
61 const NO_UPDATE_PATH = 6;
62
63 /**
64 * packages which exclude the current package, expects the detail 'packages' (list of \wcf\data\package\Package)
65 * @var int
66 */
67 const EXCLUDING_PACKAGES = 7;
68
69 /**
70 * packages which are excluded by current package, expects the detail 'packages' (list of \wcf\data\package\Package)
71 * @var int
72 */
73 const EXCLUDED_PACKAGES = 8;
74
75 /**
76 * package version is lower than the request version, expects the
77 * details 'packageName', 'packageVersion' and 'deliveredPackageVersion'
78 * @var int
79 */
80 const INSUFFICIENT_VERSION = 9;
81
82 /**
83 * requirement is set but neither installed nor provided, expects the
84 * details 'packageName', 'packageVersion' and 'package' (must be
85 * an instance of \wcf\data\package\Package or null if not installed)
86 * @var int
87 */
88 const MISSING_REQUIREMENT = 10;
89
90 /**
91 * file reference for a package installation plugin is missing, expects the details 'pip', 'type' and 'value'
92 * @var int
93 */
94 const MISSING_INSTRUCTION_FILE = 11;
95
96 /**
97 * the uploaded version is already installed, expects the details 'packageName' and 'packageVersion'
98 * @var int
99 */
100 const ALREADY_INSTALLED = 12;
101
102 /**
103 * the provided API version string is invalid and does not fall into the range from `2017` through `2099`
104 * @var int
105 * @deprecated 5.2
106 */
107 const INVALID_API_VERSION = 13;
108
109 /**
110 * the package is not compatible with the current API version or any other of the supported ones
111 * @var int
112 * @deprecated 5.2
113 */
114 const INCOMPATIBLE_API_VERSION = 14;
115
116 /**
117 * the package lacks any sort of API compatibility data
118 * @var int
119 * @deprecated 5.2
120 */
121 const MISSING_API_VERSION = 15;
122
123 /**
124 * the void is not the only instruction
125 * @var int
126 */
127 const VOID_NOT_ALONE = 16;
128
129 /**
130 * the void is used during installation
131 * @var int
132 */
133 const VOID_ON_INSTALL = 17;
134
135 /**
136 * an app with the same abbreviation is already installed
137 * @since 5.4
138 */
139 const DUPLICATE_ABBREVIATION = 18;
140
141 /**
142 * the version of an excluded package is invalid
143 * @var int
144 * @since 5.5
145 */
146 const INVALID_EXCLUDED_PACKAGE_VERSION_NUMBER = 19;
147
148 /**
149 * the package excludes itself
150 * @var int
151 * @since 5.5
152 */
153 const SELF_EXCLUDE = 20;
154
155 /**
156 * the package does not explicitly require com.woltlab.wcf
157 * @var int
158 * @since 6.0
159 */
160 const MISSING_COM_WOLTLAB_WCF_REQUIREMENT = 21;
161
162 /**
163 * the package requires com.woltlab.wcf in an ancient version
164 * @var int
165 * @since 6.0
166 */
167 const ANCIENT_COM_WOLTLAB_WCF_REQUIREMENT = 22;
168
169 /**
170 * the version of a required package is invalid
171 * @var int
172 * @since 6.0
173 */
174 const INVALID_REQUIRED_PACKAGE_VERSION_NUMBER = 23;
175
176 /**
177 * an unknown tag is given in the <packageinformation>
178 * @var int
179 * @since 6.0
180 */
181 const UNKNOWN_PACKAGE_INFORMATION = 24;
182
183 /**
184 * an unknown tag is given in the <authorinformation>
185 * @var int
186 * @since 6.0
187 */
188 const UNKNOWN_AUTHOR_INFORMATION = 25;
189
190 /**
191 * no author information is given
192 * @var int
193 * @since 6.0
194 */
195 const MISSING_AUTHOR_INFORMATION = 26;
196
197 /**
198 * no display name is given
199 * @var int
200 * @since 6.0
201 */
202 const MISSING_DISPLAY_NAME = 27;
203
204 /**
205 * an duplicate tag is given in the <packageinformation>
206 * @var int
207 * @since 6.0
208 */
209 const DUPLICATE_PACKAGE_INFORMATION = 28;
210
211 /**
212 * no version information is given
213 * @var int
214 * @since 6.0
215 */
216 const MISSING_PACKAGE_VERSION = 29;
217
218 /**
219 * no release date is given
220 * @var int
221 * @since 6.0
222 */
223 const MISSING_PACKAGE_DATE = 30;
224
225 /**
226 * the `package.xml` has syntax errors
227 * @var int
228 * @since 6.0
229 */
230 const INVALID_PACKAGE_XML = 31;
231
232 /**
233 * Creates a new PackageArchiveValidationException.
234 *
235 * @param int $code
236 * @param string[] $details
237 */
238 public function __construct($code, array $details = [])
239 {
240 $this->details = $details;
241
242 parent::__construct($this->getLegacyMessage($code), $code);
243 }
244
245 /**
246 * Returns exception details.
247 *
248 * @return string[]
249 */
250 public function getDetails()
251 {
252 return $this->details;
253 }
254
255 /**
256 * Returns the readable error message.
257 *
258 * @param int $code
259 * @return string
260 */
261 public function getErrorMessage($code = null)
262 {
263 if (!empty($this->details['legacyMessage'])) {
264 return $this->details['legacyMessage'];
265 }
266
267 return WCF::getLanguage()->getDynamicVariable(
268 'wcf.acp.package.validation.errorCode.' . ($code === null ? $this->getCode() : $code),
269 $this->getDetails()
270 );
271 }
272
273 /**
274 * Returns legacy error messages to mimic WCF 2.0.x PackageArchive's exceptions.
275 *
276 * @param int $code
277 * @return string
278 */
279 protected function getLegacyMessage($code)
280 {
281 switch ($code) {
282 case self::FILE_NOT_FOUND:
283 if (isset($this->details['targetArchive'])) {
284 return "tar archive '" . $this->details['targetArchive'] . "' not found in '" . $this->details['archive'] . "'.";
285 }
286
287 return "unable to find package file '" . $this->details['archive'] . "'";
288 break;
289
290 case self::MISSING_PACKAGE_XML:
291 return "package information file '" . PackageArchive::INFO_FILE . "' not found in '" . $this->details['archive'] . "'";
292 break;
293
294 case self::INVALID_PACKAGE_NAME:
295 return "'" . $this->details['packageName'] . "' is not a valid package name.";
296 break;
297
298 case self::INVALID_PACKAGE_VERSION:
299 return "package version '" . $this->details['packageVersion'] . "' is invalid";
300 break;
301
302 default:
303 return $this->getErrorMessage($code);
304 break;
305 }
306 }
307
308 /**
309 * @inheritDoc
310 */
311 protected function logError()
312 {
313 // do not log errors
314 }
315 }