getBools doesn't exist in Translate anymore
[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 global $wgUploadStashMaxAge;
45
46 $repo = RepoGroup::singleton()->getLocalRepo();
47 $tempRepo = $repo->getTempRepo();
48
49 $dbr = $repo->getSlaveDb();
50
51 // how far back should this look for files to delete?
52 $cutoff = time() - $wgUploadStashMaxAge;
53
54 $this->output( "Getting list of files to clean up...\n" );
55 $res = $dbr->select(
56 'uploadstash',
57 'us_key',
58 'us_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $cutoff ) ),
59 __METHOD__
60 );
61
62 // Delete all registered stash files...
63 if ( $res->numRows() == 0 ) {
64 $this->output( "No stashed files to cleanup according to the DB.\n" );
65 } else {
66 // finish the read before starting writes.
67 $keys = array();
68 foreach( $res as $row ) {
69 array_push( $keys, $row->us_key );
70 }
71
72 $this->output( 'Removing ' . count( $keys ) . " file(s)...\n" );
73 // this could be done some other, more direct/efficient way, but using
74 // UploadStash's own methods means it's less likely to fall accidentally
75 // out-of-date someday
76 $stash = new UploadStash( $repo );
77
78 $i = 0;
79 foreach( $keys as $key ) {
80 $i++;
81 try {
82 $stash->getFile( $key, true );
83 $stash->removeFileNoAuth( $key );
84 } catch ( UploadStashBadPathException $ex ) {
85 $this->output( "Failed removing stashed upload with key: $key\n" );
86 } catch ( UploadStashZeroLengthFileException $ex ) {
87 $this->output( "Failed removing stashed upload with key: $key\n" );
88 }
89 if ( $i % 100 == 0 ) {
90 $this->output( "$i\n" );
91 }
92 }
93 $this->output( "$i done\n" );
94 }
95
96 // Delete all the corresponding thumbnails...
97 $dir = $tempRepo->getZonePath( 'thumb' );
98 $iterator = $tempRepo->getBackend()->getFileList( array( 'dir' => $dir ) );
99 $this->output( "Deleting old thumbnails...\n" );
100 $i = 0;
101 foreach ( $iterator as $file ) {
102 if ( wfTimestamp( TS_UNIX, $tempRepo->getFileTimestamp( "$dir/$file" ) ) < $cutoff ) {
103 $status = $tempRepo->quickPurge( "$dir/$file" );
104 if ( !$status->isOK() ) {
105 $this->error( print_r( $status->getErrorsArray(), true ) );
106 }
107 if ( ( ++$i % 100 ) == 0 ) {
108 $this->output( "$i\n" );
109 }
110 }
111 }
112 $this->output( "$i done\n" );
113
114 // Apparently lots of stash files are not registered in the DB...
115 $dir = $tempRepo->getZonePath( 'public' );
116 $iterator = $tempRepo->getBackend()->getFileList( array( 'dir' => $dir ) );
117 $this->output( "Deleting orphaned temp files...\n" );
118 if ( strpos( $dir, '/local-temp' ) === false ) { // sanity check
119 $this->error( "Temp repo is not using the temp container.", 1 ); // die
120 }
121 $i = 0;
122 foreach ( $iterator as $file ) {
123 // Absolute sanity check for stashed files and file segments
124 if ( !preg_match( '#(^\d{14}!|\.\d+\.\w+\.\d+$)#', basename( $file ) ) ) {
125 $this->output( "Skipped non-stash $file\n" );
126 continue;
127 }
128 if ( wfTimestamp( TS_UNIX, $tempRepo->getFileTimestamp( "$dir/$file" ) ) < $cutoff ) {
129 $status = $tempRepo->quickPurge( "$dir/$file" );
130 if ( !$status->isOK() ) {
131 $this->error( print_r( $status->getErrorsArray(), true ) );
132 }
133 if ( ( ++$i % 100 ) == 0 ) {
134 $this->output( "$i\n" );
135 }
136 }
137 }
138 $this->output( "$i done\n" );
139 }
140 }
141
142 $maintClass = "UploadStashCleanup";
143 require_once( RUN_MAINTENANCE_IF_MAIN );