X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=blobdiff_plain;f=maintenance%2Fsql.php;h=a93e51fe5ace013278f008702b46dac388a35c18;hb=f2c242e2e8d86d6d12ae9c160b88c43558306d8f;hp=afa3ef725c79a8bc762de4dadfd00a03d24c9ff7;hpb=ca28853e225fb8c3a2715c6f5bcc558d9e482590;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/sql.php b/maintenance/sql.php index afa3ef725c..a93e51fe5a 100644 --- a/maintenance/sql.php +++ b/maintenance/sql.php @@ -32,14 +32,17 @@ require_once __DIR__ . '/Maintenance.php'; class MwSql extends Maintenance { public function __construct() { parent::__construct(); - $this->mDescription = "Send SQL queries to a MediaWiki database"; + $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 ); } public function execute() { - $wiki = $this->getOption( 'wikidb' ) ?: false; + // We wan't to allow "" for the wikidb, meaning don't call select_db() + $wiki = $this->hasOption( 'wikidb' ) ? $this->getOption( 'wikidb' ) : false; // Get the appropriate load balancer (for this wiki) if ( $this->hasOption( 'cluster' ) ) { $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ), $wiki ); @@ -87,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*/ ); @@ -100,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 @@ -120,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 ); - $prompt = $newPrompt; - $wholeLine = ''; - } 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