CRM-14871: Rebuild caches after extensions are upgraded in a CiviCRM-managed way...
[civicrm-core.git] / CRM / Extension / Manager.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 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 path to a local directory containing a copy of the new (inert) code
123 * @return void
124 * @throws CRM_Extension_Exception
125 */
126 public function replace($tmpCodeDir) {
127 if (! $this->defaultContainer) {
128 throw new CRM_Extension_Exception("Default extension container is not configured");
129 }
130
131 $newInfo = CRM_Extension_Info::loadFromFile($tmpCodeDir . DIRECTORY_SEPARATOR . CRM_Extension_Info::FILENAME);
132 $oldStatus = $this->getStatus($newInfo->key);
133
134 // find $tgtPath, $oldInfo, $typeManager
135 switch ($oldStatus) {
136 case self::STATUS_UNINSTALLED:
137 case self::STATUS_INSTALLED:
138 case self::STATUS_DISABLED:
139 // There is an old copy of the extension. Try to install in the same place -- but it must go somewhere in the default-container
140 list ($oldInfo, $typeManager) = $this->_getInfoTypeHandler($newInfo->key); // throws Exception
141 $tgtPath = $this->fullContainer->getPath($newInfo->key);
142 if (! CRM_Utils_File::isChildPath($this->defaultContainer->getBaseDir(), $tgtPath)) {
143 // force installation in the default-container
144 $oldPath = $tgtPath;
145 $tgtPath = $this->defaultContainer->getBaseDir() . DIRECTORY_SEPARATOR . $newInfo->key;
146 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(
147 1 => $newInfo->key,
148 2 => $oldPath,
149 )));
150 }
151 break;
152 case self::STATUS_INSTALLED_MISSING:
153 case self::STATUS_DISABLED_MISSING:
154 // the extension does not exist in any container; we're free to put it anywhere
155 $tgtPath = $this->defaultContainer->getBaseDir() . DIRECTORY_SEPARATOR . $newInfo->key;
156 list ($oldInfo, $typeManager) = $this->_getMissingInfoTypeHandler($newInfo->key); // throws Exception
157 break;
158 case self::STATUS_UNKNOWN:
159 // the extension does not exist in any container; we're free to put it anywhere
160 $tgtPath = $this->defaultContainer->getBaseDir() . DIRECTORY_SEPARATOR . $newInfo->key;
161 $oldInfo = $typeManager = NULL;
162 break;
163 default:
164 throw new CRM_Extension_Exception("Cannot install or enable extension: {$newInfo->key}");
165 }
166
167 // move the code!
168 switch ($oldStatus) {
169 case self::STATUS_UNINSTALLED:
170 case self::STATUS_UNKNOWN:
171 // There are no DB records to worry about, so we'll just put the files in place
172 if (!CRM_Utils_File::replaceDir($tmpCodeDir, $tgtPath)) {
173 throw new CRM_Extension_Exception("Failed to move $tmpCodeDir to $tgtPath");
174 }
175 break;
176 case self::STATUS_INSTALLED:
177 case self::STATUS_INSTALLED_MISSING:
178 case self::STATUS_DISABLED:
179 case self::STATUS_DISABLED_MISSING:
180 // There are DB records; coordinate the file placement with the DB updates
181 $typeManager->onPreReplace($oldInfo, $newInfo);
182 if (!CRM_Utils_File::replaceDir($tmpCodeDir, $tgtPath)) {
183 throw new CRM_Extension_Exception("Failed to move $tmpCodeDir to $tgtPath");
184 }
185 $this->_updateExtensionEntry($newInfo);
186 $typeManager->onPostReplace($oldInfo, $newInfo);
187 break;
188 default:
189 throw new CRM_Extension_Exception("Cannot install or enable extension: {$newInfo->key}");
190 }
191
192 $this->refresh();
193 CRM_Core_Invoke::rebuildMenuAndCache(TRUE);
194 }
195
196 /**
197 * Add records of the extension to the database -- and enable it
198 *
199 * @param array $keys list of extension keys
200 * @return void
201 * @throws CRM_Extension_Exception
202 */
203 public function install($keys) {
204 $origStatuses = $this->getStatuses();
205
206 // TODO: to mitigate the risk of crashing during installation, scan
207 // keys/statuses/types before doing anything
208
209 foreach ($keys as $key) {
210 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
211
212 switch ($origStatuses[$key]) {
213 case self::STATUS_INSTALLED:
214 // ok, nothing to do
215 break;
216 case self::STATUS_DISABLED:
217 // re-enable it
218 $typeManager->onPreEnable($info);
219 $this->_setExtensionActive($info, 1);
220 $typeManager->onPostEnable($info);
221 break;
222 case self::STATUS_UNINSTALLED:
223 // install anew
224 $typeManager->onPreInstall($info);
225 $this->_createExtensionEntry($info);
226 $typeManager->onPostInstall($info);
227 break;
228 case self::STATUS_UNKNOWN:
229 default:
230 throw new CRM_Extension_Exception("Cannot install or enable extension: $key");
231 }
232 }
233
234 $this->statuses = NULL;
235 $this->mapper->refresh();
236 CRM_Core_Invoke::rebuildMenuAndCaches(TRUE);
237
238 foreach ($keys as $key) {
239 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
240 //print_r(array('post post?', $info, 'k' => $key, 'os'=> $origStatuses[$key]));
241
242 switch ($origStatuses[$key]) {
243 case self::STATUS_INSTALLED:
244 // ok, nothing to do
245 break;
246 case self::STATUS_DISABLED:
247 // re-enable it
248 break;
249 case self::STATUS_UNINSTALLED:
250 // install anew
251 $typeManager->onPostPostInstall($info);
252 break;
253 case self::STATUS_UNKNOWN:
254 default:
255 throw new CRM_Extension_Exception("Cannot install or enable extension: $key");
256 }
257 }
258
259 }
260
261 /**
262 * Add records of the extension to the database -- and enable it
263 *
264 * @param array $keys list of extension keys
265 * @return void
266 * @throws CRM_Extension_Exception
267 */
268 public function enable($keys) {
269 $this->install($keys);
270 }
271
272 /**
273 * Add records of the extension to the database -- and enable it
274 *
275 * @param array $keys list of extension keys
276 * @return void
277 * @throws CRM_Extension_Exception
278 */
279 public function disable($keys) {
280 $origStatuses = $this->getStatuses();
281
282 // TODO: to mitigate the risk of crashing during installation, scan
283 // keys/statuses/types before doing anything
284
285 foreach ($keys as $key) {
286 switch ($origStatuses[$key]) {
287 case self::STATUS_INSTALLED:
288 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
289 $typeManager->onPreDisable($info);
290 $this->_setExtensionActive($info, 0);
291 $typeManager->onPostDisable($info);
292 break;
293 case self::STATUS_INSTALLED_MISSING:
294 list ($info, $typeManager) = $this->_getMissingInfoTypeHandler($key); // throws Exception
295 $typeManager->onPreDisable($info);
296 $this->_setExtensionActive($info, 0);
297 $typeManager->onPostDisable($info);
298 break;
299 case self::STATUS_DISABLED:
300 case self::STATUS_DISABLED_MISSING:
301 case self::STATUS_UNINSTALLED:
302 // ok, nothing to do
303 break;
304 case self::STATUS_UNKNOWN:
305 default:
306 throw new CRM_Extension_Exception("Cannot disable unknown extension: $key");
307 }
308 }
309
310 $this->statuses = NULL;
311 $this->mapper->refresh();
312 CRM_Core_Invoke::rebuildMenuAndCaches(TRUE);
313 }
314
315 /**
316 * Remove all database references to an extension
317 *
318 * Add records of the extension to the database -- and enable it
319 *
320 * @param array $keys list of extension keys
321 * @return void
322 * @throws CRM_Extension_Exception
323 */
324 public function uninstall($keys) {
325 $origStatuses = $this->getStatuses();
326
327 // TODO: to mitigate the risk of crashing during installation, scan
328 // keys/statuses/types before doing anything
329
330 foreach ($keys as $key) {
331 switch ($origStatuses[$key]) {
332 case self::STATUS_INSTALLED:
333 case self::STATUS_INSTALLED_MISSING:
334 throw new CRM_Extension_Exception("Cannot uninstall extension; disable it first: $key");
335 break;
336 case self::STATUS_DISABLED:
337 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
338 $typeManager->onPreUninstall($info);
339 $this->_removeExtensionEntry($info);
340 $typeManager->onPostUninstall($info);
341 break;
342 case self::STATUS_DISABLED_MISSING:
343 list ($info, $typeManager) = $this->_getMissingInfoTypeHandler($key); // throws Exception
344 $typeManager->onPreUninstall($info);
345 $this->_removeExtensionEntry($info);
346 $typeManager->onPostUninstall($info);
347 break;
348 case self::STATUS_UNINSTALLED:
349 // ok, nothing to do
350 break;
351 case self::STATUS_UNKNOWN:
352 default:
353 throw new CRM_Extension_Exception("Cannot disable unknown extension: $key");
354 }
355 }
356
357 $this->statuses = NULL;
358 $this->mapper->refresh();
359 CRM_Core_Invoke::rebuildMenuAndCaches(TRUE);
360 }
361
362 /**
363 * Determine the status of an extension
364 *
365 * @param $key
366 *
367 * @return string constant (STATUS_INSTALLED, STATUS_DISABLED, STATUS_UNINSTALLED, STATUS_UNKNOWN)
368 */
369 public function getStatus($key) {
370 $statuses = $this->getStatuses();
371 if (array_key_exists($key, $statuses)) {
372 return $statuses[$key];
373 } else {
374 return self::STATUS_UNKNOWN;
375 }
376 }
377
378 /**
379 * Determine the status of all extensions
380 *
381 * @return array ($key => status_constant)
382 */
383 public function getStatuses() {
384 if (!is_array($this->statuses)) {
385 $this->statuses = array();
386
387 foreach ($this->fullContainer->getKeys() as $key) {
388 $this->statuses[$key] = self::STATUS_UNINSTALLED;
389 }
390
391 $sql = '
392 SELECT full_name, is_active
393 FROM civicrm_extension
394 ';
395 $dao = CRM_Core_DAO::executeQuery($sql);
396 while ($dao->fetch()) {
397 try {
398 $path = $this->fullContainer->getPath($dao->full_name);
399 $codeExists = !empty($path) && is_dir($path);
400 } catch (CRM_Extension_Exception $e) {
401 $codeExists = FALSE;
402 }
403 if ($dao->is_active) {
404 $this->statuses[$dao->full_name] = $codeExists ? self::STATUS_INSTALLED : self::STATUS_INSTALLED_MISSING;
405 } else {
406 $this->statuses[$dao->full_name] = $codeExists ? self::STATUS_DISABLED : self::STATUS_DISABLED_MISSING;
407 }
408 }
409 }
410 return $this->statuses;
411 }
412
413 public function refresh() {
414 $this->statuses = NULL;
415 $this->fullContainer->refresh(); // and, indirectly, defaultContainer
416 $this->mapper->refresh();
417 }
418
419 // ----------------------
420
421 /**
422 * Find the $info and $typeManager for a $key
423 *
424 * @param $key
425 *
426 * @throws CRM_Extension_Exception
427 * @return array (0 => CRM_Extension_Info, 1 => CRM_Extension_Manager_Interface)
428 */
429 private function _getInfoTypeHandler($key) {
430 $info = $this->mapper->keyToInfo($key); // throws Exception
431 if (array_key_exists($info->type, $this->typeManagers)) {
432 return array($info, $this->typeManagers[$info->type]);
433 } else {
434 throw new CRM_Extension_Exception("Unrecognized extension type: " . $info->type);
435 }
436 }
437
438 /**
439 * Find the $info and $typeManager for a $key
440 *
441 * @param $key
442 *
443 * @throws CRM_Extension_Exception
444 * @return array (0 => CRM_Extension_Info, 1 => CRM_Extension_Manager_Interface)
445 */
446 private function _getMissingInfoTypeHandler($key) {
447 $info = $this->createInfoFromDB($key);
448 if ($info) {
449 if (array_key_exists($info->type, $this->typeManagers)) {
450 return array($info, $this->typeManagers[$info->type]);
451 } else {
452 throw new CRM_Extension_Exception("Unrecognized extension type: " . $info->type);
453 }
454 } else {
455 throw new CRM_Extension_Exception("Failed to reconstruct missing extension: " . $key);
456 }
457 }
458
459 /**
460 * @param CRM_Extension_Info $info
461 *
462 * @return bool
463 */
464 private function _createExtensionEntry(CRM_Extension_Info $info) {
465 $dao = new CRM_Core_DAO_Extension();
466 $dao->label = $info->label;
467 $dao->name = $info->name;
468 $dao->full_name = $info->key;
469 $dao->type = $info->type;
470 $dao->file = $info->file;
471 $dao->is_active = 1;
472 return (bool) ($dao->insert());
473 }
474
475 /**
476 * @param CRM_Extension_Info $info
477 *
478 * @return bool
479 */
480 private function _updateExtensionEntry(CRM_Extension_Info $info) {
481 $dao = new CRM_Core_DAO_Extension();
482 $dao->full_name = $info->key;
483 if ($dao->find(TRUE)) {
484 $dao->label = $info->label;
485 $dao->name = $info->name;
486 $dao->full_name = $info->key;
487 $dao->type = $info->type;
488 $dao->file = $info->file;
489 $dao->is_active = 1;
490 return (bool) ($dao->update());
491 } else {
492 return $this->_createExtensionEntry($info);
493 }
494 }
495
496 /**
497 * @param CRM_Extension_Info $info
498 *
499 * @throws CRM_Extension_Exception
500 */
501 private function _removeExtensionEntry(CRM_Extension_Info $info) {
502 $dao = new CRM_Core_DAO_Extension();
503 $dao->full_name = $info->key;
504 if ($dao->find(TRUE)) {
505 if (CRM_Core_BAO_Extension::del($dao->id)) {
506 CRM_Core_Session::setStatus(ts('Selected option value has been deleted.'), ts('Deleted'), 'success');
507 } else {
508 throw new CRM_Extension_Exception("Failed to remove extension entry");
509 }
510 } // else: post-condition already satisified
511 }
512
513 /**
514 * @param CRM_Extension_Info $info
515 * @param $isActive
516 */
517 private function _setExtensionActive(CRM_Extension_Info $info, $isActive) {
518 CRM_Core_DAO::executeQuery('UPDATE civicrm_extension SET is_active = %1 where full_name = %2', array(
519 1 => array($isActive, 'Integer'),
520 2 => array($info->key, 'String'),
521 ));
522 }
523
524 /**
525 * Auto-generate a place-holder for a missing extension using info from
526 * database.
527 *
528 * @param $key
529 * @return CRM_Extension_Info|NULL
530 */
531 public function createInfoFromDB($key) {
532 $dao = new CRM_Core_DAO_Extension();
533 $dao->full_name = $key;
534 if ($dao->find(TRUE)) {
535 $info = new CRM_Extension_Info($dao->full_name, $dao->type, $dao->name, $dao->label, $dao->file);
536 return $info;
537 } else {
538 return NULL;
539 }
540 }
541 }