Split ApiImport.php to have one class in one file
[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\ILoadBalancer;
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 array */
38 protected $fileFactory = [ ForeignDBFile::class, 'newFromTitle' ];
39
40 /** @var array */
41 protected $fileFromRowFactory = [ ForeignDBFile::class, 'newFromRow' ];
42
43 /** @var bool */
44 protected $hasSharedCache;
45
46 /**
47 * @param array|null $info
48 */
49 function __construct( $info ) {
50 parent::__construct( $info );
51 $this->wiki = $info['wiki'];
52 $this->hasSharedCache = $info['hasSharedCache'];
53 }
54
55 /**
56 * @return IDatabase
57 */
58 function getMasterDB() {
59 return $this->getDBLoadBalancer()->getConnectionRef( DB_MASTER, [], $this->wiki );
60 }
61
62 /**
63 * @return IDatabase
64 */
65 function getReplicaDB() {
66 return $this->getDBLoadBalancer()->getConnectionRef( DB_REPLICA, [], $this->wiki );
67 }
68
69 /**
70 * @return Closure
71 */
72 protected function getDBFactory() {
73 return function ( $index ) {
74 return $this->getDBLoadBalancer()->getConnectionRef( $index, [], $this->wiki );
75 };
76 }
77
78 /**
79 * @return ILoadBalancer
80 */
81 protected function getDBLoadBalancer() {
82 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
83
84 return $lbFactory->getMainLB( $this->wiki );
85 }
86
87 function hasSharedCache() {
88 return $this->hasSharedCache;
89 }
90
91 /**
92 * Get a key on the primary cache for this repository.
93 * Returns false if the repository's cache is not accessible at this site.
94 * The parameters are the parts of the key, as for wfMemcKey().
95 * @return bool|string
96 */
97 function getSharedCacheKey( /*...*/ ) {
98 if ( $this->hasSharedCache() ) {
99 $args = func_get_args();
100 array_unshift( $args, $this->wiki );
101
102 return implode( ':', $args );
103 } else {
104 return false;
105 }
106 }
107
108 protected function assertWritableRepo() {
109 throw new MWException( static::class . ': write operations are not supported.' );
110 }
111
112 public function getInfo() {
113 return FileRepo::getInfo();
114 }
115 }