1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Data Migrations class |
5
|
|
|
* |
6
|
|
|
* Migration from file, to file, from web, to web |
7
|
|
|
* |
8
|
|
|
* @author Alexey Krupskiy <[email protected]> |
9
|
|
|
* @link http://inji.ru/ |
10
|
|
|
* @copyright 2015 Alexey Krupskiy |
11
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
//define('mdebug',true); |
|
|
|
|
15
|
|
|
class Migrations extends \Module { |
16
|
|
|
|
17
|
|
|
public $ids = []; |
18
|
|
|
public $migrationObjects = []; |
19
|
|
|
|
20
|
|
|
public function startMigration($migrationId, $mapId, $filePath) { |
21
|
|
|
$log = new \Migrations\Log(); |
22
|
|
|
$log->migration_id = $migrationId; |
23
|
|
|
$log->migration_map_id = $mapId; |
24
|
|
|
$log->source = $filePath; |
25
|
|
|
$log->save(); |
26
|
|
|
|
27
|
|
|
$reader = new Migrations\Reader\Xml(); |
28
|
|
|
if (!$reader->loadData($filePath)) { |
29
|
|
|
$event = new Migrations\Log\Event(); |
30
|
|
|
$event->log_id = $log->id; |
31
|
|
|
$event->type = 'load_data_error'; |
32
|
|
|
$event->save(); |
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
$walker = new \Migrations\Walker(); |
36
|
|
|
$walker->migration = Migrations\Migration::get($migrationId); |
37
|
|
|
$walker->map = Migrations\Migration\Map::get($mapId); |
38
|
|
|
$walker->data = $reader->getArray(); |
39
|
|
|
$walker->migtarionLog = $log; |
40
|
|
|
$walker->walk(); |
41
|
|
|
$log->result = 'success'; |
42
|
|
|
$log->save(); |
43
|
|
|
$this->saveLastAccess(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function loadParseIds($type) { |
47
|
|
|
$this->ids['parseIds'][$type] = \Migrations\Id::getList(['where' => ['type', $type], 'key' => 'parse_id']); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function loadObjectIds($type) { |
51
|
|
|
$this->ids['objectIds'][$type] = \Migrations\Id::getList(['where' => ['type', $type], 'key' => 'object_id']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function findObject($parseId, $type) { |
55
|
|
|
if (empty($this->ids['parseIds'][$type])) { |
56
|
|
|
$this->loadParseIds($type); |
57
|
|
|
ksort($this->ids['parseIds'][$type]); |
58
|
|
|
} |
59
|
|
|
if (!empty($this->ids['parseIds'][$type][$parseId])) { |
60
|
|
|
$this->ids['parseIds'][$type][$parseId]->last_access = Date('Y-m-d H:i:s'); |
61
|
|
|
return $this->ids['parseIds'][$type][$parseId]; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public function findParse($objectId, $type) { |
66
|
|
|
if (empty($this->ids['objectIds'][$type])) { |
67
|
|
|
$this->loadObjectIds($type); |
68
|
|
|
ksort($this->ids['objectIds'][$type]); |
69
|
|
|
} |
70
|
|
|
if (!empty($this->ids['objectIds'][$type][$objectId])) { |
71
|
|
|
$this->ids['objectIds'][$type][$objectId]->last_access = Date('Y-m-d H:i:s'); |
72
|
|
|
return $this->ids['objectIds'][$type][$objectId]; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getMigrationObject($objectId) { |
77
|
|
|
if (empty($this->migrationObjects)) { |
78
|
|
|
$this->migrationObjects = \Migrations\Migration\Object::getList(); |
79
|
|
|
} |
80
|
|
|
if (!empty($this->migrationObjects[$objectId])) { |
81
|
|
|
return $this->migrationObjects[$objectId]; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function saveLastAccess() { |
86
|
|
|
foreach ($this->ids as $group => $types) { |
87
|
|
|
foreach ($types as $type => $ids) { |
88
|
|
|
foreach ($ids as $key => $id) { |
89
|
|
|
if ($id->_changedParams) { |
90
|
|
|
$id->save(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.