6 * Implements uploading from a HTTP resource.
8 * @author Bryan Tong Minh
11 class UploadFromUrl
extends UploadBase
{
12 protected $mTempDownloadPath;
13 protected $comment, $watchList;
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.
19 public static function isAllowed( $user ) {
20 if( !$user->isAllowed( 'upload_by_url' ) )
21 return 'upload_by_url';
22 return parent
::isAllowed( $user );
26 * 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
36 public function initialize( $name, $url, $comment, $watchlist ) {
39 if( !Http
::isValidURI( $url ) ) {
40 return Status
::newFatal( 'http-invalid-url' );
43 "userName" => $wgUser->getName(),
44 "userID" => $wgUser->getID(),
45 "url" => trim( $url ),
46 "timestamp" => wfTimestampNow(),
47 "comment" => $comment,
48 "watchlist" => $watchlist);
50 $title = Title
::newFromText( $name );
51 /* // Check whether the user has the appropriate permissions to upload anyway */
52 /* $permission = $this->isAllowed( $wgUser ); */
54 /* if ( $permission !== true ) { */
55 /* if ( !$wgUser->isLoggedIn() ) { */
56 /* return Status::newFatal( 'uploadnologintext' ); */
58 /* return Status::newFatal( 'badaccess-groups' ); */
62 /* $permErrors = $this->verifyPermissions( $wgUser ); */
63 /* if ( $permErrors !== true ) { */
64 /* return Status::newFatal( 'badaccess-groups' ); */
68 $job = new UploadFromUrlJob( $title, $params );
73 * Initialize a queued download
76 public function initializeFromJob( $job ) {
77 $this->mUrl
= $job->params
['url'];
78 $this->mTempPath
= tempnam( $wgTmpDirectory, 'COPYUPLOAD' );
79 $this->mDesiredDestName
= $job->title
;
80 $this->comment
= $job->params
['comment'];
81 $this->watchList
= $job->params
['watchlist'];
86 * Entry point for SpecialUpload
87 * @param $request Object: WebRequest object
89 public function initializeFromRequest( &$request ) {
90 $desiredDestName = $request->getText( 'wpDestFile' );
91 if( !$desiredDestName )
92 $desiredDestName = $request->getText( 'wpUploadFileURL' );
93 return $this->initialize(
95 $request->getVal( 'wpUploadFileURL' ),
101 * @param $request Object: WebRequest object
103 public static function isValidRequest( $request ){
104 if( !$request->getVal( 'wpUploadFileURL' ) )
106 // check that is a valid url:
107 return self
::isValidUrl( $request->getVal( 'wpUploadFileURL' ) );
110 private function saveTempFile( $req ) {
111 $filename = tempnam( wfTempDir(), 'URL' );
112 if ( $filename === false ) {
113 return Status
::newFatal( 'tmp-create-error' );
115 if ( file_put_contents( $filename, $req->getContent() ) === false ) {
116 return Status
::newFatal( 'tmp-write-error' );
119 $this->mTempPath
= $filename;
120 $this->mFileSize
= filesize( $filename );
122 return Status
::newGood();
125 public function doUpload() {
128 $req = HttpRequest
::factory($this->mUrl
);
129 $status = $req->execute();
131 if( !$status->isOk() ) {
135 $status = $this->saveTempFile( $req );
136 $this->mRemoveTempFile
= true;
138 if( !$status->isOk() ) {
142 $v = $this->verifyUpload();
143 if( $v['status'] !== UploadBase
::OK
) {
144 return $this->convertVerifyErrorToStatus( $v['status'], $v['details'] );
147 // This has to come from API
148 /* $warnings = $this->checkForWarnings(); */
149 /* if( isset($warnings) ) return $warnings; */
151 // Use comment as initial page text by default
152 if ( is_null( $this->mParams
['text'] ) ) {
153 $this->mParams
['text'] = $this->mParams
['comment'];
156 $file = $this->getLocalFile();
157 // This comes from ApiBase
158 /* $watch = $this->getWatchlistValue( $this->mParams['watchlist'], $file->getTitle() ); */
160 if ( !$status->isGood() ) {
164 $status = $this->getLocalFile()->upload( $this->mTempPath
, $this->comment
,
165 $this->pageText
, File
::DELETE_SOURCE
, $this->mFileProps
, false, $wgUser );