MediaWiki REL1_39
getReplicaServer.php
Go to the documentation of this file.
1<?php
24require_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;
68require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script that reports the hostname of a replica DB server.
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Service locator for MediaWiki core services.