Follow-up I0b781c11 (2a55449): use User::getAutomaticGroups().
[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 $dbw = wfGetDB( DB_MASTER );
34 if ( $this->hasArg() ) {
35 $fileName = $this->getArg();
36 $file = fopen( $fileName, 'r' );
37 if ( !$file ) {
38 $this->error( "Unable to open input file", true );
39 }
40
41 $error = $dbw->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
42 if ( $error !== true ) {
43 $this->error( $error, true );
44 } else {
45 exit( 0 );
46 }
47 }
48
49 $useReadline = function_exists( 'readline_add_history' )
50 && Maintenance::posix_isatty( 0 /*STDIN*/ );
51
52 if ( $useReadline ) {
53 global $IP;
54 $historyFile = isset( $_ENV['HOME'] ) ?
55 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
56 readline_read_history( $historyFile );
57 }
58
59 $wholeLine = '';
60 while ( ( $line = Maintenance::readconsole() ) !== false ) {
61 $done = $dbw->streamStatementEnd( $wholeLine, $line );
62
63 $wholeLine .= $line;
64
65 if ( !$done ) {
66 continue;
67 }
68 if ( $useReadline ) {
69 readline_add_history( $wholeLine );
70 readline_write_history( $historyFile );
71 }
72 try{
73 $res = $dbw->query( $wholeLine );
74 $this->sqlPrintResult( $res, $dbw );
75 $wholeLine = '';
76 } catch (DBQueryError $e) {
77 $this->error( $e, true );
78 }
79 }
80 }
81
82 /**
83 * Print the results, callback for $db->sourceStream()
84 * @param $res ResultWrapper The results object
85 * @param $db DatabaseBase object
86 */
87 public function sqlPrintResult( $res, $db ) {
88 if ( !$res ) {
89 // Do nothing
90 } elseif ( is_object( $res ) && $res->numRows() ) {
91 foreach ( $res as $row ) {
92 $this->output( print_r( $row, true ) );
93 }
94 } else {
95 $affected = $db->affectedRows();
96 $this->output( "Query OK, $affected row(s) affected\n" );
97 }
98 }
99
100 /**
101 * @return int DB_TYPE constant
102 */
103 public function getDbType() {
104 return Maintenance::DB_ADMIN;
105 }
106 }
107
108 $maintClass = "MwSql";
109 require_once( RUN_MAINTENANCE_IF_MAIN );