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

Change subject: Decorator to populate the model table if it's not there
......................................................................

Decorator to populate the model table if it's not there

Bug: T184938
Change-Id: Ibcb28666cffbd8763d4f1ab676726bb12599a9ce
---
M includes/ServiceWiring.php
A includes/Storage/PopulatedSqlModelLookup.php
2 files changed, 108 insertions(+), 1 deletion(-)


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

diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php
index 4a741cd..0896585 100644
--- a/includes/ServiceWiring.php
+++ b/includes/ServiceWiring.php
@@ -18,13 +18,17 @@
 
 use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\MediaWikiServices;
+use ORES\Storage\PopulatedSqlModelLookup;
 use ORES\Storage\SqlModelLookup;
 use ORES\Storage\SqlScoreLookup;
 use ORES\Storage\SqlScoreStorage;
 
 return [
        'ORESModelLookup' => function ( MediaWikiServices $services ) {
-               return new SqlModelLookup( $services->getDBLoadBalancer() );
+               return new PopulatedSqlModelLookup(
+                       new SqlModelLookup( $services->getDBLoadBalancer() ),
+                       $services->getService( 'ORESService' )
+               );
        },
 
        'ORESThresholdLookup' => function ( MediaWikiServices $services ) {
diff --git a/includes/Storage/PopulatedSqlModelLookup.php 
b/includes/Storage/PopulatedSqlModelLookup.php
new file mode 100644
index 0000000..7c87851
--- /dev/null
+++ b/includes/Storage/PopulatedSqlModelLookup.php
@@ -0,0 +1,103 @@
+<?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 ORES\ORESService;
+use ORES\ScoreFetcher;
+use RuntimeException;
+
+class PopulatedSqlModelLookup implements ModelLookup {
+
+       private $modelLookup;
+
+       private $ORESService;
+
+       public function __construct(
+               ModelLookup $modelLookup,
+               ORESService $ORESService
+       ) {
+               $this->modelLookup = $modelLookup;
+               $this->ORESService = $ORESService;
+       }
+
+       /**
+        * @see ModelLookup::getModels()
+        *
+        * @return array[]
+        */
+       public function getModels() {
+               $modelData = $this->modelLookup->getModels();
+               if ( $modelData === [] ) {
+                       global $wgOresModels;
+                       $models = array_keys( array_filter( $wgOresModels ) );
+                       if ( $models === [] ) {
+                               return $modelData;
+                       }
+
+                       $this->initializeModels( $models );
+               }
+
+               return $this->modelLookup->getModels();
+       }
+
+       private function initializeModels( $models ) {
+               $wikiId = ORESService::getWikiID();
+               $response = $this->ORESService->request( [] );
+               if ( !isset( $response[$wikiId] ) || empty( 
$response[$wikiId]['models'] ) ) {
+                       throw new RuntimeException( 'Bad response from ORES 
when requesting models: '
+                               . json_encode( $response ) );
+               }
+
+               foreach ( $models as $model ) {
+                       $this->initializeModel( $model, 
$response[$wikiId]['models'] );
+               }
+       }
+
+       private function initializeModel( $model, $modelsData ) {
+               if ( !isset( $modelsData[$model] ) || !isset( 
$modelsData[$model]['version'] ) ) {
+                       return;
+               }
+
+               ScoreFetcher::instance()->updateModelVersion( $model, 
$modelsData[$model]['version'] );
+       }
+
+       /**
+        * @see ModelLookup::getModelId()
+        * @param string $model
+        *
+        * @throws InvalidArgumentException
+        * @return int
+        */
+       public function getModelId( $model ) {
+               $this->getModels();
+               return $this->modelLookup->getModelId( $model );
+       }
+
+       /**
+        * @see ModelLookup::getModelVersion()
+        * @param string $model
+        *
+        * @throws InvalidArgumentException
+        * @return string
+        */
+       public function getModelVersion( $model ) {
+               $this->getModels();
+               return $this->modelLookup->getModelVersion( $model );
+       }
+
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibcb28666cffbd8763d4f1ab676726bb12599a9ce
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

Reply via email to