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 if ( $this->hasArg() ) {
34 $fileName = $this->getArg();
35 $file = fopen( $fileName, 'r' );
37 $file = $this->getStdin();
41 $this->error( "Unable to open input file", true );
44 $useReadline = function_exists( 'readline_add_history' )
45 && Maintenance
::posix_isatty( 0 /*STDIN*/ );
49 $historyFile = isset( $_ENV['HOME'] ) ?
50 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
51 readline_read_history( $historyFile );
54 $dbw = wfGetDB( DB_MASTER
);
56 while ( ( $line = Maintenance
::readconsole() ) !== false ) {
57 $done = $dbw->streamStatementEnd( $wholeLine, $line );
65 readline_add_history( $wholeLine );
66 readline_write_history( $historyFile );
69 $res = $dbw->query( $wholeLine );
70 $this->sqlPrintResult( $res, $dbw );
72 } catch (DBQueryError
$e) {
73 $this->error( $e, true );
79 * Print the results, callback for $db->sourceStream()
80 * @param $res ResultWrapper The results object
81 * @param $db DatabaseBase object
83 public function sqlPrintResult( $res, $db ) {
86 } elseif ( is_object( $res ) && $res->numRows() ) {
87 foreach ( $res as $row ) {
88 $this->output( print_r( $row, true ) );
91 $affected = $db->affectedRows();
92 $this->output( "Query OK, $affected row(s) affected\n" );
97 * @return int DB_TYPE constant
99 public function getDbType() {
100 return Maintenance
::DB_ADMIN
;
104 $maintClass = "MwSql";
105 require_once( RUN_MAINTENANCE_IF_MAIN
);