Merge "Disable transaction warnings for automatic trx."
[mediawiki.git] / maintenance / sql.php
blob04e98d918d26865e66038c6559d4ca3ae3ce0199
1 <?php
2 /**
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 * @file
22 * @ingroup Maintenance
25 require_once( __DIR__ . '/Maintenance.php' );
27 /**
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' );
43 if ( !$file ) {
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 );
50 } else {
51 exit( 0 );
55 $useReadline = function_exists( 'readline_add_history' )
56 && Maintenance::posix_isatty( 0 /*STDIN*/ );
58 if ( $useReadline ) {
59 global $IP;
60 $historyFile = isset( $_ENV['HOME'] ) ?
61 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
62 readline_read_history( $historyFile );
65 $wholeLine = '';
66 while ( ( $line = Maintenance::readconsole() ) !== false ) {
67 $done = $dbw->streamStatementEnd( $wholeLine, $line );
69 $wholeLine .= $line;
71 if ( !$done ) {
72 continue;
74 if ( $useReadline ) {
75 readline_add_history( $wholeLine );
76 readline_write_history( $historyFile );
78 try{
79 $res = $dbw->query( $wholeLine );
80 $this->sqlPrintResult( $res, $dbw );
81 $wholeLine = '';
82 } catch (DBQueryError $e) {
83 $this->error( $e, true );
88 /**
89 * Print the results, callback for $db->sourceStream()
90 * @param $res ResultWrapper The results object
91 * @param $db DatabaseBase object
93 public function sqlPrintResult( $res, $db ) {
94 if ( !$res ) {
95 // Do nothing
96 } elseif ( is_object( $res ) && $res->numRows() ) {
97 foreach ( $res as $row ) {
98 $this->output( print_r( $row, true ) );
100 } else {
101 $affected = $db->affectedRows();
102 $this->output( "Query OK, $affected row(s) affected\n" );
107 * @return int DB_TYPE constant
109 public function getDbType() {
110 return Maintenance::DB_ADMIN;
114 $maintClass = "MwSql";
115 require_once( RUN_MAINTENANCE_IF_MAIN );