MediaWiki  REL1_20
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                 while ( ( $line = Maintenance::readconsole() ) !== false ) {
00067                         $done = $dbw->streamStatementEnd( $wholeLine, $line );
00068 
00069                         $wholeLine .= $line;
00070 
00071                         if ( !$done ) {
00072                                 continue;
00073                         }
00074                         if ( $useReadline ) {
00075                                 readline_add_history( $wholeLine );
00076                                 readline_write_history( $historyFile );
00077                         }
00078                         try{
00079                                 $res = $dbw->query( $wholeLine );
00080                                 $this->sqlPrintResult( $res, $dbw );
00081                                 $wholeLine = '';
00082                         } catch (DBQueryError $e) {
00083                                 $this->error( $e, true );
00084                         }
00085                 }
00086         }
00087 
00093         public function sqlPrintResult( $res, $db ) {
00094                 if ( !$res ) {
00095                         // Do nothing
00096                 } elseif ( is_object( $res ) && $res->numRows() ) {
00097                         foreach ( $res as $row ) {
00098                                 $this->output( print_r( $row, true ) );
00099                         }
00100                 } else {
00101                         $affected = $db->affectedRows();
00102                         $this->output( "Query OK, $affected row(s) affected\n" );
00103                 }
00104         }
00105 
00109         public function getDbType() {
00110                 return Maintenance::DB_ADMIN;
00111         }
00112 }
00113 
00114 $maintClass = "MwSql";
00115 require_once( RUN_MAINTENANCE_IF_MAIN );