Merge "add IGNORE INDEX option to mysql database handler"
[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->addDescription( 'Send SQL queries to a MediaWiki database. ' .
36 'Takes a file name containing SQL as argument or runs interactively.' );
37 $this->addOption( 'query',
38 'Run a single query instead of running interactively', false, true );
39 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
40 $this->addOption( 'wikidb',
41 'The database wiki ID to use if not the current one', false, true );
42 $this->addOption( 'replicadb',
43 'Replica DB server to use instead of the master DB (can be "any")', false, true );
44 }
45
46 public function execute() {
47 // We wan't to allow "" for the wikidb, meaning don't call select_db()
48 $wiki = $this->hasOption( 'wikidb' ) ? $this->getOption( 'wikidb' ) : false;
49 // Get the appropriate load balancer (for this wiki)
50 if ( $this->hasOption( 'cluster' ) ) {
51 $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ), $wiki );
52 } else {
53 $lb = wfGetLB( $wiki );
54 }
55 // Figure out which server to use
56 $replicaDB = $this->getOption( 'replicadb', $this->getOption( 'slave', '' ) );
57 if ( $replicaDB === 'any' ) {
58 $index = DB_REPLICA;
59 } elseif ( $replicaDB != '' ) {
60 $index = null;
61 $serverCount = $lb->getServerCount();
62 for ( $i = 0; $i < $serverCount; ++$i ) {
63 if ( $lb->getServerName( $i ) === $replicaDB ) {
64 $index = $i;
65 break;
66 }
67 }
68 if ( $index === null ) {
69 $this->error( "No replica DB server configured with the name '$server'.", 1 );
70 }
71 } else {
72 $index = DB_MASTER;
73 }
74 // Get a DB handle (with this wiki's DB selected) from the appropriate load balancer
75 $db = $lb->getConnection( $index, [], $wiki );
76 if ( $replicaDB != '' && $db->getLBInfo( 'master' ) !== null ) {
77 $this->error( "The server selected ({$db->getServer()}) is not a replica DB.", 1 );
78 }
79
80 if ( $this->hasArg( 0 ) ) {
81 $file = fopen( $this->getArg( 0 ), 'r' );
82 if ( !$file ) {
83 $this->error( "Unable to open input file", true );
84 }
85
86 $error = $db->sourceStream( $file, false, [ $this, 'sqlPrintResult' ] );
87 if ( $error !== true ) {
88 $this->error( $error, true );
89 } else {
90 exit( 0 );
91 }
92 }
93
94 if ( $this->hasOption( 'query' ) ) {
95 $query = $this->getOption( 'query' );
96 $this->sqlDoQuery( $db, $query, /* dieOnError */ true );
97 wfWaitForSlaves();
98 return;
99 }
100
101 $useReadline = function_exists( 'readline_add_history' )
102 && Maintenance::posix_isatty( 0 /*STDIN*/ );
103
104 if ( $useReadline ) {
105 global $IP;
106 $historyFile = isset( $_ENV['HOME'] ) ?
107 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
108 readline_read_history( $historyFile );
109 }
110
111 $wholeLine = '';
112 $newPrompt = '> ';
113 $prompt = $newPrompt;
114 $doDie = !Maintenance::posix_isatty( 0 );
115 while ( ( $line = Maintenance::readconsole( $prompt ) ) !== false ) {
116 if ( !$line ) {
117 # User simply pressed return key
118 continue;
119 }
120 $done = $db->streamStatementEnd( $wholeLine, $line );
121
122 $wholeLine .= $line;
123
124 if ( !$done ) {
125 $wholeLine .= ' ';
126 $prompt = ' -> ';
127 continue;
128 }
129 if ( $useReadline ) {
130 # Delimiter is eated by streamStatementEnd, we add it
131 # up in the history (bug 37020)
132 readline_add_history( $wholeLine . $db->getDelimiter() );
133 readline_write_history( $historyFile );
134 }
135 $this->sqlDoQuery( $db, $wholeLine, $doDie );
136 $prompt = $newPrompt;
137 $wholeLine = '';
138 }
139 wfWaitForSlaves();
140 }
141
142 protected function sqlDoQuery( $db, $line, $dieOnError ) {
143 try {
144 $res = $db->query( $line );
145 $this->sqlPrintResult( $res, $db );
146 } catch ( DBQueryError $e ) {
147 $this->error( $e, $dieOnError );
148 }
149 }
150
151 /**
152 * Print the results, callback for $db->sourceStream()
153 * @param ResultWrapper $res The results object
154 * @param DatabaseBase $db
155 */
156 public function sqlPrintResult( $res, $db ) {
157 if ( !$res ) {
158 // Do nothing
159 return;
160 } elseif ( is_object( $res ) && $res->numRows() ) {
161 foreach ( $res as $row ) {
162 $this->output( print_r( $row, true ) );
163 }
164 } else {
165 $affected = $db->affectedRows();
166 $this->output( "Query OK, $affected row(s) affected\n" );
167 }
168 }
169
170 /**
171 * @return int DB_TYPE constant
172 */
173 public function getDbType() {
174 return Maintenance::DB_ADMIN;
175 }
176 }
177
178 $maintClass = "MwSql";
179 require_once RUN_MAINTENANCE_IF_MAIN;