[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/maintenance/ -> sql.php (source)

   1  <?php
   2  /**
   3   * Send SQL queries from the specified file to the database, performing
   4   * variable replacement along the way.
   5   *
   6   * This program is free software; you can redistribute it and/or modify
   7   * it under the terms of the GNU General Public License as published by
   8   * the Free Software Foundation; either version 2 of the License, or
   9   * (at your option) any later version.
  10   *
  11   * This program is distributed in the hope that it will be useful,
  12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14   * GNU General Public License for more details.
  15   *
  16   * You should have received a copy of the GNU General Public License along
  17   * with this program; if not, write to the Free Software Foundation, Inc.,
  18   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19   * http://www.gnu.org/copyleft/gpl.html
  20   *
  21   * @file
  22   * @ingroup Maintenance
  23   */
  24  
  25  require_once  __DIR__ . '/Maintenance.php';
  26  
  27  /**
  28   * Maintenance script that sends SQL queries from the specified file to the database.
  29   *
  30   * @ingroup Maintenance
  31   */
  32  class MwSql extends Maintenance {
  33  	public function __construct() {
  34          parent::__construct();
  35          $this->mDescription = "Send SQL queries to a MediaWiki database";
  36          $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
  37          $this->addOption( 'wikidb', 'The database wiki ID to use if not the current one', false, true );
  38          $this->addOption( 'slave', 'Use a slave server (either "any" or by name)', false, true );
  39      }
  40  
  41  	public function execute() {
  42          $wiki = $this->getOption( 'wikidb' ) ?: false;
  43          // Get the appropriate load balancer (for this wiki)
  44          if ( $this->hasOption( 'cluster' ) ) {
  45              $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ), $wiki );
  46          } else {
  47              $lb = wfGetLB( $wiki );
  48          }
  49          // Figure out which server to use
  50          if ( $this->hasOption( 'slave' ) ) {
  51              $server = $this->getOption( 'slave' );
  52              if ( $server === 'any' ) {
  53                  $index = DB_SLAVE;
  54              } else {
  55                  $index = null;
  56                  $serverCount = $lb->getServerCount();
  57                  for ( $i = 0; $i < $serverCount; ++$i ) {
  58                      if ( $lb->getServerName( $i ) === $server ) {
  59                          $index = $i;
  60                          break;
  61                      }
  62                  }
  63                  if ( $index === null ) {
  64                      $this->error( "No slave server configured with the name '$server'.", 1 );
  65                  }
  66              }
  67          } else {
  68              $index = DB_MASTER;
  69          }
  70          // Get a DB handle (with this wiki's DB selected) from the appropriate load balancer
  71          $db = $lb->getConnection( $index, array(), $wiki );
  72          if ( $this->hasOption( 'slave' ) && $db->getLBInfo( 'master' ) !== null ) {
  73              $this->error( "The server selected ({$db->getServer()}) is not a slave.", 1 );
  74          }
  75  
  76          if ( $this->hasArg( 0 ) ) {
  77              $file = fopen( $this->getArg( 0 ), 'r' );
  78              if ( !$file ) {
  79                  $this->error( "Unable to open input file", true );
  80              }
  81  
  82              $error = $db->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
  83              if ( $error !== true ) {
  84                  $this->error( $error, true );
  85              } else {
  86                  exit( 0 );
  87              }
  88          }
  89  
  90          $useReadline = function_exists( 'readline_add_history' )
  91              && Maintenance::posix_isatty( 0 /*STDIN*/ );
  92  
  93          if ( $useReadline ) {
  94              global $IP;
  95              $historyFile = isset( $_ENV['HOME'] ) ?
  96                  "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
  97              readline_read_history( $historyFile );
  98          }
  99  
 100          $wholeLine = '';
 101          $newPrompt = '> ';
 102          $prompt = $newPrompt;
 103          while ( ( $line = Maintenance::readconsole( $prompt ) ) !== false ) {
 104              if ( !$line ) {
 105                  # User simply pressed return key
 106                  continue;
 107              }
 108              $done = $db->streamStatementEnd( $wholeLine, $line );
 109  
 110              $wholeLine .= $line;
 111  
 112              if ( !$done ) {
 113                  $wholeLine .= ' ';
 114                  $prompt = '    -> ';
 115                  continue;
 116              }
 117              if ( $useReadline ) {
 118                  # Delimiter is eated by streamStatementEnd, we add it
 119                  # up in the history (bug 37020)
 120                  readline_add_history( $wholeLine . $db->getDelimiter() );
 121                  readline_write_history( $historyFile );
 122              }
 123              try {
 124                  $res = $db->query( $wholeLine );
 125                  $this->sqlPrintResult( $res, $db );
 126                  $prompt = $newPrompt;
 127                  $wholeLine = '';
 128              } catch ( DBQueryError $e ) {
 129                  $doDie = !Maintenance::posix_isatty( 0 );
 130                  $this->error( $e, $doDie );
 131              }
 132          }
 133          wfWaitForSlaves();
 134      }
 135  
 136      /**
 137       * Print the results, callback for $db->sourceStream()
 138       * @param ResultWrapper $res The results object
 139       * @param DatabaseBase $db
 140       */
 141  	public function sqlPrintResult( $res, $db ) {
 142          if ( !$res ) {
 143              // Do nothing
 144              return;
 145          } elseif ( is_object( $res ) && $res->numRows() ) {
 146              foreach ( $res as $row ) {
 147                  $this->output( print_r( $row, true ) );
 148              }
 149          } else {
 150              $affected = $db->affectedRows();
 151              $this->output( "Query OK, $affected row(s) affected\n" );
 152          }
 153      }
 154  
 155      /**
 156       * @return int DB_TYPE constant
 157       */
 158  	public function getDbType() {
 159          return Maintenance::DB_ADMIN;
 160      }
 161  }
 162  
 163  $maintClass = "MwSql";
 164  require_once RUN_MAINTENANCE_IF_MAIN;


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1