Implement extension registration from an extension.json file
[mediawiki.git] / maintenance / sql.php
blob886e3f108d0bffc78f159d9461450b01ab8b58ab
1 <?php
2 /**
3 * Send SQL queries from the specified file to the database, performing
4 * variable replacement along the way.
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.
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.
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
21 * @file
22 * @ingroup Maintenance
25 require_once __DIR__ . '/Maintenance.php';
27 /**
28 * Maintenance script that sends SQL queries from the specified file to the database.
30 * @ingroup Maintenance
32 class MwSql extends Maintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->mDescription = "Send SQL queries to a MediaWiki database";
36 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
37 $this->addOption( 'wikidb', 'The database wiki ID to use if not the current one', false, true );
38 $this->addOption( 'slave', 'Use a slave server (either "any" or by name)', false, true );
41 public function execute() {
42 // We wan't to allow "" for the wikidb, meaning don't call select_db()
43 $wiki = $this->hasOption( 'wikidb' ) ? $this->getOption( 'wikidb' ) : false;
44 // Get the appropriate load balancer (for this wiki)
45 if ( $this->hasOption( 'cluster' ) ) {
46 $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ), $wiki );
47 } else {
48 $lb = wfGetLB( $wiki );
50 // Figure out which server to use
51 if ( $this->hasOption( 'slave' ) ) {
52 $server = $this->getOption( 'slave' );
53 if ( $server === 'any' ) {
54 $index = DB_SLAVE;
55 } else {
56 $index = null;
57 $serverCount = $lb->getServerCount();
58 for ( $i = 0; $i < $serverCount; ++$i ) {
59 if ( $lb->getServerName( $i ) === $server ) {
60 $index = $i;
61 break;
64 if ( $index === null ) {
65 $this->error( "No slave server configured with the name '$server'.", 1 );
68 } else {
69 $index = DB_MASTER;
71 // Get a DB handle (with this wiki's DB selected) from the appropriate load balancer
72 $db = $lb->getConnection( $index, array(), $wiki );
73 if ( $this->hasOption( 'slave' ) && $db->getLBInfo( 'master' ) !== null ) {
74 $this->error( "The server selected ({$db->getServer()}) is not a slave.", 1 );
77 if ( $this->hasArg( 0 ) ) {
78 $file = fopen( $this->getArg( 0 ), 'r' );
79 if ( !$file ) {
80 $this->error( "Unable to open input file", true );
83 $error = $db->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
84 if ( $error !== true ) {
85 $this->error( $error, true );
86 } else {
87 exit( 0 );
91 $useReadline = function_exists( 'readline_add_history' )
92 && Maintenance::posix_isatty( 0 /*STDIN*/ );
94 if ( $useReadline ) {
95 global $IP;
96 $historyFile = isset( $_ENV['HOME'] ) ?
97 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
98 readline_read_history( $historyFile );
101 $wholeLine = '';
102 $newPrompt = '> ';
103 $prompt = $newPrompt;
104 while ( ( $line = Maintenance::readconsole( $prompt ) ) !== false ) {
105 if ( !$line ) {
106 # User simply pressed return key
107 continue;
109 $done = $db->streamStatementEnd( $wholeLine, $line );
111 $wholeLine .= $line;
113 if ( !$done ) {
114 $wholeLine .= ' ';
115 $prompt = ' -> ';
116 continue;
118 if ( $useReadline ) {
119 # Delimiter is eated by streamStatementEnd, we add it
120 # up in the history (bug 37020)
121 readline_add_history( $wholeLine . $db->getDelimiter() );
122 readline_write_history( $historyFile );
124 try {
125 $res = $db->query( $wholeLine );
126 $this->sqlPrintResult( $res, $db );
127 $prompt = $newPrompt;
128 $wholeLine = '';
129 } catch ( DBQueryError $e ) {
130 $doDie = !Maintenance::posix_isatty( 0 );
131 $this->error( $e, $doDie );
134 wfWaitForSlaves();
138 * Print the results, callback for $db->sourceStream()
139 * @param ResultWrapper $res The results object
140 * @param DatabaseBase $db
142 public function sqlPrintResult( $res, $db ) {
143 if ( !$res ) {
144 // Do nothing
145 return;
146 } elseif ( is_object( $res ) && $res->numRows() ) {
147 foreach ( $res as $row ) {
148 $this->output( print_r( $row, true ) );
150 } else {
151 $affected = $db->affectedRows();
152 $this->output( "Query OK, $affected row(s) affected\n" );
157 * @return int DB_TYPE constant
159 public function getDbType() {
160 return Maintenance::DB_ADMIN;
164 $maintClass = "MwSql";
165 require_once RUN_MAINTENANCE_IF_MAIN;