Merge "Rewrite pref cleanup script"
[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 'foreign' => true,
112 ];
113
114 return function ( $index ) use ( $type, $params ) {
115 return Database::factory( $type, $params );
116 };
117 }
118
119 /**
120 * @return bool
121 */
122 function hasSharedCache() {
123 return $this->hasSharedCache;
124 }
125
126 /**
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().
130 * @return bool|mixed
131 */
132 function getSharedCacheKey( /*...*/ ) {
133 if ( $this->hasSharedCache() ) {
134 $args = func_get_args();
135 array_unshift( $args, $this->dbName, $this->tablePrefix );
136
137 return call_user_func_array( 'wfForeignMemcKey', $args );
138 } else {
139 return false;
140 }
141 }
142
143 protected function assertWritableRepo() {
144 throw new MWException( static::class . ': write operations are not supported.' );
145 }
146
147 /**
148 * Return information about the repository.
149 *
150 * @return array
151 * @since 1.22
152 */
153 function getInfo() {
154 return FileRepo::getInfo();
155 }
156 }