PluginProbe
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.12.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.12.0
4.12.0 4.11.2 4.11.1 4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 All 67 releases
wp-staging / Backup / BackupRepairer.php
BackupRepairer.php
99 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPStaging\Backup;
4
5 use Exception;
6 use RuntimeException;
7 use UnexpectedValueException;
8 use WPStaging\Backup\Entity\BackupMetadata;
9 use WPStaging\Framework\Job\Exception\DiskNotWritableException;
10 use WPStaging\Backup\Service\BackupMetadataEditor;
11 use WPStaging\Framework\Filesystem\FileObject;
12
13 class BackupRepairer
14 {
15
16 private $metadataEditor;
17
18
19 private $error = false;
20
21 public function __construct(BackupMetadataEditor $metadataEditor)
22 {
23 $this->metadataEditor = $metadataEditor;
24 }
25
26
27 public function getError()
28 {
29 return $this->error;
30 }
31
32
33
34
35
36 public function repairMetadataSize($filePath)
37 {
38 $this->error = false;
39
40 $file = null;
41 try {
42 $file = new FileObject($filePath, FileObject::MODE_APPEND_AND_READ);
43 } catch (DiskNotWritableException $ex) {
44 $this->error = $ex->getMessage();
45 return false;
46 } catch (Exception $ex) {
47 $this->error = $ex->getMessage();
48 return false;
49 }
50
51
52 if ($file->getExtension() !== 'wpstg') {
53 return true;
54 }
55
56 $backupMetadata = new BackupMetadata();
57 try {
58 $backupMetadata->hydrateByFile($file);
59 } catch (RuntimeException $ex) {
60 $this->error = $ex->getMessage();
61 return false;
62 }
63
64
65 if ($backupMetadata->getBackupSize() !== 0) {
66 return true;
67 }
68
69
70
71
72
73
74
75 $fileSize = $file->getSize();
76 if ($fileSize === false || $fileSize < 1) {
77 $this->error = __('Backup size cannot be determined or is zero', 'wp-staging');
78 return false;
79 }
80
81 $backupSize = $fileSize - 2 + strlen((string)$fileSize);
82 if ($backupSize < 1) {
83 $this->error = __('Backup size cannot be zero or less', 'wp-staging');
84 return false;
85 }
86
87 $backupMetadata->setBackupSize($backupSize);
88
89 try {
90 $this->metadataEditor->setBackupMetadata($file, $backupMetadata);
91 } catch (UnexpectedValueException $ex) {
92 $this->error = $ex->getMessage();
93 return false;
94 }
95
96 return true;
97 }
98 }
99