Merge "(bug 37020) sql.php with readline eats semicolon"
[lhc/web/wiklou.git] / maintenance / sql.php
1 <?php
2 /**
3 * Send SQL queries from the specified file to the database, performing
4 * variable replacement along the way.
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25 require_once( __DIR__ . '/Maintenance.php' );
26
27 /**
28 * Maintenance script that sends SQL queries from the specified file to the database.
29 *
30 * @ingroup Maintenance
31 */
32 class MwSql extends Maintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->mDescription = "Send SQL queries to a MediaWiki database";
36 }
37
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 );
45 }
46
47 $error = $dbw->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
48 if ( $error !== true ) {
49 $this->error( $error, true );
50 } else {
51 exit( 0 );
52 }
53 }
54
55 $useReadline = function_exists( 'readline_add_history' )
56 && Maintenance::posix_isatty( 0 /*STDIN*/ );
57
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 );
63 }
64
65 $wholeLine = '';
66 while ( ( $line = Maintenance::readconsole() ) !== false ) {
67 $done = $dbw->streamStatementEnd( $wholeLine, $line );
68
69 $wholeLine .= $line;
70
71 if ( !$done ) {
72 continue;
73 }
74 if ( $useReadline ) {
75 # Delimiter is eated by streamStatementEnd, we add it
76 # up in the history (bug 37020)
77 readline_add_history( $wholeLine . $dbw->getDelimiter() );
78 readline_write_history( $historyFile );
79 }
80 try{
81 $res = $dbw->query( $wholeLine );
82 $this->sqlPrintResult( $res, $dbw );
83 $wholeLine = '';
84 } catch (DBQueryError $e) {
85 $doDie = ! Maintenance::posix_isatty( 0 );
86 $this->error( $e, $doDie );
87 }
88 }
89 }
90
91 /**
92 * Print the results, callback for $db->sourceStream()
93 * @param $res ResultWrapper The results object
94 * @param $db DatabaseBase object
95 */
96 public function sqlPrintResult( $res, $db ) {
97 if ( !$res ) {
98 // Do nothing
99 } elseif ( is_object( $res ) && $res->numRows() ) {
100 foreach ( $res as $row ) {
101 $this->output( print_r( $row, true ) );
102 }
103 } else {
104 $affected = $db->affectedRows();
105 $this->output( "Query OK, $affected row(s) affected\n" );
106 }
107 }
108
109 /**
110 * @return int DB_TYPE constant
111 */
112 public function getDbType() {
113 return Maintenance::DB_ADMIN;
114 }
115 }
116
117 $maintClass = "MwSql";
118 require_once( RUN_MAINTENANCE_IF_MAIN );