31require_once __DIR__ .
'/Maintenance.php';
42 parent::__construct();
43 $this->
addDescription(
'Clean up abandoned files in temporary uploaded file stash' );
49 $tempRepo = $repo->getTempRepo();
51 $dbr = $repo->getReplicaDB();
54 $cutoff = time() - (int)$this->
getConfig()->get( MainConfigNames::UploadStashMaxAge );
56 $this->
output(
"Getting list of files to clean up...\n" );
57 $res = $dbr->newSelectQueryBuilder()
59 ->from(
'uploadstash' )
60 ->where(
'us_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $cutoff ) ) )
61 ->caller( __METHOD__ )
65 if ( $res->numRows() == 0 ) {
66 $this->
output(
"No stashed files to cleanup according to the DB.\n" );
70 foreach ( $res as $row ) {
71 $keys[] = $row->us_key;
74 $this->
output(
'Removing ' . count( $keys ) .
" file(s)...\n" );
81 foreach ( $keys as $key ) {
84 $stash->getFile( $key,
true );
85 $stash->removeFileNoAuth( $key );
87 $type = get_class( $ex );
88 $this->
output(
"Failed removing stashed upload with key: $key ($type)\n" );
90 if ( $i % 100 == 0 ) {
95 $this->
output(
"$i done\n" );
99 $dir = $tempRepo->getZonePath(
'thumb' );
100 $iterator = $tempRepo->getBackend()->getFileList( [
'dir' => $dir,
'adviseStat' => 1 ] );
101 if ( $iterator ===
null ) {
102 $this->
fatalError(
"Could not get file listing." );
104 $this->
output(
"Deleting old thumbnails...\n" );
107 foreach ( $iterator as
$file ) {
108 if (
wfTimestamp( TS_UNIX, $tempRepo->getFileTimestamp(
"$dir/$file" ) ) < $cutoff ) {
109 $batch[] = [
'op' =>
'delete',
'src' =>
"$dir/$file" ];
112 $i += count( $batch );
118 if ( count( $batch ) ) {
120 $i += count( $batch );
122 $this->
output(
"$i done\n" );
125 $dir = $tempRepo->getZonePath(
'public' );
126 $iterator = $tempRepo->getBackend()->getFileList( [
'dir' => $dir,
'adviseStat' => 1 ] );
127 if ( $iterator ===
null ) {
128 $this->
fatalError(
"Could not get file listing." );
130 $this->
output(
"Deleting orphaned temp files...\n" );
131 if ( strpos( $dir,
'/local-temp' ) ===
false ) {
132 $this->
fatalError(
"Temp repo is not using the temp container." );
137 foreach ( $iterator as
$file ) {
138 if (
wfTimestamp( TS_UNIX, $tempRepo->getFileTimestamp(
"$dir/$file" ) ) < $cutoff ) {
139 $batch[] = [
'op' =>
'delete',
'src' =>
"$dir/$file" ];
142 $i += count( $batch );
148 if ( count( $batch ) ) {
150 $i += count( $batch );
152 $this->
output(
"$i done\n" );
156 $status = $tempRepo->
getBackend()->doQuickOperations( $ops );
157 if ( !$status->isOK() ) {
158 $this->
error( print_r( Status::wrap( $status )->getErrorsArray(),
true ) );
164require_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.
waitForReplication()
Wait for replica DBs to catch up.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
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:
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.