Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
731a0992 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | ||
c490a46a CW |
28 | define('API_V3_EXTENSION_DELIMITER', ','); |
29 | ||
30 | ||
6a488035 | 31 | /** |
c28e1768 | 32 | * This api exposes CiviCRM extension. |
6a488035 TO |
33 | * |
34 | * @package CiviCRM_APIv3 | |
35 | * @subpackage API_Extension | |
36 | * | |
6a488035 TO |
37 | */ |
38 | ||
39 | /** | |
dc64d047 | 40 | * Install an extension. |
6a488035 | 41 | * |
cf470720 TO |
42 | * @param array $params |
43 | * Input parameters. | |
c28e1768 CW |
44 | * - key: string, eg "com.example.myextension" |
45 | * - keys: mixed; array of string, eg array("com.example.myextension1", "com.example.myextension2") or string with comma-delimited list | |
46 | * Using 'keys' should be more performant than making multiple API calls with 'key'. | |
6a488035 | 47 | * |
a6c01b45 | 48 | * @return array |
72b3a70c | 49 | * API result |
6a488035 TO |
50 | */ |
51 | function civicrm_api3_extension_install($params) { | |
52 | $keys = _civicrm_api3_getKeys($params); | |
53 | if (count($keys) == 0) { | |
54 | return civicrm_api3_create_success(); | |
55 | } | |
56 | ||
57 | try { | |
58 | CRM_Extension_System::singleton()->getManager()->install($keys); | |
0db6c3e1 TO |
59 | } |
60 | catch (CRM_Extension_Exception $e) { | |
6a488035 TO |
61 | return civicrm_api3_create_error($e->getMessage()); |
62 | } | |
63 | ||
64 | return civicrm_api3_create_success(); | |
65 | } | |
66 | ||
9a477d10 | 67 | /** |
d1b0d05e | 68 | * Upgrade an extension - runs upgrade_N hooks and system.flush. |
9a477d10 | 69 | * |
a6c01b45 | 70 | * @return array |
72b3a70c | 71 | * API result |
9a477d10 FG |
72 | */ |
73 | function civicrm_api3_extension_upgrade() { | |
74 | CRM_Core_Invoke::rebuildMenuAndCaches(TRUE); | |
75 | $queue = CRM_Extension_Upgrades::createQueue(); | |
76 | $runner = new CRM_Queue_Runner(array( | |
77 | 'title' => 'Extension Upgrades', | |
78 | 'queue' => $queue, | |
79 | 'errorMode' => CRM_Queue_Runner::ERROR_ABORT, | |
80 | )); | |
81 | ||
82 | try { | |
83 | $result = $runner->runAll(); | |
0db6c3e1 TO |
84 | } |
85 | catch (CRM_Extension_Exception $e) { | |
9a477d10 FG |
86 | return civicrm_api3_create_error($e->getMessage()); |
87 | } | |
88 | ||
89 | if ($result === TRUE) { | |
90 | return civicrm_api3_create_success(); | |
0db6c3e1 TO |
91 | } |
92 | else { | |
9a477d10 FG |
93 | return $result; |
94 | } | |
95 | } | |
96 | ||
6a488035 | 97 | /** |
dc64d047 | 98 | * Enable an extension. |
6a488035 | 99 | * |
cf470720 TO |
100 | * @param array $params |
101 | * Input parameters. | |
6a488035 TO |
102 | * - key: string, eg "com.example.myextension" |
103 | * - keys: mixed; array of string, eg array("com.example.myextension1", "com.example.myextension2") or string with comma-delimited list | |
104 | * using 'keys' should be more performant than making multiple API calls with 'key' | |
105 | * | |
a6c01b45 | 106 | * @return array |
72b3a70c | 107 | * API result |
6a488035 TO |
108 | */ |
109 | function civicrm_api3_extension_enable($params) { | |
110 | $keys = _civicrm_api3_getKeys($params); | |
111 | if (count($keys) == 0) { | |
112 | return civicrm_api3_create_success(); | |
113 | } | |
114 | ||
115 | CRM_Extension_System::singleton()->getManager()->enable($keys); | |
116 | return civicrm_api3_create_success(); | |
117 | } | |
118 | ||
119 | /** | |
9d32e6f7 | 120 | * Disable an extension. |
6a488035 | 121 | * |
cf470720 TO |
122 | * @param array $params |
123 | * Input parameters. | |
6a488035 TO |
124 | * - key: string, eg "com.example.myextension" |
125 | * - keys: mixed; array of string, eg array("com.example.myextension1", "com.example.myextension2") or string with comma-delimited list | |
126 | * using 'keys' should be more performant than making multiple API calls with 'key' | |
127 | * | |
a6c01b45 | 128 | * @return array |
72b3a70c | 129 | * API result |
6a488035 TO |
130 | */ |
131 | function civicrm_api3_extension_disable($params) { | |
132 | $keys = _civicrm_api3_getKeys($params); | |
133 | if (count($keys) == 0) { | |
134 | return civicrm_api3_create_success(); | |
135 | } | |
136 | ||
137 | CRM_Extension_System::singleton()->getManager()->disable($keys); | |
138 | return civicrm_api3_create_success(); | |
139 | } | |
140 | ||
141 | /** | |
dc64d047 | 142 | * Uninstall an extension. |
6a488035 | 143 | * |
cf470720 TO |
144 | * @param array $params |
145 | * Input parameters. | |
6a488035 TO |
146 | * - key: string, eg "com.example.myextension" |
147 | * - keys: array of string, eg array("com.example.myextension1", "com.example.myextension2") | |
148 | * using 'keys' should be more performant than making multiple API calls with 'key' | |
149 | * - removeFiles: bool, whether to remove source tree; default: FALSE | |
150 | * | |
a6c01b45 | 151 | * @return array |
72b3a70c | 152 | * API result |
6a488035 TO |
153 | */ |
154 | function civicrm_api3_extension_uninstall($params) { | |
155 | $keys = _civicrm_api3_getKeys($params); | |
156 | if (count($keys) == 0) { | |
157 | return civicrm_api3_create_success(); | |
158 | } | |
159 | ||
160 | // TODO // $removeFiles = CRM_Utils_Array::value('removeFiles', $params, FALSE); | |
161 | CRM_Extension_System::singleton()->getManager()->uninstall($keys); | |
162 | return civicrm_api3_create_success(); | |
163 | } | |
164 | ||
165 | /** | |
dc64d047 | 166 | * Download and install an extension. |
6a488035 | 167 | * |
cf470720 TO |
168 | * @param array $params |
169 | * Input parameters. | |
6a488035 TO |
170 | * - key: string, eg "com.example.myextension" |
171 | * - url: string eg "http://repo.com/myextension-1.0.zip" | |
172 | * | |
77b97be7 | 173 | * @throws API_Exception |
a6c01b45 | 174 | * @return array |
72b3a70c | 175 | * API result |
6a488035 | 176 | * @example ExtensionDownload.php |
6a488035 TO |
177 | */ |
178 | function civicrm_api3_extension_download($params) { | |
6c552737 | 179 | if (!array_key_exists('key', $params)) { |
6a488035 TO |
180 | throw new API_Exception('Missing required parameter: key'); |
181 | } | |
182 | ||
6c552737 TO |
183 | if (!array_key_exists('url', $params)) { |
184 | if (!CRM_Extension_System::singleton()->getBrowser()->isEnabled()) { | |
6a488035 TO |
185 | throw new API_Exception('Automatic downloading is diabled. Try adding parameter "url"'); |
186 | } | |
187 | if ($reqs = CRM_Extension_System::singleton()->getBrowser()->checkRequirements()) { | |
188 | $first = array_shift($reqs); | |
189 | throw new API_Exception($first['message']); | |
190 | } | |
191 | if ($info = CRM_Extension_System::singleton()->getBrowser()->getExtension($params['key'])) { | |
192 | if ($info->downloadUrl) { | |
193 | $params['url'] = $info->downloadUrl; | |
194 | } | |
195 | } | |
196 | } | |
197 | ||
6c552737 | 198 | if (!array_key_exists('url', $params)) { |
6a488035 TO |
199 | throw new API_Exception('Cannot resolve download url for extension. Try adding parameter "url"'); |
200 | } | |
201 | ||
202 | foreach (CRM_Extension_System::singleton()->getDownloader()->checkRequirements() as $requirement) { | |
203 | return civicrm_api3_create_error($requirement['message']); | |
204 | } | |
205 | ||
6c552737 | 206 | if (!CRM_Extension_System::singleton()->getDownloader()->download($params['key'], $params['url'])) { |
6a488035 TO |
207 | return civicrm_api3_create_error('Download failed - ZIP file is unavailable or malformed'); |
208 | } | |
209 | CRM_Extension_System::singleton()->getCache()->flush(); | |
210 | CRM_Extension_System::singleton(TRUE); | |
211 | CRM_Extension_System::singleton()->getManager()->install(array($params['key'])); | |
212 | ||
213 | return civicrm_api3_create_success(); | |
214 | } | |
215 | ||
216 | /** | |
dc64d047 | 217 | * Download and install an extension. |
6a488035 | 218 | * |
cf470720 TO |
219 | * @param array $params |
220 | * Input parameters. | |
6a488035 TO |
221 | * - local: bool, whether to rescan local filesystem (default: TRUE) |
222 | * - remote: bool, whether to rescan remote repository (default: TRUE) | |
223 | * | |
a6c01b45 | 224 | * @return array |
72b3a70c | 225 | * API result |
6a488035 TO |
226 | */ |
227 | function civicrm_api3_extension_refresh($params) { | |
228 | $defaults = array('local' => TRUE, 'remote' => TRUE); | |
229 | $params = array_merge($defaults, $params); | |
230 | ||
231 | $system = CRM_Extension_System::singleton(TRUE); | |
232 | ||
233 | if ($params['local']) { | |
234 | $system->getManager()->refresh(); | |
235 | $system->getManager()->getStatuses(); // force immediate scan | |
236 | } | |
237 | ||
238 | if ($params['remote']) { | |
239 | if ($system->getBrowser()->isEnabled() && empty($system->getBrowser()->checkRequirements)) { | |
240 | $system->getBrowser()->refresh(); | |
241 | $system->getBrowser()->getExtensions(); // force immediate download | |
242 | } | |
243 | } | |
244 | ||
245 | return civicrm_api3_create_success(); | |
246 | } | |
247 | ||
248 | /** | |
d1b0d05e | 249 | * Get a list of available extensions. |
6a488035 | 250 | * |
c490a46a | 251 | * @param array $params |
77b97be7 | 252 | * |
a6c01b45 | 253 | * @return array |
72b3a70c | 254 | * API result |
6a488035 TO |
255 | */ |
256 | function civicrm_api3_extension_get($params) { | |
257 | $statuses = CRM_Extension_System::singleton()->getManager()->getStatuses(); | |
258 | $mapper = CRM_Extension_System::singleton()->getMapper(); | |
259 | $result = array(); | |
260 | foreach ($statuses as $key => $status) { | |
261 | //try { | |
262 | // $info = (array) $mapper->keyToInfo($key); | |
263 | //} catch (CRM_Extension_Exception $e) { | |
35671d00 TO |
264 | $info = array(); |
265 | $info['key'] = $key; | |
6a488035 TO |
266 | //} |
267 | $info['status'] = $status; | |
268 | $result[] = $info; | |
269 | } | |
270 | return civicrm_api3_create_success($result); | |
271 | } | |
272 | ||
273 | /** | |
d1b0d05e | 274 | * Determine the list of extension keys. |
6a488035 | 275 | * |
cf470720 TO |
276 | * @param array $params |
277 | * API request params with 'key' or 'keys'. | |
d1b0d05e | 278 | * |
a6c01b45 | 279 | * @return array |
16b10e64 | 280 | * Array of extension keys |
6a488035 TO |
281 | * @throws API_Exception |
282 | */ | |
283 | function _civicrm_api3_getKeys($params) { | |
284 | if (array_key_exists('keys', $params) && is_array($params['keys'])) { | |
285 | return $params['keys']; | |
0db6c3e1 TO |
286 | } |
287 | elseif (array_key_exists('keys', $params) && is_string($params['keys'])) { | |
6a488035 TO |
288 | if ($params['keys'] == '') { |
289 | return array(); | |
0db6c3e1 TO |
290 | } |
291 | else { | |
6a488035 TO |
292 | return explode(API_V3_EXTENSION_DELIMITER, $params['keys']); |
293 | } | |
0db6c3e1 TO |
294 | } |
295 | elseif (array_key_exists('key', $params)) { | |
6a488035 | 296 | return array($params['key']); |
0db6c3e1 TO |
297 | } |
298 | else { | |
6a488035 TO |
299 | throw new API_Exception('Missing required parameter: key or keys'); |
300 | } | |
301 | } |