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