-
Notifications
You must be signed in to change notification settings - Fork 61
/
class-migrator.php
376 lines (309 loc) · 10.7 KB
/
class-migrator.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
<?php
/**
* Migrations manager
*
* @since 4.0.0
*/
namespace wpCloud\StatelessMedia;
use wpCloud\StatelessMedia\Batch\BatchTaskManager;
class Migrator {
use Singleton;
const MIGRATIONS_KEY = 'sm_migrations';
const MIGRATIONS_NOTIFY_KEY = 'sm_migrations_notify';
const MIGRATIONS_NOTIFY_DISMISSED_KEY = 'dismissed_notice_migrations-finished';
const NOTIFY_REQUIRE = 'require';
const NOTIFY_FINISHED = 'finished';
const STATUS_PENDING = 'pending';
const STATUS_RUNNING = 'running';
const STATUS_PAUSED = 'paused';
const STATUS_SKIPPED = 'skipped';
const STATUS_FINISHED = 'finished';
const STATUS_FAILED = 'failed';
/**
* Path to migrations directory
*
* @var string
*/
private $path;
protected function __construct() {
$this->path = ud_get_stateless_media()->path('static/migrations', 'dir');
$this->_init_hooks();
}
/**
* Initializes the needed hooks
*/
private function _init_hooks() {
add_action( 'init', [$this, 'show_messages'] );
add_action( 'wp_stateless_batch_task_started', [$this, 'migration_started'], 10, 2 );
add_action( 'wp_stateless_batch_task_failed', [$this, 'migration_failed'], 10, 3 );
add_action( 'wp_stateless_batch_task_finished', [$this, 'migration_finished'], 10, 1 );
add_filter( 'wp_stateless_batch_action_start', [$this, 'start_migration'], 10, 2);
add_action( 'wp_stateless_notice_dismissed', [$this, 'notice_dismissed'], 10, 1 );
}
/**
* Get migration ID from file name
*
* @param string $file
* @return string
*/
private function _file_to_id($file) {
return pathinfo($file, PATHINFO_FILENAME);
}
/**
* Get migration ID from class name
*
* @param string $class
* @return string
*/
private function _class_to_id($class) {
return str_replace('Migration_', '', $class);
}
/**
* Get migration class name from ID
*
* @param string $id
* @return string
*/
private function _id_to_class($id) {
return "\Migration_$id";
}
/**
* Get migration file name from ID
*
* @param string $id
* @return string
*/
private function _id_to_file($id) {
return "$this->path/$id.php";
}
/**
* Compares the list of files in the migrations directory with the list of finished migrations
* Finds the oldest migration that has not been run yet
*
* @return array
*/
private function _get_migration_ids() {
if ( !is_dir($this->path) ) {
return [];
}
$ids = [];
$files = scandir($this->path, SCANDIR_SORT_ASCENDING);
foreach ($files as $file) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
if ( $extension !== 'php' ) {
continue;
}
$ids[] = $this->_file_to_id($file);
}
return $ids;
}
/**
* Returns the migration object
*
* @param string $id
* @return wpCloud\StatelessMedia\Batch\Migration
* @throws \Exception
*/
private function _get_object($id) {
$class = $this->_id_to_class($id);
if ( !class_exists($class) ) {
require_once $this->_id_to_file($id);
}
$object = new $class();
if ( !is_a($object, '\wpCloud\StatelessMedia\Batch\Migration') ) {
throw new \Exception("$class is not a valid migration");
}
$object->init_state();
return $object;
}
/**
* Checks if any migrations required and sets or removes global flag
*
* @param array $migrations|null
*/
private function _check_required_migrations($migrations = null) {
if ( empty($migrations) ) {
$migrations = get_option(self::MIGRATIONS_KEY, []);
}
$require_migrations = false;
foreach ($migrations as $id => $migration) {
if ( !in_array( $migration['status'], [self::STATUS_FINISHED, self::STATUS_SKIPPED] ) ) {
$require_migrations = true;
break;
}
}
if ( $require_migrations ) {
update_option(self::MIGRATIONS_NOTIFY_KEY, self::NOTIFY_REQUIRE);
delete_option(self::MIGRATIONS_NOTIFY_DISMISSED_KEY);
} else {
$notify = get_option(self::MIGRATIONS_NOTIFY_KEY, false);
empty($notify) ? delete_option(self::MIGRATIONS_NOTIFY_KEY) : update_option(self::MIGRATIONS_NOTIFY_KEY, self::NOTIFY_FINISHED);
}
}
/**
* Dismisses the migration notice
*
* @param string $option_name
*/
public function notice_dismissed($option_name) {
delete_option(self::MIGRATIONS_NOTIFY_KEY);
}
/**
* Generates an updated list of migrations.
* Checks which migrations should run.
* Sets global options to display the requirement to run migrations.
*
* Is called by Bootstrap object during version upgrade on 'plugins_loaded' hook.
*/
public function migrate() {
// Rebuild the migrations list and state according to the new version
$ids = $this->_get_migration_ids();
$migrations = get_option(self::MIGRATIONS_KEY, []);
$existing = array_keys($migrations);
foreach ($ids as $id) {
if ( in_array($id, $existing) ) {
continue;
}
try {
$object = $this->_get_object($id);
$skip = !$object->should_run();
$migrations[$id] = [
'description' => $object->get_description(),
'started' => '',
'finished' => '',
'status' => $object->should_run() ? self::STATUS_PENDING : self::STATUS_SKIPPED,
'message' => '',
];
} catch (\Throwable $e) {
Helper::log("Unable to initialize migration $id: " . $e->getMessage());
}
}
krsort($migrations);
update_option(self::MIGRATIONS_KEY, $migrations);
// Check if we need to run any migrations
$this->_check_required_migrations($migrations);
}
/**
* Outputs the message that migrations are required
*/
public function show_messages() {
if ( is_network_admin() ) {
return;
}
$is_running = BatchTaskManager::instance()->is_processing() || BatchTaskManager::instance()->is_paused();
$notify = get_option(self::MIGRATIONS_NOTIFY_KEY, false);
if ( $notify ) {
ud_get_stateless_media()->errors->add([
'title' => __('WP-Stateless: Data Optimization Required', ud_get_stateless_media()->domain),
'message' => __('WP-Stateless has been updated! Your WP-Stateless data must now be optimized. <strong>Please backup your database before proceeding with the optimization.</strong>', ud_get_stateless_media()->domain),
'button' => __('Optimize Data', ud_get_stateless_media()->domain),
'button_link' => admin_url('upload.php?page=stateless-settings&tab=stless_status_tab'),
'key' => 'migrations-required',
'dismiss' => false,
'classes' => ($notify == self::NOTIFY_REQUIRE) && !$is_running ? '' : 'hidden',
], 'warning');
ud_get_stateless_media()->errors->add([
'title' => __('WP-Stateless: Data Optimization in Progress', ud_get_stateless_media()->domain),
'message' => __('A background process is optimizing your WP-Stateless data. <strong>Please do not upload, change, or delete your media while this update is underway.</strong>', ud_get_stateless_media()->domain),
'button' => __('View Progress', ud_get_stateless_media()->domain),
'button_link' => admin_url('upload.php?page=stateless-settings&tab=stless_status_tab'),
'key' => 'migrations-running',
'dismiss' => false,
'classes' => $is_running ? '' : 'hidden',
'capability' => 'upload_files',
'button_capability' => 'manage_options',
], 'warning');
ud_get_stateless_media()->errors->add([
'title' => __('WP-Stateless: Data Optimization Complete', ud_get_stateless_media()->domain),
'message' => __('Your WP-Stateless data has been optimized. You can now continue using your media as usual.', ud_get_stateless_media()->domain),
'key' => 'migrations-finished',
'classes' => ($notify == self::NOTIFY_FINISHED) && !$is_running ? '' : 'hidden',
], 'warning');
}
}
/**
* Mark migration as started
*
* @param string $class
* @param string $file
*/
public function migration_started($class, $file) {
$migrations = get_option(self::MIGRATIONS_KEY, []);
$id = $this->_file_to_id($file);
if ( array_key_exists($id, $migrations) ) {
$migrations[$id]['status'] = self::STATUS_RUNNING;
$migrations[$id]['started'] = time();
$migrations[$id]['finished'] = '';
update_option(self::MIGRATIONS_KEY, $migrations);
}
}
/**
* Mark migration as failed and check other migrations
*
* @param string $class
* @param string $file
* @param string $message
*/
public function migration_failed($class, $file, $message) {
$migrations = get_option(self::MIGRATIONS_KEY, []);
$id = $this->_file_to_id($file);
if ( array_key_exists($id, $migrations) ) {
$migrations[$id]['status'] = self::STATUS_FAILED;
$migrations[$id]['message'] = $message;
update_option(self::MIGRATIONS_KEY, $migrations);
$this->_check_required_migrations($migrations);
}
}
/**
* Mark migration as completed and check other migrations
*
* @param string $class
*/
public function migration_finished($class) {
$migrations = get_option(self::MIGRATIONS_KEY, []);
$id = $this->_class_to_id($class);
if ( array_key_exists($id, $migrations) ) {
$migrations[$id]['status'] = self::STATUS_FINISHED;
$migrations[$id]['finished'] = time();
update_option(self::MIGRATIONS_KEY, $migrations);
$this->_check_required_migrations($migrations);
}
}
/**
* Run migration
*
* @param array $state
* @param array $params
* @return array
* @throws \Exception
*/
public function start_migration($state, $params) {
// Possibly not migration action
if ( empty($params['is_migration']) || empty($params['id']) || !$params['is_migration'] ) {
return $state;
}
$id = $params['id'];
$migrations = get_option(self::MIGRATIONS_KEY, []);
// Unknown migration?
if ( !array_key_exists($id, $migrations) ) {
return $state;
}
$class = $this->_id_to_class($id);
$file = $this->_id_to_file($id);
// Still possibly not migration action
if ( !file_exists($file) ) {
return $state;
}
if ( $migrations[$id]['status'] !== self::STATUS_PENDING && !isset($params['force']) ) {
Helper::log("Migration $id is already started or finished. Status: " . $migrations[$id]['status']);
return $state;
}
if ( BatchTaskManager::instance()->is_running() ) {
Helper::log('Another batch task is already running');
return $state;
}
$email = $params['email'] ?? '';
BatchTaskManager::instance()->start_task($class, $file, $email);
return apply_filters('wp_stateless_batch_state', $state, []);
}
}