PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.7.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.7.0
4.11.2 4.11.1 4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Staging / Tasks / DatabaseAdjustmentTask.php
wp-staging / Staging / Tasks Last commit date
StagingSite 6 months ago StagingSiteCreate 7 months ago StagingSiteDelete 1 year ago StagingSiteReset 7 months ago StagingSiteUpdate 7 months ago DataAdjustmentTask.php 1 year ago DatabaseAdjustmentTask.php 1 year ago FileAdjustmentTask.php 1 year ago FileCopierTask.php 6 months ago StagingTask.php 1 year ago
DatabaseAdjustmentTask.php
182 lines
1 <?php
2
3 namespace WPStaging\Staging\Tasks;
4
5 use WPStaging\Framework\Adapter\Database;
6 use WPStaging\Framework\Database\TableService;
7 use WPStaging\Framework\Queue\SeekableQueueInterface;
8 use WPStaging\Framework\Job\Dto\StepsDto;
9 use WPStaging\Framework\Utils\Cache\Cache;
10 use WPStaging\Framework\Utils\Urls;
11 use WPStaging\Staging\Traits\WithStagingDatabase;
12 use WPStaging\Vendor\Psr\Log\LoggerInterface;
13
14 abstract class DatabaseAdjustmentTask extends DataAdjustmentTask
15 {
16 use WithStagingDatabase;
17
18 /**
19 * @var ?Database
20 */
21 protected $database = null;
22
23 /**
24 * @var ?\wpdb
25 */
26 protected $wpdb = null;
27
28 /**
29 * @param LoggerInterface $logger
30 * @param Cache $cache
31 * @param StepsDto $stepsDto
32 * @param SeekableQueueInterface $taskQueue
33 * @param Urls $urls
34 * @param Database $database
35 */
36 public function __construct(LoggerInterface $logger, Cache $cache, StepsDto $stepsDto, SeekableQueueInterface $taskQueue, Urls $urls, Database $database)
37 {
38 parent::__construct($logger, $cache, $stepsDto, $taskQueue, $urls);
39 $this->database = $database;
40 }
41
42 /**
43 * @return void
44 */
45 public function setup()
46 {
47 $this->initStagingDatabase($this->getStagingSiteDto($this->jobDataDto->getCloneId()));
48 if ($this->tableService === null) {
49 $this->tableService = new TableService($this->stagingDb);
50 }
51
52 if ($this->wpdb === null) {
53 $this->wpdb = $this->stagingDb->getWpdb();
54 }
55 }
56
57 /**
58 * Check if the table exists in the staging database.
59 * @param string $tableName
60 * @return bool
61 */
62 protected function isTableExists(string $tableName): bool
63 {
64 return $this->tableService->tableExists($tableName);
65 }
66
67 /**
68 * Check if the table excluded.
69 * @param string $tableNameWithoutPrefix
70 * @return bool
71 */
72 protected function isTableExcluded(string $tableNameWithoutPrefix): bool
73 {
74 $tableName = $this->getPrefixedStagingTableName($tableNameWithoutPrefix);
75
76 if (!$this->isTableExists($tableName)) {
77 return true;
78 }
79
80 if (in_array($tableNameWithoutPrefix, $this->jobDataDto->getExcludedTables())) {
81 return true;
82 }
83
84 return false;
85 }
86
87 protected function getPrefixedStagingTableName(string $tableName): string
88 {
89 return $this->jobDataDto->getDatabasePrefix() . $tableName;
90 }
91
92 protected function isOptionsTableExcluded(): bool
93 {
94 $optionsTable = $this->getOptionsTableName();
95 if ($this->isTableExcluded($optionsTable)) {
96 return true;
97 }
98
99 return false;
100 }
101
102 /**
103 * @param string $optionName
104 * @param string|null $optionValue
105 * @param bool $autoload
106 * @return bool
107 */
108 protected function insertOption(string $optionName, $optionValue, bool $autoload = false): bool
109 {
110 // Let delete the option regardless it exists or not
111 $this->deleteOption($optionName);
112
113 $optionTable = $this->getOptionsTableName();
114 return $this->executeQuery(
115 "INSERT INTO `{$optionTable}` (option_name, option_value, autoload) VALUES (%s, %s, %s)",
116 $optionName,
117 $optionValue,
118 $autoload ? 'on' : 'off'
119 );
120 }
121
122 /**
123 * @param string $optionName
124 * @param string $optionValue
125 * @return bool
126 */
127 protected function updateOption(string $optionName, string $optionValue): bool
128 {
129 $optionTable = $this->getOptionsTableName();
130 return $this->executeQuery(
131 "UPDATE `{$optionTable}` SET `option_value` = %s WHERE `option_name` = %s;",
132 $optionValue,
133 $optionName
134 );
135 }
136
137 /**
138 * @param string $optionName
139 * @return bool
140 */
141 protected function deleteOption(string $optionName): bool
142 {
143 $optionTable = $this->getOptionsTableName();
144 return $this->executeQuery(
145 "DELETE FROM `{$optionTable}` WHERE `option_name` = %s;",
146 $optionName
147 );
148 }
149
150 protected function getOptionsTableName(): string
151 {
152 return $this->getPrefixedStagingTableName('options');
153 }
154
155 /**
156 * @param string $query
157 * @param array $parameters
158 * @return bool
159 */
160 protected function executeQuery(string $query, ...$parameters): bool
161 {
162 $result = $this->wpdb->query(
163 $this->wpdb->prepare(
164 $query,
165 $parameters
166 )
167 );
168
169 if ($result === false) {
170 $this->logger->debug("Database adjustment failed. Query: {$query}.");
171 return false;
172 }
173
174 return true;
175 }
176
177 protected function lastError(): string
178 {
179 return $this->wpdb->last_error;
180 }
181 }
182