Gergő Tisza has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/344815 )

Change subject: Forward request data to ORES API
......................................................................

Forward request data to ORES API

Bug: T161029
Change-Id: I3cad8e0939033b75dc960a53fbc73a63e39414ca
Depends-On: I16a2eec46ae9e0e242fe740be20e39a2422dc867
---
M includes/Api.php
M includes/ApiHooks.php
M includes/FetchScoreJob.php
M includes/Hooks.php
M includes/Scoring.php
M includes/Stats.php
M maintenance/DumpThresholds.php
7 files changed, 70 insertions(+), 8 deletions(-)


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

diff --git a/includes/Api.php b/includes/Api.php
index c3c2941..d53d048 100644
--- a/includes/Api.php
+++ b/includes/Api.php
@@ -5,12 +5,34 @@
 use FormatJson;
 use MediaWiki\Logger\LoggerFactory;
 use MWHttpRequest;
+use RequestContext;
 use RuntimeException;
+use WebRequest;
 
 /**
  * Common methods for accessing an ORES server.
  */
 class Api {
+       /** @var WebRequest|string[] */
+       protected $originalRequest;
+
+       /**
+        * @return static
+        */
+       public static function newFromContext() {
+               $self = new static; // vararg constructors are a mess in PHP 5 
and nothing currently needs it
+               if ( empty( $GLOBALS['wgCommandLineMode'] ) ) {
+                       $self->setOriginalRequest( 
RequestContext::getMain()->getRequest() );
+               }
+               return $self;
+       }
+
+       /**
+        * @param WebRequest|array $originalRequest See the Http::request() 
option with the same name.
+        */
+       public function setOriginalRequest( $originalRequest ) {
+               $this->originalRequest = $originalRequest;
+       }
 
        /**
         * @param string|null $model Name of the model to query
@@ -46,7 +68,7 @@
                $params['format'] = 'json';
                $url = wfAppendQuery( $url, $params );
                $logger->debug( "Requesting: {$url}" );
-               $req = MWHttpRequest::factory( $url, null, __METHOD__ );
+               $req = MWHttpRequest::factory( $url, 
$this->getMWHttpRequestOptions(), __METHOD__ );
                $status = $req->execute();
                if ( !$status->isOK() ) {
                        throw new RuntimeException( "Failed to make ORES 
request to [{$url}], "
@@ -61,4 +83,7 @@
                return $data;
        }
 
+       protected function getMWHttpRequestOptions() {
+               return $this->originalRequest ? [ 'originalRequest' => 
$this->originalRequest ] : [];
+       }
 }
diff --git a/includes/ApiHooks.php b/includes/ApiHooks.php
index f4768c0..6442bd6 100644
--- a/includes/ApiHooks.php
+++ b/includes/ApiHooks.php
@@ -14,6 +14,7 @@
 use DeferredUpdates;
 use JobQueueGroup;
 use MediaWiki\Logger\LoggerFactory;
+use RequestContext;
 use Title;
 use ResultWrapper;
 use WatchedItem;
@@ -265,8 +266,16 @@
                                $chunks = array_chunk( $revids, 
$wgOresRevisionsPerBatch );
                                $revids = array_shift( $chunks );
                                $title = Title::makeTitle( NS_SPECIAL, 
'Badtitle/API batch score fetch' );
+                               $request = 
RequestContext::getMain()->getRequest();
                                foreach ( array_slice( $chunks, 0, 
$wgOresAPIMaxBatchJobs ) as $batch ) {
-                                       $job = new FetchScoreJob( $title, [ 
'revid' => $batch, 'extra_params' => [] ] );
+                                       $job = new FetchScoreJob( $title, [
+                                               'revid' => $batch,
+                                               'originalRequest' => [
+                                                       'ip' => 
$request->getIP(),
+                                                       'userAgent' => 
$request->getHeader( 'User-Agent' ),
+                                               ],
+                                               'extra_params' => [],
+                                       ] );
                                        JobQueueGroup::singleton()->push( $job 
);
                                }
                        }
diff --git a/includes/FetchScoreJob.php b/includes/FetchScoreJob.php
index f18b255..4b2f2e2 100644
--- a/includes/FetchScoreJob.php
+++ b/includes/FetchScoreJob.php
@@ -10,7 +10,9 @@
 
        /**
         * @param Title $title
-        * @param array $params 'revid' key
+        * @param array $params
+        *   - 'revid': revision ID
+        *   - 'originalRequest': data for Api::setOriginalRequest
         */
        public function __construct( Title $title, array $params ) {
                $expensive = is_array( $params['revid'] );
@@ -50,8 +52,11 @@
                }
 
                $logger->info( 'Fetching scores for revision ' . json_encode( 
$this->params ) );
-               $scores = Scoring::instance()->getScores(
-                       $this->params['revid'], null, 
$this->params['extra_params'] );
+               $scoring = Scoring::instance();
+               if ( isset( $this->params['originalRequest'] ) ) {
+                       $scoring->setOriginalRequest( 
$this->params['originalRequest'] );
+               }
+               $scores = $scoring->getScores( $this->params['revid'], null, 
$this->params['extra_params'] );
                $cache = Cache::instance();
                $success = true;
                $cache->setErrorCallback( function ( $mssg, $revision ) use ( 
&$success, $logger ) {
diff --git a/includes/Hooks.php b/includes/Hooks.php
index ec8d064..d6ed16b 100644
--- a/includes/Hooks.php
+++ b/includes/Hooks.php
@@ -72,8 +72,13 @@
                        $logger->debug( 'Processing edit {revid}', [
                                'revid' => $revid,
                        ] );
+                       $request = RequestContext::getMain()->getRequest();
                        $job = new FetchScoreJob( $rc->getTitle(), [
                                'revid' => $revid,
+                               'originalRequest' => [
+                                       'ip' => $request->getIP(),
+                                       'userAgent' => $request->getHeader( 
'User-Agent' ),
+                               ],
                                'extra_params' => [ 'precache' => 'true' ],
                        ] );
                        JobQueueGroup::singleton()->push( $job );
diff --git a/includes/Scoring.php b/includes/Scoring.php
index e741d37..11b0bec 100644
--- a/includes/Scoring.php
+++ b/includes/Scoring.php
@@ -2,7 +2,11 @@
 
 namespace ORES;
 
+use WebRequest;
+
 class Scoring {
+       /** @var WebRequest|string[] */
+       protected $originalRequest;
 
        /**
         * @param integer|array $revisions Single or multiple revisions
@@ -24,11 +28,23 @@
                        'revids' => implode( '|', (array) $revisions ),
                ];
 
-               $api = new Api();
+               if ( $this->originalRequest ) {
+                       $api = new Api();
+                       $api->setOriginalRequest( $this->originalRequest );
+               } else {
+                       $api = Api::newFromContext();
+               }
                $wireData = $api->request( array_merge( $params, $extra_params 
) );
                return $wireData;
        }
 
+       /**
+        * @param WebRequest|array $originalRequest See the Http::request() 
option with the same name.
+        */
+       public function setOriginalRequest( $originalRequest ) {
+               $this->originalRequest = $originalRequest;
+       }
+
        public static function instance() {
                return new self();
        }
diff --git a/includes/Stats.php b/includes/Stats.php
index 8dc2774..fd01ecb 100644
--- a/includes/Stats.php
+++ b/includes/Stats.php
@@ -182,9 +182,12 @@
                return $config[ 'default' ];
        }
 
+       /**
+        * @return self
+        */
        public static function newFromGlobalState() {
                return new self(
-                       new Api(),
+                       Api::newFromContext(),
                        
MediaWikiServices::getInstance()->getMainWANObjectCache(),
                        LoggerFactory::getInstance( 'ORES' )
                );
diff --git a/maintenance/DumpThresholds.php b/maintenance/DumpThresholds.php
index 05eeb8c..718d378 100644
--- a/maintenance/DumpThresholds.php
+++ b/maintenance/DumpThresholds.php
@@ -46,7 +46,6 @@
                }
                return $modelData['models'];
        }
-
 }
 
 $maintClass = 'ORES\DumpThresholds';

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3cad8e0939033b75dc960a53fbc73a63e39414ca
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ORES
Gerrit-Branch: master
Gerrit-Owner: GergÅ‘ Tisza <[email protected]>

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

Reply via email to