-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsalesforce.drush.inc
508 lines (461 loc) · 15.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<?php
/**
* @file
* Drush integration for Salesforce.
*/
use Drupal\salesforce\SFID;
use Drupal\salesforce\SelectQuery;
/**
* Implements hook_drush_command().
*
* @deprecated Support for drush 8 is deprecated and will be removed in a future release.
*/
function salesforce_drush_command() {
$items['sf-rest-version'] = [
'description' => 'Displays information about the current REST API version',
'aliases' => ['sfrv'],
];
$items['sf-list-objects'] = [
'description' => 'List the objects that are available in your organization and available to the logged-in user.',
'aliases' => ['sflo'],
];
$items['sf-describe-object'] = [
'description' => 'Retrieve all the metadata for an object, including information about each field, URLs, and child relationships.',
'aliases' => ['sfdo'],
'arguments' => [
'object' => 'The object name in Salesforce.',
],
'options' => [
'output' => "Specify an output type.
Options are:
info: (default) Display metadata about an object
fields: Display information about fields that are part of the object
field-data FIELDNAME: Display information about a specific field that is part of an object
raw: Display the complete, raw describe response.",
],
'examples' => [
'drush sfdo Contact' => 'Show metadata about Contact SObject type.',
'drush sfdo Contact --output=fields' => 'Show addtional metadata about Contact fields.',
'drush sfdo Contact --output=field --field=Email' => 'Show full metadata about Contact.Email field.',
'drush sfdo Contact --output=raw' => 'Display the full metadata for Contact SObject type.',
],
];
$items['sf-list-resources'] = [
'description' => 'Lists the resources available for the specified API version. It provides the name and URI of each resource.',
'aliases' => ['sflr'],
];
$items['sf-read-object'] = [
'description' => 'Retrieve all the data for an object with a specific ID.',
'aliases' => ['sfro'],
'arguments' => [
'id' => 'The object ID in Salesforce.',
],
'options' => [
'format' => [
'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'] = [
'description' => 'Create an object with specified data.',
'aliases' => ['sfco'],
'arguments' => [
'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' => [
'format' => [
'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'] = [
'description' => 'Query an object using SOQL with specified conditions.',
'aliases' => ['sfqo'],
'arguments' => [
'object' => 'The object type name in Salesforce (e.g. Account).',
],
'options' => [
'format' => [
'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' => [
'description' => 'A WHERE clause to add to the SOQL query',
],
'fields' => [
'description' => 'A comma-separated list fields to select in the SOQL query. If absent, an API call is used to find all fields',
],
'limit' => [
'description' => 'Integer limit on the number of results to return for the query.',
],
'order' => [
'description' => 'Comma-separated fields by which to sort results. Make sure to enclose in quotes for any whitespace.',
],
],
];
$items['sf-execute-query'] = [
'description' => 'Execute a SOQL query.',
'aliases' => ['sfeq', 'soql'],
'arguments' => [
'query' => 'The query to execute.',
],
];
$items['sf-pull-query'] = [
'description' => 'Given a mapping, enqueue records for pull from Salesforce, ignoring modification timestamp. This command is useful, for example, when seeding content for a Drupal site prior to deployment.',
'aliases' => ['sfpq', 'sfiq'],
'arguments' => [
'name' => 'Machine name of the Salesforce Mapping for which to queue pull records.',
],
'options' => [
'where' => [
'description' => 'A WHERE clause to add to the SOQL pull query. Default behavior is to query and pull all records.',
],
],
'examples' => [
'drush sfpq' => 'Interactively select a mapping for which to queue records.',
'drush sfpq user' => 'Query and queue all records for "user" Salesforce mapping.',
'drush sfpq user --where="Email like \'%foo%\' AND (LastName = \'bar\' OR FirstName = \'bar\')"' => 'Query and queue all records for "user" Salesforce mapping with Email field containing the string "foo" and First or Last name equal to "bar"',
],
];
$items['sf-pull-file'] = [
'description' => 'Given a mapping, enqueue a list of object IDs to be pulled from a CSV file, e.g. a Salesforce report. The first column of the CSV file must be SFIDs. Additional columns will be ignored.',
'aliases' => ['sfpf', 'sfif'],
'arguments' => [
'file' => 'CSV file name of 15- or 18-character Salesforce ids to be pulled. ',
'name' => 'Machine name of the Salesforce Mapping for which to queue pull records.',
],
];
return $items;
}
/**
* List the resources available for the specified API version.
*
* This command provides the name and URI of each resource.
*
* @deprecated Support for drush 8 is deprecated and will be removed in a future release.
*/
function drush_salesforce_sf_list_resources() {
_drush_salesforce_deprecated();
$salesforce = \Drupal::service('salesforce.client');
$resources = $salesforce->listResources();
if ($resources) {
$items[] = ['Resource', 'URL'];
foreach ($resources->resources as $resource => $url) {
$items[] = [$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.
*
* @deprecated Support for drush 8 is deprecated and will be removed in a future release.
*/
function drush_salesforce_sf_describe_object($object_name = NULL) {
_drush_salesforce_deprecated();
if (!$object_name) {
return drush_log('Please specify an object as an argument.', 'error');
}
$salesforce = \Drupal::service('salesforce.client');
$object = $salesforce->objectDescribe($object_name);
// Return if we cannot load any data.
if (!is_object($object)) {
return drush_log(dt('Could not load data for object !object', ['!object' => $object_name]), 'error');
}
$output = drush_get_option('output');
switch ($output) {
case 'raw':
drush_print_r($object->data);
return;
case 'fields':
$rows = [['Name', 'Type', 'Label']];
foreach ($object->fields as $field) {
$rows[] = [$field['name'], $field['type'], $field['label']];
}
drush_print_table($rows, TRUE);
return;
case 'field':
$fieldname = drush_get_option('field');
if (empty($fieldname)) {
drush_log(dt('Please specify a field name'), 'error');
return;
}
try {
$field_data = $object->getField($fieldname);
}
catch (\Exception $e) {
watchdog_exception('salesforce.drush', $e);
drush_log(dt('Could not load data for field !field on !object object', [
'!field' => $fieldname,
'!object' => $object_name,
]), 'error');
return;
}
drush_print_r($field_data);
return;
default:
if ($output != 'info') {
drush_log(dt('Unkonwn output option !output', ['!output' => $output]), 'error');
return;
}
// Display information about the object.
$rows = [];
$rows[] = ['Name', $object->name];
$rows[] = ['Label', $object->label];
$rows[] = ['Field count', count($object->getFields())];
$rows[] = ['SFID prefix', $object->keyPrefix];
$rows[] = [
'Child Relationships',
isset($object->childRelationships) ? count($object->childRelationships) : 0,
];
$rows[] = ['Searchable', ($object->searchable == 1) ? 'TRUE' : 'FALSE'];
$rows[] = ['Creatable', ($object->createable == 1) ? 'TRUE' : 'FALSE'];
$rows[] = ['Deletable', ($object->deletable == 1) ? 'TRUE' : 'FALSE'];
$rows[] = ['Mergeable', ($object->mergeable == 1) ? 'TRUE' : 'FALSE'];
$rows[] = ['Queryable', ($object->queryable == 1) ? 'TRUE' : 'FALSE'];
drush_print_table($rows);
return;
}
}
/**
* Displays information about the REST API version the site is using.
*
* @deprecated Support for drush 8 is deprecated and will be removed in a future release.
*/
function drush_salesforce_sf_rest_version() {
_drush_salesforce_deprecated();
$salesforce = \Drupal::service('salesforce.client');
$version_id = $salesforce->getApiVersion();
$versions = $salesforce->getVersions();
$version = $versions[$version_id];
$latest = array_pop($versions);
foreach ($version as $key => $value) {
$rows[] = [$key, $value];
}
$rows[] = ['login url', $salesforce->getLoginUrl()];
$rows[] = [
'latest version',
strcmp($version_id, $latest['version']) ? $latest['version'] : 'Yes',
];
drush_print_table($rows, TRUE);
}
/**
* List Salesforce objects.
*
* This command lists Salesforce objects that are available in your organization
* and available to the logged-in user.
*
* @deprecated Support for drush 8 is deprecated and will be removed in a future release.
*/
function drush_salesforce_sf_list_objects() {
_drush_salesforce_deprecated();
$salesforce = \Drupal::service('salesforce.client');
if ($objects = $salesforce->objects()) {
print_r($objects);
drush_print('The following objects are available in your organization and available to the logged-in user.');
$rows[] = ['Name', 'Label', 'Label Plural'];
foreach ($objects as $object) {
$rows[] = [
$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 string $id
* The Salesforce ID.
*
* @deprecated Support for drush 8 is deprecated and will be removed in a future release.
*/
function drush_salesforce_sf_read_object($id) {
_drush_salesforce_deprecated();
$salesforce = \Drupal::service('salesforce.client');
try {
$name = $salesforce->getObjectTypeName(new SFID($id));
if ($object = $salesforce->objectRead($name, $id)) {
drush_print(dt('!type with id !id:', [
'!type' => $object->type(),
'!id' => $object->id(),
]));
drush_print(drush_format($object->fields()));
}
}
catch (SalesforceException $e) {
drush_log($e->getMessage(), 'error');
}
}
/**
* Create a Salesforce object available to the logged-in user.
*
* @param string $name
* The object type name, e.g. Account.
* @param string $data
* The object data, or '-' to read from stdin.
*
* @deprecated Support for drush 8 is deprecated and will be removed in a future release.
*/
function drush_salesforce_sf_create_object($name, $data) {
_drush_salesforce_deprecated();
if ($data == '-') {
$data = stream_get_contents(STDIN);
}
$format = drush_get_option('format', 'json');
$params = [];
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 = \Drupal::service('salesforce.client');
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 string $name
* The object type name, e.g. Account.
*
* @deprecated Support for drush 8 is deprecated and will be removed in a future release.
*/
function drush_salesforce_sf_query_object($name) {
_drush_salesforce_deprecated();
$salesforce = \Drupal::service('salesforce.client');
$query = new SelectQuery($name);
$fields = drush_get_option('fields', '');
if (!$fields) {
$object = $salesforce->objectDescribe($name);
$query->fields = array_keys($object->getFields());
}
else {
$query->fields = explode(',', $fields);
}
$query->limit = drush_get_option('limit', '');
if ($where = drush_get_option('where', '')) {
$query->conditions = [[$where]];
}
if ($order = drush_get_option('order', '')) {
$query->order = [];
$orders = explode(',', $order);
foreach ($orders as $order) {
list($field, $dir) = preg_split('/\s+/', $order, 2);
$query->order[$field] = $dir;
}
}
try {
$result = $salesforce->query($query);
}
catch (SalesforceException $e) {
drush_log($e->getMessage(), 'error');
return;
}
foreach ($result->records() as $sfid => $record) {
drush_print(drush_format([$sfid => $record->fields()]));
}
$pretty_query = str_replace('+', ' ', (string) $query);
if (!$fields) {
$fields = implode(',', $query->fields);
$pretty_query = str_replace($fields, ' * ', $pretty_query);
}
drush_print(dt("Showing !size of !total records for query:\n!query", [
'!size' => count($result->records()),
'!total' => $result->size(),
'!query' => $pretty_query,
]));
}
/**
* Execute a SOQL query.
*
* @param string $query
* The query to execute.
*
* @deprecated Support for drush 8 is deprecated and will be removed in a future release.
*/
function drush_salesforce_sf_execute_query($query = NULL) {
_drush_salesforce_deprecated();
if (!$query) {
return drush_log('Please specify a query as an argument.', 'error');
}
$salesforce = \Drupal::service('salesforce.client');
try {
$result = $salesforce->apiCall('query?q=' . urlencode($query));
drush_print(drush_format($result));
}
catch (SalesforceException $e) {
drush_log($e->getMessage(), 'error');
}
}
/**
* Get a mapping from the given name, or from user input if name is empty.
*
* @param string $name
* The mapping name.
*
* @return \Drupal\salesforce_mapping\Entity\SalesforceMappingInterface|null
* The mapping.
*
* @deprecated Support for drush 8 is deprecated and will be removed in a future release.
*/
function _salesforce_drush_get_mapping($name = NULL) {
_drush_salesforce_deprecated();
$mapping_storage = \Drupal::service('entity_type.manager')
->getStorage('salesforce_mapping');
if (empty($name)) {
$choices = array_keys($mapping_storage->loadMultiple());
if (empty($choices)) {
drush_log(dt('No mappings found.'), 'error');
return;
}
ksort($choices);
$choice = drush_choice($choices, dt('Enter a number to choose which mapping to use.'));
if ($choice === FALSE) {
return;
}
$name = $choices[$choice];
}
$mapping = $mapping_storage->load($name);
if (empty($mapping)) {
drush_log(dt('Mapping !name not found.', ['!name' => $name]), 'error');
}
return $mapping;
}
/**
* Trigger a deprecation error.
*/
function _drush_salesforce_deprecated() {
trigger_error('Salesforce module support for Drush 8 is deprecated and will be removed in a future release', E_USER_DEPRECATED);
}