98 $repo = $this->file->getRepo();
99 if ( !$this->all && !$this->ids ) {
101 return $repo->newGood();
104 $status = $this->file->acquireFileLock();
105 if ( !$status->isOK() ) {
109 $dbw = $this->file->repo->getPrimaryDB();
111 $ownTrx = !$dbw->trxLevel();
112 $funcName = __METHOD__;
113 $dbw->startAtomic( __METHOD__ );
115 $unlockScope =
new ScopedCallback(
function () use ( $dbw, $funcName ) {
116 $dbw->endAtomic( $funcName );
117 $this->file->releaseFileLock();
120 $commentStore = MediaWikiServices::getInstance()->getCommentStore();
122 $status = $this->file->repo->newGood();
124 $queryBuilder = $dbw->newSelectQueryBuilder()
127 ->where( [
'img_name' => $this->file->getName() ] );
132 $queryBuilder->lockInShareMode();
134 $exists = (bool)$queryBuilder->caller( __METHOD__ )->fetchField();
138 $arQueryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbw );
139 $arQueryBuilder->where( [
'fa_name' => $this->file->getName() ] )
140 ->orderBy(
'fa_timestamp', SelectQueryBuilder::SORT_DESC );
143 $arQueryBuilder->andWhere( [
'fa_id' => $this->ids ] );
146 $result = $arQueryBuilder->caller( __METHOD__ )->fetchResultSet();
151 $insertCurrent =
false;
156 foreach ( $result as $row ) {
157 $idsPresent[] = $row->fa_id;
159 if ( $row->fa_name != $this->file->getName() ) {
160 $status->error(
'undelete-filename-mismatch',
$wgLang->timeanddate( $row->fa_timestamp ) );
161 $status->failCount++;
165 if ( $row->fa_storage_key ==
'' ) {
167 $status->error(
'undelete-bad-store-key',
$wgLang->timeanddate( $row->fa_timestamp ) );
168 $status->failCount++;
172 $deletedRel = $repo->getDeletedHashPath( $row->fa_storage_key ) .
173 $row->fa_storage_key;
174 $deletedUrl = $repo->getVirtualUrl() .
'/deleted/' . $deletedRel;
176 if ( isset( $row->fa_sha1 ) ) {
177 $sha1 = $row->fa_sha1;
180 $sha1 = LocalRepo::getHashFromKey( $row->fa_storage_key );
184 if ( strlen( $sha1 ) == 32 && $sha1[0] ==
'0' ) {
185 $sha1 = substr( $sha1, 1 );
188 if ( $row->fa_major_mime ===
null || $row->fa_major_mime ==
'unknown'
189 || $row->fa_minor_mime ===
null || $row->fa_minor_mime ==
'unknown'
190 || $row->fa_media_type ===
null || $row->fa_media_type ==
'UNKNOWN'
191 || $row->fa_metadata ===
null
195 $this->file->loadFromFile( $deletedUrl );
196 $mime = $this->file->getMimeType();
197 [ $majorMime, $minorMime ] = File::splitMime( $mime );
199 'minor_mime' => $minorMime,
200 'major_mime' => $majorMime,
201 'media_type' => $this->file->getMediaType(),
202 'metadata' => $this->file->getMetadataForDb( $dbw )
206 'minor_mime' => $row->fa_minor_mime,
207 'major_mime' => $row->fa_major_mime,
208 'media_type' => $row->fa_media_type,
209 'metadata' => $row->fa_metadata
213 $comment = $commentStore->getComment(
'fa_description', $row );
214 if ( $first && !$exists ) {
216 $destRel = $this->file->getRel();
217 $commentFields = $commentStore->insert( $dbw,
'img_description', $comment );
219 'img_name' => $row->fa_name,
220 'img_size' => $row->fa_size,
221 'img_width' => $row->fa_width,
222 'img_height' => $row->fa_height,
223 'img_metadata' => $mediaInfo[
'metadata'],
224 'img_bits' => $row->fa_bits,
225 'img_media_type' => $mediaInfo[
'media_type'],
226 'img_major_mime' => $mediaInfo[
'major_mime'],
227 'img_minor_mime' => $mediaInfo[
'minor_mime'],
228 'img_actor' => $row->fa_actor,
229 'img_timestamp' => $row->fa_timestamp,
234 if ( !$this->unsuppress && $row->fa_deleted ) {
235 $status->fatal(
'undeleterevdel' );
239 $archiveName = $row->fa_archive_name;
241 if ( $archiveName ===
null ) {
245 $timestamp = (int)
wfTimestamp( TS_UNIX, $row->fa_deleted_timestamp );
248 $archiveName =
wfTimestamp( TS_MW, $timestamp ) .
'!' . $row->fa_name;
250 }
while ( isset( $archiveNames[$archiveName] ) );
253 $archiveNames[$archiveName] =
true;
254 $destRel = $this->file->getArchiveRel( $archiveName );
256 'oi_name' => $row->fa_name,
257 'oi_archive_name' => $archiveName,
258 'oi_size' => $row->fa_size,
259 'oi_width' => $row->fa_width,
260 'oi_height' => $row->fa_height,
261 'oi_bits' => $row->fa_bits,
262 'oi_actor' => $row->fa_actor,
263 'oi_timestamp' => $row->fa_timestamp,
264 'oi_metadata' => $mediaInfo[
'metadata'],
265 'oi_media_type' => $mediaInfo[
'media_type'],
266 'oi_major_mime' => $mediaInfo[
'major_mime'],
267 'oi_minor_mime' => $mediaInfo[
'minor_mime'],
268 'oi_deleted' => $this->unsuppress ? 0 : $row->fa_deleted,
270 ] + $commentStore->insert( $dbw,
'oi_description', $comment );
273 $deleteIds[] = $row->fa_id;
275 if ( !$this->unsuppress && $row->fa_deleted & File::DELETED_FILE ) {
277 $status->successCount++;
279 $storeBatch[] = [ $deletedUrl,
'public', $destRel ];
280 $this->cleanupBatch[] = $row->fa_storage_key;
289 $missingIds = array_diff( $this->ids, $idsPresent );
291 foreach ( $missingIds as $id ) {
292 $status->error(
'undelete-missing-filearchive', $id );
295 if ( !$repo->hasSha1Storage() ) {
298 if ( !$checkStatus->isGood() ) {
299 $status->merge( $checkStatus );
302 $storeBatch = $checkStatus->value;
307 $status->merge( $storeStatus );
309 if ( !$status->isGood() ) {
313 $status->setOK(
false );
324 if ( $insertCurrent ) {
325 $dbw->newInsertQueryBuilder()
326 ->insertInto(
'image' )
327 ->row( $insertCurrent )
328 ->caller( __METHOD__ )->execute();
331 if ( $insertBatch ) {
332 $dbw->newInsertQueryBuilder()
333 ->insertInto(
'oldimage' )
334 ->rows( $insertBatch )
335 ->caller( __METHOD__ )->execute();
339 $dbw->newDeleteQueryBuilder()
340 ->deleteFrom(
'filearchive' )
341 ->where( [
'fa_id' => $deleteIds ] )
342 ->caller( __METHOD__ )->execute();
346 if ( $status->successCount > 0 || !$storeBatch || $repo->hasSha1Storage() ) {
348 wfDebug( __METHOD__ .
" restored {$status->successCount} items, creating a new current" );
350 DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [
'images' => 1 ] ) );
352 $this->file->purgeEverything();
354 wfDebug( __METHOD__ .
" restored {$status->successCount} as archived versions" );
355 $this->file->purgeDescription();
359 ScopedCallback::consume( $unlockScope );