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 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
37 $this->addOption( 'wikidb', 'The database wiki ID to use if not the current one', false, true );
38 $this->addOption( 'slave', 'Use a slave server (either "any" or by name)', false, true );
41 public function execute() {
42 $wiki = $this->getOption( 'wikidb' ) ?
: false;
43 // Get the appropriate load balancer (for this wiki)
44 if ( $this->hasOption( 'cluster' ) ) {
45 $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ), $wiki );
47 $lb = wfGetLB( $wiki );
49 // Figure out which server to use
50 if ( $this->hasOption( 'slave' ) ) {
51 $server = $this->getOption( 'slave' );
52 if ( $server === 'any' ) {
56 $serverCount = $lb->getServerCount();
57 for ( $i = 0; $i < $serverCount; ++
$i ) {
58 if ( $lb->getServerName( $i ) === $server ) {
63 if ( $index === null ) {
64 $this->error( "No slave server configured with the name '$server'.", 1 );
70 // Get a DB handle (with this wiki's DB selected) from the appropriate load balancer
71 $db = $lb->getConnection( $index, array(), $wiki );
72 if ( $this->hasOption( 'slave' ) && $db->getLBInfo( 'master' ) !== null ) {
73 $this->error( "The server selected ({$db->getServer()}) is not a slave.", 1 );
76 if ( $this->hasArg( 0 ) ) {
77 $file = fopen( $this->getArg( 0 ), 'r' );
79 $this->error( "Unable to open input file", true );
82 $error = $db->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
83 if ( $error !== true ) {
84 $this->error( $error, true );
90 $useReadline = function_exists( 'readline_add_history' )
91 && Maintenance
::posix_isatty( 0 /*STDIN*/ );
95 $historyFile = isset( $_ENV['HOME'] ) ?
96 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
97 readline_read_history( $historyFile );
102 $prompt = $newPrompt;
103 while ( ( $line = Maintenance
::readconsole( $prompt ) ) !== false ) {
105 # User simply pressed return key
108 $done = $db->streamStatementEnd( $wholeLine, $line );
117 if ( $useReadline ) {
118 # Delimiter is eated by streamStatementEnd, we add it
119 # up in the history (bug 37020)
120 readline_add_history( $wholeLine . $db->getDelimiter() );
121 readline_write_history( $historyFile );
124 $res = $db->query( $wholeLine );
125 $this->sqlPrintResult( $res, $db );
126 $prompt = $newPrompt;
128 } catch ( DBQueryError
$e ) {
129 $doDie = !Maintenance
::posix_isatty( 0 );
130 $this->error( $e, $doDie );
137 * Print the results, callback for $db->sourceStream()
138 * @param ResultWrapper $res The results object
139 * @param DatabaseBase $db object
141 public function sqlPrintResult( $res, $db ) {
145 } elseif ( is_object( $res ) && $res->numRows() ) {
146 foreach ( $res as $row ) {
147 $this->output( print_r( $row, true ) );
150 $affected = $db->affectedRows();
151 $this->output( "Query OK, $affected row(s) affected\n" );
156 * @return int DB_TYPE constant
158 public function getDbType() {
159 return Maintenance
::DB_ADMIN
;
163 $maintClass = "MwSql";
164 require_once RUN_MAINTENANCE_IF_MAIN
;