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
|
|
|
|
2018-02-25 09:05:07 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2017-02-19 05:03:13 +00:00
|
|
|
use Wikimedia\Rdbms\ResultWrapper;
|
2017-02-10 18:09:05 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2017-02-24 16:17:16 +00:00
|
|
|
use Wikimedia\Rdbms\DBQueryError;
|
2017-02-19 05:03:13 +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();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( 'Send SQL queries to a MediaWiki database. ' .
|
|
|
|
|
'Takes a file name containing SQL as argument or runs interactively.' );
|
2016-09-05 20:14:41 +00:00
|
|
|
$this->addOption( 'query',
|
|
|
|
|
'Run a single query instead of running interactively', false, true );
|
2018-02-16 21:21:38 +00:00
|
|
|
$this->addOption( 'json', 'Output the results as JSON instead of PHP objects' );
|
2013-02-21 20:48:35 +00:00
|
|
|
$this->addOption( 'cluster', 'Use an external cluster by name', false, true );
|
2016-09-05 20:14:41 +00:00
|
|
|
$this->addOption( 'wikidb',
|
|
|
|
|
'The database wiki ID to use if not the current one', false, true );
|
|
|
|
|
$this->addOption( 'replicadb',
|
|
|
|
|
'Replica DB server to use instead of the master DB (can be "any")', 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() {
|
2016-09-19 21:17:19 +00:00
|
|
|
global $IP;
|
|
|
|
|
|
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)
|
2018-02-25 09:05:07 +00:00
|
|
|
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
|
2013-02-21 20:48:35 +00:00
|
|
|
if ( $this->hasOption( 'cluster' ) ) {
|
2018-02-25 09:05:07 +00:00
|
|
|
$lb = $lbFactory->getExternalLB( $this->getOption( 'cluster' ) );
|
2013-02-21 20:48:35 +00:00
|
|
|
} else {
|
2018-02-25 09:05:07 +00:00
|
|
|
$lb = $lbFactory->getMainLB( $wiki );
|
2013-02-21 20:48:35 +00:00
|
|
|
}
|
2013-05-21 19:50:11 +00:00
|
|
|
// Figure out which server to use
|
2016-09-05 20:14:41 +00:00
|
|
|
$replicaDB = $this->getOption( 'replicadb', $this->getOption( 'slave', '' ) );
|
|
|
|
|
if ( $replicaDB === 'any' ) {
|
2016-09-05 19:55:19 +00:00
|
|
|
$index = DB_REPLICA;
|
2016-09-05 20:14:41 +00:00
|
|
|
} elseif ( $replicaDB != '' ) {
|
|
|
|
|
$index = null;
|
|
|
|
|
$serverCount = $lb->getServerCount();
|
|
|
|
|
for ( $i = 0; $i < $serverCount; ++$i ) {
|
|
|
|
|
if ( $lb->getServerName( $i ) === $replicaDB ) {
|
|
|
|
|
$index = $i;
|
|
|
|
|
break;
|
2013-05-21 19:50:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-09-05 20:14:41 +00:00
|
|
|
if ( $index === null ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "No replica DB server configured with the name '$replicaDB'." );
|
2016-09-05 20:14:41 +00:00
|
|
|
}
|
2013-05-21 19:50:11 +00:00
|
|
|
} else {
|
|
|
|
|
$index = DB_MASTER;
|
|
|
|
|
}
|
2016-09-19 21:17:19 +00:00
|
|
|
|
2017-03-30 20:46:06 +00:00
|
|
|
/** @var IDatabase $db DB handle for the appropriate cluster/wiki */
|
2016-02-17 09:09:32 +00:00
|
|
|
$db = $lb->getConnection( $index, [], $wiki );
|
2016-09-06 01:30:36 +00:00
|
|
|
if ( $replicaDB != '' && $db->getLBInfo( 'master' ) !== null ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "The server selected ({$db->getServer()}) is not a replica DB." );
|
2013-05-21 19:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-21 20:39:00 +00:00
|
|
|
if ( $index === DB_MASTER ) {
|
|
|
|
|
$updater = DatabaseUpdater::newForDB( $db, true, $this );
|
|
|
|
|
$db->setSchemaVars( $updater->getSchemaVars() );
|
|
|
|
|
}
|
|
|
|
|
|
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 ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Unable to open input file" );
|
2012-01-17 13:29:42 +00:00
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2016-10-03 17:48:58 +00:00
|
|
|
$error = $db->sourceStream( $file, null, [ $this, 'sqlPrintResult' ] );
|
2012-01-17 13:29:42 +00:00
|
|
|
if ( $error !== true ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( $error );
|
2012-01-17 13:29:42 +00:00
|
|
|
} else {
|
|
|
|
|
exit( 0 );
|
|
|
|
|
}
|
2012-01-12 22:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
2015-07-18 20:21:00 +00:00
|
|
|
if ( $this->hasOption( 'query' ) ) {
|
|
|
|
|
$query = $this->getOption( 'query' );
|
|
|
|
|
$this->sqlDoQuery( $db, $query, /* dieOnError */ true );
|
|
|
|
|
wfWaitForSlaves();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-19 21:17:19 +00:00
|
|
|
if (
|
|
|
|
|
function_exists( 'readline_add_history' ) &&
|
|
|
|
|
Maintenance::posix_isatty( 0 /*STDIN*/ )
|
|
|
|
|
) {
|
2012-01-12 22:57:51 +00:00
|
|
|
$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 );
|
2016-09-19 21:17:19 +00:00
|
|
|
} else {
|
|
|
|
|
$historyFile = null;
|
2012-01-12 22:57:51 +00:00
|
|
|
}
|
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;
|
2015-07-18 20:21:00 +00:00
|
|
|
$doDie = !Maintenance::posix_isatty( 0 );
|
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;
|
|
|
|
|
}
|
2016-09-19 21:17:19 +00:00
|
|
|
if ( $historyFile ) {
|
2012-10-25 11:00:03 +00:00
|
|
|
# Delimiter is eated by streamStatementEnd, we add it
|
2017-02-20 22:48:21 +00:00
|
|
|
# up in the history (T39020)
|
2016-09-19 21:17:19 +00:00
|
|
|
readline_add_history( $wholeLine . ';' );
|
2012-01-12 22:57:51 +00:00
|
|
|
readline_write_history( $historyFile );
|
|
|
|
|
}
|
2015-07-18 20:21:00 +00:00
|
|
|
$this->sqlDoQuery( $db, $wholeLine, $doDie );
|
2015-07-10 17:38:19 +00:00
|
|
|
$prompt = $newPrompt;
|
|
|
|
|
$wholeLine = '';
|
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
|
|
|
|
2016-09-19 21:17:19 +00:00
|
|
|
protected function sqlDoQuery( IDatabase $db, $line, $dieOnError ) {
|
2015-07-18 20:21:00 +00:00
|
|
|
try {
|
|
|
|
|
$res = $db->query( $line );
|
|
|
|
|
$this->sqlPrintResult( $res, $db );
|
|
|
|
|
} catch ( DBQueryError $e ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
if ( $dieOnError ) {
|
|
|
|
|
$this->fatalError( $e );
|
|
|
|
|
} else {
|
|
|
|
|
$this->error( $e );
|
|
|
|
|
}
|
2015-07-18 20:21:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
/**
|
|
|
|
|
* Print the results, callback for $db->sourceStream()
|
2017-12-28 15:06:10 +00:00
|
|
|
* @param ResultWrapper|bool $res
|
2016-09-19 21:17:19 +00:00
|
|
|
* @param IDatabase $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() ) {
|
2018-02-16 21:21:38 +00:00
|
|
|
$out = '';
|
2009-08-17 21:15:31 +00:00
|
|
|
foreach ( $res as $row ) {
|
2018-02-16 21:21:38 +00:00
|
|
|
$out .= print_r( $row, true );
|
|
|
|
|
$rows[] = $row;
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2018-02-16 21:21:38 +00:00
|
|
|
if ( $this->hasOption( 'json' ) ) {
|
|
|
|
|
$out = json_encode( $rows, JSON_PRETTY_PRINT );
|
|
|
|
|
}
|
|
|
|
|
$this->output( $out . "\n" );
|
2009-08-02 19:35:17 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = MwSql::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|