Ladsgroup has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/403384 )

Change subject: Turn ORESService to a service and turn originalRequest to 
attribute
......................................................................

Turn ORESService to a service and turn originalRequest to attribute

It makes the data flow more clear

Bug: T184142
Change-Id: I488482c05d01f722c848279f7972155ca349a854
---
M includes/FetchScoreJob.php
M includes/ORESService.php
M includes/ScoreFetcher.php
M includes/ServiceWiring.php
M maintenance/DumpThresholds.php
M tests/phpunit/includes/ServiceWiringTest.php
6 files changed, 55 insertions(+), 51 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ORES 
refs/changes/84/403384/1

diff --git a/includes/FetchScoreJob.php b/includes/FetchScoreJob.php
index 2a6d651..23b7c82 100644
--- a/includes/FetchScoreJob.php
+++ b/includes/FetchScoreJob.php
@@ -83,9 +83,6 @@
                }
 
                $logger->info( 'Fetching scores for revision ' . json_encode( 
$this->params ) );
-               if ( isset( $this->params['originalRequest'] ) ) {
-                       $this->scoreFetcher->setOriginalRequest( 
$this->params['originalRequest'] );
-               }
                if ( isset( $this->params['models'] ) ) {
                        $models = $this->params['models'];
                } else {
@@ -94,7 +91,8 @@
                $scores = $this->scoreFetcher->getScores(
                        $this->params['revid'],
                        $models,
-                       $this->params['precache']
+                       $this->params['precache'],
+                       $this->params['originalRequest']
                );
                $scoreStorage = MediaWikiServices::getInstance()->getService( 
'ORESScoreStorage' );
 
diff --git a/includes/ORESService.php b/includes/ORESService.php
index 12bd468..72f68d6 100644
--- a/includes/ORESService.php
+++ b/includes/ORESService.php
@@ -17,8 +17,8 @@
 namespace ORES;
 
 use FormatJson;
-use MediaWiki\Logger\LoggerFactory;
 use MWHttpRequest;
+use Psr\Log\LoggerInterface;
 use RequestContext;
 use RuntimeException;
 use WebRequest;
@@ -27,27 +27,19 @@
  * Common methods for accessing an ORES server.
  */
 class ORESService {
-       /** @var WebRequest|string[]|null */
-       private $originalRequest;
 
        const API_VERSION = 3;
 
        /**
-        * @return ORESService
+        * @var LoggerInterface
         */
-       public static function newFromContext() {
-               $self = new self();
-               if ( empty( $GLOBALS['wgCommandLineMode'] ) ) {
-                       $self->setOriginalRequest( 
RequestContext::getMain()->getRequest() );
-               }
-               return $self;
-       }
+       private $logger;
 
        /**
-        * @param WebRequest|string[] $originalRequest See 
MwHttpRequest::setOriginalRequest()
+        * @param LoggerInterface $logger
         */
-       public function setOriginalRequest( $originalRequest ) {
-               $this->originalRequest = $originalRequest;
+       public function __construct( LoggerInterface $logger ) {
+               $this->logger = $logger;
        }
 
        /**
@@ -79,24 +71,30 @@
         * Make an ORES API request and return the decoded result.
         *
         * @param array $params
-        * @return array Decoded response
+        * @param WebRequest|string[] $originalRequest See 
MwHttpRequest::setOriginalRequest()
         *
+        * @return array Decoded response
         */
-       public function request( array $params ) {
-               $logger = LoggerFactory::getInstance( 'ORES' );
-
+       public function request(
+               array $params,
+               $originalRequest = null
+       ) {
                $url = $this->getUrl();
                $params['format'] = 'json';
                $url = wfAppendQuery( $url, $params );
-               $logger->debug( "Requesting: {$url}" );
-               $req = MWHttpRequest::factory( $url, 
$this->getMWHttpRequestOptions(), __METHOD__ );
+               $this->logger->debug( "Requesting: {$url}" );
+               $req = MWHttpRequest::factory(
+                       $url,
+                       $this->getMWHttpRequestOptions( $originalRequest ),
+                       __METHOD__
+               );
                $status = $req->execute();
                if ( !$status->isOK() ) {
                        throw new RuntimeException( "Failed to make ORES 
request to [{$url}], "
                                . $status->getMessage()->text() );
                }
                $json = $req->getContent();
-               $logger->debug( "Raw response: {$json}" );
+               $this->logger->debug( "Raw response: {$json}" );
                $data = FormatJson::decode( $json, true );
                if ( !$data || !empty( $data['error'] ) ) {
                        throw new RuntimeException( "Bad response from ORES 
endpoint [{$url}]: {$json}" );
@@ -104,8 +102,17 @@
                return $data;
        }
 
-       protected function getMWHttpRequestOptions() {
-               return $this->originalRequest ? [ 'originalRequest' => 
$this->originalRequest ] : [];
+       /**
+        * @param WebRequest|string[] $originalRequest See 
MwHttpRequest::setOriginalRequest()
+        *
+        * @return array
+        */
+       private function getMWHttpRequestOptions( $originalRequest ) {
+               if ( $originalRequest === null && empty( 
$GLOBALS['wgCommandLineMode'] ) ) {
+                       $originalRequest = 
RequestContext::getMain()->getRequest();
+               }
+
+               return $originalRequest ? [ 'originalRequest' => 
$originalRequest ] : [];
        }
 
 }
diff --git a/includes/ScoreFetcher.php b/includes/ScoreFetcher.php
index 918640e..303c35d 100644
--- a/includes/ScoreFetcher.php
+++ b/includes/ScoreFetcher.php
@@ -18,12 +18,8 @@
 
 use InvalidArgumentException;
 use MediaWiki\MediaWikiServices;
-use WebRequest;
 
 class ScoreFetcher implements ScoreLookup {
-
-       /** @var WebRequest|string[]|null */
-       private $originalRequest;
 
        /**
         * @see ScoreLookup::getScores()
@@ -32,10 +28,16 @@
         * @param string|array|null $models Single or multiple model names. If
         * left empty, all configured models are queried.
         * @param bool $precache either the request is made for precaching or 
not
-        *
+
+        * @param null $originalRequest
         * @return array Results in the form returned by ORES API
         */
-       public function getScores( $revisions, $models = null, $precache = 
false ) {
+       public function getScores(
+               $revisions,
+               $models = null,
+               $precache = false,
+               $originalRequest = null
+       ) {
                if ( !$models ) {
                        global $wgOresModels;
                        $models = array_keys( array_filter( $wgOresModels ) );
@@ -49,14 +51,8 @@
                        $params['precache'] = true;
                }
 
-               if ( $this->originalRequest === null ) {
-                       $oresService = ORESService::newFromContext();
-               } else {
-                       $oresService = new ORESService();
-                       $oresService->setOriginalRequest( 
$this->originalRequest );
-               }
-
-               $wireData = $oresService->request( $params );
+               $oresService = MediaWikiServices::getInstance()->getService( 
'ORESService' );
+               $wireData = $oresService->request( $params, $originalRequest );
 
                $wikiId = ORESService::getWikiID();
                if ( array_key_exists( 'models', $wireData[$wikiId] ) ) {
@@ -139,13 +135,6 @@
                        ],
                        __METHOD__
                );
-       }
-
-       /**
-        * @param WebRequest|string[] $originalRequest See 
MwHttpRequest::setOriginalRequest()
-        */
-       public function setOriginalRequest( $originalRequest ) {
-               $this->originalRequest = $originalRequest;
        }
 
        public static function instance() {
diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php
index e8eb7f0..552139f 100644
--- a/includes/ServiceWiring.php
+++ b/includes/ServiceWiring.php
@@ -30,7 +30,7 @@
                return new ThresholdLookup(
                        new ThresholdParser( LoggerFactory::getInstance( 'ORES' 
) ),
                        $services->getService( 'ORESModelLookup' ),
-                       ORESService::newFromContext(),
+                       $services->getService( 'ORESService' ),
                        $services->getMainWANObjectCache(),
                        LoggerFactory::getInstance( 'ORES' ),
                        $services->getStatsdDataFactory()
@@ -44,4 +44,10 @@
                );
        },
 
+       'ORESService' => function ( MediaWikiServices $services ) {
+               return new ORESService(
+                       LoggerFactory::getInstance( 'ORES' )
+               );
+       },
+
 ];
diff --git a/maintenance/DumpThresholds.php b/maintenance/DumpThresholds.php
index d969c5b..a23302a 100644
--- a/maintenance/DumpThresholds.php
+++ b/maintenance/DumpThresholds.php
@@ -40,7 +40,7 @@
         */
        protected function getModels() {
                $timestamp = \wfTimestampNow();
-               $oresService = new ORESService();
+               $oresService = MediaWikiServices::getInstance()->getService( 
'ORESService' );
                // Bypass the varnish cache
                $modelData = $oresService->request( [ $timestamp => true ] );
                if ( empty( $modelData['models'] ) ) {
diff --git a/tests/phpunit/includes/ServiceWiringTest.php 
b/tests/phpunit/includes/ServiceWiringTest.php
index c90047f..f430584 100644
--- a/tests/phpunit/includes/ServiceWiringTest.php
+++ b/tests/phpunit/includes/ServiceWiringTest.php
@@ -3,7 +3,9 @@
 namespace ORES\Tests;
 
 use MediaWiki\MediaWikiServices;
+use ORES\ORESService;
 use ORES\Storage\ModelLookup;
+use ORES\Storage\ScoreStorage;
 use ORES\ThresholdLookup;
 
 /**
@@ -16,7 +18,9 @@
        public function provideServices() {
                return [
                        [ 'ORESModelLookup', ModelLookup::class ],
-                       [ 'ORESThresholdLookup', ThresholdLookup::class ]
+                       [ 'ORESThresholdLookup', ThresholdLookup::class ],
+                       [ 'ORESScoreStorage', ScoreStorage::class ],
+                       [ 'ORESService', ORESService::class ],
                ];
        }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I488482c05d01f722c848279f7972155ca349a854
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ORES
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to