From 5d143aaa7c6df6070e66a78f3969797e7dc12b36 Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Thu, 7 Apr 2011 20:19:44 +0000 Subject: [PATCH] First part of bug 22881: Allow uploading directly into the archive to support importing files. Based on a patch by Vitaliy Filippov with some major rewrites by me. * LocalFile::publish() supports an extra parameter to support publishing into the archive * Added OldLocalFile::uploadOld(), which is the OldImage equivalent to LocalFile::upload(), but does not override it because it has an entirely different function signature. --- includes/filerepo/LocalFile.php | 16 ++++--- includes/filerepo/OldLocalFile.php | 67 ++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/includes/filerepo/LocalFile.php b/includes/filerepo/LocalFile.php index 9aa565968a..1f9184a6fe 100644 --- a/includes/filerepo/LocalFile.php +++ b/includes/filerepo/LocalFile.php @@ -1045,16 +1045,22 @@ class LocalFile extends File { * * @param $srcPath String: local filesystem path to the source image * @param $flags Integer: a bitwise combination of: - * File::DELETE_SOURCE Delete the source file, i.e. move - * rather than copy + * File::DELETE_SOURCE Delete the source file, i.e. move rather than copy + * @param $dstArchiveName string File name if the file is to be published + * into the archive * @return FileRepoStatus object. On success, the value member contains the * archive name, or an empty string if it was a new file. */ - function publish( $srcPath, $flags = 0 ) { + function publish( $srcPath, $flags = 0, $dstArchiveName = null ) { $this->lock(); - $dstRel = $this->getRel(); - $archiveName = gmdate( 'YmdHis' ) . '!' . $this->getName(); + if ( $dstArchiveName ) { + $dstRel = 'archive/' . $this->getHashPath() . $dstArchiveName; + } else { + $dstRel = $this->getRel(); + } + + $archiveName = wfTimestamp( TS_MW ) . '!'. $this->getName(); $archiveRel = 'archive/' . $this->getHashPath() . $archiveName; $flags = $flags & File::DELETE_SOURCE ? LocalRepo::DELETE_SOURCE : 0; $status = $this->repo->publish( $srcPath, $dstRel, $archiveRel, $flags ); diff --git a/includes/filerepo/OldLocalFile.php b/includes/filerepo/OldLocalFile.php index 0d224fcf0d..16b4cd7901 100644 --- a/includes/filerepo/OldLocalFile.php +++ b/includes/filerepo/OldLocalFile.php @@ -212,4 +212,71 @@ class OldLocalFile extends LocalFile { $this->load(); return Revision::userCanBitfield( $this->deleted, $field ); } + + /** + * Upload a file directly into archive. Generally for Special:Import. + * + * @param $srcPath string File system path of the source file + * @param $archiveName string Full archive name of the file, in the form + * $timestamp!$filename, where $filename must match $this->getName() + */ + function uploadOld( $srcPath, $archiveName, $comment, $user ) { + $this->lock(); + $status = $this->publish( $srcPath, $flags, $archiveName ); + + if ( $status->isGood() ) { + if ( !$this->recordOldUpload( $srcPath, $archiveName, $comment, $user ) ) { + $status->fatal( 'filenotfound', $srcPath ); + } + } + + $this->unlock(); + + return $status; + } + + /** + * Record a file upload in the oldimage table, without adding log entries. + * + * @param $srcPath string File system path to the source file + * @param $archiveName string The archive name of the file + * @param $comment string Upload comment + * @param $user User User who did this upload + * @return bool + */ + function recordOldUpload( $srcPath, $archiveName, $comment, $user ) { + $dbw = $this->repo->getMasterDB(); + $dbw->begin(); + + $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel(); + $props = self::getPropsFromPath( $dstPath ); + if ( !$props['fileExists'] ) { + return false; + } + + $dbw->insert( 'oldimage', + array( + 'oi_name' => $this->getName(), + 'oi_archive_name' => $archiveName, + 'oi_size' => $props['size'], + 'oi_width' => intval( $props['width'] ), + 'oi_height' => intval( $props['height'] ), + 'oi_bits' => $props['bits'], + 'oi_timestamp' => $props['timestamp'], + 'oi_description' => $comment, + 'oi_user' => $user->getId(), + 'oi_user_text' => $user->getName(), + 'oi_metadata' => $props['metadata'], + 'oi_media_type' => $props['media_type'], + 'oi_major_mime' => $props['major_mime'], + 'oi_minor_mime' => $props['minor_mime'], + 'oi_sha1' => $props['sha1'], + ), __METHOD__ + ); + + $dbw->commit(); + + return true; + } + } -- 2.20.1