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