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', 'Run a single query instead of running interactively', false, true );
38 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
39 $this->addOption( 'wikidb', 'The database wiki ID to use if not the current one', false, true );
40 $this->addOption( 'slave', 'Use a slave server (either "any" or by name)', false, true );
43 public function execute() {
44 // We wan't to allow "" for the wikidb, meaning don't call select_db()
45 $wiki = $this->hasOption( 'wikidb' ) ?
$this->getOption( 'wikidb' ) : false;
46 // Get the appropriate load balancer (for this wiki)
47 if ( $this->hasOption( 'cluster' ) ) {
48 $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ), $wiki );
50 $lb = wfGetLB( $wiki );
52 // Figure out which server to use
53 if ( $this->hasOption( 'slave' ) ) {
54 $server = $this->getOption( 'slave' );
55 if ( $server === 'any' ) {
59 $serverCount = $lb->getServerCount();
60 for ( $i = 0; $i < $serverCount; ++
$i ) {
61 if ( $lb->getServerName( $i ) === $server ) {
66 if ( $index === null ) {
67 $this->error( "No slave server configured with the name '$server'.", 1 );
73 // Get a DB handle (with this wiki's DB selected) from the appropriate load balancer
74 $db = $lb->getConnection( $index, [], $wiki );
75 if ( $this->hasOption( 'slave' ) && $db->getLBInfo( 'master' ) !== null ) {
76 $this->error( "The server selected ({$db->getServer()}) is not a slave.", 1 );
79 if ( $this->hasArg( 0 ) ) {
80 $file = fopen( $this->getArg( 0 ), 'r' );
82 $this->error( "Unable to open input file", true );
85 $error = $db->sourceStream( $file, false, [ $this, 'sqlPrintResult' ] );
86 if ( $error !== true ) {
87 $this->error( $error, true );
93 if ( $this->hasOption( 'query' ) ) {
94 $query = $this->getOption( 'query' );
95 $this->sqlDoQuery( $db, $query, /* dieOnError */ true );
100 $useReadline = function_exists( 'readline_add_history' )
101 && Maintenance
::posix_isatty( 0 /*STDIN*/ );
103 if ( $useReadline ) {
105 $historyFile = isset( $_ENV['HOME'] ) ?
106 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
107 readline_read_history( $historyFile );
112 $prompt = $newPrompt;
113 $doDie = !Maintenance
::posix_isatty( 0 );
114 while ( ( $line = Maintenance
::readconsole( $prompt ) ) !== false ) {
116 # User simply pressed return key
119 $done = $db->streamStatementEnd( $wholeLine, $line );
128 if ( $useReadline ) {
129 # Delimiter is eated by streamStatementEnd, we add it
130 # up in the history (bug 37020)
131 readline_add_history( $wholeLine . $db->getDelimiter() );
132 readline_write_history( $historyFile );
134 $this->sqlDoQuery( $db, $wholeLine, $doDie );
135 $prompt = $newPrompt;
141 protected function sqlDoQuery( $db, $line, $dieOnError ) {
143 $res = $db->query( $line );
144 $this->sqlPrintResult( $res, $db );
145 } catch ( DBQueryError
$e ) {
146 $this->error( $e, $dieOnError );
151 * Print the results, callback for $db->sourceStream()
152 * @param ResultWrapper $res The results object
153 * @param DatabaseBase $db
155 public function sqlPrintResult( $res, $db ) {
159 } elseif ( is_object( $res ) && $res->numRows() ) {
160 foreach ( $res as $row ) {
161 $this->output( print_r( $row, true ) );
164 $affected = $db->affectedRows();
165 $this->output( "Query OK, $affected row(s) affected\n" );
170 * @return int DB_TYPE constant
172 public function getDbType() {
173 return Maintenance
::DB_ADMIN
;
177 $maintClass = "MwSql";
178 require_once RUN_MAINTENANCE_IF_MAIN
;