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
|
|
|
*/
|
|
|
|
|
|
2012-08-27 19:03:15 +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();
|
|
|
|
|
$this->mDescription = "Send SQL queries to a MediaWiki database";
|
|
|
|
|
}
|
2007-04-22 14:04:06 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
public function execute() {
|
2012-01-17 13:29:42 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2009-08-02 19:35:17 +00:00
|
|
|
if ( $this->hasArg() ) {
|
|
|
|
|
$fileName = $this->getArg();
|
|
|
|
|
$file = fopen( $fileName, '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
|
|
|
|
2012-01-17 13:29:42 +00:00
|
|
|
$error = $dbw->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
|
|
|
|
|
if ( $error !== true ) {
|
|
|
|
|
$this->error( $error, true );
|
|
|
|
|
} else {
|
|
|
|
|
exit( 0 );
|
|
|
|
|
}
|
2012-01-12 22:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$useReadline = function_exists( 'readline_add_history' )
|
|
|
|
|
&& Maintenance::posix_isatty( 0 /*STDIN*/ );
|
|
|
|
|
|
|
|
|
|
if ( $useReadline ) {
|
|
|
|
|
global $IP;
|
|
|
|
|
$historyFile = isset( $_ENV['HOME'] ) ?
|
|
|
|
|
"{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
|
|
|
|
|
readline_read_history( $historyFile );
|
|
|
|
|
}
|
2007-04-22 14:04:06 +00:00
|
|
|
|
2012-01-12 22:57:51 +00:00
|
|
|
$wholeLine = '';
|
|
|
|
|
while ( ( $line = Maintenance::readconsole() ) !== false ) {
|
|
|
|
|
$done = $dbw->streamStatementEnd( $wholeLine, $line );
|
|
|
|
|
|
|
|
|
|
$wholeLine .= $line;
|
|
|
|
|
|
|
|
|
|
if ( !$done ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ( $useReadline ) {
|
|
|
|
|
readline_add_history( $wholeLine );
|
|
|
|
|
readline_write_history( $historyFile );
|
|
|
|
|
}
|
|
|
|
|
try{
|
|
|
|
|
$res = $dbw->query( $wholeLine );
|
|
|
|
|
$this->sqlPrintResult( $res, $dbw );
|
|
|
|
|
$wholeLine = '';
|
|
|
|
|
} catch (DBQueryError $e) {
|
|
|
|
|
$this->error( $e, 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
|
|
|
/**
|
|
|
|
|
* Print the results, callback for $db->sourceStream()
|
2012-01-12 22:57:51 +00:00
|
|
|
* @param $res ResultWrapper The results object
|
|
|
|
|
* @param $db DatabaseBase object
|
2009-08-02 19:35:17 +00:00
|
|
|
*/
|
|
|
|
|
public function sqlPrintResult( $res, $db ) {
|
|
|
|
|
if ( !$res ) {
|
|
|
|
|
// Do nothing
|
|
|
|
|
} 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";
|
2011-01-13 22:58:55 +00:00
|
|
|
require_once( RUN_MAINTENANCE_IF_MAIN );
|