Debian Codegraph @

maintenance/cleanupUploadStash.php

raw symbols SCIP
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 <[email protected]>
25 * @ingroup Maintenance
26 */
27
28 use MediaWiki\MainConfigNames;
29
30 // @codeCoverageIgnoreStart
31 require_once __DIR__ . '/Maintenance.php';
32 // @codeCoverageIgnoreEnd
33
34 /**
35 * Maintenance script to remove old or broken uploads from temporary uploaded
36 * file storage and clean up associated database records.
37 *
38 * @ingroup Maintenance
39 */
40 class CleanupUploadStash extends Maintenance {
41
42 public function __construct() {
43 parent::__construct();
44 $this->addDescription( 'Clean up abandoned files in temporary uploaded file stash' );
45 $this->setBatchSize( 50 );
46 }
47
48 public function execute() {
49 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
50 $tempRepo = $repo->getTempRepo();
51
52 $dbr = $repo->getReplicaDB();
53
54 // how far back should this look for files to delete?
55 $cutoff = time() - (int)$this->getConfig()->get( MainConfigNames::UploadStashMaxAge );
56
57 $this->output( "Getting list of files to clean up...\n" );
58 $res = $dbr->newSelectQueryBuilder()
59 ->select( 'us_key' )
60 ->from( 'uploadstash' )
61 ->where( $dbr->expr( 'us_timestamp', '<', $dbr->timestamp( $cutoff ) ) )
62 ->caller( __METHOD__ )
63 ->fetchResultSet();
64
65 // Delete all registered stash files...
66 if ( $res->numRows() == 0 ) {
67 $this->output( "No stashed files to cleanup according to the DB.\n" );
68 } else {
69 // finish the read before starting writes.
70 $keys = [];
71 foreach ( $res as $row ) {
72 $keys[] = $row->us_key;
73 }
74
75 $this->output( 'Removing ' . count( $keys ) . " file(s)...\n" );
76 // this could be done some other, more direct/efficient way, but using
77 // UploadStash's own methods means it's less likely to fall accidentally
78 // out-of-date someday
79 $stash = new UploadStash( $repo );
80
81 $i = 0;
82 foreach ( $keys as $key ) {
83 $i++;
84 try {
85 $stash->getFile( $key, true );
86 $stash->removeFileNoAuth( $key );
87 } catch ( UploadStashException $ex ) {
88 $type = get_class( $ex );
89 $this->output( "Failed removing stashed upload with key: $key ($type)\n" );
90 }
91 if ( $i % 100 == 0 ) {
92 $this->waitForReplication();
93 $this->output( "$i\n" );
94 }
95 }
96 $this->output( "$i done\n" );
97 }
98
99 // Delete all the corresponding thumbnails...
100 $dir = $tempRepo->getZonePath( 'thumb' );
101 $iterator = $tempRepo->getBackend()->getFileList( [ 'dir' => $dir, 'adviseStat' => 1 ] );
102 if ( $iterator === null ) {
103 $this->fatalError( "Could not get file listing." );
104 }
105 $this->output( "Deleting old thumbnails...\n" );
106 $i = 0;
107 $batch = [];
108 foreach ( $iterator as $file ) {
109 if ( wfTimestamp( TS_UNIX, $tempRepo->getFileTimestamp( "$dir/$file" ) ) < $cutoff ) {
110 $batch[] = [ 'op' => 'delete', 'src' => "$dir/$file" ];
111 if ( count( $batch ) >= $this->getBatchSize() ) {
112 $this->doOperations( $tempRepo, $batch );
113 $i += count( $batch );
114 $batch = [];
115 $this->output( "$i\n" );
116 }
117 }
118 }
119 if ( count( $batch ) ) {
120 $this->doOperations( $tempRepo, $batch );
121 $i += count( $batch );
122 }
123 $this->output( "$i done\n" );
124
125 // Apparently lots of stash files are not registered in the DB...
126 $dir = $tempRepo->getZonePath( 'public' );
127 $iterator = $tempRepo->getBackend()->getFileList( [ 'dir' => $dir, 'adviseStat' => 1 ] );
128 if ( $iterator === null ) {
129 $this->fatalError( "Could not get file listing." );
130 }
131 $this->output( "Deleting orphaned temp files...\n" );
132 if ( strpos( $dir, '/local-temp' ) === false ) {
133 $this->output( "Temp repo might be misconfigured. It points to directory: '$dir' \n" );
134 }
135
136 $i = 0;
137 $batch = [];
138 foreach ( $iterator as $file ) {
139 if ( wfTimestamp( TS_UNIX, $tempRepo->getFileTimestamp( "$dir/$file" ) ) < $cutoff ) {
140 $batch[] = [ 'op' => 'delete', 'src' => "$dir/$file" ];
141 if ( count( $batch ) >= $this->getBatchSize() ) {
142 $this->doOperations( $tempRepo, $batch );
143 $i += count( $batch );
144 $batch = [];
145 $this->output( "$i\n" );
146 }
147 }
148 }
149 if ( count( $batch ) ) {
150 $this->doOperations( $tempRepo, $batch );
151 $i += count( $batch );
152 }
153 $this->output( "$i done\n" );
154 }
155
156 protected function doOperations( FileRepo $tempRepo, array $ops ) {
157 $status = $tempRepo->getBackend()->doQuickOperations( $ops );
158 if ( !$status->isOK() ) {
159 $this->error( $status );
160 }
161 }
162 }
163
164 // @codeCoverageIgnoreStart
165 $maintClass = CleanupUploadStash::class;
166 require_once RUN_MAINTENANCE_IF_MAIN;
167 // @codeCoverageIgnoreEnd