From: Gergő Tisza Date: Mon, 17 Dec 2018 05:06:32 +0000 (-0800) Subject: Fix sql.php --json behavior X-Git-Tag: 1.34.0-rc.0~3157^2~1 X-Git-Url: http://git.cyclocoop.org/%7B%7B%20url_for%28%27admin_vote_add%27%29%20%7D%7D?a=commitdiff_plain;h=3f43c57024652e2a2fc718c574be327d6e8717d9;p=lhc%2Fweb%2Fwiklou.git Fix sql.php --json behavior * return empty array when the query had no result (instead of falling back into non-JSON mode) * return JSON for write queries as well * retain legacy behavior in non-JSON mode for empty SELECTs, just in case somebody relied on it Change-Id: Iaefbb443650a395278d1cc9ab6aa668b13b217c9 --- diff --git a/maintenance/sql.php b/maintenance/sql.php index e8b7448104..dba4a223a7 100644 --- a/maintenance/sql.php +++ b/maintenance/sql.php @@ -177,19 +177,26 @@ class MwSql extends Maintenance { if ( !$res ) { // Do nothing return; - } elseif ( is_object( $res ) && $res->numRows() ) { + } elseif ( is_object( $res ) ) { $out = ''; + $rows = []; foreach ( $res as $row ) { $out .= print_r( $row, true ); $rows[] = $row; } if ( $this->hasOption( 'json' ) ) { $out = json_encode( $rows, JSON_PRETTY_PRINT ); + } elseif ( !$rows ) { + $out = 'Query OK, 0 row(s) affected'; } $this->output( $out . "\n" ); } else { $affected = $db->affectedRows(); - $this->output( "Query OK, $affected row(s) affected\n" ); + if ( $this->hasOption( 'json' ) ) { + $this->output( json_encode( [ 'affected' => $affected ], JSON_PRETTY_PRINT ) . "\n" ); + } else { + $this->output( "Query OK, $affected row(s) affected\n" ); + } } }