From: Timo Tijhof Date: Sat, 18 Jul 2015 20:21:00 +0000 (-0500) Subject: maintenance: Add --query option in sql.php X-Git-Tag: 1.31.0-rc.0~10719^2 X-Git-Url: https://git.heureux-cyclage.org/?a=commitdiff_plain;h=fc34ed8df7a4c6c1b4572f2dbc0202c515405c0d;p=lhc%2Fweb%2Fwiklou.git maintenance: Add --query option in sql.php This makes it easier to run a query in a scripted fashion, e.g. with Wikimedia's foreachwiki. Change-Id: I5562be2fb0871817ee55f1395b686537069f78dc --- diff --git a/maintenance/sql.php b/maintenance/sql.php index a7fd827e05..a93e51fe5a 100644 --- a/maintenance/sql.php +++ b/maintenance/sql.php @@ -34,6 +34,7 @@ class MwSql extends Maintenance { parent::__construct(); $this->mDescription = "Send SQL queries to a MediaWiki database. " . "Takes a file name containing SQL as argument or runs interactively."; + $this->addOption( 'query', 'Run a single query instead of running interactively', false, true ); $this->addOption( 'cluster', 'Use an external cluster by name', false, true ); $this->addOption( 'wikidb', 'The database wiki ID to use if not the current one', false, true ); $this->addOption( 'slave', 'Use a slave server (either "any" or by name)', false, true ); @@ -89,6 +90,13 @@ class MwSql extends Maintenance { } } + if ( $this->hasOption( 'query' ) ) { + $query = $this->getOption( 'query' ); + $this->sqlDoQuery( $db, $query, /* dieOnError */ true ); + wfWaitForSlaves(); + return; + } + $useReadline = function_exists( 'readline_add_history' ) && Maintenance::posix_isatty( 0 /*STDIN*/ ); @@ -102,6 +110,7 @@ class MwSql extends Maintenance { $wholeLine = ''; $newPrompt = '> '; $prompt = $newPrompt; + $doDie = !Maintenance::posix_isatty( 0 ); while ( ( $line = Maintenance::readconsole( $prompt ) ) !== false ) { if ( !$line ) { # User simply pressed return key @@ -122,19 +131,22 @@ class MwSql extends Maintenance { readline_add_history( $wholeLine . $db->getDelimiter() ); readline_write_history( $historyFile ); } - try { - $res = $db->query( $wholeLine ); - $this->sqlPrintResult( $res, $db ); - } catch ( DBQueryError $e ) { - $doDie = !Maintenance::posix_isatty( 0 ); - $this->error( $e, $doDie ); - } + $this->sqlDoQuery( $db, $wholeLine, $doDie ); $prompt = $newPrompt; $wholeLine = ''; } wfWaitForSlaves(); } + protected function sqlDoQuery( $db, $line, $dieOnError ) { + try { + $res = $db->query( $line ); + $this->sqlPrintResult( $res, $db ); + } catch ( DBQueryError $e ) { + $this->error( $e, $dieOnError ); + } + } + /** * Print the results, callback for $db->sourceStream() * @param ResultWrapper $res The results object