MediaWiki  master
sql.php
Go to the documentation of this file.
00001 <?php
00025 require_once( __DIR__ . '/Maintenance.php' );
00026 
00032 class MwSql extends Maintenance {
00033         public function __construct() {
00034                 parent::__construct();
00035                 $this->mDescription = "Send SQL queries to a MediaWiki database";
00036         }
00037 
00038         public function execute() {
00039                 $dbw = wfGetDB( DB_MASTER );
00040                 if ( $this->hasArg() ) {
00041                         $fileName = $this->getArg();
00042                         $file = fopen( $fileName, 'r' );
00043                         if ( !$file ) {
00044                                 $this->error( "Unable to open input file", true );
00045                         }
00046 
00047                         $error = $dbw->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
00048                         if ( $error !== true ) {
00049                                 $this->error( $error, true );
00050                         } else {
00051                                 exit( 0 );
00052                         }
00053                 }
00054 
00055                 $useReadline = function_exists( 'readline_add_history' )
00056                                 && Maintenance::posix_isatty( 0 /*STDIN*/ );
00057 
00058                 if ( $useReadline ) {
00059                         global $IP;
00060                         $historyFile = isset( $_ENV['HOME'] ) ?
00061                                         "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
00062                         readline_read_history( $historyFile );
00063                 }
00064 
00065                 $wholeLine = '';
00066                 $newPrompt = '> ';
00067                 $prompt    = $newPrompt;
00068                 while ( ( $line = Maintenance::readconsole( $prompt ) ) !== false ) {
00069                         if( !$line ) {
00070                                 # User simply pressed return key
00071                                 continue;
00072                         }
00073                         $done = $dbw->streamStatementEnd( $wholeLine, $line );
00074 
00075                         $wholeLine .= $line;
00076 
00077                         if ( !$done ) {
00078                                 $wholeLine .= ' ';
00079                                 $prompt = '    -> ';
00080                                 continue;
00081                         }
00082                         if ( $useReadline ) {
00083                                 # Delimiter is eated by streamStatementEnd, we add it
00084                                 # up in the history (bug 37020)
00085                                 readline_add_history( $wholeLine . $dbw->getDelimiter() );
00086                                 readline_write_history( $historyFile );
00087                         }
00088                         try{
00089                                 $res = $dbw->query( $wholeLine );
00090                                 $this->sqlPrintResult( $res, $dbw );
00091                                 $prompt    = $newPrompt;
00092                                 $wholeLine = '';
00093                         } catch (DBQueryError $e) {
00094                                 $doDie = ! Maintenance::posix_isatty( 0 );
00095                                 $this->error( $e, $doDie );
00096                         }
00097                 }
00098         }
00099 
00105         public function sqlPrintResult( $res, $db ) {
00106                 if ( !$res ) {
00107                         // Do nothing
00108                 } elseif ( is_object( $res ) && $res->numRows() ) {
00109                         foreach ( $res as $row ) {
00110                                 $this->output( print_r( $row, true ) );
00111                         }
00112                 } else {
00113                         $affected = $db->affectedRows();
00114                         $this->output( "Query OK, $affected row(s) affected\n" );
00115                 }
00116         }
00117 
00121         public function getDbType() {
00122                 return Maintenance::DB_ADMIN;
00123         }
00124 }
00125 
00126 $maintClass = "MwSql";
00127 require_once( RUN_MAINTENANCE_IF_MAIN );