Merge "Minor CSS cleanup for Vector and Monobook skins"
[mediawiki.git] / maintenance / sql.php
blobb10a37933b90dbad10d1950cf91806076ceb0a78
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 );
39 public function execute() {
40 // Get a DB handle (with this wiki's DB select) from the appropriate load balancer
41 if ( $this->hasOption( 'cluster' ) ) {
42 $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ) );
43 $dbw = $lb->getConnection( DB_MASTER ); // master for external LB
44 } else {
45 $dbw = wfGetDB( DB_MASTER ); // master for primary LB for this wiki
47 if ( $this->hasArg( 0 ) ) {
48 $file = fopen( $this->getArg( 0 ), 'r' );
49 if ( !$file ) {
50 $this->error( "Unable to open input file", true );
53 $error = $dbw->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
54 if ( $error !== true ) {
55 $this->error( $error, true );
56 } else {
57 exit( 0 );
61 $useReadline = function_exists( 'readline_add_history' )
62 && Maintenance::posix_isatty( 0 /*STDIN*/ );
64 if ( $useReadline ) {
65 global $IP;
66 $historyFile = isset( $_ENV['HOME'] ) ?
67 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
68 readline_read_history( $historyFile );
71 $wholeLine = '';
72 $newPrompt = '> ';
73 $prompt = $newPrompt;
74 while ( ( $line = Maintenance::readconsole( $prompt ) ) !== false ) {
75 if ( !$line ) {
76 # User simply pressed return key
77 continue;
79 $done = $dbw->streamStatementEnd( $wholeLine, $line );
81 $wholeLine .= $line;
83 if ( !$done ) {
84 $wholeLine .= ' ';
85 $prompt = ' -> ';
86 continue;
88 if ( $useReadline ) {
89 # Delimiter is eated by streamStatementEnd, we add it
90 # up in the history (bug 37020)
91 readline_add_history( $wholeLine . $dbw->getDelimiter() );
92 readline_write_history( $historyFile );
94 try{
95 $res = $dbw->query( $wholeLine );
96 $this->sqlPrintResult( $res, $dbw );
97 $prompt = $newPrompt;
98 $wholeLine = '';
99 } catch ( DBQueryError $e ) {
100 $doDie = ! Maintenance::posix_isatty( 0 );
101 $this->error( $e, $doDie );
104 wfWaitForSlaves();
108 * Print the results, callback for $db->sourceStream()
109 * @param $res ResultWrapper The results object
110 * @param $db DatabaseBase object
112 public function sqlPrintResult( $res, $db ) {
113 if ( !$res ) {
114 // Do nothing
115 return;
116 } elseif ( is_object( $res ) && $res->numRows() ) {
117 foreach ( $res as $row ) {
118 $this->output( print_r( $row, true ) );
120 } else {
121 $affected = $db->affectedRows();
122 $this->output( "Query OK, $affected row(s) affected\n" );
127 * @return int DB_TYPE constant
129 public function getDbType() {
130 return Maintenance::DB_ADMIN;
134 $maintClass = "MwSql";
135 require_once( RUN_MAINTENANCE_IF_MAIN );