/**
  * Moves an image from a safe private location
  * Caller is responsible for clearing caches
  * @param File $oimage
  * @returns mixed, string timestamp on success, false on failure
  */
 function makeOldImagePublic($oimage)
 {
     $transaction = new FSTransaction();
     if (!FileStore::lock()) {
         wfDebug(__METHOD__ . " could not acquire filestore lock\n");
         return false;
     }
     $store = FileStore::get('deleted');
     if (!$store) {
         wfDebug(__METHOD__ . ": skipping row with no file.\n");
         return false;
     }
     $key = $oimage->sha1 . '.' . $oimage->getExtension();
     $destDir = $oimage->getArchivePath();
     if (!is_dir($destDir)) {
         wfMkdirParents($destDir);
     }
     $destPath = $destDir . DIRECTORY_SEPARATOR . $oimage->archive_name;
     // Check if any other stored revisions use this file;
     // if so, we shouldn't remove the file from the hidden
     // archives so they will still work. Check hidden files first.
     $useCount = $this->dbw->selectField('oldimage', '1', array('oi_sha1' => $oimage->sha1, 'oi_deleted & ' . File::DELETED_FILE => File::DELETED_FILE), __METHOD__, array('FOR UPDATE'));
     // Check the rest of the deleted archives too.
     // (these are the ones that don't show in the image history)
     if (!$useCount) {
         $useCount = $this->dbw->selectField('filearchive', '1', array('fa_storage_group' => 'deleted', 'fa_storage_key' => $key), __METHOD__, array('FOR UPDATE'));
     }
     if ($useCount == 0) {
         wfDebug(__METHOD__ . ": nothing else using {$oimage->sha1}, will deleting after\n");
         $flags = FileStore::DELETE_ORIGINAL;
     } else {
         $flags = 0;
     }
     $transaction->add($store->export($key, $destPath, $flags));
     wfDebug(__METHOD__ . ": set db items, applying file transactions\n");
     $transaction->commit();
     FileStore::unlock();
     $m = explode('!', $oimage->archive_name, 2);
     $timestamp = $m[0];
     return $timestamp;
 }
 /**
  * Restore all or specified deleted revisions to the given file.
  * Permissions and logging are left to the caller.
  *
  * May throw database exceptions on error.
  *
  * @param $versions set of record ids of deleted items to restore,
  *                    or empty to restore all revisions.
  * @return the number of file revisions restored if successful,
  *         or false on failure
  */
 function restore($versions = array(), $Unsuppress = false)
 {
     global $wgUser;
     if (!FileStore::lock()) {
         wfDebug(__METHOD__ . " could not acquire filestore lock\n");
         return false;
     }
     $transaction = new FSTransaction();
     try {
         $dbw = wfGetDB(DB_MASTER);
         $dbw->begin();
         // Re-confirm whether this image presently exists;
         // if no we'll need to create an image record for the
         // first item we restore.
         $exists = $dbw->selectField('image', '1', array('img_name' => $this->name), __METHOD__);
         // Fetch all or selected archived revisions for the file,
         // sorted from the most recent to the oldest.
         $conditions = array('fa_name' => $this->name);
         if ($versions) {
             $conditions['fa_id'] = $versions;
         }
         $result = $dbw->select('filearchive', '*', $conditions, __METHOD__, array('ORDER BY' => 'fa_timestamp DESC'));
         if ($dbw->numRows($result) < count($versions)) {
             // There's some kind of conflict or confusion;
             // we can't restore everything we were asked to.
             wfDebug(__METHOD__ . ": couldn't find requested items\n");
             $dbw->rollback();
             FileStore::unlock();
             return false;
         }
         if ($dbw->numRows($result) == 0) {
             // Nothing to do.
             wfDebug(__METHOD__ . ": nothing to do\n");
             $dbw->rollback();
             FileStore::unlock();
             return true;
         }
         $revisions = 0;
         while ($row = $dbw->fetchObject($result)) {
             if ($Unsuppress) {
                 // Currently, fa_deleted flags fall off upon restore, lets be careful about this
             } else {
                 if ($row->fa_deleted & Revision::DELETED_RESTRICTED && !$wgUser->isAllowed('hiderevision')) {
                     // Skip restoring file revisions that the user cannot restore
                     continue;
                 }
             }
             $revisions++;
             $store = FileStore::get($row->fa_storage_group);
             if (!$store) {
                 wfDebug(__METHOD__ . ": skipping row with no file.\n");
                 continue;
             }
             if ($revisions == 1 && !$exists) {
                 $destDir = wfImageDir($row->fa_name);
                 if (!is_dir($destDir)) {
                     wfMkdirParents($destDir);
                 }
                 $destPath = $destDir . DIRECTORY_SEPARATOR . $row->fa_name;
                 // We may have to fill in data if this was originally
                 // an archived file revision.
                 if (is_null($row->fa_metadata)) {
                     $tempFile = $store->filePath($row->fa_storage_key);
                     $magic = MimeMagic::singleton();
                     $mime = $magic->guessMimeType($tempFile, true);
                     $media_type = $magic->getMediaType($tempFile, $mime);
                     list($major_mime, $minor_mime) = self::splitMime($mime);
                     $handler = MediaHandler::getHandler($mime);
                     if ($handler) {
                         $metadata = $handler->getMetadata($image, $tempFile);
                     } else {
                         $metadata = '';
                     }
                 } else {
                     $metadata = $row->fa_metadata;
                     $major_mime = $row->fa_major_mime;
                     $minor_mime = $row->fa_minor_mime;
                     $media_type = $row->fa_media_type;
                 }
                 $table = 'image';
                 $fields = array('img_name' => $row->fa_name, 'img_size' => $row->fa_size, 'img_width' => $row->fa_width, 'img_height' => $row->fa_height, 'img_metadata' => $metadata, 'img_bits' => $row->fa_bits, 'img_media_type' => $media_type, 'img_major_mime' => $major_mime, 'img_minor_mime' => $minor_mime, 'img_description' => $row->fa_description, 'img_user' => $row->fa_user, 'img_user_text' => $row->fa_user_text, 'img_timestamp' => $row->fa_timestamp);
             } else {
                 $archiveName = $row->fa_archive_name;
                 if ($archiveName == '') {
                     // This was originally a current version; we
                     // have to devise a new archive name for it.
                     // Format is <timestamp of archiving>!<name>
                     $archiveName = wfTimestamp(TS_MW, $row->fa_deleted_timestamp) . '!' . $row->fa_name;
                 }
                 $destDir = wfImageArchiveDir($row->fa_name);
                 if (!is_dir($destDir)) {
                     wfMkdirParents($destDir);
                 }
                 $destPath = $destDir . DIRECTORY_SEPARATOR . $archiveName;
                 $table = 'oldimage';
                 $fields = array('oi_name' => $row->fa_name, 'oi_archive_name' => $archiveName, 'oi_size' => $row->fa_size, 'oi_width' => $row->fa_width, 'oi_height' => $row->fa_height, 'oi_bits' => $row->fa_bits, 'oi_description' => $row->fa_description, 'oi_user' => $row->fa_user, 'oi_user_text' => $row->fa_user_text, 'oi_timestamp' => $row->fa_timestamp);
             }
             $dbw->insert($table, $fields, __METHOD__);
             // @todo this delete is not totally safe, potentially
             $dbw->delete('filearchive', array('fa_id' => $row->fa_id), __METHOD__);
             // Check if any other stored revisions use this file;
             // if so, we shouldn't remove the file from the deletion
             // archives so they will still work.
             $useCount = $dbw->selectField('filearchive', 'COUNT(*)', array('fa_storage_group' => $row->fa_storage_group, 'fa_storage_key' => $row->fa_storage_key), __METHOD__);
             if ($useCount == 0) {
                 wfDebug(__METHOD__ . ": nothing else using {$row->fa_storage_key}, will deleting after\n");
                 $flags = FileStore::DELETE_ORIGINAL;
             } else {
                 $flags = 0;
             }
             $transaction->add($store->export($row->fa_storage_key, $destPath, $flags));
         }
         $dbw->immediateCommit();
     } catch (MWException $e) {
         wfDebug(__METHOD__ . " caught error, aborting\n");
         $transaction->rollback();
         throw $e;
     }
     $transaction->commit();
     FileStore::unlock();
     if ($revisions > 0) {
         if (!$exists) {
             wfDebug(__METHOD__ . " restored {$revisions} items, creating a new current\n");
             // Update site_stats
             $site_stats = $dbw->tableName('site_stats');
             $dbw->query("UPDATE {$site_stats} SET ss_images=ss_images+1", __METHOD__);
             $this->purgeEverything();
         } else {
             wfDebug(__METHOD__ . " restored {$revisions} as archived versions\n");
             $this->purgeDescription();
         }
     }
     return $revisions;
 }