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\IDatabase
;
27 * A foreign repository with an accessible MediaWiki database
31 class ForeignDBRepo
extends LocalRepo
{
42 protected $dbPassword;
51 protected $tablePrefix;
54 protected $hasSharedCache;
60 protected $fileFactory = [ 'ForeignDBFile', 'newFromTitle' ];
62 protected $fileFromRowFactory = [ 'ForeignDBFile', 'newFromRow' ];
65 * @param array|null $info
67 function __construct( $info ) {
68 parent
::__construct( $info );
69 $this->dbType
= $info['dbType'];
70 $this->dbServer
= $info['dbServer'];
71 $this->dbUser
= $info['dbUser'];
72 $this->dbPassword
= $info['dbPassword'];
73 $this->dbName
= $info['dbName'];
74 $this->dbFlags
= $info['dbFlags'];
75 $this->tablePrefix
= $info['tablePrefix'];
76 $this->hasSharedCache
= $info['hasSharedCache'];
82 function getMasterDB() {
83 if ( !isset( $this->dbConn
) ) {
84 $func = $this->getDBFactory();
85 $this->dbConn
= $func( DB_MASTER
);
94 function getReplicaDB() {
95 return $this->getMasterDB();
101 protected function getDBFactory() {
102 $type = $this->dbType
;
104 'host' => $this->dbServer
,
105 'user' => $this->dbUser
,
106 'password' => $this->dbPassword
,
107 'dbname' => $this->dbName
,
108 'flags' => $this->dbFlags
,
109 'tablePrefix' => $this->tablePrefix
,
113 return function ( $index ) use ( $type, $params ) {
114 return Database
::factory( $type, $params );
121 function hasSharedCache() {
122 return $this->hasSharedCache
;
126 * Get a key on the primary cache for this repository.
127 * Returns false if the repository's cache is not accessible at this site.
128 * The parameters are the parts of the key, as for wfMemcKey().
131 function getSharedCacheKey( /*...*/ ) {
132 if ( $this->hasSharedCache() ) {
133 $args = func_get_args();
134 array_unshift( $args, $this->dbName
, $this->tablePrefix
);
136 return call_user_func_array( 'wfForeignMemcKey', $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();