PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.3.1
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.3.1
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 / CleanupTmpFilesTask.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
CleanupTmpFilesTask.php
132 lines
1 <?php
2
3 namespace WPStaging\Backup\Task\Tasks;
4
5 use WPStaging\Framework\Adapter\Directory;
6 use WPStaging\Framework\Filesystem\PathIdentifier;
7 use WPStaging\Framework\Queue\SeekableQueueInterface;
8 use WPStaging\Backup\Dto\StepsDto;
9 use WPStaging\Backup\Task\AbstractTask;
10 use WPStaging\Backup\Task\RestoreTask;
11 use WPStaging\Vendor\Psr\Log\LoggerInterface;
12 use WPStaging\Framework\Utils\Cache\Cache;
13 use WPStaging\Framework\Filesystem\Filesystem;
14
15 class CleanupTmpFilesTask extends AbstractTask
16 {
17 private $filesystem;
18 private $directory;
19 private $pathIdentifier;
20
21 public function __construct(LoggerInterface $logger, Cache $cache, StepsDto $stepsDto, Filesystem $filesystem, Directory $directory, SeekableQueueInterface $taskQueue, PathIdentifier $pathIdentifier)
22 {
23 parent::__construct($logger, $cache, $stepsDto, $taskQueue);
24 $this->filesystem = $filesystem;
25 $this->directory = $directory;
26 $this->pathIdentifier = $pathIdentifier;
27 }
28
29 public static function getTaskName()
30 {
31 return 'backup_restore_cleanup_files';
32 }
33
34 public static function getTaskTitle()
35 {
36 return 'Cleaning Up Restore Files';
37 }
38
39 /**
40 * @return \WPStaging\Backup\Dto\TaskResponseDto
41 */
42 public function execute()
43 {
44 $this->prepareCleanupRestoreTask();
45
46 $tmpRestoreDir = $this->directory->getTmpDirectory();
47
48 $tmpRestoreDir = untrailingslashit($tmpRestoreDir);
49
50 $relativePathForLogging = str_replace($this->filesystem->normalizePath(ABSPATH, true), '', $this->filesystem->normalizePath($tmpRestoreDir, true));
51
52 // Early bail: Path to Clean does not exist
53 if (!file_exists($tmpRestoreDir)) {
54 return $this->generateResponse();
55 }
56
57 try {
58 $deleted = $this->filesystem
59 ->setRecursive(true)
60 ->setShouldStop(function () {
61 return $this->isThreshold();
62 })
63 ->delete($tmpRestoreDir);
64 } catch (\Exception $e) {
65 $this->logger->warning(sprintf(
66 '%s: Could not cleanup path "%s". May be a permission issue?',
67 static::getTaskTitle(),
68 $relativePathForLogging
69 ));
70
71 return $this->generateResponse();
72 }
73
74 if ($deleted) {
75 // Successfully deleted
76 $this->logger->info(sprintf(
77 '%s: Path "%s" successfully cleaned up.',
78 static::getTaskTitle(),
79 $relativePathForLogging
80 ));
81
82 return $this->generateResponse();
83 } else {
84 /*
85 * Not successfully deleted.
86 * This can happen if the folder to delete is too large
87 * to be deleted in a single request. We continue
88 * deleting it in the next request...
89 */
90 $response = $this->generateResponse(false);
91 $response->setIsRunning(true);
92
93 $this->logger->info(sprintf(
94 '%s: Re-enqueing path %s for deletion, as it couldn\'t be deleted in a single request without
95 hitting execution limits. If you see this message in a loop, PHP might not be able to delete
96 this directory, so you might want to try to delete it manually.',
97 static::getTaskTitle(),
98 $relativePathForLogging
99 ));
100
101 // Early bail: Response modified for repeating
102 return $response;
103 }
104 }
105
106 public function prepareCleanupRestoreTask()
107 {
108 // We only cleanup database file for RestoreTask
109 if (!$this instanceof RestoreTask) {
110 return;
111 }
112
113 // Early bail: Already prepared
114 if ($this->stepsDto->getTotal() === 1) {
115 return;
116 }
117
118 // Clear the .sql file used during the restore, if this backup includes a database.
119 $databaseFile = $this->jobDataDto->getBackupMetadata()->getDatabaseFile();
120
121 if ($databaseFile) {
122 $databaseFile = $this->pathIdentifier->transformIdentifiableToPath($this->jobDataDto->getBackupMetadata()->getDatabaseFile());
123
124 if (file_exists($databaseFile)) {
125 unlink($databaseFile);
126 }
127 }
128
129 $this->stepsDto->setTotal(1);
130 }
131 }
132