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