MediaWiki master
cleanupUploadStash.php
Go to the documentation of this file.
1<?php
29
30require_once __DIR__ . '/Maintenance.php';
31
39
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Clean up abandoned files in temporary uploaded file stash' );
43 $this->setBatchSize( 50 );
44 }
45
46 public function execute() {
47 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
48 $tempRepo = $repo->getTempRepo();
49
50 $dbr = $repo->getReplicaDB();
51
52 // how far back should this look for files to delete?
53 $cutoff = time() - (int)$this->getConfig()->get( MainConfigNames::UploadStashMaxAge );
54
55 $this->output( "Getting list of files to clean up...\n" );
56 $res = $dbr->newSelectQueryBuilder()
57 ->select( 'us_key' )
58 ->from( 'uploadstash' )
59 ->where( $dbr->expr( 'us_timestamp', '<', $dbr->timestamp( $cutoff ) ) )
60 ->caller( __METHOD__ )
61 ->fetchResultSet();
62
63 // Delete all registered stash files...
64 if ( $res->numRows() == 0 ) {
65 $this->output( "No stashed files to cleanup according to the DB.\n" );
66 } else {
67 // finish the read before starting writes.
68 $keys = [];
69 foreach ( $res as $row ) {
70 $keys[] = $row->us_key;
71 }
72
73 $this->output( 'Removing ' . count( $keys ) . " file(s)...\n" );
74 // this could be done some other, more direct/efficient way, but using
75 // UploadStash's own methods means it's less likely to fall accidentally
76 // out-of-date someday
77 $stash = new UploadStash( $repo );
78
79 $i = 0;
80 foreach ( $keys as $key ) {
81 $i++;
82 try {
83 $stash->getFile( $key, true );
84 $stash->removeFileNoAuth( $key );
85 } catch ( UploadStashException $ex ) {
86 $type = get_class( $ex );
87 $this->output( "Failed removing stashed upload with key: $key ($type)\n" );
88 }
89 if ( $i % 100 == 0 ) {
90 $this->waitForReplication();
91 $this->output( "$i\n" );
92 }
93 }
94 $this->output( "$i done\n" );
95 }
96
97 // Delete all the corresponding thumbnails...
98 $dir = $tempRepo->getZonePath( 'thumb' );
99 $iterator = $tempRepo->getBackend()->getFileList( [ 'dir' => $dir, 'adviseStat' => 1 ] );
100 if ( $iterator === null ) {
101 $this->fatalError( "Could not get file listing." );
102 }
103 $this->output( "Deleting old thumbnails...\n" );
104 $i = 0;
105 $batch = [];
106 foreach ( $iterator as $file ) {
107 if ( wfTimestamp( TS_UNIX, $tempRepo->getFileTimestamp( "$dir/$file" ) ) < $cutoff ) {
108 $batch[] = [ 'op' => 'delete', 'src' => "$dir/$file" ];
109 if ( count( $batch ) >= $this->getBatchSize() ) {
110 $this->doOperations( $tempRepo, $batch );
111 $i += count( $batch );
112 $batch = [];
113 $this->output( "$i\n" );
114 }
115 }
116 }
117 if ( count( $batch ) ) {
118 $this->doOperations( $tempRepo, $batch );
119 $i += count( $batch );
120 }
121 $this->output( "$i done\n" );
122
123 // Apparently lots of stash files are not registered in the DB...
124 $dir = $tempRepo->getZonePath( 'public' );
125 $iterator = $tempRepo->getBackend()->getFileList( [ 'dir' => $dir, 'adviseStat' => 1 ] );
126 if ( $iterator === null ) {
127 $this->fatalError( "Could not get file listing." );
128 }
129 $this->output( "Deleting orphaned temp files...\n" );
130 if ( strpos( $dir, '/local-temp' ) === false ) {
131 $this->output( "Temp repo might be misconfigured. It points to directory: '$dir' \n" );
132 }
133
134 $i = 0;
135 $batch = [];
136 foreach ( $iterator as $file ) {
137 if ( wfTimestamp( TS_UNIX, $tempRepo->getFileTimestamp( "$dir/$file" ) ) < $cutoff ) {
138 $batch[] = [ 'op' => 'delete', 'src' => "$dir/$file" ];
139 if ( count( $batch ) >= $this->getBatchSize() ) {
140 $this->doOperations( $tempRepo, $batch );
141 $i += count( $batch );
142 $batch = [];
143 $this->output( "$i\n" );
144 }
145 }
146 }
147 if ( count( $batch ) ) {
148 $this->doOperations( $tempRepo, $batch );
149 $i += count( $batch );
150 }
151 $this->output( "$i done\n" );
152 }
153
154 protected function doOperations( FileRepo $tempRepo, array $ops ) {
155 $status = $tempRepo->getBackend()->doQuickOperations( $ops );
156 if ( !$status->isOK() ) {
157 $this->error( $status );
158 }
159 }
160}
161
162$maintClass = CleanupUploadStash::class;
163require_once RUN_MAINTENANCE_IF_MAIN;
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Maintenance script to remove old or broken uploads from temporary uploaded file storage and clean up ...
execute()
Do the actual work.
doOperations(FileRepo $tempRepo, array $ops)
__construct()
Default constructor.
Base class for file repositories.
Definition FileRepo.php:52
getBackend()
Get the file backend instance.
Definition FileRepo.php:254
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
A class containing constants representing the names of configuration variables.
UploadStash is intended to accomplish a few things: