Give sql.php eval.php type scrollback
[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 * @ingroup Maintenance
22 */
23
24 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
25
26 class MwSql extends Maintenance {
27 public function __construct() {
28 parent::__construct();
29 $this->mDescription = "Send SQL queries to a MediaWiki database";
30 }
31
32 public function execute() {
33 if ( $this->hasArg() ) {
34 $fileName = $this->getArg();
35 $file = fopen( $fileName, 'r' );
36 } else {
37 $file = $this->getStdin();
38 }
39
40 if ( !$file ) {
41 $this->error( "Unable to open input file", true );
42 }
43
44 $useReadline = function_exists( 'readline_add_history' )
45 && Maintenance::posix_isatty( 0 /*STDIN*/ );
46
47 if ( $useReadline ) {
48 global $IP;
49 $historyFile = isset( $_ENV['HOME'] ) ?
50 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
51 readline_read_history( $historyFile );
52 }
53
54 $dbw = wfGetDB( DB_MASTER );
55 $wholeLine = '';
56 while ( ( $line = Maintenance::readconsole() ) !== false ) {
57 $done = $dbw->streamStatementEnd( $wholeLine, $line );
58
59 $wholeLine .= $line;
60
61 if ( !$done ) {
62 continue;
63 }
64 if ( $useReadline ) {
65 readline_add_history( $wholeLine );
66 readline_write_history( $historyFile );
67 }
68 try{
69 $res = $dbw->query( $wholeLine );
70 $this->sqlPrintResult( $res, $dbw );
71 $wholeLine = '';
72 } catch (DBQueryError $e) {
73 $this->error( $e, true );
74 }
75 }
76 }
77
78 /**
79 * Print the results, callback for $db->sourceStream()
80 * @param $res ResultWrapper The results object
81 * @param $db DatabaseBase object
82 */
83 public function sqlPrintResult( $res, $db ) {
84 if ( !$res ) {
85 // Do nothing
86 } elseif ( is_object( $res ) && $res->numRows() ) {
87 foreach ( $res as $row ) {
88 $this->output( print_r( $row, true ) );
89 }
90 } else {
91 $affected = $db->affectedRows();
92 $this->output( "Query OK, $affected row(s) affected\n" );
93 }
94 }
95
96 /**
97 * @return int DB_TYPE constant
98 */
99 public function getDbType() {
100 return Maintenance::DB_ADMIN;
101 }
102 }
103
104 $maintClass = "MwSql";
105 require_once( RUN_MAINTENANCE_IF_MAIN );