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
21 * @ingroup Maintenance
24 require_once( dirname( __FILE__
) . '/Maintenance.php' );
26 class MwSql
extends Maintenance
{
27 public function __construct() {
28 parent
::__construct();
29 $this->mDescription
= "Send SQL queries to a MediaWiki database";
32 public function execute() {
33 $dbw = wfGetDB( DB_MASTER
);
34 if ( $this->hasArg() ) {
35 $fileName = $this->getArg();
36 $file = fopen( $fileName, 'r' );
38 $this->error( "Unable to open input file", true );
41 $error = $dbw->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
42 if ( $error !== true ) {
43 $this->error( $error, true );
49 $useReadline = function_exists( 'readline_add_history' )
50 && Maintenance
::posix_isatty( 0 /*STDIN*/ );
54 $historyFile = isset( $_ENV['HOME'] ) ?
55 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
56 readline_read_history( $historyFile );
60 while ( ( $line = Maintenance
::readconsole() ) !== false ) {
61 $done = $dbw->streamStatementEnd( $wholeLine, $line );
69 readline_add_history( $wholeLine );
70 readline_write_history( $historyFile );
73 $res = $dbw->query( $wholeLine );
74 $this->sqlPrintResult( $res, $dbw );
76 } catch (DBQueryError
$e) {
77 $this->error( $e, true );
83 * Print the results, callback for $db->sourceStream()
84 * @param $res ResultWrapper The results object
85 * @param $db DatabaseBase object
87 public function sqlPrintResult( $res, $db ) {
90 } elseif ( is_object( $res ) && $res->numRows() ) {
91 foreach ( $res as $row ) {
92 $this->output( print_r( $row, true ) );
95 $affected = $db->affectedRows();
96 $this->output( "Query OK, $affected row(s) affected\n" );
101 * @return int DB_TYPE constant
103 public function getDbType() {
104 return Maintenance
::DB_ADMIN
;
108 $maintClass = "MwSql";
109 require_once( RUN_MAINTENANCE_IF_MAIN
);