-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathBuildCommand.php
More file actions
806 lines (708 loc) · 30.6 KB
/
Copy pathBuildCommand.php
File metadata and controls
806 lines (708 loc) · 30.6 KB
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
<?php
namespace modmore\Gitify\Command;
use modmore\Gitify\BaseCommand;
use modmore\Gitify\Gitify;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Exception\ParseException;
/**
* Class BuildCommand
*
* Builds a MODX site from the files and configuration.
*
* @package modmore\Gitify\Command
*/
class BuildCommand extends BaseCommand
{
public $categories = array();
public $isForce = false;
public $existingObjects = array();
public $conflictingObjects = array();
public $updatedObjects;
public $orphanedObjects;
protected $_metaCache = array();
protected $_resourceIds = array();
protected function configure()
{
$this
->setName('build')
->setDescription('Builds a MODX site from the files and configuration.')
->addOption(
'skip-clear-cache',
null,
InputOption::VALUE_NONE,
'When specified, it will skip clearing the cache after building.'
)
->addOption(
'force',
'f',
InputOption::VALUE_NONE,
'When specified, all existing content will be removed before rebuilding. Can be useful when having dealt with complex conflicts.'
)
->addOption(
'no-backup',
null,
InputOption::VALUE_NONE,
'When using the --force attribute, Gitify will automatically create a full database backup first. Specify --no-backup to skip creating the backup, at your own risk.'
)
->addOption(
'no-cleanup',
null,
InputOption::VALUE_NONE,
'With --no-cleanup specified the built-in orphan handling is disabled for this build. The orphan handling removes objects that no longer exist in files from the database. '
)
->addArgument(
'partitions',
InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
'Specify the data partition key (folder name), or keys separated by a space, that you want to build. '
)
;
}
/**
* Runs the command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->isForce = $input->getOption('force');
if ($this->isForce && !$input->getOption('no-backup')) {
$backup = $this->getApplication()->find('backup');
$arguments = array(
'command' => 'backup'
);
$backupInput = new ArrayInput($arguments);
if ($backup->run($backupInput, $output) !== 0) {
$output->writeln('<error>Could not write backup. Try building without --force, or specify the --no-backup flag to force build without writing a backup.');
return 1;
}
}
$partitions = $input->getArgument('partitions');
if (!$partitions || empty($partitions)) {
$partitions = array_keys($this->config['data']);
}
foreach ($this->config['data'] as $folder => $type) {
if (!in_array($folder, $partitions, true)) {
if ($output->isVerbose()) {
$output->writeln('Skipping ' . $folder);
}
continue;
}
$type['folder'] = $folder;
switch (true) {
case (!empty($type['type']) && $type['type'] == 'content'):
// "content" is a shorthand for contexts + resources
$output->writeln("Building content from $folder/...");
$this->buildContent($this->config['data_directory'] . $folder, $type);
break;
case (!empty($type['class'])):
$doing = $this->isForce ? 'Force building' : 'Building';
$output->writeln("{$doing} {$type['class']} from {$folder}/...");
if (isset($type['package'])) {
$this->getPackage($type['package'], $type);
}
$this->buildObjects($this->config['data_directory'] . $folder, $type);
break;
}
}
if (!$input->getOption('skip-clear-cache')) {
$output->writeln('Clearing cache...');
$this->modx->getCacheManager()->refresh();
}
$output->writeln('Done! ' . $this->getRunStats());
return 0;
}
/**
* Loads the Content, handling uris for naming etc.
*
* @param $folder
* @param $type
*/
public function buildContent($folder, $type)
{
if ($this->isForce) {
$this->output->writeln('Forcing build, removing prior Resources...');
$forceCriteria = $this->getPartitionCriteria($type['folder']);
if (is_null($forceCriteria)) {
$forceCriteria = array();
}
$this->modx->removeCollection('modResource', $forceCriteria);
if (isset($type['truncate_on_force'])) {
foreach ($type['truncate_on_force'] as $class) {
$this->output->writeln('> Truncating ' . $class . ' before force building Resources...');
$this->modx->removeCollection($class, array());
}
}
}
// Conflict handling
$this->resetConflicts();
$this->getExistingObjects('modResource', $this->getPartitionCriteria($type['folder']));
$folder = GITIFY_WORKING_DIR . $folder;
$directory = new \DirectoryIterator($folder);
foreach ($directory as $path => $info) {
/** @var \SplFileInfo $info */
$name = $info->getFilename();
// Ignore dotfiles/folders
if (substr($name, 0, 1) == '.') continue;
if (!$info->isDir()) {
//$output->writeln('Expecting directory, got ' . $info->getType() . ': ' . $name);
continue;
}
$context = $this->modx->getObject('modContext', array('key' => $name));
if (!$context) {
$this->output->writeln('- Context ' . $name . ' does not exist. Perhaps you\'re missing contexts data?');
continue;
}
$this->output->writeln('- Building ' . $name . ' context...');
$path = $info->getRealPath();
$this->buildResources($name, new \RecursiveDirectoryIterator($path));
}
$type['class'] = 'modResource';
$this->removeOrphans($type, 'uri');
$this->resolveConflicts($folder, $type, true);
}
/**
* Loops over resource files to create the resources.
*
* @param $context
* @param $iterator
*/
public function buildResources($context, $iterator)
{
$resources = array();
$parents = array();
foreach ($iterator as $fileInfo) {
if (substr($fileInfo->getFilename(), 0, 1) == '.') {
continue;
}
/** @var \SplFileInfo $fileInfo */
if ($fileInfo->isDir()) {
$parents[] = $fileInfo;
}
elseif ($fileInfo->isFile()) {
$resources[] = $fileInfo;
}
}
// create the resources first
/** @var \SplFileInfo $resource */
foreach ($resources as $resource) {
$file = file_get_contents($resource->getRealPath());
// Normalize line-endings to \n to ensure consistency
$file = str_replace("\r\n", "\n", $file);
$file = str_replace("\r", "\n", $file);
// Check if delimiter exists, otherwise add it to avoid WARN in explode()
// (WARN @ Gitify/src/Command/BuildCommand.php : 246) PHP notice: Undefined offset: 1
if (strpos($file, Gitify::$contentSeparator) === false) {
$file = $file . Gitify::$contentSeparator;
}
list($rawData, $content) = explode(Gitify::$contentSeparator, $file);
try {
$data = Gitify::fromYAML($rawData);
} catch (ParseException $Exception) {
$this->output->writeln('<error>Could not parse ' . $resource->getFilename() . ': ' . $Exception->getMessage() .'</error>');
continue;
}
if (!empty($content)) {
$data['content'] = $content;
}
$data['context_key'] = $context;
$this->buildSingleResource($data);
}
// Then loop over all subs
foreach ($parents as $parentResource) {
$this->buildResources($context, new \RecursiveDirectoryIterator($parentResource.'/'));
}
}
/**
* Creates or updates a single Resource
*
* @param $data
* @param bool $isConflictResolution
*/
public function buildSingleResource($data, $isConflictResolution = false) {
$this->modx->setOption(\xPDO::OPT_SETUP, true);
// Figure out the primary key - it's either uri or id in the case of a resource.
if (!empty($data['uri'])) {
$primary = array('uri' => $data['uri'], 'context_key' => $data['context_key']);
$primaryKey = array('uri', 'context_key');
$method = 'uri';
}
else {
$primary = $data['id'];
$primaryKey = 'id';
$method = 'id';
}
if (!$isConflictResolution && $this->hasConflict('modResource', $primaryKey, $primary, $data)) {
return;
}
// Grab the resource, or create a new one.
$new = false;
$oldId = null;
$object = ($this->isForce) ? false : $this->modx->getObject('modResource', $primary);
if (!($object instanceof \modResource)) {
$object = $this->modx->newObject('modResource');
$new = true;
// Attempt to duplicate existing resource to another context or uri
if ($method === 'uri' && $this->modx->getCount('modResource', $data['id'])) {
$oldId = $data['id'];
unset($data['id']);
$data['parent'] = isset($data['parent'], $this->_resourceIds[$data['parent']])
? $this->_resourceIds[$data['parent']]
: 0;
}
}
// Ensure all fields have a value
foreach ($object->_fieldMeta as $field => $meta) {
if (!isset($data[$field])) $data[$field] = isset($meta['default']) ? $meta['default'] : null;
}
// Set the fields on the resource
$object->fromArray($data, '', true, true);
// Process stored TVs
if (isset($data['tvs'])) {
foreach($data['tvs'] as $key => $value) {
$object->setTVValue($key, $value);
}
}
// Save it!
if ($object->save()) {
if ($oldId) {
$this->_resourceIds[$oldId] = $object->get('id');
}
if ($this->output->isVerbose()) {
$new = ($new) ? 'Created new' : 'Updated';
$this->output->writeln("- {$new} resource from {$method}: {$data[$method]}");
}
$pk = $object->getPrimaryKey();
if (is_array($pk)) {
$pk = json_encode($pk);
}
if (isset($this->orphanedObjects[$pk])) {
unset($this->orphanedObjects[$pk]);
}
}
else {
$new = ($new) ? 'new' : 'updated';
$this->output->writeln("- <error>Could not save {$new} resource from {$method}: {$data[$method]}</error>");
}
}
/**
* Loops over an object folder and parses the files to pass to buildSingleObject
*
* @param $folder
* @param $type
*/
public function buildObjects($folder, $type)
{
if (!file_exists(GITIFY_WORKING_DIR . $folder)) {
$this->output->writeln('> Skipping ' . $type['class'] . ', ' . $folder. ' does not exist.');
return;
}
$criteria = $this->getPartitionCriteria($type['folder']);
if (is_null($criteria)) {
$criteria = array();
}
if ($this->isForce) {
$this->modx->removeCollection($type['class'], $criteria);
if (isset($type['truncate_on_force'])) {
foreach ($type['truncate_on_force'] as $class) {
$this->output->writeln('> Truncating ' . $class . ' before force building ' . $type['class'] . '...');
$this->modx->removeCollection($class, array());
}
}
/**
* @deprecated 2015-03-30
*
* Deprecated in favour of specifying truncate_on_force in the .gitify file.
*/
switch ($type['class']) {
// $modx->removeCollection does not automatically remove related objects, which in the case
// of modCategory results in orphaned modCategoryClosure objects. Normally, this is okay, because
// Gitify recreates the objects with the same ID. But Categories automatically add the closure on
// save, which then throws a bunch of errors about duplicate IDs. Worst of all, it _removes_ the
// category object if it can't save the closure, causing the IDs to go all over the place.
// So in this case, we make sure all category closures are wiped too.
case 'modCategory':
$this->modx->removeCollection('modCategoryClosure', array());
break;
}
}
$directory = new \DirectoryIterator(GITIFY_WORKING_DIR . $folder);
// Reset the conflicts so we're good to go on new ones
$this->resetConflicts();
$this->getExistingObjects($type['class'], $criteria);
foreach ($directory as $file) {
/** @var \SplFileInfo $file */
$name = $file->getFilename();
// Ignore dotfiles/folders
if (substr($name, 0, 1) == '.') continue;
if (!$file->isFile()) {
$this->output->writeln('- Skipping ' . $file->getType() . ': ' . $name);
continue;
}
// Load the file contents
$file = file_get_contents($file->getRealPath());
// Normalize line-endings to \n to ensure consistency
$file = str_replace("\r\n", "\n", $file);
$file = str_replace("\r", "\n", $file);
// Check if delimiter exists, otherwise add it to avoid WARN in explode()
// (WARN @ Gitify/src/Command/BuildCommand.php : 407) PHP notice: Undefined offset: 1
if (strpos($file, Gitify::$contentSeparator) === false) {
$file = $file . Gitify::$contentSeparator;
}
// Get the raw data, and the content
list($rawData, $content) = explode(Gitify::$contentSeparator, $file);
// Turn the raw YAML data into an array
$data = Gitify::fromYAML($rawData);
if (!empty($content)) {
$data['content'] = $content;
}
$this->buildSingleObject($data, $type);
}
$this->removeOrphans($type);
$this->resolveConflicts($folder, $type);
// Due to autoloading via namespaces (bootstrap.php) in MODX 3.x, rebuild namespace cache.
$namespaces = [
'modNamespace',
'\modNamespace',
'MODX\Revolution\modNamespace',
'\MODX\Revolution\modNamespace',
];
if (class_exists('\MODX\Revolution\modNamespace') && in_array($type['class'], $namespaces, true)) {
$this->modx->getCacheManager()->generateNamespacesCache('namespaces');
\MODX\Revolution\modNamespace::loadCache($this->modx);
}
}
/**
* Creates or updates a single xPDOObject.
*
* @param $data
* @param $type
* @param bool $isConflictResolution
*/
public function buildSingleObject($data, $type, $isConflictResolution = false) {
$this->modx->setOption(\xPDO::OPT_SETUP, true);
$primaryKey = !empty($type['primary']) ? $type['primary'] : 'id';
$class = $type['class'];
$primary = $this->_getPrimaryKey($class, $primaryKey, $data);
$showPrimary = (is_array($primary)) ? json_encode($primary) : $primary;
if (!$isConflictResolution && $this->hasConflict($class, $primaryKey, $primary, $data)) {
return;
}
$new = false;
/** @var \xPDOObject|bool $object */
if (!is_array($primaryKey)) {
$primary = array($primaryKey => $primary);
}
$object = ($this->isForce) ? false : $this->modx->getObject($class, $primary);
if (!($object instanceof \xPDOObject)) {
$object = $this->modx->newObject($class);
$new = true;
}
if ($object instanceof \modElement && !($object instanceof \modCategory)) {
// Handle string-based category names automagically
if (isset($data['category']) && !empty($data['category']) && !is_numeric($data['category'])) {
$catName = $data['category'];
$data['category'] = $this->getCategoryId($catName);
}
// Make sure default value is applied to empty tinyint fields
foreach ($object->_fieldMeta as $key => $value) {
if ($value['dbtype'] == 'tinyint') {
$excludes = (isset($type['exclude_keys']) && is_array($type['exclude_keys'])) ? $type['exclude_keys'] : array();
// Skip fields that are explicitly defined OR excluded in YAML config
if (isset($data[$key]) || in_array($key, $excludes)) continue;
// Reset fields with a value other than the default
if (isset($value['default']) && $object->get($key) != $value['default']) {
if ($this->output->isVerbose()) {
$this->output->writeln("- Resetting <comment>$key</comment> field to default value for <comment>" . $object->get('name') . "</comment> object.");
}
$data[$key] = $value['default'];
}
}
}
}
$object->fromArray($data, '', true, true);
$prefix = $isConflictResolution ? ' \ ' : '- ';
if ($object->save()) {
if ($this->output->isVerbose()) {
$new = ($new) ? 'Created new' : 'Updated';
$this->output->writeln("{$prefix}{$new} {$class}: {$showPrimary}");
}
$pk = $object->getPrimaryKey();
if (is_array($pk)) {
$pk = json_encode($pk);
}
if (isset($this->orphanedObjects[$pk])) {
unset($this->orphanedObjects[$pk]);
}
}
else {
$new = ($new) ? 'new' : 'updated';
$this->output->writeln("{$prefix}<error>Could not save {$new} {$class}: {$showPrimary}</error>");
}
}
/**
* Grabs a category ID (or creates one!) for a category name
*
* @param $name
* @return int
*/
public function getCategoryId($name)
{
// Hashing it as md5 to make sure invalid characters don't mess with our array
if (isset($this->categories[md5($name)])) {
return $this->categories[md5($name)];
}
$category = $this->modx->getObject('modCategory', array('category' => $name));
if (!$category) {
$category = $this->modx->newObject('modCategory');
$category->fromArray(array(
'category' => $name,
));
if (!$category->save()) {
return 0;
}
}
if ($category) {
$this->categories[md5($name)] = $category->get('id');
return $this->categories[md5($name)];
}
return 0;
}
/**
* Looks at the field meta to find the default value.
*
* @param string $class The xPDOObject to grab adefault value for
* @param string $field The field in the xPDOObject to grab a default value for
* @return null
*/
protected function _getDefaultForField($class, $field)
{
if (!isset($this->_metaCache[$class])) {
$this->_metaCache[$class] = $this->modx->getFieldMeta($class);
}
if (isset($this->_metaCache[$class][$field]) && isset($this->_metaCache[$class][$field]['default'])) {
return $this->_metaCache[$class][$field]['default'];
}
return null;
}
/**
* Returns an array of all current objects in the database, per key => array
*
* @param $class
* @param array $criteria
* @return array
*/
public function getExistingObjects($class, $criteria = array())
{
$this->existingObjects = array();
$this->orphanedObjects = array();
if (!$this->isForce) {
$iterator = $this->modx->getIterator($class, $criteria);
foreach ($iterator as $object) {
/** @var \xPDOObject $object */
$key = $pk = $object->getPrimaryKey();
if (is_array($key)) {
$key = implode('--', $key);
$pk = json_encode($pk);
}
$this->existingObjects[$key] = $object->toArray();
$this->orphanedObjects[$pk] = 1;
}
}
}
public function resetConflicts()
{
$this->updatedObjects = array();
$this->conflictingObjects = array();
}
/**
* @param $folder
* @param $type
* @param bool $isResource
* @throws \Exception
*/
public function resolveConflicts($folder, $type, $isResource = false)
{
if (!empty($this->conflictingObjects)) {
$runExtract = false;
foreach ($this->conflictingObjects as $conflict) {
$showOriginalPrimary = is_array($conflict['existing_object_primary']) ? json_encode($conflict['existing_object_primary']) : $conflict['existing_object_primary'];
$this->output->writeln('- Attempting to resolve ID Conflict for <comment>' . $showOriginalPrimary . '</comment> with <comment>' . count($conflict['conflicts']) . '</comment> duplicate(s).');
// Get the original/master copy of this conflict
$getPrimary = $conflict['existing_object_primary'];
if (!is_array($getPrimary)) {
$getPrimary = array('id' => $getPrimary);
}
$original = $this->modx->getObject($type['class'], $getPrimary);
if ($original instanceof \xPDOObject) {
// Get the primary key definition of the class
$objectPrimaryKey = $original->getPK();
foreach ($conflict['conflicts'] as $dupe) {
$resolved = false;
$duplicateObject = $dupe['data'];
if (is_string($objectPrimaryKey) && $objectPrimaryKey === 'id') {
unset($duplicateObject[$objectPrimaryKey]);
$this->output->writeln(" \\ <comment>Duplicate #{$dupe['idx']}</comment>: resolving <comment>primary key conflict</comment> by building object with new auto incremented primary key. ");
if ($isResource) {
$this->buildSingleResource($duplicateObject, true);
}
else {
$this->buildSingleObject($duplicateObject, $type, true);
}
$resolved = $runExtract = true;
}
if (!$resolved) {
$this->output->writeln(" \ <error>Unable to resolve ID conflict.</error> The ID conflict will need to be solved manually. ");
}
}
}
else {
$this->output->writeln(" \ <comment>Can't load original {$type['class']} with primary {$showOriginalPrimary}</comment> assuming conflict was due to an orphaned object.");
$conflict = reset($conflict['conflicts']);
$duplicateObject = $conflict['data'];
if ($isResource) {
$this->buildSingleResource($duplicateObject, true);
}
else {
$this->buildSingleObject($duplicateObject, $type, true);
}
}
}
if ($runExtract) {
$this->output->writeln('- Re-extracting ' . basename($folder) . '; you will need to commit the changes manually.');
$command = $this->getApplication()->find('extract');
$inputArray = array(
'command' => 'extract',
'partitions' => array(basename($folder)),
);
$input = new ArrayInput($inputArray);
$output = new BufferedOutput();
$command->run($input, $output);
$cmdOutput = $output->fetch();
$cmdOutput = explode("\n", $cmdOutput);
$cmdOutput = array_map(function ($n) {
return ' \ ' . $n;
}, $cmdOutput);
$cmdOutput = implode("\n", $cmdOutput);
$this->output->write($cmdOutput);
}
}
}
/**
* @param $class
* @param $primaryKey
* @param $data
* @return array
*/
protected function _getPrimaryKey($class, $primaryKey, $data)
{
if (is_array($primaryKey)) {
$primary = array();
foreach ($primaryKey as $pkVal) {
$primary[$pkVal] = isset($data[$pkVal]) ? $data[$pkVal] : $this->_getDefaultForField($class, $pkVal);
}
} else {
$primary = $data[$primaryKey];
}
return $primary;
}
/**
* @param $data
* @param $internalPrimary
* @param $primary
*/
protected function registerConflict($data, $internalPrimary, $primary)
{
if (!isset($this->conflictingObjects[$internalPrimary])) {
$this->conflictingObjects[$internalPrimary] = array(
'existing_object_primary' => $primary,
'conflicts' => array(),
);
}
$this->conflictingObjects[$internalPrimary]['conflicts'][] = array(
'idx' => count($this->conflictingObjects[$internalPrimary]['conflicts']) + 1,
'data' => $data,
);
}
/**
* Checks against conflicts in the source and database. Returns true if there was a conflict, false if all's good.
*
* @param $class
* @param $primaryKey
* @param $primary
* @param $data
* @return bool
*/
public function hasConflict($class, $primaryKey, $primary, $data)
{
$showPrimary = (is_array($primary)) ? json_encode($primary) : $primary;
// Get the primary to match for ID conflict resolution
$classPrimary = $this->modx->getPK($class);
if (is_array($classPrimary)) {
$internalPrimary = array();
foreach ($classPrimary as $classPrimaryField) {
$fieldMeta = $this->modx->getFieldMeta($class);
$default = isset($fieldMeta[$classPrimaryField]['default']) ? $fieldMeta[$classPrimaryField]['default'] : null;
$internalPrimary[$classPrimaryField] = (isset($data[$classPrimaryField])) ? $data[$classPrimaryField] : $default;
}
$internalPrimary = implode('--', $internalPrimary);
} else {
$internalPrimary = $data[$classPrimary];
}
$prefix = ($class === 'modResource') ? ' \ ' : '- ';
// First check - have we came across an object with this primary key before?
if (isset($this->updatedObjects[$internalPrimary])) {
$this->registerConflict($data, $internalPrimary, $primary);
$this->output->writeln("$prefix<comment>Primary Key Duplicate found: duplicate {$class} with primary {$showPrimary}</comment>");
return true;
}
// Second check - see if the object already exists in the database with a different real primary keys
elseif (isset($this->existingObjects[$internalPrimary])) {
$existingObjPrimary = $this->_getPrimaryKey($class, $primaryKey, $this->existingObjects[$internalPrimary]);
if ($primary !== $existingObjPrimary) {
$this->registerConflict($data, $internalPrimary, $existingObjPrimary);
$showExistingObjPrimary = (is_array($existingObjPrimary)) ? json_encode($existingObjPrimary) : $existingObjPrimary;
$this->output->writeln("{$prefix}<comment>Primary Key Conflict found: {$class} {$showPrimary} has the same primary key as {$showExistingObjPrimary}</comment>");
return true;
}
}
$this->updatedObjects[$internalPrimary] = $primary;
return false;
}
/**
* @param $type
* @param bool $primary
*/
public function removeOrphans($type, $primary = false)
{
if ($this->input->getOption('no-cleanup')) {
$orphans = count($this->orphanedObjects);
if ($orphans > 0) {
$this->output->writeln("- Found <comment>{$orphans} orphaned {$type['class']}</comment> object(s), but the <comment>--no-cleanup</comment> flag was specified.");
}
return;
}
if (!$primary) {
$primary = $type['primary'];
}
foreach ($this->orphanedObjects as $pk => $val) {
$getPrimary = (json_decode($pk)) ? json_decode($pk) : $pk;
$obj = $this->modx->getObject($type['class'], $getPrimary);
if ($obj instanceof \xPDOObject) {
$showPrimary = $this->_getPrimaryKey($type['class'], $primary, $obj->toArray());
$showPrimary = (is_array($showPrimary)) ? json_encode($showPrimary) : $showPrimary;
if ($obj->remove()) {
$this->output->writeln("- <info>Removed orphaned {$type['class']} with primary {$showPrimary}</info>");
} else {
$this->output->writeln("- <comment>Could not remove orphaned {$type['class']} with primary {$showPrimary}</comment>");
}
}
}
}
}