6 * Implements uploading from a HTTP resource.
8 * @author Bryan Tong Minh
11 class UploadFromUrl
extends UploadBase
{
12 protected $mAsync, $mUrl;
15 * Checks if the user is allowed to use the upload-by-URL feature. If the
16 * user is allowed, pass on permissions checking to the parent.
18 public static function isAllowed( $user ) {
19 if ( !$user->isAllowed( 'upload_by_url' ) )
20 return 'upload_by_url';
21 return parent
::isAllowed( $user );
25 * Checks if the upload from URL feature is enabled
28 public static function isEnabled() {
29 global $wgAllowCopyUploads;
30 return $wgAllowCopyUploads && parent
::isEnabled();
34 * Entry point for API upload
38 * @param $async mixed Whether the download should be performed
39 * asynchronous. False for synchronous, async or async-leavemessage for
40 * asynchronous download.
42 public function initialize( $name, $url, $async = false ) {
46 $this->mAsync
= $async;
48 $tempPath = $async ?
null : $this->makeTemporaryFile();
49 # File size and removeTempFile will be filled in later
50 $this->initializePathInfo( $name, $tempPath, 0, false );
54 * Entry point for SpecialUpload
55 * @param $request Object: WebRequest object
57 public function initializeFromRequest( &$request ) {
58 $desiredDestName = $request->getText( 'wpDestFile' );
59 if ( !$desiredDestName )
60 $desiredDestName = $request->getText( 'wpUploadFileURL' );
61 return $this->initialize(
63 $request->getVal( 'wpUploadFileURL' ),
69 * @param $request Object: WebRequest object
71 public static function isValidRequest( $request ) {
74 $url = $request->getVal( 'wpUploadFileURL' );
76 && Http
::isValidURI( $url )
77 && $wgUser->isAllowed( 'upload_by_url' );
81 public function fetchFile() {
82 if ( !Http
::isValidURI( $this->mUrl
) ) {
83 return Status
::newFatal( 'http-invalid-url' );
86 if ( !$this->mAsync
) {
87 return $this->reallyFetchFile();
89 return Status
::newGood();
91 protected function makeTemporaryFile() {
92 return tempnam( wfTempDir(), 'URL' );
94 private function saveTempFile( $req ) {
95 if ( $this->mTempPath
=== false ) {
96 return Status
::newFatal( 'tmp-create-error' );
98 if ( file_put_contents( $this->mTempPath
, $req->getContent() ) === false ) {
99 return Status
::newFatal( 'tmp-write-error' );
102 $this->mFileSize
= filesize( $this->mTempPath
);
104 return Status
::newGood();
106 protected function reallyFetchFile() {
107 $req = HttpRequest
::factory( $this->mUrl
);
108 $status = $req->execute();
110 if ( !$status->isOk() ) {
114 $status = $this->saveTempFile( $req );
115 if ( !$status->isGood() ) {
118 $this->mRemoveTempFile
= true;
123 public function performUpload( $comment, $pageText, $watch, $user ) {
124 if ( $this->mAsync
) {
125 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
127 $status = new Status
;
128 $status->error( 'async', $sessionKey );
132 return parent
::performUpload( $comment, $pageText, $watch, $user );
136 protected function insertJob( $comment, $pageText, $watch, $user ) {
137 $sessionKey = $this->getSessionKey();
138 $job = new UploadFromUrlJob( $this->getTitle(), array(
139 'url' => $this->mUrl
,
140 'comment' => $comment,
141 'pageText' => $pageText,
143 'userName' => $user->getName(),
144 'leaveMessage' => $this->mAsync
== 'async-leavemessage',
145 'ignoreWarnings' => $this->mIgnoreWarnings
,
146 'sessionKey' => $sessionKey,