Introduce RevisionStoreFactory & Tests
authoraddshore <addshorewiki@gmail.com>
Wed, 27 Jun 2018 12:16:35 +0000 (13:16 +0100)
committerAddshore <addshorewiki@gmail.com>
Fri, 29 Jun 2018 12:55:20 +0000 (12:55 +0000)
Bug: T194729
Change-Id: I0a8a441b803816281113e52a2a57cc07af8a1119

includes/MediaWikiServices.php
includes/ServiceWiring.php
includes/Storage/RevisionStoreFactory.php [new file with mode: 0644]
tests/phpunit/includes/MediaWikiServicesTest.php
tests/phpunit/includes/Storage/RevisionStoreFactoryTest.php [new file with mode: 0644]

index ac15574..8bd25d7 100644 (file)
@@ -21,6 +21,7 @@ use MediaWiki\Storage\NameTableStore;
 use MediaWiki\Storage\RevisionFactory;
 use MediaWiki\Storage\RevisionLookup;
 use MediaWiki\Storage\RevisionStore;
+use MediaWiki\Storage\RevisionStoreFactory;
 use Wikimedia\Rdbms\LBFactory;
 use LinkCache;
 use Wikimedia\Rdbms\LoadBalancer;
@@ -757,6 +758,14 @@ class MediaWikiServices extends ServiceContainer {
                return $this->getService( 'RevisionStore' );
        }
 
+       /**
+        * @since 1.32
+        * @return RevisionStoreFactory
+        */
+       public function getRevisionStoreFactory() {
+               return $this->getService( 'RevisionStoreFactory' );
+       }
+
        /**
         * @since 1.31
         * @return RevisionLookup
index 425b789..ba576d5 100644 (file)
@@ -46,7 +46,7 @@ use MediaWiki\Preferences\DefaultPreferencesFactory;
 use MediaWiki\Shell\CommandFactory;
 use MediaWiki\Storage\BlobStoreFactory;
 use MediaWiki\Storage\NameTableStore;
-use MediaWiki\Storage\RevisionStore;
+use MediaWiki\Storage\RevisionStoreFactory;
 use MediaWiki\Storage\SqlBlobStore;
 use Wikimedia\ObjectFactory;
 
@@ -463,10 +463,15 @@ return [
        },
 
        'RevisionStore' => function ( MediaWikiServices $services ) {
+               return $services->getRevisionStoreFactory()->getRevisionStore();
+       },
+
+       'RevisionStoreFactory' => function ( MediaWikiServices $services ) {
                /** @var SqlBlobStore $blobStore */
                $blobStore = $services->getService( '_SqlBlobStore' );
+               $config = $services->getMainConfig();
 
-               $store = new RevisionStore(
+               $store = new RevisionStoreFactory(
                        $services->getDBLoadBalancer(),
                        $blobStore,
                        $services->getMainWANObjectCache(),
@@ -474,14 +479,11 @@ return [
                        $services->getContentModelStore(),
                        $services->getSlotRoleStore(),
                        $services->getMainConfig()->get( 'MultiContentRevisionSchemaMigrationStage' ),
-                       $services->getActorMigration()
+                       $services->getActorMigration(),
+                       LoggerFactory::getInstance( 'RevisionStore' ),
+                       $config->get( 'ContentHandlerUseDB' )
                );
 
-               $store->setLogger( LoggerFactory::getInstance( 'RevisionStore' ) );
-
-               $config = $services->getMainConfig();
-               $store->setContentHandlerUseDB( $config->get( 'ContentHandlerUseDB' ) );
-
                return $store;
        },
 
diff --git a/includes/Storage/RevisionStoreFactory.php b/includes/Storage/RevisionStoreFactory.php
new file mode 100644 (file)
index 0000000..6f8bd99
--- /dev/null
@@ -0,0 +1,141 @@
+<?php
+
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * Attribution notice: when this file was created, much of its content was taken
+ * from the Revision.php file as present in release 1.30. Refer to the history
+ * of that file for original authorship.
+ *
+ * @file
+ */
+
+namespace MediaWiki\Storage;
+
+use ActorMigration;
+use CommentStore;
+use Psr\Log\LoggerInterface;
+use WANObjectCache;
+use Wikimedia\Assert\Assert;
+use Wikimedia\Rdbms\LoadBalancer;
+
+/**
+ * @since 1.32
+ */
+class RevisionStoreFactory {
+
+       /** @var SqlBlobStore */
+       private $blobStore;
+
+       /** @var LoadBalancer */
+       private $loadBalancer;
+
+       /** @var WANObjectCache */
+       private $cache;
+
+       /** @var CommentStore */
+       private $commentStore;
+
+       /** @var ActorMigration */
+       private $actorMigration;
+
+       /** @var NameTableStore */
+       private $contentModelStore;
+
+       /** @var NameTableStore */
+       private $slotRoleStore;
+
+       /** @var int One of the MIGRATION_* constants */
+       private $mcrMigrationStage;
+
+       /**
+        * @var bool
+        * @see $wgContentHandlerUseDB
+        */
+       private $contentHandlerUseDB;
+
+       /** @var LoggerInterface */
+       private $logger;
+
+       /**
+        * @todo $blobStore should be allowed to be any BlobStore!
+        *
+        * @param LoadBalancer $loadBalancer
+        * @param SqlBlobStore $blobStore
+        * @param WANObjectCache $cache
+        * @param CommentStore $commentStore
+        * @param NameTableStore $contentModelStore
+        * @param NameTableStore $slotRoleStore
+        * @param int $migrationStage
+        * @param ActorMigration $actorMigration
+        * @param LoggerInterface $logger
+        * @param bool $contentHandlerUseDB see {@link $wgContentHandlerUseDB}
+        */
+       public function __construct(
+               LoadBalancer $loadBalancer,
+               SqlBlobStore $blobStore,
+               WANObjectCache $cache,
+               CommentStore $commentStore,
+               NameTableStore $contentModelStore,
+               NameTableStore $slotRoleStore,
+               $migrationStage,
+               ActorMigration $actorMigration,
+               LoggerInterface $logger,
+               $contentHandlerUseDB
+       ) {
+               Assert::parameterType( 'integer', $migrationStage, '$migrationStage' );
+
+               $this->loadBalancer = $loadBalancer;
+               $this->blobStore = $blobStore;
+               $this->cache = $cache;
+               $this->commentStore = $commentStore;
+               $this->contentModelStore = $contentModelStore;
+               $this->slotRoleStore = $slotRoleStore;
+               $this->mcrMigrationStage = $migrationStage;
+               $this->actorMigration = $actorMigration;
+               $this->logger = $logger;
+               $this->contentHandlerUseDB = $contentHandlerUseDB;
+       }
+
+       /**
+        * @since 1.32
+        *
+        * @param bool|string $wikiId false for the current domain / wikid
+        *
+        * @return RevisionStore for the given wikiId with all necessary services and a logger
+        */
+       public function getRevisionStore( $wikiId = false ) {
+               Assert::parameterType( 'string|boolean', $wikiId, '$wikiId' );
+
+               $store = new RevisionStore(
+                       $this->loadBalancer,
+                       $this->blobStore,
+                       $this->cache,
+                       $this->commentStore,
+                       $this->contentModelStore,
+                       $this->slotRoleStore,
+                       $this->mcrMigrationStage,
+                       $this->actorMigration,
+                       $wikiId
+               );
+
+               $store->setLogger( $this->logger );
+               $store->setContentHandlerUseDB( $this->contentHandlerUseDB );
+
+               return $store;
+       }
+
+}
index 93c7ed3..31e6fef 100644 (file)
@@ -14,6 +14,7 @@ use MediaWiki\Storage\BlobStoreFactory;
 use MediaWiki\Storage\NameTableStore;
 use MediaWiki\Storage\RevisionLookup;
 use MediaWiki\Storage\RevisionStore;
+use MediaWiki\Storage\RevisionStoreFactory;
 use MediaWiki\Storage\SqlBlobStore;
 
 /**
@@ -346,6 +347,7 @@ class MediaWikiServicesTest extends MediaWikiTestCase {
                        'BlobStore' => [ 'BlobStore', BlobStore::class ],
                        '_SqlBlobStore' => [ '_SqlBlobStore', SqlBlobStore::class ],
                        'RevisionStore' => [ 'RevisionStore', RevisionStore::class ],
+                       'RevisionStoreFactory' => [ 'RevisionStoreFactory', RevisionStoreFactory::class ],
                        'RevisionLookup' => [ 'RevisionLookup', RevisionLookup::class ],
                        'HttpRequestFactory' => [ 'HttpRequestFactory', HttpRequestFactory::class ],
                        'CommentStore' => [ 'CommentStore', CommentStore::class ],
diff --git a/tests/phpunit/includes/Storage/RevisionStoreFactoryTest.php b/tests/phpunit/includes/Storage/RevisionStoreFactoryTest.php
new file mode 100644 (file)
index 0000000..a5c18ac
--- /dev/null
@@ -0,0 +1,131 @@
+<?php
+
+namespace MediaWiki\Tests\Storage;
+
+use ActorMigration;
+use CommentStore;
+use MediaWiki\Logger\LoggerFactory;
+use MediaWiki\Storage\NameTableStore;
+use MediaWiki\Storage\RevisionStore;
+use MediaWiki\Storage\RevisionStoreFactory;
+use MediaWiki\Storage\SqlBlobStore;
+use MediaWikiTestCase;
+use WANObjectCache;
+use Wikimedia\Rdbms\LoadBalancer;
+use Wikimedia\TestingAccessWrapper;
+
+class RevisionStoreFactoryTest extends MediaWikiTestCase {
+
+       public function testValidConstruction_doesntCauseErrors() {
+               new RevisionStoreFactory(
+                       $this->getMockLoadBalancer(),
+                       $this->getMockSqlBlobStore(),
+                       $this->getHashWANObjectCache(),
+                       $this->getMockCommentStore(),
+                       $this->getMockNameTableStore(),
+                       $this->getMockNameTableStore(),
+                       MIGRATION_OLD,
+                       ActorMigration::newMigration(),
+                       LoggerFactory::getInstance( 'someInstance' ),
+                       true
+               );
+               $this->assertTrue( true );
+       }
+
+       public function provideWikiIds() {
+               yield [ true ];
+               yield [ false ];
+               yield [ 'somewiki' ];
+               yield [ 'somewiki', MIGRATION_OLD , false ];
+               yield [ 'somewiki', MIGRATION_NEW , true ];
+       }
+
+       /**
+        * @dataProvider provideWikiIds
+        */
+       public function testGetRevisionStore(
+               $wikiId,
+               $mcrMigrationStage = MIGRATION_OLD,
+               $contentHandlerUseDb = true
+       ) {
+               $lb = $this->getMockLoadBalancer();
+               $blobStore = $this->getMockSqlBlobStore();
+               $cache = $this->getHashWANObjectCache();
+               $commentStore = $this->getMockCommentStore();
+               $contentModelStore = $this->getMockNameTableStore();
+               $slotRoleStore = $this->getMockNameTableStore();
+               $actorMigration = ActorMigration::newMigration();
+               $logger = LoggerFactory::getInstance( 'someInstance' );
+
+               $factory = new RevisionStoreFactory(
+                       $lb,
+                       $blobStore,
+                       $cache,
+                       $commentStore,
+                       $contentModelStore,
+                       $slotRoleStore,
+                       $mcrMigrationStage,
+                       $actorMigration,
+                       $logger,
+                       $contentHandlerUseDb
+               );
+
+               $store = $factory->getRevisionStore( $wikiId );
+               $wrapper = TestingAccessWrapper::newFromObject( $store );
+
+               // ensure the correct object type is returned
+               $this->assertInstanceOf( RevisionStore::class, $store );
+
+               // ensure the RevisionStore is for the given wikiId
+               $this->assertSame( $wikiId, $wrapper->wikiId );
+
+               // ensure all other required services are correctly set
+               $this->assertSame( $lb, $wrapper->loadBalancer );
+               $this->assertSame( $blobStore, $wrapper->blobStore );
+               $this->assertSame( $cache, $wrapper->cache );
+               $this->assertSame( $commentStore, $wrapper->commentStore );
+               $this->assertSame( $contentModelStore, $wrapper->contentModelStore );
+               $this->assertSame( $slotRoleStore, $wrapper->slotRoleStore );
+               $this->assertSame( $mcrMigrationStage, $wrapper->mcrMigrationStage );
+               $this->assertSame( $actorMigration, $wrapper->actorMigration );
+               $this->assertSame( $logger, $wrapper->logger );
+               $this->assertSame( $contentHandlerUseDb, $store->getContentHandlerUseDB() );
+       }
+
+       /**
+        * @return \PHPUnit_Framework_MockObject_MockObject|NameTableStore
+        */
+       private function getMockNameTableStore() {
+               return $this->getMockBuilder( NameTableStore::class )
+                       ->disableOriginalConstructor()->getMock();
+       }
+
+       /**
+        * @return \PHPUnit_Framework_MockObject_MockObject|LoadBalancer
+        */
+       private function getMockLoadBalancer() {
+               return $this->getMockBuilder( LoadBalancer::class )
+                       ->disableOriginalConstructor()->getMock();
+       }
+
+       /**
+        * @return \PHPUnit_Framework_MockObject_MockObject|SqlBlobStore
+        */
+       private function getMockSqlBlobStore() {
+               return $this->getMockBuilder( SqlBlobStore::class )
+                       ->disableOriginalConstructor()->getMock();
+       }
+
+       /**
+        * @return \PHPUnit_Framework_MockObject_MockObject|CommentStore
+        */
+       private function getMockCommentStore() {
+               return $this->getMockBuilder( CommentStore::class )
+                       ->disableOriginalConstructor()->getMock();
+       }
+
+       private function getHashWANObjectCache() {
+               return new WANObjectCache( [ 'cache' => new \HashBagOStuff() ] );
+       }
+
+}