Merge "RC Filters: Hooks for highlight guided tour"
[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\IDatabase;
25
26 /**
27 * A foreign repository with an accessible MediaWiki database
28 *
29 * @ingroup FileRepo
30 */
31 class ForeignDBRepo extends LocalRepo {
32 /** @var string */
33 protected $dbType;
34
35 /** @var string */
36 protected $dbServer;
37
38 /** @var string */
39 protected $dbUser;
40
41 /** @var string */
42 protected $dbPassword;
43
44 /** @var string */
45 protected $dbName;
46
47 /** @var string */
48 protected $dbFlags;
49
50 /** @var string */
51 protected $tablePrefix;
52
53 /** @var bool */
54 protected $hasSharedCache;
55
56 /** @var IDatabase */
57 protected $dbConn;
58
59 /** @var callable */
60 protected $fileFactory = [ 'ForeignDBFile', 'newFromTitle' ];
61 /** @var callable */
62 protected $fileFromRowFactory = [ 'ForeignDBFile', 'newFromRow' ];
63
64 /**
65 * @param array|null $info
66 */
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'];
77 }
78
79 /**
80 * @return IDatabase
81 */
82 function getMasterDB() {
83 if ( !isset( $this->dbConn ) ) {
84 $func = $this->getDBFactory();
85 $this->dbConn = $func( DB_MASTER );
86 }
87
88 return $this->dbConn;
89 }
90
91 /**
92 * @return IDatabase
93 */
94 function getReplicaDB() {
95 return $this->getMasterDB();
96 }
97
98 /**
99 * @return Closure
100 */
101 protected function getDBFactory() {
102 $type = $this->dbType;
103 $params = [
104 'host' => $this->dbServer,
105 'user' => $this->dbUser,
106 'password' => $this->dbPassword,
107 'dbname' => $this->dbName,
108 'flags' => $this->dbFlags,
109 'tablePrefix' => $this->tablePrefix,
110 'foreign' => true,
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 array_unshift( $args, $this->dbName, $this->tablePrefix );
135
136 return call_user_func_array( 'wfForeignMemcKey', $args );
137 } else {
138 return false;
139 }
140 }
141
142 protected function assertWritableRepo() {
143 throw new MWException( static::class . ': write operations are not supported.' );
144 }
145
146 /**
147 * Return information about the repository.
148 *
149 * @return array
150 * @since 1.22
151 */
152 function getInfo() {
153 return FileRepo::getInfo();
154 }
155 }