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'
55 * @example ExtensionInstall.php
58 function civicrm_api3_extension_install($params) {
59 $keys = _civicrm_api3_getKeys($params);
60 if (count($keys) == 0) {
61 return civicrm_api3_create_success();
65 CRM_Extension_System
::singleton()->getManager()->install($keys);
67 catch (CRM_Extension_Exception
$e) {
68 return civicrm_api3_create_error($e->getMessage());
71 return civicrm_api3_create_success();
75 * Upgrade an extension - runs upgrade_N hooks and system.flush
83 function civicrm_api3_extension_upgrade() {
84 CRM_Core_Invoke
::rebuildMenuAndCaches(TRUE);
85 $queue = CRM_Extension_Upgrades
::createQueue();
86 $runner = new CRM_Queue_Runner(array(
87 'title' => 'Extension Upgrades',
89 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
93 $result = $runner->runAll();
95 catch (CRM_Extension_Exception
$e) {
96 return civicrm_api3_create_error($e->getMessage());
99 if ($result === TRUE) {
100 return civicrm_api3_create_success();
108 * Enable an extension
110 * @param array $params
112 * - key: string, eg "com.example.myextension"
113 * - keys: mixed; array of string, eg array("com.example.myextension1", "com.example.myextension2") or string with comma-delimited list
114 * using 'keys' should be more performant than making multiple API calls with 'key'
120 * @example ExtensionEnable.php
123 function civicrm_api3_extension_enable($params) {
124 $keys = _civicrm_api3_getKeys($params);
125 if (count($keys) == 0) {
126 return civicrm_api3_create_success();
129 CRM_Extension_System
::singleton()->getManager()->enable($keys);
130 return civicrm_api3_create_success();
134 * Disable an extension
136 * @param array $params
138 * - key: string, eg "com.example.myextension"
139 * - keys: mixed; array of string, eg array("com.example.myextension1", "com.example.myextension2") or string with comma-delimited list
140 * using 'keys' should be more performant than making multiple API calls with 'key'
146 * @example ExtensionDisable.php
149 function civicrm_api3_extension_disable($params) {
150 $keys = _civicrm_api3_getKeys($params);
151 if (count($keys) == 0) {
152 return civicrm_api3_create_success();
155 CRM_Extension_System
::singleton()->getManager()->disable($keys);
156 return civicrm_api3_create_success();
160 * Uninstall an extension
162 * @param array $params
164 * - key: string, eg "com.example.myextension"
165 * - keys: array of string, eg array("com.example.myextension1", "com.example.myextension2")
166 * using 'keys' should be more performant than making multiple API calls with 'key'
167 * - removeFiles: bool, whether to remove source tree; default: FALSE
173 * @example ExtensionUninstall.php
176 function civicrm_api3_extension_uninstall($params) {
177 $keys = _civicrm_api3_getKeys($params);
178 if (count($keys) == 0) {
179 return civicrm_api3_create_success();
182 // TODO // $removeFiles = CRM_Utils_Array::value('removeFiles', $params, FALSE);
183 CRM_Extension_System
::singleton()->getManager()->uninstall($keys);
184 return civicrm_api3_create_success();
188 * Download and install an extension
190 * @param array $params
192 * - key: string, eg "com.example.myextension"
193 * - url: string eg "http://repo.com/myextension-1.0.zip"
195 * @throws API_Exception
200 * @example ExtensionDownload.php
202 function civicrm_api3_extension_download($params) {
203 if (! array_key_exists('key', $params)) {
204 throw new API_Exception('Missing required parameter: key');
207 if (! array_key_exists('url', $params)) {
208 if (! CRM_Extension_System
::singleton()->getBrowser()->isEnabled()) {
209 throw new API_Exception('Automatic downloading is diabled. Try adding parameter "url"');
211 if ($reqs = CRM_Extension_System
::singleton()->getBrowser()->checkRequirements()) {
212 $first = array_shift($reqs);
213 throw new API_Exception($first['message']);
215 if ($info = CRM_Extension_System
::singleton()->getBrowser()->getExtension($params['key'])) {
216 if ($info->downloadUrl
) {
217 $params['url'] = $info->downloadUrl
;
222 if (! array_key_exists('url', $params)) {
223 throw new API_Exception('Cannot resolve download url for extension. Try adding parameter "url"');
226 foreach (CRM_Extension_System
::singleton()->getDownloader()->checkRequirements() as $requirement) {
227 return civicrm_api3_create_error($requirement['message']);
230 if (! CRM_Extension_System
::singleton()->getDownloader()->download($params['key'], $params['url'])) {
231 return civicrm_api3_create_error('Download failed - ZIP file is unavailable or malformed');
233 CRM_Extension_System
::singleton()->getCache()->flush();
234 CRM_Extension_System
::singleton(TRUE);
235 CRM_Extension_System
::singleton()->getManager()->install(array($params['key']));
237 return civicrm_api3_create_success();
241 * Download and install an extension
243 * @param array $params
245 * - local: bool, whether to rescan local filesystem (default: TRUE)
246 * - remote: bool, whether to rescan remote repository (default: TRUE)
252 * @example ExtensionRefresh.php
255 function civicrm_api3_extension_refresh($params) {
256 $defaults = array('local' => TRUE, 'remote' => TRUE);
257 $params = array_merge($defaults, $params);
259 $system = CRM_Extension_System
::singleton(TRUE);
261 if ($params['local']) {
262 $system->getManager()->refresh();
263 $system->getManager()->getStatuses(); // force immediate scan
266 if ($params['remote']) {
267 if ($system->getBrowser()->isEnabled() && empty($system->getBrowser()->checkRequirements
)) {
268 $system->getBrowser()->refresh();
269 $system->getBrowser()->getExtensions(); // force immediate download
273 return civicrm_api3_create_success();
277 * Get a list of available extensions
279 * @param array $params
285 * @example ExtensionGet.php
287 function civicrm_api3_extension_get($params) {
288 $statuses = CRM_Extension_System
::singleton()->getManager()->getStatuses();
289 $mapper = CRM_Extension_System
::singleton()->getMapper();
291 foreach ($statuses as $key => $status) {
293 // $info = (array) $mapper->keyToInfo($key);
294 //} catch (CRM_Extension_Exception $e) {
298 $info['status'] = $status;
301 return civicrm_api3_create_success($result);
305 * Determine the list of extension keys
307 * @param array $params
308 * API request params with 'key' or 'keys'.
311 * @throws API_Exception
313 function _civicrm_api3_getKeys($params) {
314 if (array_key_exists('keys', $params) && is_array($params['keys'])) {
315 return $params['keys'];
317 elseif (array_key_exists('keys', $params) && is_string($params['keys'])) {
318 if ($params['keys'] == '') {
322 return explode(API_V3_EXTENSION_DELIMITER
, $params['keys']);
325 elseif (array_key_exists('key', $params)) {
326 return array($params['key']);
329 throw new API_Exception('Missing required parameter: key or keys');