Merge "[FileBackend] Optimized concatenate() to use getLocalReferenceMulti()."
[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 readline_add_history( $wholeLine );
76 readline_write_history( $historyFile );
77 }
78 try{
79 $res = $dbw->query( $wholeLine );
80 $this->sqlPrintResult( $res, $dbw );
81 $wholeLine = '';
82 } catch (DBQueryError $e) {
83 $this->error( $e, true );
84 }
85 }
86 }
87
88 /**
89 * Print the results, callback for $db->sourceStream()
90 * @param $res ResultWrapper The results object
91 * @param $db DatabaseBase object
92 */
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 ) );
99 }
100 } else {
101 $affected = $db->affectedRows();
102 $this->output( "Query OK, $affected row(s) affected\n" );
103 }
104 }
105
106 /**
107 * @return int DB_TYPE constant
108 */
109 public function getDbType() {
110 return Maintenance::DB_ADMIN;
111 }
112 }
113
114 $maintClass = "MwSql";
115 require_once( RUN_MAINTENANCE_IF_MAIN );