PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.3.1
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.3.1
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 / Backup / Task / Tasks / CleanupTmpTablesTask.php
wp-staging / Backup / Task / Tasks Last commit date
JobBackup 2 years ago JobRestore 2 years 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