forked from danieljpeter/salesforceMetadataBackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetOrgData.php
679 lines (519 loc) · 25.3 KB
/
getOrgData.php
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
<?php
require_once ('settings.php');
require_once ('phpToolkit/SforcePartnerClient.php');
require_once ('phpToolkit/SforceHeaderOptions.php');
//========================================= OPERATION 1, do some file copy / delete preparation to clear out the results of the previous time we did this
echo shell_exec('cp /home/dpeter/sf/build_template.xml /home/dpeter/sf/build.xml');
echo shell_exec('rm -rf /home/dpeter/sf/lists/*');
echo shell_exec('rm -rf /home/dpeter/sf/metadata/*');
//========================================= END OPERATION 1
//========================================= OPERATION 2, get a bunch of listings via ant's listMetadata so we can parse those resulting files later in the script
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listCustomField');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listAccountCriteriaBasedSharingRule');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listAccountOwnerSharingRule');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listAccountSharingRules');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listBusinessProcess');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listCampaignCriteriaBasedSharingRule');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listCampaignOwnerSharingRule');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listCampaignSharingRules');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listCaseCriteriaBasedSharingRule');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listCaseOwnerSharingRule');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listCaseSharingRules');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listContactCriteriaBasedSharingRule');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listContactOwnerSharingRule');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listContactSharingRules');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listCustomField');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listCustomObjectCriteriaBasedSharingRule');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listCustomObjectSharingRules');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listFieldSet');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listLeadCriteriaBasedSharingRule');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listLeadOwnerSharingRule');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listLeadSharingRules');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listLetterhead');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listListView');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listNamedFilter');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listOpportunityCriteriaBasedSharingRule');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listOpportunityOwnerSharingRule');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listOpportunitySharingRules');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listRecordType');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listSharingReason');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listValidationRule');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml listWebLink');
//========================================= END OPERATION 2
//========================================= OPERATION 3, get some folder names via the REST API so we can get some metadata components which rely on the folder names
//get a session ID via the REST API
$sessionId = getSession($USERNAME, $PASSWORD);
$arrReports = getReportFolders($sessionId);
$arrDashboards = getDashboardFolders($sessionId);
$arrEmailTemplates = getEmailTemplateFolders($sessionId);
$arrDocuments = getDocumentFolders($sessionId);
$bulkRetrieve = '';
$bulkRetrieve .= buildBulkRetrieve('Report', $arrReports);
$bulkRetrieve .= buildBulkRetrieve('Dashboard', $arrDashboards);
$bulkRetrieve .= buildBulkRetrieve('EmailTemplate', $arrEmailTemplates);
$bulkRetrieve .= buildBulkRetrieve('Document', $arrDocuments);
//open the build.xml file and populate the bulkRetrieve based on the folders names we got
searchReplaceBuildXmlFile('<!-- bulkRetrieveFolders populated by script -->', $bulkRetrieve);
//========================================= END OPERATION 3
//========================================= OPERATION 4. get the standard and custom object names via the rest API, and there subcomponents via ant list
//get a list of standard and custom objects
$arrObjectList = getObjectLists($sessionId);
$arrStdObjects = $arrObjectList['standard'];
$arrCustomObjects = $arrObjectList['custom'];
//loop through the std and custom objects, building this kind of XML:
/*
<types>
<members>Account</members>
<name>CustomObject</name>
</types>
*/
$objectXML = "";
$objectXML .= "\t<types>\r\n";
$objectXML .= "\t\t<members>*</members>\r\n";
foreach($arrStdObjects as $stdObj) {
$objectXML .= "\t\t<members>".$stdObj."</members>\r\n";
}
foreach($arrCustomObjects as $custObj) {
$objectXML .= "\t\t<members>".$custObj."</members>\r\n";
}
$objectXML .= "\t\t<name>CustomObject</name>\r\n";
$objectXML .= "\t</types>\r\n";
// we need to get these subcomponents of our objects:
//CustomField,BusinessProcess,RecordType,WebLink,ValidationRule,NamedFilter,SharingReason,ListView,FieldSet
$arrCustomField = filerToStandardObjects(parseLog("/home/dpeter/sf/lists/CustomField.log"));
$arrBusinessProcess = parseLog("/home/dpeter/sf/lists/BusinessProcess.log");
$arrRecordType = parseLog("/home/dpeter/sf/lists/RecordType.log");
$arrWebLink = parseLog("/home/dpeter/sf/lists/WebLink.log");
$arrValidationRule = parseLog("/home/dpeter/sf/lists/ValidationRule.log");
$arrNamedFilter = parseLog("/home/dpeter/sf/lists/NamedFilter.log");
$arrSharingReason = parseLog("/home/dpeter/sf/lists/SharingReason.log");
$arrListView = parseLog("/home/dpeter/sf/lists/ListView.log");
$arrFieldSet = parseLog("/home/dpeter/sf/lists/FieldSet.log");
$objectSubXML = "";
$objectSubXML .= generateXMLTypeBlock($arrCustomField, "CustomField");
$objectSubXML .= generateXMLTypeBlock($arrBusinessProcess, "BusinessProcess");
$objectSubXML .= generateXMLTypeBlock($arrRecordType, "RecordType");
$objectSubXML .= generateXMLTypeBlock($arrWebLink, "WebLink");
$objectSubXML .= generateXMLTypeBlock($arrValidationRule, "ValidationRule");
$objectSubXML .= generateXMLTypeBlock($arrNamedFilter, "NamedFilter");
$objectSubXML .= generateXMLTypeBlock($arrSharingReason, "SharingReason");
$objectSubXML .= generateXMLTypeBlock($arrListView, "ListView");
$objectSubXML .= generateXMLTypeBlock($arrFieldSet, "FieldSet");
$completeObjectXML = $objectXML.$objectSubXML; //join the object, and subcomponent together
createPackageXMLfile($completeObjectXML, "objects.xml"); //write them to the package.xml file
//========================================= END OPERATION 4
//========================================= OPERATION 5. get any remaining components which need to be dot qualified with info we get from ant
$arrLetterhead = parseLog("/home/dpeter/sf/lists/Letterhead.log");
$arrAccountCriteriaBasedSharingRule = parseLog("/home/dpeter/sf/lists/AccountCriteriaBasedSharingRule.log");
$arrAccountOwnerSharingRule = parseLog("/home/dpeter/sf/lists/AccountOwnerSharingRule.log");
$arrAccountSharingRules = parseLog("/home/dpeter/sf/lists/AccountSharingRules.log");
$arrCampaignCriteriaBasedSharingRule = parseLog("/home/dpeter/sf/lists/CampaignCriteriaBasedSharingRule.log");
$arrCampaignOwnerSharingRule = parseLog("/home/dpeter/sf/lists/CampaignOwnerSharingRule.log");
$arrCampaignSharingRules = parseLog("/home/dpeter/sf/lists/CampaignSharingRules.log");
$arrCaseCriteriaBasedSharingRule = parseLog("/home/dpeter/sf/lists/CaseCriteriaBasedSharingRule.log");
$arrCaseOwnerSharingRule = parseLog("/home/dpeter/sf/lists/CaseOwnerSharingRule.log");
$arrCaseSharingRules = parseLog("/home/dpeter/sf/lists/CaseSharingRules.log");
$arrContactCriteriaBasedSharingRule = parseLog("/home/dpeter/sf/lists/ContactCriteriaBasedSharingRule.log");
$arrContactOwnerSharingRule = parseLog("/home/dpeter/sf/lists/ContactOwnerSharingRule.log");
$arrContactSharingRules = parseLog("/home/dpeter/sf/lists/ContactSharingRules.log");
$arrCustomObjectCriteriaBasedSharingRule = parseLog("/home/dpeter/sf/lists/CustomObjectCriteriaBasedSharingRule.log");
$arrCustomObjectSharingRules = parseLog("/home/dpeter/sf/lists/CustomObjectSharingRules.log");
$arrLeadCriteriaBasedSharingRule = parseLog("/home/dpeter/sf/lists/LeadCriteriaBasedSharingRule.log");
$arrLeadOwnerSharingRule = parseLog("/home/dpeter/sf/lists/LeadOwnerSharingRule.log");
$arrLeadSharingRules = parseLog("/home/dpeter/sf/lists/LeadSharingRules.log");
$arrOpportunityCriteriaBasedSharingRule = parseLog("/home/dpeter/sf/lists/OpportunityCriteriaBasedSharingRule.log");
$arrOpportunityOwnerSharingRule = parseLog("/home/dpeter/sf/lists/OpportunityOwnerSharingRule.log");
$arrOpportunitySharingRules = parseLog("/home/dpeter/sf/lists/OpportunitySharingRules.log");
$remainingXML = "";
$remainingXML .= generateXMLTypeBlock($arrLetterhead,"Letterhead");
$remainingXML .= generateXMLTypeBlock($arrAccountCriteriaBasedSharingRule,"AccountCriteriaBasedSharingRule");
$remainingXML .= generateXMLTypeBlock($arrAccountOwnerSharingRule,"AccountOwnerSharingRule");
$remainingXML .= generateXMLTypeBlock($arrAccountSharingRules,"AccountSharingRules");
$remainingXML .= generateXMLTypeBlock($arrCampaignCriteriaBasedSharingRule,"CampaignCriteriaBasedSharingRule");
$remainingXML .= generateXMLTypeBlock($arrCampaignOwnerSharingRule,"CampaignOwnerSharingRule");
$remainingXML .= generateXMLTypeBlock($arrCampaignSharingRules,"CampaignSharingRules");
$remainingXML .= generateXMLTypeBlock($arrCaseCriteriaBasedSharingRule,"CaseCriteriaBasedSharingRule");
$remainingXML .= generateXMLTypeBlock($arrCaseOwnerSharingRule,"CaseOwnerSharingRule");
$remainingXML .= generateXMLTypeBlock($arrCaseSharingRules,"CaseSharingRules");
$remainingXML .= generateXMLTypeBlock($arrContactCriteriaBasedSharingRule,"ContactCriteriaBasedSharingRule");
$remainingXML .= generateXMLTypeBlock($arrContactOwnerSharingRule,"ContactOwnerSharingRule");
$remainingXML .= generateXMLTypeBlock($arrContactSharingRules,"ContactSharingRules");
$remainingXML .= generateXMLTypeBlock($arrCustomObjectCriteriaBasedSharingRule,"CustomObjectCriteriaBasedSharingRule");
$remainingXML .= generateXMLTypeBlock($arrCustomObjectSharingRules,"CustomObjectSharingRules");
$remainingXML .= generateXMLTypeBlock($arrLeadCriteriaBasedSharingRule,"CriteriaBasedSharingRule");
$remainingXML .= generateXMLTypeBlock($arrLeadOwnerSharingRule,"LeadOwnerSharingRule");
$remainingXML .= generateXMLTypeBlock($arrLeadSharingRules,"LeadSharingRules");
$remainingXML .= generateXMLTypeBlock($arrOpportunityCriteriaBasedSharingRule,"OpportunityCriteriaBasedSharingRule");
$remainingXML .= generateXMLTypeBlock($arrOpportunityOwnerSharingRule,"OpportunityOwnerSharingRule");
$remainingXML .= generateXMLTypeBlock($arrOpportunitySharingRules,"OpportunitySharingRules");
createPackageXMLfile($remainingXML, "remaining.xml"); //write them to the package.xml file
//========================================= END OPERATION 5
//========================================= OPERATION 6, run the ant jobs to retrieve the metadata we have defined
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml bulkRetrieve');
echo shell_exec('ant -buildfile /home/dpeter/sf/build.xml bulkRetrieveFolders');
//========================================= END OPERATION 6
//========================================= OPERATION 7, remove any files from local git repo which were removed in salesforce, copy all the files to the git repo, add, commit, push to github
/*
we have a local git repo in:
/home/dpeter/git/salesforce-metadata
we want to copy the metadata we downloaded here:
/home/dpeter/metadata
which we want to push up to github:
[email protected]:safarijv/salesforce-metadata.git
*/
//=============================================================
//remove all the files from the local github repo which aren't in the metadata repo (in case these were objects which were deleted in salesforce)
//get the an array of each file listings, recursively. Strip off the beginning path from each entry in the array so we can do text comparison
$arrMetadata = stripBlanks(str_replace('/home/dpeter/sf/metadata', '', getFilesFromDir('/home/dpeter/sf/metadata')));
$arrGit = stripBlanks(str_replace('/home/dpeter/sf/git/salesforce-metadata', '', getFilesFromDir('/home/dpeter/sf/git/salesforce-metadata')));
//remove array entries in the /.git directory
$arrGit = stripGit($arrGit);
$arrDeleteFromGit = array();
$fileInGitAndMetadata = false;
foreach ($arrGit as $gitFile) {
$fileInGitAndMetadata = false;
foreach ($arrMetadata as $metadataFile) {
if ($gitFile == $metadataFile) {
$fileInGitAndMetadata = true;
}
}
if (!$fileInGitAndMetadata) {
array_push($arrDeleteFromGit, $gitFile);
}
}
//print_r($arrDeleteFromGit);
foreach ($arrDeleteFromGit as $gitFile) {
echo shell_exec('git --git-dir=/home/dpeter/sf/git/salesforce-metadata/.git --work-tree=/home/dpeter/sf/git/salesforce-metadata/ rm -r -f /home/dpeter/sf/git/salesforce-metadata'.$gitFile);
}
//=============================================================
//copy all the files from the metadata downloads back into the local github repo
echo shell_exec('cp -R -f metadata/* git/salesforce-metadata');
//add the files to the local git repo
echo shell_exec('git --git-dir=/home/dpeter/sf/git/salesforce-metadata/.git --work-tree=/home/dpeter/sf/git/salesforce-metadata/ add /home/dpeter/sf/git/salesforce-metadata/*');
//commit the files
echo shell_exec('git --git-dir=/home/dpeter/sf/git/salesforce-metadata/.git --work-tree=/home/dpeter/sf/git/salesforce-metadata/ commit -m "Automated Daily Commit from SFSYNC"');
//push the files
echo shell_exec('git --git-dir=/home/dpeter/sf/git/salesforce-metadata/.git --work-tree=/home/dpeter/sf/git/salesforce-metadata/ push origin master');
//========================================= END OPERATION 7
function generateXMLTypeBlock($arrMembers, $sName) {
//Build an XML block like below, iterating over $arrMembers
/*
<types>
<members>Account</members>
<name>CustomObject</name>
</types>
*/
$sXML = "";
if (count($arrMembers) > 0) {
$sXML .= "\t<types>\r\n";
foreach ($arrMembers as $arrField) {
$sXML .= "\t\t<members>".$arrField."</members>\r\n";
}
$sXML .= "\t\t<name>".$sName."</name>\r\n";
$sXML .= "\t</types>\r\n";
}
return $sXML;
}
function parseLog($fileName) {
$arrFields = array();
$myFile = $fileName;
if (file_exists($myFile)) {
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
//read the file into an array of lines
$arrLines = explode("\n", $theData);
//loop through the lines and get the ones that start with "FullName/Id:"
//lines look like this: FullName/Id: Case.Queue_Feedback/00B000000095c8IEAQ
foreach ($arrLines as $arrLine) {
if (strpos($arrLine, "FullName/Id:") !== false) {
//parse out just the objectName.ListView, FieldName, etc from the string
$arrTemp = explode("FullName/Id:", $arrLine, 2);
if (count($arrTemp) == 2) {
$arrTemp = explode("/", $arrTemp[1], 2);
if (count($arrTemp) == 2) {
array_push($arrFields, trim($arrTemp[0]));
}
}
}
}
}
sort($arrFields);
return $arrFields;
}
function filerToStandardObjects($arrInput) {
//now we want to filter this list down to just the custom fields on the standard object. we don't want the custom fields on the custom objects, because they
//will already get included with the custom objects.
$arrStandard = array();
foreach ($arrInput as $arrField) {
//Account.Auto_renewing__c //standard looks like this
//Alert__c.Send_Support_Page__c //custom looks like this
//Support_Alert__kav.Details__c //custom also looks like this
$isCustom = false;
$arrTemp = explode(".", $arrField, 2);
if (count($arrTemp) == 2) {
if (strtolower(substr($arrTemp[0], -3)) == "__c") {
$isCustom = true;
}
if (strtolower(substr($arrTemp[0], -5)) == "__kav") {
$isCustom = true;
}
if (!$isCustom) {
array_push($arrStandard, $arrField);
}
}
}
return $arrStandard;
}
function parseCustomFieldLog() {
$myFile = "lists/CustomField.log";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
//read the file into an array of lines
$arrLines = explode("\n", $theData);
//loop through the lines and get the ones that start with "FullName/Id:"
//lines look like this: FullName/Id: Opportunity.Qualified_Pre_Trial_Checklist__c/00N000000072Qe0EAE
$arrFields = array();
foreach ($arrLines as $arrLine) {
if (strpos($arrLine, "FullName/Id:") !== false) {
//parse out just the objectName.FieldName from the string
$arrTemp = explode("FullName/Id:", $arrLine, 2);
if (count($arrTemp) == 2) {
$arrTemp = explode("/", $arrTemp[1], 2);
if (count($arrTemp) == 2) {
array_push($arrFields, trim($arrTemp[0]));
}
}
}
}
//now we want to filter this list down to just the custom fields on the standard object. we don't want the custom fields on the custom objects, because they
//will already get included with the custom objects.
$arrStandard = array();
foreach ($arrFields as $arrField) {
//Account.Auto_renewing__c //standard looks like this
//Alert__c.Send_Support_Page__c //custom looks like this
//Support_Alert__kav.Details__c //custom also looks like this
$isCustom = false;
$arrTemp = explode(".", $arrField, 2);
if (count($arrTemp) == 2) {
if (strtolower(substr($arrTemp[0], -3)) == "__c") {
$isCustom = true;
}
if (strtolower(substr($arrTemp[0], -5)) == "__kav") {
$isCustom = true;
}
if (!$isCustom) {
array_push($arrStandard, $arrField);
}
}
}
$arrFields = $arrStandard;
sort($arrFields);
return $arrFields;
}
function createPackageXMLfile($sXML, $sFilename) {
$myFile = $sFilename;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, getPackageXMLheader().$sXML.getPackageXMLfooter());
fclose($fh);
}
function getPackageXMLheader() {
$sReturn = '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
$sReturn .= '<Package xmlns="http://soap.sforce.com/2006/04/metadata">'."\r\n";
return $sReturn;
}
function getPackageXMLfooter() {
$sReturn = '</Package>'."\r\n";
return $sReturn;
}
function getCustomFieldsFromObjectRaw($arrObject) {
$arrReturn = array();
foreach ((array) $arrObject['fields'] as $field) {
if ($field['custom'] == true) {
array_push($arrReturn, $field['name']);
}
}
return $arrReturn;
}
function getObjectInfoRaw($objectName, $access_token) {
$url = "https://ssl.salesforce.com/services/data/v24.0/sobjects/".$objectName."/describe/";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: OAuth $access_token"));
$json_response = curl_exec($curl);
curl_close($curl);
$response = json_decode($json_response, true);
return $response;
}
function getObjectLists($access_token) {
//this function returns an assoc array. The value "standard" is an array of the std objects, "custom" is an array of the custom objects
$arrReturn = array();
$arrReturn['standard'] = array();
$arrReturn['custom'] = array();
$url = "https://ssl.salesforce.com/services/data/v24.0/sobjects/";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: OAuth $access_token"));
$json_response = curl_exec($curl);
curl_close($curl);
$response = json_decode($json_response, true);
foreach ((array) $response['sobjects'] as $record) {
if ($record['custom'] == true) {
array_push($arrReturn['custom'], $record['name']);
} else {
array_push($arrReturn['standard'], $record['name']);
}
}
return $arrReturn;
}
function searchReplaceBuildXmlFile($search, $replace) {
// Open file for read and string modification
$file = "/home/dpeter/sf/build.xml";
$fh = fopen($file, 'r');
$contents = fread($fh, filesize($file));
$new_contents = str_replace($search, $replace, $contents);
fclose($fh);
// Open file to write
$fh = fopen($file, 'w');
fwrite($fh, $new_contents);
fclose($fh);
}
function buildBulkRetrieve($metadataType, $arrFolders) {
$sReturn = '';
foreach ($arrFolders as $value) {
$sReturn .= "\t\t".'<sf:bulkRetrieve username="${sf.username}" password="${sf.password}" serverurl="${sf.serverurl}" metadataType="'.$metadataType.'" containingFolder="'.$value.'" retrieveTarget="/home/dpeter/sf/metadata"/>'."\r\n";
}
return rtrim($sReturn);
}
function getSession($USERNAME, $PASSWORD) {
$sessionId = '';
try {
$mySforceConnection = new SforcePartnerClient();
$mySoapClient = $mySforceConnection->createConnection('phpToolkit/partner.wsdl.xml');
$mylogin = $mySforceConnection->login($USERNAME, $PASSWORD);
$sessionId = $mylogin->sessionId;
} catch (Exception $e) {
print_r($mySforceConnection->getLastRequest());
echo $e->faultstring;
}
return $sessionId;
}
function getReportFolders($access_token) {
$arrReturn = array();
$query = "SELECT Id, DeveloperName FROM Folder WHERE Type='Report' AND DeveloperName != ''";
$url = "https://ssl.salesforce.com/services/data/v20.0/query?q=" . urlencode($query);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: OAuth $access_token"));
$json_response = curl_exec($curl);
curl_close($curl);
$response = json_decode($json_response, true);
foreach ((array) $response['records'] as $record) {
array_push($arrReturn, $record['DeveloperName']);
}
return $arrReturn;
}
function getDashboardFolders($access_token) {
$arrReturn = array();
$query = "SELECT Id, DeveloperName FROM Folder WHERE Type='Dashboard' AND DeveloperName != ''";
$url = "https://ssl.salesforce.com/services/data/v20.0/query?q=" . urlencode($query);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: OAuth $access_token"));
$json_response = curl_exec($curl);
curl_close($curl);
$response = json_decode($json_response, true);
foreach ((array) $response['records'] as $record) {
array_push($arrReturn, $record['DeveloperName']);
}
return $arrReturn;
}
function getDocumentFolders($access_token) {
$arrReturn = array();
$query = "SELECT Id, DeveloperName FROM Folder WHERE Type='Document' AND DeveloperName != ''";
$url = "https://ssl.salesforce.com/services/data/v20.0/query?q=" . urlencode($query);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: OAuth $access_token"));
$json_response = curl_exec($curl);
curl_close($curl);
$response = json_decode($json_response, true);
foreach ((array) $response['records'] as $record) {
array_push($arrReturn, $record['DeveloperName']);
}
return $arrReturn;
}
function getEmailTemplateFolders($access_token) {
$arrReturn = array();
$query = "SELECT Id, DeveloperName FROM Folder WHERE Type='Email' AND DeveloperName != ''";
$url = "https://ssl.salesforce.com/services/data/v20.0/query?q=" . urlencode($query);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: OAuth $access_token"));
$json_response = curl_exec($curl);
curl_close($curl);
$response = json_decode($json_response, true);
foreach ((array) $response['records'] as $record) {
array_push($arrReturn, $record['DeveloperName']);
}
return $arrReturn;
}
function getFilesFromDir($dir) {
$files = array();
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if(is_dir($dir.'/'.$file)) {
$dir2 = $dir.'/'.$file;
$files[] = getFilesFromDir($dir2);
}
else {
$files[] = $dir.'/'.$file;
}
}
}
closedir($handle);
}
return array_flat($files);
}
function array_flat($array) {
foreach($array as $a) {
if(is_array($a)) {
$tmp = array_merge($tmp, array_flat($a));
}
else {
$tmp[] = $a;
}
}
return $tmp;
}
function stripGit($arrInput) {
$arrStripped = array();
foreach ($arrInput as $stripFile) {
if (substr($stripFile, 0, 5) == "/.git") {
//don't add it
} else {
array_push($arrStripped, $stripFile);
}
}
return $arrStripped;
}
function stripBlanks($arrInput) {
$arrStripped = array();
foreach ($arrInput as $stripFile) {
if (trim($stripFile) == "") {
//don't add it
} else {
array_push($arrStripped, trim($stripFile));
}
}
return $arrStripped;
}
?>