3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * The extension manager handles installing, disabling enabling, and
30 * uninstalling extensions.
33 * @copyright CiviCRM LLC (c) 2004-2014
37 class CRM_Extension_Manager
{
39 * The extension is fully installed and enabled
41 const STATUS_INSTALLED
= 'installed';
44 * The extension config has been applied to database but deactivated
46 const STATUS_DISABLED
= 'disabled';
49 * The extension code is visible, but nothing has been applied to DB
51 const STATUS_UNINSTALLED
= 'uninstalled';
54 * The extension code is not locally accessible
56 const STATUS_UNKNOWN
= 'unknown';
59 * The extension is fully installed and enabled
61 const STATUS_INSTALLED_MISSING
= 'installed-missing';
64 * The extension is fully installed and enabled
66 const STATUS_DISABLED_MISSING
= 'disabled-missing';
69 * @var CRM_Extension_Container_Interface
71 * Note: Treat as private. This is only public to facilitate debugging.
73 public $fullContainer;
76 * @var CRM_Extension_Container_Basic|FALSE
78 * Note: Treat as private. This is only public to facilitate debugging.
80 public $defaultContainer;
83 * @var CRM_Extension_Mapper
85 * Note: Treat as private. This is only public to facilitate debugging.
90 * @var array (typeName => CRM_Extension_Manager_Interface)
92 * Note: Treat as private. This is only public to facilitate debugging.
97 * @var array (extensionKey => statusConstant)
99 * Note: Treat as private. This is only public to facilitate debugging.
104 * @param CRM_Extension_Container_Interface $fullContainer
105 * @param CRM_Extension_Container_Basic|FALSE $defaultContainer
106 * @param CRM_Extension_Mapper $mapper
107 * @param $typeManagers
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;
117 * Install or upgrade the code for an extension -- and perform any
118 * necessary database changes (eg replacing extension metadata).
120 * This only works if the extension is stored in the default container.
122 * @param string $tmpCodeDir
123 * Path to a local directory containing a copy of the new (inert) code.
125 * @throws CRM_Extension_Exception
127 public function replace($tmpCodeDir) {
128 if (! $this->defaultContainer
) {
129 throw new CRM_Extension_Exception("Default extension container is not configured");
132 $newInfo = CRM_Extension_Info
::loadFromFile($tmpCodeDir . DIRECTORY_SEPARATOR
. CRM_Extension_Info
::FILENAME
);
133 $oldStatus = $this->getStatus($newInfo->key
);
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
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(
153 case self
::STATUS_INSTALLED_MISSING
:
154 case self
::STATUS_DISABLED_MISSING
:
155 // the extension does not exist in any container; we're free to put it anywhere
156 $tgtPath = $this->defaultContainer
->getBaseDir() . DIRECTORY_SEPARATOR
. $newInfo->key
;
157 list ($oldInfo, $typeManager) = $this->_getMissingInfoTypeHandler($newInfo->key
); // throws Exception
159 case self
::STATUS_UNKNOWN
:
160 // the extension does not exist in any container; we're free to put it anywhere
161 $tgtPath = $this->defaultContainer
->getBaseDir() . DIRECTORY_SEPARATOR
. $newInfo->key
;
162 $oldInfo = $typeManager = NULL;
165 throw new CRM_Extension_Exception("Cannot install or enable extension: {$newInfo->key}");
169 switch ($oldStatus) {
170 case self
::STATUS_UNINSTALLED
:
171 case self
::STATUS_UNKNOWN
:
172 // There are no DB records to worry about, so we'll just put the files in place
173 if (!CRM_Utils_File
::replaceDir($tmpCodeDir, $tgtPath)) {
174 throw new CRM_Extension_Exception("Failed to move $tmpCodeDir to $tgtPath");
177 case self
::STATUS_INSTALLED
:
178 case self
::STATUS_INSTALLED_MISSING
:
179 case self
::STATUS_DISABLED
:
180 case self
::STATUS_DISABLED_MISSING
:
181 // There are DB records; coordinate the file placement with the DB updates
182 $typeManager->onPreReplace($oldInfo, $newInfo);
183 if (!CRM_Utils_File
::replaceDir($tmpCodeDir, $tgtPath)) {
184 throw new CRM_Extension_Exception("Failed to move $tmpCodeDir to $tgtPath");
186 $this->_updateExtensionEntry($newInfo);
187 $typeManager->onPostReplace($oldInfo, $newInfo);
190 throw new CRM_Extension_Exception("Cannot install or enable extension: {$newInfo->key}");
194 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
198 * Add records of the extension to the database -- and enable it
201 * List of extension keys.
203 * @throws CRM_Extension_Exception
205 public function install($keys) {
206 $origStatuses = $this->getStatuses();
208 // TODO: to mitigate the risk of crashing during installation, scan
209 // keys/statuses/types before doing anything
211 foreach ($keys as $key) {
212 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
214 switch ($origStatuses[$key]) {
215 case self
::STATUS_INSTALLED
:
218 case self
::STATUS_DISABLED
:
220 $typeManager->onPreEnable($info);
221 $this->_setExtensionActive($info, 1);
222 $typeManager->onPostEnable($info);
224 case self
::STATUS_UNINSTALLED
:
226 $typeManager->onPreInstall($info);
227 $this->_createExtensionEntry($info);
228 $typeManager->onPostInstall($info);
230 case self
::STATUS_UNKNOWN
:
232 throw new CRM_Extension_Exception("Cannot install or enable extension: $key");
236 $this->statuses
= NULL;
237 $this->mapper
->refresh();
238 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
240 foreach ($keys as $key) {
241 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
242 //print_r(array('post post?', $info, 'k' => $key, 'os'=> $origStatuses[$key]));
244 switch ($origStatuses[$key]) {
245 case self
::STATUS_INSTALLED
:
248 case self
::STATUS_DISABLED
:
251 case self
::STATUS_UNINSTALLED
:
253 $typeManager->onPostPostInstall($info);
255 case self
::STATUS_UNKNOWN
:
257 throw new CRM_Extension_Exception("Cannot install or enable extension: $key");
264 * Add records of the extension to the database -- and enable it
267 * List of extension keys.
269 * @throws CRM_Extension_Exception
271 public function enable($keys) {
272 $this->install($keys);
276 * Add records of the extension to the database -- and enable it
279 * List of extension keys.
281 * @throws CRM_Extension_Exception
283 public function disable($keys) {
284 $origStatuses = $this->getStatuses();
286 // TODO: to mitigate the risk of crashing during installation, scan
287 // keys/statuses/types before doing anything
289 foreach ($keys as $key) {
290 switch ($origStatuses[$key]) {
291 case self
::STATUS_INSTALLED
:
292 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
293 $typeManager->onPreDisable($info);
294 $this->_setExtensionActive($info, 0);
295 $typeManager->onPostDisable($info);
297 case self
::STATUS_INSTALLED_MISSING
:
298 list ($info, $typeManager) = $this->_getMissingInfoTypeHandler($key); // throws Exception
299 $typeManager->onPreDisable($info);
300 $this->_setExtensionActive($info, 0);
301 $typeManager->onPostDisable($info);
303 case self
::STATUS_DISABLED
:
304 case self
::STATUS_DISABLED_MISSING
:
305 case self
::STATUS_UNINSTALLED
:
308 case self
::STATUS_UNKNOWN
:
310 throw new CRM_Extension_Exception("Cannot disable unknown extension: $key");
314 $this->statuses
= NULL;
315 $this->mapper
->refresh();
316 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
320 * Remove all database references to an extension
322 * Add records of the extension to the database -- and enable it
325 * List of extension keys.
327 * @throws CRM_Extension_Exception
329 public function uninstall($keys) {
330 $origStatuses = $this->getStatuses();
332 // TODO: to mitigate the risk of crashing during installation, scan
333 // keys/statuses/types before doing anything
335 foreach ($keys as $key) {
336 switch ($origStatuses[$key]) {
337 case self
::STATUS_INSTALLED
:
338 case self
::STATUS_INSTALLED_MISSING
:
339 throw new CRM_Extension_Exception("Cannot uninstall extension; disable it first: $key");
341 case self
::STATUS_DISABLED
:
342 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
343 $typeManager->onPreUninstall($info);
344 $this->_removeExtensionEntry($info);
345 $typeManager->onPostUninstall($info);
347 case self
::STATUS_DISABLED_MISSING
:
348 list ($info, $typeManager) = $this->_getMissingInfoTypeHandler($key); // throws Exception
349 $typeManager->onPreUninstall($info);
350 $this->_removeExtensionEntry($info);
351 $typeManager->onPostUninstall($info);
353 case self
::STATUS_UNINSTALLED
:
356 case self
::STATUS_UNKNOWN
:
358 throw new CRM_Extension_Exception("Cannot disable unknown extension: $key");
362 $this->statuses
= NULL;
363 $this->mapper
->refresh();
364 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
368 * Determine the status of an extension
372 * @return string constant (STATUS_INSTALLED, STATUS_DISABLED, STATUS_UNINSTALLED, STATUS_UNKNOWN)
374 public function getStatus($key) {
375 $statuses = $this->getStatuses();
376 if (array_key_exists($key, $statuses)) {
377 return $statuses[$key];
379 return self
::STATUS_UNKNOWN
;
384 * Determine the status of all extensions
386 * @return array ($key => status_constant)
388 public function getStatuses() {
389 if (!is_array($this->statuses
)) {
390 $this->statuses
= array();
392 foreach ($this->fullContainer
->getKeys() as $key) {
393 $this->statuses
[$key] = self
::STATUS_UNINSTALLED
;
397 SELECT full_name, is_active
398 FROM civicrm_extension
400 $dao = CRM_Core_DAO
::executeQuery($sql);
401 while ($dao->fetch()) {
403 $path = $this->fullContainer
->getPath($dao->full_name
);
404 $codeExists = !empty($path) && is_dir($path);
405 } catch (CRM_Extension_Exception
$e) {
408 if ($dao->is_active
) {
409 $this->statuses
[$dao->full_name
] = $codeExists ? self
::STATUS_INSTALLED
: self
::STATUS_INSTALLED_MISSING
;
411 $this->statuses
[$dao->full_name
] = $codeExists ? self
::STATUS_DISABLED
: self
::STATUS_DISABLED_MISSING
;
415 return $this->statuses
;
418 public function refresh() {
419 $this->statuses
= NULL;
420 $this->fullContainer
->refresh(); // and, indirectly, defaultContainer
421 $this->mapper
->refresh();
424 // ----------------------
427 * Find the $info and $typeManager for a $key
431 * @throws CRM_Extension_Exception
432 * @return array (0 => CRM_Extension_Info, 1 => CRM_Extension_Manager_Interface)
434 private function _getInfoTypeHandler($key) {
435 $info = $this->mapper
->keyToInfo($key); // throws Exception
436 if (array_key_exists($info->type
, $this->typeManagers
)) {
437 return array($info, $this->typeManagers
[$info->type
]);
439 throw new CRM_Extension_Exception("Unrecognized extension type: " . $info->type
);
444 * Find the $info and $typeManager for a $key
448 * @throws CRM_Extension_Exception
449 * @return array (0 => CRM_Extension_Info, 1 => CRM_Extension_Manager_Interface)
451 private function _getMissingInfoTypeHandler($key) {
452 $info = $this->createInfoFromDB($key);
454 if (array_key_exists($info->type
, $this->typeManagers
)) {
455 return array($info, $this->typeManagers
[$info->type
]);
457 throw new CRM_Extension_Exception("Unrecognized extension type: " . $info->type
);
460 throw new CRM_Extension_Exception("Failed to reconstruct missing extension: " . $key);
465 * @param CRM_Extension_Info $info
469 private function _createExtensionEntry(CRM_Extension_Info
$info) {
470 $dao = new CRM_Core_DAO_Extension();
471 $dao->label
= $info->label
;
472 $dao->name
= $info->name
;
473 $dao->full_name
= $info->key
;
474 $dao->type
= $info->type
;
475 $dao->file
= $info->file
;
477 return (bool) ($dao->insert());
481 * @param CRM_Extension_Info $info
485 private function _updateExtensionEntry(CRM_Extension_Info
$info) {
486 $dao = new CRM_Core_DAO_Extension();
487 $dao->full_name
= $info->key
;
488 if ($dao->find(TRUE)) {
489 $dao->label
= $info->label
;
490 $dao->name
= $info->name
;
491 $dao->full_name
= $info->key
;
492 $dao->type
= $info->type
;
493 $dao->file
= $info->file
;
495 return (bool) ($dao->update());
497 return $this->_createExtensionEntry($info);
502 * @param CRM_Extension_Info $info
504 * @throws CRM_Extension_Exception
506 private function _removeExtensionEntry(CRM_Extension_Info
$info) {
507 $dao = new CRM_Core_DAO_Extension();
508 $dao->full_name
= $info->key
;
509 if ($dao->find(TRUE)) {
510 if (CRM_Core_BAO_Extension
::del($dao->id
)) {
511 CRM_Core_Session
::setStatus(ts('Selected option value has been deleted.'), ts('Deleted'), 'success');
513 throw new CRM_Extension_Exception("Failed to remove extension entry");
515 } // else: post-condition already satisified
519 * @param CRM_Extension_Info $info
522 private function _setExtensionActive(CRM_Extension_Info
$info, $isActive) {
523 CRM_Core_DAO
::executeQuery('UPDATE civicrm_extension SET is_active = %1 where full_name = %2', array(
524 1 => array($isActive, 'Integer'),
525 2 => array($info->key
, 'String'),
530 * Auto-generate a place-holder for a missing extension using info from
534 * @return CRM_Extension_Info|NULL
536 public function createInfoFromDB($key) {
537 $dao = new CRM_Core_DAO_Extension();
538 $dao->full_name
= $key;
539 if ($dao->find(TRUE)) {
540 $info = new CRM_Extension_Info($dao->full_name
, $dao->type
, $dao->name
, $dao->label
, $dao->file
);