Ticket #30202: 30202.3.diff
File 30202.3.diff, 6.4 KB (added by , 10 years ago) |
---|
-
src/wp-includes/ms-functions.php
1686 1686 * 1687 1687 * @since MU 1688 1688 * 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 */ 1693 function 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. 1694 1703 if ( is_array( $dirsize ) && isset( $dirsize[ $directory ][ 'size' ] ) ) 1695 1704 return $dirsize[ $directory ][ 'size' ]; 1696 1705 1697 1706 if ( ! is_array( $dirsize ) ) 1698 1707 $dirsize = array(); 1699 1708 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 } 1701 1717 1702 set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );1703 1718 return $dirsize[ $directory ][ 'size' ]; 1704 1719 } 1705 1720 … … 1711 1726 * 1712 1727 * @since MU 1713 1728 * 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. 1716 1732 */ 1717 function recurse_dirsize( $directory ) {1733 function recurse_dirsize( $directory, $exclude = null ) { 1718 1734 $size = 0; 1719 1735 1720 1736 $directory = untrailingslashit( $directory ); 1721 1737 1722 if ( ! file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) )1738 if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) || $directory === $exclude ) { 1723 1739 return false; 1740 } 1724 1741 1725 1742 if ($handle = opendir($directory)) { 1726 1743 while(($file = readdir($handle)) !== false) { … … 1729 1746 if (is_file($path)) { 1730 1747 $size += filesize($path); 1731 1748 } elseif (is_dir($path)) { 1732 $handlesize = recurse_dirsize( $path);1749 $handlesize = recurse_dirsize( $path, $exclude ); 1733 1750 if ($handlesize > 0) 1734 1751 $size += $handlesize; 1735 1752 } … … 2301 2318 $space_used = apply_filters( 'pre_get_space_used', false ); 2302 2319 if ( false === $space_used ) { 2303 2320 $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; 2305 2323 } 2306 2324 2307 2325 return $space_used; -
tests/phpunit/tests/multisite/getSpaceUsed.php
1 <?php 2 3 if ( is_multisite() ) : 4 5 /** 6 * @group space-used 7 * @group multisite 8 * @covers ::get_space_used 9 */ 10 class 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 } 95 endif; 96 No newline at end of file