StagingSite
1 week ago
StagingSiteCreate
2 weeks ago
StagingSiteDelete
10 months ago
StagingSiteReset
2 weeks ago
StagingSiteUpdate
2 weeks ago
DataAdjustmentTask.php
11 months ago
DatabaseAdjustmentTask.php
2 weeks ago
FileAdjustmentTask.php
11 months ago
FileCopierTask.php
2 weeks ago
StagingTask.php
1 year ago
DatabaseAdjustmentTask.php
181 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 | if ($this->isTableExcluded('options')) { |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @param string $optionName |
| 103 | * @param string|null $optionValue |
| 104 | * @param bool $autoload |
| 105 | * @return bool |
| 106 | */ |
| 107 | protected function insertOption(string $optionName, $optionValue, bool $autoload = false): bool |
| 108 | { |
| 109 | // Let delete the option regardless it exists or not |
| 110 | $this->deleteOption($optionName); |
| 111 | |
| 112 | $optionTable = $this->getOptionsTableName(); |
| 113 | return $this->executeQuery( |
| 114 | "INSERT INTO `{$optionTable}` (option_name, option_value, autoload) VALUES (%s, %s, %s)", |
| 115 | $optionName, |
| 116 | $optionValue, |
| 117 | $autoload ? 'on' : 'off' |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param string $optionName |
| 123 | * @param string $optionValue |
| 124 | * @return bool |
| 125 | */ |
| 126 | protected function updateOption(string $optionName, string $optionValue): bool |
| 127 | { |
| 128 | $optionTable = $this->getOptionsTableName(); |
| 129 | return $this->executeQuery( |
| 130 | "UPDATE `{$optionTable}` SET `option_value` = %s WHERE `option_name` = %s;", |
| 131 | $optionValue, |
| 132 | $optionName |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param string $optionName |
| 138 | * @return bool |
| 139 | */ |
| 140 | protected function deleteOption(string $optionName): bool |
| 141 | { |
| 142 | $optionTable = $this->getOptionsTableName(); |
| 143 | return $this->executeQuery( |
| 144 | "DELETE FROM `{$optionTable}` WHERE `option_name` = %s;", |
| 145 | $optionName |
| 146 | ); |
| 147 | } |
| 148 | |
| 149 | protected function getOptionsTableName(): string |
| 150 | { |
| 151 | return $this->getPrefixedStagingTableName('options'); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @param string $query |
| 156 | * @param array $parameters |
| 157 | * @return bool |
| 158 | */ |
| 159 | protected function executeQuery(string $query, ...$parameters): bool |
| 160 | { |
| 161 | $result = $this->wpdb->query( |
| 162 | $this->wpdb->prepare( |
| 163 | $query, |
| 164 | $parameters |
| 165 | ) |
| 166 | ); |
| 167 | |
| 168 | if ($result === false) { |
| 169 | $this->logger->debug("Database adjustment failed. Query: {$query}."); |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | protected function lastError(): string |
| 177 | { |
| 178 | return $this->wpdb->last_error; |
| 179 | } |
| 180 | } |
| 181 |