3 * Implements uploading from a HTTP resource.
6 * @author Bryan Tong Minh
9 class UploadFromUrl
extends UploadBase
{
10 protected $mAsync, $mUrl;
11 protected $mIgnoreWarnings = true;
13 protected $mTempPath, $mTmpHandle;
16 * Checks if the user is allowed to use the upload-by-URL feature. If the
17 * user is allowed, pass on permissions checking to the parent.
23 public static function isAllowed( $user ) {
24 if ( !$user->isAllowed( 'upload_by_url' ) ) {
25 return 'upload_by_url';
27 return parent
::isAllowed( $user );
31 * Checks if the upload from URL feature is enabled
34 public static function isEnabled() {
35 global $wgAllowCopyUploads;
36 return $wgAllowCopyUploads && parent
::isEnabled();
40 * Entry point for API upload
44 * @param $async mixed Whether the download should be performed
45 * asynchronous. False for synchronous, async or async-leavemessage for
46 * asynchronous download.
48 public function initialize( $name, $url, $async = false ) {
49 global $wgAllowAsyncCopyUploads;
52 $this->mAsync
= $wgAllowAsyncCopyUploads ?
$async : false;
54 throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
57 $tempPath = $this->mAsync ?
null : $this->makeTemporaryFile();
58 # File size and removeTempFile will be filled in later
59 $this->initializePathInfo( $name, $tempPath, 0, false );
63 * Entry point for SpecialUpload
64 * @param $request WebRequest object
66 public function initializeFromRequest( &$request ) {
67 $desiredDestName = $request->getText( 'wpDestFile' );
68 if ( !$desiredDestName ) {
69 $desiredDestName = $request->getText( 'wpUploadFileURL' );
71 return $this->initialize(
73 trim( $request->getVal( 'wpUploadFileURL' ) ),
79 * @param $request WebRequest object
82 public static function isValidRequest( $request ) {
85 $url = $request->getVal( 'wpUploadFileURL' );
87 && Http
::isValidURI( $url )
88 && $wgUser->isAllowed( 'upload_by_url' );
94 public function getSourceType() { return 'url'; }
99 public function fetchFile() {
100 if ( !Http
::isValidURI( $this->mUrl
) ) {
101 return Status
::newFatal( 'http-invalid-url' );
104 if ( !$this->mAsync
) {
105 return $this->reallyFetchFile();
107 return Status
::newGood();
110 * Create a new temporary file in the URL subdirectory of wfTempDir().
112 * @return string Path to the file
114 protected function makeTemporaryFile() {
115 return tempnam( wfTempDir(), 'URL' );
119 * Callback: save a chunk of the result of a HTTP request to the temporary file
122 * @param $buffer string
123 * @return int number of bytes handled
125 public function saveTempFileChunk( $req, $buffer ) {
126 $nbytes = fwrite( $this->mTmpHandle
, $buffer );
128 if ( $nbytes == strlen( $buffer ) ) {
129 $this->mFileSize +
= $nbytes;
131 // Well... that's not good!
132 fclose( $this->mTmpHandle
);
133 $this->mTmpHandle
= false;
140 * Download the file, save it to the temporary file and update the file
141 * size and set $mRemoveTempFile to true.
144 protected function reallyFetchFile() {
145 if ( $this->mTempPath
=== false ) {
146 return Status
::newFatal( 'tmp-create-error' );
149 // Note the temporary file should already be created by makeTemporaryFile()
150 $this->mTmpHandle
= fopen( $this->mTempPath
, 'wb' );
151 if ( !$this->mTmpHandle
) {
152 return Status
::newFatal( 'tmp-create-error' );
155 $this->mRemoveTempFile
= true;
156 $this->mFileSize
= 0;
158 $req = MWHttpRequest
::factory( $this->mUrl
, array(
159 'followRedirects' => true
161 $req->setCallback( array( $this, 'saveTempFileChunk' ) );
162 $status = $req->execute();
164 if ( $this->mTmpHandle
) {
165 // File got written ok...
166 fclose( $this->mTmpHandle
);
167 $this->mTmpHandle
= null;
169 // We encountered a write error during the download...
170 return Status
::newFatal( 'tmp-write-error' );
173 if ( !$status->isOk() ) {
181 * Wrapper around the parent function in order to defer verifying the
182 * upload until the file really has been fetched.
183 * @return array|mixed
185 public function verifyUpload() {
186 if ( $this->mAsync
) {
187 return array( 'status' => UploadBase
::OK
);
189 return parent
::verifyUpload();
193 * Wrapper around the parent function in order to defer checking warnings
194 * until the file really has been fetched.
197 public function checkWarnings() {
198 if ( $this->mAsync
) {
199 $this->mIgnoreWarnings
= false;
202 return parent
::checkWarnings();
206 * Wrapper around the parent function in order to defer checking protection
207 * until we are sure that the file can actually be uploaded
210 public function verifyTitlePermissions( $user ) {
211 if ( $this->mAsync
) {
214 return parent
::verifyTitlePermissions( $user );
218 * Wrapper around the parent function in order to defer uploading to the
219 * job queue for asynchronous uploads
222 public function performUpload( $comment, $pageText, $watch, $user ) {
223 if ( $this->mAsync
) {
224 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
226 return Status
::newFatal( 'async', $sessionKey );
229 return parent
::performUpload( $comment, $pageText, $watch, $user );
239 protected function insertJob( $comment, $pageText, $watch, $user ) {
240 $sessionKey = $this->stashSession();
241 $job = new UploadFromUrlJob( $this->getTitle(), array(
242 'url' => $this->mUrl
,
243 'comment' => $comment,
244 'pageText' => $pageText,
246 'userName' => $user->getName(),
247 'leaveMessage' => $this->mAsync
== 'async-leavemessage',
248 'ignoreWarnings' => $this->mIgnoreWarnings
,
249 'sessionId' => session_id(),
250 'sessionKey' => $sessionKey,
252 $job->initializeSessionData();