Merge "jquery.spinner: Improve documentation formatting"
[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 # Settings
31 protected $dbType;
32 protected $dbServer;
33 protected $dbUser;
34 protected $dbPassword;
35 protected $dbName;
36 protected $dbFlags;
37 protected $tablePrefix;
38 protected $hasSharedCache;
39
40 # Other stuff
41 protected $dbConn;
42 protected $fileFactory = array( 'ForeignDBFile', 'newFromTitle' );
43 protected $fileFromRowFactory = array( 'ForeignDBFile', 'newFromRow' );
44
45 /**
46 * @param $info array|null
47 */
48 function __construct( $info ) {
49 parent::__construct( $info );
50 $this->dbType = $info['dbType'];
51 $this->dbServer = $info['dbServer'];
52 $this->dbUser = $info['dbUser'];
53 $this->dbPassword = $info['dbPassword'];
54 $this->dbName = $info['dbName'];
55 $this->dbFlags = $info['dbFlags'];
56 $this->tablePrefix = $info['tablePrefix'];
57 $this->hasSharedCache = $info['hasSharedCache'];
58 }
59
60 /**
61 * @return DatabaseBase
62 */
63 function getMasterDB() {
64 if ( !isset( $this->dbConn ) ) {
65 $this->dbConn = DatabaseBase::factory( $this->dbType,
66 array(
67 'host' => $this->dbServer,
68 'user' => $this->dbUser,
69 'password' => $this->dbPassword,
70 'dbname' => $this->dbName,
71 'flags' => $this->dbFlags,
72 'tablePrefix' => $this->tablePrefix,
73 'foreign' => true,
74 )
75 );
76 }
77
78 return $this->dbConn;
79 }
80
81 /**
82 * @return DatabaseBase
83 */
84 function getSlaveDB() {
85 return $this->getMasterDB();
86 }
87
88 /**
89 * @return bool
90 */
91 function hasSharedCache() {
92 return $this->hasSharedCache;
93 }
94
95 /**
96 * Get a key on the primary cache for this repository.
97 * Returns false if the repository's cache is not accessible at this site.
98 * The parameters are the parts of the key, as for wfMemcKey().
99 * @return bool|mixed
100 */
101 function getSharedCacheKey( /*...*/ ) {
102 if ( $this->hasSharedCache() ) {
103 $args = func_get_args();
104 array_unshift( $args, $this->dbName, $this->tablePrefix );
105
106 return call_user_func_array( 'wfForeignMemcKey', $args );
107 } else {
108 return false;
109 }
110 }
111
112 protected function assertWritableRepo() {
113 throw new MWException( get_class( $this ) . ': write operations are not supported.' );
114 }
115 }