MediaWiki  REL1_20
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                 $repo = RepoGroup::singleton()->getLocalRepo();
00045 
00046                 $dbr = $repo->getSlaveDb();
00047 
00048                 // how far back should this look for files to delete?
00049                 global $wgUploadStashMaxAge;
00050 
00051                 $this->output( "Getting list of files to clean up...\n" );
00052                 $res = $dbr->select(
00053                         'uploadstash',
00054                         'us_key',
00055                         'us_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( time() - $wgUploadStashMaxAge ) ),
00056                         __METHOD__
00057                 );
00058 
00059                 if( !is_object( $res ) || $res->numRows() == 0 ) {
00060                         $this->output( "No files to cleanup!\n" );
00061                         // nothing to do.
00062                         return;
00063                 }
00064 
00065                 // finish the read before starting writes.
00066                 $keys = array();
00067                 foreach( $res as $row ) {
00068                         array_push( $keys, $row->us_key );
00069                 }
00070 
00071                 $this->output( 'Removing ' . count($keys) . " file(s)...\n" );
00072                 // this could be done some other, more direct/efficient way, but using
00073                 // UploadStash's own methods means it's less likely to fall accidentally
00074                 // out-of-date someday
00075                 $stash = new UploadStash( $repo );
00076 
00077                 $i = 0;
00078                 foreach( $keys as $key ) {
00079                         $i++;
00080                         try {
00081                                 $stash->getFile( $key, true );
00082                                 $stash->removeFileNoAuth( $key );
00083                         } catch ( UploadStashBadPathException $ex ) {
00084                                 $this->output( "Failed removing stashed upload with key: $key\n"  );
00085                         }
00086                         if ( $i % 100 == 0 ) {
00087                                 $this->output( "$i\n" );
00088                         }
00089                 }
00090                 $this->output( "$i done\n" );
00091         }
00092 }
00093 
00094 $maintClass = "UploadStashCleanup";
00095 require_once( RUN_MAINTENANCE_IF_MAIN );