jenkins-bot has submitted this change and it was merged.
Change subject: Encapsulate ORES fetch and store
......................................................................
Encapsulate ORES fetch and store
Also, strip the base URL config variable down to the hostname and
contruct
paths at the point of use.
Bug: T112856
Change-Id: I360606e53a1dc32002b7f61faa909c4ec38ad5f1
---
M README
M extension.json
A includes/Cache.php
M includes/FetchScoreJob.php
A includes/Scoring.php
5 files changed, 122 insertions(+), 62 deletions(-)
Approvals:
Legoktm: Looks good to me, approved
jenkins-bot: Verified
diff --git a/README b/README
index ae1ba25..a8f8091 100644
--- a/README
+++ b/README
@@ -1,7 +1,7 @@
New globals:
-$wgOresBaseUrl - API endpoint pattern for all ORES requests. "$wiki" will be
-substituted with your wiki's ID.
+$wgOresBaseUrl - API endpoint base URL for all ORES requests. The specific
+route will be appended to this URL.
$wgOresModels - Array of models we want to fetch from the server and cache in
the database.
diff --git a/extension.json b/extension.json
index e756ac9..82bea99 100644
--- a/extension.json
+++ b/extension.json
@@ -8,8 +8,10 @@
],
"url": "https://www.mediawiki.org/wiki/Extension:ORES",
"AutoloadClasses": {
+ "ORES\\Cache": "includes/Cache.php",
"ORES\\Hooks": "includes/Hooks.php",
- "ORES\\FetchScoreJob": "includes/FetchScoreJob.php"
+ "ORES\\FetchScoreJob": "includes/FetchScoreJob.php",
+ "ORES\\Scoring": "includes/Scoring.php"
},
"Hooks": {
"LoadExtensionSchemaUpdates": [
@@ -50,7 +52,7 @@
"ORESFetchScoreJob": "ORES\\FetchScoreJob"
},
"config": {
- "OresBaseUrl": "https://ores.wmflabs.org/scores/$wiki/",
+ "OresBaseUrl": "https://ores.wmflabs.org/",
"OresModels": [
"reverted"
],
diff --git a/includes/Cache.php b/includes/Cache.php
new file mode 100644
index 0000000..fa3e00d
--- /dev/null
+++ b/includes/Cache.php
@@ -0,0 +1,62 @@
+<?php
+
+namespace ORES;
+
+use RuntimeException;
+
+class Cache {
+ /**
+ * Save scores to the database
+ *
+ * @param array $scores in the same structure as is returned by ORES.
+ * @param integer $rcid Recent changes ID
+ *
+ * @throws RuntimeException
+ */
+ public function storeScores( $scores, $rcid ) {
+ // Map to database fields.
+ $dbData = array();
+ foreach ( $scores as $revision => $revisionData ) {
+ foreach ( $revisionData as $model => $modelOutputs ) {
+ if ( isset( $modelOutputs['error'] ) ) {
+ throw new RuntimeException( 'Model
contains an error: ' . $modelOutputs['error']['message'] );
+ }
+
+ $prediction = $modelOutputs['prediction'];
+ // Kludge out booleans so we can match
prediction against class name.
+ if ( $prediction === false ) {
+ $prediction = 'false';
+ } elseif ( $prediction === true ) {
+ $prediction = 'true';
+ }
+
+ foreach ( $modelOutputs['probability'] as
$class => $probability ) {
+ $dbData[] = array(
+ 'ores_rc' => $rcid,
+ 'ores_model' => $model,
+ 'ores_model_version' => 0, //
FIXME: waiting for API support
+ 'ores_class' => $class,
+ 'ores_probability' =>
$probability,
+ 'ores_is_predicted' => (
$prediction === $class ),
+ );
+ }
+ }
+ }
+
+ wfGetDB( DB_MASTER )->insert( 'ores_classification', $dbData,
__METHOD__ );
+ }
+
+ public function purge( $model, $version ) {
+ wfGetDb( DB_MASTER )->delete( 'ores_classification',
+ array(
+ 'ores_model' => $model,
+ 'ores_model_version' => $version,
+ ),
+ __METHOD__
+ );
+ }
+
+ public static function instance() {
+ return new self();
+ }
+}
diff --git a/includes/FetchScoreJob.php b/includes/FetchScoreJob.php
index 00898f5..7e9e50c 100644
--- a/includes/FetchScoreJob.php
+++ b/includes/FetchScoreJob.php
@@ -3,9 +3,7 @@
namespace ORES;
use Job;
-use FormatJson;
use Title;
-use MWHttpRequest;
class FetchScoreJob extends Job {
/**
@@ -16,64 +14,10 @@
parent::__construct( 'ORESFetchScoreJob', $title, $params );
}
- private function getUrl() {
- global $wgOresBaseUrl, $wgOresModels;
-
- $url = str_replace( '$wiki', wfWikiID(), $wgOresBaseUrl );
- $params = array(
- 'models' => implode( '|', $wgOresModels ),
- 'revids' => $this->params['revid'],
- );
- return wfAppendQuery( $url, $params );
- }
-
public function run() {
- $url = $this->getUrl();
- $req = MWHttpRequest::factory( $url, null, __METHOD__ );
- $status = $req->execute();
- if ( !$status->isOK() ) {
- wfDebugLog( 'ORES', "No response from ORES server at
$url, "
- . $status->getMessage()->text() );
- return false;
- }
- $json = $req->getContent();
- $wireData = FormatJson::decode( $json, true );
- if ( !$wireData || !empty( $wireData['error'] ) ) {
- wfDebugLog( 'ORES', 'Bad response from ORES server: ' .
$json );
- return false;
- }
+ $scores = Scoring::instance()->getScores(
$this->params['revid'] );
+ Cache::instance()->storeScores( $scores, $this->params['rcid']
);
- // Map from wire format to database fields.
- $dbData = array();
- foreach ( $wireData as $revisionId => $revisionData ) {
- foreach ( $revisionData as $model => $modelOutputs ) {
- if ( isset( $modelOutputs['error'] ) ) {
- wfDebugLog( 'ORES', 'Model output an
error: ' . $modelOutputs['error']['message'] );
- return false;
- }
-
- $prediction = $modelOutputs['prediction'];
- // Kludge out booleans so we can match
prediction against class name.
- if ( $prediction === false ) {
- $prediction = 'false';
- } elseif ( $prediction === true ) {
- $prediction = 'true';
- }
-
- foreach ( $modelOutputs['probability'] as
$class => $probability ) {
- $dbData[] = array(
- 'ores_rev' => $revisionId,
- 'ores_model' => $model,
- 'ores_model_version' => '', //
FIXME: waiting for API support
- 'ores_class' => $class,
- 'ores_probability' =>
$probability,
- 'ores_is_predicted' => (
$prediction === $class ),
- );
- }
- }
- }
-
- wfGetDB( DB_MASTER )->insert( 'ores_classification', $dbData,
__METHOD__ );
return true;
}
}
diff --git a/includes/Scoring.php b/includes/Scoring.php
new file mode 100644
index 0000000..5b801d5
--- /dev/null
+++ b/includes/Scoring.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace ORES;
+
+use FormatJson;
+use MWHttpRequest;
+use RuntimeException;
+
+class Scoring {
+ protected function getScoresUrl( $revisions, $models ) {
+ global $wgOresBaseUrl;
+
+ $url = $wgOresBaseUrl . 'scores/' . wfWikiID();
+ $params = array(
+ 'models' => implode( '|', (array) $models ),
+ 'revids' => implode( '|', (array) $revisions ),
+ );
+ return wfAppendQuery( $url, $params );
+ }
+
+ /**
+ * @param integer|array $revisions Single or multiple revisions
+ * @param string|array|null $models Single or multiple model names. If
+ * left empty, all configured models are queries.
+ * @return array Results in the form returned by ORES
+ * @throws RuntimeException
+ */
+ public function getScores( $revisions, $models = null ) {
+ if ( !$models ) {
+ global $wgOresModels;
+ $models = $wgOresModels;
+ }
+ $url = $this->getScoresUrl( $revisions, $models );
+ $req = MWHttpRequest::factory( $url, null, __METHOD__ );
+ $status = $req->execute();
+ if ( !$status->isOK() ) {
+ throw new RuntimeException( "No response from ORES
server at $url, "
+ . $status->getMessage()->text() );
+ }
+ $json = $req->getContent();
+ $wireData = FormatJson::decode( $json, true );
+ if ( !$wireData || !empty( $wireData['error'] ) ) {
+ throw new RuntimeException( 'Bad response from ORES
server: ' . $json );
+ }
+
+ return $wireData;
+ }
+
+ public static function instance() {
+ return new self();
+ }
+}
--
To view, visit https://gerrit.wikimedia.org/r/239327
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I360606e53a1dc32002b7f61faa909c4ec38ad5f1
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/extensions/ORES
Gerrit-Branch: master
Gerrit-Owner: Awight <[email protected]>
Gerrit-Reviewer: Alex Monk <[email protected]>
Gerrit-Reviewer: Halfak <[email protected]>
Gerrit-Reviewer: He7d3r <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: Paladox <[email protected]>
Gerrit-Reviewer: Yuvipanda <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits