Start adding support for leaving user's messages. Begin using it in UploadFromUrl.php
[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, $ignoreWarn ) {
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 /* // Check whether the user has the appropriate permissions to upload anyway */
55 /* $permission = $this->isAllowed( $wgUser ); */
56
57 /* if ( $permission !== true ) { */
58 /* if ( !$wgUser->isLoggedIn() ) { */
59 /* return Status::newFatal( 'uploadnologintext' ); */
60 /* } else { */
61 /* return Status::newFatal( 'badaccess-groups' ); */
62 /* } */
63 /* } */
64
65 /* $permErrors = $this->verifyPermissions( $wgUser ); */
66 /* if ( $permErrors !== true ) { */
67 /* return Status::newFatal( 'badaccess-groups' ); */
68 /* } */
69
70
71 $job = new UploadFromUrlJob( $title, $params );
72 return $job->insert();
73 }
74
75 /**
76 * Initialize a queued download
77 * @param $job Job
78 */
79 public function initializeFromJob( $job ) {
80 global $wgTmpDirectory;
81
82 $this->mUrl = $job->params['url'];
83 $this->mTempPath = tempnam( $wgTmpDirectory, 'COPYUPLOAD' );
84 $this->mDesiredDestName = $job->title;
85 $this->comment = $job->params['comment'];
86 $this->watchList = $job->params['watchlist'];
87 $this->ignoreWarnings = $job->params['ignorewarnings'];
88 $this->getTitle();
89 }
90
91 /**
92 * Entry point for SpecialUpload
93 * @param $request Object: WebRequest object
94 */
95 public function initializeFromRequest( &$request ) {
96 $desiredDestName = $request->getText( 'wpDestFile' );
97 if( !$desiredDestName )
98 $desiredDestName = $request->getText( 'wpUploadFileURL' );
99 return $this->initialize(
100 $desiredDestName,
101 $request->getVal( 'wpUploadFileURL' ),
102 $request->getVal( 'wpUploadDescription' ),
103 $request->getVal( 'wpWatchThis' ),
104 $request->getVal( 'wpIgnoreWarnings' )
105 );
106 }
107
108 /**
109 * @param $request Object: WebRequest object
110 */
111 public static function isValidRequest( $request ){
112 global $wgUser;
113
114 $url = $request->getVal( 'wpUploadFileURL' );
115 return !empty( $url )
116 && Http::isValidURI( $url )
117 && $wgUser->isAllowed( 'upload_by_url' );
118 }
119
120 private function saveTempFile( $req ) {
121 $filename = tempnam( wfTempDir(), 'URL' );
122 if ( $filename === false ) {
123 return Status::newFatal( 'tmp-create-error' );
124 }
125 if ( file_put_contents( $filename, $req->getContent() ) === false ) {
126 return Status::newFatal( 'tmp-write-error' );
127 }
128
129 $this->mTempPath = $filename;
130 $this->mFileSize = filesize( $filename );
131
132 return Status::newGood();
133 }
134
135 public function doUpload() {
136 global $wgUser;
137
138 $req = HttpRequest::factory($this->mUrl);
139 $status = $req->execute();
140
141 if( !$status->isOk() ) {
142 return $status;
143 }
144
145 $status = $this->saveTempFile( $req );
146 if ( !$status->isGood() ) {
147 return $status;
148 }
149
150 $this->mRemoveTempFile = true;
151
152 $v = $this->verifyUpload();
153 if( $v['status'] !== UploadBase::OK ) {
154 return $this->convertVerifyErrorToStatus( $v['status'], $v['details'] );
155 }
156
157 // This has to come from API
158 /* $warnings = $this->checkForWarnings(); */
159 /* if( isset($warnings) ) return $warnings; */
160
161 $file = $this->getLocalFile();
162 // This comes from ApiBase
163 /* $watch = $this->getWatchlistValue( $this->mParams['watchlist'], $file->getTitle() ); */
164
165 $status = $this->getLocalFile()->upload( $this->mTempPath, $this->comment,
166 $this->comment, File::DELETE_SOURCE, $this->mFileProps, false, $wgUser );
167
168 if ( $status->isGood() ) {
169 $url = $this->getLocalFile()->getDescriptionUrl();
170 $wgUser->leaveUserMessage( wfMsg( 'successfulupload' ),
171 wfMsg( 'upload-success-msg', $url ) );
172 } else {
173 $wgUser->leaveUserMessage( wfMsg( 'upload-failure-subj' ),
174 wfMsg( 'upload-failure-msg', $status->getWikiText() ) );
175 }
176
177 return $status;
178 }
179 }