PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.1.3
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.1.3
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 / AbstractTask.php
wp-staging / Backup / Task Last commit date
RestoreFileHandlers 2 years ago Tasks 2 years ago AbstractTask.php 2 years ago BackupTask.php 3 years ago FileBackupTask.php 2 years ago FileRestoreTask.php 3 years ago RestoreTask.php 2 years ago
AbstractTask.php
295 lines
1 <?php
2
3 namespace WPStaging\Backup\Task;
4
5 use Exception;
6 use WPStaging\Framework\Exceptions\WPStagingException;
7 use WPStaging\Framework\Queue\SeekableQueueInterface;
8 use WPStaging\Framework\Traits\ResourceTrait;
9 use WPStaging\Backup\Dto\AbstractTaskDto;
10 use WPStaging\Backup\Dto\JobDataDto;
11 use WPStaging\Backup\Dto\StepsDto;
12 use WPStaging\Backup\Dto\TaskResponseDto;
13 use WPStaging\Backup\Job\AbstractJob;
14 use WPStaging\Backup\Task\Tasks\JobBackup\FilesystemScannerTask;
15 use WPStaging\Backup\Task\Tasks\JobRestore\ExtractFilesTask;
16 use WPStaging\Vendor\Psr\Log\LoggerInterface;
17 use WPStaging\Framework\Utils\Cache\Cache;
18 use WPStaging\Core\Utils\Logger;
19 use WPStaging\Core\WPStaging;
20
21 abstract class AbstractTask
22 {
23 use ResourceTrait;
24
25 /** @var Logger */
26 protected $logger;
27
28 /** @var Cache */
29 protected $cache;
30
31 /** @var bool */
32 protected $prepared;
33
34 // TODO RPoC
35 /** @var string|null */
36 protected $jobName;
37
38 /** @var int|null */
39 protected $jobId;
40
41 /** @var bool */
42 protected $debug;
43
44 /** @var StepsDto */
45 protected $stepsDto;
46
47 /** @var JobDataDto */
48 protected $jobDataDto;
49
50 /** @var AbstractJob */
51 protected $job;
52
53 /** @var AbstractTaskDto */
54 protected $currentTaskDto;
55
56 /** @var SeekableQueueInterface */
57 protected $taskQueue;
58
59 public function __construct(LoggerInterface $logger, Cache $cache, StepsDto $stepsDto, SeekableQueueInterface $taskQueue)
60 {
61 /** @var Logger logger */
62 $this->logger = $logger; // @phpstan-ignore-line
63 $this->cache = $cache;
64 $this->stepsDto = $stepsDto;
65 $this->taskQueue = $taskQueue;
66
67 if (method_exists($this, 'init')) {
68 $this->init();
69 }
70 }
71
72 /**
73 * @return TaskResponseDto
74 */
75 abstract public function execute();
76
77 /**
78 * @example 'backup_site_restore_themes'
79 * @return string
80 */
81 public static function getTaskName()
82 {
83 throw new WPStagingException('Any extending class MUST override the getTaskName method.');
84 }
85
86 /**
87 * @example 'Restoring Themes From Backup'
88 * @return string
89 */
90 public static function getTaskTitle()
91 {
92 throw new WPStagingException('Any extending class MUST override the getTaskTitle method.');
93 }
94
95 /**
96 * @param AbstractJob $job
97 * @return void
98 */
99 public function setJobContext(AbstractJob $job)
100 {
101 $this->cache->setLifetime(HOUR_IN_SECONDS);
102 $this->cache->setFilename('task_steps_' . static::getTaskName());
103
104 $this->stepsDto->hydrate($this->cache->get([
105 'current' => 0,
106 'total' => 0,
107 ]));
108
109 $this->job = $job;
110 }
111
112 /**
113 * @param JobDataDto $jobDataDto
114 * @return void
115 */
116 public function setJobDataDto(JobDataDto $jobDataDto)
117 {
118 $this->jobDataDto = $jobDataDto;
119 $this->taskQueue->setup(static::getTaskName());
120 $this->taskQueue->seek($this->jobDataDto->getQueueOffset());
121 $this->setupCurrentTaskDto();
122 }
123
124 /**
125 * @var bool $incrementStep Whether to increment the step when generating a response or not.
126 * This might be false when you want to generate a response and still be
127 * able to retry the same step in the next request.
128 *
129 * @return TaskResponseDto
130 */
131 public function generateResponse($incrementStep = true): TaskResponseDto
132 {
133 if ($incrementStep) {
134 $this->stepsDto->incrementCurrentStep();
135 }
136
137 // TODO Hydrate
138 $response = $this->getResponseDto();
139 $response->setIsRunning(!$this->stepsDto->isFinished());
140 $response->setPercentage($this->stepsDto->getPercentage());
141 $response->setTotal($this->stepsDto->getTotal());
142 $response->setStep($this->stepsDto->getCurrent());
143 $response->setTask($this->getTaskName());
144 $response->setStatusTitle(static::getTaskTitle());
145 $response->setJobId($this->jobDataDto->getId());
146
147 /*
148 * If this backup contains only a database, let's not display log entries
149 * for file-related tasks, as they expose internal behavior of the backup
150 * feature that are not relevant to the user.
151 */
152 if ($this->jobDataDto->getDatabaseOnlyBackup()) {
153 if (
154 !$this instanceof FilesystemScannerTask
155 && !$this instanceof FileBackupTask
156 && !$this instanceof ExtractFilesTask
157 ) {
158 $response->addMessage($this->logger->getLastLogMsg());
159 }
160 } else {
161 $response->addMessage($this->logger->getLastLogMsg());
162 }
163
164 $this->logger->setFileName(sprintf(
165 '%s__%s__%s',
166 $this->getJobName(),
167 date('Y_m_d__H'),
168 $this->getJobId()
169 ));
170
171 if ($this->stepsDto->isFinished()) {
172 $this->taskQueue->seek(0);
173 $this->jobDataDto->setQueueOffset(0);
174 $response->setPercentage(0);
175 $this->cache->delete();
176 $this->jobDataDto->setCurrentTaskData([]);
177 } else {
178 $this->persistStepsDto();
179 }
180
181 $response = apply_filters('wpstg.task.response', $response);
182
183 return $response;
184 }
185
186 /**
187 * Save StepsDto to disk.
188 * This happens automatically during the shutdown process,
189 * but it can also be called manually.
190 * @return void
191 */
192 public function persistStepsDto()
193 {
194 $this->cache->save($this->stepsDto->toArray(), true);
195 }
196
197 /**
198 * @return string|null
199 */
200 public function getJobName()
201 {
202 return $this->jobName;
203 }
204
205 /**
206 * @param string|null $jobName
207 */
208 public function setJobName($jobName)
209 {
210 $this->jobName = $jobName;
211 }
212
213 /**
214 * @return string|int|null
215 */
216 public function getJobId()
217 {
218 return $this->jobId;
219 }
220
221 /**
222 * @param string|int|null $jobId
223 */
224 public function setJobId($jobId)
225 {
226 $this->jobId = $jobId;
227 }
228
229 /**
230 * @param bool $debug
231 */
232 public function setDebug($debug)
233 {
234 $this->debug = (bool)$debug;
235 }
236
237 /**
238 * @return Logger
239 */
240 public function getLogger(): Logger
241 {
242 return $this->logger;
243 }
244
245 /**
246 * @return SeekableQueueInterface
247 */
248 public function getQueue(): SeekableQueueInterface
249 {
250 return $this->taskQueue;
251 }
252
253 /**
254 * @param AbstractTaskDto $taskDto
255 * @return void
256 */
257 public function setCurrentTaskDto(AbstractTaskDto $taskDto)
258 {
259 $this->currentTaskDto = $taskDto;
260 $this->jobDataDto->setCurrentTaskData($taskDto->toArray());
261 }
262
263 /**
264 * @return TaskResponseDto
265 */
266 protected function getResponseDto()
267 {
268 return new TaskResponseDto();
269 }
270
271 /** @return string */
272 protected function getCurrentTaskType(): string
273 {
274 return '';
275 }
276
277 /**
278 * @return void
279 */
280 protected function setupCurrentTaskDto()
281 {
282 $currentTaskType = $this->getCurrentTaskType();
283 if (empty($currentTaskType) || !class_exists($currentTaskType)) {
284 return;
285 }
286
287 try {
288 $currentTaskData = $this->jobDataDto->getCurrentTaskData();
289 $this->currentTaskDto = WPStaging::make($currentTaskType);
290 $this->currentTaskDto->hydrateProperties($currentTaskData);
291 } catch (Exception $e) {
292 }
293 }
294 }
295