3 define('API_V3_EXTENSION_DELIMITER', ',');
6 +--------------------------------------------------------------------+
7 | CiviCRM version 4.5 |
8 +--------------------------------------------------------------------+
9 | Copyright CiviCRM LLC (c) 2004-2014 |
10 +--------------------------------------------------------------------+
11 | This file is a part of CiviCRM. |
13 | CiviCRM is free software; you can copy, modify, and distribute it |
14 | under the terms of the GNU Affero General Public License |
15 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
17 | CiviCRM is distributed in the hope that it will be useful, but |
18 | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
20 | See the GNU Affero General Public License for more details. |
22 | You should have received a copy of the GNU Affero General Public |
23 | License and the CiviCRM Licensing Exception along |
24 | with this program; if not, contact CiviCRM LLC |
25 | at info[AT]civicrm[DOT]org. If you have questions about the |
26 | GNU Affero General Public License or the licensing of CiviCRM, |
27 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
28 +--------------------------------------------------------------------+
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 input parameters
46 * - key: string, eg "com.example.myextension"
47 * - keys: mixed; array of string, eg array("com.example.myextension1", "com.example.myextension2") or string with comma-delimited list
48 * using 'keys' should be more performant than making multiple API calls with 'key'
50 * @return array API result
53 * @example ExtensionInstall.php
56 function civicrm_api3_extension_install($params) {
57 $keys = _civicrm_api3_getKeys($params);
58 if (count($keys) == 0) {
59 return civicrm_api3_create_success();
63 CRM_Extension_System
::singleton()->getManager()->install($keys);
64 } catch (CRM_Extension_Exception
$e) {
65 return civicrm_api3_create_error($e->getMessage());
68 return civicrm_api3_create_success();
72 * Upgrade an extension - runs upgrade_N hooks and system.flush
74 * @return array API result
79 function civicrm_api3_extension_upgrade() {
80 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
81 $queue = CRM_Extension_Upgrades
::createQueue();
82 $runner = new CRM_Queue_Runner(array(
83 'title' => 'Extension Upgrades',
85 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
89 $result = $runner->runAll();
90 } catch (CRM_Extension_Exception
$e) {
91 return civicrm_api3_create_error($e->getMessage());
94 if ($result === TRUE) {
95 return civicrm_api3_create_success();
102 * Enable an extension
104 * @param array $params input parameters
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'
109 * @return array API result
112 * @example ExtensionEnable.php
115 function civicrm_api3_extension_enable($params) {
116 $keys = _civicrm_api3_getKeys($params);
117 if (count($keys) == 0) {
118 return civicrm_api3_create_success();
121 CRM_Extension_System
::singleton()->getManager()->enable($keys);
122 return civicrm_api3_create_success();
126 * Disable an extension
128 * @param array $params input parameters
129 * - key: string, eg "com.example.myextension"
130 * - keys: mixed; array of string, eg array("com.example.myextension1", "com.example.myextension2") or string with comma-delimited list
131 * using 'keys' should be more performant than making multiple API calls with 'key'
133 * @return array API result
136 * @example ExtensionDisable.php
139 function civicrm_api3_extension_disable($params) {
140 $keys = _civicrm_api3_getKeys($params);
141 if (count($keys) == 0) {
142 return civicrm_api3_create_success();
145 CRM_Extension_System
::singleton()->getManager()->disable($keys);
146 return civicrm_api3_create_success();
150 * Uninstall an extension
152 * @param array $params input parameters
153 * - key: string, eg "com.example.myextension"
154 * - keys: array of string, eg array("com.example.myextension1", "com.example.myextension2")
155 * using 'keys' should be more performant than making multiple API calls with 'key'
156 * - removeFiles: bool, whether to remove source tree; default: FALSE
158 * @return array API result
161 * @example ExtensionUninstall.php
164 function civicrm_api3_extension_uninstall($params) {
165 $keys = _civicrm_api3_getKeys($params);
166 if (count($keys) == 0) {
167 return civicrm_api3_create_success();
170 // TODO // $removeFiles = CRM_Utils_Array::value('removeFiles', $params, FALSE);
171 CRM_Extension_System
::singleton()->getManager()->uninstall($keys);
172 return civicrm_api3_create_success();
176 * Download and install an extension
178 * @param array $params input parameters
179 * - key: string, eg "com.example.myextension"
180 * - url: string eg "http://repo.com/myextension-1.0.zip"
182 * @throws API_Exception
183 * @return array API result
186 * @example ExtensionDownload.php
188 function civicrm_api3_extension_download($params) {
189 if (! array_key_exists('key', $params)) {
190 throw new API_Exception('Missing required parameter: key');
193 if (! array_key_exists('url', $params)) {
194 if (! CRM_Extension_System
::singleton()->getBrowser()->isEnabled()) {
195 throw new API_Exception('Automatic downloading is diabled. Try adding parameter "url"');
197 if ($reqs = CRM_Extension_System
::singleton()->getBrowser()->checkRequirements()) {
198 $first = array_shift($reqs);
199 throw new API_Exception($first['message']);
201 if ($info = CRM_Extension_System
::singleton()->getBrowser()->getExtension($params['key'])) {
202 if ($info->downloadUrl
) {
203 $params['url'] = $info->downloadUrl
;
208 if (! array_key_exists('url', $params)) {
209 throw new API_Exception('Cannot resolve download url for extension. Try adding parameter "url"');
212 foreach (CRM_Extension_System
::singleton()->getDownloader()->checkRequirements() as $requirement) {
213 return civicrm_api3_create_error($requirement['message']);
216 if (! CRM_Extension_System
::singleton()->getDownloader()->download($params['key'], $params['url'])) {
217 return civicrm_api3_create_error('Download failed - ZIP file is unavailable or malformed');
219 CRM_Extension_System
::singleton()->getCache()->flush();
220 CRM_Extension_System
::singleton(TRUE);
221 CRM_Extension_System
::singleton()->getManager()->install(array($params['key']));
223 return civicrm_api3_create_success();
227 * Download and install an extension
229 * @param array $params input parameters
230 * - local: bool, whether to rescan local filesystem (default: TRUE)
231 * - remote: bool, whether to rescan remote repository (default: TRUE)
233 * @return array API result
236 * @example ExtensionRefresh.php
239 function civicrm_api3_extension_refresh($params) {
240 $defaults = array('local' => TRUE, 'remote' => TRUE);
241 $params = array_merge($defaults, $params);
243 $system = CRM_Extension_System
::singleton(TRUE);
245 if ($params['local']) {
246 $system->getManager()->refresh();
247 $system->getManager()->getStatuses(); // force immediate scan
250 if ($params['remote']) {
251 if ($system->getBrowser()->isEnabled() && empty($system->getBrowser()->checkRequirements
)) {
252 $system->getBrowser()->refresh();
253 $system->getBrowser()->getExtensions(); // force immediate download
257 return civicrm_api3_create_success();
261 * Get a list of available extensions
265 * @return array API result
268 * @example ExtensionGet.php
270 function civicrm_api3_extension_get($params) {
271 $statuses = CRM_Extension_System
::singleton()->getManager()->getStatuses();
272 $mapper = CRM_Extension_System
::singleton()->getMapper();
274 foreach ($statuses as $key => $status) {
276 // $info = (array) $mapper->keyToInfo($key);
277 //} catch (CRM_Extension_Exception $e) {
281 $info['status'] = $status;
284 return civicrm_api3_create_success($result);
288 * Determine the list of extension keys
290 * @param array $params API request params with 'key' or 'keys'
291 * @return array of extension keys
292 * @throws API_Exception
294 function _civicrm_api3_getKeys($params) {
295 if (array_key_exists('keys', $params) && is_array($params['keys'])) {
296 return $params['keys'];
297 } elseif (array_key_exists('keys', $params) && is_string($params['keys'])) {
298 if ($params['keys'] == '') {
301 return explode(API_V3_EXTENSION_DELIMITER
, $params['keys']);
303 } elseif (array_key_exists('key', $params)) {
304 return array($params['key']);
306 throw new API_Exception('Missing required parameter: key or keys');