pruneFileCache.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Prune file cache for pages, objects, resources, etc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Maintenance
  22. */
  23. require_once __DIR__ . '/Maintenance.php';
  24. /**
  25. * Maintenance script that prunes file cache for pages, objects, resources, etc.
  26. *
  27. * @ingroup Maintenance
  28. */
  29. class PruneFileCache extends Maintenance {
  30. protected $minSurviveTimestamp;
  31. public function __construct() {
  32. parent::__construct();
  33. $this->addDescription( 'Build file cache for content pages' );
  34. $this->addOption( 'agedays', 'How many days old files must be in order to delete', true, true );
  35. $this->addOption( 'subdir', 'Prune one $wgFileCacheDirectory subdirectory name', false, true );
  36. }
  37. public function execute() {
  38. global $wgUseFileCache, $wgFileCacheDirectory;
  39. if ( !$wgUseFileCache ) {
  40. $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
  41. }
  42. $age = $this->getOption( 'agedays' );
  43. if ( !ctype_digit( $age ) ) {
  44. $this->error( "Non-integer 'age' parameter given.", true );
  45. }
  46. // Delete items with a TS older than this
  47. $this->minSurviveTimestamp = time() - ( 86400 * $age );
  48. $dir = $wgFileCacheDirectory;
  49. if ( !is_dir( $dir ) ) {
  50. $this->error( "Nothing to do -- \$wgFileCacheDirectory directory not found.", true );
  51. }
  52. $subDir = $this->getOption( 'subdir' );
  53. if ( $subDir !== null ) {
  54. if ( !is_dir( "$dir/$subDir" ) ) {
  55. $this->error( "The specified subdirectory `$subDir` does not exist.", true );
  56. }
  57. $this->output( "Pruning `$dir/$subDir` directory...\n" );
  58. $this->prune_directory( "$dir/$subDir", 'report' );
  59. $this->output( "Done pruning `$dir/$subDir` directory\n" );
  60. } else {
  61. $this->output( "Pruning `$dir` directory...\n" );
  62. // Note: don't prune things like .cdb files on the top level!
  63. $this->prune_directory( $dir, 'report' );
  64. $this->output( "Done pruning `$dir` directory\n" );
  65. }
  66. }
  67. /**
  68. * @param string $dir
  69. * @param string|bool $report Use 'report' to report the directories being scanned
  70. */
  71. protected function prune_directory( $dir, $report = false ) {
  72. $tsNow = time();
  73. $dirHandle = opendir( $dir );
  74. while ( false !== ( $file = readdir( $dirHandle ) ) ) {
  75. // Skip ".", "..", and also any dirs or files like ".svn" or ".htaccess"
  76. if ( $file[0] != "." ) {
  77. $path = $dir . '/' . $file; // absolute
  78. if ( is_dir( $path ) ) {
  79. if ( $report === 'report' ) {
  80. $this->output( "Scanning `$path`...\n" );
  81. }
  82. $this->prune_directory( $path );
  83. } else {
  84. $mts = filemtime( $path );
  85. // Sanity check the file extension against known cache types
  86. if ( $mts < $this->minSurviveTimestamp
  87. && preg_match( '/\.(?:html|cache)(?:\.gz)?$/', $file )
  88. && unlink( $path )
  89. ) {
  90. $daysOld = round( ( $tsNow - $mts ) / 86400, 2 );
  91. $this->output( "Deleted `$path` [days=$daysOld]\n" );
  92. }
  93. }
  94. }
  95. }
  96. closedir( $dirHandle );
  97. }
  98. }
  99. $maintClass = "PruneFileCache";
  100. require_once RUN_MAINTENANCE_IF_MAIN;