Ajax
2 years ago
BackgroundProcessing
2 years ago
Dto
2 years ago
Entity
2 years ago
Exceptions
2 years ago
Interfaces
2 years ago
Job
2 years ago
Request
2 years ago
Service
2 years ago
Storage
2 years ago
Task
2 years ago
AfterRestore.php
2 years ago
BackupDeleter.php
2 years ago
BackupDownload.php
2 years ago
BackupFileIndex.php
2 years ago
BackupHeader.php
2 years ago
BackupRepairer.php
2 years ago
BackupRetentionHandler.php
2 years ago
BackupScheduler.php
2 years ago
BackupServiceProvider.php
2 years ago
BackupValidator.php
2 years ago
FileHeader.php
2 years ago
FileHeaderAttribute.php
2 years ago
WithBackupIdentifier.php
2 years ago
WithBackupIdentifier.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup; |
| 4 | |
| 5 | use WPStaging\Backup\Task\Tasks\JobBackup\DatabaseBackupTask; |
| 6 | use WPStaging\Framework\Filesystem\PartIdentifier; |
| 7 | |
| 8 | trait WithBackupIdentifier |
| 9 | { |
| 10 | /** |
| 11 | * @var string[] |
| 12 | */ |
| 13 | protected $listedMultipartBackups = []; |
| 14 | |
| 15 | /** |
| 16 | * @param string $identifier |
| 17 | * @param string $input |
| 18 | * @return bool |
| 19 | */ |
| 20 | public function checkPartByIdentifier(string $identifier, string $input) |
| 21 | { |
| 22 | return preg_match("#{$identifier}(.[0-9]+)?.wpstg$#", $input); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @param string $name |
| 27 | * @return bool |
| 28 | */ |
| 29 | public function isBackupPart(string $name) |
| 30 | { |
| 31 | $dbExtension = DatabaseBackupTask::FILE_FORMAT; |
| 32 | $dbIdentifier = PartIdentifier::DATABASE_PART_IDENTIFIER; |
| 33 | if (preg_match("#{$dbIdentifier}(.[0-9]+)?.{$dbExtension}$#", $name)) { |
| 34 | return true; |
| 35 | } |
| 36 | |
| 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 = "({$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 (strpos($filename, '.' . PartIdentifier::DATABASE_PART_IDENTIFIER . '.' . DatabaseBackupTask::FILE_FORMAT) !== false) { |
| 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 | // This is required if the table prefix contains underscore like wp_some |
| 97 | $filename = str_replace('.' . PartIdentifier::DATABASE_PART_IDENTIFIER . '.' . DatabaseBackupTask::FILE_FORMAT, '', $filename); |
| 98 | // Get position of last dot . in filename |
| 99 | $lastDotPosition = strrpos($filename, '.'); |
| 100 | // Get filename until last dot to remove the table prefix |
| 101 | $filename = substr($filename, 0, $lastDotPosition); |
| 102 | |
| 103 | $fileInfos = explode('_', $filename); |
| 104 | return $fileInfos[count($fileInfos) - 1]; |
| 105 | } |
| 106 | } |
| 107 |