r70097 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r70096‎ | r70097 | r70098 >
Date:17:31, 28 July 2010
Author:btongminh
Status:ok
Tags:
Comment:
Refactor some code out of execute into selectUploadModule. Fixed an undefined variable.
Modified paths:
  • /trunk/phase3/includes/api/ApiUpload.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiUpload.php
@@ -45,12 +45,58 @@
4646 $this->dieUsageMsg( array( 'uploaddisabled' ) );
4747 }
4848
 49+ // Parameter handling
4950 $this->mParams = $this->extractRequestParams();
5051 $request = $this->getMain()->getRequest();
51 -
5252 // Add the uploaded file to the params array
5353 $this->mParams['file'] = $request->getFileName( 'file' );
5454
 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+
55101 // One and only one of the following parameters is needed
56102 $this->requireOnlyOneParameter( $this->mParams,
57103 'sessionkey', 'file', 'url' );
@@ -61,7 +107,7 @@
62108
63109 if ( $this->mParams['sessionkey'] ) {
64110 // Upload stashed in a previous request
65 - $sessionData = $request->getSessionData( UploadBase::SESSION_KEYNAME );
 111+ $sessionData = $request->getSessionData( UploadBase::getSessionKeyName() );
66112 if ( !UploadFromStash::isValidSessionKey( $this->mParams['sessionkey'], $sessionData ) ) {
67113 $this->dieUsageMsg( array( 'invalid-session-key' ) );
68114 }
@@ -97,42 +143,6 @@
98144 $this->mParams['url'], $async );
99145
100146 }
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();
137147 }
138148
139149 /**
@@ -156,22 +166,12 @@
157167 /**
158168 * Performs file verification, dies on error.
159169 */
160 - public function verifyUpload( ) {
 170+ protected function verifyUpload( ) {
161171 $verification = $this->mUpload->verifyUpload( );
162172 if ( $verification['status'] === UploadBase::OK ) {
163 - return $verification;
 173+ return;
164174 }
165175
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 ) {
176176 // TODO: Move them to ApiBase's message map
177177 switch( $verification['status'] ) {
178178 case UploadBase::EMPTY_FILE:
@@ -217,6 +217,10 @@
218218 }
219219 }
220220
 221+ /**
 222+ * Check warnings if ignorewarnings is not set.
 223+ * Returns a suitable result array if there were warnings
 224+ */
221225 protected function checkForWarnings() {
222226 $result = array();
223227
@@ -256,6 +260,10 @@
257261 return;
258262 }
259263
 264+ /**
 265+ * Perform the actual upload. Returns a suitable result array on success;
 266+ * dies on failure.
 267+ */
260268 protected function performUpload() {
261269 global $wgUser;
262270
@@ -278,7 +286,7 @@
279287
280288 if ( !$status->isGood() ) {
281289 $error = $status->getErrorsArray();
282 - $this->getResult()->setIndexedTagName( $result['details'], 'error' );
 290+ $this->getResult()->setIndexedTagName( $error, 'error' );
283291
284292 $this->dieUsage( 'An internal error occurred', 'internal-error', 0, $error );
285293 }

Status & tagging log