Merge "Handle missing namespace prefix in XML dumps more gracefully"
[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 /**
25 * A foreign repository with an accessible MediaWiki database
26 *
27 * @ingroup FileRepo
28 */
29 class ForeignDBRepo extends LocalRepo {
30 /** @var string */
31 protected $dbType;
32
33 /** @var string */
34 protected $dbServer;
35
36 /** @var string */
37 protected $dbUser;
38
39 /** @var string */
40 protected $dbPassword;
41
42 /** @var string */
43 protected $dbName;
44
45 /** @var string */
46 protected $dbFlags;
47
48 /** @var string */
49 protected $tablePrefix;
50
51 /** @var bool */
52 protected $hasSharedCache;
53
54 /** @var IDatabase */
55 protected $dbConn;
56
57 /** @var callable */
58 protected $fileFactory = [ 'ForeignDBFile', 'newFromTitle' ];
59 /** @var callable */
60 protected $fileFromRowFactory = [ 'ForeignDBFile', 'newFromRow' ];
61
62 /**
63 * @param array|null $info
64 */
65 function __construct( $info ) {
66 parent::__construct( $info );
67 $this->dbType = $info['dbType'];
68 $this->dbServer = $info['dbServer'];
69 $this->dbUser = $info['dbUser'];
70 $this->dbPassword = $info['dbPassword'];
71 $this->dbName = $info['dbName'];
72 $this->dbFlags = $info['dbFlags'];
73 $this->tablePrefix = $info['tablePrefix'];
74 $this->hasSharedCache = $info['hasSharedCache'];
75 }
76
77 /**
78 * @return IDatabase
79 */
80 function getMasterDB() {
81 if ( !isset( $this->dbConn ) ) {
82 $func = $this->getDBFactory();
83 $this->dbConn = $func( DB_MASTER );
84 }
85
86 return $this->dbConn;
87 }
88
89 /**
90 * @return IDatabase
91 */
92 function getReplicaDB() {
93 return $this->getMasterDB();
94 }
95
96 /**
97 * @return Closure
98 */
99 protected function getDBFactory() {
100 $type = $this->dbType;
101 $params = [
102 'host' => $this->dbServer,
103 'user' => $this->dbUser,
104 'password' => $this->dbPassword,
105 'dbname' => $this->dbName,
106 'flags' => $this->dbFlags,
107 'tablePrefix' => $this->tablePrefix,
108 'foreign' => true,
109 ];
110
111 return function ( $index ) use ( $type, $params ) {
112 return Database::factory( $type, $params );
113 };
114 }
115
116 /**
117 * @return bool
118 */
119 function hasSharedCache() {
120 return $this->hasSharedCache;
121 }
122
123 /**
124 * Get a key on the primary cache for this repository.
125 * Returns false if the repository's cache is not accessible at this site.
126 * The parameters are the parts of the key, as for wfMemcKey().
127 * @return bool|mixed
128 */
129 function getSharedCacheKey( /*...*/ ) {
130 if ( $this->hasSharedCache() ) {
131 $args = func_get_args();
132 array_unshift( $args, $this->dbName, $this->tablePrefix );
133
134 return call_user_func_array( 'wfForeignMemcKey', $args );
135 } else {
136 return false;
137 }
138 }
139
140 protected function assertWritableRepo() {
141 throw new MWException( static::class . ': write operations are not supported.' );
142 }
143
144 /**
145 * Return information about the repository.
146 *
147 * @return array
148 * @since 1.22
149 */
150 function getInfo() {
151 return FileRepo::getInfo();
152 }
153 }