Localisation updates from http://translatewiki.net.
[mediawiki.git] / maintenance / sql.php
bloba628b0bcd4800c1c01429cac5883053d98155237
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( 'slave', 'Use a slave server (either "any" or by name)', false, true );
40 public function execute() {
41 // Get the appropriate load balancer (for this wiki)
42 if ( $this->hasOption( 'cluster' ) ) {
43 $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ) );
44 } else {
45 $lb = wfGetLB();
47 // Figure out which server to use
48 if ( $this->hasOption( 'slave' ) ) {
49 $server = $this->getOption( 'slave' );
50 if ( $server === 'any' ) {
51 $index = DB_SLAVE;
52 } else {
53 $index = null;
54 for ( $i = 0; $i < $lb->getServerCount(); ++$i ) {
55 if ( $lb->getServerName( $i ) === $server ) {
56 $index = $i;
57 break;
60 if ( $index === null ) {
61 $this->error( "No slave server configured with the name '$server'.", 1 );
64 } else {
65 $index = DB_MASTER;
67 // Get a DB handle (with this wiki's DB selected) from the appropriate load balancer
68 $dbw = $lb->getConnection( $index );
69 if ( $this->hasOption( 'slave' ) && $dbw->getLBInfo( 'master' ) !== null ) {
70 $this->error( "The server selected ({$dbw->getServer()}) is not a slave.", 1 );
73 if ( $this->hasArg( 0 ) ) {
74 $file = fopen( $this->getArg( 0 ), 'r' );
75 if ( !$file ) {
76 $this->error( "Unable to open input file", true );
79 $error = $dbw->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
80 if ( $error !== true ) {
81 $this->error( $error, true );
82 } else {
83 exit( 0 );
87 $useReadline = function_exists( 'readline_add_history' )
88 && Maintenance::posix_isatty( 0 /*STDIN*/ );
90 if ( $useReadline ) {
91 global $IP;
92 $historyFile = isset( $_ENV['HOME'] ) ?
93 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
94 readline_read_history( $historyFile );
97 $wholeLine = '';
98 $newPrompt = '> ';
99 $prompt = $newPrompt;
100 while ( ( $line = Maintenance::readconsole( $prompt ) ) !== false ) {
101 if ( !$line ) {
102 # User simply pressed return key
103 continue;
105 $done = $dbw->streamStatementEnd( $wholeLine, $line );
107 $wholeLine .= $line;
109 if ( !$done ) {
110 $wholeLine .= ' ';
111 $prompt = ' -> ';
112 continue;
114 if ( $useReadline ) {
115 # Delimiter is eated by streamStatementEnd, we add it
116 # up in the history (bug 37020)
117 readline_add_history( $wholeLine . $dbw->getDelimiter() );
118 readline_write_history( $historyFile );
120 try {
121 $res = $dbw->query( $wholeLine );
122 $this->sqlPrintResult( $res, $dbw );
123 $prompt = $newPrompt;
124 $wholeLine = '';
125 } catch ( DBQueryError $e ) {
126 $doDie = ! Maintenance::posix_isatty( 0 );
127 $this->error( $e, $doDie );
130 wfWaitForSlaves();
134 * Print the results, callback for $db->sourceStream()
135 * @param $res ResultWrapper The results object
136 * @param $db DatabaseBase object
138 public function sqlPrintResult( $res, $db ) {
139 if ( !$res ) {
140 // Do nothing
141 return;
142 } elseif ( is_object( $res ) && $res->numRows() ) {
143 foreach ( $res as $row ) {
144 $this->output( print_r( $row, true ) );
146 } else {
147 $affected = $db->affectedRows();
148 $this->output( "Query OK, $affected row(s) affected\n" );
153 * @return int DB_TYPE constant
155 public function getDbType() {
156 return Maintenance::DB_ADMIN;
160 $maintClass = "MwSql";
161 require_once RUN_MAINTENANCE_IF_MAIN;