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