Compression
4 months ago
Database
2 weeks ago
AbstractBackupsFinder.php
1 year ago
AbstractExtractor.php
2 days ago
AbstractServiceProvider.php
2 years ago
Archiver.php
2 weeks ago
BackupAssets.php
1 month ago
BackupContent.php
1 year ago
BackupMetadataEditor.php
1 year ago
BackupMetadataReader.php
5 months ago
BackupSigner.php
7 months ago
BackupsDirectoryResolver.php
2 weeks ago
BackupsFinder.php
2 weeks ago
Extractor.php
2 weeks ago
FileBackupService.php
2 days ago
FileBackupServiceProvider.php
2 years ago
ServiceInterface.php
2 years ago
TmpBackupCleaner.php
2 weeks ago
ZlibCompressor.php
11 months ago
AbstractBackupsFinder.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup\Service; |
| 4 | |
| 5 | use WPStaging\Backup\WithBackupIdentifier; |
| 6 | use WPStaging\Framework\Traits\DebugLogTrait; |
| 7 | use WPStaging\Framework\Traits\WindowsOsTrait; |
| 8 | |
| 9 | /** |
| 10 | * Class AbstractBackupsFinder |
| 11 | * This class should not use any wp core functions or classes. |
| 12 | * |
| 13 | * Finds the .wpstg backups in the filesystem. |
| 14 | * |
| 15 | * @package WPStaging\Backup |
| 16 | */ |
| 17 | abstract class AbstractBackupsFinder |
| 18 | { |
| 19 | use WithBackupIdentifier; |
| 20 | use DebugLogTrait; |
| 21 | use WindowsOsTrait; |
| 22 | |
| 23 | /** @var int */ |
| 24 | const MAX_BACKUP_FILE_TO_SCAN = 1000; |
| 25 | |
| 26 | /** @var string */ |
| 27 | protected $backupsDirectory; |
| 28 | |
| 29 | /** @var int */ |
| 30 | protected $backupsCount; |
| 31 | |
| 32 | public function resetBackupsCount() |
| 33 | { |
| 34 | $this->backupsCount = 0; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param string $backupsDirectory |
| 39 | * @return void |
| 40 | */ |
| 41 | public function setBackupsDirectory(string $backupsDirectory) |
| 42 | { |
| 43 | $this->backupsDirectory = $backupsDirectory; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param bool $refresh |
| 48 | * @return string |
| 49 | */ |
| 50 | public function getBackupsDirectory(bool $refresh = false): string |
| 51 | { |
| 52 | return $this->backupsDirectory; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @return array An array of SplFileInfo objects of .wpstg backup files. |
| 57 | */ |
| 58 | public function findBackups(): array |
| 59 | { |
| 60 | try { |
| 61 | $it = new \DirectoryIterator($this->getBackupsDirectory(true)); |
| 62 | } catch (\Exception $e) { |
| 63 | $this->debugLog('WP STAGING: Could not find backup directory ' . $e->getMessage()); |
| 64 | return []; |
| 65 | } |
| 66 | |
| 67 | $backups = []; |
| 68 | |
| 69 | $this->clearListedMultipartBackups(); |
| 70 | |
| 71 | /** @var \SplFileInfo $file */ |
| 72 | foreach ($it as $file) { |
| 73 | if (($file->getExtension() === 'wpstg' || $file->getExtension() === 'sql') && !$file->isLink()) { |
| 74 | if ($this->backupsCount >= self::MAX_BACKUP_FILE_TO_SCAN) { |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | if ($this->isBackupPart($file->getFilename()) && $this->isListedMultipartBackup($file->getFilename())) { |
| 79 | continue; |
| 80 | } |
| 81 | |
| 82 | // Windows has cache issue, to keep linux operations fast we only check file exist on Windows |
| 83 | if ($this->isWindowsOs() && !file_exists($file->getPathname())) { |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | $backups[] = clone $file; |
| 88 | |
| 89 | $this->backupsCount++; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return $backups; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param string $md5 |
| 98 | * |
| 99 | * @return \SplFileInfo |
| 100 | */ |
| 101 | public function findBackupByMd5Hash(string $md5): \SplFileInfo |
| 102 | { |
| 103 | $backup = array_filter($this->findBackups(), function ($splFileInfo) use ($md5) { |
| 104 | return md5($splFileInfo->getBasename()) === $md5; |
| 105 | }); |
| 106 | |
| 107 | if (empty($backup)) { |
| 108 | throw new \UnexpectedValueException('WP STAGING: Could not find backup by hash ' . $md5); |
| 109 | } |
| 110 | |
| 111 | return array_shift($backup); |
| 112 | } |
| 113 | } |
| 114 |