First part of bug 22881: Allow uploading directly into the archive to support importi...
authorBryan Tong Minh <btongminh@users.mediawiki.org>
Thu, 7 Apr 2011 20:19:44 +0000 (20:19 +0000)
committerBryan Tong Minh <btongminh@users.mediawiki.org>
Thu, 7 Apr 2011 20:19:44 +0000 (20:19 +0000)
* 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
includes/filerepo/OldLocalFile.php

index 9aa5659..1f9184a 100644 (file)
@@ -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 );
index 0d224fc..16b4cd7 100644 (file)
@@ -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;
+       }
+       
 }