Introduce RevisionStoreFactory & Tests
[lhc/web/wiklou.git] / includes / Storage / RevisionStoreFactory.php
1 <?php
2
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * Attribution notice: when this file was created, much of its content was taken
20 * from the Revision.php file as present in release 1.30. Refer to the history
21 * of that file for original authorship.
22 *
23 * @file
24 */
25
26 namespace MediaWiki\Storage;
27
28 use ActorMigration;
29 use CommentStore;
30 use MediaWiki\Logger\Spi as LoggerSpi;
31 use WANObjectCache;
32 use Wikimedia\Assert\Assert;
33 use Wikimedia\Rdbms\ILBFactory;
34
35 /**
36 * Factory service for RevisionStore instances. This allows RevisionStores to be created for
37 * cross-wiki access.
38 *
39 * @warning Beware compatibility issues with schema migration in the context of cross-wiki access!
40 * This class assumes that all wikis are at compatible migration stages for all relevant schemas.
41 * Relevant schemas are: revision storage (MCR), the revision comment table, and the actor table.
42 * Migration stages are compatible as long as a) there are no wikis in the cluster that only write
43 * the old schema or b) there are no wikis that read only the new schema.
44 *
45 * @since 1.32
46 */
47 class RevisionStoreFactory {
48
49 /** @var BlobStoreFactory */
50 private $blobStoreFactory;
51 /** @var ILBFactory */
52 private $dbLoadBalancerFactory;
53 /** @var WANObjectCache */
54 private $cache;
55 /** @var LoggerSpi */
56 private $loggerProvider;
57
58 /** @var CommentStore */
59 private $commentStore;
60 /** @var ActorMigration */
61 private $actorMigration;
62 /** @var int One of the MIGRATION_* constants */
63 private $mcrMigrationStage;
64 /**
65 * @var bool
66 * @see $wgContentHandlerUseDB
67 */
68 private $contentHandlerUseDB;
69
70 /**
71 * @param ILBFactory $dbLoadBalancerFactory
72 * @param BlobStoreFactory $blobStoreFactory
73 * @param WANObjectCache $cache
74 * @param CommentStore $commentStore
75 * @param ActorMigration $actorMigration
76 * @param int $migrationStage
77 * @param LoggerSpi $loggerProvider
78 * @param bool $contentHandlerUseDB see {@link $wgContentHandlerUseDB}
79 */
80 public function __construct(
81 ILBFactory $dbLoadBalancerFactory,
82 BlobStoreFactory $blobStoreFactory,
83 WANObjectCache $cache,
84 CommentStore $commentStore,
85 ActorMigration $actorMigration,
86 $migrationStage,
87 LoggerSpi $loggerProvider,
88 $contentHandlerUseDB
89 ) {
90 Assert::parameterType( 'integer', $migrationStage, '$migrationStage' );
91 $this->dbLoadBalancerFactory = $dbLoadBalancerFactory;
92 $this->blobStoreFactory = $blobStoreFactory;
93 $this->cache = $cache;
94 $this->commentStore = $commentStore;
95 $this->actorMigration = $actorMigration;
96 $this->mcrMigrationStage = $migrationStage;
97 $this->loggerProvider = $loggerProvider;
98 $this->contentHandlerUseDB = $contentHandlerUseDB;
99 }
100 /**
101
102 /**
103 * @since 1.32
104 *
105 * @param bool|string $wikiId false for the current domain / wikid
106 *
107 * @return RevisionStore for the given wikiId with all necessary services and a logger
108 */
109 public function getRevisionStore( $wikiId = false ) {
110 Assert::parameterType( 'string|boolean', $wikiId, '$wikiId' );
111
112 $store = new RevisionStore(
113 $this->dbLoadBalancerFactory->getMainLB( $wikiId ),
114 $this->blobStoreFactory->newSqlBlobStore( $wikiId ),
115 $this->cache, // Pass local cache instance; Leave cache sharing to RevisionStore.
116 $this->commentStore,
117 $this->getContentModelStore( $wikiId ),
118 $this->getSlotRoleStore( $wikiId ),
119 $this->mcrMigrationStage,
120 $this->actorMigration,
121 $wikiId
122 );
123
124 $store->setLogger( $this->loggerProvider->getLogger( 'RevisionStore' ) );
125 $store->setContentHandlerUseDB( $this->contentHandlerUseDB );
126
127 return $store;
128 }
129
130 /**
131 * @param string $wikiId
132 * @return NameTableStore
133 */
134 private function getContentModelStore( $wikiId ) {
135 // XXX: a dedicated ContentModelStore subclass would avoid hard-coding
136 // knowledge about the schema here.
137 return new NameTableStore(
138 $this->dbLoadBalancerFactory->getMainLB( $wikiId ),
139 $this->cache, // Pass local cache instance; Leave cache sharing to NameTableStore.
140 $this->loggerProvider->getLogger( 'NameTableSqlStore' ),
141 'content_models',
142 'model_id',
143 'model_name',
144 null,
145 $wikiId
146 );
147 }
148
149 /**
150 * @param string $wikiId
151 * @return NameTableStore
152 */
153 private function getSlotRoleStore( $wikiId ) {
154 // XXX: a dedicated ContentModelStore subclass would avoid hard-coding
155 // knowledge about the schema here.
156 return new NameTableStore(
157 $this->dbLoadBalancerFactory->getMainLB( $wikiId ),
158 $this->cache, // Pass local cache instance; Leave cache sharing to NameTableStore.
159 $this->loggerProvider->getLogger( 'NameTableSqlStore' ),
160 'slot_roles',
161 'role_id',
162 'role_name',
163 'strtolower',
164 $wikiId
165 );
166 }
167
168 }