Fix Maintenance::output when called very early
authorMarius Hoch <hoo@online.de>
Thu, 4 Jan 2018 23:19:55 +0000 (00:19 +0100)
committerMarius Hoch <hoo@online.de>
Thu, 4 Jan 2018 23:19:55 +0000 (00:19 +0100)
If a maintenance script is called with --help (or without
required args), this is currently failing, as MediaWikiServices
is only loaded after Maintenance::setup is initially called.
Follows 336454104d1249439e3ff97746de69c5e7b4a5f9.

Change-Id: Ied283d362675b6b98bd3144132a240b432998991

maintenance/Maintenance.php

index 07f547f..8bfead3 100644 (file)
@@ -381,11 +381,15 @@ abstract class Maintenance {
         * @param mixed $channel Unique identifier for the channel. See function outputChanneled.
         */
        protected function output( $out, $channel = null ) {
-               // Try to periodically flush buffered metrics to avoid OOMs
-               $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
-               if ( $stats->getDataCount() > 1000 ) {
-                       MediaWiki::emitBufferedStatsdData( $stats, $this->getConfig() );
+               // This is sometimes called very early, before Setup.php is included.
+               if ( class_exists( MediaWikiServices::class ) ) {
+                       // Try to periodically flush buffered metrics to avoid OOMs
+                       $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
+                       if ( $stats->getDataCount() > 1000 ) {
+                               MediaWiki::emitBufferedStatsdData( $stats, $this->getConfig() );
+                       }
                }
+
                if ( $this->mQuiet ) {
                        return;
                }