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() { |
||
82 | |||
83 | /** |
||
84 | * Get the site size formatted |
||
85 | * |
||
86 | * @see size_format |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | public function get_formatted_site_size() { |
||
93 | |||
94 | /** |
||
95 | * Whether the total filesize is being calculated |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | public static function is_site_size_being_calculated() { |
||
102 | |||
103 | /** |
||
104 | * Whether the total filesize is cached |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | public function is_site_size_cached() { |
||
109 | return (bool) $this->get_cached_filesizes(); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Recursively scans a directory to calculate the total filesize |
||
114 | * |
||
115 | * Locks should be set by the caller with `set_transient( 'hmbkp_directory_filesizes_running', true, HOUR_IN_SECONDS );` |
||
116 | * |
||
117 | * @return array $directory_sizes An array of directory paths => filesize sum of all files in directory |
||
118 | */ |
||
119 | public function recursive_filesize_scanner() { |
||
164 | |||
165 | /** |
||
166 | * Get the total filesize for a given file or directory. Aware of exclusions. |
||
167 | * |
||
168 | * If $file is a file then return the result of `filesize()` or 0 if it's excluded. |
||
169 | * If $file is a directory then recursively calculate the size without |
||
170 | * the size of excluded files/directories. |
||
171 | * |
||
172 | * @param \SplFileInfo $file The file or directory you want to know the size of. |
||
173 | * |
||
174 | * @return int The total filesize of the file or directory without |
||
175 | * the size of excluded files/directories. |
||
176 | */ |
||
177 | public function filesize( \SplFileInfo $file ) { |
||
178 | |||
179 | // Skip missing or unreadable files. |
||
180 | if ( ! file_exists( $file->getPathname() ) || ! $file->getRealpath() || ! $file->isReadable() ) { |
||
181 | return 0; |
||
182 | } |
||
183 | |||
184 | // If it's a file then return its filesize or 0 if it's excluded. |
||
185 | if ( $file->isFile() ) { |
||
186 | |||
187 | if ( $this->excludes && $this->excludes->is_file_excluded( $file ) ) { |
||
188 | return 0; |
||
189 | } else { |
||
190 | return $file->getSize(); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | // If it's a directory then pull it from the cached filesize array. |
||
195 | if ( $file->isDir() ) { |
||
196 | return $this->directory_filesize( $file ); |
||
197 | } |
||
198 | } |
||
199 | |||
200 | public function directory_filesize( \SplFileInfo $file ) { |
||
249 | |||
250 | public function rebuild_directory_filesizes() { |
||
264 | |||
265 | public function get_cached_filesizes( $max_age = WEEK_IN_SECONDS ) { |
||
281 | } |
||
282 |
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.