From d8c29fee35e021f3683a9fc59b46eb8098174925 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Fri, 16 Feb 2018 13:21:38 -0800 Subject: [PATCH] sql.php: Provide --json output mode stdObject is a pretty useless format unless you're working with PHP Change-Id: I7549347a630768223fba5b282a930361dfe6f2d3 --- maintenance/sql.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/maintenance/sql.php b/maintenance/sql.php index dd05bbe4ad..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 ); @@ -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" ); -- 2.20.1