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 );
39 public function execute() {
40 // Get a DB handle (with this wiki's DB select) from the appropriate load balancer
41 if ( $this->hasOption( 'cluster' ) ) {
42 $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ) );
43 $dbw = $lb->getConnection( DB_MASTER
); // master for external LB
45 $dbw = wfGetDB( DB_MASTER
); // master for primary LB for this wiki
47 if ( $this->hasArg( 0 ) ) {
48 $file = fopen( $this->getArg( 0 ), 'r' );
50 $this->error( "Unable to open input file", true );
53 $error = $dbw->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
54 if ( $error !== true ) {
55 $this->error( $error, true );
61 $useReadline = function_exists( 'readline_add_history' )
62 && Maintenance
::posix_isatty( 0 /*STDIN*/ );
66 $historyFile = isset( $_ENV['HOME'] ) ?
67 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
68 readline_read_history( $historyFile );
74 while ( ( $line = Maintenance
::readconsole( $prompt ) ) !== false ) {
76 # User simply pressed return key
79 $done = $dbw->streamStatementEnd( $wholeLine, $line );
89 # Delimiter is eated by streamStatementEnd, we add it
90 # up in the history (bug 37020)
91 readline_add_history( $wholeLine . $dbw->getDelimiter() );
92 readline_write_history( $historyFile );
95 $res = $dbw->query( $wholeLine );
96 $this->sqlPrintResult( $res, $dbw );
99 } catch ( DBQueryError
$e ) {
100 $doDie = ! Maintenance
::posix_isatty( 0 );
101 $this->error( $e, $doDie );
108 * Print the results, callback for $db->sourceStream()
109 * @param $res ResultWrapper The results object
110 * @param $db DatabaseBase object
112 public function sqlPrintResult( $res, $db ) {
116 } elseif ( is_object( $res ) && $res->numRows() ) {
117 foreach ( $res as $row ) {
118 $this->output( print_r( $row, true ) );
121 $affected = $db->affectedRows();
122 $this->output( "Query OK, $affected row(s) affected\n" );
127 * @return int DB_TYPE constant
129 public function getDbType() {
130 return Maintenance
::DB_ADMIN
;
134 $maintClass = "MwSql";
135 require_once RUN_MAINTENANCE_IF_MAIN
;