92 $repo = $this->file->getRepo();
93 if ( !$this->all && !$this->ids ) {
95 return $repo->newGood();
98 $lockOwnsTrx = $this->file->lock();
100 $dbw = $this->file->repo->getPrimaryDB();
102 $commentStore = MediaWikiServices::getInstance()->getCommentStore();
104 $status = $this->file->repo->newGood();
106 $exists = (bool)$dbw->selectField(
'image',
'1',
107 [
'img_name' => $this->file->getName() ],
112 $lockOwnsTrx ? [] : [
'LOCK IN SHARE MODE' ]
117 $conditions = [
'fa_name' => $this->file->getName() ];
124 $result = $dbw->select(
125 $arFileQuery[
'tables'],
126 $arFileQuery[
'fields'],
129 [
'ORDER BY' =>
'fa_timestamp DESC' ],
130 $arFileQuery[
'joins']
136 $insertCurrent =
false;
141 foreach ( $result as $row ) {
142 $idsPresent[] = $row->fa_id;
144 if ( $row->fa_name != $this->file->getName() ) {
145 $status->error(
'undelete-filename-mismatch',
$wgLang->timeanddate( $row->fa_timestamp ) );
146 $status->failCount++;
150 if ( $row->fa_storage_key ==
'' ) {
152 $status->error(
'undelete-bad-store-key',
$wgLang->timeanddate( $row->fa_timestamp ) );
153 $status->failCount++;
157 $deletedRel = $repo->getDeletedHashPath( $row->fa_storage_key ) .
158 $row->fa_storage_key;
159 $deletedUrl = $repo->getVirtualUrl() .
'/deleted/' . $deletedRel;
161 if ( isset( $row->fa_sha1 ) ) {
162 $sha1 = $row->fa_sha1;
169 if ( strlen( $sha1 ) == 32 && $sha1[0] ==
'0' ) {
170 $sha1 = substr( $sha1, 1 );
173 if ( $row->fa_major_mime ===
null || $row->fa_major_mime ==
'unknown'
174 || $row->fa_minor_mime ===
null || $row->fa_minor_mime ==
'unknown'
175 || $row->fa_media_type ===
null || $row->fa_media_type ==
'UNKNOWN'
176 || $row->fa_metadata ===
null
180 $this->file->loadFromFile( $deletedUrl );
181 $mime = $this->file->getMimeType();
182 list( $majorMime, $minorMime ) = File::splitMime(
$mime );
184 'minor_mime' => $minorMime,
185 'major_mime' => $majorMime,
186 'media_type' => $this->file->getMediaType(),
187 'metadata' => $this->file->getMetadataForDb( $dbw )
191 'minor_mime' => $row->fa_minor_mime,
192 'major_mime' => $row->fa_major_mime,
193 'media_type' => $row->fa_media_type,
194 'metadata' => $row->fa_metadata
198 $comment = $commentStore->getComment(
'fa_description', $row );
199 if ( $first && !$exists ) {
201 $destRel = $this->file->getRel();
202 $commentFields = $commentStore->insert( $dbw,
'img_description', $comment );
204 'img_name' => $row->fa_name,
205 'img_size' => $row->fa_size,
206 'img_width' => $row->fa_width,
207 'img_height' => $row->fa_height,
208 'img_metadata' => $mediaInfo[
'metadata'],
209 'img_bits' => $row->fa_bits,
210 'img_media_type' => $mediaInfo[
'media_type'],
211 'img_major_mime' => $mediaInfo[
'major_mime'],
212 'img_minor_mime' => $mediaInfo[
'minor_mime'],
213 'img_actor' => $row->fa_actor,
214 'img_timestamp' => $row->fa_timestamp,
219 if ( !$this->unsuppress && $row->fa_deleted ) {
220 $status->fatal(
'undeleterevdel' );
221 $this->file->unlock();
225 $archiveName = $row->fa_archive_name;
227 if ( $archiveName ==
'' ) {
231 $timestamp =
wfTimestamp( TS_UNIX, $row->fa_deleted_timestamp );
234 $archiveName =
wfTimestamp( TS_MW, $timestamp ) .
'!' . $row->fa_name;
236 }
while ( isset( $archiveNames[$archiveName] ) );
239 $archiveNames[$archiveName] =
true;
240 $destRel = $this->file->getArchiveRel( $archiveName );
242 'oi_name' => $row->fa_name,
243 'oi_archive_name' => $archiveName,
244 'oi_size' => $row->fa_size,
245 'oi_width' => $row->fa_width,
246 'oi_height' => $row->fa_height,
247 'oi_bits' => $row->fa_bits,
248 'oi_actor' => $row->fa_actor,
249 'oi_timestamp' => $row->fa_timestamp,
250 'oi_metadata' => $mediaInfo[
'metadata'],
251 'oi_media_type' => $mediaInfo[
'media_type'],
252 'oi_major_mime' => $mediaInfo[
'major_mime'],
253 'oi_minor_mime' => $mediaInfo[
'minor_mime'],
254 'oi_deleted' => $this->unsuppress ? 0 : $row->fa_deleted,
256 ] + $commentStore->insert( $dbw,
'oi_description', $comment );
259 $deleteIds[] = $row->fa_id;
261 if ( !$this->unsuppress && $row->fa_deleted & File::DELETED_FILE ) {
263 $status->successCount++;
265 $storeBatch[] = [ $deletedUrl,
'public', $destRel ];
266 $this->cleanupBatch[] = $row->fa_storage_key;
275 $missingIds = array_diff( $this->ids, $idsPresent );
277 foreach ( $missingIds as $id ) {
278 $status->error(
'undelete-missing-filearchive', $id );
281 if ( !$repo->hasSha1Storage() ) {
284 if ( !$checkStatus->isGood() ) {
285 $status->merge( $checkStatus );
288 $storeBatch = $checkStatus->value;
293 $status->merge( $storeStatus );
295 if ( !$status->isGood() ) {
299 $status->setOK(
false );
300 $this->file->unlock();
312 if ( $insertCurrent ) {
313 $dbw->insert(
'image', $insertCurrent, __METHOD__ );
316 if ( $insertBatch ) {
317 $dbw->insert(
'oldimage', $insertBatch, __METHOD__ );
321 $dbw->delete(
'filearchive',
322 [
'fa_id' => $deleteIds ],
327 if ( $status->successCount > 0 || !$storeBatch || $repo->hasSha1Storage() ) {
329 wfDebug( __METHOD__ .
" restored {$status->successCount} items, creating a new current" );
331 DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [
'images' => 1 ] ) );
333 $this->file->purgeEverything();
335 wfDebug( __METHOD__ .
" restored {$status->successCount} as archived versions" );
336 $this->file->purgeDescription();
340 $this->file->unlock();