Make WordPress Core

Ticket #30202: 30202.3.diff

File 30202.3.diff, 6.4 KB (added by jeremyfelt, 10 years ago)
  • src/wp-includes/ms-functions.php

     
    16861686 *
    16871687 * @since MU
    16881688 *
    1689  * @param string $directory
    1690  * @return int
    1691  */
    1692 function get_dirsize( $directory ) {
    1693         $dirsize = get_transient( 'dirsize_cache' );
     1689 * @param string $directory The full path of a directory to check size of.
     1690 * @param string $exclude   Optional. Full path of a subdirectory to exclude from the total.
     1691 * @return int Size of the directory in MB
     1692 */
     1693function get_dirsize( $directory, $exclude = null  ) {
     1694        // Retrieve dirsize from transient if no exclude parameter was passed or if this is
     1695        // the main site and a specific exclude parameter used by core is passed.
     1696        if ( is_null( $exclude ) || ( is_main_site() && $directory . '/sites' === $exclude ) ) {
     1697                $dirsize = get_transient( 'dirsize_cache' );
     1698        } else {
     1699                $dirsize = false;
     1700        }
     1701
     1702        // If a cached value is available matching this directory, return it immediately.
    16941703        if ( is_array( $dirsize ) && isset( $dirsize[ $directory ][ 'size' ] ) )
    16951704                return $dirsize[ $directory ][ 'size' ];
    16961705
    16971706        if ( ! is_array( $dirsize ) )
    16981707                $dirsize = array();
    16991708
    1700         $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
     1709        // No cached value is available, calculate the size recursively.
     1710        $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory, $exclude );
     1711
     1712        // Cache the dirsize if no exclude parameter was passed or if this is the main site
     1713        // and a specific exclude parameter used by core is passed.
     1714        if ( is_null( $exclude ) || ( is_main_site() && $directory . '/sites' === $exclude ) ) {
     1715                set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
     1716        }
    17011717
    1702         set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
    17031718        return $dirsize[ $directory ][ 'size' ];
    17041719}
    17051720
     
    17111726 *
    17121727 * @since MU
    17131728 *
    1714  * @param string $directory
    1715  * @return int|false
     1729 * @param string $directory The full path of a directory to check the size of.
     1730 * @param string $exclude   Optional. The full path of a subdirectory to exclude from the total.
     1731 * @return int|false Size in MB if a valid directory. False if not.
    17161732 */
    1717 function recurse_dirsize( $directory ) {
     1733function recurse_dirsize( $directory, $exclude = null ) {
    17181734        $size = 0;
    17191735
    17201736        $directory = untrailingslashit( $directory );
    17211737
    1722         if ( !file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) )
     1738        if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) || $directory === $exclude ) {
    17231739                return false;
     1740        }
    17241741
    17251742        if ($handle = opendir($directory)) {
    17261743                while(($file = readdir($handle)) !== false) {
     
    17291746                                if (is_file($path)) {
    17301747                                        $size += filesize($path);
    17311748                                } elseif (is_dir($path)) {
    1732                                         $handlesize = recurse_dirsize($path);
     1749                                        $handlesize = recurse_dirsize( $path, $exclude );
    17331750                                        if ($handlesize > 0)
    17341751                                                $size += $handlesize;
    17351752                                }
     
    23012318        $space_used = apply_filters( 'pre_get_space_used', false );
    23022319        if ( false === $space_used ) {
    23032320                $upload_dir = wp_upload_dir();
    2304                 $space_used = get_dirsize( $upload_dir['basedir'] ) / 1024 / 1024;
     2321                $exclude = ( is_main_site() ) ? $upload_dir['basedir'] . '/sites' : null;
     2322                $space_used = get_dirsize( $upload_dir['basedir'], $exclude ) / 1024 / 1024;
    23052323        }
    23062324
    23072325        return $space_used;
  • tests/phpunit/tests/multisite/getSpaceUsed.php

     
     1<?php
     2
     3if ( is_multisite() ) :
     4
     5/**
     6 * @group space-used
     7 * @group multisite
     8 * @covers ::get_space_used
     9 */
     10class Tests_Multisite_Get_Space_Used extends WP_UnitTestCase {
     11        protected $suppress = false;
     12
     13        function setUp() {
     14                global $wpdb;
     15                parent::setUp();
     16                $this->suppress = $wpdb->suppress_errors();
     17
     18                $_SERVER['REMOTE_ADDR'] = '';
     19        }
     20
     21        function tearDown() {
     22                global $wpdb;
     23                $wpdb->suppress_errors( $this->suppress );
     24                parent::tearDown();
     25        }
     26
     27        function test_get_space_used_switched_site() {
     28                $blog_id = $this->factory->blog->create();
     29                switch_to_blog( $blog_id );
     30
     31                // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
     32                // src directory already contains a content directory with site content, then the initial expectation
     33                // will be polluted. We create sites until an empty one is available.
     34                while ( 0 != get_space_used() ) {
     35                        restore_current_blog();
     36                        $blog_id = $this->factory->blog->create();
     37                        switch_to_blog( $blog_id );
     38                }
     39
     40                // Upload a file to the new site.
     41                $filename = rand_str().'.jpg';
     42                $contents = rand_str();
     43                $file = wp_upload_bits( $filename, null, $contents );
     44
     45                // get_space_used() is measures in MB, get the size of the new file in MB.
     46                $size = filesize( $file['file'] ) / 1024 / 1024;
     47
     48                delete_transient( 'dirsize_cache' );
     49
     50                $this->assertEquals( $size, get_space_used() );
     51                $upload_dir = wp_upload_dir();
     52                $this->remove_added_uploads();
     53                $this->delete_folders( $upload_dir['basedir'] );
     54                restore_current_blog();
     55        }
     56
     57        /**
     58         * Directories of sub sites on a network should not count against the same spaced used total for
     59         * the main site.
     60         */
     61        function test_get_space_used_main_site() {
     62                $space_used = get_space_used();
     63
     64                $blog_id = $this->factory->blog->create();
     65                switch_to_blog( $blog_id );
     66
     67                // We don't rely on an initial value of 0 for space used, but should have a clean space available
     68                // so that we can remove any uploaded files and directories without concern of a conflict with
     69                // existing content directories in src.
     70                while ( 0 != get_space_used() ) {
     71                        restore_current_blog();
     72                        $blog_id = $this->factory->blog->create();
     73                        switch_to_blog( $blog_id );
     74                }
     75
     76                // Upload a file to the new site.
     77                $filename = rand_str().'.jpg';
     78                $contents = rand_str();
     79                wp_upload_bits( $filename, null, $contents );
     80
     81                restore_current_blog();
     82
     83                delete_transient( 'dirsize_cache' );
     84
     85                $this->assertEquals( $space_used, get_space_used() );
     86
     87                // Switch back to the new site to remove the uploaded file.
     88                switch_to_blog( $blog_id );
     89                $upload_dir = wp_upload_dir();
     90                $this->remove_added_uploads();
     91                $this->delete_folders( $upload_dir['basedir'] );
     92                restore_current_blog();
     93        }
     94}
     95endif;
     96 No newline at end of file