r94536 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94535‎ | r94536 | r94537 >
Date:18:10, 15 August 2011
Author:raindrift
Status:resolved (Comments)
Tags:
Comment:
Fixed incorrect usage of || operator, added test
removed spurious use of empty()
listFiles() was broken, now works
followup to r92009
Modified paths:
  • /trunk/phase3/includes/upload/UploadFromStash.php (modified) (history)
  • /trunk/phase3/includes/upload/UploadStash.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/upload/UploadStashTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/upload/UploadStashTest.php
@@ -45,11 +45,35 @@
4646
4747 $stash->removeFile( $file->getFileKey() );
4848 }
 49+
 50+ public function testValidRequest() {
 51+ $request = new FauxRequest( array( 'wpFileKey' => 'foo') );
 52+ $this->assertFalse( UploadFromStash::isValidRequest($request), 'Check failure on bad wpFileKey' );
 53+
 54+ $request = new FauxRequest( array( 'wpSessionKey' => 'foo') );
 55+ $this->assertFalse( UploadFromStash::isValidRequest($request), 'Check failure on bad wpSessionKey' );
 56+
 57+ $request = new FauxRequest( array( 'wpFileKey' => 'testkey-test.test') );
 58+ $this->assertTrue( UploadFromStash::isValidRequest($request), 'Check good wpFileKey' );
 59+
 60+ $request = new FauxRequest( array( 'wpFileKey' => 'testkey-test.test') );
 61+ $this->assertTrue( UploadFromStash::isValidRequest($request), 'Check good wpSessionKey' );
 62+
 63+ $request = new FauxRequest( array( 'wpFileKey' => 'testkey-test.test', 'wpSessionKey' => 'foo') );
 64+ $this->assertTrue( UploadFromStash::isValidRequest($request), 'Check key precedence' );
 65+ }
 66+
 67+
4968
5069 public function tearDown() {
5170 parent::tearDown();
5271
53 - unlink( $this->bug29408File . "." );
54 -
 72+ if( file_exists( $this->bug29408File . "." ) ) {
 73+ unlink( $this->bug29408File . "." );
 74+ }
 75+
 76+ if( file_exists( $this->bug29408File ) ) {
 77+ unlink( $this->bug29408File );
 78+ }
5579 }
5680 }
Index: trunk/phase3/includes/upload/UploadFromStash.php
@@ -38,7 +38,7 @@
3939
4040 public static function isValidKey( $key ) {
4141 // this is checked in more detail in UploadStash
42 - return preg_match( UploadStash::KEY_FORMAT_REGEX, $key );
 42+ return preg_match( UploadStash::KEY_FORMAT_REGEX, $key ) ? true : false;
4343 }
4444
4545 /**
@@ -47,7 +47,10 @@
4848 * @return Boolean
4949 */
5050 public static function isValidRequest( $request ) {
51 - return self::isValidKey( $request->getText( 'wpFileKey' ) || $request->getText( 'wpSessionKey' ) );
 51+ // this passes wpSessionKey to getText() as a default when wpFileKey isn't set.
 52+ // wpSessionKey has no default which guarantees failure if both are missing
 53+ // (though that should have been caught earlier)
 54+ return self::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) );
5255 }
5356
5457 public function initialize( $key, $name = 'upload_file' ) {
@@ -74,12 +77,12 @@
7578 * @param $request WebRequest
7679 */
7780 public function initializeFromRequest( &$request ) {
78 - $fileKey = $request->getText( 'wpFileKey' ) || $request->getText( 'wpSessionKey' );
 81+ // sends wpSessionKey as a default when wpFileKey is missing
 82+ $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
7983
80 - $desiredDestName = $request->getText( 'wpDestFile' );
81 - if( !$desiredDestName ) {
82 - $desiredDestName = $request->getText( 'wpUploadFile' ) || $request->getText( 'filename' );
83 - }
 84+ // chooses one of wpDestFile, wpUploadFile, filename in that order.
 85+ $desiredDestName = $request->getText( 'wpDestFile', $request->getText( 'wpUploadFile', $request->getText( 'filename' ) ) );
 86+
8487 return $this->initialize( $fileKey, $desiredDestName );
8588 }
8689
@@ -100,7 +103,7 @@
101104 * There is no need to stash the image twice
102105 */
103106 public function stashFile( $key = null ) {
104 - if ( !empty( $this->mLocalFile ) ) {
 107+ if ( !$this->mLocalFile ) {
105108 return $this->mLocalFile;
106109 }
107110 return parent::stashFile( $key );
Index: trunk/phase3/includes/upload/UploadStash.php
@@ -416,7 +416,7 @@
417417 $res = $dbr->select(
418418 'uploadstash',
419419 'us_key',
420 - array( 'us_key' => $key ),
 420+ array( 'us_user' => $this->userId ),
421421 __METHOD__
422422 );
423423

Follow-up revisions

RevisionCommit summaryAuthorDate
r94540checking for existence of mLocalFile in stashFile() was inverted...raindrift18:19, 15 August 2011
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
r92009Refactored UploadStash and related classes to use the database for file metad...raindrift21:11, 12 July 2011

Comments

#Comment by Nikerabbit (talk | contribs)   11:01, 16 August 2011

Hmph, the type of mLocalFile is not stated anywhere. I guess it's not a string but object|null.

#Comment by Catrope (talk | contribs)   11:05, 16 August 2011
+		return preg_match( UploadStash::KEY_FORMAT_REGEX, $key ) ? true : false;

Instead of ? true : false a cast to boolean is preferred: return (bool)preg_match( .... .

Status & tagging log