Daniel Kinzler has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/96026


Change subject: Make dumpJson log to stderr.
......................................................................

Make dumpJson log to stderr.

Progress reports and errors now go to stderr per default,
and can be redirected using the --log option.

Change-Id: I561a003ea2ef4fa979d260b1a754b853343c4f4e
---
M repo/maintenance/dumpJson.php
1 file changed, 64 insertions(+), 17 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/26/96026/1

diff --git a/repo/maintenance/dumpJson.php b/repo/maintenance/dumpJson.php
index 75da1ea..9dbc950 100644
--- a/repo/maintenance/dumpJson.php
+++ b/repo/maintenance/dumpJson.php
@@ -47,7 +47,10 @@
         */
        public $entityPerPage;
 
-       public $quiet = true;
+       /**
+        * @var bool|resource
+        */
+       public $logFileHandle = false;
 
        public function __construct() {
                parent::__construct();
@@ -58,7 +61,8 @@
                $this->addOption( 'entity-type', "Only dump this kind of 
entity, e.g. `item` or `property`.", false, true );
                $this->addOption( 'sharding-factor', "The number of shards 
(must be >= 1)", false, true );
                $this->addOption( 'shard', "The shard to output (must be less 
than the sharding-factor) ", false, true );
-               $this->addOption( 'output', "Output file (default is stdout) ", 
false, true );
+               $this->addOption( 'output', "Output file (default is stdout). 
Will be overwritten.", false, true );
+               $this->addOption( 'log', "Log file (default is stderr). Will be 
appended.", false, true );
                $this->addOption( 'quiet', "Disable progress reporting", false, 
false );
        }
 
@@ -74,12 +78,53 @@
        /**
         * Outputs a message vis the output() method.
         *
-        * @see MessageReporter::reportMessage()
+        * @see MessageReporter::logMessage()
         *
         * @param string $message
         */
-       public function reportMessage( $message ) {
+       public function logMessage( $message ) {
+               if ( $this->logFileHandle ) {
+                       fwrite( $this->logFileHandle, "$message\n" );
+                       fflush( $this->logFileHandle );
+               } else {
                        $this->output( "$message\n" );
+               }
+       }
+
+       /**
+        * Opens the given file for use by logMessage().
+        *
+        * @param $file
+        *
+        * @throws \MWException
+        */
+       protected function openLogFile( $file ) {
+               $this->closeLogFile();
+
+               if ( $file === '-' ) {
+                       $file = 'php://stdout';
+               }
+
+               // wouldn't streams be nice...
+               $this->logFileHandle = fopen( $file, 'a' );
+
+               if ( !$this->logFileHandle ) {
+                       throw new \MWException( 'Filed to open log file: ' . 
$file );
+               }
+       }
+
+       /**
+        * Closes any currently open file opened with openLogFile().
+        */
+       protected function closeLogFile() {
+               if ( $this->logFileHandle
+                       && $this->logFileHandle !== STDERR
+                       && $this->logFileHandle !== STDOUT ) {
+
+                       fclose( $this->logFileHandle );
+               }
+
+               $this->logFileHandle = false;
        }
 
        /**
@@ -93,37 +138,38 @@
                $shardingFactor = (int)$this->getOption( 'sharding-factor', 1 );
                $shard = (int)$this->getOption( 'shard', 0 );
 
+               //TODO: Allow injection of an OutputStream for logging
+               $this->openLogFile( $this->getOption( 'log', 'php://stderr' ) );
+
                $outFile = $this->getOption( 'output', 'php://stdout' );
 
                if ( $outFile === '-' ) {
                        $outFile = 'php://stdout';
                }
 
-               $output = fopen( $outFile, 'wa' ); //TODO: Allow injection of 
an OutputStream
+               $output = fopen( $outFile, 'w' ); //TODO: Allow injection of an 
OutputStream
 
                if ( !$output ) {
                        throw new \MWException( 'Failed to open ' . $outFile . 
'!' );
                }
 
+               if ( $this->hasOption( 'list-file' ) ) {
+                       $this->logMessage( "Dumping entities listed in " . 
$this->getOption( 'list-file' ) );
+               }
+
                if ( $entityType ) {
-                       $this->reportMessage( "Dumping entities of type 
$entityType" );
+                       $this->logMessage( "Dumping entities of type 
$entityType" );
                }
 
                if ( $shardingFactor && $shard ) {
-                       $this->reportMessage( "Dumping shard 
$shard/$shardingFactor" );
+                       $this->logMessage( "Dumping shard 
$shard/$shardingFactor" );
                }
 
                $dumper = new JsonDumpGenerator( $output, $this->entityLookup, 
$this->entitySerializer );
 
-               if ( $outFile !== 'php://stdout' && $outFile !== 'php://output' 
) {
-                       $progressReporter = new \ObservableMessageReporter();
-                       $progressReporter->registerReporterCallback( array( 
$this, 'reportMessage' ) );
-
-                       $dumper->setProgressReporter( $progressReporter );
-               } else {
-                       $progressReporter = new \NullMessageReporter();
-                       $this->mQuiet = true; // suppress in-band output
-               }
+               $progressReporter = new \ObservableMessageReporter();
+               $progressReporter->registerReporterCallback( array( $this, 
'logMessage' ) );
+               $dumper->setProgressReporter( $progressReporter );
 
                $exceptionReporter = new \ReportingExceptionHandler( 
$progressReporter );
                $dumper->setExceptionHandler( $exceptionReporter );
@@ -140,6 +186,8 @@
                        // close stream / free resources
                        $idStream->dispose();
                }
+
+               $this->closeLogFile();
        }
 
        /**
@@ -179,7 +227,6 @@
 
                return $stream;
        }
-
 }
 
 $maintClass = 'Wikibase\DumpJson';

-- 
To view, visit https://gerrit.wikimedia.org/r/96026
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I561a003ea2ef4fa979d260b1a754b853343c4f4e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to