Remove all custom plural rules and use CLDR plural rule system
[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( __DIR__ . '/Maintenance.php' );
29
30 /**
31 * Maintenance script to remove old or broken uploads from temporary uploaded
32 * file storage and clean up associated database records.
33 *
34 * @ingroup Maintenance
35 */
36 class UploadStashCleanup extends Maintenance {
37
38 public function __construct() {
39 parent::__construct();
40 $this->mDescription = "Clean up abandoned files in temporary uploaded file stash";
41 }
42
43 public function execute() {
44 $repo = RepoGroup::singleton()->getLocalRepo();
45
46 $dbr = $repo->getSlaveDb();
47
48 // how far back should this look for files to delete?
49 global $wgUploadStashMaxAge;
50
51 $this->output( "Getting list of files to clean up...\n" );
52 $res = $dbr->select(
53 'uploadstash',
54 'us_key',
55 'us_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( time() - $wgUploadStashMaxAge ) ),
56 __METHOD__
57 );
58
59 if( !is_object( $res ) || $res->numRows() == 0 ) {
60 $this->output( "No files to cleanup!\n" );
61 // nothing to do.
62 return;
63 }
64
65 // finish the read before starting writes.
66 $keys = array();
67 foreach( $res as $row ) {
68 array_push( $keys, $row->us_key );
69 }
70
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 );
76
77 $i = 0;
78 foreach( $keys as $key ) {
79 $i++;
80 try {
81 $stash->getFile( $key, true );
82 $stash->removeFileNoAuth( $key );
83 } catch ( UploadStashBadPathException $ex ) {
84 $this->output( "Failed removing stashed upload with key: $key\n" );
85 }
86 if ( $i % 100 == 0 ) {
87 $this->output( "$i\n" );
88 }
89 }
90 $this->output( "$i done\n" );
91 }
92 }
93
94 $maintClass = "UploadStashCleanup";
95 require_once( RUN_MAINTENANCE_IF_MAIN );