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
29 public static function isEnabled() {
30 global $wgAllowCopyUploads;
31 return $wgAllowCopyUploads && parent
::isEnabled();
35 * Entry point for API upload
36 * @return bool true on success
38 public function initialize( $name, $url, $comment, $watchlist ) {
41 if( !Http
::isValidURI( $url ) ) {
42 return Status
::newFatal( 'http-invalid-url' );
45 "userName" => $wgUser->getName(),
46 "userID" => $wgUser->getID(),
47 "url" => trim( $url ),
48 "timestamp" => wfTimestampNow(),
49 "comment" => $comment,
50 "watchlist" => $watchlist);
52 $title = Title
::newFromText( $name );
53 /* // Check whether the user has the appropriate permissions to upload anyway */
54 /* $permission = $this->isAllowed( $wgUser ); */
56 /* if ( $permission !== true ) { */
57 /* if ( !$wgUser->isLoggedIn() ) { */
58 /* return Status::newFatal( 'uploadnologintext' ); */
60 /* return Status::newFatal( 'badaccess-groups' ); */
64 /* $permErrors = $this->verifyPermissions( $wgUser ); */
65 /* if ( $permErrors !== true ) { */
66 /* return Status::newFatal( 'badaccess-groups' ); */
70 $job = new UploadFromUrlJob( $title, $params );
71 return $job->insert();
75 * Initialize a queued download
78 public function initializeFromJob( $job ) {
79 $this->mUrl
= $job->params
['url'];
80 $this->mTempPath
= tempnam( $wgTmpDirectory, 'COPYUPLOAD' );
81 $this->mDesiredDestName
= $job->title
;
82 $this->comment
= $job->params
['comment'];
83 $this->watchList
= $job->params
['watchlist'];
88 * Entry point for SpecialUpload
89 * @param $request Object: WebRequest object
91 public function initializeFromRequest( &$request ) {
92 $desiredDestName = $request->getText( 'wpDestFile' );
93 if( !$desiredDestName )
94 $desiredDestName = $request->getText( 'wpUploadFileURL' );
95 return $this->initialize(
97 $request->getVal( 'wpUploadFileURL' ),
104 * @param $request Object: WebRequest object
106 public static function isValidRequest( $request ){
107 if( !$request->getVal( 'wpUploadFileURL' ) )
109 // check that is a valid url:
110 return Http
::isValidURI( $request->getVal( 'wpUploadFileURL' ) );
113 private function saveTempFile( $req ) {
114 $filename = tempnam( wfTempDir(), 'URL' );
115 if ( $filename === false ) {
116 return Status
::newFatal( 'tmp-create-error' );
118 if ( file_put_contents( $filename, $req->getContent() ) === false ) {
119 return Status
::newFatal( 'tmp-write-error' );
122 $this->mTempPath
= $filename;
123 $this->mFileSize
= filesize( $filename );
125 return Status
::newGood();
128 public function doUpload() {
131 $req = HttpRequest
::factory($this->mUrl
);
132 $status = $req->execute();
134 if( !$status->isOk() ) {
138 $status = $this->saveTempFile( $req );
139 $this->mRemoveTempFile
= true;
141 if( !$status->isOk() ) {
145 $v = $this->verifyUpload();
146 if( $v['status'] !== UploadBase
::OK
) {
147 return $this->convertVerifyErrorToStatus( $v['status'], $v['details'] );
150 // This has to come from API
151 /* $warnings = $this->checkForWarnings(); */
152 /* if( isset($warnings) ) return $warnings; */
154 // Use comment as initial page text by default
155 if ( is_null( $this->mParams
['text'] ) ) {
156 $this->mParams
['text'] = $this->mParams
['comment'];
159 $file = $this->getLocalFile();
160 // This comes from ApiBase
161 /* $watch = $this->getWatchlistValue( $this->mParams['watchlist'], $file->getTitle() ); */
163 if ( !$status->isGood() ) {
167 $status = $this->getLocalFile()->upload( $this->mTempPath
, $this->comment
,
168 $this->pageText
, File
::DELETE_SOURCE
, $this->mFileProps
, false, $wgUser );