1 | <?php |
||
13 | class Site_Size { |
||
14 | |||
15 | private $size = 0; |
||
16 | private $type = ''; |
||
17 | private $excludes = array(); |
||
18 | |||
19 | /** |
||
20 | * Constructor |
||
21 | * |
||
22 | * Set up some initial conditions including whether we want to calculate the |
||
23 | * size of the database, files or both and whether to exclude any files from |
||
24 | * the file size calculation. |
||
25 | * |
||
26 | * @param string $type Whether to calculate the size of the database, files |
||
27 | * or both. Should be one of 'file', 'database' or 'complete' |
||
28 | * @param array $excludes An array of exclude rules |
||
|
|||
29 | */ |
||
30 | public function __construct( $type = 'complete', Excludes $excludes = null ) { |
||
34 | |||
35 | /** |
||
36 | * Calculate the size total size of the database + files. |
||
37 | * |
||
38 | * Doesn't account for any compression that would be gained by zipping. |
||
39 | * |
||
40 | * @return string |
||
41 | */ |
||
42 | public function get_site_size() { |
||
43 | |||
44 | if ( $this->size ) { |
||
45 | return $this->size; |
||
46 | } |
||
47 | |||
48 | $size = 0; |
||
49 | |||
50 | // Include database size except for file only schedule. |
||
51 | if ( 'file' !== $this->type ) { |
||
52 | |||
53 | global $wpdb; |
||
54 | $tables = $wpdb->get_results( 'SHOW TABLE STATUS FROM `' . DB_NAME . '`', ARRAY_A ); |
||
55 | |||
56 | foreach ( $tables as $table ) { |
||
57 | $size += (float) $table['Data_length']; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | // Include total size of dirs/files except for database only schedule. |
||
62 | if ( 'database' !== $this->type ) { |
||
63 | |||
64 | $root = new \SplFileInfo( Path::get_root() ); |
||
65 | $size += $this->filesize( $root ); |
||
66 | |||
67 | } |
||
68 | |||
69 | $this->size = $size; |
||
70 | |||
71 | return $size; |
||
72 | |||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Get the site size formatted |
||
77 | * |
||
78 | * @see size_format |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public function get_formatted_site_size() { |
||
85 | |||
86 | /** |
||
87 | * Whether the total filesize is being calculated |
||
88 | * |
||
89 | * @return bool |
||
90 | */ |
||
91 | public static function is_site_size_being_calculated() { |
||
94 | |||
95 | /** |
||
96 | * Whether the total filesize is cached |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | public static function is_site_size_cached() { |
||
103 | |||
104 | /** |
||
105 | * Recursively scans a directory to calculate the total filesize |
||
106 | * |
||
107 | * Locks should be set by the caller with `set_transient( 'hmbkp_directory_filesizes_running', true, HOUR_IN_SECONDS );` |
||
108 | * |
||
109 | * @return array $directory_sizes An array of directory paths => filesize sum of all files in directory |
||
110 | */ |
||
111 | public function recursive_filesize_scanner() { |
||
157 | |||
158 | /** |
||
159 | * Get the total filesize for a given file or directory |
||
160 | * |
||
161 | * If $file is a file then just return the result of `filesize()`. |
||
162 | * If $file is a directory then recursively calculate the size. |
||
163 | * |
||
164 | * @param \SplFileInfo $file The file or directory you want to know the size of |
||
165 | * |
||
166 | * @return int The total filesize of the file or directory |
||
167 | */ |
||
168 | public function filesize( \SplFileInfo $file ) { |
||
186 | |||
187 | public function directory_filesize( \SplFileInfo $file ) { |
||
215 | |||
216 | public function rebuild_directory_filesizes() { |
||
230 | |||
231 | } |
||
232 |
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.