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.
25 public static function isAllowed( $user ) {
26 if ( !$user->isAllowed( 'upload_by_url' ) ) {
27 return 'upload_by_url';
29 return parent
::isAllowed( $user );
33 * Checks if the upload from URL feature is enabled
36 public static function isEnabled() {
37 global $wgAllowCopyUploads;
38 return $wgAllowCopyUploads && parent
::isEnabled();
42 * Entry point for API upload
46 * @param $async mixed Whether the download should be performed
47 * asynchronous. False for synchronous, async or async-leavemessage for
48 * asynchronous download.
50 public function initialize( $name, $url, $async = false ) {
51 global $wgAllowAsyncCopyUploads;
54 $this->mAsync
= $wgAllowAsyncCopyUploads ?
$async : false;
56 throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
59 $tempPath = $this->mAsync ?
null : $this->makeTemporaryFile();
60 # File size and removeTempFile will be filled in later
61 $this->initializePathInfo( $name, $tempPath, 0, false );
65 * Entry point for SpecialUpload
66 * @param $request WebRequest object
68 public function initializeFromRequest( &$request ) {
69 $desiredDestName = $request->getText( 'wpDestFile' );
70 if ( !$desiredDestName )
71 $desiredDestName = $request->getText( 'wpUploadFileURL' );
72 return $this->initialize(
74 trim( $request->getVal( 'wpUploadFileURL' ) ),
80 * @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' );
91 public function getSourceType() { return 'url'; }
93 public function fetchFile() {
94 if ( !Http
::isValidURI( $this->mUrl
) ) {
95 return Status
::newFatal( 'http-invalid-url' );
98 if ( !$this->mAsync
) {
99 return $this->reallyFetchFile();
101 return Status
::newGood();
104 * Create a new temporary file in the URL subdirectory of wfTempDir().
106 * @return string Path to the file
108 protected function makeTemporaryFile() {
109 return tempnam( wfTempDir(), 'URL' );
113 * Callback: save a chunk of the result of a HTTP request to the temporary file
116 * @param $buffer string
117 * @return int number of bytes handled
119 public function saveTempFileChunk( $req, $buffer ) {
120 $nbytes = fwrite( $this->mTmpHandle
, $buffer );
122 if ( $nbytes == strlen( $buffer ) ) {
123 $this->mFileSize +
= $nbytes;
125 // Well... that's not good!
126 fclose( $this->mTmpHandle
);
127 $this->mTmpHandle
= false;
134 * Download the file, save it to the temporary file and update the file
135 * size and set $mRemoveTempFile to true.
137 protected function reallyFetchFile() {
138 if ( $this->mTempPath
=== false ) {
139 return Status
::newFatal( 'tmp-create-error' );
142 // Note the temporary file should already be created by makeTemporaryFile()
143 $this->mTmpHandle
= fopen( $this->mTempPath
, 'wb' );
144 if ( !$this->mTmpHandle
) {
145 return Status
::newFatal( 'tmp-create-error' );
148 $this->mRemoveTempFile
= true;
149 $this->mFileSize
= 0;
151 $req = MWHttpRequest
::factory( $this->mUrl
, array(
152 'followRedirects' => true
154 $req->setCallback( array( $this, 'saveTempFileChunk' ) );
155 $status = $req->execute();
157 if ( $this->mTmpHandle
) {
158 // File got written ok...
159 fclose( $this->mTmpHandle
);
160 $this->mTmpHandle
= null;
162 // We encountered a write error during the download...
163 return Status
::newFatal( 'tmp-write-error' );
166 if ( !$status->isOk() ) {
174 * Wrapper around the parent function in order to defer verifying the
175 * upload until the file really has been fetched.
177 public function verifyUpload() {
178 if ( $this->mAsync
) {
179 return array( 'status' => UploadBase
::OK
);
181 return parent
::verifyUpload();
185 * Wrapper around the parent function in order to defer checking warnings
186 * until the file really has been fetched.
188 public function checkWarnings() {
189 if ( $this->mAsync
) {
190 $this->mIgnoreWarnings
= false;
193 return parent
::checkWarnings();
197 * Wrapper around the parent function in order to defer checking protection
198 * until we are sure that the file can actually be uploaded
200 public function verifyTitlePermissions( $user ) {
201 if ( $this->mAsync
) {
204 return parent
::verifyTitlePermissions( $user );
208 * Wrapper around the parent function in order to defer uploading to the
209 * job queue for asynchronous uploads
211 public function performUpload( $comment, $pageText, $watch, $user ) {
212 if ( $this->mAsync
) {
213 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
215 $status = new Status
;
216 $status->error( 'async', $sessionKey );
220 return parent
::performUpload( $comment, $pageText, $watch, $user );
230 protected function insertJob( $comment, $pageText, $watch, $user ) {
231 $sessionKey = $this->stashSession();
232 $job = new UploadFromUrlJob( $this->getTitle(), array(
233 'url' => $this->mUrl
,
234 'comment' => $comment,
235 'pageText' => $pageText,
237 'userName' => $user->getName(),
238 'leaveMessage' => $this->mAsync
== 'async-leavemessage',
239 'ignoreWarnings' => $this->mIgnoreWarnings
,
240 'sessionId' => session_id(),
241 'sessionKey' => $sessionKey,
243 $job->initializeSessionData();