示例#1
0
function calculate_site_size()
{
    $site_size = new Site_Size();
    if (!$site_size->is_site_size_cached()) {
        $root = new \SplFileInfo(Path::get_root());
        $site_size->filesize($root);
    }
}
/**
 * Return the contents of `$directory` as a single depth list ordered by total filesize.
 *
 * Will schedule background threads to recursively calculate the filesize of subdirectories.
 * The total filesize of each directory and subdirectory is cached in a transient for 1 week.
 *
 * @param string $directory The directory to list
 *
 * @todo doesn't really belong in this class, should just be a function
 * @return array            returns an array of files ordered by filesize
 */
function list_directory_by_total_filesize($directory, Excludes $excludes)
{
    $files = $files_with_no_size = $empty_files = $files_with_size = $unreadable_files = array();
    if (!is_dir($directory)) {
        return $files;
    }
    $finder = new \Symfony\Component\Finder\Finder();
    $finder->followLinks();
    $finder->ignoreDotFiles(false);
    $finder->ignoreUnreadableDirs();
    $finder->depth('== 0');
    $site_size = new Site_Size('file', $excludes);
    $files = $finder->in($directory);
    foreach ($files as $entry) {
        // Get the total filesize for each file and directory
        $filesize = $site_size->filesize($entry);
        if ($filesize) {
            // 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
            while (array_key_exists($filesize, $files_with_size)) {
                $filesize++;
            }
            $files_with_size[$filesize] = $entry;
        } elseif (0 === $filesize) {
            $empty_files[] = $entry;
        } else {
            $files_with_no_size[] = $entry;
        }
    }
    // Sort files by filesize, largest first
    krsort($files_with_size);
    // Add 0 byte files / directories to the bottom
    $files = $files_with_size + array_merge($empty_files, $unreadable_files);
    // Add directories that are still calculating to the top
    if ($files_with_no_size) {
        // 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
        foreach ($files_with_no_size as $entry) {
            array_unshift($files, $entry);
        }
    }
    return $files;
}
?>

			</th>

			<td class="column-filesize">

				<?php 
if (Site_Size::is_site_size_being_calculated()) {
    ?>

					<span class="spinner is-active"></span>

				<?php 
} else {
    $root = new \SplFileInfo(Path::get_root());
    $size = $site_size->filesize($root);
    if (false !== $size) {
        $size = size_format($size);
        if (!$size) {
            $size = '0 B';
        }
        ?>

						<code>

							<?php 
        echo esc_html($size);
        ?>

							<a class="dashicons dashicons-update"
							   href="<?php 
 /**
  * File is NOT excluded directly (either in the root or any non-excluded sub-directory).
  *
  * Main test: Site_Size->filesize( $file )
  * Expected:  file size should be what it is.
  */
 public function test_file_size_not_excluded_directly()
 {
     $file_path = $this->test_data . '/test-data.txt';
     $file = new \SplFileInfo($file_path);
     // Check the file is created and its size is NOT 0.
     $this->assertContains($this->root->getPath(), $file->getPath());
     $this->assertNotSame($file->getSize(), 0);
     // Check file size via BWP function. It should NOT be 0, should be the same size as via getSize().
     $site_size = new Site_Size('file');
     $this->assertNotSame($site_size->filesize($file), 0);
     $this->assertSame($site_size->filesize($file), $file->getSize());
 }
				</th>

				<td class="column-filesize">

					<?php 
if (Site_Size::is_site_size_being_calculated()) {
    ?>

						<span class="spinner is-active"></span>

					<?php 
} else {
    $root = new \SplFileInfo(Path::get_root());
    $size = $site_size->filesize($root);
    $excluded_size = $excluded_site_size->filesize($root);
    $excluded_size = is_same_size_format($size, $excluded_size) ? (int) size_format($excluded_size) : size_format($excluded_size);
    ?>

							<code>
								<?php 
    /* translators: 1: Excluded size 2: Overall site size */
    printf(esc_html__('%1$s of %2$s', 'backupwordpress'), esc_html($excluded_size), esc_html(size_format($size)));
    ?>

								<a class="dashicons dashicons-update" href="<?php 
    echo esc_url(wp_nonce_url(add_query_arg('hmbkp_recalculate_directory_filesize', urlencode(Path::get_root())), 'hmbkp-recalculate_directory_filesize'));
    ?>
">
									<span><?php 
    esc_html_e('Refresh', 'backupwordpress');
				</th>

				<td class="column-filesize">

					<?php 
if (Site_Size::is_site_size_being_calculated()) {
    ?>

						<span class="spinner is-active"></span>

					<?php 
} else {
    $root = new \SplFileInfo(Path::get_root());
    $size = $site_size->filesize($root);
    $excluded_size = $excluded_site_size->filesize($root);
    ?>

							<code>
								<?php 
    $excluded_size = is_same_size_format($size, $excluded_size) ? (int) size_format($excluded_size) : size_format($excluded_size);
    ?>
								<?php 
    echo sprintf(__('%s of %s', 'backupwordpress'), esc_html($excluded_size), esc_html(size_format($size)));
    ?>
								<a class="dashicons dashicons-update" href="<?php 
    echo esc_url(wp_nonce_url(add_query_arg('hmbkp_recalculate_directory_filesize', urlencode(Path::get_root())), 'hmbkp-recalculate_directory_filesize'));
    ?>
"><span><?php 
    _e('Refresh', 'backupwordpress');
    ?>