Merge pull request #7048 from eileenmcnaughton/CRM-17442
[civicrm-core.git] / CRM / Extension / Manager.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * The extension manager handles installing, disabling enabling, and
30 * uninstalling extensions.
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2015
34 * $Id$
35 *
36 */
37 class CRM_Extension_Manager {
38 /**
39 * The extension is fully installed and enabled.
40 */
41 const STATUS_INSTALLED = 'installed';
42
43 /**
44 * The extension config has been applied to database but deactivated.
45 */
46 const STATUS_DISABLED = 'disabled';
47
48 /**
49 * The extension code is visible, but nothing has been applied to DB
50 */
51 const STATUS_UNINSTALLED = 'uninstalled';
52
53 /**
54 * The extension code is not locally accessible
55 */
56 const STATUS_UNKNOWN = 'unknown';
57
58 /**
59 * The extension is fully installed and enabled
60 */
61 const STATUS_INSTALLED_MISSING = 'installed-missing';
62
63 /**
64 * The extension is fully installed and enabled
65 */
66 const STATUS_DISABLED_MISSING = 'disabled-missing';
67
68 /**
69 * @var CRM_Extension_Container_Interface
70 *
71 * Note: Treat as private. This is only public to facilitate debugging.
72 */
73 public $fullContainer;
74
75 /**
76 * @var CRM_Extension_Container_Basic|FALSE
77 *
78 * Note: Treat as private. This is only public to facilitate debugging.
79 */
80 public $defaultContainer;
81
82 /**
83 * @var CRM_Extension_Mapper
84 *
85 * Note: Treat as private. This is only public to facilitate debugging.
86 */
87 public $mapper;
88
89 /**
90 * @var array (typeName => CRM_Extension_Manager_Interface)
91 *
92 * Note: Treat as private. This is only public to facilitate debugging.
93 */
94 public $typeManagers;
95
96 /**
97 * @var array (extensionKey => statusConstant)
98 *
99 * Note: Treat as private. This is only public to facilitate debugging.
100 */
101 public $statuses;
102
103 /**
104 * @param CRM_Extension_Container_Interface $fullContainer
105 * @param CRM_Extension_Container_Basic|FALSE $defaultContainer
106 * @param CRM_Extension_Mapper $mapper
107 * @param $typeManagers
108 */
109 public function __construct(CRM_Extension_Container_Interface $fullContainer, $defaultContainer, CRM_Extension_Mapper $mapper, $typeManagers) {
110 $this->fullContainer = $fullContainer;
111 $this->defaultContainer = $defaultContainer;
112 $this->mapper = $mapper;
113 $this->typeManagers = $typeManagers;
114 }
115
116 /**
117 * Install or upgrade the code for an extension -- and perform any
118 * necessary database changes (eg replacing extension metadata).
119 *
120 * This only works if the extension is stored in the default container.
121 *
122 * @param string $tmpCodeDir
123 * Path to a local directory containing a copy of the new (inert) code.
124 * @return void
125 * @throws CRM_Extension_Exception
126 */
127 public function replace($tmpCodeDir) {
128 if (!$this->defaultContainer) {
129 throw new CRM_Extension_Exception("Default extension container is not configured");
130 }
131
132 $newInfo = CRM_Extension_Info::loadFromFile($tmpCodeDir . DIRECTORY_SEPARATOR . CRM_Extension_Info::FILENAME);
133 $oldStatus = $this->getStatus($newInfo->key);
134
135 // find $tgtPath, $oldInfo, $typeManager
136 switch ($oldStatus) {
137 case self::STATUS_UNINSTALLED:
138 case self::STATUS_INSTALLED:
139 case self::STATUS_DISABLED:
140 // There is an old copy of the extension. Try to install in the same place -- but it must go somewhere in the default-container
141 list ($oldInfo, $typeManager) = $this->_getInfoTypeHandler($newInfo->key); // throws Exception
142 $tgtPath = $this->fullContainer->getPath($newInfo->key);
143 if (!CRM_Utils_File::isChildPath($this->defaultContainer->getBaseDir(), $tgtPath)) {
144 // force installation in the default-container
145 $oldPath = $tgtPath;
146 $tgtPath = $this->defaultContainer->getBaseDir() . DIRECTORY_SEPARATOR . $newInfo->key;
147 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.', array(
148 1 => $newInfo->key,
149 2 => $oldPath,
150 )));
151 }
152 break;
153
154 case self::STATUS_INSTALLED_MISSING:
155 case self::STATUS_DISABLED_MISSING:
156 // the extension does not exist in any container; we're free to put it anywhere
157 $tgtPath = $this->defaultContainer->getBaseDir() . DIRECTORY_SEPARATOR . $newInfo->key;
158 list ($oldInfo, $typeManager) = $this->_getMissingInfoTypeHandler($newInfo->key); // throws Exception
159 break;
160
161 case self::STATUS_UNKNOWN:
162 // the extension does not exist in any container; we're free to put it anywhere
163 $tgtPath = $this->defaultContainer->getBaseDir() . DIRECTORY_SEPARATOR . $newInfo->key;
164 $oldInfo = $typeManager = NULL;
165 break;
166
167 default:
168 throw new CRM_Extension_Exception("Cannot install or enable extension: {$newInfo->key}");
169 }
170
171 // move the code!
172 switch ($oldStatus) {
173 case self::STATUS_UNINSTALLED:
174 case self::STATUS_UNKNOWN:
175 // There are no DB records to worry about, so we'll just put the files in place
176 if (!CRM_Utils_File::replaceDir($tmpCodeDir, $tgtPath)) {
177 throw new CRM_Extension_Exception("Failed to move $tmpCodeDir to $tgtPath");
178 }
179 break;
180
181 case self::STATUS_INSTALLED:
182 case self::STATUS_INSTALLED_MISSING:
183 case self::STATUS_DISABLED:
184 case self::STATUS_DISABLED_MISSING:
185 // There are DB records; coordinate the file placement with the DB updates
186 $typeManager->onPreReplace($oldInfo, $newInfo);
187 if (!CRM_Utils_File::replaceDir($tmpCodeDir, $tgtPath)) {
188 throw new CRM_Extension_Exception("Failed to move $tmpCodeDir to $tgtPath");
189 }
190 $this->_updateExtensionEntry($newInfo);
191 $typeManager->onPostReplace($oldInfo, $newInfo);
192 break;
193
194 default:
195 throw new CRM_Extension_Exception("Cannot install or enable extension: {$newInfo->key}");
196 }
197
198 $this->refresh();
199 CRM_Core_Invoke::rebuildMenuAndCaches(TRUE);
200 }
201
202 /**
203 * Add records of the extension to the database -- and enable it
204 *
205 * @param array $keys
206 * List of extension keys.
207 * @return void
208 * @throws CRM_Extension_Exception
209 */
210 public function install($keys) {
211 $origStatuses = $this->getStatuses();
212
213 // TODO: to mitigate the risk of crashing during installation, scan
214 // keys/statuses/types before doing anything
215
216 foreach ($keys as $key) {
217 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
218
219 switch ($origStatuses[$key]) {
220 case self::STATUS_INSTALLED:
221 // ok, nothing to do
222 break;
223
224 case self::STATUS_DISABLED:
225 // re-enable it
226 $typeManager->onPreEnable($info);
227 $this->_setExtensionActive($info, 1);
228 $typeManager->onPostEnable($info);
229 break;
230
231 case self::STATUS_UNINSTALLED:
232 // install anew
233 $typeManager->onPreInstall($info);
234 $this->_createExtensionEntry($info);
235 $typeManager->onPostInstall($info);
236 break;
237
238 case self::STATUS_UNKNOWN:
239 default:
240 throw new CRM_Extension_Exception("Cannot install or enable extension: $key");
241 }
242 }
243
244 $this->statuses = NULL;
245 $this->mapper->refresh();
246 CRM_Core_Invoke::rebuildMenuAndCaches(TRUE);
247 $schema = new CRM_Logging_Schema();
248 $schema->fixSchemaDifferences();
249
250 foreach ($keys as $key) {
251 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
252 //print_r(array('post post?', $info, 'k' => $key, 'os'=> $origStatuses[$key]));
253
254 switch ($origStatuses[$key]) {
255 case self::STATUS_INSTALLED:
256 // ok, nothing to do
257 break;
258
259 case self::STATUS_DISABLED:
260 // re-enable it
261 break;
262
263 case self::STATUS_UNINSTALLED:
264 // install anew
265 $typeManager->onPostPostInstall($info);
266 break;
267
268 case self::STATUS_UNKNOWN:
269 default:
270 throw new CRM_Extension_Exception("Cannot install or enable extension: $key");
271 }
272 }
273
274 }
275
276 /**
277 * Add records of the extension to the database -- and enable it
278 *
279 * @param array $keys
280 * List of extension keys.
281 * @return void
282 * @throws CRM_Extension_Exception
283 */
284 public function enable($keys) {
285 $this->install($keys);
286 }
287
288 /**
289 * Add records of the extension to the database -- and enable it
290 *
291 * @param array $keys
292 * List of extension keys.
293 * @return void
294 * @throws CRM_Extension_Exception
295 */
296 public function disable($keys) {
297 $origStatuses = $this->getStatuses();
298
299 // TODO: to mitigate the risk of crashing during installation, scan
300 // keys/statuses/types before doing anything
301
302 foreach ($keys as $key) {
303 switch ($origStatuses[$key]) {
304 case self::STATUS_INSTALLED:
305 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
306 $typeManager->onPreDisable($info);
307 $this->_setExtensionActive($info, 0);
308 $typeManager->onPostDisable($info);
309 break;
310
311 case self::STATUS_INSTALLED_MISSING:
312 list ($info, $typeManager) = $this->_getMissingInfoTypeHandler($key); // throws Exception
313 $typeManager->onPreDisable($info);
314 $this->_setExtensionActive($info, 0);
315 $typeManager->onPostDisable($info);
316 break;
317
318 case self::STATUS_DISABLED:
319 case self::STATUS_DISABLED_MISSING:
320 case self::STATUS_UNINSTALLED:
321 // ok, nothing to do
322 break;
323
324 case self::STATUS_UNKNOWN:
325 default:
326 throw new CRM_Extension_Exception("Cannot disable unknown extension: $key");
327 }
328 }
329
330 $this->statuses = NULL;
331 $this->mapper->refresh();
332 CRM_Core_Invoke::rebuildMenuAndCaches(TRUE);
333 }
334
335 /**
336 * Remove all database references to an extension.
337 *
338 * Add records of the extension to the database -- and enable it
339 *
340 * @param array $keys
341 * List of extension keys.
342 * @return void
343 * @throws CRM_Extension_Exception
344 */
345 public function uninstall($keys) {
346 $origStatuses = $this->getStatuses();
347
348 // TODO: to mitigate the risk of crashing during installation, scan
349 // keys/statuses/types before doing anything
350
351 foreach ($keys as $key) {
352 switch ($origStatuses[$key]) {
353 case self::STATUS_INSTALLED:
354 case self::STATUS_INSTALLED_MISSING:
355 throw new CRM_Extension_Exception("Cannot uninstall extension; disable it first: $key");
356
357 case self::STATUS_DISABLED:
358 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
359 $typeManager->onPreUninstall($info);
360 $this->_removeExtensionEntry($info);
361 $typeManager->onPostUninstall($info);
362 break;
363
364 case self::STATUS_DISABLED_MISSING:
365 list ($info, $typeManager) = $this->_getMissingInfoTypeHandler($key); // throws Exception
366 $typeManager->onPreUninstall($info);
367 $this->_removeExtensionEntry($info);
368 $typeManager->onPostUninstall($info);
369 break;
370
371 case self::STATUS_UNINSTALLED:
372 // ok, nothing to do
373 break;
374
375 case self::STATUS_UNKNOWN:
376 default:
377 throw new CRM_Extension_Exception("Cannot disable unknown extension: $key");
378 }
379 }
380
381 $this->statuses = NULL;
382 $this->mapper->refresh();
383 CRM_Core_Invoke::rebuildMenuAndCaches(TRUE);
384 }
385
386 /**
387 * Determine the status of an extension.
388 *
389 * @param $key
390 *
391 * @return string
392 * constant (STATUS_INSTALLED, STATUS_DISABLED, STATUS_UNINSTALLED, STATUS_UNKNOWN)
393 */
394 public function getStatus($key) {
395 $statuses = $this->getStatuses();
396 if (array_key_exists($key, $statuses)) {
397 return $statuses[$key];
398 }
399 else {
400 return self::STATUS_UNKNOWN;
401 }
402 }
403
404 /**
405 * Determine the status of all extensions.
406 *
407 * @return array
408 * ($key => status_constant)
409 */
410 public function getStatuses() {
411 if (!is_array($this->statuses)) {
412 $this->statuses = array();
413
414 foreach ($this->fullContainer->getKeys() as $key) {
415 $this->statuses[$key] = self::STATUS_UNINSTALLED;
416 }
417
418 $sql = '
419 SELECT full_name, is_active
420 FROM civicrm_extension
421 ';
422 $dao = CRM_Core_DAO::executeQuery($sql);
423 while ($dao->fetch()) {
424 try {
425 $path = $this->fullContainer->getPath($dao->full_name);
426 $codeExists = !empty($path) && is_dir($path);
427 }
428 catch (CRM_Extension_Exception $e) {
429 $codeExists = FALSE;
430 }
431 if ($dao->is_active) {
432 $this->statuses[$dao->full_name] = $codeExists ? self::STATUS_INSTALLED : self::STATUS_INSTALLED_MISSING;
433 }
434 else {
435 $this->statuses[$dao->full_name] = $codeExists ? self::STATUS_DISABLED : self::STATUS_DISABLED_MISSING;
436 }
437 }
438 }
439 return $this->statuses;
440 }
441
442 public function refresh() {
443 $this->statuses = NULL;
444 $this->fullContainer->refresh(); // and, indirectly, defaultContainer
445 $this->mapper->refresh();
446 }
447
448 // ----------------------
449
450 /**
451 * Find the $info and $typeManager for a $key
452 *
453 * @param $key
454 *
455 * @throws CRM_Extension_Exception
456 * @return array
457 * (0 => CRM_Extension_Info, 1 => CRM_Extension_Manager_Interface)
458 */
459 private function _getInfoTypeHandler($key) {
460 $info = $this->mapper->keyToInfo($key); // throws Exception
461 if (array_key_exists($info->type, $this->typeManagers)) {
462 return array($info, $this->typeManagers[$info->type]);
463 }
464 else {
465 throw new CRM_Extension_Exception("Unrecognized extension type: " . $info->type);
466 }
467 }
468
469 /**
470 * Find the $info and $typeManager for a $key
471 *
472 * @param $key
473 *
474 * @throws CRM_Extension_Exception
475 * @return array
476 * (0 => CRM_Extension_Info, 1 => CRM_Extension_Manager_Interface)
477 */
478 private function _getMissingInfoTypeHandler($key) {
479 $info = $this->createInfoFromDB($key);
480 if ($info) {
481 if (array_key_exists($info->type, $this->typeManagers)) {
482 return array($info, $this->typeManagers[$info->type]);
483 }
484 else {
485 throw new CRM_Extension_Exception("Unrecognized extension type: " . $info->type);
486 }
487 }
488 else {
489 throw new CRM_Extension_Exception("Failed to reconstruct missing extension: " . $key);
490 }
491 }
492
493 /**
494 * @param CRM_Extension_Info $info
495 *
496 * @return bool
497 */
498 private function _createExtensionEntry(CRM_Extension_Info $info) {
499 $dao = new CRM_Core_DAO_Extension();
500 $dao->label = $info->label;
501 $dao->name = $info->name;
502 $dao->full_name = $info->key;
503 $dao->type = $info->type;
504 $dao->file = $info->file;
505 $dao->is_active = 1;
506 return (bool) ($dao->insert());
507 }
508
509 /**
510 * @param CRM_Extension_Info $info
511 *
512 * @return bool
513 */
514 private function _updateExtensionEntry(CRM_Extension_Info $info) {
515 $dao = new CRM_Core_DAO_Extension();
516 $dao->full_name = $info->key;
517 if ($dao->find(TRUE)) {
518 $dao->label = $info->label;
519 $dao->name = $info->name;
520 $dao->full_name = $info->key;
521 $dao->type = $info->type;
522 $dao->file = $info->file;
523 $dao->is_active = 1;
524 return (bool) ($dao->update());
525 }
526 else {
527 return $this->_createExtensionEntry($info);
528 }
529 }
530
531 /**
532 * @param CRM_Extension_Info $info
533 *
534 * @throws CRM_Extension_Exception
535 */
536 private function _removeExtensionEntry(CRM_Extension_Info $info) {
537 $dao = new CRM_Core_DAO_Extension();
538 $dao->full_name = $info->key;
539 if ($dao->find(TRUE)) {
540 if (CRM_Core_BAO_Extension::del($dao->id)) {
541 CRM_Core_Session::setStatus(ts('Selected option value has been deleted.'), ts('Deleted'), 'success');
542 }
543 else {
544 throw new CRM_Extension_Exception("Failed to remove extension entry");
545 }
546 } // else: post-condition already satisified
547 }
548
549 /**
550 * @param CRM_Extension_Info $info
551 * @param $isActive
552 */
553 private function _setExtensionActive(CRM_Extension_Info $info, $isActive) {
554 CRM_Core_DAO::executeQuery('UPDATE civicrm_extension SET is_active = %1 where full_name = %2', array(
555 1 => array($isActive, 'Integer'),
556 2 => array($info->key, 'String'),
557 ));
558 }
559
560 /**
561 * Auto-generate a place-holder for a missing extension using info from
562 * database.
563 *
564 * @param $key
565 * @return CRM_Extension_Info|NULL
566 */
567 public function createInfoFromDB($key) {
568 $dao = new CRM_Core_DAO_Extension();
569 $dao->full_name = $key;
570 if ($dao->find(TRUE)) {
571 $info = new CRM_Extension_Info($dao->full_name, $dao->type, $dao->name, $dao->label, $dao->file);
572 return $info;
573 }
574 else {
575 return NULL;
576 }
577 }
578
579 }