3 * Implements uploading from a HTTP resource.
7 * @author Bryan Tong Minh
11 class UploadFromUrl
extends UploadBase
{
12 protected $mAsync, $mUrl;
13 protected $mIgnoreWarnings = true;
18 * Checks if the user is allowed to use the upload-by-URL feature. If the
19 * 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';
26 return parent
::isAllowed( $user );
30 * Checks if the upload from URL feature is enabled
33 public static function isEnabled() {
34 global $wgAllowCopyUploads;
35 return $wgAllowCopyUploads && parent
::isEnabled();
39 * Entry point for API upload
43 * @param $async mixed Whether the download should be performed
44 * asynchronous. False for synchronous, async or async-leavemessage for
45 * asynchronous download.
47 public function initialize( $name, $url, $async = false ) {
48 global $wgAllowAsyncCopyUploads;
51 $this->mAsync
= $wgAllowAsyncCopyUploads ?
$async : false;
53 throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
56 $tempPath = $this->mAsync ?
null : $this->makeTemporaryFile();
57 # File size and removeTempFile will be filled in later
58 $this->initializePathInfo( $name, $tempPath, 0, false );
62 * Entry point for SpecialUpload
63 * @param $request WebRequest object
65 public function initializeFromRequest( &$request ) {
66 $desiredDestName = $request->getText( 'wpDestFile' );
67 if ( !$desiredDestName )
68 $desiredDestName = $request->getText( 'wpUploadFileURL' );
69 return $this->initialize(
71 trim( $request->getVal( 'wpUploadFileURL' ) ),
77 * @param $request WebRequest object
79 public static function isValidRequest( $request ) {
82 $url = $request->getVal( 'wpUploadFileURL' );
84 && Http
::isValidURI( $url )
85 && $wgUser->isAllowed( 'upload_by_url' );
88 public function getSourceType() { return 'url'; }
90 public function fetchFile() {
91 if ( !Http
::isValidURI( $this->mUrl
) ) {
92 return Status
::newFatal( 'http-invalid-url' );
95 if ( !$this->mAsync
) {
96 return $this->reallyFetchFile();
98 return Status
::newGood();
101 * Create a new temporary file in the URL subdirectory of wfTempDir().
103 * @return string Path to the file
105 protected function makeTemporaryFile() {
106 return tempnam( wfTempDir(), 'URL' );
110 * Callback: save a chunk of the result of a HTTP request to the temporary file
113 * @param $buffer string
114 * @return int number of bytes handled
116 public function saveTempFileChunk( $req, $buffer ) {
117 $nbytes = fwrite( $this->mTmpHandle
, $buffer );
119 if ( $nbytes == strlen( $buffer ) ) {
120 $this->mFileSize +
= $nbytes;
122 // Well... that's not good!
123 fclose( $this->mTmpHandle
);
124 $this->mTmpHandle
= false;
131 * Download the file, save it to the temporary file and update the file
132 * size and set $mRemoveTempFile to true.
134 protected function reallyFetchFile() {
135 if ( $this->mTempPath
=== false ) {
136 return Status
::newFatal( 'tmp-create-error' );
139 // Note the temporary file should already be created by makeTemporaryFile()
140 $this->mTmpHandle
= fopen( $this->mTempPath
, 'wb' );
141 if ( !$this->mTmpHandle
) {
142 return Status
::newFatal( 'tmp-create-error' );
145 $this->mRemoveTempFile
= true;
146 $this->mFileSize
= 0;
148 $req = MWHttpRequest
::factory( $this->mUrl
);
149 $req->setCallback( array( $this, 'saveTempFileChunk' ) );
150 $status = $req->execute();
152 if ( $this->mTmpHandle
) {
153 // File got written ok...
154 fclose( $this->mTmpHandle
);
155 $this->mTmpHandle
= null;
157 // We encountered a write error during the download...
158 return Status
::newFatal( 'tmp-write-error' );
161 if ( !$status->isOk() ) {
169 * Wrapper around the parent function in order to defer verifying the
170 * upload until the file really has been fetched.
172 public function verifyUpload() {
173 if ( $this->mAsync
) {
174 return array( 'status' => UploadBase
::OK
);
176 return parent
::verifyUpload();
180 * Wrapper around the parent function in order to defer checking warnings
181 * until the file really has been fetched.
183 public function checkWarnings() {
184 if ( $this->mAsync
) {
185 $this->mIgnoreWarnings
= false;
188 return parent
::checkWarnings();
192 * Wrapper around the parent function in order to defer checking protection
193 * until we are sure that the file can actually be uploaded
195 public function verifyTitlePermissions( $user ) {
196 if ( $this->mAsync
) {
199 return parent
::verifyTitlePermissions( $user );
203 * Wrapper around the parent function in order to defer uploading to the
204 * job queue for asynchronous uploads
206 public function performUpload( $comment, $pageText, $watch, $user ) {
207 if ( $this->mAsync
) {
208 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
210 $status = new Status
;
211 $status->error( 'async', $sessionKey );
215 return parent
::performUpload( $comment, $pageText, $watch, $user );
225 protected function insertJob( $comment, $pageText, $watch, $user ) {
226 $sessionKey = $this->stashSession();
227 $job = new UploadFromUrlJob( $this->getTitle(), array(
228 'url' => $this->mUrl
,
229 'comment' => $comment,
230 'pageText' => $pageText,
232 'userName' => $user->getName(),
233 'leaveMessage' => $this->mAsync
== 'async-leavemessage',
234 'ignoreWarnings' => $this->mIgnoreWarnings
,
235 'sessionId' => session_id(),
236 'sessionKey' => $sessionKey,
238 $job->initializeSessionData();