Merge "Make a single colon an invalid title in php and js"
[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( 'slave', 'Use a slave server (either "any" or by name)', false, true );
38 }
39
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();
46 }
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;
58 }
59 }
60 if ( $index === null ) {
61 $this->error( "No slave server configured with the name '$server'.", 1 );
62 }
63 }
64 } else {
65 $index = DB_MASTER;
66 }
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 );
71 }
72
73 if ( $this->hasArg( 0 ) ) {
74 $file = fopen( $this->getArg( 0 ), 'r' );
75 if ( !$file ) {
76 $this->error( "Unable to open input file", true );
77 }
78
79 $error = $dbw->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
80 if ( $error !== true ) {
81 $this->error( $error, true );
82 } else {
83 exit( 0 );
84 }
85 }
86
87 $useReadline = function_exists( 'readline_add_history' )
88 && Maintenance::posix_isatty( 0 /*STDIN*/ );
89
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 );
95 }
96
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;
104 }
105 $done = $dbw->streamStatementEnd( $wholeLine, $line );
106
107 $wholeLine .= $line;
108
109 if ( !$done ) {
110 $wholeLine .= ' ';
111 $prompt = ' -> ';
112 continue;
113 }
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 );
119 }
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 );
128 }
129 }
130 wfWaitForSlaves();
131 }
132
133 /**
134 * Print the results, callback for $db->sourceStream()
135 * @param $res ResultWrapper The results object
136 * @param $db DatabaseBase object
137 */
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 ) );
145 }
146 } else {
147 $affected = $db->affectedRows();
148 $this->output( "Query OK, $affected row(s) affected\n" );
149 }
150 }
151
152 /**
153 * @return int DB_TYPE constant
154 */
155 public function getDbType() {
156 return Maintenance::DB_ADMIN;
157 }
158 }
159
160 $maintClass = "MwSql";
161 require_once RUN_MAINTENANCE_IF_MAIN;