PluginProbe
ManageWP Worker / 4.9.25
ManageWP Worker v4.9.25
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / MWP / Migration / Migration.php

Migration.php in ManageWP Worker 4.9.25, at src/MWP/Migration/Migration.php

174 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * This file is part of the ManageWP Worker plugin.
4 *
5 * (c) ManageWP LLC <contact@managewp.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 /**
12 * Base component for ManageWP Worker migrations.
13 * Renamed options and those that changed their format from one plugin version to another should be handled here.
14 */
15 class MWP_Migration_Migration
16 {
17 /**
18 * Upon plugin activation this version is persisted to the database.
19 * It is also checked against on every master request.
20 */
21 const VERSION = 2;
22
23 /**
24 * @var MWP_WordPress_Context
25 */
26 private $context;
27
28 /**
29 * Migration version => migration name (method).
30 * The last migration index MUST be the same as the VERSION constant above.
31 *
32 * @var array
33 */
34 private static $migrations = array(
35 1 => 'migrateBackupFileNames',
36 2 => 'setDefaultOptionValues',
37 );
38
39 public function __construct(MWP_WordPress_Context $context)
40 {
41 $this->context = $context;
42 }
43
44 private function acquireLock($lockName, $ttl = 3600)
45 {
46 $wpdb = $this->context->getDb();
47 $lockRow = $wpdb->get_row("SELECT option_value FROM {$wpdb->prefix}options WHERE option_name = '$lockName'");
48 $currentTimestamp = $this->context->getCurrentTime()->format('U');
49
50 if ($lockRow) {
51 /** @noinspection PhpUndefinedFieldInspection */
52 if ((int) $lockRow->option_value + $ttl > $currentTimestamp) {
53 return false;
54 } else {
55 $this->releaseLock($lockName);
56 }
57 }
58
59 $locked = $wpdb->query("INSERT INTO {$wpdb->prefix}options SET option_name = '$lockName', option_value = '$currentTimestamp'");
60
61 return (bool) $locked;
62 }
63
64 private function releaseLock($lockName)
65 {
66 $wpdb = $this->context->getDb();
67 $released = $wpdb->query("DELETE FROM {$wpdb->prefix}options WHERE option_name = '$lockName'");
68
69 return (bool) $released;
70 }
71
72 public function migrate()
73 {
74 $wpdb = $this->context->getDb();
75 $lockName = 'worker_migration_lock';
76
77 $version = (int) $wpdb->get_var("SELECT option_value FROM {$wpdb->prefix}options WHERE option_name = 'worker_migration_version'");
78 $currentVersion = self::VERSION;
79
80 if ($version >= $currentVersion) {
81 return;
82 }
83
84 if (!$this->acquireLock($lockName)) {
85 return;
86 }
87
88 foreach (self::$migrations as $migrationVersion => $migrationName) {
89 if ($version < $migrationVersion) {
90 $this->$migrationName();
91 $wpdb->query("INSERT INTO {$wpdb->prefix}options SET option_name = 'worker_migration_version', option_value = '$migrationVersion' ON DUPLICATE KEY UPDATE option_value = '$migrationVersion'");
92 }
93 }
94
95 $this->releaseLock($lockName);
96 }
97
98 private function setDefaultOptionValues()
99 {
100 if (!$this->context->optionGet('mwp_recovering')) {
101 $this->context->optionSet('mwp_recovering', '');
102 }
103
104 if (!$this->context->optionGet('mwp_incremental_update_active')) {
105 $this->context->optionSet('mwp_incremental_update_active', '');
106 }
107
108 if (!$this->context->optionGet('mwp_core_autoupdate')) {
109 $this->context->optionSet('mwp_core_autoupdate', '');
110 }
111
112 if (!$this->context->optionGet('mwp_container_parameters')) {
113 $this->context->optionSet('mwp_container_parameters', array());
114 }
115
116 if (!$this->context->optionGet('mwp_container_site_parameters')) {
117 $this->context->optionSet('mwp_container_site_parameters', array());
118 }
119
120 if (!$this->context->optionGet('_worker_nossl_key')) {
121 $this->context->optionSet('_worker_nossl_key', '');
122 }
123
124 if (!$this->context->optionGet('_worker_public_key')) {
125 $this->context->optionSet('_worker_public_key', '');
126 }
127
128 if (!$this->context->optionGet('mwp_maintenace_mode')) {
129 $this->context->optionSet('mwp_maintenace_mode', array());
130 }
131 }
132
133 private function migrateBackupFileNames()
134 {
135 $tasks = (array) $this->context->optionGet('mwp_backup_tasks');
136 $changed = false;
137
138 foreach ($tasks as $taskName => $taskInfo) {
139 if (!isset($taskInfo['task_results']) || !is_array($taskInfo['task_results'])) {
140 continue;
141 }
142 foreach ($taskInfo['task_results'] as $resultId => $taskResult) {
143 if (!isset($taskResult['server']['file_path']) || !file_exists($taskResult['server']['file_path'])) {
144 continue;
145 }
146 $filePath = $taskResult['server']['file_path'];
147 $hash = $this->generateRandomHash(filesize($filePath));
148 $newFilePath = preg_replace('{_[a-z0-9]{32}\.zip$}', sprintf('_%s.zip', $hash), $filePath);
149 $renamed = rename($filePath, $newFilePath);
150 if (!$renamed) {
151 continue;
152 }
153 $changed = true;
154 $oldUrl = $tasks[$taskName]['task_results'][$resultId]['server']['file_url'];
155 $newUrl = preg_replace('{_[a-z0-9]{32}\.zip$}', sprintf('_%s.zip', $hash), $oldUrl);
156
157 $tasks[$taskName]['task_results'][$resultId]['server']['file_path'] = $newFilePath;
158 $tasks[$taskName]['task_results'][$resultId]['server']['file_url'] = $newUrl;
159 }
160 }
161
162 if (!$changed) {
163 return;
164 }
165
166 $this->context->optionSet('mwp_backup_tasks', $tasks);
167 }
168
169 private function generateRandomHash($seed)
170 {
171 return str_shuffle(md5(mt_rand().$seed));
172 }
173 }
174