X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=maintenance%2Fsql.php;h=2c8bdb67ce33c4cdcafda2f2412ee97f25479cda;hb=f4ec592330517bc80e2ee351d0385e752f16e3fb;hp=8e276e77507b6ce4e15cb7e80fa2dbdde4805037;hpb=64a6afaf6964b3f9e61d595d5586fe7428a57e73;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/sql.php b/maintenance/sql.php index 8e276e7750..2c8bdb67ce 100644 --- a/maintenance/sql.php +++ b/maintenance/sql.php @@ -40,6 +40,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 ); @@ -167,7 +168,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 +176,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 +199,5 @@ class MwSql extends Maintenance { } } -$maintClass = "MwSql"; +$maintClass = MwSql::class; require_once RUN_MAINTENANCE_IF_MAIN;