6 * Implements uploading from a HTTP resource.
8 * @author Bryan Tong Minh
11 class UploadFromUrl
extends UploadBase
{
12 protected $mTempDownloadPath;
13 protected $comment, $watchList, $ignoreWarnings;
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, $ignoreWarn ) {
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,
51 "ignorewarnings" => $ignoreWarn);
53 $title = Title
::newFromText( $name );
54 /* // Check whether the user has the appropriate permissions to upload anyway */
55 /* $permission = $this->isAllowed( $wgUser ); */
57 /* if ( $permission !== true ) { */
58 /* if ( !$wgUser->isLoggedIn() ) { */
59 /* return Status::newFatal( 'uploadnologintext' ); */
61 /* return Status::newFatal( 'badaccess-groups' ); */
65 /* $permErrors = $this->verifyPermissions( $wgUser ); */
66 /* if ( $permErrors !== true ) { */
67 /* return Status::newFatal( 'badaccess-groups' ); */
71 $job = new UploadFromUrlJob( $title, $params );
72 return $job->insert();
76 * Initialize a queued download
79 public function initializeFromJob( $job ) {
80 $this->mUrl
= $job->params
['url'];
81 $this->mTempPath
= tempnam( $wgTmpDirectory, 'COPYUPLOAD' );
82 $this->mDesiredDestName
= $job->title
;
83 $this->comment
= $job->params
['comment'];
84 $this->watchList
= $job->params
['watchlist'];
85 $this->ignoreWarnings
= $job->params
['ignorewarnings'];
90 * Entry point for SpecialUpload
91 * @param $request Object: WebRequest object
93 public function initializeFromRequest( &$request ) {
94 $desiredDestName = $request->getText( 'wpDestFile' );
95 if( !$desiredDestName )
96 $desiredDestName = $request->getText( 'wpUploadFileURL' );
97 return $this->initialize(
99 $request->getVal( 'wpUploadFileURL' ),
100 $request->getVal( 'wpUploadDescription' ),
101 $request->getVal( 'wpWatchThis' ),
102 $request->getVal( 'wpIgnoreWarnings' )
107 * @param $request Object: WebRequest object
109 public static function isValidRequest( $request ){
112 $url = $request->getVal( 'wpUploadFileURL' );
113 return !empty( $url )
114 && Http
::isValidURI( $url )
115 && $wgUser->isAllowed( 'upload_by_url' );
118 private function saveTempFile( $req ) {
119 $filename = tempnam( wfTempDir(), 'URL' );
120 if ( $filename === false ) {
121 return Status
::newFatal( 'tmp-create-error' );
123 if ( file_put_contents( $filename, $req->getContent() ) === false ) {
124 return Status
::newFatal( 'tmp-write-error' );
127 $this->mTempPath
= $filename;
128 $this->mFileSize
= filesize( $filename );
130 return Status
::newGood();
133 public function doUpload() {
136 $req = HttpRequest
::factory($this->mUrl
);
137 $status = $req->execute();
139 if( !$status->isOk() ) {
143 $status = $this->saveTempFile( $req );
144 $this->mRemoveTempFile
= true;
146 if( !$status->isOk() ) {
150 $v = $this->verifyUpload();
151 if( $v['status'] !== UploadBase
::OK
) {
152 return $this->convertVerifyErrorToStatus( $v['status'], $v['details'] );
155 // This has to come from API
156 /* $warnings = $this->checkForWarnings(); */
157 /* if( isset($warnings) ) return $warnings; */
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->comment
, File
::DELETE_SOURCE
, $this->mFileProps
, false, $wgUser );