Ladsgroup has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/393620 )
Change subject: Introduce ModelLookup interface and its SQL implementation ...................................................................... Introduce ModelLookup interface and its SQL implementation Bug: T181334 Change-Id: Ibdd201b1861c387f23f3685a77fb9e6772958da6 --- M extension.json A includes/Storage/Storage/ModelLookup.php A includes/Storage/Storage/SqlModelLookup.php A tests/phpunit/includes/Storage/Storage/SqlModelLookupTest.php 4 files changed, 227 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ORES refs/changes/20/393620/1 diff --git a/extension.json b/extension.json index 63f226b..b846f8a 100644 --- a/extension.json +++ b/extension.json @@ -20,6 +20,8 @@ "ORES\\Range": "includes/Range.php", "ORES\\Scoring": "includes/Scoring.php", "ORES\\Stats": "includes/Stats.php", + "ORES\\Storage\\ModelLookup": "includes/Storage/ModelLookup.php", + "ORES\\Storage\\SqlModelLookup": "includes/Storage/SqlModelLookup.php", "ORES\\ApiQueryORES": "includes/ApiQueryORES.php", "ORES\\WatchedItemQueryServiceExtension": "includes/WatchedItemQueryServiceExtension.php" }, diff --git a/includes/Storage/Storage/ModelLookup.php b/includes/Storage/Storage/ModelLookup.php new file mode 100644 index 0000000..822f2d7 --- /dev/null +++ b/includes/Storage/Storage/ModelLookup.php @@ -0,0 +1,37 @@ +<?php + +namespace ORES\Storage; + +use InvalidArgumentException; + +/** + * Service interface for retrieving model data from storage. + * + * @license GPL-2.0+ + */ +interface ModelLookup { + + /** + * @param string $model + * + * @throws InvalidArgumentException + * @return int cached id of last seen version + */ + public function getModelId( $model ); + + /** + * @param string $model + * + * @throws InvalidArgumentException + * @return string cached version of last seen version + */ + public function getModelVersion( $model ); + + /** + * Returns models and their data from storage + * + * @return string[] + */ + public function getModels(); + +} diff --git a/includes/Storage/Storage/SqlModelLookup.php b/includes/Storage/Storage/SqlModelLookup.php new file mode 100644 index 0000000..4b4efd1 --- /dev/null +++ b/includes/Storage/Storage/SqlModelLookup.php @@ -0,0 +1,95 @@ +<?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\Storage; + +use InvalidArgumentException; +use Wikimedia\Rdbms\LoadBalancer; + +class SqlModelLookup implements ModelLookup { + + private $loadBalancer; + + private $modelData = null; + + public function __construct( LoadBalancer $loadBalancer ) { + $this->loadBalancer = $loadBalancer; + } + + /** + * @see ModelLookup::getModelId() + * @param string $model + * + * @throws InvalidArgumentException + * @return int cached id of last seen version + */ + public function getModelId( $model ) { + $modelData = $this->getModelData(); + if ( !array_key_exists( $model, $modelData ) ) { + throw new InvalidArgumentException( "No model available for [{$model}]" ); + } + + return $modelData[$model]['id']; + } + + /** + * @see ModelLookup::getModelVersion() + * @param string $model + * + * @throws InvalidArgumentException + * @return string cached version of last seen version + */ + public function getModelVersion( $model ) { + $modelData = $this->getModelData(); + if ( !array_key_exists( $model, $modelData ) ) { + throw new InvalidArgumentException( "No model available for [{$model}]" ); + } + + return $modelData[$model]['version']; + } + + /** + * @see ModelLookup::getModels() + * + * Returns models and their data from storage + * + * @return string[] + */ + public function getModels() { + return $this->getModelData(); + } + + private function getModelData() { + if ( $this->modelData === null ) { + $this->modelData = []; + $result = $this->loadBalancer->getConnection( DB_REPLICA )->select( + 'ores_model', + [ 'oresm_id', 'oresm_name', 'oresm_version' ], + [ 'oresm_is_current' => 1 ], + __METHOD__ + ); + foreach ( $result as $row ) { + $this->modelData[$row->oresm_name] = [ + 'id' => (int)$row->oresm_id, + 'version' => $row->oresm_version + ]; + } + } + + return $this->modelData; + } + +} diff --git a/tests/phpunit/includes/Storage/Storage/SqlModelLookupTest.php b/tests/phpunit/includes/Storage/Storage/SqlModelLookupTest.php new file mode 100644 index 0000000..31f7bf8 --- /dev/null +++ b/tests/phpunit/includes/Storage/Storage/SqlModelLookupTest.php @@ -0,0 +1,93 @@ +<?php + +namespace ORES\Tests; + +use InvalidArgumentException; +use MediaWiki\MediaWikiServices; +use MediaWikiLangTestCase; +use ORES\Storage\SqlModelLookup; + +/** + * @group ORES + * @group Database + * @covers ORES\Storage\SqlModelLookup + */ +class SqlModelLookupTest extends MediaWikiLangTestCase { + + const DAMAGING_OLD = 1; + const REVERTED = 2; + const DAMAGING = 3; + + /** + * @var SqlModelLookup + */ + protected $storage; + + protected function setUp() { + parent::setUp(); + + $this->tablesUsed[] = 'ores_model'; + self::insertModelData(); + $this->storage = new SqlModelLookup( MediaWikiServices::getInstance()->getDBLoadBalancer() ); + } + + public static function insertModelData() { + $db = \wfGetDB( DB_MASTER ); + $dump = [ + [ + 'oresm_id' => self::DAMAGING, + 'oresm_name' => 'damaging', + 'oresm_version' => '0.0.2', + 'oresm_is_current' => true + ], + [ + 'oresm_id' => self::REVERTED, + 'oresm_name' => 'reverted', + 'oresm_version' => '0.0.1', + 'oresm_is_current' => true + ], + [ + 'oresm_id' => self::DAMAGING_OLD, + 'oresm_name' => 'damaging', + 'oresm_version' => '0.0.1', + 'oresm_is_current' => false + ], + ]; + + $db->delete( 'ores_model', '*' ); + + foreach ( $dump as $row ) { + $db->insert( 'ores_model', $row ); + } + } + + public function testGetModels() { + $models = $this->storage->getModels(); + $expected = [ + 'reverted' => [ 'id' => self::REVERTED, 'version' => '0.0.1' ], + 'damaging' => [ 'id' => self::DAMAGING, 'version' => '0.0.2' ] + ]; + $this->assertEquals( $expected, $models ); + } + + public function testGetModelId() { + $this->assertEquals( self::REVERTED, $this->storage->getModelId( 'reverted' ) ); + $this->assertEquals( self::DAMAGING, $this->storage->getModelId( 'damaging' ) ); + } + + public function testGetInvalidModelId() { + $this->setExpectedException( InvalidArgumentException::class ); + $this->storage->getModelId( 'foo' ); + } + + public function testGetModelVersion() { + $this->assertEquals( '0.0.1', $this->storage->getModelVersion( 'reverted' ) ); + $this->assertEquals( '0.0.2', $this->storage->getModelVersion( 'damaging' ) ); + } + + public function testGetInvalidModelVersion() { + $this->setExpectedException( InvalidArgumentException::class ); + $this->storage->getModelVersion( 'foo' ); + } + +} -- To view, visit https://gerrit.wikimedia.org/r/393620 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ibdd201b1861c387f23f3685a77fb9e6772958da6 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