r94594 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94593‎ | r94594 | r94595 >
Date:23:58, 15 August 2011
Author:raindrift
Status:ok (Comments)
Tags:todo 
Comment:
Removed the ability to pass a key into stashFile(), which simplifies the stash row creation a great deal.
Updated UploadFromUrlJob to properly use the database stash
followup to r92200
Modified paths:
  • /trunk/phase3/includes/job/UploadFromUrlJob.php (modified) (history)
  • /trunk/phase3/includes/upload/UploadBase.php (modified) (history)
  • /trunk/phase3/includes/upload/UploadFromStash.php (modified) (history)
  • /trunk/phase3/includes/upload/UploadStash.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/upload/UploadBase.php
@@ -745,11 +745,11 @@
746746 * @param $key String: (optional) the file key used to find the file info again. If not supplied, a key will be autogenerated.
747747 * @return UploadStashFile stashed file
748748 */
749 - public function stashFile( $key = null ) {
 749+ public function stashFile() {
750750 // was stashSessionFile
751751 $stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash();
752752
753 - $file = $stash->stashFile( $this->mTempPath, $this->getSourceType(), $key );
 753+ $file = $stash->stashFile( $this->mTempPath, $this->getSourceType() );
754754 $this->mLocalFile = $file;
755755 return $file;
756756 }
@@ -760,8 +760,8 @@
761761 * @param $key String: (optional) the file key used to find the file info again. If not supplied, a key will be autogenerated.
762762 * @return String: file key
763763 */
764 - public function stashFileGetKey( $key = null ) {
765 - return $this->stashFile( $key )->getFileKey();
 764+ public function stashFileGetKey() {
 765+ return $this->stashFile()->getFileKey();
766766 }
767767
768768 /**
@@ -770,8 +770,8 @@
771771 * @param $key String: (optional) the file key used to find the file info again. If not supplied, a key will be autogenerated.
772772 * @return String: file key
773773 */
774 - public function stashSession( $key = null ) {
775 - return $this->stashFileGetKey( $key );
 774+ public function stashSession() {
 775+ return $this->stashFileGetKey();
776776 }
777777
778778 /**
Index: trunk/phase3/includes/upload/UploadStash.php
@@ -161,7 +161,7 @@
162162 * @throws UploadStashNotLoggedInException
163163 * @return UploadStashFile: file, or null on failure
164164 */
165 - public function stashFile( $path, $sourceType = null, $key = null ) {
 165+ public function stashFile( $path, $sourceType = null ) {
166166 if ( ! file_exists( $path ) ) {
167167 wfDebug( __METHOD__ . " tried to stash file at '$path', but it doesn't exist\n" );
168168 throw new UploadStashBadPathException( "path doesn't exist" );
@@ -181,17 +181,16 @@
182182 }
183183
184184 // If no key was supplied, make one. a mysql insertid would be totally reasonable here, except
185 - // that some users of this function might expect to supply the key instead of using the generated one.
186 - if ( is_null( $key ) ) {
187 - // some things that when combined will make a suitably unique key.
188 - // see: http://www.jwz.org/doc/mid.html
189 - list ($usec, $sec) = explode( ' ', microtime() );
190 - $usec = substr($usec, 2);
191 - $key = wfBaseConvert( $sec . $usec, 10, 36 ) . '.' .
192 - wfBaseConvert( mt_rand(), 10, 36 ) . '.'.
193 - $this->userId . '.' .
194 - $extension;
195 - }
 185+ // that for historical reasons, the key is this random thing instead. At least it's not guessable.
 186+ //
 187+ // some things that when combined will make a suitably unique key.
 188+ // see: http://www.jwz.org/doc/mid.html
 189+ list ($usec, $sec) = explode( ' ', microtime() );
 190+ $usec = substr($usec, 2);
 191+ $key = wfBaseConvert( $sec . $usec, 10, 36 ) . '.' .
 192+ wfBaseConvert( mt_rand(), 10, 36 ) . '.'.
 193+ $this->userId . '.' .
 194+ $extension;
196195
197196 $this->fileProps[$key] = $fileProps;
198197
@@ -232,31 +231,6 @@
233232 wfDebug( __METHOD__ . " inserting $stashPath under $key\n" );
234233 $dbw = $this->repo->getMasterDb();
235234
236 - // select happens on the master so this can all be in a transaction, which
237 - // avoids a race condition that's likely with multiple people uploading from the same
238 - // set of files
239 - $dbw->begin();
240 - // first, check to see if it's already there.
241 - $row = $dbw->selectRow(
242 - 'uploadstash',
243 - 'us_user, us_timestamp',
244 - array( 'us_key' => $key ),
245 - __METHOD__
246 - );
247 -
248 - // The current user can't have this key if:
249 - // - the key is owned by someone else and
250 - // - the age of the key is less than $wgUploadStashMaxAge
251 - if ( is_object( $row ) ) {
252 - global $wgUploadStashMaxAge;
253 - if ( $row->us_user != $this->userId &&
254 - $row->wfTimestamp( TS_UNIX, $row->us_timestamp ) > time() - $wgUploadStashMaxAge
255 - ) {
256 - $dbw->rollback();
257 - throw new UploadStashWrongOwnerException( "Attempting to upload a duplicate of a file that someone else has stashed" );
258 - }
259 - }
260 -
261235 $this->fileMetadata[$key] = array(
262236 'us_user' => $this->userId,
263237 'us_key' => $key,
@@ -275,13 +249,11 @@
276250 );
277251
278252 // if a row exists but previous checks on it passed, let the current user take over this key.
279 - $dbw->replace(
 253+ $dbw->insert(
280254 'uploadstash',
281 - 'us_key',
282255 $this->fileMetadata[$key],
283256 __METHOD__
284257 );
285 - $dbw->commit();
286258
287259 // store the insertid in the class variable so immediate retrieval (possibly laggy) isn't necesary.
288260 $this->fileMetadata[$key]['us_id'] = $dbw->insertId();
Index: trunk/phase3/includes/upload/UploadFromStash.php
@@ -102,18 +102,18 @@
103103 /**
104104 * There is no need to stash the image twice
105105 */
106 - public function stashFile( $key = null ) {
 106+ public function stashFile() {
107107 if ( $this->mLocalFile ) {
108108 return $this->mLocalFile;
109109 }
110 - return parent::stashFile( $key );
 110+ return parent::stashFile();
111111 }
112112
113113 /**
114114 * Alias for stashFile
115115 */
116 - public function stashSession( $key = null ) {
117 - return $this->stashFile( $key );
 116+ public function stashSession() {
 117+ return $this->stashFile();
118118 }
119119
120120 /**
Index: trunk/phase3/includes/job/UploadFromUrlJob.php
@@ -61,24 +61,24 @@
6262 if ( !$this->params['ignoreWarnings'] ) {
6363 $warnings = $this->upload->checkWarnings();
6464 if ( $warnings ) {
65 - wfSetupSession( $this->params['sessionId'] );
6665
 66+ # Stash the upload
 67+ $key = $this->upload->stashFile();
 68+
6769 if ( $this->params['leaveMessage'] ) {
6870 $this->user->leaveUserMessage(
6971 wfMsg( 'upload-warning-subj' ),
7072 wfMsg( 'upload-warning-msg',
71 - $this->params['sessionKey'],
 73+ $key,
7274 $this->params['url'] )
7375 );
7476 } else {
 77+ wfSetupSession( $this->params['sessionId'] );
7578 $this->storeResultInSession( 'Warning',
7679 'warnings', $warnings );
 80+ session_write_close();
7781 }
7882
79 - # Stash the upload in the session
80 - $this->upload->stashSession( $this->params['sessionKey'] );
81 - session_write_close();
82 -
8383 return true;
8484 }
8585 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r94670Cleaning up little things, updates to code clarity, documentation fixes per C...raindrift17:57, 16 August 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r92200fixed a condition where re-uploading a file that's already stashed causes bre...raindrift21:33, 14 July 2011

Comments

#Comment by Catrope (talk | contribs)   11:34, 16 August 2011
 	 * @param $key String: (optional) the file key used to find the file info again. If not supplied, a key will be autogenerated.
 	 * @return UploadStashFile stashed file
 	 */
-	public function stashFile( $key = null ) {
+	public function stashFile() {

In this and pretty much every other instance, by the looks of it, you've removed the parameter from the function prototype but not from the doc comment.

 		// if a row exists but previous checks on it passed, let the current user take over this key.

This comment doesn't make sense anymore and should be removed.

Why is the session still being used in UploadFromUrlJob?

OK otherwise, marking as such, tagging todo for the comment issues.

Status & tagging log