Merge "Sort user groups in Special:Listusers"
[lhc/web/wiklou.git] / includes / filerepo / ForeignDBViaLBRepo.php
1 <?php
2 /**
3 * A foreign repository with a MediaWiki database accessible via the configured LBFactory.
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 MediaWiki\MediaWikiServices;
25 use Wikimedia\Rdbms\IDatabase;
26 use Wikimedia\Rdbms\LoadBalancer;
27
28 /**
29 * A foreign repository with a MediaWiki database accessible via the configured LBFactory
30 *
31 * @ingroup FileRepo
32 */
33 class ForeignDBViaLBRepo extends LocalRepo {
34 /** @var string */
35 protected $wiki;
36
37 /** @var string */
38 protected $dbName;
39
40 /** @var string */
41 protected $tablePrefix;
42
43 /** @var array */
44 protected $fileFactory = [ ForeignDBFile::class, 'newFromTitle' ];
45
46 /** @var array */
47 protected $fileFromRowFactory = [ ForeignDBFile::class, 'newFromRow' ];
48
49 /** @var bool */
50 protected $hasSharedCache;
51
52 /**
53 * @param array|null $info
54 */
55 function __construct( $info ) {
56 parent::__construct( $info );
57 $this->wiki = $info['wiki'];
58 list( $this->dbName, $this->tablePrefix ) = wfSplitWikiID( $this->wiki );
59 $this->hasSharedCache = $info['hasSharedCache'];
60 }
61
62 /**
63 * @return IDatabase
64 */
65 function getMasterDB() {
66 return $this->getDBLoadBalancer()->getConnectionRef( DB_MASTER, [], $this->wiki );
67 }
68
69 /**
70 * @return IDatabase
71 */
72 function getReplicaDB() {
73 return $this->getDBLoadBalancer()->getConnectionRef( DB_REPLICA, [], $this->wiki );
74 }
75
76 /**
77 * @return Closure
78 */
79 protected function getDBFactory() {
80 return function ( $index ) {
81 return $this->getDBLoadBalancer()->getConnectionRef( $index, [], $this->wiki );
82 };
83 }
84
85 /**
86 * @return LoadBalancer
87 */
88 protected function getDBLoadBalancer() {
89 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
90 return $lbFactory->getMainLB( $this->wiki );
91 }
92
93 function hasSharedCache() {
94 return $this->hasSharedCache;
95 }
96
97 /**
98 * Get a key on the primary cache for this repository.
99 * Returns false if the repository's cache is not accessible at this site.
100 * The parameters are the parts of the key, as for wfMemcKey().
101 * @return bool|string
102 */
103 function getSharedCacheKey( /*...*/ ) {
104 if ( $this->hasSharedCache() ) {
105 $args = func_get_args();
106 array_unshift( $args, $this->wiki );
107
108 return implode( ':', $args );
109 } else {
110 return false;
111 }
112 }
113
114 protected function assertWritableRepo() {
115 throw new MWException( static::class . ': write operations are not supported.' );
116 }
117
118 public function getInfo() {
119 return FileRepo::getInfo();
120 }
121 }