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' );
30 class UploadStashCleanup
extends Maintenance
{
32 public function __construct() {
33 parent
::__construct();
34 $this->mDescription
= "Clean up abandoned files in temporary uploaded file stash";
37 public function execute() {
38 $repo = RepoGroup
::singleton()->getLocalRepo();
40 $dbr = $repo->getSlaveDb();
42 // how far back should this look for files to delete?
43 global $wgUploadStashMaxAge;
45 $this->output( "Getting list of files to clean up...\n" );
49 'us_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( time() - $wgUploadStashMaxAge ) ),
53 if( !is_object( $res ) ||
$res->numRows() == 0 ) {
54 $this->output( "No files to cleanup!\n" );
59 // finish the read before starting writes.
61 foreach( $res as $row ) {
62 array_push( $keys, $row->us_key
);
65 $this->output( 'Removing ' . count($keys) . " file(s)...\n" );
66 // this could be done some other, more direct/efficient way, but using
67 // UploadStash's own methods means it's less likely to fall accidentally
68 // out-of-date someday
69 $stash = new UploadStash( $repo );
72 foreach( $keys as $key ) {
75 $stash->getFile( $key, true );
76 $stash->removeFileNoAuth( $key );
77 } catch ( UploadStashBadPathException
$ex ) {
78 $this->output( "Failed removing stashed upload with key: $key\n" );
80 if ( $i %
100 == 0 ) {
81 $this->output( "$i\n" );
84 $this->output( "$i done\n" );
88 $maintClass = "UploadStashCleanup";
89 require_once( RUN_MAINTENANCE_IF_MAIN
);