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