00001 <?php
00010 require_once( dirname(__FILE__) . '/' . 'commandLine.inc' );
00011
00012 if ( isset( $options['help'] ) ) {
00013 echo "Send SQL queries to a MediaWiki database.\nUsage: php sql.php [<file>]\n";
00014 exit( 1 );
00015 }
00016
00017 if ( isset( $args[0] ) ) {
00018 $fileName = $args[0];
00019 $file = fopen( $fileName, 'r' );
00020 $promptCallback = false;
00021 } else {
00022 $file = STDIN;
00023 $promptObject = new SqlPromptPrinter( "> " );
00024 $promptCallback = $promptObject->cb();
00025 }
00026
00027 if ( !$file ) {
00028 echo "Unable to open input file\n";
00029 exit( 1 );
00030 }
00031
00032 $dbw =& wfGetDB( DB_MASTER );
00033 $error = $dbw->sourceStream( $file, $promptCallback, 'sqlPrintResult' );
00034 if ( $error !== true ) {
00035 echo $error;
00036 exit( 1 );
00037 } else {
00038 exit( 0 );
00039 }
00040
00041
00042 class SqlPromptPrinter {
00043 function __construct( $prompt ) {
00044 $this->prompt = $prompt;
00045 }
00046
00047 function cb() {
00048 return array( $this, 'printPrompt' );
00049 }
00050
00051 function printPrompt() {
00052 echo $this->prompt;
00053 }
00054 }
00055
00056 function sqlPrintResult( $res, $db ) {
00057 if ( !$res ) {
00058
00059 } elseif ( is_object( $res ) && $res->numRows() ) {
00060 while ( $row = $res->fetchObject() ) {
00061 print_r( $row );
00062 }
00063 } else {
00064 $affected = $db->affectedRows();
00065 echo "Query OK, $affected row(s) affected\n";
00066 }
00067 }
00068
00069