wiki.techinc.nl/maintenance/sql.php
Tim Starling 0a93752af6 * Removed mysql5 SQL files, obviously we're collectively incapable of keeping them up to date. They were littered with errors. The only deliberate difference appears to be the table character set, which can be adjusted programatically using a variable.
* Added table option variable to the SQL patch files, so now upgrading a non-standard schema won't leave your database horribly corrupted.
* Added sql.php, for sourcing SQL files with MediaWiki variable substitution.
* Disable MySQL's strict mode at session start for MySQL 4.1+, to avoid the various problems that occur when it is on.
2007-04-22 14:04:06 +00:00

67 lines
1.3 KiB
PHP

<?php
/**
* Send SQL queries from the specified file to the database, performing
* variable replacement along the way.
*/
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 ) {
if ( !$res ) {
// Do nothing
} elseif ( $res->numRows() ) {
while ( $row = $res->fetchObject() ) {
print_r( $row );
}
} else {
$affected = $res->db->affectedRows();
echo "Query OK, $affected row(s) affected\n";
}
}
?>