Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / includes / filerepo / ForeignDBRepo.php
1 <?php
2 /**
3 * A foreign repository with an accessible MediaWiki database.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup FileRepo
22 */
23
24 use Wikimedia\Rdbms\Database;
25 use Wikimedia\Rdbms\IDatabase;
26
27 /**
28 * A foreign repository with an accessible MediaWiki database
29 *
30 * @ingroup FileRepo
31 */
32 class ForeignDBRepo extends LocalRepo {
33 /** @var string */
34 protected $dbType;
35
36 /** @var string */
37 protected $dbServer;
38
39 /** @var string */
40 protected $dbUser;
41
42 /** @var string */
43 protected $dbPassword;
44
45 /** @var string */
46 protected $dbName;
47
48 /** @var string */
49 protected $dbFlags;
50
51 /** @var string */
52 protected $tablePrefix;
53
54 /** @var bool */
55 protected $hasSharedCache;
56
57 /** @var IDatabase */
58 protected $dbConn;
59
60 /** @var callable */
61 protected $fileFactory = [ ForeignDBFile::class, 'newFromTitle' ];
62 /** @var callable */
63 protected $fileFromRowFactory = [ ForeignDBFile::class, 'newFromRow' ];
64
65 /**
66 * @param array|null $info
67 */
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'];
78 }
79
80 /**
81 * @return IDatabase
82 */
83 function getMasterDB() {
84 if ( !isset( $this->dbConn ) ) {
85 $func = $this->getDBFactory();
86 $this->dbConn = $func( DB_MASTER );
87 }
88
89 return $this->dbConn;
90 }
91
92 /**
93 * @return IDatabase
94 */
95 function getReplicaDB() {
96 return $this->getMasterDB();
97 }
98
99 /**
100 * @return Closure
101 */
102 protected function getDBFactory() {
103 $type = $this->dbType;
104 $params = [
105 'host' => $this->dbServer,
106 'user' => $this->dbUser,
107 'password' => $this->dbPassword,
108 'dbname' => $this->dbName,
109 'flags' => $this->dbFlags,
110 'tablePrefix' => $this->tablePrefix
111 ];
112
113 return function ( $index ) use ( $type, $params ) {
114 return Database::factory( $type, $params );
115 };
116 }
117
118 /**
119 * @return bool
120 */
121 function hasSharedCache() {
122 return $this->hasSharedCache;
123 }
124
125 /**
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().
129 * @return bool|mixed
130 */
131 function getSharedCacheKey( /*...*/ ) {
132 if ( $this->hasSharedCache() ) {
133 $args = func_get_args();
134
135 return wfForeignMemcKey( $this->dbName, $this->tablePrefix, ...$args );
136 } else {
137 return false;
138 }
139 }
140
141 protected function assertWritableRepo() {
142 throw new MWException( static::class . ': write operations are not supported.' );
143 }
144
145 /**
146 * Return information about the repository.
147 *
148 * @return array
149 * @since 1.22
150 */
151 function getInfo() {
152 return FileRepo::getInfo();
153 }
154 }