Start adding support for leaving user's messages. Begin using it in UploadFromUrl.php
[lhc/web/wiklou.git] / includes / upload / UploadFromUrl.php
index af97d9f..91a630b 100644 (file)
@@ -10,6 +10,7 @@
  */
 class UploadFromUrl extends UploadBase {
        protected $mTempDownloadPath;
+       protected $comment, $watchList, $ignoreWarnings;
 
        /**
         * Checks if the user is allowed to use the upload-by-URL feature. If the
@@ -23,6 +24,7 @@ class UploadFromUrl extends UploadBase {
 
        /**
         * Checks if the upload from URL feature is enabled
+        * @return bool
         */
        public static function isEnabled() {
                global $wgAllowCopyUploads;
@@ -31,14 +33,59 @@ class UploadFromUrl extends UploadBase {
 
        /**
         * Entry point for API upload
+        * @return bool true on success
         */
-       public function initialize( $name, $url, $na, $nb = false ) {
-               global $wgTmpDirectory;
+       public function initialize( $name, $url, $comment, $watchList, $ignoreWarn ) {
+               global $wgUser;
+
+               if( !Http::isValidURI( $url ) ) {
+                       return Status::newFatal( 'http-invalid-url' );
+               }
+               $params = array(
+                       "userName" => $wgUser->getName(),
+                       "userID" => $wgUser->getID(),
+                       "url" => trim( $url ),
+                       "timestamp" => wfTimestampNow(),
+                       "comment" => $comment,
+                       "watchlist" => $watchList,
+                       "ignorewarnings" => $ignoreWarn);
+
+               $title = Title::newFromText( $name );
+               /* // Check whether the user has the appropriate permissions to upload anyway */
+               /* $permission = $this->isAllowed( $wgUser ); */
+
+               /* if ( $permission !== true ) { */
+               /*      if ( !$wgUser->isLoggedIn() ) { */
+               /*              return Status::newFatal( 'uploadnologintext' ); */
+               /*      } else { */
+               /*              return Status::newFatal( 'badaccess-groups' ); */
+               /*      } */
+               /* } */
 
-               $localFile = tempnam( $wgTmpDirectory, 'WEBUPLOAD' );
-               $this->initializePathInfo( $name, $localFile, 0, true );
+               /* $permErrors = $this->verifyPermissions( $wgUser ); */
+               /* if ( $permErrors !== true ) { */
+               /*      return Status::newFatal( 'badaccess-groups' ); */
+               /* } */
 
-               $this->mUrl = trim( $url );
+
+               $job = new UploadFromUrlJob( $title, $params );
+               return $job->insert();
+       }
+
+       /**
+        * Initialize a queued download
+        * @param $job Job
+        */
+       public function initializeFromJob( $job ) {
+               global $wgTmpDirectory;
+
+               $this->mUrl = $job->params['url'];
+               $this->mTempPath = tempnam( $wgTmpDirectory, 'COPYUPLOAD' );
+               $this->mDesiredDestName = $job->title;
+               $this->comment = $job->params['comment'];
+               $this->watchList = $job->params['watchlist'];
+               $this->ignoreWarnings = $job->params['ignorewarnings'];
+               $this->getTitle();
        }
 
        /**
@@ -52,7 +99,9 @@ class UploadFromUrl extends UploadBase {
                return $this->initialize(
                        $desiredDestName,
                        $request->getVal( 'wpUploadFileURL' ),
-                       false
+                       $request->getVal( 'wpUploadDescription' ),
+                       $request->getVal( 'wpWatchThis' ),
+                       $request->getVal( 'wpIgnoreWarnings' )
                );
        }
 
@@ -60,66 +109,71 @@ class UploadFromUrl extends UploadBase {
         * @param $request Object: WebRequest object
         */
        public static function isValidRequest( $request ){
-               if( !$request->getVal( 'wpUploadFileURL' ) )
-                       return false;
-               // check that is a valid url:
-               return self::isValidUrl( $request->getVal( 'wpUploadFileURL' ) );
+               global $wgUser;
+
+               $url = $request->getVal( 'wpUploadFileURL' );
+               return !empty( $url )
+                       && Http::isValidURI( $url )
+                       && $wgUser->isAllowed( 'upload_by_url' );
        }
 
-       public static function isValidUrl( $url ) {
-               // Only allow HTTP or FTP for now
-               return (bool)preg_match( '!^(http://|ftp://)!', $url );
+       private function saveTempFile( $req ) {
+               $filename = tempnam( wfTempDir(), 'URL' );
+               if ( $filename === false ) {
+                       return Status::newFatal( 'tmp-create-error' );
+               }
+               if ( file_put_contents( $filename, $req->getContent() ) === false ) {
+                       return Status::newFatal( 'tmp-write-error' );
+               }
+
+               $this->mTempPath = $filename;
+               $this->mFileSize = filesize( $filename );
+
+               return Status::newGood();
        }
 
-       /**
-        * Do the real fetching stuff
-        */
-       function fetchFile() {
-               if( !self::isValidUrl( $this->mUrl ) ) {
-                       return Status::newFatal( 'upload-proto-error' );
+       public function doUpload() {
+               global $wgUser;
+
+               $req = HttpRequest::factory($this->mUrl);
+               $status = $req->execute();
+
+               if( !$status->isOk() ) {
+                       return $status;
                }
 
-               # Open temporary file
-               $this->mCurlDestHandle = @fopen( $this->mTempPath, "wb" );
-               if( $this->mCurlDestHandle === false ) {
-                       # Could not open temporary file to write in
-                       return Status::newFatal( 'upload-file-error' );
+               $status = $this->saveTempFile( $req );
+               if ( !$status->isGood() ) {
+                       return $status;
                }
-               
-               $options = array(
-                       'method' => 'GET',
-                       'timeout' => 10,
-               );
-               $req = HttpRequest::factory( $this->mUrl, $options );
-               $req->setCallback( array( $this, 'uploadCurlCallback' ) );
-               $status = $req->execute();
-               fclose( $this->mCurlDestHandle );
-               unset( $this->mCurlDestHandle );
-
-               global $wgMaxUploadSize;
-               if ( $this->mFileSize > $wgMaxUploadSize ) {
-                       # Just return an ok, so that the regular verifications can handle 
-                       # the file-too-large error
-                       return Status::newGood();
+
+               $this->mRemoveTempFile = true;
+
+               $v = $this->verifyUpload();
+               if( $v['status'] !== UploadBase::OK ) {
+                       return $this->convertVerifyErrorToStatus( $v['status'], $v['details'] );
                }
 
-               return $status;
-       }
+               // This has to come from API
+               /* $warnings = $this->checkForWarnings(); */
+               /* if( isset($warnings) ) return $warnings; */
 
-       /**
-        * Callback function for CURL-based web transfer
-        * Write data to file unless we've passed the length limit;
-        * if so, abort immediately.
-        * @access private
-        */
-       function uploadCurlCallback( $ch, $data ) {
-               global $wgMaxUploadSize;
-               $length = strlen( $data );
-               $this->mFileSize += $length;
-               if( $this->mFileSize > $wgMaxUploadSize ) {
-                       return 0;
+               $file = $this->getLocalFile();
+               // This comes from ApiBase
+               /* $watch = $this->getWatchlistValue( $this->mParams['watchlist'], $file->getTitle() ); */
+
+               $status = $this->getLocalFile()->upload( $this->mTempPath, $this->comment,
+                       $this->comment, File::DELETE_SOURCE, $this->mFileProps, false, $wgUser );
+
+               if ( $status->isGood() ) {
+                       $url = $this->getLocalFile()->getDescriptionUrl();
+                       $wgUser->leaveUserMessage( wfMsg( 'successfulupload' ),
+                               wfMsg( 'upload-success-msg', $url ) );
+               } else {
+                       $wgUser->leaveUserMessage( wfMsg( 'upload-failure-subj' ),
+                               wfMsg( 'upload-failure-msg', $status->getWikiText() ) );
                }
-               fwrite( $this->mCurlDestHandle, $data );
-               return $length;
+
+               return $status;
        }
 }