MediaWiki REL1_39
cleanupUploadStash.php
Go to the documentation of this file.
1<?php
30
31require_once __DIR__ . '/Maintenance.php';
32
40
41 public function __construct() {
42 parent::__construct();
43 $this->addDescription( 'Clean up abandoned files in temporary uploaded file stash' );
44 $this->setBatchSize( 50 );
45 }
46
47 public function execute() {
48 $repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
49 $tempRepo = $repo->getTempRepo();
50
51 $dbr = $repo->getReplicaDB();
52
53 // how far back should this look for files to delete?
54 $cutoff = time() - (int)$this->getConfig()->get( MainConfigNames::UploadStashMaxAge );
55
56 $this->output( "Getting list of files to clean up...\n" );
57 $res = $dbr->newSelectQueryBuilder()
58 ->select( 'us_key' )
59 ->from( 'uploadstash' )
60 ->where( 'us_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $cutoff ) ) )
61 ->caller( __METHOD__ )
62 ->fetchResultSet();
63
64 // Delete all registered stash files...
65 if ( $res->numRows() == 0 ) {
66 $this->output( "No stashed files to cleanup according to the DB.\n" );
67 } else {
68 // finish the read before starting writes.
69 $keys = [];
70 foreach ( $res as $row ) {
71 array_push( $keys, $row->us_key );
72 }
73
74 $this->output( 'Removing ' . count( $keys ) . " file(s)...\n" );
75 // this could be done some other, more direct/efficient way, but using
76 // UploadStash's own methods means it's less likely to fall accidentally
77 // out-of-date someday
78 $stash = new UploadStash( $repo );
79
80 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
81
82 $i = 0;
83 foreach ( $keys as $key ) {
84 $i++;
85 try {
86 $stash->getFile( $key, true );
87 $stash->removeFileNoAuth( $key );
88 } catch ( UploadStashException $ex ) {
89 $type = get_class( $ex );
90 $this->output( "Failed removing stashed upload with key: $key ($type)\n" );
91 }
92 if ( $i % 100 == 0 ) {
93 $lbFactory->waitForReplication();
94 $this->output( "$i\n" );
95 }
96 }
97 $this->output( "$i done\n" );
98 }
99
100 // Delete all the corresponding thumbnails...
101 $dir = $tempRepo->getZonePath( 'thumb' );
102 $iterator = $tempRepo->getBackend()->getFileList( [ 'dir' => $dir, 'adviseStat' => 1 ] );
103 if ( $iterator === null ) {
104 $this->fatalError( "Could not get file listing." );
105 }
106 $this->output( "Deleting old thumbnails...\n" );
107 $i = 0;
108 $batch = [];
109 foreach ( $iterator as $file ) {
110 if ( wfTimestamp( TS_UNIX, $tempRepo->getFileTimestamp( "$dir/$file" ) ) < $cutoff ) {
111 $batch[] = [ 'op' => 'delete', 'src' => "$dir/$file" ];
112 if ( count( $batch ) >= $this->getBatchSize() ) {
113 $this->doOperations( $tempRepo, $batch );
114 $i += count( $batch );
115 $batch = [];
116 $this->output( "$i\n" );
117 }
118 }
119 }
120 if ( count( $batch ) ) {
121 $this->doOperations( $tempRepo, $batch );
122 $i += count( $batch );
123 }
124 $this->output( "$i done\n" );
125
126 // Apparently lots of stash files are not registered in the DB...
127 $dir = $tempRepo->getZonePath( 'public' );
128 $iterator = $tempRepo->getBackend()->getFileList( [ 'dir' => $dir, 'adviseStat' => 1 ] );
129 if ( $iterator === null ) {
130 $this->fatalError( "Could not get file listing." );
131 }
132 $this->output( "Deleting orphaned temp files...\n" );
133 if ( strpos( $dir, '/local-temp' ) === false ) {
134 $this->fatalError( "Temp repo is not using the temp container." );
135 }
136
137 $i = 0;
138 $batch = [];
139 foreach ( $iterator as $file ) {
140 if ( wfTimestamp( TS_UNIX, $tempRepo->getFileTimestamp( "$dir/$file" ) ) < $cutoff ) {
141 $batch[] = [ 'op' => 'delete', 'src' => "$dir/$file" ];
142 if ( count( $batch ) >= $this->getBatchSize() ) {
143 $this->doOperations( $tempRepo, $batch );
144 $i += count( $batch );
145 $batch = [];
146 $this->output( "$i\n" );
147 }
148 }
149 }
150 if ( count( $batch ) ) {
151 $this->doOperations( $tempRepo, $batch );
152 $i += count( $batch );
153 }
154 $this->output( "$i done\n" );
155 }
156
157 protected function doOperations( FileRepo $tempRepo, array $ops ) {
158 $status = $tempRepo->getBackend()->doQuickOperations( $ops );
159 if ( !$status->isOK() ) {
160 $this->error( print_r( Status::wrap( $status )->getErrorsArray(), true ) );
161 }
162 }
163}
164
165$maintClass = CleanupUploadStash::class;
166require_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:47
getBackend()
Get the file backend instance.
Definition FileRepo.php:250
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.
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.
Service locator for MediaWiki core services.
UploadStash is intended to accomplish a few things:
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42