Add getStatsdDataFactory to MediawikiServices
authoraddshore <addshorewiki@gmail.com>
Mon, 11 Apr 2016 18:28:17 +0000 (19:28 +0100)
committeraddshore <addshorewiki@gmail.com>
Tue, 19 Apr 2016 12:11:38 +0000 (13:11 +0100)
Change-Id: Iaf89d5d7887f766064266874ea7ba73362531ed2

includes/DefaultSettings.php
includes/MediaWikiServices.php
includes/ServiceWiring.php
includes/context/ContextSource.php
includes/context/DerivativeContext.php
includes/context/IContextSource.php
includes/context/RequestContext.php
tests/phpunit/includes/MediaWikiServicesTest.php

index 10e6adb..f5b3caa 100644 (file)
@@ -5896,7 +5896,7 @@ $wgStatsdServer = false;
 /**
  * Prefix for metric names sent to $wgStatsdServer.
  *
- * @see RequestContext::getStats
+ * @see MediaWikiServices::getStatsdDataFactory
  * @see BufferingStatsdDataFactory
  * @since 1.25
  */
index 7b1def9..3f4d8ed 100644 (file)
@@ -6,6 +6,7 @@ use GlobalVarConfig;
 use Config;
 use Hooks;
 use LBFactory;
+use Liuggio\StatsdClient\Factory\StatsdDataFactory;
 use LoadBalancer;
 use MediaWiki\Services\ServiceContainer;
 use SiteLookup;
@@ -144,6 +145,13 @@ class MediaWikiServices extends ServiceContainer {
                return $this->getService( 'SiteStore' );
        }
 
+       /**
+        * @return StatsdDataFactory
+        */
+       public function getStatsdDataFactory() {
+               return $this->getService( 'StatsdDataFactory' );
+       }
+
        ///////////////////////////////////////////////////////////////////////////
        // NOTE: When adding a service getter here, don't forget to add a test
        // case for it in MediaWikiServicesTest::provideGetters() and in
index d8709b9..7e1d4e3 100644 (file)
@@ -72,6 +72,12 @@ return [
                return $services->getConfigFactory()->makeConfig( 'main' );
        },
 
+       'StatsdDataFactory' => function( MediaWikiServices $services ) {
+               return new BufferingStatsdDataFactory(
+                       rtrim( $services->getMainConfig()->get( 'StatsdMetricPrefix' ), '.' )
+               );
+       },
+
        ///////////////////////////////////////////////////////////////////////////
        // NOTE: When adding a service here, don't forget to add a getter function
        // in the MediaWikiServices class. The convenience getter should just call
index 911ecdd..b617871 100644 (file)
@@ -18,6 +18,8 @@
  * @author Happy-melon
  * @file
  */
+use Liuggio\StatsdClient\Factory\StatsdDataFactory;
+use MediaWiki\MediaWikiServices;
 
 /**
  * The simplest way of implementing IContextSource is to hold a RequestContext as a
@@ -165,8 +167,10 @@ abstract class ContextSource implements IContextSource {
        /**
         * Get the Stats object
         *
+        * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
+        *
         * @since 1.25
-        * @return BufferingStatsdDataFactory
+        * @return StatsdDataFactory
         */
        public function getStats() {
                return $this->getContext()->getStats();
index 1b881e4..e77a058 100644 (file)
@@ -18,6 +18,8 @@
  * @author Daniel Friesen
  * @file
  */
+use Liuggio\StatsdClient\Factory\StatsdDataFactory;
+use MediaWiki\MediaWikiServices;
 
 /**
  * An IContextSource implementation which will inherit context from another source
@@ -67,11 +69,6 @@ class DerivativeContext extends ContextSource implements MutableContext {
         */
        private $config;
 
-       /**
-        * @var Stats
-        */
-       private $stats;
-
        /**
         * @var Timing
         */
@@ -110,14 +107,12 @@ class DerivativeContext extends ContextSource implements MutableContext {
        /**
         * Get the stats object
         *
-        * @return BufferingStatsdDataFactory
+        * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
+        *
+        * @return StatsdDataFactory
         */
        public function getStats() {
-               if ( !is_null( $this->stats ) ) {
-                       return $this->stats;
-               } else {
-                       return $this->getContext()->getStats();
-               }
+               return MediaWikiServices::getInstance()->getStatsdDataFactory();
        }
 
        /**
index 750389d..ccefc72 100644 (file)
@@ -21,6 +21,8 @@
  * @file
  */
 
+use Liuggio\StatsdClient\Factory\StatsdDataFactory;
+
 /**
  * Interface for objects which can provide a MediaWiki context on request
  *
@@ -126,8 +128,10 @@ interface IContextSource {
        /**
         * Get the stats object
         *
+        * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
+        *
         * @since 1.25
-        * @return BufferingStatsdDataFactory
+        * @return StatsdDataFactory
         */
        public function getStats();
 
index c8b8108..c87798e 100644 (file)
@@ -22,7 +22,9 @@
  * @file
  */
 
+use Liuggio\StatsdClient\Factory\StatsdDataFactory;
 use MediaWiki\Logger\LoggerFactory;
+use MediaWiki\MediaWikiServices;
 
 /**
  * Group all the pieces relevant to the context of a request into one instance
@@ -63,11 +65,6 @@ class RequestContext implements IContextSource, MutableContext {
         */
        private $skin;
 
-       /**
-        * @var \Liuggio\StatsdClient\Factory\StatsdDataFactory
-        */
-       private $stats;
-
        /**
         * @var Timing
         */
@@ -138,14 +135,12 @@ class RequestContext implements IContextSource, MutableContext {
        /**
         * Get the Stats object
         *
-        * @return BufferingStatsdDataFactory
+        * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
+        *
+        * @return StatsdDataFactory
         */
        public function getStats() {
-               if ( $this->stats === null ) {
-                       $prefix = rtrim( $this->getConfig()->get( 'StatsdMetricPrefix' ), '.' );
-                       $this->stats = new BufferingStatsdDataFactory( $prefix );
-               }
-               return $this->stats;
+               return MediaWikiServices::getInstance()->getStatsdDataFactory();
        }
 
        /**
index 127f869..1889575 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+use Liuggio\StatsdClient\Factory\StatsdDataFactory;
 use MediaWiki\MediaWikiServices;
 
 /**
@@ -23,6 +24,7 @@ class MediaWikiServicesTest extends PHPUnit_Framework_TestCase {
                        'MainConfig' => [ 'getMainConfig', Config::class ],
                        'SiteStore' => [ 'getSiteStore', SiteStore::class ],
                        'SiteLookup' => [ 'getSiteLookup', SiteLookup::class ],
+                       'StatsdDataFactory' => [ 'getStatsdDataFactory', StatsdDataFactory::class ],
                ];
        }
 
@@ -46,6 +48,7 @@ class MediaWikiServicesTest extends PHPUnit_Framework_TestCase {
                        'MainConfig' => [ 'MainConfig', Config::class ],
                        'SiteStore' => [ 'SiteStore', SiteStore::class ],
                        'SiteLookup' => [ 'SiteLookup', SiteLookup::class ],
+                       'StatsdDataFactory' => [ 'StatsdDataFactory', StatsdDataFactory::class ],
                ];
        }