30 require_once __DIR__ .
'/Maintenance.php';
41 parent::__construct();
42 $this->
addDescription(
'Clean up abandoned files in temporary uploaded file stash' );
49 $repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
50 $tempRepo = $repo->getTempRepo();
52 $dbr = $repo->getReplicaDB();
57 $this->
output(
"Getting list of files to clean up...\n" );
61 'us_timestamp < ' .
$dbr->addQuotes(
$dbr->timestamp( $cutoff ) ),
66 if (
$res->numRows() == 0 ) {
67 $this->
output(
"No stashed files to cleanup according to the DB.\n" );
71 foreach (
$res as $row ) {
72 array_push(
$keys, $row->us_key );
75 $this->
output(
'Removing ' . count(
$keys ) .
" file(s)...\n" );
81 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
84 foreach (
$keys as $key ) {
87 $stash->getFile( $key,
true );
88 $stash->removeFileNoAuth( $key );
90 $type = get_class( $ex );
91 $this->
output(
"Failed removing stashed upload with key: $key ($type)\n" );
93 if ( $i % 100 == 0 ) {
94 $lbFactory->waitForReplication();
98 $this->
output(
"$i done\n" );
102 $dir = $tempRepo->getZonePath(
'thumb' );
103 $iterator = $tempRepo->getBackend()->getFileList( [
'dir' => $dir,
'adviseStat' => 1 ] );
104 if ( $iterator ===
null ) {
105 $this->
fatalError(
"Could not get file listing." );
107 $this->
output(
"Deleting old thumbnails...\n" );
110 foreach ( $iterator as
$file ) {
111 if (
wfTimestamp( TS_UNIX, $tempRepo->getFileTimestamp(
"$dir/$file" ) ) < $cutoff ) {
112 $batch[] = [
'op' =>
'delete',
'src' =>
"$dir/$file" ];
115 $i += count( $batch );
121 if ( count( $batch ) ) {
123 $i += count( $batch );
125 $this->
output(
"$i done\n" );
128 $dir = $tempRepo->getZonePath(
'public' );
129 $iterator = $tempRepo->getBackend()->getFileList( [
'dir' => $dir,
'adviseStat' => 1 ] );
130 if ( $iterator ===
null ) {
131 $this->
fatalError(
"Could not get file listing." );
133 $this->
output(
"Deleting orphaned temp files...\n" );
134 if ( strpos( $dir,
'/local-temp' ) ===
false ) {
135 $this->
fatalError(
"Temp repo is not using the temp container." );
140 foreach ( $iterator as
$file ) {
141 if (
wfTimestamp( TS_UNIX, $tempRepo->getFileTimestamp(
"$dir/$file" ) ) < $cutoff ) {
142 $batch[] = [
'op' =>
'delete',
'src' =>
"$dir/$file" ];
145 $i += count( $batch );
151 if ( count( $batch ) ) {
153 $i += count( $batch );
155 $this->
output(
"$i done\n" );
159 $status = $tempRepo->
getBackend()->doQuickOperations( $ops );
160 if ( !$status->isOK() ) {
167 require_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.
getBackend()
Get the file backend instance.
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
UploadStash is intended to accomplish a few things:
$wgUploadStashMaxAge
Config variable stub for the UploadStashMaxAge setting, for use by phpdoc and IDEs.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.