X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=maintenance%2Fsql.php;h=e8b744810461913cfada845a0f7d97aff7480306;hb=ed6d53fd38cbc08ad503174aa09d155dc9867a13;hp=dd05bbe4ad9d74e350082b23c6a6285f74a370c1;hpb=220bda9175a18458449e9d754fb48830c1f76f25;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/sql.php b/maintenance/sql.php index dd05bbe4ad..e8b7448104 100644 --- a/maintenance/sql.php +++ b/maintenance/sql.php @@ -24,6 +24,7 @@ require_once __DIR__ . '/Maintenance.php'; +use MediaWiki\MediaWikiServices; use Wikimedia\Rdbms\ResultWrapper; use Wikimedia\Rdbms\IDatabase; use Wikimedia\Rdbms\DBQueryError; @@ -40,6 +41,7 @@ class MwSql extends Maintenance { '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( 'json', 'Output the results as JSON instead of PHP objects' ); $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 ); @@ -53,10 +55,11 @@ class MwSql extends Maintenance { // 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) + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); if ( $this->hasOption( 'cluster' ) ) { - $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ) ); + $lb = $lbFactory->getExternalLB( $this->getOption( 'cluster' ) ); } else { - $lb = wfGetLB( $wiki ); + $lb = $lbFactory->getMainLB( $wiki ); } // Figure out which server to use $replicaDB = $this->getOption( 'replicadb', $this->getOption( 'slave', '' ) ); @@ -175,9 +178,15 @@ class MwSql extends Maintenance { // Do nothing return; } elseif ( is_object( $res ) && $res->numRows() ) { + $out = ''; foreach ( $res as $row ) { - $this->output( print_r( $row, true ) ); + $out .= print_r( $row, true ); + $rows[] = $row; } + if ( $this->hasOption( 'json' ) ) { + $out = json_encode( $rows, JSON_PRETTY_PRINT ); + } + $this->output( $out . "\n" ); } else { $affected = $db->affectedRows(); $this->output( "Query OK, $affected row(s) affected\n" );