Merge "Revert "Adding sanity check to Title::isRedirect().""
[lhc/web/wiklou.git] / includes / upload / UploadStash.php
index 3d624a5..12531c2 100644 (file)
@@ -1,4 +1,26 @@
 <?php
+/**
+ * Temporary storage for uploaded files.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Upload
+ */
+
 /**
  * UploadStash is intended to accomplish a few things:
  *   - enable applications to temporarily stash files without publishing them to the wiki.
@@ -16,6 +38,8 @@
  * UploadStash represents the entire stash of temporary files.
  * UploadStashFile is a filestore for the actual physical disk files.
  * UploadFromStash extends UploadBase, and represents a single stashed file as it is moved from the stash to the regular file repository
+ *
+ * @ingroup Upload
  */
 class UploadStash {
 
@@ -48,7 +72,7 @@ class UploadStash {
         *
         * @param $repo FileRepo
         */
-       public function __construct( $repo, $user = null ) {
+       public function __construct( FileRepo $repo, $user = null ) {
                // this might change based on wiki's configuration.
                $this->repo = $repo;
 
@@ -91,8 +115,6 @@ class UploadStash {
                        }
                }
 
-               $dbr = $this->repo->getSlaveDb();
-
                if ( !isset( $this->fileMetadata[$key] ) ) {
                        if ( !$this->fetchFileMetadata( $key ) ) {
                                // If nothing was received, it's likely due to replication lag.  Check the master to see if the record is there.
@@ -108,10 +130,7 @@ class UploadStash {
 
                        // fetch fileprops
                        $path = $this->fileMetadata[$key]['us_path'];
-                       if ( $this->repo->isVirtualUrl( $path ) ) {
-                               $path = $this->repo->resolveVirtualUrl( $path );
-                       }
-                       $this->fileProps[$key] = File::getPropsFromPath( $path );
+                       $this->fileProps[$key] = $this->repo->getFileProps( $path );
                }
 
                if ( ! $this->files[$key]->exists() ) {
@@ -165,7 +184,7 @@ class UploadStash {
                        wfDebug( __METHOD__ . " tried to stash file at '$path', but it doesn't exist\n" );
                        throw new UploadStashBadPathException( "path doesn't exist" );
                }
-               $fileProps = File::getPropsFromPath( $path );
+               $fileProps = FSFile::getPropsFromPath( $path );
                wfDebug( __METHOD__ . " stashing file at '$path'\n" );
 
                // we will be initializing from some tmpnam files that don't have extensions.
@@ -188,7 +207,7 @@ class UploadStash {
                $usec = substr($usec, 2);
                $key = wfBaseConvert( $sec . $usec, 10, 36 ) . '.' .
                        wfBaseConvert( mt_rand(), 10, 36 ) . '.'.
-                       $this->userId . '.' . 
+                       $this->userId . '.' .
                        $extension;
 
                $this->fileProps[$key] = $fileProps;
@@ -217,7 +236,8 @@ class UploadStash {
                                        $error = array( 'unknown', 'no error recorded' );
                                }
                        }
-                       throw new UploadStashFileException( "error storing file in '$path': " . implode( '; ', $error ) );
+                       // at this point, $error should contain the single "most important" error, plus any parameters.
+                       throw new UploadStashFileException( "Error storing file in '$path': " . wfMessage( $error )->text() );
                }
                $stashPath = $storeStatus->value;
 
@@ -231,10 +251,11 @@ class UploadStash {
                $dbw = $this->repo->getMasterDb();
 
                $this->fileMetadata[$key] = array(
+                       'us_id' => $dbw->nextSequenceValue( 'uploadstash_us_id_seq' ),
                        'us_user' => $this->userId,
                        'us_key' => $key,
                        'us_orig_path' => $path,
-                       'us_path' => $stashPath,
+                       'us_path' => $stashPath, // virtual URL
                        'us_size' => $fileProps['size'],
                        'us_sha1' => $fileProps['sha1'],
                        'us_mime' => $fileProps['mime'],
@@ -335,13 +356,13 @@ class UploadStash {
                $dbw = $this->repo->getMasterDb();
 
                // this gets its own transaction since it's called serially by the cleanupUploadStash maintenance script
-               $dbw->begin();
+               $dbw->begin( __METHOD__ );
                $dbw->delete(
                        'uploadstash',
                        array( 'us_key' => $key ),
                        __METHOD__
                );
-               $dbw->commit();
+               $dbw->commit( __METHOD__ );
 
                // TODO: look into UnregisteredLocalFile and find out why the rv here is sometimes wrong (false when file was removed)
                // for now, ignore.
@@ -392,6 +413,7 @@ class UploadStash {
         * with an extension.
         * XXX this is somewhat redundant with the checks that ApiUpload.php does with incoming
         * uploads versus the desired filename. Maybe we can get that passed to us...
+        * @return string
         */
        public static function getExtensionForPath( $path ) {
                // Does this have an extension?
@@ -476,7 +498,7 @@ class UploadStashFile extends UnregisteredLocalFile {
         * A LocalFile wrapper around a file that has been temporarily stashed, so we can do things like create thumbnails for it
         * Arguably UnregisteredLocalFile should be handling its own file repo but that class is a bit retarded currently
         *
-        * @param $repo FSRepo: repository where we should find the path
+        * @param $repo FileRepo: repository where we should find the path
         * @param $path String: path to file
         * @param $key String: key to store the path and any stashed data under
         * @throws UploadStashBadPathException
@@ -499,7 +521,7 @@ class UploadStashFile extends UnregisteredLocalFile {
                        }
 
                        // check if path exists! and is a plain file.
-                       if ( ! $repo->fileExists( $path, FileRepo::FILES_ONLY ) ) {
+                       if ( ! $repo->fileExists( $path ) ) {
                                wfDebug( "UploadStash: tried to construct an UploadStashFile from a file that should already exist at '$path', but path is not found\n" );
                                throw new UploadStashFileNotFoundException( 'cannot find path, or not a plain file' );
                        }
@@ -623,7 +645,7 @@ class UploadStashFile extends UnregisteredLocalFile {
         * @return Status: success
         */
        public function remove() {
-               if ( !$this->repo->fileExists( $this->path, FileRepo::FILES_ONLY ) ) {
+               if ( !$this->repo->fileExists( $this->path ) ) {
                        // Maybe the file's already been removed? This could totally happen in UploadBase.
                        return true;
                }
@@ -632,7 +654,7 @@ class UploadStashFile extends UnregisteredLocalFile {
        }
 
        public function exists() {
-               return $this->repo->fileExists( $this->path, FileRepo::FILES_ONLY );
+               return $this->repo->fileExists( $this->path );
        }
 
 }