Index: trunk/phase3/includes/api/ApiUpload.php |
— | — | @@ -45,12 +45,58 @@ |
46 | 46 | $this->dieUsageMsg( array( 'uploaddisabled' ) ); |
47 | 47 | } |
48 | 48 | |
| 49 | + // Parameter handling |
49 | 50 | $this->mParams = $this->extractRequestParams(); |
50 | 51 | $request = $this->getMain()->getRequest(); |
51 | | - |
52 | 52 | // Add the uploaded file to the params array |
53 | 53 | $this->mParams['file'] = $request->getFileName( 'file' ); |
54 | 54 | |
| 55 | + // Select an upload module |
| 56 | + $this->selectUploadModule(); |
| 57 | + if ( !isset( $this->mUpload ) ) { |
| 58 | + $this->dieUsage( 'No upload module set', 'nomodule' ); |
| 59 | + } |
| 60 | + |
| 61 | + // First check permission to upload |
| 62 | + $this->checkPermissions( $wgUser ); |
| 63 | + // Check permission to upload this file |
| 64 | + $permErrors = $this->mUpload->verifyPermissions( $wgUser ); |
| 65 | + if ( $permErrors !== true ) { |
| 66 | + // Todo: more specific error message |
| 67 | + $this->dieUsageMsg( array( 'badaccess-groups' ) ); |
| 68 | + } |
| 69 | + |
| 70 | + // Fetch the file |
| 71 | + $status = $this->mUpload->fetchFile(); |
| 72 | + if ( !$status->isGood() ) { |
| 73 | + $errors = $status->getErrorsArray(); |
| 74 | + $error = array_shift( $errors[0] ); |
| 75 | + $this->dieUsage( 'Error fetching file from remote source', $error, 0, $errors[0] ); |
| 76 | + } |
| 77 | + |
| 78 | + // Check if the uploaded file is sane |
| 79 | + $this->verifyUpload(); |
| 80 | + |
| 81 | + // Check warnings if necessary |
| 82 | + $warnings = $this->checkForWarnings(); |
| 83 | + if ( $warnings ) { |
| 84 | + $this->getResult()->addValue( null, $this->getModuleName(), $warnings ); |
| 85 | + } else { |
| 86 | + // Perform the upload |
| 87 | + $result = $this->performUpload(); |
| 88 | + $this->getResult()->addValue( null, $this->getModuleName(), $result ); |
| 89 | + } |
| 90 | + |
| 91 | + // Cleanup any temporary mess |
| 92 | + $this->mUpload->cleanupTempFile(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Select an upload module and set it to mUpload. Dies on failure. |
| 97 | + */ |
| 98 | + protected function selectUploadModule() { |
| 99 | + $request = $this->getMain()->getRequest(); |
| 100 | + |
55 | 101 | // One and only one of the following parameters is needed |
56 | 102 | $this->requireOnlyOneParameter( $this->mParams, |
57 | 103 | 'sessionkey', 'file', 'url' ); |
— | — | @@ -61,7 +107,7 @@ |
62 | 108 | |
63 | 109 | if ( $this->mParams['sessionkey'] ) { |
64 | 110 | // Upload stashed in a previous request |
65 | | - $sessionData = $request->getSessionData( UploadBase::SESSION_KEYNAME ); |
| 111 | + $sessionData = $request->getSessionData( UploadBase::getSessionKeyName() ); |
66 | 112 | if ( !UploadFromStash::isValidSessionKey( $this->mParams['sessionkey'], $sessionData ) ) { |
67 | 113 | $this->dieUsageMsg( array( 'invalid-session-key' ) ); |
68 | 114 | } |
— | — | @@ -97,42 +143,6 @@ |
98 | 144 | $this->mParams['url'], $async ); |
99 | 145 | |
100 | 146 | } |
101 | | - if ( !isset( $this->mUpload ) ) { |
102 | | - $this->dieUsage( 'No upload module set', 'nomodule' ); |
103 | | - } |
104 | | - |
105 | | - // First check permission to upload |
106 | | - $this->checkPermissions( $wgUser ); |
107 | | - // Check permission to upload this file |
108 | | - $permErrors = $this->mUpload->verifyPermissions( $wgUser ); |
109 | | - if ( $permErrors !== true ) { |
110 | | - // Todo: more specific error message |
111 | | - $this->dieUsageMsg( array( 'badaccess-groups' ) ); |
112 | | - } |
113 | | - |
114 | | - // Fetch the file |
115 | | - $status = $this->mUpload->fetchFile(); |
116 | | - if ( !$status->isGood() ) { |
117 | | - $errors = $status->getErrorsArray(); |
118 | | - $error = array_shift( $errors[0] ); |
119 | | - $this->dieUsage( 'Error fetching file from remote source', $error, 0, $errors[0] ); |
120 | | - } |
121 | | - |
122 | | - // Check if the uploaded file is sane |
123 | | - $this->verifyUpload(); |
124 | | - |
125 | | - // Check warnings if necessary |
126 | | - $warnings = $this->checkForWarnings(); |
127 | | - if ( $warnings ) { |
128 | | - $this->getResult()->addValue( null, $this->getModuleName(), $warnings ); |
129 | | - } else { |
130 | | - // Perform the upload |
131 | | - $result = $this->performUpload(); |
132 | | - $this->getResult()->addValue( null, $this->getModuleName(), $result ); |
133 | | - } |
134 | | - |
135 | | - // Cleanup any temporary mess |
136 | | - $this->mUpload->cleanupTempFile(); |
137 | 147 | } |
138 | 148 | |
139 | 149 | /** |
— | — | @@ -156,22 +166,12 @@ |
157 | 167 | /** |
158 | 168 | * Performs file verification, dies on error. |
159 | 169 | */ |
160 | | - public function verifyUpload( ) { |
| 170 | + protected function verifyUpload( ) { |
161 | 171 | $verification = $this->mUpload->verifyUpload( ); |
162 | 172 | if ( $verification['status'] === UploadBase::OK ) { |
163 | | - return $verification; |
| 173 | + return; |
164 | 174 | } |
165 | 175 | |
166 | | - $this->getVerificationError( $verification ); |
167 | | - } |
168 | | - |
169 | | - /** |
170 | | - * Produce the usage error |
171 | | - * |
172 | | - * @param $verification array an associative array with the status |
173 | | - * key |
174 | | - */ |
175 | | - public function getVerificationError( $verification ) { |
176 | 176 | // TODO: Move them to ApiBase's message map |
177 | 177 | switch( $verification['status'] ) { |
178 | 178 | case UploadBase::EMPTY_FILE: |
— | — | @@ -217,6 +217,10 @@ |
218 | 218 | } |
219 | 219 | } |
220 | 220 | |
| 221 | + /** |
| 222 | + * Check warnings if ignorewarnings is not set. |
| 223 | + * Returns a suitable result array if there were warnings |
| 224 | + */ |
221 | 225 | protected function checkForWarnings() { |
222 | 226 | $result = array(); |
223 | 227 | |
— | — | @@ -256,6 +260,10 @@ |
257 | 261 | return; |
258 | 262 | } |
259 | 263 | |
| 264 | + /** |
| 265 | + * Perform the actual upload. Returns a suitable result array on success; |
| 266 | + * dies on failure. |
| 267 | + */ |
260 | 268 | protected function performUpload() { |
261 | 269 | global $wgUser; |
262 | 270 | |
— | — | @@ -278,7 +286,7 @@ |
279 | 287 | |
280 | 288 | if ( !$status->isGood() ) { |
281 | 289 | $error = $status->getErrorsArray(); |
282 | | - $this->getResult()->setIndexedTagName( $result['details'], 'error' ); |
| 290 | + $this->getResult()->setIndexedTagName( $error, 'error' ); |
283 | 291 | |
284 | 292 | $this->dieUsage( 'An internal error occurred', 'internal-error', 0, $error ); |
285 | 293 | } |