Apply jdanni's patch from from 2+ years ago for Bug 15218 - LinkSearch results should...
[lhc/web/wiklou.git] / maintenance / cleanupUploadStash.php
1 <?php
2 /**
3 * Remove old or broken uploads from temporary uploaded file storage,
4 * clean up associated database records
5 *
6 * Copyright © 2011, Wikimedia Foundation
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 * @author Ian Baker <ibaker@wikimedia.org>
25 * @ingroup Maintenance
26 */
27
28 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
29
30 class UploadStashCleanup extends Maintenance {
31
32 public function __construct() {
33 parent::__construct();
34 $this->mDescription = "Clean up abandoned files in temporary uploaded file stash";
35 }
36
37 public function execute() {
38 $repo = RepoGroup::singleton()->getLocalRepo();
39
40 $dbr = $repo->getSlaveDb();
41
42 // how far back should this look for files to delete?
43 global $wgUploadStashMaxAge;
44
45 $this->output( "Getting list of files to clean up...\n" );
46 $res = $dbr->select(
47 'uploadstash',
48 'us_key',
49 'us_timestamp < ' . $dbr->timestamp( time() - $wgUploadStashMaxAge ),
50 __METHOD__
51 );
52
53 if( !is_object( $res ) || $res->numRows() == 0 ) {
54 $this->output( 'No files to cleanup!' );
55 // nothing to do.
56 return;
57 }
58
59 // finish the read before starting writes.
60 $keys = array();
61 foreach($res as $row) {
62 array_push( $keys, $row->us_key );
63 }
64
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 );
70
71 foreach( $keys as $key ) {
72 $stash->getFile( $key, true );
73 $stash->removeFileNoAuth( $key );
74 }
75 }
76 }
77
78 $maintClass = "UploadStashCleanup";
79 require_once( RUN_MAINTENANCE_IF_MAIN );