X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=maintenance%2Fsql.php;h=e8b744810461913cfada845a0f7d97aff7480306;hb=977b389d6a6ed441891fba350c3a6e28d1f21315;hp=8e276e77507b6ce4e15cb7e80fa2dbdde4805037;hpb=bc6ed2ba24a8691ad6d6cc321a43720bcfb770e2;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/sql.php b/maintenance/sql.php index 8e276e7750..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', '' ) ); @@ -167,7 +170,7 @@ class MwSql extends Maintenance { /** * Print the results, callback for $db->sourceStream() - * @param ResultWrapper|bool $res The results object + * @param ResultWrapper|bool $res * @param IDatabase $db */ public function sqlPrintResult( $res, $db ) { @@ -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" ); @@ -192,5 +201,5 @@ class MwSql extends Maintenance { } } -$maintClass = "MwSql"; +$maintClass = MwSql::class; require_once RUN_MAINTENANCE_IF_MAIN;