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->mDescription
= "Send SQL queries to a MediaWiki database. " .
36 "Takes a file name containing SQL as argument or runs interactively.";
37 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
38 $this->addOption( 'wikidb', 'The database wiki ID to use if not the current one', false, true );
39 $this->addOption( 'slave', 'Use a slave server (either "any" or by name)', false, true );
42 public function execute() {
43 // We wan't to allow "" for the wikidb, meaning don't call select_db()
44 $wiki = $this->hasOption( 'wikidb' ) ?
$this->getOption( 'wikidb' ) : false;
45 // Get the appropriate load balancer (for this wiki)
46 if ( $this->hasOption( 'cluster' ) ) {
47 $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ), $wiki );
49 $lb = wfGetLB( $wiki );
51 // Figure out which server to use
52 if ( $this->hasOption( 'slave' ) ) {
53 $server = $this->getOption( 'slave' );
54 if ( $server === 'any' ) {
58 $serverCount = $lb->getServerCount();
59 for ( $i = 0; $i < $serverCount; ++
$i ) {
60 if ( $lb->getServerName( $i ) === $server ) {
65 if ( $index === null ) {
66 $this->error( "No slave server configured with the name '$server'.", 1 );
72 // Get a DB handle (with this wiki's DB selected) from the appropriate load balancer
73 $db = $lb->getConnection( $index, array(), $wiki );
74 if ( $this->hasOption( 'slave' ) && $db->getLBInfo( 'master' ) !== null ) {
75 $this->error( "The server selected ({$db->getServer()}) is not a slave.", 1 );
78 if ( $this->hasArg( 0 ) ) {
79 $file = fopen( $this->getArg( 0 ), 'r' );
81 $this->error( "Unable to open input file", true );
84 $error = $db->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
85 if ( $error !== true ) {
86 $this->error( $error, true );
92 $useReadline = function_exists( 'readline_add_history' )
93 && Maintenance
::posix_isatty( 0 /*STDIN*/ );
97 $historyFile = isset( $_ENV['HOME'] ) ?
98 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
99 readline_read_history( $historyFile );
104 $prompt = $newPrompt;
105 while ( ( $line = Maintenance
::readconsole( $prompt ) ) !== false ) {
107 # User simply pressed return key
110 $done = $db->streamStatementEnd( $wholeLine, $line );
119 if ( $useReadline ) {
120 # Delimiter is eated by streamStatementEnd, we add it
121 # up in the history (bug 37020)
122 readline_add_history( $wholeLine . $db->getDelimiter() );
123 readline_write_history( $historyFile );
126 $res = $db->query( $wholeLine );
127 $this->sqlPrintResult( $res, $db );
128 $prompt = $newPrompt;
130 } catch ( DBQueryError
$e ) {
131 $doDie = !Maintenance
::posix_isatty( 0 );
132 $this->error( $e, $doDie );
139 * Print the results, callback for $db->sourceStream()
140 * @param ResultWrapper $res The results object
141 * @param DatabaseBase $db
143 public function sqlPrintResult( $res, $db ) {
147 } elseif ( is_object( $res ) && $res->numRows() ) {
148 foreach ( $res as $row ) {
149 $this->output( print_r( $row, true ) );
152 $affected = $db->affectedRows();
153 $this->output( "Query OK, $affected row(s) affected\n" );
158 * @return int DB_TYPE constant
160 public function getDbType() {
161 return Maintenance
::DB_ADMIN
;
165 $maintClass = "MwSql";
166 require_once RUN_MAINTENANCE_IF_MAIN
;