Merge "Don't check namespace in SpecialWantedtemplates"
[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 # Other stuff
55 protected $dbConn;
56 protected $fileFactory = array( 'ForeignDBFile', 'newFromTitle' );
57 protected $fileFromRowFactory = array( 'ForeignDBFile', 'newFromRow' );
58
59 /**
60 * @param array|null $info
61 */
62 function __construct( $info ) {
63 parent::__construct( $info );
64 $this->dbType = $info['dbType'];
65 $this->dbServer = $info['dbServer'];
66 $this->dbUser = $info['dbUser'];
67 $this->dbPassword = $info['dbPassword'];
68 $this->dbName = $info['dbName'];
69 $this->dbFlags = $info['dbFlags'];
70 $this->tablePrefix = $info['tablePrefix'];
71 $this->hasSharedCache = $info['hasSharedCache'];
72 }
73
74 /**
75 * @return DatabaseBase
76 */
77 function getMasterDB() {
78 if ( !isset( $this->dbConn ) ) {
79 $func = $this->getDBFactory();
80 $this->dbConn = $func( DB_MASTER );
81 }
82
83 return $this->dbConn;
84 }
85
86 /**
87 * @return DatabaseBase
88 */
89 function getSlaveDB() {
90 return $this->getMasterDB();
91 }
92
93 /**
94 * @return Closure
95 */
96 protected function getDBFactory() {
97 return function( $index ) {
98 return DatabaseBase::factory( $this->dbType,
99 array(
100 'host' => $this->dbServer,
101 'user' => $this->dbUser,
102 'password' => $this->dbPassword,
103 'dbname' => $this->dbName,
104 'flags' => $this->dbFlags,
105 'tablePrefix' => $this->tablePrefix,
106 'foreign' => true,
107 )
108 );
109 };
110 }
111
112 /**
113 * @return bool
114 */
115 function hasSharedCache() {
116 return $this->hasSharedCache;
117 }
118
119 /**
120 * Get a key on the primary cache for this repository.
121 * Returns false if the repository's cache is not accessible at this site.
122 * The parameters are the parts of the key, as for wfMemcKey().
123 * @return bool|mixed
124 */
125 function getSharedCacheKey( /*...*/ ) {
126 if ( $this->hasSharedCache() ) {
127 $args = func_get_args();
128 array_unshift( $args, $this->dbName, $this->tablePrefix );
129
130 return call_user_func_array( 'wfForeignMemcKey', $args );
131 } else {
132 return false;
133 }
134 }
135
136 protected function assertWritableRepo() {
137 throw new MWException( get_class( $this ) . ': write operations are not supported.' );
138 }
139
140 /**
141 * Return information about the repository.
142 *
143 * @return array
144 * @since 1.22
145 */
146 function getInfo() {
147 return FileRepo::getInfo();
148 }
149 }