2007-04-22 14:04:06 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Send SQL queries from the specified file to the database, performing
|
|
|
|
|
* variable replacement along the way.
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
*
|
2009-08-02 19:35:17 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-09-04 16:48:17 +00:00
|
|
|
* @file
|
2009-08-02 19:35:17 +00:00
|
|
|
* @ingroup Maintenance
|
2007-04-22 14:04:06 +00:00
|
|
|
*/
|
|
|
|
|
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2007-04-22 14:04:06 +00:00
|
|
|
|
2012-09-02 18:33:22 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script that sends SQL queries from the specified file to the database.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2009-08-02 19:35:17 +00:00
|
|
|
class MwSql extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2015-05-20 19:21:11 +00:00
|
|
|
$this->mDescription = "Send SQL queries to a MediaWiki database. " .
|
|
|
|
|
"Takes a file name containing SQL as argument or runs interactively.";
|
2013-02-21 20:48:35 +00:00
|
|
|
$this->addOption( 'cluster', 'Use an external cluster by name', false, true );
|
2013-12-10 23:03:41 +00:00
|
|
|
$this->addOption( 'wikidb', 'The database wiki ID to use if not the current one', false, true );
|
2013-05-21 19:50:11 +00:00
|
|
|
$this->addOption( 'slave', 'Use a slave server (either "any" or by name)', false, true );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2007-04-22 14:04:06 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
public function execute() {
|
2014-11-06 19:38:22 +00:00
|
|
|
// We wan't to allow "" for the wikidb, meaning don't call select_db()
|
|
|
|
|
$wiki = $this->hasOption( 'wikidb' ) ? $this->getOption( 'wikidb' ) : false;
|
2013-05-21 19:50:11 +00:00
|
|
|
// Get the appropriate load balancer (for this wiki)
|
2013-02-21 20:48:35 +00:00
|
|
|
if ( $this->hasOption( 'cluster' ) ) {
|
2013-12-10 23:03:41 +00:00
|
|
|
$lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ), $wiki );
|
2013-02-21 20:48:35 +00:00
|
|
|
} else {
|
2013-12-10 23:03:41 +00:00
|
|
|
$lb = wfGetLB( $wiki );
|
2013-02-21 20:48:35 +00:00
|
|
|
}
|
2013-05-21 19:50:11 +00:00
|
|
|
// Figure out which server to use
|
|
|
|
|
if ( $this->hasOption( 'slave' ) ) {
|
|
|
|
|
$server = $this->getOption( 'slave' );
|
|
|
|
|
if ( $server === 'any' ) {
|
|
|
|
|
$index = DB_SLAVE;
|
|
|
|
|
} else {
|
|
|
|
|
$index = null;
|
2014-04-22 20:55:25 +00:00
|
|
|
$serverCount = $lb->getServerCount();
|
|
|
|
|
for ( $i = 0; $i < $serverCount; ++$i ) {
|
2013-05-21 19:50:11 +00:00
|
|
|
if ( $lb->getServerName( $i ) === $server ) {
|
|
|
|
|
$index = $i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $index === null ) {
|
|
|
|
|
$this->error( "No slave server configured with the name '$server'.", 1 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$index = DB_MASTER;
|
|
|
|
|
}
|
|
|
|
|
// Get a DB handle (with this wiki's DB selected) from the appropriate load balancer
|
2013-12-10 23:03:41 +00:00
|
|
|
$db = $lb->getConnection( $index, array(), $wiki );
|
|
|
|
|
if ( $this->hasOption( 'slave' ) && $db->getLBInfo( 'master' ) !== null ) {
|
|
|
|
|
$this->error( "The server selected ({$db->getServer()}) is not a slave.", 1 );
|
2013-05-21 19:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
2013-02-21 20:48:35 +00:00
|
|
|
if ( $this->hasArg( 0 ) ) {
|
|
|
|
|
$file = fopen( $this->getArg( 0 ), 'r' );
|
2012-01-17 13:29:42 +00:00
|
|
|
if ( !$file ) {
|
|
|
|
|
$this->error( "Unable to open input file", true );
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2013-12-10 23:03:41 +00:00
|
|
|
$error = $db->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
|
2012-01-17 13:29:42 +00:00
|
|
|
if ( $error !== true ) {
|
|
|
|
|
$this->error( $error, true );
|
|
|
|
|
} else {
|
|
|
|
|
exit( 0 );
|
|
|
|
|
}
|
2012-01-12 22:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$useReadline = function_exists( 'readline_add_history' )
|
2014-04-23 18:09:26 +00:00
|
|
|
&& Maintenance::posix_isatty( 0 /*STDIN*/ );
|
2012-01-12 22:57:51 +00:00
|
|
|
|
|
|
|
|
if ( $useReadline ) {
|
|
|
|
|
global $IP;
|
|
|
|
|
$historyFile = isset( $_ENV['HOME'] ) ?
|
2014-04-23 18:09:26 +00:00
|
|
|
"{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
|
2012-01-12 22:57:51 +00:00
|
|
|
readline_read_history( $historyFile );
|
|
|
|
|
}
|
2007-04-22 14:04:06 +00:00
|
|
|
|
2012-01-12 22:57:51 +00:00
|
|
|
$wholeLine = '';
|
2012-10-25 11:03:59 +00:00
|
|
|
$newPrompt = '> ';
|
2013-04-18 18:48:44 +00:00
|
|
|
$prompt = $newPrompt;
|
2012-10-25 11:03:59 +00:00
|
|
|
while ( ( $line = Maintenance::readconsole( $prompt ) ) !== false ) {
|
2013-04-18 18:48:44 +00:00
|
|
|
if ( !$line ) {
|
2012-10-25 11:03:59 +00:00
|
|
|
# User simply pressed return key
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2013-12-10 23:03:41 +00:00
|
|
|
$done = $db->streamStatementEnd( $wholeLine, $line );
|
2012-01-12 22:57:51 +00:00
|
|
|
|
|
|
|
|
$wholeLine .= $line;
|
|
|
|
|
|
|
|
|
|
if ( !$done ) {
|
2012-10-25 11:03:59 +00:00
|
|
|
$wholeLine .= ' ';
|
|
|
|
|
$prompt = ' -> ';
|
2012-01-12 22:57:51 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ( $useReadline ) {
|
2012-10-25 11:00:03 +00:00
|
|
|
# Delimiter is eated by streamStatementEnd, we add it
|
|
|
|
|
# up in the history (bug 37020)
|
2013-12-10 23:03:41 +00:00
|
|
|
readline_add_history( $wholeLine . $db->getDelimiter() );
|
2012-01-12 22:57:51 +00:00
|
|
|
readline_write_history( $historyFile );
|
|
|
|
|
}
|
2013-04-27 11:23:52 +00:00
|
|
|
try {
|
2013-12-10 23:03:41 +00:00
|
|
|
$res = $db->query( $wholeLine );
|
|
|
|
|
$this->sqlPrintResult( $res, $db );
|
2013-04-18 18:48:44 +00:00
|
|
|
$prompt = $newPrompt;
|
2012-01-12 22:57:51 +00:00
|
|
|
$wholeLine = '';
|
2013-04-18 18:48:44 +00:00
|
|
|
} catch ( DBQueryError $e ) {
|
2014-04-23 18:09:26 +00:00
|
|
|
$doDie = !Maintenance::posix_isatty( 0 );
|
2012-10-25 11:14:24 +00:00
|
|
|
$this->error( $e, $doDie );
|
2012-01-12 22:57:51 +00:00
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2013-02-25 23:57:31 +00:00
|
|
|
wfWaitForSlaves();
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2007-04-22 14:04:06 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
/**
|
|
|
|
|
* Print the results, callback for $db->sourceStream()
|
2014-04-17 20:48:32 +00:00
|
|
|
* @param ResultWrapper $res The results object
|
2014-08-09 19:26:14 +00:00
|
|
|
* @param DatabaseBase $db
|
2009-08-02 19:35:17 +00:00
|
|
|
*/
|
|
|
|
|
public function sqlPrintResult( $res, $db ) {
|
|
|
|
|
if ( !$res ) {
|
|
|
|
|
// Do nothing
|
2013-01-30 15:05:17 +00:00
|
|
|
return;
|
2009-08-02 19:35:17 +00:00
|
|
|
} elseif ( is_object( $res ) && $res->numRows() ) {
|
2009-08-17 21:15:31 +00:00
|
|
|
foreach ( $res as $row ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( print_r( $row, true ) );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$affected = $db->affectedRows();
|
|
|
|
|
$this->output( "Query OK, $affected row(s) affected\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2012-01-12 22:57:51 +00:00
|
|
|
/**
|
|
|
|
|
* @return int DB_TYPE constant
|
|
|
|
|
*/
|
2010-03-10 12:59:44 +00:00
|
|
|
public function getDbType() {
|
2009-09-22 16:41:04 +00:00
|
|
|
return Maintenance::DB_ADMIN;
|
|
|
|
|
}
|
2007-04-22 14:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
$maintClass = "MwSql";
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|