3 * A foreign repository with an accessible MediaWiki database.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use Wikimedia\Rdbms\Database
;
25 use Wikimedia\Rdbms\IDatabase
;
28 * A foreign repository with an accessible MediaWiki database
32 class ForeignDBRepo
extends LocalRepo
{
43 protected $dbPassword;
52 protected $tablePrefix;
55 protected $hasSharedCache;
61 protected $fileFactory = [ ForeignDBFile
::class, 'newFromTitle' ];
63 protected $fileFromRowFactory = [ ForeignDBFile
::class, 'newFromRow' ];
66 * @param array|null $info
68 function __construct( $info ) {
69 parent
::__construct( $info );
70 $this->dbType
= $info['dbType'];
71 $this->dbServer
= $info['dbServer'];
72 $this->dbUser
= $info['dbUser'];
73 $this->dbPassword
= $info['dbPassword'];
74 $this->dbName
= $info['dbName'];
75 $this->dbFlags
= $info['dbFlags'];
76 $this->tablePrefix
= $info['tablePrefix'];
77 $this->hasSharedCache
= $info['hasSharedCache'];
83 function getMasterDB() {
84 if ( !isset( $this->dbConn
) ) {
85 $func = $this->getDBFactory();
86 $this->dbConn
= $func( DB_MASTER
);
95 function getReplicaDB() {
96 return $this->getMasterDB();
102 protected function getDBFactory() {
103 $type = $this->dbType
;
105 'host' => $this->dbServer
,
106 'user' => $this->dbUser
,
107 'password' => $this->dbPassword
,
108 'dbname' => $this->dbName
,
109 'flags' => $this->dbFlags
,
110 'tablePrefix' => $this->tablePrefix
,
114 return function ( $index ) use ( $type, $params ) {
115 return Database
::factory( $type, $params );
122 function hasSharedCache() {
123 return $this->hasSharedCache
;
127 * Get a key on the primary cache for this repository.
128 * Returns false if the repository's cache is not accessible at this site.
129 * The parameters are the parts of the key, as for wfMemcKey().
132 function getSharedCacheKey( /*...*/ ) {
133 if ( $this->hasSharedCache() ) {
134 $args = func_get_args();
136 return wfForeignMemcKey( $this->dbName
, $this->tablePrefix
, ...$args );
142 protected function assertWritableRepo() {
143 throw new MWException( static::class . ': write operations are not supported.' );
147 * Return information about the repository.
153 return FileRepo
::getInfo();