JobBackup
1 year ago
JobRestore
1 year ago
CleanupBakTablesTask.php
2 years ago
CleanupTmpFilesTask.php
2 years ago
CleanupTmpTablesTask.php
2 years ago
CleanupTmpTablesTask.php
161 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup\Task\Tasks; |
| 4 | |
| 5 | use WPStaging\Backup\Dto\TaskResponseDto; |
| 6 | use WPStaging\Framework\Database\TableService; |
| 7 | use WPStaging\Framework\Queue\SeekableQueueInterface; |
| 8 | use WPStaging\Backup\Ajax\Restore\PrepareRestore; |
| 9 | use WPStaging\Backup\Dto\StepsDto; |
| 10 | use WPStaging\Backup\Task\AbstractTask; |
| 11 | use WPStaging\Vendor\Psr\Log\LoggerInterface; |
| 12 | use WPStaging\Framework\Utils\Cache\Cache; |
| 13 | |
| 14 | class CleanupTmpTablesTask extends AbstractTask |
| 15 | { |
| 16 | private $tableService; |
| 17 | |
| 18 | /** @var array An array with the name of all existing tables. */ |
| 19 | protected $tables = []; |
| 20 | |
| 21 | /** @var array An array with the name of all existing views. */ |
| 22 | protected $views = []; |
| 23 | |
| 24 | public function __construct(LoggerInterface $logger, Cache $cache, StepsDto $stepsDto, TableService $tableService, SeekableQueueInterface $taskQueue) |
| 25 | { |
| 26 | parent::__construct($logger, $cache, $stepsDto, $taskQueue); |
| 27 | $this->tableService = $tableService; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Can be either wpstgtmp_ or wpstgbak_ |
| 32 | * |
| 33 | * @return string |
| 34 | */ |
| 35 | public static function getTempTableType(): string |
| 36 | { |
| 37 | return PrepareRestore::TMP_DATABASE_PREFIX; |
| 38 | } |
| 39 | |
| 40 | public static function getTaskName(): string |
| 41 | { |
| 42 | $cleaningType = static::getTempTableType(); |
| 43 | return "backup_restore_cleanup_{$cleaningType}tables"; |
| 44 | } |
| 45 | |
| 46 | public static function getTaskTitle(): string |
| 47 | { |
| 48 | return 'Cleaning Up Restore Tables'; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @return TaskResponseDto |
| 53 | */ |
| 54 | public function execute(): TaskResponseDto |
| 55 | { |
| 56 | $tmpTableType = static::getTempTableType(); |
| 57 | $this->prepareCleanupRestoreTask($tmpTableType); |
| 58 | |
| 59 | $this->tables = $this->tableService->findTableNamesStartWith(); |
| 60 | $this->views = $this->tableService->findViewsNamesStartWith(); |
| 61 | |
| 62 | while (!$this->isThreshold() && !$this->stepsDto->isFinished()) { |
| 63 | $tableOrViewName = $this->taskQueue->dequeue(); |
| 64 | |
| 65 | // Double-check we are deleting a temporary table just to be extra-careful. |
| 66 | if (strpos($tableOrViewName, $tmpTableType) !== 0) { |
| 67 | $this->logger->warning(sprintf( |
| 68 | __('%s: Temporary table "%s" did not start with temporary prefix "%s" and was skipped.', 'wp-staging'), |
| 69 | static::getTaskTitle(), |
| 70 | $tableOrViewName, |
| 71 | $tmpTableType |
| 72 | )); |
| 73 | |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | $label = 'table'; |
| 78 | |
| 79 | if (in_array($tableOrViewName, $this->views)) { |
| 80 | $label = 'view'; |
| 81 | $deleted = $this->tableService->deleteViews([$tableOrViewName]); |
| 82 | } elseif (in_array($tableOrViewName, $this->tables)) { |
| 83 | $deleted = $this->tableService->deleteTables([$tableOrViewName]); |
| 84 | } else { |
| 85 | $deleted = false; |
| 86 | } |
| 87 | |
| 88 | $this->stepsDto->incrementCurrentStep(); |
| 89 | |
| 90 | if ($deleted) { |
| 91 | $this->logger->debug(sprintf( |
| 92 | __('%s: Deleted temporary %s "%s".', 'wp-staging'), |
| 93 | static::getTaskTitle(), |
| 94 | $label, |
| 95 | $tableOrViewName |
| 96 | )); |
| 97 | } else { |
| 98 | $this->logger->warning(sprintf( |
| 99 | __('%s: Temporary %s "%s" was not successfully cleaned up.', 'wp-staging'), |
| 100 | static::getTaskTitle(), |
| 101 | $label, |
| 102 | $tableOrViewName |
| 103 | )); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if ($this->taskQueue->isFinished()) { |
| 108 | $this->stepsDto->finish(); |
| 109 | |
| 110 | // Successfully deleted |
| 111 | $this->logger->info(sprintf( |
| 112 | __('%s: Tables with temporary prefix "%s" successfully cleaned up.', 'wp-staging'), |
| 113 | static::getTaskTitle(), |
| 114 | $tmpTableType |
| 115 | )); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | return $this->generateResponse(false); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @param string $tmpTableType |
| 124 | */ |
| 125 | public function prepareCleanupRestoreTask(string $tmpTableType) |
| 126 | { |
| 127 | // Early bail: Already prepared |
| 128 | if ($this->stepsDto->getTotal() > 0) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | global $wpdb; |
| 133 | |
| 134 | if ($wpdb->prefix === $tmpTableType) { |
| 135 | $this->logger->warning(sprintf( |
| 136 | __('%s: Temporary table prefix "%s" is the same as the WordPress table prefix. This is not allowed.', 'wp-staging'), |
| 137 | static::getTaskTitle(), |
| 138 | $tmpTableType |
| 139 | )); |
| 140 | |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | $tmpViews = $this->tableService->findViewsNamesStartWith($tmpTableType); |
| 145 | $tmpTables = $this->tableService->findTableNamesStartWith($tmpTableType); |
| 146 | |
| 147 | $toDelete = array_merge($tmpViews, $tmpTables); |
| 148 | |
| 149 | $count = 0; |
| 150 | |
| 151 | foreach ($toDelete as $tableOrViewName) { |
| 152 | $count++; |
| 153 | $this->taskQueue->enqueue($tableOrViewName); |
| 154 | } |
| 155 | |
| 156 | $this->taskQueue->seek(0); |
| 157 | |
| 158 | $this->stepsDto->setTotal($count); |
| 159 | } |
| 160 | } |
| 161 |