1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HM\BackUpWordPress; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Finder\Finder; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Site Size class |
9
|
|
|
* |
10
|
|
|
* Use to calculate the total or partial size of the sites database and files. |
11
|
|
|
*/ |
12
|
|
|
class Site_Size { |
13
|
|
|
|
14
|
|
|
private $size = 0; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Constructor |
18
|
|
|
* |
19
|
|
|
* Set up some initial conditions including whether we want to calculate the |
20
|
|
|
* size of the database, files or both and whether to exclude any files from |
21
|
|
|
* the file size calculation. |
22
|
|
|
* |
23
|
|
|
* @param string $type Whether to calculate the size of the database, files |
24
|
|
|
* or both. Should be one of 'file', 'database' or 'complete' |
25
|
|
|
* @param array $excludes An array of exclude rules |
|
|
|
|
26
|
|
|
*/ |
27
|
|
|
public function __construct( $type = 'complete', Excludes $excludes = null ) { |
28
|
|
|
$this->type = $type; |
|
|
|
|
29
|
|
|
$this->excludes = $excludes; |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Calculate the size total size of the database + files. |
34
|
|
|
* |
35
|
|
|
* Doesn't account for any compression that would be gained by zipping. |
36
|
|
|
* |
37
|
|
|
* @return string |
|
|
|
|
38
|
|
|
*/ |
39
|
|
|
public function get_site_size() { |
40
|
|
|
|
41
|
|
|
if ( $this->size ) { |
42
|
|
|
return $this->size; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$size = 0; |
46
|
|
|
|
47
|
|
|
// Include database size except for file only schedule. |
48
|
|
|
if ( 'file' !== $this->type ) { |
49
|
|
|
|
50
|
|
|
global $wpdb; |
51
|
|
|
$tables = $wpdb->get_results( 'SHOW TABLE STATUS FROM `' . DB_NAME . '`', ARRAY_A ); |
52
|
|
|
|
53
|
|
|
foreach ( $tables as $table ) { |
54
|
|
|
$size += (float) $table['Data_length']; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Include total size of dirs/files except for database only schedule. |
59
|
|
|
if ( 'database' !== $this->type ) { |
60
|
|
|
|
61
|
|
|
$root = new \SplFileInfo( Path::get_root() ); |
62
|
|
|
$size += $this->filesize( $root ); |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->size = $size; |
|
|
|
|
67
|
|
|
|
68
|
|
|
return $size; |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the site size formatted |
74
|
|
|
* |
75
|
|
|
* @see size_format |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function get_formatted_site_size() { |
80
|
|
|
return size_format( $this->get_site_size() ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Whether the total filesize is being calculated |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
public static function is_site_size_being_calculated() { |
89
|
|
|
return false !== get_transient( 'hmbkp_directory_filesizes_running' ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Whether the total filesize is cached |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public static function is_site_size_cached() { |
98
|
|
|
return false !== get_transient( 'hmbkp_directory_filesizes' ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Return the contents of `$directory` as a single depth list ordered by total filesize. |
103
|
|
|
* |
104
|
|
|
* Will schedule background threads to recursively calculate the filesize of subdirectories. |
105
|
|
|
* The total filesize of each directory and subdirectory is cached in a transient for 1 week. |
106
|
|
|
* |
107
|
|
|
* @param string $directory The directory to list |
108
|
|
|
* |
109
|
|
|
* @return array returns an array of files ordered by filesize |
110
|
|
|
*/ |
111
|
|
|
public function list_directory_by_total_filesize( $directory ) { |
112
|
|
|
|
113
|
|
|
$files = $files_with_no_size = $empty_files = $files_with_size = $unreadable_files = array(); |
114
|
|
|
|
115
|
|
|
if ( ! is_dir( $directory ) ) { |
116
|
|
|
return $files; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$finder = new Finder(); |
120
|
|
|
$finder->followLinks(); |
121
|
|
|
$finder->ignoreDotFiles( false ); |
122
|
|
|
$finder->ignoreUnreadableDirs(); |
123
|
|
|
$finder->depth( '== 0' ); |
124
|
|
|
|
125
|
|
|
$files = $finder->in( $directory ); |
|
|
|
|
126
|
|
|
|
127
|
|
|
foreach ( $files as $entry ) { |
128
|
|
|
|
129
|
|
|
// Get the total filesize for each file and directory |
130
|
|
|
$filesize = $this->filesize( $entry ); |
131
|
|
|
|
132
|
|
|
if ( $filesize ) { |
133
|
|
|
|
134
|
|
|
// If there is already a file with exactly the same filesize then let's keep increasing the filesize of this one until we don't have a clash |
135
|
|
|
while ( array_key_exists( $filesize, $files_with_size ) ) { |
136
|
|
|
$filesize ++; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$files_with_size[ $filesize ] = $entry; |
140
|
|
|
|
141
|
|
|
} elseif ( 0 === $filesize ) { |
142
|
|
|
|
143
|
|
|
$empty_files[] = $entry; |
144
|
|
|
|
145
|
|
|
} else { |
146
|
|
|
|
147
|
|
|
$files_with_no_size[] = $entry; |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Sort files by filesize, largest first |
154
|
|
|
krsort( $files_with_size ); |
155
|
|
|
|
156
|
|
|
// Add 0 byte files / directories to the bottom |
157
|
|
|
$files = $files_with_size + array_merge( $empty_files, $unreadable_files ); |
158
|
|
|
|
159
|
|
|
// Add directories that are still calculating to the top |
160
|
|
|
if ( $files_with_no_size ) { |
|
|
|
|
161
|
|
|
|
162
|
|
|
// We have to loop as merging or concatenating the array would re-flow the keys which we don't want because the filesize is stored in the key |
163
|
|
|
foreach ( $files_with_no_size as $entry ) { |
164
|
|
|
array_unshift( $files, $entry ); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $files; |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Recursively scans a directory to calculate the total filesize |
174
|
|
|
* |
175
|
|
|
* Locks should be set by the caller with `set_transient( 'hmbkp_directory_filesizes_running', true, HOUR_IN_SECONDS );` |
176
|
|
|
* |
177
|
|
|
* @return array $directory_sizes An array of directory paths => filesize sum of all files in directory |
178
|
|
|
*/ |
179
|
|
|
public function recursive_filesize_scanner() { |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Raise the `memory_limit` and `max_execution time` |
183
|
|
|
* |
184
|
|
|
* Respects the WP_MAX_MEMORY_LIMIT Constant and the `admin_memory_limit` |
185
|
|
|
* filter. |
186
|
|
|
*/ |
187
|
|
|
@ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); |
|
|
|
|
188
|
|
|
@set_time_limit( 0 ); |
|
|
|
|
189
|
|
|
|
190
|
|
|
// Use the cached array directory sizes if available |
191
|
|
|
$directory_sizes = get_transient( 'hmbkp_directory_filesizes' ); |
192
|
|
|
|
193
|
|
|
// If we do have it in cache then let's use it and also clear the lock |
194
|
|
|
if ( is_array( $directory_sizes ) ) { |
195
|
|
|
delete_transient( 'hmbkp_directory_filesizes_running' ); |
196
|
|
|
return $directory_sizes; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// If we don't have it cached then we'll need to re-calculate |
200
|
|
|
$finder = new Finder(); |
201
|
|
|
$finder->followLinks(); |
202
|
|
|
$finder->ignoreDotFiles( false ); |
203
|
|
|
$finder->ignoreUnreadableDirs( true ); |
204
|
|
|
|
205
|
|
|
$files = $finder->in( Path::get_root() ); |
206
|
|
|
|
207
|
|
|
foreach ( $files as $file ) { |
208
|
|
|
|
209
|
|
|
if ( $file->isReadable() ) { |
210
|
|
|
$directory_sizes[ wp_normalize_path( $file->getRealpath() ) ] = $file->getSize(); |
211
|
|
|
} else { |
212
|
|
|
$directory_sizes[ wp_normalize_path( $file->getRealpath() ) ] = 0; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
set_transient( 'hmbkp_directory_filesizes', $directory_sizes, DAY_IN_SECONDS ); |
218
|
|
|
|
219
|
|
|
// Remove the lock |
220
|
|
|
delete_transient( 'hmbkp_directory_filesizes_running' ); |
221
|
|
|
|
222
|
|
|
return $directory_sizes; |
223
|
|
|
|
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Get the total filesize for a given file or directory |
228
|
|
|
* |
229
|
|
|
* If $file is a file then just return the result of `filesize()`. |
230
|
|
|
* If $file is a directory then schedule a recursive filesize scan. |
231
|
|
|
* |
232
|
|
|
* @param \SplFileInfo $file The file or directory you want to know the size of |
233
|
|
|
* |
234
|
|
|
* @return int The total of the file or directory |
235
|
|
|
*/ |
236
|
|
|
public function filesize( \SplFileInfo $file ) { |
237
|
|
|
|
238
|
|
|
// Skip missing or unreadable files |
239
|
|
|
if ( ! file_exists( $file->getPathname() ) || ! $file->getRealpath() || ! $file->isReadable() ) { |
240
|
|
|
return 0; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
// If it's a file then just pass back the filesize |
244
|
|
|
if ( $file->isFile() && $file->isReadable() ) { |
245
|
|
|
return $file->getSize(); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
// If it's a directory then pull it from the cached filesize array |
249
|
|
|
if ( $file->isDir() ) { |
250
|
|
|
|
251
|
|
|
// If we haven't calculated the site size yet then kick it off in a thread |
252
|
|
|
$directory_sizes = get_transient( 'hmbkp_directory_filesizes' ); |
253
|
|
|
|
254
|
|
|
if ( ! is_array( $directory_sizes ) ) { |
255
|
|
|
|
256
|
|
|
if ( ! $this->is_site_size_being_calculated() ) { |
257
|
|
|
|
258
|
|
|
// Mark the filesize as being calculated |
259
|
|
|
set_transient( 'hmbkp_directory_filesizes_running', true, HOUR_IN_SECONDS ); |
260
|
|
|
|
261
|
|
|
// Schedule a Backdrop task to trigger a recalculation |
262
|
|
|
$task = new \HM\Backdrop\Task( array( $this, 'recursive_filesize_scanner' ) ); |
263
|
|
|
$task->schedule(); |
264
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// If the filesize is being calculated then return null |
268
|
|
|
return null; |
269
|
|
|
|
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
$directory_sizes = array_flip( preg_grep( '(' . wp_normalize_path( $file->getRealPath() ) . ')', array_flip( $directory_sizes ) ) ); |
273
|
|
|
|
274
|
|
|
if ( $this->excludes ) { |
275
|
|
|
$excludes = implode( '|', $this->excludes->get_excludes_for_regex() ); |
276
|
|
|
if ( $excludes ) { |
277
|
|
|
$directory_sizes = array_flip( preg_grep( '(' . $excludes . ')', array_flip( $directory_sizes ), PREG_GREP_INVERT ) ); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
// Directory size is now just a sum of all files across all sub directories |
282
|
|
|
return absint( array_sum( $directory_sizes ) ); |
283
|
|
|
|
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
} |
289
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.