Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * The extension manager handles installing, disabling enabling, and | |
14 | * uninstalling extensions. | |
15 | * | |
20429eb9 RL |
16 | * You should obtain a singleton of this class via |
17 | * | |
c486a70f | 18 | * $manager = CRM_Extension_System::singleton()->getManager(); |
20429eb9 | 19 | * |
6a488035 | 20 | * @package CRM |
ca5cec67 | 21 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
22 | */ |
23 | class CRM_Extension_Manager { | |
24 | /** | |
fe482240 | 25 | * The extension is fully installed and enabled. |
6a488035 TO |
26 | */ |
27 | const STATUS_INSTALLED = 'installed'; | |
28 | ||
29 | /** | |
fe482240 | 30 | * The extension config has been applied to database but deactivated. |
6a488035 TO |
31 | */ |
32 | const STATUS_DISABLED = 'disabled'; | |
33 | ||
34 | /** | |
35 | * The extension code is visible, but nothing has been applied to DB | |
36 | */ | |
37 | const STATUS_UNINSTALLED = 'uninstalled'; | |
38 | ||
39 | /** | |
40 | * The extension code is not locally accessible | |
41 | */ | |
42 | const STATUS_UNKNOWN = 'unknown'; | |
43 | ||
44 | /** | |
25fcba46 | 45 | * The extension is installed but the code is not accessible |
6a488035 TO |
46 | */ |
47 | const STATUS_INSTALLED_MISSING = 'installed-missing'; | |
48 | ||
49 | /** | |
25fcba46 | 50 | * The extension was installed and is now disabled; the code is not accessible |
6a488035 TO |
51 | */ |
52 | const STATUS_DISABLED_MISSING = 'disabled-missing'; | |
53 | ||
54 | /** | |
55 | * @var CRM_Extension_Container_Interface | |
56 | * | |
57 | * Note: Treat as private. This is only public to facilitate debugging. | |
58 | */ | |
59 | public $fullContainer; | |
60 | ||
61 | /** | |
041ecc95 | 62 | * Default container. |
63 | * | |
64 | * @var CRM_Extension_Container_Basic|false | |
6a488035 TO |
65 | * |
66 | * Note: Treat as private. This is only public to facilitate debugging. | |
67 | */ | |
68 | public $defaultContainer; | |
69 | ||
70 | /** | |
041ecc95 | 71 | * Mapper. |
72 | * | |
6a488035 TO |
73 | * @var CRM_Extension_Mapper |
74 | * | |
75 | * Note: Treat as private. This is only public to facilitate debugging. | |
76 | */ | |
77 | public $mapper; | |
78 | ||
79 | /** | |
041ecc95 | 80 | * Type managers. |
81 | * | |
82 | * @var array | |
83 | * | |
84 | * Format is (typeName => CRM_Extension_Manager_Interface) | |
6a488035 TO |
85 | * |
86 | * Note: Treat as private. This is only public to facilitate debugging. | |
87 | */ | |
88 | public $typeManagers; | |
89 | ||
90 | /** | |
041ecc95 | 91 | * Statuses. |
92 | * | |
93 | * @var array | |
94 | * | |
95 | * Format is (extensionKey => statusConstant) | |
6a488035 TO |
96 | * |
97 | * Note: Treat as private. This is only public to facilitate debugging. | |
98 | */ | |
99 | public $statuses; | |
100 | ||
20429eb9 RL |
101 | /** |
102 | * Live process(es) per extension. | |
103 | * | |
104 | * @var array | |
105 | * | |
106 | * Format is: { | |
107 | * extensionKey => [ | |
108 | * ['operation' => 'install|enable|uninstall|disable', 'phase' => 'queued|live|completed' | |
109 | * ... | |
110 | * ], | |
111 | * ... | |
112 | * } | |
113 | * | |
114 | * The inner array is a stack, so the most recent current operation is the | |
115 | * last entry. As this manager handles multiple extensions at once, here's | |
116 | * the flow for an install operation. | |
117 | * | |
118 | * $manager->install(['ext1', 'ext2']); | |
119 | * | |
120 | * 0. {} | |
121 | * 1. { ext1: ['install'], ext2: ['install'] } | |
122 | * 2. { ext1: ['install', 'installing'], ext2: ['install'] } | |
123 | * 3. { ext1: ['install'], ext2: ['install', 'installing'] } | |
124 | * 4. { ext1: ['install'], ext2: ['install'] } | |
125 | * 5. {} | |
126 | */ | |
127 | protected $processes = []; | |
128 | ||
6a488035 | 129 | /** |
041ecc95 | 130 | * Class constructor. |
131 | * | |
6c8f6e67 | 132 | * @param CRM_Extension_Container_Interface $fullContainer |
6a488035 | 133 | * @param CRM_Extension_Container_Basic|FALSE $defaultContainer |
6c8f6e67 | 134 | * @param CRM_Extension_Mapper $mapper |
041ecc95 | 135 | * @param array $typeManagers |
6a488035 | 136 | */ |
00be9182 | 137 | public function __construct(CRM_Extension_Container_Interface $fullContainer, $defaultContainer, CRM_Extension_Mapper $mapper, $typeManagers) { |
6a488035 TO |
138 | $this->fullContainer = $fullContainer; |
139 | $this->defaultContainer = $defaultContainer; | |
140 | $this->mapper = $mapper; | |
141 | $this->typeManagers = $typeManagers; | |
142 | } | |
143 | ||
144 | /** | |
145 | * Install or upgrade the code for an extension -- and perform any | |
146 | * necessary database changes (eg replacing extension metadata). | |
147 | * | |
148 | * This only works if the extension is stored in the default container. | |
149 | * | |
f41911fd TO |
150 | * @param string $tmpCodeDir |
151 | * Path to a local directory containing a copy of the new (inert) code. | |
6a488035 TO |
152 | * @throws CRM_Extension_Exception |
153 | */ | |
154 | public function replace($tmpCodeDir) { | |
353ffa53 | 155 | if (!$this->defaultContainer) { |
6a488035 TO |
156 | throw new CRM_Extension_Exception("Default extension container is not configured"); |
157 | } | |
158 | ||
159 | $newInfo = CRM_Extension_Info::loadFromFile($tmpCodeDir . DIRECTORY_SEPARATOR . CRM_Extension_Info::FILENAME); | |
160 | $oldStatus = $this->getStatus($newInfo->key); | |
161 | ||
162 | // find $tgtPath, $oldInfo, $typeManager | |
163 | switch ($oldStatus) { | |
164 | case self::STATUS_UNINSTALLED: | |
165 | case self::STATUS_INSTALLED: | |
166 | case self::STATUS_DISABLED: | |
167 | // There is an old copy of the extension. Try to install in the same place -- but it must go somewhere in the default-container | |
7b966967 SL |
168 | // throws Exception |
169 | list ($oldInfo, $typeManager) = $this->_getInfoTypeHandler($newInfo->key); | |
6a488035 | 170 | $tgtPath = $this->fullContainer->getPath($newInfo->key); |
353ffa53 | 171 | if (!CRM_Utils_File::isChildPath($this->defaultContainer->getBaseDir(), $tgtPath)) { |
6a488035 | 172 | // force installation in the default-container |
615841de | 173 | $oldPath = $tgtPath; |
6a488035 | 174 | $tgtPath = $this->defaultContainer->getBaseDir() . DIRECTORY_SEPARATOR . $newInfo->key; |
be2fb01f | 175 | CRM_Core_Session::setStatus(ts('A copy of the extension (%1) is in a system folder (%2). The system copy will be preserved, but the new copy will be used.', [ |
6a488035 TO |
176 | 1 => $newInfo->key, |
177 | 2 => $oldPath, | |
be2fb01f | 178 | ])); |
6a488035 TO |
179 | } |
180 | break; | |
b3a4b879 | 181 | |
6a488035 TO |
182 | case self::STATUS_INSTALLED_MISSING: |
183 | case self::STATUS_DISABLED_MISSING: | |
b3a4b879 | 184 | // the extension does not exist in any container; we're free to put it anywhere |
6a488035 | 185 | $tgtPath = $this->defaultContainer->getBaseDir() . DIRECTORY_SEPARATOR . $newInfo->key; |
7b966967 SL |
186 | // throws Exception |
187 | list ($oldInfo, $typeManager) = $this->_getMissingInfoTypeHandler($newInfo->key); | |
6a488035 | 188 | break; |
b3a4b879 | 189 | |
6a488035 | 190 | case self::STATUS_UNKNOWN: |
b3a4b879 | 191 | // the extension does not exist in any container; we're free to put it anywhere |
6a488035 TO |
192 | $tgtPath = $this->defaultContainer->getBaseDir() . DIRECTORY_SEPARATOR . $newInfo->key; |
193 | $oldInfo = $typeManager = NULL; | |
194 | break; | |
b3a4b879 | 195 | |
6a488035 | 196 | default: |
615841de | 197 | throw new CRM_Extension_Exception("Cannot install or enable extension: {$newInfo->key}"); |
6a488035 TO |
198 | } |
199 | ||
200 | // move the code! | |
201 | switch ($oldStatus) { | |
202 | case self::STATUS_UNINSTALLED: | |
203 | case self::STATUS_UNKNOWN: | |
204 | // There are no DB records to worry about, so we'll just put the files in place | |
205 | if (!CRM_Utils_File::replaceDir($tmpCodeDir, $tgtPath)) { | |
206 | throw new CRM_Extension_Exception("Failed to move $tmpCodeDir to $tgtPath"); | |
207 | } | |
208 | break; | |
b3a4b879 | 209 | |
6a488035 TO |
210 | case self::STATUS_INSTALLED: |
211 | case self::STATUS_INSTALLED_MISSING: | |
212 | case self::STATUS_DISABLED: | |
213 | case self::STATUS_DISABLED_MISSING: | |
214 | // There are DB records; coordinate the file placement with the DB updates | |
215 | $typeManager->onPreReplace($oldInfo, $newInfo); | |
216 | if (!CRM_Utils_File::replaceDir($tmpCodeDir, $tgtPath)) { | |
217 | throw new CRM_Extension_Exception("Failed to move $tmpCodeDir to $tgtPath"); | |
218 | } | |
219 | $this->_updateExtensionEntry($newInfo); | |
220 | $typeManager->onPostReplace($oldInfo, $newInfo); | |
221 | break; | |
b3a4b879 | 222 | |
6a488035 | 223 | default: |
615841de | 224 | throw new CRM_Extension_Exception("Cannot install or enable extension: {$newInfo->key}"); |
6a488035 TO |
225 | } |
226 | ||
227 | $this->refresh(); | |
a618b2b8 | 228 | CRM_Core_Invoke::rebuildMenuAndCaches(TRUE); |
6a488035 TO |
229 | } |
230 | ||
231 | /** | |
232 | * Add records of the extension to the database -- and enable it | |
233 | * | |
df7a1988 CW |
234 | * @param string|array $keys |
235 | * One or more extension keys. | |
20429eb9 | 236 | * @param string $mode install|enable |
6a488035 TO |
237 | * @throws CRM_Extension_Exception |
238 | */ | |
20429eb9 | 239 | public function install($keys, $mode = 'install') { |
df7a1988 | 240 | $keys = (array) $keys; |
6a488035 TO |
241 | $origStatuses = $this->getStatuses(); |
242 | ||
243 | // TODO: to mitigate the risk of crashing during installation, scan | |
244 | // keys/statuses/types before doing anything | |
245 | ||
25fcba46 CW |
246 | // Check compatibility |
247 | $incompatible = []; | |
6a488035 | 248 | foreach ($keys as $key) { |
25fcba46 CW |
249 | if ($this->isIncompatible($key)) { |
250 | $incompatible[] = $key; | |
251 | } | |
252 | } | |
253 | if ($incompatible) { | |
254 | throw new CRM_Extension_Exception('Cannot install incompatible extension: ' . implode(', ', $incompatible)); | |
255 | } | |
256 | ||
20429eb9 RL |
257 | // Keep state for these operations. |
258 | $this->addProcess($keys, $mode); | |
259 | ||
25fcba46 CW |
260 | foreach ($keys as $key) { |
261 | /** @var CRM_Extension_Info $info */ | |
262 | /** @var CRM_Extension_Manager_Base $typeManager */ | |
7b966967 | 263 | list ($info, $typeManager) = $this->_getInfoTypeHandler($key); |
6a488035 TO |
264 | |
265 | switch ($origStatuses[$key]) { | |
266 | case self::STATUS_INSTALLED: | |
20429eb9 RL |
267 | // ok, nothing to do. As such the status of this process is no longer |
268 | // 'install' install was the intent, which might have resulted in | |
269 | // changes but these changes will not be happening, so processes that | |
270 | // are sensitive to installs (like the managed entities reconcile | |
271 | // operation) should not assume that these changes have happened. | |
272 | $this->popProcess([$key]); | |
6a488035 | 273 | break; |
b3a4b879 | 274 | |
6a488035 TO |
275 | case self::STATUS_DISABLED: |
276 | // re-enable it | |
20429eb9 | 277 | $this->addProcess([$key], 'enabling'); |
6a488035 TO |
278 | $typeManager->onPreEnable($info); |
279 | $this->_setExtensionActive($info, 1); | |
280 | $typeManager->onPostEnable($info); | |
34ba82e8 TO |
281 | |
282 | // A full refresh would be preferrable but very slow. This at least allows | |
283 | // later extensions to access classes from earlier extensions. | |
284 | $this->statuses = NULL; | |
285 | $this->mapper->refresh(); | |
20429eb9 RL |
286 | |
287 | $this->popProcess([$key]); | |
6a488035 | 288 | break; |
b3a4b879 | 289 | |
6a488035 TO |
290 | case self::STATUS_UNINSTALLED: |
291 | // install anew | |
20429eb9 | 292 | $this->addProcess([$key], 'installing'); |
6a488035 TO |
293 | $typeManager->onPreInstall($info); |
294 | $this->_createExtensionEntry($info); | |
295 | $typeManager->onPostInstall($info); | |
34ba82e8 TO |
296 | |
297 | // A full refresh would be preferrable but very slow. This at least allows | |
298 | // later extensions to access classes from earlier extensions. | |
299 | $this->statuses = NULL; | |
300 | $this->mapper->refresh(); | |
20429eb9 RL |
301 | |
302 | $this->popProcess([$key]); | |
6a488035 | 303 | break; |
b3a4b879 | 304 | |
6a488035 TO |
305 | case self::STATUS_UNKNOWN: |
306 | default: | |
307 | throw new CRM_Extension_Exception("Cannot install or enable extension: $key"); | |
308 | } | |
309 | } | |
310 | ||
311 | $this->statuses = NULL; | |
312 | $this->mapper->refresh(); | |
d5abe16a SL |
313 | if (!CRM_Core_Config::isUpgradeMode()) { |
314 | CRM_Core_Invoke::rebuildMenuAndCaches(TRUE); | |
3d0e24ec | 315 | |
d5abe16a SL |
316 | $schema = new CRM_Logging_Schema(); |
317 | $schema->fixSchemaDifferences(); | |
318 | } | |
3d0e24ec | 319 | foreach ($keys as $key) { |
7b966967 SL |
320 | // throws Exception |
321 | list ($info, $typeManager) = $this->_getInfoTypeHandler($key); | |
3d0e24ec | 322 | |
323 | switch ($origStatuses[$key]) { | |
324 | case self::STATUS_INSTALLED: | |
325 | // ok, nothing to do | |
326 | break; | |
b3a4b879 | 327 | |
3d0e24ec | 328 | case self::STATUS_DISABLED: |
329 | // re-enable it | |
330 | break; | |
b3a4b879 | 331 | |
3d0e24ec | 332 | case self::STATUS_UNINSTALLED: |
333 | // install anew | |
20429eb9 | 334 | $this->addProcess([$key], 'installing'); |
3d0e24ec | 335 | $typeManager->onPostPostInstall($info); |
20429eb9 | 336 | $this->popProcess([$key]); |
3d0e24ec | 337 | break; |
b3a4b879 | 338 | |
3d0e24ec | 339 | case self::STATUS_UNKNOWN: |
340 | default: | |
341 | throw new CRM_Extension_Exception("Cannot install or enable extension: $key"); | |
342 | } | |
343 | } | |
344 | ||
20429eb9 RL |
345 | // All processes for these keys |
346 | $this->popProcess($keys); | |
6a488035 TO |
347 | } |
348 | ||
349 | /** | |
350 | * Add records of the extension to the database -- and enable it | |
351 | * | |
f41911fd TO |
352 | * @param array $keys |
353 | * List of extension keys. | |
6a488035 TO |
354 | * @throws CRM_Extension_Exception |
355 | */ | |
356 | public function enable($keys) { | |
20429eb9 | 357 | $this->install($keys, 'enable'); |
6a488035 TO |
358 | } |
359 | ||
360 | /** | |
25fcba46 | 361 | * Disable extension without removing record from db. |
6a488035 | 362 | * |
df7a1988 CW |
363 | * @param string|array $keys |
364 | * One or more extension keys. | |
6a488035 TO |
365 | * @throws CRM_Extension_Exception |
366 | */ | |
367 | public function disable($keys) { | |
df7a1988 | 368 | $keys = (array) $keys; |
6a488035 TO |
369 | $origStatuses = $this->getStatuses(); |
370 | ||
371 | // TODO: to mitigate the risk of crashing during installation, scan | |
372 | // keys/statuses/types before doing anything | |
373 | ||
f8a7cfff TO |
374 | sort($keys); |
375 | $disableRequirements = $this->findDisableRequirements($keys); | |
7b966967 SL |
376 | // This munges order, but makes it comparable. |
377 | sort($disableRequirements); | |
f8a7cfff | 378 | if ($keys !== $disableRequirements) { |
41d113e9 | 379 | throw new CRM_Extension_Exception_DependencyException("Cannot disable extension due to dependencies. Consider disabling all these: " . implode(',', $disableRequirements)); |
f8a7cfff TO |
380 | } |
381 | ||
20429eb9 RL |
382 | $this->addProcess($keys, 'disable'); |
383 | ||
6a488035 TO |
384 | foreach ($keys as $key) { |
385 | switch ($origStatuses[$key]) { | |
386 | case self::STATUS_INSTALLED: | |
20429eb9 | 387 | $this->addProcess([$key], 'disabling'); |
7b966967 SL |
388 | // throws Exception |
389 | list ($info, $typeManager) = $this->_getInfoTypeHandler($key); | |
6a488035 TO |
390 | $typeManager->onPreDisable($info); |
391 | $this->_setExtensionActive($info, 0); | |
392 | $typeManager->onPostDisable($info); | |
20429eb9 | 393 | $this->popProcess([$key]); |
6a488035 | 394 | break; |
b3a4b879 | 395 | |
6a488035 | 396 | case self::STATUS_INSTALLED_MISSING: |
7b966967 SL |
397 | // throws Exception |
398 | list ($info, $typeManager) = $this->_getMissingInfoTypeHandler($key); | |
6a488035 TO |
399 | $typeManager->onPreDisable($info); |
400 | $this->_setExtensionActive($info, 0); | |
401 | $typeManager->onPostDisable($info); | |
402 | break; | |
b3a4b879 | 403 | |
6a488035 TO |
404 | case self::STATUS_DISABLED: |
405 | case self::STATUS_DISABLED_MISSING: | |
406 | case self::STATUS_UNINSTALLED: | |
407 | // ok, nothing to do | |
20429eb9 RL |
408 | // Remove the 'disable' process as we're not doing that. |
409 | $this->popProcess([$key]); | |
6a488035 | 410 | break; |
b3a4b879 | 411 | |
6a488035 TO |
412 | case self::STATUS_UNKNOWN: |
413 | default: | |
414 | throw new CRM_Extension_Exception("Cannot disable unknown extension: $key"); | |
415 | } | |
416 | } | |
417 | ||
418 | $this->statuses = NULL; | |
419 | $this->mapper->refresh(); | |
420 | CRM_Core_Invoke::rebuildMenuAndCaches(TRUE); | |
20429eb9 RL |
421 | |
422 | $this->popProcess($keys); | |
6a488035 TO |
423 | } |
424 | ||
425 | /** | |
fe482240 | 426 | * Remove all database references to an extension. |
6a488035 | 427 | * |
df7a1988 CW |
428 | * @param string|array $keys |
429 | * One or more extension keys. | |
6a488035 TO |
430 | * @throws CRM_Extension_Exception |
431 | */ | |
432 | public function uninstall($keys) { | |
df7a1988 | 433 | $keys = (array) $keys; |
6a488035 TO |
434 | $origStatuses = $this->getStatuses(); |
435 | ||
436 | // TODO: to mitigate the risk of crashing during installation, scan | |
437 | // keys/statuses/types before doing anything | |
438 | ||
20429eb9 RL |
439 | $this->addProcess($keys, 'uninstall'); |
440 | ||
b3a4b879 | 441 | foreach ($keys as $key) { |
6a488035 TO |
442 | switch ($origStatuses[$key]) { |
443 | case self::STATUS_INSTALLED: | |
444 | case self::STATUS_INSTALLED_MISSING: | |
445 | throw new CRM_Extension_Exception("Cannot uninstall extension; disable it first: $key"); | |
b3a4b879 | 446 | |
6a488035 | 447 | case self::STATUS_DISABLED: |
20429eb9 | 448 | $this->addProcess([$key], 'uninstalling'); |
7b966967 SL |
449 | // throws Exception |
450 | list ($info, $typeManager) = $this->_getInfoTypeHandler($key); | |
6a488035 TO |
451 | $typeManager->onPreUninstall($info); |
452 | $this->_removeExtensionEntry($info); | |
453 | $typeManager->onPostUninstall($info); | |
454 | break; | |
b3a4b879 | 455 | |
6a488035 | 456 | case self::STATUS_DISABLED_MISSING: |
7b966967 SL |
457 | // throws Exception |
458 | list ($info, $typeManager) = $this->_getMissingInfoTypeHandler($key); | |
6a488035 TO |
459 | $typeManager->onPreUninstall($info); |
460 | $this->_removeExtensionEntry($info); | |
461 | $typeManager->onPostUninstall($info); | |
462 | break; | |
b3a4b879 | 463 | |
6a488035 TO |
464 | case self::STATUS_UNINSTALLED: |
465 | // ok, nothing to do | |
20429eb9 RL |
466 | // remove the 'uninstall' process since we're not doing that. |
467 | $this->popProcess([$key]); | |
6a488035 | 468 | break; |
b3a4b879 | 469 | |
6a488035 TO |
470 | case self::STATUS_UNKNOWN: |
471 | default: | |
472 | throw new CRM_Extension_Exception("Cannot disable unknown extension: $key"); | |
473 | } | |
474 | } | |
475 | ||
476 | $this->statuses = NULL; | |
477 | $this->mapper->refresh(); | |
478 | CRM_Core_Invoke::rebuildMenuAndCaches(TRUE); | |
20429eb9 | 479 | $this->popProcess($keys); |
6a488035 TO |
480 | } |
481 | ||
482 | /** | |
fe482240 | 483 | * Determine the status of an extension. |
6a488035 | 484 | * |
77b97be7 EM |
485 | * @param $key |
486 | * | |
a6c01b45 | 487 | * @return string |
25fcba46 | 488 | * constant self::STATUS_* |
6a488035 TO |
489 | */ |
490 | public function getStatus($key) { | |
491 | $statuses = $this->getStatuses(); | |
492 | if (array_key_exists($key, $statuses)) { | |
493 | return $statuses[$key]; | |
0db6c3e1 TO |
494 | } |
495 | else { | |
6a488035 TO |
496 | return self::STATUS_UNKNOWN; |
497 | } | |
498 | } | |
499 | ||
25fcba46 CW |
500 | /** |
501 | * Check if a given extension is incompatible with this version of CiviCRM | |
502 | * | |
503 | * @param $key | |
504 | * @return bool|array | |
505 | */ | |
506 | public function isIncompatible($key) { | |
507 | $info = CRM_Extension_System::getCompatibilityInfo(); | |
508 | return $info[$key] ?? FALSE; | |
509 | } | |
510 | ||
6a488035 | 511 | /** |
fe482240 | 512 | * Determine the status of all extensions. |
6a488035 | 513 | * |
a6c01b45 CW |
514 | * @return array |
515 | * ($key => status_constant) | |
6a488035 TO |
516 | */ |
517 | public function getStatuses() { | |
518 | if (!is_array($this->statuses)) { | |
6542d699 TO |
519 | $compat = CRM_Extension_System::getCompatibilityInfo(); |
520 | ||
be2fb01f | 521 | $this->statuses = []; |
6a488035 TO |
522 | |
523 | foreach ($this->fullContainer->getKeys() as $key) { | |
524 | $this->statuses[$key] = self::STATUS_UNINSTALLED; | |
525 | } | |
526 | ||
527 | $sql = ' | |
528 | SELECT full_name, is_active | |
529 | FROM civicrm_extension | |
530 | '; | |
531 | $dao = CRM_Core_DAO::executeQuery($sql); | |
532 | while ($dao->fetch()) { | |
533 | try { | |
534 | $path = $this->fullContainer->getPath($dao->full_name); | |
535 | $codeExists = !empty($path) && is_dir($path); | |
0db6c3e1 TO |
536 | } |
537 | catch (CRM_Extension_Exception $e) { | |
6a488035 TO |
538 | $codeExists = FALSE; |
539 | } | |
6542d699 TO |
540 | if (!empty($compat[$dao->full_name]['force-uninstall'])) { |
541 | $this->statuses[$dao->full_name] = self::STATUS_UNINSTALLED; | |
542 | } | |
543 | elseif ($dao->is_active) { | |
6a488035 | 544 | $this->statuses[$dao->full_name] = $codeExists ? self::STATUS_INSTALLED : self::STATUS_INSTALLED_MISSING; |
0db6c3e1 TO |
545 | } |
546 | else { | |
6a488035 TO |
547 | $this->statuses[$dao->full_name] = $codeExists ? self::STATUS_DISABLED : self::STATUS_DISABLED_MISSING; |
548 | } | |
549 | } | |
550 | } | |
551 | return $this->statuses; | |
552 | } | |
553 | ||
554 | public function refresh() { | |
555 | $this->statuses = NULL; | |
7b966967 SL |
556 | // and, indirectly, defaultContainer |
557 | $this->fullContainer->refresh(); | |
6a488035 TO |
558 | $this->mapper->refresh(); |
559 | } | |
560 | ||
20429eb9 RL |
561 | /** |
562 | * Return current processes for given extension. | |
563 | * | |
564 | * @param String $key extension key | |
565 | * | |
566 | * @return array | |
567 | */ | |
568 | public function getActiveProcesses(string $key) :Array { | |
569 | return $this->processes[$key] ?? []; | |
570 | } | |
571 | ||
572 | /** | |
573 | * Determine if the extension specified is currently involved in an install | |
574 | * or enable process. Just sugar code to make things more readable. | |
575 | * | |
576 | * @param String $key extension key | |
577 | * | |
578 | * @return bool | |
579 | */ | |
580 | public function extensionIsBeingInstalledOrEnabled($key) :bool { | |
581 | foreach ($this->getActiveProcesses($key) as $process) { | |
582 | if (in_array($process, ['install', 'installing', 'enable', 'enabling'])) { | |
583 | return TRUE; | |
584 | } | |
585 | } | |
586 | return FALSE; | |
587 | } | |
588 | ||
6a488035 TO |
589 | // ---------------------- |
590 | ||
591 | /** | |
592 | * Find the $info and $typeManager for a $key | |
593 | * | |
77b97be7 EM |
594 | * @param $key |
595 | * | |
6a488035 | 596 | * @throws CRM_Extension_Exception |
a6c01b45 | 597 | * @return array |
25fcba46 | 598 | * [CRM_Extension_Info, CRM_Extension_Manager_Interface] |
6a488035 TO |
599 | */ |
600 | private function _getInfoTypeHandler($key) { | |
7b966967 SL |
601 | // throws Exception |
602 | $info = $this->mapper->keyToInfo($key); | |
6a488035 | 603 | if (array_key_exists($info->type, $this->typeManagers)) { |
be2fb01f | 604 | return [$info, $this->typeManagers[$info->type]]; |
0db6c3e1 TO |
605 | } |
606 | else { | |
6a488035 TO |
607 | throw new CRM_Extension_Exception("Unrecognized extension type: " . $info->type); |
608 | } | |
609 | } | |
610 | ||
611 | /** | |
612 | * Find the $info and $typeManager for a $key | |
613 | * | |
2a6da8d7 EM |
614 | * @param $key |
615 | * | |
6a488035 | 616 | * @throws CRM_Extension_Exception |
a6c01b45 | 617 | * @return array |
25fcba46 | 618 | * [CRM_Extension_Info, CRM_Extension_Manager_Interface] |
6a488035 TO |
619 | */ |
620 | private function _getMissingInfoTypeHandler($key) { | |
621 | $info = $this->createInfoFromDB($key); | |
622 | if ($info) { | |
623 | if (array_key_exists($info->type, $this->typeManagers)) { | |
be2fb01f | 624 | return [$info, $this->typeManagers[$info->type]]; |
0db6c3e1 TO |
625 | } |
626 | else { | |
6a488035 TO |
627 | throw new CRM_Extension_Exception("Unrecognized extension type: " . $info->type); |
628 | } | |
0db6c3e1 TO |
629 | } |
630 | else { | |
6a488035 TO |
631 | throw new CRM_Extension_Exception("Failed to reconstruct missing extension: " . $key); |
632 | } | |
633 | } | |
634 | ||
e0ef6999 EM |
635 | /** |
636 | * @param CRM_Extension_Info $info | |
637 | * | |
638 | * @return bool | |
639 | */ | |
6a488035 TO |
640 | private function _createExtensionEntry(CRM_Extension_Info $info) { |
641 | $dao = new CRM_Core_DAO_Extension(); | |
642 | $dao->label = $info->label; | |
643 | $dao->name = $info->name; | |
644 | $dao->full_name = $info->key; | |
645 | $dao->type = $info->type; | |
646 | $dao->file = $info->file; | |
647 | $dao->is_active = 1; | |
648 | return (bool) ($dao->insert()); | |
649 | } | |
650 | ||
e0ef6999 EM |
651 | /** |
652 | * @param CRM_Extension_Info $info | |
653 | * | |
654 | * @return bool | |
655 | */ | |
6a488035 TO |
656 | private function _updateExtensionEntry(CRM_Extension_Info $info) { |
657 | $dao = new CRM_Core_DAO_Extension(); | |
658 | $dao->full_name = $info->key; | |
659 | if ($dao->find(TRUE)) { | |
660 | $dao->label = $info->label; | |
661 | $dao->name = $info->name; | |
662 | $dao->full_name = $info->key; | |
663 | $dao->type = $info->type; | |
664 | $dao->file = $info->file; | |
665 | $dao->is_active = 1; | |
666 | return (bool) ($dao->update()); | |
0db6c3e1 TO |
667 | } |
668 | else { | |
6a488035 TO |
669 | return $this->_createExtensionEntry($info); |
670 | } | |
671 | } | |
672 | ||
e0ef6999 EM |
673 | /** |
674 | * @param CRM_Extension_Info $info | |
675 | * | |
676 | * @throws CRM_Extension_Exception | |
677 | */ | |
6a488035 TO |
678 | private function _removeExtensionEntry(CRM_Extension_Info $info) { |
679 | $dao = new CRM_Core_DAO_Extension(); | |
680 | $dao->full_name = $info->key; | |
681 | if ($dao->find(TRUE)) { | |
682 | if (CRM_Core_BAO_Extension::del($dao->id)) { | |
683 | CRM_Core_Session::setStatus(ts('Selected option value has been deleted.'), ts('Deleted'), 'success'); | |
0db6c3e1 TO |
684 | } |
685 | else { | |
6a488035 TO |
686 | throw new CRM_Extension_Exception("Failed to remove extension entry"); |
687 | } | |
688 | } // else: post-condition already satisified | |
689 | } | |
690 | ||
e0ef6999 EM |
691 | /** |
692 | * @param CRM_Extension_Info $info | |
693 | * @param $isActive | |
694 | */ | |
6a488035 | 695 | private function _setExtensionActive(CRM_Extension_Info $info, $isActive) { |
be2fb01f CW |
696 | CRM_Core_DAO::executeQuery('UPDATE civicrm_extension SET is_active = %1 where full_name = %2', [ |
697 | 1 => [$isActive, 'Integer'], | |
698 | 2 => [$info->key, 'String'], | |
699 | ]); | |
6a488035 TO |
700 | } |
701 | ||
702 | /** | |
703 | * Auto-generate a place-holder for a missing extension using info from | |
704 | * database. | |
705 | * | |
2a6da8d7 | 706 | * @param $key |
6a488035 TO |
707 | * @return CRM_Extension_Info|NULL |
708 | */ | |
709 | public function createInfoFromDB($key) { | |
710 | $dao = new CRM_Core_DAO_Extension(); | |
711 | $dao->full_name = $key; | |
712 | if ($dao->find(TRUE)) { | |
713 | $info = new CRM_Extension_Info($dao->full_name, $dao->type, $dao->name, $dao->label, $dao->file); | |
714 | return $info; | |
0db6c3e1 TO |
715 | } |
716 | else { | |
6a488035 TO |
717 | return NULL; |
718 | } | |
719 | } | |
96025800 | 720 | |
f8a7cfff TO |
721 | /** |
722 | * Build a list of extensions to install, in an order that will satisfy dependencies. | |
723 | * | |
724 | * @param array $keys | |
725 | * List of extensions to install. | |
19ec0aa5 MWMC |
726 | * @param \CRM_Extension_Info $info |
727 | * An extension info object that we should use instead of our local versions (eg. when checking for upgradeability). | |
728 | * | |
f8a7cfff TO |
729 | * @return array |
730 | * List of extension keys, including dependencies, in order of installation. | |
19ec0aa5 MWMC |
731 | * @throws \CRM_Extension_Exception |
732 | * @throws \MJS\TopSort\CircularDependencyException | |
733 | * @throws \MJS\TopSort\ElementNotFoundException | |
f8a7cfff | 734 | */ |
19ec0aa5 MWMC |
735 | public function findInstallRequirements($keys, $info = NULL) { |
736 | // Use our passed in info, or get the local versions | |
737 | if ($info) { | |
738 | $infos[$info->key] = $info; | |
739 | } | |
740 | else { | |
741 | $infos = $this->mapper->getAllInfos(); | |
742 | } | |
7b966967 SL |
743 | // array(string $key). |
744 | $todoKeys = array_unique($keys); | |
745 | // array(string $key => 1); | |
746 | $doneKeys = []; | |
f8a7cfff TO |
747 | $sorter = new \MJS\TopSort\Implementations\FixedArraySort(); |
748 | ||
749 | while (!empty($todoKeys)) { | |
750 | $key = array_shift($todoKeys); | |
751 | if (isset($doneKeys[$key])) { | |
752 | continue; | |
753 | } | |
754 | $doneKeys[$key] = 1; | |
755 | ||
756 | /** @var CRM_Extension_Info $info */ | |
757 | $info = @$infos[$key]; | |
758 | ||
19ec0aa5 | 759 | if ($info && $info->requires) { |
f8a7cfff | 760 | $sorter->add($key, $info->requires); |
3ea86448 | 761 | $todoKeys = array_merge($todoKeys, $info->requires); |
f8a7cfff TO |
762 | } |
763 | else { | |
be2fb01f | 764 | $sorter->add($key, []); |
f8a7cfff TO |
765 | } |
766 | } | |
767 | return $sorter->sort(); | |
768 | } | |
769 | ||
770 | /** | |
771 | * Build a list of extensions to remove, in an order that will satisfy dependencies. | |
772 | * | |
773 | * @param array $keys | |
774 | * List of extensions to install. | |
775 | * @return array | |
776 | * List of extension keys, including dependencies, in order of removal. | |
777 | */ | |
778 | public function findDisableRequirements($keys) { | |
be2fb01f | 779 | $INSTALLED = [ |
f8a7cfff TO |
780 | self::STATUS_INSTALLED, |
781 | self::STATUS_INSTALLED_MISSING, | |
be2fb01f | 782 | ]; |
f8a7cfff TO |
783 | $installedInfos = $this->filterInfosByStatus($this->mapper->getAllInfos(), $INSTALLED); |
784 | $revMap = CRM_Extension_Info::buildReverseMap($installedInfos); | |
785 | $todoKeys = array_unique($keys); | |
be2fb01f | 786 | $doneKeys = []; |
f8a7cfff TO |
787 | $sorter = new \MJS\TopSort\Implementations\FixedArraySort(); |
788 | ||
789 | while (!empty($todoKeys)) { | |
790 | $key = array_shift($todoKeys); | |
791 | if (isset($doneKeys[$key])) { | |
792 | continue; | |
793 | } | |
794 | $doneKeys[$key] = 1; | |
795 | ||
796 | if (isset($revMap[$key])) { | |
797 | $requiredBys = CRM_Utils_Array::collect('key', | |
798 | $this->filterInfosByStatus($revMap[$key], $INSTALLED)); | |
799 | $sorter->add($key, $requiredBys); | |
800 | $todoKeys = array_merge($todoKeys, $requiredBys); | |
801 | } | |
802 | else { | |
be2fb01f | 803 | $sorter->add($key, []); |
f8a7cfff TO |
804 | } |
805 | } | |
806 | return $sorter->sort(); | |
807 | } | |
808 | ||
20429eb9 RL |
809 | /** |
810 | * Provides way to set processes property for phpunit tests - not for general use. | |
811 | * | |
812 | * @param $processes | |
813 | */ | |
814 | public function setProcessesForTesting(array $processes) { | |
815 | $this->processes = $processes; | |
816 | } | |
817 | ||
f8a7cfff TO |
818 | /** |
819 | * @param $infos | |
820 | * @param $filterStatuses | |
821 | * @return array | |
822 | */ | |
823 | protected function filterInfosByStatus($infos, $filterStatuses) { | |
be2fb01f | 824 | $matches = []; |
f8a7cfff TO |
825 | foreach ($infos as $k => $v) { |
826 | if (in_array($this->getStatus($v->key), $filterStatuses)) { | |
827 | $matches[$k] = $v; | |
828 | } | |
829 | } | |
830 | return $matches; | |
831 | } | |
832 | ||
20429eb9 RL |
833 | /** |
834 | * Add a process to the stacks for the extensions. | |
835 | * | |
836 | * @param array $keys extensionKey | |
837 | * @param string $process one of: install|uninstall|enable|disable|installing|uninstalling|enabling|disabling | |
838 | */ | |
839 | protected function addProcess(array $keys, string $process) { | |
840 | foreach ($keys as $key) { | |
841 | $this->processes[$key][] = $process; | |
842 | } | |
843 | } | |
844 | ||
845 | /** | |
846 | * Pop the top op from the stacks for the extensions. | |
847 | * | |
848 | * @param array $keys extensionKey | |
849 | */ | |
850 | protected function popProcess(array $keys) { | |
851 | foreach ($keys as $key) { | |
852 | if (!empty($this->process[$key])) { | |
853 | array_pop($this->process[$key]); | |
854 | } | |
855 | } | |
856 | } | |
857 | ||
6a488035 | 858 | } |