-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsalesforce.drush.inc
366 lines (347 loc) · 10.9 KB
/
salesforce.drush.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
/**
* @file
* Drush integration for Salesforce.
*/
/**
* Implements hook_drush_command().
*/
function salesforce_drush_command() {
$items['sf-rest-version'] = array(
'description' => 'Displays information about the current REST API version',
'aliases' => array('sfrv'),
);
$items['sf-list-objects'] = array(
'description' => 'List the objects that are available in your organization and available to the logged-in user.',
'aliases' => array('sflo'),
);
$items['sf-describe-object'] = array(
'description' => 'Retrieve all the metadata for an object, including information about each field, URLs, and child relationships.',
'aliases' => array('sfdo'),
'arguments' => array(
'object' => 'The object name in Salesforce.',
),
'options' => array(
'fields' => 'Display information about fields that are part of the object.',
'field-data' => 'Display information about a specific field that is part of an object',
),
);
$items['sf-read-object'] = array(
'description' => 'Retrieve all the data for an object with a specific ID.',
'aliases' => array('sfro'),
'arguments' => array(
'object' => 'The object name in Salesforce (e.g. Account).',
'id' => 'The object ID in Salesforce.',
),
'options' => array(
'format' => array(
'description' => 'Format to output the object. Use "print_r" for print_r (default), "export" for var_export, and "json" for JSON.',
'example-value' => 'export',
),
),
);
$items['sf-create-object'] = array(
'description' => 'Create an object with specified data.',
'aliases' => array('sfco'),
'arguments' => array(
'object' => 'The object type name in Salesforce (e.g. Account).',
'data' => 'The data to use when creating the object (default is JSON format). Use \'-\' to read the data from STDIN.',
),
'options' => array(
'format' => array(
'description' => 'Format to parse the object. Use "json" for JSON (default) or "query" for data formatted like a query string, e.g. \'Company=Foo&LastName=Bar\'.',
'example-value' => 'json',
),
),
);
$items['sf-query-object'] = array(
'description' => 'Query an object using SOQL with specified conditions.',
'aliases' => array('sfqo'),
'arguments' => array(
'object' => 'The object type name in Salesforce (e.g. Account).',
),
'options' => array(
'format' => array(
'description' => 'Format to output the objects. Use "print_r" for print_r (default), "export" for var_export, and "json" for JSON.',
'example-value' => 'export',
),
'where' =>array(
'description' => 'A WHERE clause to add to the SOQL query',
'example-value' => 'isDeleted = TRUE',
),
'fields' =>array(
'description' => 'A comma-separated list fields to select in the SOQL query. If absent, an API call is used to find all fields',
'example-value' => 'Id, LastName',
),
),
);
$items['sf-list-resources'] = array(
'description' => 'Lists the resources available for the specified API version. It provides the name and URI of each resource.',
'aliases' => array('sflr'),
);
$items['sf-execute-query'] = array(
'description' => 'Execute a SOQL query.',
'aliases' => array('sfeq'),
'arguments' => array(
'query' => 'The query to execute.',
),
);
return $items;
}
/**
* List the resources available for the specified API version.
*
* This command provides the name and URI of each resource.
*/
function drush_salesforce_sf_list_resources() {
$salesforce = _drush_salesforce_drush_get_api();
$resources = $salesforce->listResources();
if ($resources) {
$items[] = array('Resource', 'URL');
foreach ($resources as $resource => $url) {
$items[] = array($resource, $url);
}
drush_print("The following resources are available:\n");
drush_print_table($items);
}
else {
drush_log('Could not obtain a list of resources!', 'error');
}
}
/**
* Describes a Salesforce object.
*
* Use the --fields option to display information about the fields of an object,
* or the --field-data option to display information about a single field in an
* object.
*
* @param string $object_name
* The name of a Salesforce object to query.
*/
function drush_salesforce_sf_describe_object($object_name = NULL) {
if (!$object_name) {
return drush_log('Please specify an object as an argument.', 'error');
}
$salesforce = _drush_salesforce_drush_get_api();
$object = $salesforce->objectDescribe($object_name);
// Return if we cannot load any data.
if (!is_array($object)) {
return drush_log(dt('Could not load data for object !object', array('!object' => $object)), 'error');
}
// Display only information about fields for an option,
if (drush_get_option('fields')) {
$rows = array(array('Name', 'Type', 'Label'));
foreach ($object['fields'] as $field) {
$rows[] = array($field['name'], $field['type'], $field['label']);
}
drush_print_r($rows);
drush_print_table($rows, TRUE);
return;
}
// Display only information about a specific field.
if ($fieldname = drush_get_option('field-data')) {
$field_data = NULL;
foreach ($object['fields'] as $field) {
if ($field['name'] === $fieldname) {
$field_data = $field;
break;
}
}
if (!$field_data) {
drush_log(dt('Could not load data for field !field on !object object', array(
'!field' => $fieldname,
'!object' => $object_name,
)), 'error');
}
else {
drush_print_r($field);
}
return;
}
// Display information about the object.
// @TODO add remaining field objects?
$rows = array();
$rows[] = array('Name', $object['name']);
$rows[] = array(
'Fields',
isset($object['fields']) ? count($object['fields']) : 0,
);
$rows[] = array(
'Child Relationships',
isset($object['childRelationships']) ? count($object['childRelationships']) : 0,
);
$rows[] = array(
'Searchable',
($object['searchable'] == 1) ? 'TRUE' : 'FALSE',
);
$rows[] = array('Creatable', ($object['createable'] == 1) ? 'TRUE' : 'FALSE');
$rows[] = array('Deletable', ($object['deletable'] == 1) ? 'TRUE' : 'FALSE');
$rows[] = array('Mergeable', ($object['mergeable'] == 1) ? 'TRUE' : 'FALSE');
$rows[] = array('Queryable', ($object['queryable'] == 1) ? 'TRUE' : 'FALSE');
drush_print_table($rows);
}
/**
* Displays information about the REST API version the site is using.
*/
function drush_salesforce_sf_rest_version() {
$salesforce = _drush_salesforce_drush_get_api();
if (isset($salesforce->rest_api_version)) {
$rows[] = array('Salesforce', 'Value');
foreach ($salesforce->rest_api_version as $key => $value) {
$rows[] = array($key, $value);
}
$rows[] = array('login url', $salesforce->login_url);
drush_print_table($rows, TRUE);
}
else {
drush_log('Could not obtain information about the current REST API version!', 'error');
}
}
/**
* Wrapper around salesforce_get_api().
*
* If salesforce_get_api() does not return a connection to Salesforce,
* this function can prompt the user for username/password to obtain a new
* token.
*
* @TODO implement this function
*/
function _drush_salesforce_drush_get_api() {
if ($salesforce = salesforce_get_api()) {
return $salesforce;
}
}
/**
* List Salesforce objects.
*
* This command lists Salesforce objects that are available in your organization
* and available to the logged-in user.
*/
function drush_salesforce_sf_list_objects() {
$salesforce = _drush_salesforce_drush_get_api();
if ($objects = $salesforce->objects()) {
drush_print('The following objects are available in your organization and available to the logged-in user.');
$rows[] = array('Name', 'Label', 'Label Plural');
foreach ($objects as $object) {
$rows[] = array(
$object['name'],
$object['label'],
$object['labelPlural'],
);
}
drush_print_table($rows, TRUE);
}
else {
drush_log('Could not load any information about available objects.', 'error');
}
}
/**
* Read a Salesforce object available to the logged-in user.
*
* @param $name
* The object type name, e.g. Account
* @param $id
* The Salesforce ID
*/
function drush_salesforce_sf_read_object($name, $id) {
$salesforce = _drush_salesforce_drush_get_api();
try {
if ($object = $salesforce->objectRead($name, $id)) {
drush_print(drush_format($object));
}
}
catch (SalesforceException $e) {
drush_log($e->getMessage(), 'error');
}
}
/**
* Create a Salesforce object available to the logged-in user.
*
* @param $name
* The object type name, e.g. Account
* @param $data
* The object data, or '-' to read from stdin
*/
function drush_salesforce_sf_create_object($name, $data) {
if ($data == '-') {
$data = stream_get_contents(STDIN);
}
$format = drush_get_option('format', 'json');
$params = array();
switch ($format) {
case 'query':
parse_str($data, $params);
break;
case 'json':
$params = json_decode($data, TRUE);
break;
default:
drush_log(dt('Invalid format'), 'error');
return;
}
$salesforce = _drush_salesforce_drush_get_api();
try {
if ($result = $salesforce->objectCreate($name, $params)) {
drush_print_r($result);
}
}
catch (SalesforceException $e) {
drush_log($e->getMessage(), 'error');
}
}
/**
* Query Salesforce objects available to the logged-in user.
*
* @param $name
* The object type name, e.g. Account
*/
function drush_salesforce_sf_query_object($name) {
$salesforce = _drush_salesforce_drush_get_api();
$fields = drush_get_option('fields', '');
if (!$fields) {
try {
$object = $salesforce->objectDescribe($name);
}
catch (SalesforceException $e) {
drush_log($e->getMessage(), 'error');
return;
}
$names = array();
foreach ($object['fields'] as $field) {
$names[] = $field['name'];
}
$fields = implode(',', $names);
}
try {
$where = drush_get_option('where', '');
if ($where) {
$where = ' WHERE ' . $where;
}
$query = 'SELECT ' . $fields . ' FROM ' . $name;
// @todo - figure how to leverage SalesforceSelectQuery.
$result = $salesforce->apiCall('query?q=' . urlencode($query . $where));
drush_print(drush_format($result));
}
catch (SalesforceException $e) {
drush_log($e->getMessage(), 'error');
}
}
/**
* Execute a SOQL query.
*
* @param $query
* The query to execute
*/
function drush_salesforce_sf_execute_query($query = NULL) {
if (!$query) {
return drush_log('Please specify a query as an argument.', 'error');
}
$salesforce = _drush_salesforce_drush_get_api();
try {
$result = $salesforce->apiCall('query?q=' . urlencode($query));
drush_print(drush_format($result));
}
catch (SalesforceException $e) {
drush_log($e->getMessage(), 'error');
}
}