3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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(
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
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;
168 throw new CRM_Extension_Exception("Cannot install or enable extension: {$newInfo->key}");
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");
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");
190 $this->_updateExtensionEntry($newInfo);
191 $typeManager->onPostReplace($oldInfo, $newInfo);
195 throw new CRM_Extension_Exception("Cannot install or enable extension: {$newInfo->key}");
199 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
203 * Add records of the extension to the database -- and enable it
206 * List of extension keys.
208 * @throws CRM_Extension_Exception
210 public function install($keys) {
211 $origStatuses = $this->getStatuses();
213 // TODO: to mitigate the risk of crashing during installation, scan
214 // keys/statuses/types before doing anything
216 foreach ($keys as $key) {
217 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
219 switch ($origStatuses[$key]) {
220 case self
::STATUS_INSTALLED
:
224 case self
::STATUS_DISABLED
:
226 $typeManager->onPreEnable($info);
227 $this->_setExtensionActive($info, 1);
228 $typeManager->onPostEnable($info);
231 case self
::STATUS_UNINSTALLED
:
233 $typeManager->onPreInstall($info);
234 $this->_createExtensionEntry($info);
235 $typeManager->onPostInstall($info);
238 case self
::STATUS_UNKNOWN
:
240 throw new CRM_Extension_Exception("Cannot install or enable extension: $key");
244 $this->statuses
= NULL;
245 $this->mapper
->refresh();
246 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
248 foreach ($keys as $key) {
249 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
250 //print_r(array('post post?', $info, 'k' => $key, 'os'=> $origStatuses[$key]));
252 switch ($origStatuses[$key]) {
253 case self
::STATUS_INSTALLED
:
257 case self
::STATUS_DISABLED
:
261 case self
::STATUS_UNINSTALLED
:
263 $typeManager->onPostPostInstall($info);
266 case self
::STATUS_UNKNOWN
:
268 throw new CRM_Extension_Exception("Cannot install or enable extension: $key");
275 * Add records of the extension to the database -- and enable it
278 * List of extension keys.
280 * @throws CRM_Extension_Exception
282 public function enable($keys) {
283 $this->install($keys);
287 * Add records of the extension to the database -- and enable it
290 * List of extension keys.
292 * @throws CRM_Extension_Exception
294 public function disable($keys) {
295 $origStatuses = $this->getStatuses();
297 // TODO: to mitigate the risk of crashing during installation, scan
298 // keys/statuses/types before doing anything
300 foreach ($keys as $key) {
301 switch ($origStatuses[$key]) {
302 case self
::STATUS_INSTALLED
:
303 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
304 $typeManager->onPreDisable($info);
305 $this->_setExtensionActive($info, 0);
306 $typeManager->onPostDisable($info);
309 case self
::STATUS_INSTALLED_MISSING
:
310 list ($info, $typeManager) = $this->_getMissingInfoTypeHandler($key); // throws Exception
311 $typeManager->onPreDisable($info);
312 $this->_setExtensionActive($info, 0);
313 $typeManager->onPostDisable($info);
316 case self
::STATUS_DISABLED
:
317 case self
::STATUS_DISABLED_MISSING
:
318 case self
::STATUS_UNINSTALLED
:
322 case self
::STATUS_UNKNOWN
:
324 throw new CRM_Extension_Exception("Cannot disable unknown extension: $key");
328 $this->statuses
= NULL;
329 $this->mapper
->refresh();
330 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
334 * Remove all database references to an extension.
336 * Add records of the extension to the database -- and enable it
339 * List of extension keys.
341 * @throws CRM_Extension_Exception
343 public function uninstall($keys) {
344 $origStatuses = $this->getStatuses();
346 // TODO: to mitigate the risk of crashing during installation, scan
347 // keys/statuses/types before doing anything
349 foreach ($keys as $key) {
350 switch ($origStatuses[$key]) {
351 case self
::STATUS_INSTALLED
:
352 case self
::STATUS_INSTALLED_MISSING
:
353 throw new CRM_Extension_Exception("Cannot uninstall extension; disable it first: $key");
355 case self
::STATUS_DISABLED
:
356 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
357 $typeManager->onPreUninstall($info);
358 $this->_removeExtensionEntry($info);
359 $typeManager->onPostUninstall($info);
362 case self
::STATUS_DISABLED_MISSING
:
363 list ($info, $typeManager) = $this->_getMissingInfoTypeHandler($key); // throws Exception
364 $typeManager->onPreUninstall($info);
365 $this->_removeExtensionEntry($info);
366 $typeManager->onPostUninstall($info);
369 case self
::STATUS_UNINSTALLED
:
373 case self
::STATUS_UNKNOWN
:
375 throw new CRM_Extension_Exception("Cannot disable unknown extension: $key");
379 $this->statuses
= NULL;
380 $this->mapper
->refresh();
381 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
385 * Determine the status of an extension.
390 * constant (STATUS_INSTALLED, STATUS_DISABLED, STATUS_UNINSTALLED, STATUS_UNKNOWN)
392 public function getStatus($key) {
393 $statuses = $this->getStatuses();
394 if (array_key_exists($key, $statuses)) {
395 return $statuses[$key];
398 return self
::STATUS_UNKNOWN
;
403 * Determine the status of all extensions.
406 * ($key => status_constant)
408 public function getStatuses() {
409 if (!is_array($this->statuses
)) {
410 $this->statuses
= array();
412 foreach ($this->fullContainer
->getKeys() as $key) {
413 $this->statuses
[$key] = self
::STATUS_UNINSTALLED
;
417 SELECT full_name, is_active
418 FROM civicrm_extension
420 $dao = CRM_Core_DAO
::executeQuery($sql);
421 while ($dao->fetch()) {
423 $path = $this->fullContainer
->getPath($dao->full_name
);
424 $codeExists = !empty($path) && is_dir($path);
426 catch (CRM_Extension_Exception
$e) {
429 if ($dao->is_active
) {
430 $this->statuses
[$dao->full_name
] = $codeExists ? self
::STATUS_INSTALLED
: self
::STATUS_INSTALLED_MISSING
;
433 $this->statuses
[$dao->full_name
] = $codeExists ? self
::STATUS_DISABLED
: self
::STATUS_DISABLED_MISSING
;
437 return $this->statuses
;
440 public function refresh() {
441 $this->statuses
= NULL;
442 $this->fullContainer
->refresh(); // and, indirectly, defaultContainer
443 $this->mapper
->refresh();
446 // ----------------------
449 * Find the $info and $typeManager for a $key
453 * @throws CRM_Extension_Exception
455 * (0 => CRM_Extension_Info, 1 => CRM_Extension_Manager_Interface)
457 private function _getInfoTypeHandler($key) {
458 $info = $this->mapper
->keyToInfo($key); // throws Exception
459 if (array_key_exists($info->type
, $this->typeManagers
)) {
460 return array($info, $this->typeManagers
[$info->type
]);
463 throw new CRM_Extension_Exception("Unrecognized extension type: " . $info->type
);
468 * Find the $info and $typeManager for a $key
472 * @throws CRM_Extension_Exception
474 * (0 => CRM_Extension_Info, 1 => CRM_Extension_Manager_Interface)
476 private function _getMissingInfoTypeHandler($key) {
477 $info = $this->createInfoFromDB($key);
479 if (array_key_exists($info->type
, $this->typeManagers
)) {
480 return array($info, $this->typeManagers
[$info->type
]);
483 throw new CRM_Extension_Exception("Unrecognized extension type: " . $info->type
);
487 throw new CRM_Extension_Exception("Failed to reconstruct missing extension: " . $key);
492 * @param CRM_Extension_Info $info
496 private function _createExtensionEntry(CRM_Extension_Info
$info) {
497 $dao = new CRM_Core_DAO_Extension();
498 $dao->label
= $info->label
;
499 $dao->name
= $info->name
;
500 $dao->full_name
= $info->key
;
501 $dao->type
= $info->type
;
502 $dao->file
= $info->file
;
504 return (bool) ($dao->insert());
508 * @param CRM_Extension_Info $info
512 private function _updateExtensionEntry(CRM_Extension_Info
$info) {
513 $dao = new CRM_Core_DAO_Extension();
514 $dao->full_name
= $info->key
;
515 if ($dao->find(TRUE)) {
516 $dao->label
= $info->label
;
517 $dao->name
= $info->name
;
518 $dao->full_name
= $info->key
;
519 $dao->type
= $info->type
;
520 $dao->file
= $info->file
;
522 return (bool) ($dao->update());
525 return $this->_createExtensionEntry($info);
530 * @param CRM_Extension_Info $info
532 * @throws CRM_Extension_Exception
534 private function _removeExtensionEntry(CRM_Extension_Info
$info) {
535 $dao = new CRM_Core_DAO_Extension();
536 $dao->full_name
= $info->key
;
537 if ($dao->find(TRUE)) {
538 if (CRM_Core_BAO_Extension
::del($dao->id
)) {
539 CRM_Core_Session
::setStatus(ts('Selected option value has been deleted.'), ts('Deleted'), 'success');
542 throw new CRM_Extension_Exception("Failed to remove extension entry");
544 } // else: post-condition already satisified
548 * @param CRM_Extension_Info $info
551 private function _setExtensionActive(CRM_Extension_Info
$info, $isActive) {
552 CRM_Core_DAO
::executeQuery('UPDATE civicrm_extension SET is_active = %1 where full_name = %2', array(
553 1 => array($isActive, 'Integer'),
554 2 => array($info->key
, 'String'),
559 * Auto-generate a place-holder for a missing extension using info from
563 * @return CRM_Extension_Info|NULL
565 public function createInfoFromDB($key) {
566 $dao = new CRM_Core_DAO_Extension();
567 $dao->full_name
= $key;
568 if ($dao->find(TRUE)) {
569 $info = new CRM_Extension_Info($dao->full_name
, $dao->type
, $dao->name
, $dao->label
, $dao->file
);