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' );
109 * Save the result of a HTTP request to the temporary file
111 * @param $req MWHttpRequest
114 private function saveTempFile( $req ) {
115 if ( $this->mTempPath
=== false ) {
116 return Status
::newFatal( 'tmp-create-error' );
118 if ( file_put_contents( $this->mTempPath
, $req->getContent() ) === false ) {
119 return Status
::newFatal( 'tmp-write-error' );
122 $this->mFileSize
= filesize( $this->mTempPath
);
124 return Status
::newGood();
127 * Download the file, save it to the temporary file and update the file
128 * size and set $mRemoveTempFile to true.
130 protected function reallyFetchFile() {
131 $req = MWHttpRequest
::factory( $this->mUrl
);
132 $status = $req->execute();
134 if ( !$status->isOk() ) {
138 $status = $this->saveTempFile( $req );
139 if ( !$status->isGood() ) {
142 $this->mRemoveTempFile
= true;
148 * Wrapper around the parent function in order to defer verifying the
149 * upload until the file really has been fetched.
151 public function verifyUpload() {
152 if ( $this->mAsync
) {
153 return array( 'status' => UploadBase
::OK
);
155 return parent
::verifyUpload();
159 * Wrapper around the parent function in order to defer checking warnings
160 * until the file really has been fetched.
162 public function checkWarnings() {
163 if ( $this->mAsync
) {
164 $this->mIgnoreWarnings
= false;
167 return parent
::checkWarnings();
171 * Wrapper around the parent function in order to defer checking protection
172 * until we are sure that the file can actually be uploaded
174 public function verifyTitlePermissions( $user ) {
175 if ( $this->mAsync
) {
178 return parent
::verifyTitlePermissions( $user );
182 * Wrapper around the parent function in order to defer uploading to the
183 * job queue for asynchronous uploads
185 public function performUpload( $comment, $pageText, $watch, $user ) {
186 if ( $this->mAsync
) {
187 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
189 $status = new Status
;
190 $status->error( 'async', $sessionKey );
194 return parent
::performUpload( $comment, $pageText, $watch, $user );
204 protected function insertJob( $comment, $pageText, $watch, $user ) {
205 $sessionKey = $this->stashSession();
206 $job = new UploadFromUrlJob( $this->getTitle(), array(
207 'url' => $this->mUrl
,
208 'comment' => $comment,
209 'pageText' => $pageText,
211 'userName' => $user->getName(),
212 'leaveMessage' => $this->mAsync
== 'async-leavemessage',
213 'ignoreWarnings' => $this->mIgnoreWarnings
,
214 'sessionId' => session_id(),
215 'sessionKey' => $sessionKey,
217 $job->initializeSessionData();