Merge "- Fix bug 35076 - More tests, test comments"
authorBrion VIBBER <brion@wikimedia.org>
Tue, 10 Apr 2012 19:54:11 +0000 (19:54 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 10 Apr 2012 19:54:11 +0000 (19:54 +0000)
includes/Block.php
includes/filerepo/FileRepo.php
tests/phpunit/MediaWikiTestCase.php
tests/phpunit/includes/BlockTest.php
thumb.php

index 522316f..dff1a5d 100644 (file)
@@ -33,6 +33,9 @@ class Block {
        /// @var User|String
        protected $target;
 
+       // @var Integer Hack for foreign blocking (CentralAuth)
+       protected $forcedTargetID;
+
        /// @var Block::TYPE_ constant.  Can only be USER, IP or RANGE internally
        protected $type;
 
@@ -72,7 +75,7 @@ class Block {
 
                $this->setTarget( $address );
                if ( $this->target instanceof User && $user ) {
-                       $this->target->setId( $user ); // needed for foreign users
+                       $this->forcedTargetID = $user; // needed for foreign users
                }
                if ( $by ) { // local user
                        $this->setBlocker( User::newFromID( $by ) );
@@ -483,9 +486,15 @@ class Block {
                }
                $expiry = $db->encodeExpiry( $this->mExpiry );
 
+               if ( $this->forcedTargetID ) {
+                       $uid = $this->forcedTargetID;
+               } else {
+                       $uid = $this->target instanceof User ? $this->target->getID() : 0;
+               }
+
                $a = array(
                        'ipb_address'          => (string)$this->target,
-                       'ipb_user'             => $this->target instanceof User ? $this->target->getID() : 0,
+                       'ipb_user'             => $uid,
                        'ipb_by'               => $this->getBy(),
                        'ipb_by_text'          => $this->getByName(),
                        'ipb_reason'           => $this->mReason,
index d121454..dcc9610 100644 (file)
@@ -483,13 +483,26 @@ class FileRepo {
         * Get a relative path including trailing slash, e.g. f/fa/
         * If the repo is not hashed, returns an empty string
         *
-        * @param $name string
+        * @param $name string Name of file
         * @return string
         */
        public function getHashPath( $name ) {
                return self::getHashPathForLevel( $name, $this->hashLevels );
        }
 
+       /**
+        * Get a relative path including trailing slash, e.g. f/fa/
+        * If the repo is not hashed, returns an empty string
+        *
+        * @param $suffix string Basename of file from FileRepo::storeTemp()
+        * @return string
+        */
+       public function getTempHashPath( $suffix ) {
+               $parts = explode( '!', $suffix, 2 ); // format is <timestamp>!<name> or just <name>
+               $name = isset( $parts[1] ) ? $parts[1] : $suffix; // hash path is not based on timestamp
+               return self::getHashPathForLevel( $name, $this->hashLevels );
+       }
+
        /**
         * @param $name
         * @param $levels
@@ -1404,6 +1417,36 @@ class FileRepo {
                return call_user_func_array( 'wfMemcKey', $args );
        }
 
+       /**
+        * Get an temporary FileRepo associated with this repo.
+        * Files will be created in the temp zone of this repo and
+        * thumbnails in a /temp subdirectory in thumb zone of this repo.
+        * It will have the same backend as this repo.
+        *
+        * @return TempFileRepo
+        */
+       public function getTempRepo() {
+               return new TempFileRepo( array(
+                       'name'      => "{$this->name}-temp",
+                       'backend'   => $this->backend,
+                       'zones'     => array(
+                               'public' => array(
+                                       'container' => $this->zones['temp']['container'],
+                                       'directory' => $this->zones['temp']['directory']
+                               ),
+                               'thumb'  => array(
+                                       'container' => $this->zones['thumb']['container'],
+                                       'directory' => ( $this->zones['thumb']['directory'] == '' )
+                                               ? 'temp'
+                                               : $this->zones['thumb']['directory'] . '/temp'
+                               )
+                       ),
+                       'url'        => $this->getZoneUrl( 'temp' ),
+                       'thumbUrl'   => $this->getZoneUrl( 'thumb' ) . '/temp',
+                       'hashLevels' => $this->hashLevels // performance
+               ) );
+       }
+
        /**
         * Get an UploadStash associated with this repo.
         *
@@ -1422,3 +1465,12 @@ class FileRepo {
         */
        protected function assertWritableRepo() {}
 }
+
+/**
+ * FileRepo for temporary files created via FileRepo::getTempRepo()
+ */
+class TempFileRepo extends FileRepo {
+       public function getTempRepo() {
+               throw new MWException( "Cannot get a temp repo from a temp repo." );
+       }
+}
index 6ec8bdc..f626459 100644 (file)
@@ -16,6 +16,15 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
        private static $dbSetup = false;
 
+       /**
+        * Holds the paths of temporary files/directories created through getNewTempFile,
+        * and getNewTempDirectory
+        *
+        * @var array
+        */
+       private $tmpfiles = array();
+
+
        /**
         * Table name prefixes. Oracle likes it shorter.
         */
@@ -71,6 +80,53 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                }
        }
 
+       /**
+        * obtains a new temporary file name
+        *
+        * The obtained filename is enlisted to be removed upon tearDown
+        *
+        * @returns string: absolute name of the temporary file
+        */
+       protected function getNewTempFile() {
+               $fname = tempnam( wfTempDir(), 'MW_PHPUnit_' . get_class( $this ) . '_' );
+               $this->tmpfiles[] = $fname;
+               return $fname;
+       }
+
+       /**
+        * obtains a new temporary directory
+        *
+        * The obtained directory is enlisted to be removed (recursively with all its contained
+        * files) upon tearDown.
+        *
+        * @returns string: absolute name of the temporary directory
+        */
+       protected function getNewTempDirectory() {
+               // Starting of with a temporary /file/.
+               $fname = $this->getNewTempFile();
+
+               // Converting the temporary /file/ to a /directory/
+               //
+               // The following is not atomic, but at least we now have a single place,
+               // where temporary directory creation is bundled and can be improved
+               unlink( $fname );
+               $this->assertTrue( wfMkdirParents( $fname ) );
+               return $fname;
+       }
+
+       protected function tearDown() {
+               // Cleaning up temoporary files
+               foreach ( $this->tmpfiles as $fname ) {
+                       if ( is_file( $fname ) || ( is_link( $fname ) ) ) {
+                               unlink( $fname );
+                       } elseif ( is_dir( $fname ) ) {
+                               wfRecursiveRemoveDir( $fname );
+                       }
+               }
+
+               parent::tearDown();
+       }
+
        function dbPrefix() {
                return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
        }
index 749f40b..f197da8 100644 (file)
@@ -67,7 +67,7 @@ class BlockTest extends MediaWikiLangTestCase {
                // $this->dumpBlocks();
 
                $this->assertTrue( $this->block->equals( Block::newFromTarget('UTBlockee') ), "newFromTarget() returns the same block as the one that was made");
-               
+
                $this->assertTrue( $this->block->equals( Block::newFromID( $this->blockId ) ), "newFromID() returns the same block as the one that was made");
 
        }
@@ -122,4 +122,48 @@ class BlockTest extends MediaWikiLangTestCase {
                        array( false )
                );
        }
+
+       function testCrappyCrossWikiBlocks() {
+               // Delete the last round's block if it's still there
+               $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
+               if ( $oldBlock ) {
+                       // An old block will prevent our new one from saving.
+                       $oldBlock->delete();
+               }
+
+               // Foreign perspective (blockee not on current wiki)...
+               $block = new Block(
+                       /* $address */ 'UserOnForeignWiki',
+                       /* $user */ 14146,
+                       /* $by */ 0,
+                       /* $reason */ 'crosswiki block...',
+                       /* $timestamp */ wfTimestampNow(),
+                       /* $auto */ false,
+                       /* $expiry */ $this->db->getInfinity(),
+                       /* anonOnly */ false,
+                       /* $createAccount */ true,
+                       /* $enableAutoblock */ true,
+                       /* $hideName (ipb_deleted) */ true,
+                       /* $blockEmail */ true,
+                       /* $allowUsertalk */ false,
+                       /* $byName */ 'MetaWikiUser'
+               );
+
+               $res = $block->insert( $this->db );
+               $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
+
+               // Local perspective (blockee on current wiki)...
+               $user = User::newFromName( 'UserOnForeignWiki' );
+               $user->addToDatabase();
+               // Set user ID to match the test value
+               $this->db->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
+               $user = null; // clear
+
+               $block = Block::newFromID( $res['id'] );
+               $this->assertEquals( 'UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name' );
+               $this->assertEquals( '14146',  $block->getTarget()->getId(), 'Correct blockee id' );
+               $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
+               $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
+               $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
+       }
 }
index 6afc7e5..6855581 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -97,7 +97,11 @@ function wfStreamThumb( array $params ) {
 
        // Is this a thumb of an archived file?
        $isOld = ( isset( $params['archived'] ) && $params['archived'] );
-       unset( $params['archived'] );
+       unset( $params['archived'] ); // handlers don't care
+
+       // Is this a thumb of a temp file?
+       $isTemp = ( isset( $params['temp'] ) && $params['temp'] );
+       unset( $params['temp'] ); // handlers don't care
 
        // Some basic input validation
        $fileName = strtr( $fileName, '\\/', '__' );
@@ -118,6 +122,20 @@ function wfStreamThumb( array $params ) {
                        return;
                }
                $img = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $fileName );
+       } elseif ( $isTemp ) {
+               $repo = RepoGroup::singleton()->getLocalRepo()->getTempRepo();
+               // Format is <timestamp>!<name> or just <name>
+               $bits = explode( '!', $fileName, 2 );
+               // Get the name without the timestamp so hash paths are correctly computed
+               $title = Title::makeTitleSafe( NS_FILE, isset( $bits[1] ) ? $bits[1] : $fileName );
+               if ( !$title ) {
+                       wfThumbError( 404, wfMsg( 'badtitletext' ) );
+                       wfProfileOut( __METHOD__ );
+                       return;
+               }
+               $img = new UnregisteredLocalFile( $title, $repo,
+                       $repo->getZonePath( 'public' ) . '/' . $repo->getTempHashPath( $fileName ) . $fileName
+               );
        } else {
                $img = wfLocalFile( $fileName );
        }
@@ -178,7 +196,7 @@ function wfStreamThumb( array $params ) {
                        // Check that the zone relative path matches up so squid caches won't pick
                        // up thumbs that would not be purged on source file deletion (bug 34231).
                        if ( isset( $params['rel404'] ) // thumbnail was handled via 404
-                               && urldecode( $params['rel404'] ) !== $img->getThumbRel( $thumbName ) ) 
+                               && urldecode( $params['rel404'] ) !== $img->getThumbRel( $thumbName ) )
                        {
                                wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
                                wfProfileOut( __METHOD__ );