Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.24% covered (warning)
88.24%
15 / 17
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
GetReplicaServer
88.24% covered (warning)
88.24%
15 / 17
50.00% covered (danger)
50.00%
1 / 2
7.08
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 execute
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 1
6.13
1<?php
2/**
3 * Reports the hostname of a replica DB server.
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup Maintenance
8 */
9
10use MediaWiki\Maintenance\Maintenance;
11
12// @codeCoverageIgnoreStart
13require_once __DIR__ . '/Maintenance.php';
14// @codeCoverageIgnoreEnd
15
16/**
17 * Maintenance script that reports the hostname of a replica DB server.
18 *
19 * @ingroup Maintenance
20 */
21class GetReplicaServer extends Maintenance {
22    public function __construct() {
23        parent::__construct();
24        $this->addOption( "group", "Query group to check specifically" );
25        $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
26        $this->addDescription( 'Report the hostname of a replica DB server' );
27    }
28
29    public function execute() {
30        $lbf = $this->getServiceContainer()->getDBLoadBalancerFactory();
31        if ( $this->hasOption( 'cluster' ) ) {
32            try {
33                $lb = $lbf->getExternalLB( $this->getOption( 'cluster' ) );
34            } catch ( InvalidArgumentException $e ) {
35                $this->fatalError( 'Error: ' . $e->getMessage() );
36            }
37        } else {
38            $lb = $lbf->getMainLB();
39        }
40
41        $group = $this->getOption( 'group', false );
42        $index = $lb->getReaderIndex( $group );
43        if ( $index === false && $group ) {
44            // retry without the group; it may not exist
45            $index = $lb->getReaderIndex( false );
46        }
47        if ( $index === false ) {
48            $this->fatalError( 'Error: unable to get reader index' );
49        }
50
51        $this->output( $lb->getServerName( $index ) . "\n" );
52    }
53}
54
55// @codeCoverageIgnoreStart
56$maintClass = GetReplicaServer::class;
57require_once RUN_MAINTENANCE_IF_MAIN;
58// @codeCoverageIgnoreEnd