MediaWiki  REL1_21
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                 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
00037         }
00038 
00039         public function execute() {
00040                 // Get a DB handle (with this wiki's DB select) from the appropriate load balancer
00041                 if ( $this->hasOption( 'cluster' ) ) {
00042                         $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ) );
00043                         $dbw = $lb->getConnection( DB_MASTER ); // master for external LB
00044                 } else {
00045                         $dbw = wfGetDB( DB_MASTER ); // master for primary LB for this wiki
00046                 }
00047                 if ( $this->hasArg( 0 ) ) {
00048                         $file = fopen( $this->getArg( 0 ), 'r' );
00049                         if ( !$file ) {
00050                                 $this->error( "Unable to open input file", true );
00051                         }
00052 
00053                         $error = $dbw->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
00054                         if ( $error !== true ) {
00055                                 $this->error( $error, true );
00056                         } else {
00057                                 exit( 0 );
00058                         }
00059                 }
00060 
00061                 $useReadline = function_exists( 'readline_add_history' )
00062                                 && Maintenance::posix_isatty( 0 /*STDIN*/ );
00063 
00064                 if ( $useReadline ) {
00065                         global $IP;
00066                         $historyFile = isset( $_ENV['HOME'] ) ?
00067                                         "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
00068                         readline_read_history( $historyFile );
00069                 }
00070 
00071                 $wholeLine = '';
00072                 $newPrompt = '> ';
00073                 $prompt    = $newPrompt;
00074                 while ( ( $line = Maintenance::readconsole( $prompt ) ) !== false ) {
00075                         if( !$line ) {
00076                                 # User simply pressed return key
00077                                 continue;
00078                         }
00079                         $done = $dbw->streamStatementEnd( $wholeLine, $line );
00080 
00081                         $wholeLine .= $line;
00082 
00083                         if ( !$done ) {
00084                                 $wholeLine .= ' ';
00085                                 $prompt = '    -> ';
00086                                 continue;
00087                         }
00088                         if ( $useReadline ) {
00089                                 # Delimiter is eated by streamStatementEnd, we add it
00090                                 # up in the history (bug 37020)
00091                                 readline_add_history( $wholeLine . $dbw->getDelimiter() );
00092                                 readline_write_history( $historyFile );
00093                         }
00094                         try{
00095                                 $res = $dbw->query( $wholeLine );
00096                                 $this->sqlPrintResult( $res, $dbw );
00097                                 $prompt    = $newPrompt;
00098                                 $wholeLine = '';
00099                         } catch (DBQueryError $e) {
00100                                 $doDie = ! Maintenance::posix_isatty( 0 );
00101                                 $this->error( $e, $doDie );
00102                         }
00103                 }
00104                 wfWaitForSlaves();
00105         }
00106 
00112         public function sqlPrintResult( $res, $db ) {
00113                 if ( !$res ) {
00114                         // Do nothing
00115                         return;
00116                 } elseif ( is_object( $res ) && $res->numRows() ) {
00117                         foreach ( $res as $row ) {
00118                                 $this->output( print_r( $row, true ) );
00119                         }
00120                 } else {
00121                         $affected = $db->affectedRows();
00122                         $this->output( "Query OK, $affected row(s) affected\n" );
00123                 }
00124         }
00125 
00129         public function getDbType() {
00130                 return Maintenance::DB_ADMIN;
00131         }
00132 }
00133 
00134 $maintClass = "MwSql";
00135 require_once( RUN_MAINTENANCE_IF_MAIN );