00001 <?php
00024 require_once( dirname(__FILE__) . '/Maintenance.php' );
00025
00026 class MwSql extends Maintenance {
00027 public function __construct() {
00028 parent::__construct();
00029 $this->mDescription = "Send SQL queries to a MediaWiki database";
00030 }
00031
00032 public function execute() {
00033 if ( $this->hasArg() ) {
00034 $fileName = $this->getArg();
00035 $file = fopen( $fileName, 'r' );
00036 $promptCallback = false;
00037 } else {
00038 $file = $this->getStdin();
00039 $promptObject = new SqlPromptPrinter( "> " );
00040 $promptCallback = $promptObject->cb();
00041 }
00042
00043 if ( !$file )
00044 $this->error( "Unable to open input file", true );
00045
00046 $dbw = wfGetDB( DB_MASTER );
00047 $error = $dbw->sourceStream( $file, $promptCallback, array( $this, 'sqlPrintResult' ) );
00048 if ( $error !== true ) {
00049 $this->error( $error, true );
00050 } else {
00051 exit( 0 );
00052 }
00053 }
00054
00060 public function sqlPrintResult( $res, $db ) {
00061 if ( !$res ) {
00062
00063 } elseif ( is_object( $res ) && $res->numRows() ) {
00064 foreach ( $res as $row ) {
00065 $this->output( print_r( $row, true ) );
00066 }
00067 } else {
00068 $affected = $db->affectedRows();
00069 $this->output( "Query OK, $affected row(s) affected\n" );
00070 }
00071 }
00072
00073 public function getDbType() {
00074 return Maintenance::DB_ADMIN;
00075 }
00076 }
00077
00078 class SqlPromptPrinter {
00079 function __construct( $prompt ) {
00080 $this->prompt = $prompt;
00081 }
00082
00083 function cb() {
00084 return array( $this, 'printPrompt' );
00085 }
00086
00087 function printPrompt() {
00088 echo $this->prompt;
00089 }
00090 }
00091
00092 $maintClass = "MwSql";
00093 require_once( DO_MAINTENANCE );