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 $usedIds = []; |
19
|
|
|
public $migrationObjects = []; |
20
|
|
|
|
21
|
|
|
public function startMigration($migrationId, $mapId, $filePath) { |
22
|
|
|
$log = new \Migrations\Log(); |
23
|
|
|
$log->migration_id = $migrationId; |
24
|
|
|
$log->migration_map_id = $mapId; |
25
|
|
|
$log->source = $filePath; |
26
|
|
|
$log->save(); |
27
|
|
|
|
28
|
|
|
$reader = new Migrations\Reader\Xml(); |
29
|
|
|
App::$cur->log->forceView(true); |
30
|
|
|
$logKey = App::$cur->log->start('load xml'); |
31
|
|
|
if (!$reader->loadData($filePath)) { |
32
|
|
|
$event = new Migrations\Log\Event(); |
33
|
|
|
$event->log_id = $log->id; |
34
|
|
|
$event->type = 'load_data_error'; |
35
|
|
|
$event->save(); |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
App::$cur->log->end($logKey); |
39
|
|
|
$walker = new \Migrations\Walker(); |
40
|
|
|
$walker->migration = \Migrations\Migration::get($migrationId); |
|
|
|
|
41
|
|
|
$walker->map = \Migrations\Migration\Map::get($mapId); |
42
|
|
|
$logKey = App::$cur->log->start('parse xml'); |
43
|
|
|
$walker->data = $reader->getArray(); |
44
|
|
|
App::$cur->log->end($logKey); |
45
|
|
|
$walker->migtarionLog = $log; |
46
|
|
|
$walker->walk(); |
47
|
|
|
$log->result = 'success'; |
48
|
|
|
$log->save(); |
49
|
|
|
$this->saveLastAccess(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function loadParseIds($type) { |
53
|
|
|
$this->ids['parseIds'][$type] = \Migrations\Id::getList(['where' => ['type', $type], 'key' => 'parse_id', 'array' => true]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function loadObjectIds($type) { |
57
|
|
|
$this->ids['objectIds'][$type] = \Migrations\Id::getList(['where' => ['type', $type], 'key' => 'object_id', 'array' => true]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
public function findObject($parseId, $type) { |
61
|
|
|
if (empty($this->ids['parseIds'][$type])) { |
62
|
|
|
$this->loadParseIds($type); |
63
|
|
|
ksort($this->ids['parseIds'][$type]); |
64
|
|
|
} |
65
|
|
|
if (!empty($this->ids['parseIds'][$type][$parseId])) { |
66
|
|
|
$this->usedIds['parseIds'][$type][$parseId] = new \Migrations\Id($this->ids['parseIds'][$type][$parseId]); |
67
|
|
|
$this->usedIds['parseIds'][$type][$parseId]->last_access = Date('Y-m-d H:i:s'); |
68
|
|
|
return $this->usedIds['parseIds'][$type][$parseId]; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
public function findParse($objectId, $type) { |
73
|
|
|
if (empty($this->ids['objectIds'][$type])) { |
74
|
|
|
$this->loadObjectIds($type); |
75
|
|
|
ksort($this->ids['objectIds'][$type]); |
76
|
|
|
} |
77
|
|
|
if (!empty($this->ids['objectIds'][$type][$objectId])) { |
78
|
|
|
$this->usedIds['objectIds'][$type][$objectId] = new \Migrations\Id($this->ids['objectIds'][$type][$objectId]); |
79
|
|
|
$this->usedIds['objectIds'][$type][$objectId]->last_access = Date('Y-m-d H:i:s'); |
80
|
|
|
return $this->usedIds['objectIds'][$type][$objectId]; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param \Migrations\Migration $migration |
86
|
|
|
* @param int|string $objectId |
87
|
|
|
* @param null|string $col |
88
|
|
|
* @return null|\Migrations\Migration\Object |
89
|
|
|
*/ |
90
|
|
|
public function getMigrationObject($migration, $objectId, $col = null) { |
91
|
|
|
if ($col === null && isset($migration->objects[$objectId])) { |
92
|
|
|
return $migration->objects[$objectId]; |
93
|
|
|
} |
94
|
|
|
if ($col !== null && isset($migration->objects(['key' => $col])[$objectId])) { |
95
|
|
|
return $migration->objects(['key' => $col])[$objectId]; |
96
|
|
|
} |
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function saveLastAccess() { |
101
|
|
|
foreach ($this->usedIds as $group => $types) { |
102
|
|
|
foreach ($types as $type => $ids) { |
103
|
|
|
foreach ($ids as $key => $id) { |
104
|
|
|
if ($id->_changedParams) { |
105
|
|
|
$id->save(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
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.