Ajax
1 week ago
BackgroundProcessing
1 year ago
Dto
1 month ago
Entity
2 weeks ago
Exceptions
1 year ago
FileHeader
2 months ago
Interfaces
8 months ago
Job
3 months ago
Request
1 year ago
Service
1 month ago
Storage
2 months ago
Task
1 week ago
Traits
11 months ago
Utils
1 week ago
AfterRestore.php
6 months ago
BackupDeleter.php
1 week ago
BackupDownload.php
10 months ago
BackupFileIndex.php
1 year ago
BackupGlitchReason.php
1 year ago
BackupHeader.php
2 months ago
BackupRepairer.php
8 months ago
BackupRetentionHandler.php
2 months ago
BackupScheduler.php
1 month ago
BackupServiceProvider.php
3 months ago
BackupValidator.php
1 week ago
FileHeader.php
1 month ago
FileHeaderAttribute.php
2 years ago
WithBackupIdentifier.php
1 week ago
WithBackupIdentifier.php
115 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup; |
| 4 | |
| 5 | use WPStaging\Backup\Service\Database\DatabaseImporter; |
| 6 | use WPStaging\Framework\Filesystem\PartIdentifier; |
| 7 | |
| 8 | trait WithBackupIdentifier |
| 9 | { |
| 10 | /** |
| 11 | * List of ids of multipart backups |
| 12 | * @var string[] |
| 13 | */ |
| 14 | protected $listedMultipartBackups = []; |
| 15 | |
| 16 | /** |
| 17 | * @param string $identifier |
| 18 | * @param string $input |
| 19 | * @return bool |
| 20 | */ |
| 21 | public function checkPartByIdentifier(string $identifier, string $input) |
| 22 | { |
| 23 | return preg_match("#{$identifier}(.[0-9]+)?.wpstg$#", $input); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @param string $name |
| 28 | * @return bool |
| 29 | */ |
| 30 | public function isBackupPart(string $name) |
| 31 | { |
| 32 | if (preg_match($this->getDatabasePartSuffixPattern(), $name)) { |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | $dbIdentifier = PartIdentifier::DATABASE_PART_IDENTIFIER; |
| 37 | $pluginIdentifier = PartIdentifier::PLUGIN_PART_IDENTIFIER; |
| 38 | $mupluginIdentifier = PartIdentifier::MU_PLUGIN_PART_IDENTIFIER; |
| 39 | $themeIdentifier = PartIdentifier::THEME_PART_IDENTIFIER; |
| 40 | $uploadIdentifier = PartIdentifier::UPLOAD_PART_IDENTIFIER; |
| 41 | $otherIdentifier = PartIdentifier::OTHER_WP_CONTENT_PART_IDENTIFIER; |
| 42 | $otherWpRootIdentifier = PartIdentifier::OTHER_WP_ROOT_PART_IDENTIFIER; |
| 43 | |
| 44 | $identifiers = "({$dbIdentifier}|{$pluginIdentifier}|{$mupluginIdentifier}|{$themeIdentifier}|{$uploadIdentifier}|{$otherIdentifier}|{$otherWpRootIdentifier})"; |
| 45 | |
| 46 | if ($this->checkPartByIdentifier($identifiers, $name)) { |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @return void |
| 55 | */ |
| 56 | public function clearListedMultipartBackups() |
| 57 | { |
| 58 | $this->listedMultipartBackups = []; |
| 59 | } |
| 60 | |
| 61 | public function isListedMultipartBackup(string $filename, bool $shouldAddBackup = true) |
| 62 | { |
| 63 | $id = $this->extractBackupIdFromFilename($filename); |
| 64 | if (in_array($id, $this->listedMultipartBackups)) { |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | if ($shouldAddBackup) { |
| 69 | $this->listedMultipartBackups[] = $id; |
| 70 | } |
| 71 | |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @param string $filename |
| 77 | * @return string |
| 78 | */ |
| 79 | public function extractBackupIdFromFilename(string $filename) |
| 80 | { |
| 81 | if (preg_match($this->getDatabasePartSuffixPattern(), $filename)) { |
| 82 | return $this->extractBackupIdFromDatabaseBackupFilename($filename); |
| 83 | } |
| 84 | |
| 85 | $fileInfos = explode('_', $filename); |
| 86 | $fileInfos = $fileInfos[count($fileInfos) - 1]; |
| 87 | return explode('.', $fileInfos)[0]; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @param string $filename |
| 92 | * @return string |
| 93 | */ |
| 94 | protected function extractBackupIdFromDatabaseBackupFilename(string $filename) |
| 95 | { |
| 96 | $filename = preg_replace($this->getDatabasePartSuffixPattern(), '', $filename); |
| 97 | |
| 98 | $lastDotPosition = strrpos($filename, '.'); |
| 99 | if ($lastDotPosition !== false) { |
| 100 | $filename = substr($filename, 0, $lastDotPosition); |
| 101 | } |
| 102 | |
| 103 | $fileInfos = explode('_', $filename); |
| 104 | return $fileInfos[count($fileInfos) - 1]; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @return string |
| 109 | */ |
| 110 | protected function getDatabasePartSuffixPattern(): string |
| 111 | { |
| 112 | return '#\.' . PartIdentifier::DATABASE_PART_IDENTIFIER . '(\.\d+)?\.' . DatabaseImporter::FILE_FORMAT . '$#'; |
| 113 | } |
| 114 | } |
| 115 |