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
;
29 use Wikimedia\Rdbms\DBQueryError
;
32 * Maintenance script that sends SQL queries from the specified file to the database.
34 * @ingroup Maintenance
36 class MwSql
extends Maintenance
{
37 public function __construct() {
38 parent
::__construct();
39 $this->addDescription( 'Send SQL queries to a MediaWiki database. ' .
40 'Takes a file name containing SQL as argument or runs interactively.' );
41 $this->addOption( 'query',
42 'Run a single query instead of running interactively', false, true );
43 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
44 $this->addOption( 'wikidb',
45 'The database wiki ID to use if not the current one', false, true );
46 $this->addOption( 'replicadb',
47 'Replica DB server to use instead of the master DB (can be "any")', false, true );
50 public function execute() {
53 // We wan't to allow "" for the wikidb, meaning don't call select_db()
54 $wiki = $this->hasOption( 'wikidb' ) ?
$this->getOption( 'wikidb' ) : false;
55 // Get the appropriate load balancer (for this wiki)
56 if ( $this->hasOption( 'cluster' ) ) {
57 $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ) );
59 $lb = wfGetLB( $wiki );
61 // Figure out which server to use
62 $replicaDB = $this->getOption( 'replicadb', $this->getOption( 'slave', '' ) );
63 if ( $replicaDB === 'any' ) {
65 } elseif ( $replicaDB != '' ) {
67 $serverCount = $lb->getServerCount();
68 for ( $i = 0; $i < $serverCount; ++
$i ) {
69 if ( $lb->getServerName( $i ) === $replicaDB ) {
74 if ( $index === null ) {
75 $this->fatalError( "No replica DB server configured with the name '$replicaDB'." );
81 /** @var IDatabase $db DB handle for the appropriate cluster/wiki */
82 $db = $lb->getConnection( $index, [], $wiki );
83 if ( $replicaDB != '' && $db->getLBInfo( 'master' ) !== null ) {
84 $this->fatalError( "The server selected ({$db->getServer()}) is not a replica DB." );
87 if ( $index === DB_MASTER
) {
88 $updater = DatabaseUpdater
::newForDB( $db, true, $this );
89 $db->setSchemaVars( $updater->getSchemaVars() );
92 if ( $this->hasArg( 0 ) ) {
93 $file = fopen( $this->getArg( 0 ), 'r' );
95 $this->fatalError( "Unable to open input file" );
98 $error = $db->sourceStream( $file, null, [ $this, 'sqlPrintResult' ] );
99 if ( $error !== true ) {
100 $this->fatalError( $error );
106 if ( $this->hasOption( 'query' ) ) {
107 $query = $this->getOption( 'query' );
108 $this->sqlDoQuery( $db, $query, /* dieOnError */ true );
114 function_exists( 'readline_add_history' ) &&
115 Maintenance
::posix_isatty( 0 /*STDIN*/ )
117 $historyFile = isset( $_ENV['HOME'] ) ?
118 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
119 readline_read_history( $historyFile );
126 $prompt = $newPrompt;
127 $doDie = !Maintenance
::posix_isatty( 0 );
128 while ( ( $line = Maintenance
::readconsole( $prompt ) ) !== false ) {
130 # User simply pressed return key
133 $done = $db->streamStatementEnd( $wholeLine, $line );
142 if ( $historyFile ) {
143 # Delimiter is eated by streamStatementEnd, we add it
144 # up in the history (T39020)
145 readline_add_history( $wholeLine . ';' );
146 readline_write_history( $historyFile );
148 $this->sqlDoQuery( $db, $wholeLine, $doDie );
149 $prompt = $newPrompt;
155 protected function sqlDoQuery( IDatabase
$db, $line, $dieOnError ) {
157 $res = $db->query( $line );
158 $this->sqlPrintResult( $res, $db );
159 } catch ( DBQueryError
$e ) {
161 $this->fatalError( $e );
169 * Print the results, callback for $db->sourceStream()
170 * @param ResultWrapper|bool $res
171 * @param IDatabase $db
173 public function sqlPrintResult( $res, $db ) {
177 } elseif ( is_object( $res ) && $res->numRows() ) {
178 foreach ( $res as $row ) {
179 $this->output( print_r( $row, true ) );
182 $affected = $db->affectedRows();
183 $this->output( "Query OK, $affected row(s) affected\n" );
188 * @return int DB_TYPE constant
190 public function getDbType() {
191 return Maintenance
::DB_ADMIN
;
195 $maintClass = MwSql
::class;
196 require_once RUN_MAINTENANCE_IF_MAIN
;