1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules; |
4
|
|
|
|
5
|
|
|
use DirectoryIterator; |
6
|
|
|
use GeminiLabs\SiteReviews\Database; |
7
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
8
|
|
|
use GeminiLabs\SiteReviews\Helper; |
9
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
10
|
|
|
|
11
|
|
|
class Migrate |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
public $currentVersion; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string[] |
20
|
|
|
*/ |
21
|
|
|
public $migrations; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
public $migrationsKey; |
27
|
|
|
|
28
|
7 |
|
public function __construct() |
29
|
|
|
{ |
30
|
7 |
|
$this->currentVersion = $this->currentVersion(); |
31
|
7 |
|
$this->migrations = $this->availableMigrations(); |
32
|
7 |
|
$this->migrationsKey = glsr()->prefix.'migrations'; |
33
|
7 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
public function isMigrationNeeded() |
39
|
|
|
{ |
40
|
|
|
if (empty($this->migrations)) { |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
if (!empty($this->pendingMigrations())) { |
44
|
|
|
// check if this is a fresh install of the plugin |
45
|
|
|
return glsr(OptionManager::class)->get('version_upgraded_from') !== '0.0.0'; |
46
|
|
|
} |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
7 |
|
public function reset() |
54
|
|
|
{ |
55
|
7 |
|
delete_option($this->migrationsKey); |
56
|
7 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function run() |
62
|
|
|
{ |
63
|
|
|
if (glsr(Database::class)->isMigrationNeeded()) { |
64
|
|
|
$this->runAll(); |
65
|
|
|
} else { |
66
|
|
|
$this->runMigrations(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
7 |
|
public function runAll() |
74
|
|
|
{ |
75
|
7 |
|
$this->reset(); |
76
|
7 |
|
$this->runMigrations(); |
77
|
7 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
7 |
|
protected function availableMigrations() |
83
|
|
|
{ |
84
|
7 |
|
$migrations = []; |
85
|
7 |
|
$dir = glsr()->path('plugin/Modules/Migrations'); |
86
|
7 |
|
if (is_dir($dir)) { |
87
|
7 |
|
$iterator = new DirectoryIterator($dir); |
88
|
7 |
|
foreach ($iterator as $fileinfo) { |
89
|
7 |
|
if ('file' === $fileinfo->getType()) { |
90
|
7 |
|
$migrations[] = str_replace('.php', '', $fileinfo->getFilename()); |
91
|
|
|
} |
92
|
|
|
} |
93
|
7 |
|
natsort($migrations); |
94
|
|
|
} |
95
|
7 |
|
return Arr::reindex($migrations); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
7 |
|
protected function createMigrations() |
102
|
|
|
{ |
103
|
7 |
|
$migrations = []; |
104
|
7 |
|
foreach ($this->migrations as $migration) { |
105
|
7 |
|
$migrations[$migration] = false; |
106
|
|
|
} |
107
|
7 |
|
return $migrations; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
7 |
|
protected function currentVersion() |
114
|
|
|
{ |
115
|
7 |
|
$fallback = '0.0.0'; |
116
|
7 |
|
$majorVersions = range(glsr()->version('major'), 1); |
117
|
7 |
|
foreach ($majorVersions as $majorVersion) { |
118
|
7 |
|
$settings = get_option(OptionManager::databaseKey($majorVersion)); |
119
|
7 |
|
$version = Arr::get($settings, 'version', $fallback); |
120
|
7 |
|
if (Helper::isGreaterThan($version, $fallback)) { |
121
|
|
|
return $version; |
122
|
|
|
} |
123
|
|
|
} |
124
|
7 |
|
return $fallback; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return string[] |
129
|
|
|
*/ |
130
|
7 |
|
protected function pendingMigrations(array $migrations = []) |
131
|
|
|
{ |
132
|
7 |
|
if (empty($migrations)) { |
133
|
|
|
$migrations = $this->storedMigrations(); |
134
|
|
|
} |
135
|
|
|
return array_keys(array_filter($migrations, function ($hasRun) { |
136
|
7 |
|
return !$hasRun; |
137
|
7 |
|
})); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
7 |
|
protected function runMigrations() |
144
|
|
|
{ |
145
|
7 |
|
wp_raise_memory_limit('admin'); |
146
|
7 |
|
$migrations = $this->storedMigrations(); |
147
|
7 |
|
glsr()->action('migration/start', $migrations); |
148
|
7 |
|
foreach ($this->pendingMigrations($migrations) as $migration) { |
149
|
7 |
|
if (class_exists($classname = __NAMESPACE__.'\Migrations\\'.$migration)) { |
150
|
7 |
|
if (glsr($classname)->run()) { |
151
|
7 |
|
$migrations[$migration] = true; |
152
|
7 |
|
glsr_log()->debug("[$migration] has run successfully"); |
153
|
7 |
|
continue; |
154
|
|
|
} |
155
|
|
|
glsr_log()->error("[$migration] was unsuccessful"); |
156
|
|
|
} |
157
|
|
|
} |
158
|
7 |
|
$this->storeMigrations($migrations); |
159
|
7 |
|
if ($this->currentVersion !== glsr()->version) { |
160
|
7 |
|
$this->updateVersionFrom($this->currentVersion); |
161
|
|
|
} |
162
|
7 |
|
glsr(OptionManager::class)->set('last_migration_run', current_time('timestamp')); |
163
|
7 |
|
glsr()->action('migration/end', $migrations); |
164
|
7 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
7 |
|
protected function storeMigrations(array $migrations) |
170
|
|
|
{ |
171
|
7 |
|
update_option($this->migrationsKey, $migrations); |
172
|
7 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return array |
176
|
|
|
*/ |
177
|
7 |
|
protected function storedMigrations() |
178
|
|
|
{ |
179
|
7 |
|
$migrations = Arr::consolidate(get_option($this->migrationsKey)); |
180
|
7 |
|
if (!Arr::compare(array_keys($migrations), array_values($this->migrations))) { |
181
|
7 |
|
$newMigrations = $this->createMigrations(); |
182
|
7 |
|
foreach ($newMigrations as $migration => &$hasRun) { |
183
|
7 |
|
$hasRun = Arr::get($migrations, $migration, false); |
184
|
|
|
} |
185
|
7 |
|
$migrations = $newMigrations; |
186
|
7 |
|
$this->storeMigrations($migrations); |
187
|
|
|
} |
188
|
7 |
|
return array_map('wp_validate_boolean', $migrations); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param string $previousVersion |
193
|
|
|
* @return void |
194
|
|
|
*/ |
195
|
7 |
|
protected function updateVersionFrom($previousVersion) |
196
|
|
|
{ |
197
|
7 |
|
glsr(OptionManager::class)->set('version', glsr()->version); |
198
|
7 |
|
glsr(OptionManager::class)->set('version_upgraded_from', $previousVersion); |
199
|
7 |
|
} |
200
|
|
|
} |
201
|
|
|
|