3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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);
247 $schema = new CRM_Logging_Schema();
248 $schema->fixSchemaDifferences();
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]));
254 switch ($origStatuses[$key]) {
255 case self
::STATUS_INSTALLED
:
259 case self
::STATUS_DISABLED
:
263 case self
::STATUS_UNINSTALLED
:
265 $typeManager->onPostPostInstall($info);
268 case self
::STATUS_UNKNOWN
:
270 throw new CRM_Extension_Exception("Cannot install or enable extension: $key");
277 * Add records of the extension to the database -- and enable it
280 * List of extension keys.
282 * @throws CRM_Extension_Exception
284 public function enable($keys) {
285 $this->install($keys);
289 * Add records of the extension to the database -- and enable it
292 * List of extension keys.
294 * @throws CRM_Extension_Exception
296 public function disable($keys) {
297 $origStatuses = $this->getStatuses();
299 // TODO: to mitigate the risk of crashing during installation, scan
300 // keys/statuses/types before doing anything
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);
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);
318 case self
::STATUS_DISABLED
:
319 case self
::STATUS_DISABLED_MISSING
:
320 case self
::STATUS_UNINSTALLED
:
324 case self
::STATUS_UNKNOWN
:
326 throw new CRM_Extension_Exception("Cannot disable unknown extension: $key");
330 $this->statuses
= NULL;
331 $this->mapper
->refresh();
332 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
336 * Remove all database references to an extension.
338 * Add records of the extension to the database -- and enable it
341 * List of extension keys.
343 * @throws CRM_Extension_Exception
345 public function uninstall($keys) {
346 $origStatuses = $this->getStatuses();
348 // TODO: to mitigate the risk of crashing during installation, scan
349 // keys/statuses/types before doing anything
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");
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);
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);
371 case self
::STATUS_UNINSTALLED
:
375 case self
::STATUS_UNKNOWN
:
377 throw new CRM_Extension_Exception("Cannot disable unknown extension: $key");
381 $this->statuses
= NULL;
382 $this->mapper
->refresh();
383 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
387 * Determine the status of an extension.
392 * constant (STATUS_INSTALLED, STATUS_DISABLED, STATUS_UNINSTALLED, STATUS_UNKNOWN)
394 public function getStatus($key) {
395 $statuses = $this->getStatuses();
396 if (array_key_exists($key, $statuses)) {
397 return $statuses[$key];
400 return self
::STATUS_UNKNOWN
;
405 * Determine the status of all extensions.
408 * ($key => status_constant)
410 public function getStatuses() {
411 if (!is_array($this->statuses
)) {
412 $this->statuses
= array();
414 foreach ($this->fullContainer
->getKeys() as $key) {
415 $this->statuses
[$key] = self
::STATUS_UNINSTALLED
;
419 SELECT full_name, is_active
420 FROM civicrm_extension
422 $dao = CRM_Core_DAO
::executeQuery($sql);
423 while ($dao->fetch()) {
425 $path = $this->fullContainer
->getPath($dao->full_name
);
426 $codeExists = !empty($path) && is_dir($path);
428 catch (CRM_Extension_Exception
$e) {
431 if ($dao->is_active
) {
432 $this->statuses
[$dao->full_name
] = $codeExists ? self
::STATUS_INSTALLED
: self
::STATUS_INSTALLED_MISSING
;
435 $this->statuses
[$dao->full_name
] = $codeExists ? self
::STATUS_DISABLED
: self
::STATUS_DISABLED_MISSING
;
439 return $this->statuses
;
442 public function refresh() {
443 $this->statuses
= NULL;
444 $this->fullContainer
->refresh(); // and, indirectly, defaultContainer
445 $this->mapper
->refresh();
448 // ----------------------
451 * Find the $info and $typeManager for a $key
455 * @throws CRM_Extension_Exception
457 * (0 => CRM_Extension_Info, 1 => CRM_Extension_Manager_Interface)
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
]);
465 throw new CRM_Extension_Exception("Unrecognized extension type: " . $info->type
);
470 * Find the $info and $typeManager for a $key
474 * @throws CRM_Extension_Exception
476 * (0 => CRM_Extension_Info, 1 => CRM_Extension_Manager_Interface)
478 private function _getMissingInfoTypeHandler($key) {
479 $info = $this->createInfoFromDB($key);
481 if (array_key_exists($info->type
, $this->typeManagers
)) {
482 return array($info, $this->typeManagers
[$info->type
]);
485 throw new CRM_Extension_Exception("Unrecognized extension type: " . $info->type
);
489 throw new CRM_Extension_Exception("Failed to reconstruct missing extension: " . $key);
494 * @param CRM_Extension_Info $info
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
;
506 return (bool) ($dao->insert());
510 * @param CRM_Extension_Info $info
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
;
524 return (bool) ($dao->update());
527 return $this->_createExtensionEntry($info);
532 * @param CRM_Extension_Info $info
534 * @throws CRM_Extension_Exception
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');
544 throw new CRM_Extension_Exception("Failed to remove extension entry");
546 } // else: post-condition already satisified
550 * @param CRM_Extension_Info $info
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'),
561 * Auto-generate a place-holder for a missing extension using info from
565 * @return CRM_Extension_Info|NULL
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
);