3 * Send SQL queries from the specified file to the database, performing
4 * variable replacement along the way.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup Maintenance
25 require_once __DIR__
. '/Maintenance.php';
27 use Wikimedia\Rdbms\ResultWrapper
;
28 use Wikimedia\Rdbms\IDatabase
;
31 * Maintenance script that sends SQL queries from the specified file to the database.
33 * @ingroup Maintenance
35 class MwSql
extends Maintenance
{
36 public function __construct() {
37 parent
::__construct();
38 $this->addDescription( 'Send SQL queries to a MediaWiki database. ' .
39 'Takes a file name containing SQL as argument or runs interactively.' );
40 $this->addOption( 'query',
41 'Run a single query instead of running interactively', false, true );
42 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
43 $this->addOption( 'wikidb',
44 'The database wiki ID to use if not the current one', false, true );
45 $this->addOption( 'replicadb',
46 'Replica DB server to use instead of the master DB (can be "any")', false, true );
49 public function execute() {
52 // We wan't to allow "" for the wikidb, meaning don't call select_db()
53 $wiki = $this->hasOption( 'wikidb' ) ?
$this->getOption( 'wikidb' ) : false;
54 // Get the appropriate load balancer (for this wiki)
55 if ( $this->hasOption( 'cluster' ) ) {
56 $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ) );
58 $lb = wfGetLB( $wiki );
60 // Figure out which server to use
61 $replicaDB = $this->getOption( 'replicadb', $this->getOption( 'slave', '' ) );
62 if ( $replicaDB === 'any' ) {
64 } elseif ( $replicaDB != '' ) {
66 $serverCount = $lb->getServerCount();
67 for ( $i = 0; $i < $serverCount; ++
$i ) {
68 if ( $lb->getServerName( $i ) === $replicaDB ) {
73 if ( $index === null ) {
74 $this->error( "No replica DB server configured with the name '$replicaDB'.", 1 );
80 /** @var Database $db DB handle for the appropriate cluster/wiki */
81 $db = $lb->getConnection( $index, [], $wiki );
82 if ( $replicaDB != '' && $db->getLBInfo( 'master' ) !== null ) {
83 $this->error( "The server selected ({$db->getServer()}) is not a replica DB.", 1 );
86 if ( $index === DB_MASTER
) {
87 $updater = DatabaseUpdater
::newForDB( $db, true, $this );
88 $db->setSchemaVars( $updater->getSchemaVars() );
91 if ( $this->hasArg( 0 ) ) {
92 $file = fopen( $this->getArg( 0 ), 'r' );
94 $this->error( "Unable to open input file", true );
97 $error = $db->sourceStream( $file, null, [ $this, 'sqlPrintResult' ] );
98 if ( $error !== true ) {
99 $this->error( $error, true );
105 if ( $this->hasOption( 'query' ) ) {
106 $query = $this->getOption( 'query' );
107 $this->sqlDoQuery( $db, $query, /* dieOnError */ true );
113 function_exists( 'readline_add_history' ) &&
114 Maintenance
::posix_isatty( 0 /*STDIN*/ )
116 $historyFile = isset( $_ENV['HOME'] ) ?
117 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
118 readline_read_history( $historyFile );
125 $prompt = $newPrompt;
126 $doDie = !Maintenance
::posix_isatty( 0 );
127 while ( ( $line = Maintenance
::readconsole( $prompt ) ) !== false ) {
129 # User simply pressed return key
132 $done = $db->streamStatementEnd( $wholeLine, $line );
141 if ( $historyFile ) {
142 # Delimiter is eated by streamStatementEnd, we add it
143 # up in the history (T39020)
144 readline_add_history( $wholeLine . ';' );
145 readline_write_history( $historyFile );
147 $this->sqlDoQuery( $db, $wholeLine, $doDie );
148 $prompt = $newPrompt;
154 protected function sqlDoQuery( IDatabase
$db, $line, $dieOnError ) {
156 $res = $db->query( $line );
157 $this->sqlPrintResult( $res, $db );
158 } catch ( DBQueryError
$e ) {
159 $this->error( $e, $dieOnError );
164 * Print the results, callback for $db->sourceStream()
165 * @param ResultWrapper|bool $res The results object
166 * @param IDatabase $db
168 public function sqlPrintResult( $res, $db ) {
172 } elseif ( is_object( $res ) && $res->numRows() ) {
173 foreach ( $res as $row ) {
174 $this->output( print_r( $row, true ) );
177 $affected = $db->affectedRows();
178 $this->output( "Query OK, $affected row(s) affected\n" );
183 * @return int DB_TYPE constant
185 public function getDbType() {
186 return Maintenance
::DB_ADMIN
;
190 $maintClass = "MwSql";
191 require_once RUN_MAINTENANCE_IF_MAIN
;