MediaWiki  1.34.0
LoadBalancerSingle.php
Go to the documentation of this file.
1 <?php
24 namespace Wikimedia\Rdbms;
25 
26 use InvalidArgumentException;
27 
33  private $db;
34 
39  public function __construct( array $params ) {
40  if ( !isset( $params['connection'] ) ) {
41  throw new InvalidArgumentException( "Missing 'connection' argument." );
42  }
43 
44  $this->db = $params['connection'];
45 
46  parent::__construct( [
47  'servers' => [
48  [
49  'type' => $this->db->getType(),
50  'host' => $this->db->getServer(),
51  'dbname' => $this->db->getDBname(),
52  'load' => 1,
53  ]
54  ],
55  'trxProfiler' => $params['trxProfiler'] ?? null,
56  'srvCache' => $params['srvCache'] ?? null,
57  'wanCache' => $params['wanCache'] ?? null,
58  'localDomain' => $params['localDomain'] ?? $this->db->getDomainID(),
59  'readOnlyReason' => $params['readOnlyReason'] ?? false,
60  ] );
61 
62  if ( isset( $params['readOnlyReason'] ) ) {
63  $this->db->setLBInfo( 'readOnlyReason', $params['readOnlyReason'] );
64  }
65  }
66 
73  public static function newFromConnection( IDatabase $db, array $params = [] ) {
74  return new static( array_merge(
75  [ 'localDomain' => $db->getDomainID() ],
76  $params,
77  [ 'connection' => $db ]
78  ) );
79  }
80 
81  protected function reallyOpenConnection( array $server, DatabaseDomain $domain ) {
82  return $this->db;
83  }
84 
85  public function __destruct() {
86  // do nothing since the connection was injected
87  }
88 }
89 
93 class_alias( LoadBalancerSingle::class, 'LoadBalancerSingle' );
Wikimedia\Rdbms\LoadBalancerSingle
Trivial LoadBalancer that always returns an injected connection handle.
Definition: LoadBalancerSingle.php:31
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
Wikimedia\Rdbms\LoadBalancerSingle\reallyOpenConnection
reallyOpenConnection(array $server, DatabaseDomain $domain)
Open a new network connection to a server (uncached)
Definition: LoadBalancerSingle.php:81
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
Wikimedia\Rdbms\LoadBalancer
Database connection, tracking, load balancing, and transaction manager for a cluster.
Definition: LoadBalancer.php:42
Wikimedia\Rdbms\LoadBalancerSingle\newFromConnection
static newFromConnection(IDatabase $db, array $params=[])
Definition: LoadBalancerSingle.php:73
Wikimedia\Rdbms\IDatabase\getDomainID
getDomainID()
Return the currently selected domain ID.
Wikimedia\Rdbms\LoadBalancerSingle\$db
IDatabase $db
Definition: LoadBalancerSingle.php:33
Wikimedia\Rdbms\LoadBalancerSingle\__destruct
__destruct()
Definition: LoadBalancerSingle.php:85
Wikimedia\Rdbms\LoadBalancerSingle\__construct
__construct(array $params)
Definition: LoadBalancerSingle.php:39
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/schema/prefix specifications for IDatabase.
Definition: DatabaseDomain.php:40