Debian Codegraph @

extensions/Echo/includes/DbFactory.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications;
4
5 use MediaWiki\MediaWikiServices;
6 use Wikimedia\Rdbms\IDatabase;
7 use Wikimedia\Rdbms\ILoadBalancer;
8
9 /**
10 * Database factory class, this will determine whether to use the main database
11 * or an external database defined in the configuration file
12 */
13 class DbFactory {
14
15 /**
16 * The cluster for the database
17 * @var string|false
18 */
19 private $cluster;
20
21 /** @var string|false */
22 private $shared;
23
24 /** @var string|false */
25 private $sharedCluster;
26
27 /**
28 * @param string|false $cluster
29 * @param string|false $shared
30 * @param string|false $sharedCluster
31 */
32 public function __construct( $cluster = false, $shared = false, $sharedCluster = false ) {
33 $this->cluster = $cluster;
34 $this->shared = $shared;
35 $this->sharedCluster = $sharedCluster;
36 }
37
38 /**
39 * Create a db factory instance from default Echo configuration
40 * A singleton is not necessary because it's actually handled
41 * inside core database object
42 *
43 * @return DbFactory
44 */
45 public static function newFromDefault() {
46 global $wgEchoCluster, $wgEchoSharedTrackingDB, $wgEchoSharedTrackingCluster;
47
48 return new self( $wgEchoCluster, $wgEchoSharedTrackingDB, $wgEchoSharedTrackingCluster );
49 }
50
51 /**
52 * @return bool
53 */
54 public function isReadOnly() {
55 return ( $this->getLB()->getReadOnlyReason() !== false );
56 }
57
58 /**
59 * Get the database load balancer
60 * @return ILoadBalancer
61 */
62 protected function getLB() {
63 // Use the external db defined for Echo
64 if ( $this->cluster ) {
65 $lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getExternalLB( $this->cluster );
66 } else {
67 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
68 }
69
70 return $lb;
71 }
72
73 /**
74 * @return ILoadBalancer
75 */
76 protected function getSharedLB() {
77 if ( $this->sharedCluster ) {
78 $lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getExternalLB( $this->sharedCluster );
79 } else {
80 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
81 }
82
83 return $lb;
84 }
85
86 /**
87 * Get the database connection for Echo
88 * @param int $db Index of the connection to get
89 * @param string[] $groups Query groups.
90 * @return IDatabase
91 */
92 public function getEchoDb( $db, array $groups = [] ) {
93 return $this->getLB()->getConnection( $db, $groups );
94 }
95
96 /**
97 * @param int $db Index of the connection to get
98 * @param string[] $groups Query groups
99 *
100 * @return bool|IDatabase false if no shared db is configured
101 */
102 public function getSharedDb( $db, array $groups = [] ) {
103 if ( !$this->shared ) {
104 return false;
105 }
106
107 return $this->getSharedLB()->getConnection( $db, $groups, $this->shared );
108 }
109
110 /**
111 * Wrapper function for LBFactory::getExternalLB/getMainLB, some extensions like MobileFrontend is
112 * using this to issue sql queries against Echo database directly. This
113 * is totally not accepted and should be updated to use Echo database access
114 * objects
115 *
116 * @deprecated Use newFromDefault() instead to create a db factory
117 * @param int $db Index of the connection to get
118 * @param string[] $groups Query groups.
119 * @param string|bool $wiki The wiki ID, or false for the current wiki
120 * @return IDatabase
121 */
122 public static function getDB( $db, array $groups = [], $wiki = false ) {
123 global $wgEchoCluster;
124
125 $services = MediaWikiServices::getInstance();
126
127 // Use the external db defined for Echo
128 if ( $wgEchoCluster ) {
129 $lb = $services->getDBLoadBalancerFactory()->getExternalLB( $wgEchoCluster );
130 } else {
131 if ( $wiki === false ) {
132 $lb = $services->getDBLoadBalancer();
133 } else {
134 $lb = $services->getDBLoadBalancerFactory()->getMainLB( $wiki );
135 }
136 }
137
138 return $lb->getConnection( $db, $groups, $wiki );
139 }
140
141 /**
142 * Check whether it makes sense to retry a failed lookup on the primary database.
143 * @return bool True if there are multiple servers and changes were made in this request; false otherwise
144 */
145 public function canRetryPrimary() {
146 return $this->getLB()->getServerCount() > 1 && $this->getLB()->hasOrMadeRecentPrimaryChanges();
147 }
148 }
149
150 class_alias( DbFactory::class, 'MWEchoDbFactory' );