| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
n |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
use MediaWiki\MainConfigNames; |
| 29 |
|
| 30 |
|
| 31 |
require_once __DIR__ . '/Maintenance.php'; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 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 |
|
| 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 |
|
| 66 |
if ( $res->numRows() == 0 ) { |
| 67 |
$this->output( "No stashed files to cleanup according to the DB.\n" ); |
| 68 |
} else { |
| 69 |
|
| 70 |
$keys = []; |
| 71 |
foreach ( $res as $row ) { |
| 72 |
$keys[] = $row->us_key; |
| 73 |
} |
| 74 |
|
| 75 |
$this->output( 'Removing ' . count( $keys ) . " file(s)...\n" ); |
| 76 |
|
| 77 |
|
| 78 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 165 |
$maintClass = CleanupUploadStash::class; |
| 166 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 167 |
|