MediaWiki  master
getReplicaServer.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
27 
34  public function __construct() {
35  parent::__construct();
36  $this->addOption( "group", "Query group to check specifically" );
37  $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
38  $this->addDescription( 'Report the hostname of a replica DB server' );
39  }
40 
41  public function execute() {
42  $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
43  if ( $this->hasOption( 'cluster' ) ) {
44  try {
45  $lb = $lbf->getExternalLB( $this->getOption( 'cluster' ) );
46  } catch ( InvalidArgumentException $e ) {
47  $this->fatalError( 'Error: ' . $e->getMessage() );
48  }
49  } else {
50  $lb = $lbf->getMainLB();
51  }
52 
53  $group = $this->getOption( 'group', false );
54  $index = $lb->getReaderIndex( $group );
55  if ( $index === false && $group ) {
56  // retry without the group; it may not exist
57  $index = $lb->getReaderIndex( false );
58  }
59  if ( $index === false ) {
60  $this->fatalError( 'Error: unable to get reader index' );
61  }
62 
63  $this->output( $lb->getServerName( $index ) . "\n" );
64  }
65 }
66 
67 $maintClass = GetReplicaServer::class;
68 require_once RUN_MAINTENANCE_IF_MAIN;
GetReplicaServer\execute
execute()
Do the actual work.
Definition: getReplicaServer.php:41
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:203
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
GetReplicaServer\__construct
__construct()
Default constructor.
Definition: getReplicaServer.php:34
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
GetReplicaServer
Maintenance script that reports the hostname of a replica DB server.
Definition: getReplicaServer.php:33
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271
$maintClass
$maintClass
Definition: getReplicaServer.php:67