Ladsgroup has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/401824 )
Change subject: Rename and cleanup Scoring.php ...................................................................... Rename and cleanup Scoring.php Bug: T184142 Change-Id: Ic756af5f7978e8fa8db464dbd17698b6ff15cd2f --- M includes/FetchScoreJob.php M includes/Hooks.php M includes/Hooks/ApiHooksHandler.php M includes/Parser/ScoreParser.php R includes/ScoreFetcher.php A includes/ScoreLookup.php M maintenance/PopulateDatabase.php R tests/phpunit/includes/ScoreFetcherTest.php 8 files changed, 80 insertions(+), 38 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ORES refs/changes/24/401824/1 diff --git a/includes/FetchScoreJob.php b/includes/FetchScoreJob.php index 83e831d..6bdc88b 100644 --- a/includes/FetchScoreJob.php +++ b/includes/FetchScoreJob.php @@ -68,7 +68,7 @@ } $logger->info( 'Fetching scores for revision ' . json_encode( $this->params ) ); - $scoring = Scoring::instance(); + $scoring = ScoreFetcher::instance(); if ( isset( $this->params['originalRequest'] ) ) { $scoring->setOriginalRequest( $this->params['originalRequest'] ); } @@ -77,14 +77,14 @@ } else { $models = null; } - $scores = $scoring->getScores( $this->params['revid'], $models, $this->params['extra_params'] ); + $scores = $scoring->getScores( $this->params['revid'], $models, $this->params['precache'] ); $scoreStorage = MediaWikiServices::getInstance()->getService( 'ORESScoreStorage' ); $success = true; $scoreStorage->storeScores( $scores, function ( $mssg, $revision ) use ( &$success, $logger ) { - $logger->warning( "Scoring errored for $revision: $mssg\n" ); + $logger->warning( "ScoreFetcher errored for $revision: $mssg\n" ); $success = false; } ); diff --git a/includes/Hooks.php b/includes/Hooks.php index 9e4d751..f0161cf 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -102,7 +102,7 @@ 'userAgent' => $request->getHeader( 'User-Agent' ), ], 'models' => $models, - 'extra_params' => [ 'precache' => 'true' ], + 'precache' => true, ] ); JobQueueGroup::singleton()->push( $job ); $logger->debug( 'Job pushed for {revid}', [ diff --git a/includes/Hooks/ApiHooksHandler.php b/includes/Hooks/ApiHooksHandler.php index a1ea636..cc43094 100644 --- a/includes/Hooks/ApiHooksHandler.php +++ b/includes/Hooks/ApiHooksHandler.php @@ -35,7 +35,7 @@ use ORES\FetchScoreJob; use ORES\Hooks; use ORES\Parser\ScoreParser; -use ORES\Scoring; +use ORES\ScoreFetcher; use ORES\WatchedItemQueryServiceExtension; use RequestContext; use Title; @@ -322,7 +322,7 @@ } } - $loadedScores = Scoring::instance()->getScores( $revids ); + $loadedScores = ScoreFetcher::instance()->getScores( $revids ); // Filter loaded scores to store cacheable ones $cacheableScores = array_intersect_key( $loadedScores, array_flip( $cacheableRevids ) ); @@ -361,7 +361,7 @@ } catch ( InvalidArgumentException $exception ) { $logger = LoggerFactory::getInstance( 'ORES' ); $mssg = $exception->getMessage(); - $logger->info( "Scoring errored for $revid: $mssg\n" ); + $logger->info( "ScoreFetcher errored for $revid: $mssg\n" ); return []; } $scores = []; diff --git a/includes/Parser/ScoreParser.php b/includes/Parser/ScoreParser.php index 7075d2a..65e4597 100644 --- a/includes/Parser/ScoreParser.php +++ b/includes/Parser/ScoreParser.php @@ -37,11 +37,11 @@ } /** - * Convert data returned by Scoring::getScores() into ores_classification rows + * Convert data returned by ScoreFetcher::getScores() into ores_classification rows * * @note No row is generated for class 0 * @param int $revision Revision being processed - * @param array $revisionData Data returned by Scoring::getScores() for the revision. + * @param array $revisionData Data returned by ScoreFetcher::getScores() for the revision. * * @return array[] * @throws RuntimeException diff --git a/includes/Scoring.php b/includes/ScoreFetcher.php similarity index 77% rename from includes/Scoring.php rename to includes/ScoreFetcher.php index 34e4a59..fa3bf67 100644 --- a/includes/Scoring.php +++ b/includes/ScoreFetcher.php @@ -19,20 +19,15 @@ use MediaWiki\MediaWikiServices; use WebRequest; -class Scoring { +class ScoreFetcher implements ScoreLookup { + /** @var WebRequest|string[]|null */ private $originalRequest; /** - * @param int|array $revisions Single or multiple revisions - * @param string|array|null $models Single or multiple model names. If - * left empty, all configured models are queries. - * @param array $extra_params to be passed to ORES endpoint - * - * @return array Results in the form returned by ORES - * @throws \RuntimeException + * @see ScoreLookup::getScores() */ - public function getScores( $revisions, $models = null, array $extra_params = [] ) { + public function getScores( $revisions, $models = null, $precache = false ) { if ( !$models ) { global $wgOresModels; $models = array_keys( array_filter( $wgOresModels ) ); @@ -42,6 +37,9 @@ 'models' => implode( '|', (array)$models ), 'revids' => implode( '|', (array)$revisions ), ]; + if ( $precache === true ) { + $params['precache'] = true; + } if ( $this->originalRequest === null ) { $api = Api::newFromContext(); @@ -49,22 +47,27 @@ $api = new Api(); $api->setOriginalRequest( $this->originalRequest ); } - $wireData = $api->request( array_merge( $params, $extra_params ) ); - // Dig down to the scores. + $wireData = $api->request( $params ); + $wikiId = Api::getWikiID(); if ( array_key_exists( 'models', $wireData[$wikiId] ) ) { - foreach ( $wireData[$wikiId]['models'] as $model => $modelOutputs ) { - $responseVersion = $this->checkModelVersion( $model, $modelOutputs ); - if ( $responseVersion !== null ) { - $this->updateModelVersion( $model, $responseVersion ); - } - } - + $this->checkAndUpdateModels( $wireData[$wikiId]['models'] ); } - $wireData = $wireData[$wikiId]['scores']; - return $wireData; + return $wireData[$wikiId]['scores']; + } + + /** + * @param array $modelData Model information returned by the API + */ + private function checkAndUpdateModels( array $modelData ) { + foreach ( $modelData as $model => $modelOutputs ) { + $responseVersion = $this->checkModelVersion( $model, $modelOutputs ); + if ( $responseVersion !== null ) { + $this->updateModelVersion( $model, $responseVersion ); + } + } } /** diff --git a/includes/ScoreLookup.php b/includes/ScoreLookup.php new file mode 100644 index 0000000..557f2e6 --- /dev/null +++ b/includes/ScoreLookup.php @@ -0,0 +1,39 @@ +<?php +/** + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace ORES; + +use RuntimeException; + +/** + * Service interface for retrieving score data from storage or API. + * + * @license GPL-2.0+ + */ +interface ScoreLookup { + + /** + * @param int|array $revisions Single or multiple revisions + * @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 + * + * @return array Results in the form returned by ORES API + * @throws RuntimeException + */ + public function getScores( $revisions, $models = null, $precache = false ); + +} diff --git a/maintenance/PopulateDatabase.php b/maintenance/PopulateDatabase.php index af9a481..5d40017 100644 --- a/maintenance/PopulateDatabase.php +++ b/maintenance/PopulateDatabase.php @@ -43,7 +43,7 @@ public function execute() { global $wgOresExcludeBots, $wgOresRevisionsPerBatch; - $scoring = Scoring::instance(); + $scoring = ScoreFetcher::instance(); /** @var ScoreStorage $scoreStorage */ $scoreStorage = MediaWikiServices::getInstance()->getService( 'ORESScoreStorage' ); $this->batchSize = $this->getOption( 'batch', 5000 ); @@ -104,10 +104,10 @@ * Process several edits and store the scores in the database * * @param array $revs array of revision ids - * @param Scoring $scoring + * @param ScoreFetcher $scoring * @param ScoreStorage $scoreStorage service to store scores in persistence layer */ - private function processScores( array $revs, Scoring $scoring, ScoreStorage $scoreStorage ) { + private function processScores( array $revs, ScoreFetcher $scoring, ScoreStorage $scoreStorage ) { $size = count( $revs ); $this->output( "Processing $size revisions\n" ); @@ -115,7 +115,7 @@ $scoreStorage->storeScores( $scores, function ( $mssg, $revision ) { - $this->output( "Scoring errored for $revision: $mssg\n" ); + $this->output( "ScoreFetcher errored for $revision: $mssg\n" ); } ); } diff --git a/tests/phpunit/includes/ScoringTest.php b/tests/phpunit/includes/ScoreFetcherTest.php similarity index 91% rename from tests/phpunit/includes/ScoringTest.php rename to tests/phpunit/includes/ScoreFetcherTest.php index cb600ff..84d8d58 100644 --- a/tests/phpunit/includes/ScoringTest.php +++ b/tests/phpunit/includes/ScoreFetcherTest.php @@ -2,15 +2,15 @@ namespace ORES\Tests; -use ORES\Scoring; +use ORES\ScoreFetcher; use ORES\Storage\HashModelLookup; /** * @group ORES * @group Database - * @covers ORES\Scoring + * @covers ORES\ScoreFetcher */ -class ScoringTest extends \MediaWikiTestCase { +class ScoreFetcherTest extends \MediaWikiTestCase { const REVERTED = 2; const DAMAGING = 3; @@ -41,7 +41,7 @@ * @dataProvider provideTestCheckModelVersion */ public function testCheckModelVersion( $expected, $model, array $response ) { - $scoring = Scoring::instance(); + $scoring = ScoreFetcher::instance(); $this->assertSame( $expected, $scoring->checkModelVersion( $model, $response ) ); } @@ -56,7 +56,7 @@ ], __METHOD__ ); - $scoring = Scoring::instance(); + $scoring = ScoreFetcher::instance(); $scoring->updateModelVersion( 'damaging', '0.0.4' ); $res = wfGetDB( DB_REPLICA )->select( -- To view, visit https://gerrit.wikimedia.org/r/401824 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ic756af5f7978e8fa8db464dbd17698b6ff15cd2f Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/ORES Gerrit-Branch: master Gerrit-Owner: Ladsgroup <ladsgr...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits