Fix for r47482: put the RELEASE-NOTES entry in the "bug fixes" section and not the...
[mediawiki.git] / maintenance / sql.php
blobab6546b9e08c428ecc958a41a4bffa488816c874
1 <?php
2 /**
3 * Send SQL queries from the specified file to the database, performing
4 * variable replacement along the way.
6 * @file
7 * @ingroup Database Maintenance
8 */
10 require_once( dirname(__FILE__) . '/' . 'commandLine.inc' );
12 if ( isset( $options['help'] ) ) {
13 echo "Send SQL queries to a MediaWiki database.\nUsage: php sql.php [<file>]\n";
14 exit( 1 );
17 if ( isset( $args[0] ) ) {
18 $fileName = $args[0];
19 $file = fopen( $fileName, 'r' );
20 $promptCallback = false;
21 } else {
22 $file = STDIN;
23 $promptObject = new SqlPromptPrinter( "> " );
24 $promptCallback = $promptObject->cb();
27 if ( !$file ) {
28 echo "Unable to open input file\n";
29 exit( 1 );
32 $dbw =& wfGetDB( DB_MASTER );
33 $error = $dbw->sourceStream( $file, $promptCallback, 'sqlPrintResult' );
34 if ( $error !== true ) {
35 echo $error;
36 exit( 1 );
37 } else {
38 exit( 0 );
41 //-----------------------------------------------------------------------------
42 class SqlPromptPrinter {
43 function __construct( $prompt ) {
44 $this->prompt = $prompt;
47 function cb() {
48 return array( $this, 'printPrompt' );
51 function printPrompt() {
52 echo $this->prompt;
56 function sqlPrintResult( $res, $db ) {
57 if ( !$res ) {
58 // Do nothing
59 } elseif ( is_object( $res ) && $res->numRows() ) {
60 while ( $row = $res->fetchObject() ) {
61 print_r( $row );
63 } else {
64 $affected = $db->affectedRows();
65 echo "Query OK, $affected row(s) affected\n";