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";
38 public function execute() {
39 $dbw = wfGetDB( DB_MASTER
);
40 if ( $this->hasArg() ) {
41 $fileName = $this->getArg();
42 $file = fopen( $fileName, 'r' );
44 $this->error( "Unable to open input file", true );
47 $error = $dbw->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
48 if ( $error !== true ) {
49 $this->error( $error, true );
55 $useReadline = function_exists( 'readline_add_history' )
56 && Maintenance
::posix_isatty( 0 /*STDIN*/ );
60 $historyFile = isset( $_ENV['HOME'] ) ?
61 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
62 readline_read_history( $historyFile );
68 while ( ( $line = Maintenance
::readconsole( $prompt ) ) !== false ) {
70 # User simply pressed return key
73 $done = $dbw->streamStatementEnd( $wholeLine, $line );
83 # Delimiter is eated by streamStatementEnd, we add it
84 # up in the history (bug 37020)
85 readline_add_history( $wholeLine . $dbw->getDelimiter() );
86 readline_write_history( $historyFile );
89 $res = $dbw->query( $wholeLine );
90 $this->sqlPrintResult( $res, $dbw );
93 } catch (DBQueryError
$e) {
94 $doDie = ! Maintenance
::posix_isatty( 0 );
95 $this->error( $e, $doDie );
101 * Print the results, callback for $db->sourceStream()
102 * @param $res ResultWrapper The results object
103 * @param $db DatabaseBase object
105 public function sqlPrintResult( $res, $db ) {
108 } elseif ( is_object( $res ) && $res->numRows() ) {
109 foreach ( $res as $row ) {
110 $this->output( print_r( $row, true ) );
113 $affected = $db->affectedRows();
114 $this->output( "Query OK, $affected row(s) affected\n" );
119 * @return int DB_TYPE constant
121 public function getDbType() {
122 return Maintenance
::DB_ADMIN
;
126 $maintClass = "MwSql";
127 require_once( RUN_MAINTENANCE_IF_MAIN
);