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