Ajax
2 years ago
BackgroundProcessing
2 years ago
Dto
2 years ago
Entity
2 years ago
Exceptions
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
3 years ago
BackupDeleter.php
3 years ago
BackupDownload.php
2 years ago
BackupFileIndex.php
2 years ago
BackupProcessLock.php
2 years ago
BackupRepairer.php
3 years ago
BackupRetentionHandler.php
2 years ago
BackupScheduler.php
2 years ago
BackupServiceProvider.php
2 years ago
BackupValidator.php
2 years ago
WithBackupIdentifier.php
2 years ago
wpstgBackupHeader.txt
3 years ago
BackupFileIndex.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup; |
| 4 | |
| 5 | use WPStaging\Framework\Filesystem\PathIdentifier; |
| 6 | |
| 7 | class BackupFileIndex |
| 8 | { |
| 9 | /** @var int */ |
| 10 | public $bytesStart; |
| 11 | |
| 12 | /** @var int */ |
| 13 | public $bytesEnd; |
| 14 | |
| 15 | /** @var string */ |
| 16 | public $identifiablePath; |
| 17 | |
| 18 | /** @var int */ |
| 19 | public $isCompressed; |
| 20 | |
| 21 | /** |
| 22 | * @param string $index |
| 23 | * @return BackupFileIndex |
| 24 | */ |
| 25 | public function readIndex(string $index): BackupFileIndex |
| 26 | { |
| 27 | /* |
| 28 | * We start with a string that is the backup file index, like this: |
| 29 | * |
| 30 | * wpstg_t_/twentytwentyone/readme.txt|9378469:4491 |
| 31 | * |
| 32 | * We split it into two parts, using the pipe "|" character as the delimiter. |
| 33 | * The first part is the identifiable path, the second is the metadata about the file. |
| 34 | * |
| 35 | * By "Identifiable Path", we mean a path that has a prefix that identifies |
| 36 | * what kind of file it is, such as a plugin, mu-plugin, theme, etc. |
| 37 | */ |
| 38 | list($identifiablePath, $entryMetadata) = explode('|', trim($index)); |
| 39 | |
| 40 | $entryMetadata = explode(':', trim($entryMetadata)); |
| 41 | |
| 42 | // This should never happen. |
| 43 | if (count($entryMetadata) < 2) { |
| 44 | // todo: Log this when we have a logger. |
| 45 | throw new \UnexpectedValueException('Invalid backup file index.'); |
| 46 | } |
| 47 | |
| 48 | $offsetStart = (int)$entryMetadata[0]; |
| 49 | $writtenPreviously = (int)$entryMetadata[1]; |
| 50 | |
| 51 | if (count($entryMetadata) >= 3) { |
| 52 | $isCompressed = (int)$entryMetadata[2]; |
| 53 | } else { |
| 54 | $isCompressed = 0; |
| 55 | } |
| 56 | |
| 57 | $backupFileIndex = new BackupFileIndex(); |
| 58 | |
| 59 | // Replace the placeholder with the pipe character. |
| 60 | $backupFileIndex->identifiablePath = str_replace(['{WPSTG_PIPE}', '{WPSTG_COLON}'], ['|', ':'], $identifiablePath); |
| 61 | $backupFileIndex->bytesStart = $offsetStart; |
| 62 | $backupFileIndex->bytesEnd = $writtenPreviously; |
| 63 | $backupFileIndex->isCompressed = $isCompressed; |
| 64 | |
| 65 | return $backupFileIndex; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Creates an index entry for a file to be added to the backup's file index. |
| 70 | * |
| 71 | * @param string $identifiablePath The identifiable path to the file. |
| 72 | * @param int $bytesStart The offset in the backup file where the file starts. |
| 73 | * @param int $bytesEnd The offset in the backup file where the file ends. |
| 74 | * @param int $isCompressed Whether the file is compressed. |
| 75 | * |
| 76 | * @see PathIdentifier For definition of identifiable path. |
| 77 | * |
| 78 | * @return BackupFileIndex |
| 79 | */ |
| 80 | public function createIndex(string $identifiablePath, int $bytesStart, int $bytesEnd, int $isCompressed): BackupFileIndex |
| 81 | { |
| 82 | $backupFileIndex = new BackupFileIndex(); |
| 83 | |
| 84 | // Replace the pipe character with a placeholder to avoid conflicts. |
| 85 | $backupFileIndex->identifiablePath = str_replace(['|', ':'], ['{WPSTG_PIPE}', '{WPSTG_COLON}'], $identifiablePath); |
| 86 | $backupFileIndex->bytesStart = $bytesStart; |
| 87 | $backupFileIndex->bytesEnd = $bytesEnd; |
| 88 | $backupFileIndex->isCompressed = $isCompressed; |
| 89 | |
| 90 | return $backupFileIndex; |
| 91 | } |
| 92 | |
| 93 | public function getIndex(): string |
| 94 | { |
| 95 | return "$this->identifiablePath|$this->bytesStart:$this->bytesEnd:$this->isCompressed"; |
| 96 | } |
| 97 | |
| 98 | public function isIndexLine($item): bool |
| 99 | { |
| 100 | return !empty($item) && strpos($item, ':') !== false && strpos($item, '|') !== false; |
| 101 | } |
| 102 | } |
| 103 |