MediaWiki  REL1_19
sql.php
Go to the documentation of this file.
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                 $dbw = wfGetDB( DB_MASTER );
00034                 if ( $this->hasArg() ) {
00035                         $fileName = $this->getArg();
00036                         $file = fopen( $fileName, 'r' );
00037                         if ( !$file ) {
00038                                 $this->error( "Unable to open input file", true );
00039                         }
00040 
00041                         $error = $dbw->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
00042                         if ( $error !== true ) {
00043                                 $this->error( $error, true );
00044                         } else {
00045                                 exit( 0 );
00046                         }
00047                 }
00048 
00049                 $useReadline = function_exists( 'readline_add_history' )
00050                                 && Maintenance::posix_isatty( 0 /*STDIN*/ );
00051 
00052                 if ( $useReadline ) {
00053                         global $IP;
00054                         $historyFile = isset( $_ENV['HOME'] ) ?
00055                                         "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
00056                         readline_read_history( $historyFile );
00057                 }
00058 
00059                 $wholeLine = '';
00060                 while ( ( $line = Maintenance::readconsole() ) !== false ) {
00061                         $done = $dbw->streamStatementEnd( $wholeLine, $line );
00062 
00063                         $wholeLine .= $line;
00064 
00065                         if ( !$done ) {
00066                                 continue;
00067                         }
00068                         if ( $useReadline ) {
00069                                 readline_add_history( $wholeLine );
00070                                 readline_write_history( $historyFile );
00071                         }
00072                         try{
00073                                 $res = $dbw->query( $wholeLine );
00074                                 $this->sqlPrintResult( $res, $dbw );
00075                                 $wholeLine = '';
00076                         } catch (DBQueryError $e) {
00077                                 $this->error( $e, true );
00078                         }
00079                 }
00080         }
00081 
00087         public function sqlPrintResult( $res, $db ) {
00088                 if ( !$res ) {
00089                         // Do nothing
00090                 } elseif ( is_object( $res ) && $res->numRows() ) {
00091                         foreach ( $res as $row ) {
00092                                 $this->output( print_r( $row, true ) );
00093                         }
00094                 } else {
00095                         $affected = $db->affectedRows();
00096                         $this->output( "Query OK, $affected row(s) affected\n" );
00097                 }
00098         }
00099 
00103         public function getDbType() {
00104                 return Maintenance::DB_ADMIN;
00105         }
00106 }
00107 
00108 $maintClass = "MwSql";
00109 require_once( RUN_MAINTENANCE_IF_MAIN );