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';
28 * Maintenance script that sends SQL queries from the specified file to the database.
30 * @ingroup Maintenance
32 class MwSql
extends Maintenance
{
33 public function __construct() {
34 parent
::__construct();
35 $this->addDescription( 'Send SQL queries to a MediaWiki database. ' .
36 'Takes a file name containing SQL as argument or runs interactively.' );
37 $this->addOption( 'query',
38 'Run a single query instead of running interactively', false, true );
39 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
40 $this->addOption( 'wikidb',
41 'The database wiki ID to use if not the current one', false, true );
42 $this->addOption( 'replicadb',
43 'Replica DB server to use instead of the master DB (can be "any")', false, true );
46 public function execute() {
47 // We wan't to allow "" for the wikidb, meaning don't call select_db()
48 $wiki = $this->hasOption( 'wikidb' ) ?
$this->getOption( 'wikidb' ) : false;
49 // Get the appropriate load balancer (for this wiki)
50 if ( $this->hasOption( 'cluster' ) ) {
51 $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ), $wiki );
53 $lb = wfGetLB( $wiki );
55 // Figure out which server to use
56 $replicaDB = $this->getOption( 'replicadb', $this->getOption( 'slave', '' ) );
57 if ( $replicaDB === 'any' ) {
59 } elseif ( $replicaDB != '' ) {
61 $serverCount = $lb->getServerCount();
62 for ( $i = 0; $i < $serverCount; ++
$i ) {
63 if ( $lb->getServerName( $i ) === $replicaDB ) {
68 if ( $index === null ) {
69 $this->error( "No replica DB server configured with the name '$server'.", 1 );
74 // Get a DB handle (with this wiki's DB selected) from the appropriate load balancer
75 $db = $lb->getConnection( $index, [], $wiki );
76 if ( $replicaDB != '' && $db->getLBInfo( 'master' ) !== null ) {
77 $this->error( "The server selected ({$db->getServer()}) is not a replica DB.", 1 );
80 if ( $this->hasArg( 0 ) ) {
81 $file = fopen( $this->getArg( 0 ), 'r' );
83 $this->error( "Unable to open input file", true );
86 $error = $db->sourceStream( $file, false, [ $this, 'sqlPrintResult' ] );
87 if ( $error !== true ) {
88 $this->error( $error, true );
94 if ( $this->hasOption( 'query' ) ) {
95 $query = $this->getOption( 'query' );
96 $this->sqlDoQuery( $db, $query, /* dieOnError */ true );
101 $useReadline = function_exists( 'readline_add_history' )
102 && Maintenance
::posix_isatty( 0 /*STDIN*/ );
104 if ( $useReadline ) {
106 $historyFile = isset( $_ENV['HOME'] ) ?
107 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
108 readline_read_history( $historyFile );
113 $prompt = $newPrompt;
114 $doDie = !Maintenance
::posix_isatty( 0 );
115 while ( ( $line = Maintenance
::readconsole( $prompt ) ) !== false ) {
117 # User simply pressed return key
120 $done = $db->streamStatementEnd( $wholeLine, $line );
129 if ( $useReadline ) {
130 # Delimiter is eated by streamStatementEnd, we add it
131 # up in the history (bug 37020)
132 readline_add_history( $wholeLine . $db->getDelimiter() );
133 readline_write_history( $historyFile );
135 $this->sqlDoQuery( $db, $wholeLine, $doDie );
136 $prompt = $newPrompt;
142 protected function sqlDoQuery( $db, $line, $dieOnError ) {
144 $res = $db->query( $line );
145 $this->sqlPrintResult( $res, $db );
146 } catch ( DBQueryError
$e ) {
147 $this->error( $e, $dieOnError );
152 * Print the results, callback for $db->sourceStream()
153 * @param ResultWrapper $res The results object
154 * @param DatabaseBase $db
156 public function sqlPrintResult( $res, $db ) {
160 } elseif ( is_object( $res ) && $res->numRows() ) {
161 foreach ( $res as $row ) {
162 $this->output( print_r( $row, true ) );
165 $affected = $db->affectedRows();
166 $this->output( "Query OK, $affected row(s) affected\n" );
171 * @return int DB_TYPE constant
173 public function getDbType() {
174 return Maintenance
::DB_ADMIN
;
178 $maintClass = "MwSql";
179 require_once RUN_MAINTENANCE_IF_MAIN
;