3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 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 path to a local directory containing a copy of the new (inert) code
124 * @throws CRM_Extension_Exception
126 public function replace($tmpCodeDir) {
127 if (! $this->defaultContainer
) {
128 throw new CRM_Extension_Exception("Default extension container is not configured");
131 $newInfo = CRM_Extension_Info
::loadFromFile($tmpCodeDir . DIRECTORY_SEPARATOR
. CRM_Extension_Info
::FILENAME
);
132 $oldStatus = $this->getStatus($newInfo->key
);
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
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(
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
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;
164 throw new CRM_Extension_Exception("Cannot install or enable extension: {$newInfo->key}");
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");
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");
185 $this->_updateExtensionEntry($newInfo);
186 $typeManager->onPostReplace($oldInfo, $newInfo);
189 throw new CRM_Extension_Exception("Cannot install or enable extension: {$newInfo->key}");
193 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
197 * Add records of the extension to the database -- and enable it
199 * @param array $keys list of extension keys
201 * @throws CRM_Extension_Exception
203 public function install($keys) {
204 $origStatuses = $this->getStatuses();
206 // TODO: to mitigate the risk of crashing during installation, scan
207 // keys/statuses/types before doing anything
209 foreach ($keys as $key) {
210 list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception
212 switch ($origStatuses[$key]) {
213 case self
::STATUS_INSTALLED
:
216 case self
::STATUS_DISABLED
:
218 $typeManager->onPreEnable($info);
219 $this->_setExtensionActive($info, 1);
220 $typeManager->onPostEnable($info);
222 case self
::STATUS_UNINSTALLED
:
224 $typeManager->onPreInstall($info);
225 $this->_createExtensionEntry($info);
226 $typeManager->onPostInstall($info);
228 case self
::STATUS_UNKNOWN
:
230 throw new CRM_Extension_Exception("Cannot install or enable extension: $key");
234 $this->statuses
= NULL;
235 $this->mapper
->refresh();
236 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
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]));
242 switch ($origStatuses[$key]) {
243 case self
::STATUS_INSTALLED
:
246 case self
::STATUS_DISABLED
:
249 case self
::STATUS_UNINSTALLED
:
251 $typeManager->onPostPostInstall($info);
253 case self
::STATUS_UNKNOWN
:
255 throw new CRM_Extension_Exception("Cannot install or enable extension: $key");
262 * Add records of the extension to the database -- and enable it
264 * @param array $keys list of extension keys
266 * @throws CRM_Extension_Exception
268 public function enable($keys) {
269 $this->install($keys);
273 * Add records of the extension to the database -- and enable it
275 * @param array $keys list of extension keys
277 * @throws CRM_Extension_Exception
279 public function disable($keys) {
280 $origStatuses = $this->getStatuses();
282 // TODO: to mitigate the risk of crashing during installation, scan
283 // keys/statuses/types before doing anything
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);
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);
299 case self
::STATUS_DISABLED
:
300 case self
::STATUS_DISABLED_MISSING
:
301 case self
::STATUS_UNINSTALLED
:
304 case self
::STATUS_UNKNOWN
:
306 throw new CRM_Extension_Exception("Cannot disable unknown extension: $key");
310 $this->statuses
= NULL;
311 $this->mapper
->refresh();
312 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
316 * Remove all database references to an extension
318 * Add records of the extension to the database -- and enable it
320 * @param array $keys list of extension keys
322 * @throws CRM_Extension_Exception
324 public function uninstall($keys) {
325 $origStatuses = $this->getStatuses();
327 // TODO: to mitigate the risk of crashing during installation, scan
328 // keys/statuses/types before doing anything
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");
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);
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);
348 case self
::STATUS_UNINSTALLED
:
351 case self
::STATUS_UNKNOWN
:
353 throw new CRM_Extension_Exception("Cannot disable unknown extension: $key");
357 $this->statuses
= NULL;
358 $this->mapper
->refresh();
359 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
363 * Determine the status of an extension
367 * @return string constant (STATUS_INSTALLED, STATUS_DISABLED, STATUS_UNINSTALLED, STATUS_UNKNOWN)
369 public function getStatus($key) {
370 $statuses = $this->getStatuses();
371 if (array_key_exists($key, $statuses)) {
372 return $statuses[$key];
374 return self
::STATUS_UNKNOWN
;
379 * Determine the status of all extensions
381 * @return array ($key => status_constant)
383 public function getStatuses() {
384 if (!is_array($this->statuses
)) {
385 $this->statuses
= array();
387 foreach ($this->fullContainer
->getKeys() as $key) {
388 $this->statuses
[$key] = self
::STATUS_UNINSTALLED
;
392 SELECT full_name, is_active
393 FROM civicrm_extension
395 $dao = CRM_Core_DAO
::executeQuery($sql);
396 while ($dao->fetch()) {
398 $path = $this->fullContainer
->getPath($dao->full_name
);
399 $codeExists = !empty($path) && is_dir($path);
400 } catch (CRM_Extension_Exception
$e) {
403 if ($dao->is_active
) {
404 $this->statuses
[$dao->full_name
] = $codeExists ? self
::STATUS_INSTALLED
: self
::STATUS_INSTALLED_MISSING
;
406 $this->statuses
[$dao->full_name
] = $codeExists ? self
::STATUS_DISABLED
: self
::STATUS_DISABLED_MISSING
;
410 return $this->statuses
;
413 public function refresh() {
414 $this->statuses
= NULL;
415 $this->fullContainer
->refresh(); // and, indirectly, defaultContainer
416 $this->mapper
->refresh();
419 // ----------------------
422 * Find the $info and $typeManager for a $key
426 * @throws CRM_Extension_Exception
427 * @return array (0 => CRM_Extension_Info, 1 => CRM_Extension_Manager_Interface)
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
]);
434 throw new CRM_Extension_Exception("Unrecognized extension type: " . $info->type
);
439 * Find the $info and $typeManager for a $key
443 * @throws CRM_Extension_Exception
444 * @return array (0 => CRM_Extension_Info, 1 => CRM_Extension_Manager_Interface)
446 private function _getMissingInfoTypeHandler($key) {
447 $info = $this->createInfoFromDB($key);
449 if (array_key_exists($info->type
, $this->typeManagers
)) {
450 return array($info, $this->typeManagers
[$info->type
]);
452 throw new CRM_Extension_Exception("Unrecognized extension type: " . $info->type
);
455 throw new CRM_Extension_Exception("Failed to reconstruct missing extension: " . $key);
460 * @param CRM_Extension_Info $info
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
;
472 return (bool) ($dao->insert());
476 * @param CRM_Extension_Info $info
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
;
490 return (bool) ($dao->update());
492 return $this->_createExtensionEntry($info);
497 * @param CRM_Extension_Info $info
499 * @throws CRM_Extension_Exception
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');
508 throw new CRM_Extension_Exception("Failed to remove extension entry");
510 } // else: post-condition already satisified
514 * @param CRM_Extension_Info $info
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'),
525 * Auto-generate a place-holder for a missing extension using info from
529 * @return CRM_Extension_Info|NULL
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
);