wiki.techinc.nl/maintenance/sql.php
Brion Vibber dff6e3ae0b Don't die horribly from sql.php when making INSERT/DELETE/UPDATE/CREATE TABLE/etc calls that return 'true' instead of a result object.
Code was attempting to handle this case by asking the result object for its db so we can ask for the affected row count -- obviously that doesn't do so good when the result is not an object.
Changed Database::sourceStream() to send itself as the second parameter to the result-handling callback, so the callback knows which DB to check.
2008-08-01 21:00:24 +00:00

69 lines
1.4 KiB
PHP

<?php
/**
* Send SQL queries from the specified file to the database, performing
* variable replacement along the way.
*
* @file
* @ingroup Database Maintenance
*/
require_once( dirname(__FILE__) . '/' . 'commandLine.inc' );
if ( isset( $options['help'] ) ) {
echo "Send SQL queries to a MediaWiki database.\nUsage: php sql.php [<file>]\n";
exit( 1 );
}
if ( isset( $args[0] ) ) {
$fileName = $args[0];
$file = fopen( $fileName, 'r' );
$promptCallback = false;
} else {
$file = STDIN;
$promptObject = new SqlPromptPrinter( "> " );
$promptCallback = $promptObject->cb();
}
if ( !$file ) {
echo "Unable to open input file\n";
exit( 1 );
}
$dbw =& wfGetDB( DB_MASTER );
$error = $dbw->sourceStream( $file, $promptCallback, 'sqlPrintResult' );
if ( $error !== true ) {
echo $error;
exit( 1 );
} else {
exit( 0 );
}
//-----------------------------------------------------------------------------
class SqlPromptPrinter {
function __construct( $prompt ) {
$this->prompt = $prompt;
}
function cb() {
return array( $this, 'printPrompt' );
}
function printPrompt() {
echo $this->prompt;
}
}
function sqlPrintResult( $res, $db ) {
if ( !$res ) {
// Do nothing
} elseif ( is_object( $res ) && $res->numRows() ) {
while ( $row = $res->fetchObject() ) {
print_r( $row );
}
} else {
$affected = $db->affectedRows();
echo "Query OK, $affected row(s) affected\n";
}
}