Compression
1 week ago
Database
1 week ago
DirectoryExplorer
1 week ago
AbstractBackgroundBackupRequest.php
1 week ago
AbstractBackupsFinder.php
1 week ago
AbstractExtractor.php
1 week ago
AbstractServiceProvider.php
1 week ago
Archiver.php
1 week ago
BackupAssets.php
1 week ago
BackupContent.php
1 week ago
BackupMetadataEditor.php
1 week ago
BackupMetadataReader.php
1 week ago
BackupSigner.php
1 week ago
BackupsDirectoryResolver.php
1 week ago
BackupsFinder.php
1 week ago
BeforeUpdateBackupRequest.php
1 week ago
BeforeUpdateBackupsService.php
1 week ago
Extractor.php
1 week ago
FileBackupService.php
1 week ago
FileBackupServiceProvider.php
1 week ago
ServiceInterface.php
1 week ago
TmpBackupCleaner.php
1 week ago
UpdateProtectionHealth.php
1 week ago
UpdateProtectionSettings.php
1 week ago
ZlibCompressor.php
1 week ago
BackupMetadataEditor.php
58 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup\Service; |
| 4 | |
| 5 | use WPStaging\Backup\BackupHeader; |
| 6 | use WPStaging\Framework\Filesystem\FileObject; |
| 7 | use WPStaging\Backup\Entity\BackupMetadata; |
| 8 | |
| 9 | class BackupMetadataEditor |
| 10 | { |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | public function setBackupMetadata(FileObject $backupFile, BackupMetadata $newMetadata) |
| 16 | { |
| 17 | $backupMetadataReader = new BackupMetadataReader($backupFile); |
| 18 | $existingMetadataPosition = $backupMetadataReader->getExistingMetadataPosition(); |
| 19 | |
| 20 | $backupFile->fseek($existingMetadataPosition); |
| 21 | |
| 22 | $maybeMetadataLine = $backupFile->readAndMoveNext(); |
| 23 | |
| 24 | |
| 25 | if (!is_array($backupMetadataReader->extractMetadata($maybeMetadataLine))) { |
| 26 | throw new \UnexpectedValueException('Could not find the existing metadata from the backup.'); |
| 27 | } |
| 28 | |
| 29 | $backupFile->ftruncate($existingMetadataPosition); |
| 30 | $backupFile->fseek($existingMetadataPosition); |
| 31 | |
| 32 | $prepandForSql = ''; |
| 33 | if ($backupFile->isSqlFile()) { |
| 34 | $prepandForSql = '-- '; |
| 35 | } |
| 36 | |
| 37 | $backupFile->fwrite($prepandForSql . json_encode($newMetadata) . BackupHeader::LINE_TERMINATOR); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | public function getLineTerminatorOverhead(FileObject $backupFile): int |
| 47 | { |
| 48 | $existingMetadataPosition = (new BackupMetadataReader($backupFile))->getExistingMetadataPosition(); |
| 49 | |
| 50 | $backupFile->fseek($existingMetadataPosition); |
| 51 | $existingLine = $backupFile->readAndMoveNext(); |
| 52 | |
| 53 | $existingTerminatorLength = strlen($existingLine) - strlen(rtrim($existingLine, "\r\n")); |
| 54 | |
| 55 | return $existingTerminatorLength - strlen(BackupHeader::LINE_TERMINATOR); |
| 56 | } |
| 57 | } |
| 58 |