use class constant from r64403
[lhc/web/wiklou.git] / includes / upload / UploadBase.php
index ca9f6a5..4f0522e 100644 (file)
@@ -33,6 +33,11 @@ abstract class UploadBase {
        const HOOK_ABORTED = 11;
 
        const SESSION_VERSION = 2;
+       const SESSION_KEYNAME = 'wsUploadData';
+
+       static public function getSessionKeyname() {
+               return self::SESSION_KEYNAME;
+       }
 
        /**
         * Returns true if uploads are enabled.
@@ -143,12 +148,34 @@ abstract class UploadBase {
        }
 
        /**
-        * Return the file size
+        * Return true if the file is empty
+        * @return bool
         */
        public function isEmptyFile() {
                return empty( $this->mFileSize );
        }
 
+       /**
+        * Return the file size
+        * @return integer
+        */
+       public function getFileSize() {
+               return $this->mFileSize;
+       }
+
+       /**
+        * Append a file to the Repo file
+        *
+        * @param string $srcPath Path to source file
+        * @param string $toAppendPath Path to the Repo file that will be appended to.
+        * @return Status Status
+        */
+       protected function appendToUploadFile( $srcPath, $toAppendPath ) {
+               $repo = RepoGroup::singleton()->getLocalRepo();
+               $status = $repo->append( $srcPath, $toAppendPath );
+               return $status;
+       }
+
        /**
         * @param $srcPath String: the source path
         * @return the real path if it was a virtual URL
@@ -163,9 +190,9 @@ abstract class UploadBase {
 
        /**
         * Verify whether the upload is sane.
-        * Returns self::OK or else an array with error information
+        * @return mixed self::OK or else an array with error information
         */
-       public function verifyUpload() {
+       public function verifyUpload( ) {
                /**
                 * If there was no filename or a zero size given, give up quick.
                 */
@@ -189,6 +216,31 @@ abstract class UploadBase {
                        );
                }
 
+               /**
+                * Make sure this file can be created
+                */
+               $result = $this->validateNameAndOverwrite();
+               if( $result !== true ) {
+                       return $result;
+               }
+
+               $error = '';
+               if( !wfRunHooks( 'UploadVerification',
+                               array( $this->mDestName, $this->mTempPath, &$error ) ) ) {
+                       // @fixme This status needs another name...
+                       return array( 'status' => self::HOOK_ABORTED, 'error' => $error );
+               }
+
+               return array( 'status' => self::OK );
+       }
+
+       /**
+        * Verify that the name is valid and, if necessary, that we can overwrite
+        *
+        * @return mixed true if valid, otherwise and array with 'status'
+        * and other keys
+        **/
+       public function validateNameAndOverwrite() {
                $nt = $this->getTitle();
                if( is_null( $nt ) ) {
                        $result = array( 'status' => $this->mTitleError );
@@ -212,15 +264,7 @@ abstract class UploadBase {
                                'overwrite' => $overwrite
                        );
                }
-
-               $error = '';
-               if( !wfRunHooks( 'UploadVerification',
-                               array( $this->mDestName, $this->mTempPath, &$error ) ) ) {
-                       // This status needs another name...
-                       return array( 'status' => self::HOOK_ABORTED, 'error' => $error );
-               }
-
-               return array( 'status' => self::OK );
+               return true;
        }
 
        /**
@@ -390,11 +434,11 @@ abstract class UploadBase {
                $status = $this->getLocalFile()->upload( $this->mTempPath, $comment, $pageText,
                        File::DELETE_SOURCE, $this->mFileProps, false, $user );
 
-               if( $status->isGood() && $watch ) {
-                       $user->addWatch( $this->getLocalFile()->getTitle() );
-               }
-
                if( $status->isGood() ) {
+                       if ( $watch ) {
+                               $user->addWatch( $this->getLocalFile()->getTitle() );
+                       }
+
                        wfRunHooks( 'UploadComplete', array( &$this ) );
                }
 
@@ -417,9 +461,7 @@ abstract class UploadBase {
                 * filter out illegal characters, and try to make a legible name
                 * out of it. We'll strip some silently that Title would die on.
                 */
-               $basename = $this->mDesiredDestName;
-
-               $this->mFilteredName = wfStripIllegalFilenameChars( $basename );
+               $this->mFilteredName = wfStripIllegalFilenameChars( $this->mDesiredDestName );
                /* Normalize to title form before we do any further processing */
                $nt = Title::makeTitleSafe( NS_FILE, $this->mFilteredName );
                if( is_null( $nt ) ) {
@@ -466,11 +508,6 @@ abstract class UploadBase {
                        return $this->mTitle = null;
                }
 
-               $nt = Title::makeTitleSafe( NS_FILE, $this->mFilteredName );
-               if( is_null( $nt ) ) {
-                       $this->mTitleError = self::ILLEGAL_FILENAME;
-                       return $this->mTitle = null;
-               }
                return $this->mTitle = $nt;
        }
 
@@ -518,7 +555,7 @@ abstract class UploadBase {
                }
 
                $key = $this->getSessionKey();
-               $_SESSION['wsUploadData'][$key] = array(
+               $_SESSION[self::SESSION_KEYNAME][$key] = array(
                        'mTempPath'       => $status->value,
                        'mFileSize'       => $this->mFileSize,
                        'mFileProps'      => $this->mFileProps,
@@ -532,7 +569,7 @@ abstract class UploadBase {
         */
        protected function getSessionKey() {
                $key = mt_rand( 0, 0x7fffffff );
-               $_SESSION['wsUploadData'][$key] = array();
+               $_SESSION[self::SESSION_KEYNAME][$key] = array();
                return $key;
        }