Merge "Revert "Log the reason why revision->getContent() returns null""
[lhc/web/wiklou.git] / maintenance / sql.php
index 36e55f3..2c8bdb6 100644 (file)
@@ -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 );
@@ -72,7 +73,7 @@ class MwSql extends Maintenance {
                                }
                        }
                        if ( $index === null ) {
-                               $this->error( "No replica DB server configured with the name '$replicaDB'.", 1 );
+                               $this->fatalError( "No replica DB server configured with the name '$replicaDB'." );
                        }
                } else {
                        $index = DB_MASTER;
@@ -81,7 +82,7 @@ class MwSql extends Maintenance {
                /** @var IDatabase $db DB handle for the appropriate cluster/wiki */
                $db = $lb->getConnection( $index, [], $wiki );
                if ( $replicaDB != '' && $db->getLBInfo( 'master' ) !== null ) {
-                       $this->error( "The server selected ({$db->getServer()}) is not a replica DB.", 1 );
+                       $this->fatalError( "The server selected ({$db->getServer()}) is not a replica DB." );
                }
 
                if ( $index === DB_MASTER ) {
@@ -92,12 +93,12 @@ class MwSql extends Maintenance {
                if ( $this->hasArg( 0 ) ) {
                        $file = fopen( $this->getArg( 0 ), 'r' );
                        if ( !$file ) {
-                               $this->error( "Unable to open input file", true );
+                               $this->fatalError( "Unable to open input file" );
                        }
 
                        $error = $db->sourceStream( $file, null, [ $this, 'sqlPrintResult' ] );
                        if ( $error !== true ) {
-                               $this->error( $error, true );
+                               $this->fatalError( $error );
                        } else {
                                exit( 0 );
                        }
@@ -157,13 +158,17 @@ class MwSql extends Maintenance {
                        $res = $db->query( $line );
                        $this->sqlPrintResult( $res, $db );
                } catch ( DBQueryError $e ) {
-                       $this->error( $e, $dieOnError );
+                       if ( $dieOnError ) {
+                               $this->fatalError( $e );
+                       } else {
+                               $this->error( $e );
+                       }
                }
        }
 
        /**
         * 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 ) {
@@ -171,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" );
@@ -188,5 +199,5 @@ class MwSql extends Maintenance {
        }
 }
 
-$maintClass = "MwSql";
+$maintClass = MwSql::class;
 require_once RUN_MAINTENANCE_IF_MAIN;