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' ),
103 * @param $request Object: WebRequest object
105 public static function isValidRequest( $request ){
106 if( !$request->getVal( 'wpUploadFileURL' ) )
108 // check that is a valid url:
109 return Http
::isValidURI( $request->getVal( 'wpUploadFileURL' ) );
112 private function saveTempFile( $req ) {
113 $filename = tempnam( wfTempDir(), 'URL' );
114 if ( $filename === false ) {
115 return Status
::newFatal( 'tmp-create-error' );
117 if ( file_put_contents( $filename, $req->getContent() ) === false ) {
118 return Status
::newFatal( 'tmp-write-error' );
121 $this->mTempPath
= $filename;
122 $this->mFileSize
= filesize( $filename );
124 return Status
::newGood();
127 public function doUpload() {
130 $req = HttpRequest
::factory($this->mUrl
);
131 $status = $req->execute();
133 if( !$status->isOk() ) {
137 $status = $this->saveTempFile( $req );
138 $this->mRemoveTempFile
= true;
140 if( !$status->isOk() ) {
144 $v = $this->verifyUpload();
145 if( $v['status'] !== UploadBase
::OK
) {
146 return $this->convertVerifyErrorToStatus( $v['status'], $v['details'] );
149 // This has to come from API
150 /* $warnings = $this->checkForWarnings(); */
151 /* if( isset($warnings) ) return $warnings; */
153 // Use comment as initial page text by default
154 if ( is_null( $this->mParams
['text'] ) ) {
155 $this->mParams
['text'] = $this->mParams
['comment'];
158 $file = $this->getLocalFile();
159 // This comes from ApiBase
160 /* $watch = $this->getWatchlistValue( $this->mParams['watchlist'], $file->getTitle() ); */
162 if ( !$status->isGood() ) {
166 $status = $this->getLocalFile()->upload( $this->mTempPath
, $this->comment
,
167 $this->pageText
, File
::DELETE_SOURCE
, $this->mFileProps
, false, $wgUser );