MediaWiki  master
cleanupUploadStash.php
Go to the documentation of this file.
00001 <?php
00028 require_once( __DIR__ . '/Maintenance.php' );
00029 
00036 class UploadStashCleanup extends Maintenance {
00037 
00038         public function __construct() {
00039                 parent::__construct();
00040                 $this->mDescription = "Clean up abandoned files in temporary uploaded file stash";
00041         }
00042 
00043         public function execute() {
00044                 global $wgUploadStashMaxAge;
00045 
00046                 $repo = RepoGroup::singleton()->getLocalRepo();
00047 
00048                 $dbr = $repo->getSlaveDb();
00049 
00050                 // how far back should this look for files to delete?
00051                 $cutoff = time() - $wgUploadStashMaxAge;
00052 
00053                 $this->output( "Getting list of files to clean up...\n" );
00054                 $res = $dbr->select(
00055                         'uploadstash',
00056                         'us_key',
00057                         'us_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $cutoff ) ),
00058                         __METHOD__
00059                 );
00060 
00061                 if( !is_object( $res ) || $res->numRows() == 0 ) {
00062                         $this->output( "No files to cleanup!\n" );
00063                         // nothing to do.
00064                         return;
00065                 }
00066 
00067                 // finish the read before starting writes.
00068                 $keys = array();
00069                 foreach( $res as $row ) {
00070                         array_push( $keys, $row->us_key );
00071                 }
00072 
00073                 $this->output( 'Removing ' . count( $keys ) . " file(s)...\n" );
00074                 // this could be done some other, more direct/efficient way, but using
00075                 // UploadStash's own methods means it's less likely to fall accidentally
00076                 // out-of-date someday
00077                 $stash = new UploadStash( $repo );
00078 
00079                 $i = 0;
00080                 foreach( $keys as $key ) {
00081                         $i++;
00082                         try {
00083                                 $stash->getFile( $key, true );
00084                                 $stash->removeFileNoAuth( $key );
00085                         } catch ( UploadStashBadPathException $ex ) {
00086                                 $this->output( "Failed removing stashed upload with key: $key\n"  );
00087                         } catch ( UploadStashZeroLengthFileException $ex ) {
00088                                 $this->output( "Failed removing stashed upload with key: $key\n"  );
00089                         }
00090                         if ( $i % 100 == 0 ) {
00091                                 $this->output( "$i\n" );
00092                         }
00093                 }
00094                 $this->output( "$i done\n" );
00095 
00096                 $tempRepo = $repo->getTempRepo();
00097                 $dir      = $tempRepo->getZonePath( 'thumb' );
00098                 $iterator = $tempRepo->getBackend()->getFileList( array( 'dir' => $dir ) );
00099 
00100                 $this->output( "Deleting old thumbnails...\n" );
00101                 $i = 0;
00102                 foreach ( $iterator as $file ) {
00103                         $i++;
00104                         if ( wfTimestamp( TS_UNIX, $tempRepo->getFileTimestamp( "$dir/$file" ) ) < $cutoff ) {
00105                                 $tempRepo->quickPurge( "$dir/$file" );
00106                         }
00107                         if ( $i % 100 == 0 ) {
00108                                 $this->output( "$i\n" );
00109                         }
00110                 }
00111                 $this->output( "$i done\n" );
00112         }
00113 }
00114 
00115 $maintClass = "UploadStashCleanup";
00116 require_once( RUN_MAINTENANCE_IF_MAIN );