3 * Remove old or broken uploads from temporary uploaded file storage,
4 * clean up associated database records
6 * Copyright © 2011, Wikimedia Foundation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
24 * @author Ian Baker <ibaker@wikimedia.org>
25 * @ingroup Maintenance
28 require_once( dirname( __FILE__
) . '/Maintenance.php' );
31 * Maintenance script to remove old or broken uploads from temporary uploaded
32 * file storage and clean up associated database records.
34 * @ingroup Maintenance
36 class UploadStashCleanup
extends Maintenance
{
38 public function __construct() {
39 parent
::__construct();
40 $this->mDescription
= "Clean up abandoned files in temporary uploaded file stash";
43 public function execute() {
44 $repo = RepoGroup
::singleton()->getLocalRepo();
46 $dbr = $repo->getSlaveDb();
48 // how far back should this look for files to delete?
49 global $wgUploadStashMaxAge;
51 $this->output( "Getting list of files to clean up...\n" );
55 'us_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( time() - $wgUploadStashMaxAge ) ),
59 if( !is_object( $res ) ||
$res->numRows() == 0 ) {
60 $this->output( "No files to cleanup!\n" );
65 // finish the read before starting writes.
67 foreach( $res as $row ) {
68 array_push( $keys, $row->us_key
);
71 $this->output( 'Removing ' . count($keys) . " file(s)...\n" );
72 // this could be done some other, more direct/efficient way, but using
73 // UploadStash's own methods means it's less likely to fall accidentally
74 // out-of-date someday
75 $stash = new UploadStash( $repo );
78 foreach( $keys as $key ) {
81 $stash->getFile( $key, true );
82 $stash->removeFileNoAuth( $key );
83 } catch ( UploadStashBadPathException
$ex ) {
84 $this->output( "Failed removing stashed upload with key: $key\n" );
86 if ( $i %
100 == 0 ) {
87 $this->output( "$i\n" );
90 $this->output( "$i done\n" );
94 $maintClass = "UploadStashCleanup";
95 require_once( RUN_MAINTENANCE_IF_MAIN
);