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