Follow-up r70037: Fix ApiUpload by passing a WebRequestUpload to the the initializer
[lhc/web/wiklou.git] / includes / upload / UploadFromUrl.php
1 <?php
2 /**
3 * @file
4 * @ingroup upload
5 *
6 * Implements uploading from a HTTP resource.
7 *
8 * @author Bryan Tong Minh
9 * @author Michael Dale
10 */
11 class UploadFromUrl extends UploadBase {
12 protected $mTempDownloadPath;
13 protected $comment, $watchList, $ignoreWarnings;
14
15 /**
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.
18 */
19 public static function isAllowed( $user ) {
20 if( !$user->isAllowed( 'upload_by_url' ) )
21 return 'upload_by_url';
22 return parent::isAllowed( $user );
23 }
24
25 /**
26 * Checks if the upload from URL feature is enabled
27 * @return bool
28 */
29 public static function isEnabled() {
30 global $wgAllowCopyUploads;
31 return $wgAllowCopyUploads && parent::isEnabled();
32 }
33
34 /**
35 * Entry point for API upload
36 * @return bool true on success
37 */
38 public function initialize( $name, $url, $comment, $watchList = null, $ignoreWarn = null, $async = 'async') {
39 global $wgUser;
40
41 if( !Http::isValidURI( $url ) ) {
42 return Status::newFatal( 'http-invalid-url' );
43 }
44 $params = array(
45 "userName" => $wgUser->getName(),
46 "userID" => $wgUser->getID(),
47 "url" => trim( $url ),
48 "timestamp" => wfTimestampNow(),
49 "comment" => $comment,
50 "watchlist" => $watchList,
51 "ignorewarnings" => $ignoreWarn);
52
53 $title = Title::newFromText( $name );
54
55 if ( $async == 'async' ) {
56 $job = new UploadFromUrlJob( $title, $params );
57 return $job->insert();
58 }
59 else {
60 $this->mUrl = trim( $url );
61 $this->comment = $comment;
62 $this->watchList = $watchList;
63 $this->ignoreWarnings = $ignoreWarn;
64 $this->mDesiredDestName = $title;
65 $this->getTitle();
66
67 return true;
68 }
69 }
70
71 /**
72 * Initialize a queued download
73 * @param $job Job
74 */
75 public function initializeFromJob( $job ) {
76 global $wgTmpDirectory;
77
78 $this->mUrl = $job->params['url'];
79 $this->mTempPath = tempnam( $wgTmpDirectory, 'COPYUPLOAD' );
80 $this->mDesiredDestName = $job->title;
81 $this->comment = $job->params['comment'];
82 $this->watchList = $job->params['watchlist'];
83 $this->ignoreWarnings = $job->params['ignorewarnings'];
84 $this->getTitle();
85 }
86
87 /**
88 * Entry point for SpecialUpload
89 * @param $request Object: WebRequest object
90 */
91 public function initializeFromRequest( &$request ) {
92 $desiredDestName = $request->getText( 'wpDestFile' );
93 if( !$desiredDestName )
94 $desiredDestName = $request->getText( 'wpUploadFileURL' );
95 return $this->initialize(
96 $desiredDestName,
97 $request->getVal( 'wpUploadFileURL' ),
98 $request->getVal( 'wpUploadDescription' ),
99 $request->getVal( 'wpWatchThis' ),
100 $request->getVal( 'wpIgnoreWarnings' ),
101 'async'
102 );
103 }
104
105 /**
106 * @param $request Object: WebRequest object
107 */
108 public static function isValidRequest( $request ){
109 global $wgUser;
110
111 $url = $request->getVal( 'wpUploadFileURL' );
112 return !empty( $url )
113 && Http::isValidURI( $url )
114 && $wgUser->isAllowed( 'upload_by_url' );
115 }
116
117 private function saveTempFile( $req ) {
118 $filename = tempnam( wfTempDir(), 'URL' );
119 if ( $filename === false ) {
120 return Status::newFatal( 'tmp-create-error' );
121 }
122 if ( file_put_contents( $filename, $req->getContent() ) === false ) {
123 return Status::newFatal( 'tmp-write-error' );
124 }
125
126 $this->mTempPath = $filename;
127 $this->mFileSize = filesize( $filename );
128
129 return Status::newGood();
130 }
131
132 public function retrieveFileFromUrl() {
133 $req = HttpRequest::factory($this->mUrl);
134 $status = $req->execute();
135
136 if( !$status->isOk() ) {
137 return $status;
138 }
139
140 $status = $this->saveTempFile( $req );
141 if ( !$status->isGood() ) {
142 return $status;
143 }
144 $this->mRemoveTempFile = true;
145
146 return $status;
147 }
148
149 public function doUpload() {
150 global $wgUser;
151
152 $status = $this->retrieveFileFromUrl();
153
154 if ( $status->isGood() ) {
155
156 $v = $this->verifyUpload();
157 if( $v['status'] !== UploadBase::OK ) {
158 return $this->convertVerifyErrorToStatus( $v['status'], $v['details'] );
159 }
160
161 $status = $this->getLocalFile()->upload( $this->mTempPath, $this->comment,
162 $this->comment, File::DELETE_SOURCE, $this->mFileProps, false, $wgUser );
163 }
164
165 if ( $status->isGood() ) {
166 $file = $this->getLocalFile();
167
168 $wgUser->leaveUserMessage( wfMsg( 'successfulupload' ),
169 wfMsg( 'upload-success-msg', $file->getDescriptionUrl() ) );
170 } else {
171 $wgUser->leaveUserMessage( wfMsg( 'upload-failure-subj' ),
172 wfMsg( 'upload-failure-msg', $status->getWikiText() ) );
173 }
174
175 return $status;
176 }
177 }