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'
51 * @return array API result
54 * @example ExtensionInstall.php
57 function civicrm_api3_extension_install($params) {
58 $keys = _civicrm_api3_getKeys($params);
59 if (count($keys) == 0) {
60 return civicrm_api3_create_success();
64 CRM_Extension_System
::singleton()->getManager()->install($keys);
66 catch (CRM_Extension_Exception
$e) {
67 return civicrm_api3_create_error($e->getMessage());
70 return civicrm_api3_create_success();
74 * Upgrade an extension - runs upgrade_N hooks and system.flush
76 * @return array API result
81 function civicrm_api3_extension_upgrade() {
82 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
83 $queue = CRM_Extension_Upgrades
::createQueue();
84 $runner = new CRM_Queue_Runner(array(
85 'title' => 'Extension Upgrades',
87 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
91 $result = $runner->runAll();
93 catch (CRM_Extension_Exception
$e) {
94 return civicrm_api3_create_error($e->getMessage());
97 if ($result === TRUE) {
98 return civicrm_api3_create_success();
106 * Enable an extension
108 * @param array $params
110 * - key: string, eg "com.example.myextension"
111 * - keys: mixed; array of string, eg array("com.example.myextension1", "com.example.myextension2") or string with comma-delimited list
112 * using 'keys' should be more performant than making multiple API calls with 'key'
114 * @return array API result
117 * @example ExtensionEnable.php
120 function civicrm_api3_extension_enable($params) {
121 $keys = _civicrm_api3_getKeys($params);
122 if (count($keys) == 0) {
123 return civicrm_api3_create_success();
126 CRM_Extension_System
::singleton()->getManager()->enable($keys);
127 return civicrm_api3_create_success();
131 * Disable an extension
133 * @param array $params
135 * - key: string, eg "com.example.myextension"
136 * - keys: mixed; array of string, eg array("com.example.myextension1", "com.example.myextension2") or string with comma-delimited list
137 * using 'keys' should be more performant than making multiple API calls with 'key'
139 * @return array API result
142 * @example ExtensionDisable.php
145 function civicrm_api3_extension_disable($params) {
146 $keys = _civicrm_api3_getKeys($params);
147 if (count($keys) == 0) {
148 return civicrm_api3_create_success();
151 CRM_Extension_System
::singleton()->getManager()->disable($keys);
152 return civicrm_api3_create_success();
156 * Uninstall an extension
158 * @param array $params
160 * - key: string, eg "com.example.myextension"
161 * - keys: array of string, eg array("com.example.myextension1", "com.example.myextension2")
162 * using 'keys' should be more performant than making multiple API calls with 'key'
163 * - removeFiles: bool, whether to remove source tree; default: FALSE
165 * @return array API result
168 * @example ExtensionUninstall.php
171 function civicrm_api3_extension_uninstall($params) {
172 $keys = _civicrm_api3_getKeys($params);
173 if (count($keys) == 0) {
174 return civicrm_api3_create_success();
177 // TODO // $removeFiles = CRM_Utils_Array::value('removeFiles', $params, FALSE);
178 CRM_Extension_System
::singleton()->getManager()->uninstall($keys);
179 return civicrm_api3_create_success();
183 * Download and install an extension
185 * @param array $params
187 * - key: string, eg "com.example.myextension"
188 * - url: string eg "http://repo.com/myextension-1.0.zip"
190 * @throws API_Exception
191 * @return array API result
194 * @example ExtensionDownload.php
196 function civicrm_api3_extension_download($params) {
197 if (! array_key_exists('key', $params)) {
198 throw new API_Exception('Missing required parameter: key');
201 if (! array_key_exists('url', $params)) {
202 if (! CRM_Extension_System
::singleton()->getBrowser()->isEnabled()) {
203 throw new API_Exception('Automatic downloading is diabled. Try adding parameter "url"');
205 if ($reqs = CRM_Extension_System
::singleton()->getBrowser()->checkRequirements()) {
206 $first = array_shift($reqs);
207 throw new API_Exception($first['message']);
209 if ($info = CRM_Extension_System
::singleton()->getBrowser()->getExtension($params['key'])) {
210 if ($info->downloadUrl
) {
211 $params['url'] = $info->downloadUrl
;
216 if (! array_key_exists('url', $params)) {
217 throw new API_Exception('Cannot resolve download url for extension. Try adding parameter "url"');
220 foreach (CRM_Extension_System
::singleton()->getDownloader()->checkRequirements() as $requirement) {
221 return civicrm_api3_create_error($requirement['message']);
224 if (! CRM_Extension_System
::singleton()->getDownloader()->download($params['key'], $params['url'])) {
225 return civicrm_api3_create_error('Download failed - ZIP file is unavailable or malformed');
227 CRM_Extension_System
::singleton()->getCache()->flush();
228 CRM_Extension_System
::singleton(TRUE);
229 CRM_Extension_System
::singleton()->getManager()->install(array($params['key']));
231 return civicrm_api3_create_success();
235 * Download and install an extension
237 * @param array $params
239 * - local: bool, whether to rescan local filesystem (default: TRUE)
240 * - remote: bool, whether to rescan remote repository (default: TRUE)
242 * @return array API result
245 * @example ExtensionRefresh.php
248 function civicrm_api3_extension_refresh($params) {
249 $defaults = array('local' => TRUE, 'remote' => TRUE);
250 $params = array_merge($defaults, $params);
252 $system = CRM_Extension_System
::singleton(TRUE);
254 if ($params['local']) {
255 $system->getManager()->refresh();
256 $system->getManager()->getStatuses(); // force immediate scan
259 if ($params['remote']) {
260 if ($system->getBrowser()->isEnabled() && empty($system->getBrowser()->checkRequirements
)) {
261 $system->getBrowser()->refresh();
262 $system->getBrowser()->getExtensions(); // force immediate download
266 return civicrm_api3_create_success();
270 * Get a list of available extensions
272 * @param array $params
274 * @return array API result
277 * @example ExtensionGet.php
279 function civicrm_api3_extension_get($params) {
280 $statuses = CRM_Extension_System
::singleton()->getManager()->getStatuses();
281 $mapper = CRM_Extension_System
::singleton()->getMapper();
283 foreach ($statuses as $key => $status) {
285 // $info = (array) $mapper->keyToInfo($key);
286 //} catch (CRM_Extension_Exception $e) {
290 $info['status'] = $status;
293 return civicrm_api3_create_success($result);
297 * Determine the list of extension keys
299 * @param array $params
300 * API request params with 'key' or 'keys'.
301 * @return array of extension keys
302 * @throws API_Exception
304 function _civicrm_api3_getKeys($params) {
305 if (array_key_exists('keys', $params) && is_array($params['keys'])) {
306 return $params['keys'];
308 elseif (array_key_exists('keys', $params) && is_string($params['keys'])) {
309 if ($params['keys'] == '') {
313 return explode(API_V3_EXTENSION_DELIMITER
, $params['keys']);
316 elseif (array_key_exists('key', $params)) {
317 return array($params['key']);
320 throw new API_Exception('Missing required parameter: key or keys');