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 +--------------------------------------------------------------------+
28 define('API_V3_EXTENSION_DELIMITER', ',');
32 * File for the CiviCRM APIv3 extension functions
34 * @package CiviCRM_APIv3
35 * @subpackage API_Extension
37 * @copyright CiviCRM LLC (c) 2004-2014
43 * Install an extension
45 * @param array $params
47 * - key: string, eg "com.example.myextension"
48 * - keys: mixed; array of string, eg array("com.example.myextension1", "com.example.myextension2") or string with comma-delimited list
49 * using 'keys' should be more performant than making multiple API calls with 'key'
54 function civicrm_api3_extension_install($params) {
55 $keys = _civicrm_api3_getKeys($params);
56 if (count($keys) == 0) {
57 return civicrm_api3_create_success();
61 CRM_Extension_System
::singleton()->getManager()->install($keys);
63 catch (CRM_Extension_Exception
$e) {
64 return civicrm_api3_create_error($e->getMessage());
67 return civicrm_api3_create_success();
71 * Upgrade an extension - runs upgrade_N hooks and system.flush
76 function civicrm_api3_extension_upgrade() {
77 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
78 $queue = CRM_Extension_Upgrades
::createQueue();
79 $runner = new CRM_Queue_Runner(array(
80 'title' => 'Extension Upgrades',
82 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
86 $result = $runner->runAll();
88 catch (CRM_Extension_Exception
$e) {
89 return civicrm_api3_create_error($e->getMessage());
92 if ($result === TRUE) {
93 return civicrm_api3_create_success();
101 * Enable an extension
103 * @param array $params
105 * - key: string, eg "com.example.myextension"
106 * - keys: mixed; array of string, eg array("com.example.myextension1", "com.example.myextension2") or string with comma-delimited list
107 * using 'keys' should be more performant than making multiple API calls with 'key'
112 function civicrm_api3_extension_enable($params) {
113 $keys = _civicrm_api3_getKeys($params);
114 if (count($keys) == 0) {
115 return civicrm_api3_create_success();
118 CRM_Extension_System
::singleton()->getManager()->enable($keys);
119 return civicrm_api3_create_success();
123 * Disable an extension
125 * @param array $params
127 * - key: string, eg "com.example.myextension"
128 * - keys: mixed; array of string, eg array("com.example.myextension1", "com.example.myextension2") or string with comma-delimited list
129 * using 'keys' should be more performant than making multiple API calls with 'key'
133 * @example ExtensionDisable.php
135 function civicrm_api3_extension_disable($params) {
136 $keys = _civicrm_api3_getKeys($params);
137 if (count($keys) == 0) {
138 return civicrm_api3_create_success();
141 CRM_Extension_System
::singleton()->getManager()->disable($keys);
142 return civicrm_api3_create_success();
146 * Uninstall an extension
148 * @param array $params
150 * - key: string, eg "com.example.myextension"
151 * - keys: array of string, eg array("com.example.myextension1", "com.example.myextension2")
152 * using 'keys' should be more performant than making multiple API calls with 'key'
153 * - removeFiles: bool, whether to remove source tree; default: FALSE
158 function civicrm_api3_extension_uninstall($params) {
159 $keys = _civicrm_api3_getKeys($params);
160 if (count($keys) == 0) {
161 return civicrm_api3_create_success();
164 // TODO // $removeFiles = CRM_Utils_Array::value('removeFiles', $params, FALSE);
165 CRM_Extension_System
::singleton()->getManager()->uninstall($keys);
166 return civicrm_api3_create_success();
170 * Download and install an extension
172 * @param array $params
174 * - key: string, eg "com.example.myextension"
175 * - url: string eg "http://repo.com/myextension-1.0.zip"
177 * @throws API_Exception
180 * @example ExtensionDownload.php
182 function civicrm_api3_extension_download($params) {
183 if (! array_key_exists('key', $params)) {
184 throw new API_Exception('Missing required parameter: key');
187 if (! array_key_exists('url', $params)) {
188 if (! CRM_Extension_System
::singleton()->getBrowser()->isEnabled()) {
189 throw new API_Exception('Automatic downloading is diabled. Try adding parameter "url"');
191 if ($reqs = CRM_Extension_System
::singleton()->getBrowser()->checkRequirements()) {
192 $first = array_shift($reqs);
193 throw new API_Exception($first['message']);
195 if ($info = CRM_Extension_System
::singleton()->getBrowser()->getExtension($params['key'])) {
196 if ($info->downloadUrl
) {
197 $params['url'] = $info->downloadUrl
;
202 if (! array_key_exists('url', $params)) {
203 throw new API_Exception('Cannot resolve download url for extension. Try adding parameter "url"');
206 foreach (CRM_Extension_System
::singleton()->getDownloader()->checkRequirements() as $requirement) {
207 return civicrm_api3_create_error($requirement['message']);
210 if (! CRM_Extension_System
::singleton()->getDownloader()->download($params['key'], $params['url'])) {
211 return civicrm_api3_create_error('Download failed - ZIP file is unavailable or malformed');
213 CRM_Extension_System
::singleton()->getCache()->flush();
214 CRM_Extension_System
::singleton(TRUE);
215 CRM_Extension_System
::singleton()->getManager()->install(array($params['key']));
217 return civicrm_api3_create_success();
221 * Download and install an extension
223 * @param array $params
225 * - local: bool, whether to rescan local filesystem (default: TRUE)
226 * - remote: bool, whether to rescan remote repository (default: TRUE)
230 * @example ExtensionRefresh.php
233 function civicrm_api3_extension_refresh($params) {
234 $defaults = array('local' => TRUE, 'remote' => TRUE);
235 $params = array_merge($defaults, $params);
237 $system = CRM_Extension_System
::singleton(TRUE);
239 if ($params['local']) {
240 $system->getManager()->refresh();
241 $system->getManager()->getStatuses(); // force immediate scan
244 if ($params['remote']) {
245 if ($system->getBrowser()->isEnabled() && empty($system->getBrowser()->checkRequirements
)) {
246 $system->getBrowser()->refresh();
247 $system->getBrowser()->getExtensions(); // force immediate download
251 return civicrm_api3_create_success();
255 * Get a list of available extensions
257 * @param array $params
261 * @example ExtensionGet.php
263 function civicrm_api3_extension_get($params) {
264 $statuses = CRM_Extension_System
::singleton()->getManager()->getStatuses();
265 $mapper = CRM_Extension_System
::singleton()->getMapper();
267 foreach ($statuses as $key => $status) {
269 // $info = (array) $mapper->keyToInfo($key);
270 //} catch (CRM_Extension_Exception $e) {
274 $info['status'] = $status;
277 return civicrm_api3_create_success($result);
281 * Determine the list of extension keys
283 * @param array $params
284 * API request params with 'key' or 'keys'.
286 * Array of extension keys
287 * @throws API_Exception
289 function _civicrm_api3_getKeys($params) {
290 if (array_key_exists('keys', $params) && is_array($params['keys'])) {
291 return $params['keys'];
293 elseif (array_key_exists('keys', $params) && is_string($params['keys'])) {
294 if ($params['keys'] == '') {
298 return explode(API_V3_EXTENSION_DELIMITER
, $params['keys']);
301 elseif (array_key_exists('key', $params)) {
302 return array($params['key']);
305 throw new API_Exception('Missing required parameter: key or keys');