Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
77.14% |
27 / 35 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
PopulatedSqlModelLookup | |
77.14% |
27 / 35 |
|
71.43% |
5 / 7 |
21.87 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getModels | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
4.13 | |||
initializeModels | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
initializeModelsLiftWing | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
initializeModel | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
getModelId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getModelVersion | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * This program is free software: you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation, either version 3 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License |
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
15 | */ |
16 | |
17 | namespace ORES\Services; |
18 | |
19 | use InvalidArgumentException; |
20 | use ORES\ORESService; |
21 | use ORES\Storage\ModelLookup; |
22 | use Psr\Log\LoggerInterface; |
23 | |
24 | class PopulatedSqlModelLookup implements ModelLookup { |
25 | |
26 | private $modelLookup; |
27 | |
28 | private $ORESService; |
29 | |
30 | private $logger; |
31 | private bool $useLiftWing; |
32 | |
33 | public function __construct( |
34 | ModelLookup $modelLookup, |
35 | ORESService $ORESService, |
36 | LoggerInterface $logger, |
37 | bool $useLiftWing |
38 | ) { |
39 | $this->modelLookup = $modelLookup; |
40 | $this->ORESService = $ORESService; |
41 | $this->logger = $logger; |
42 | $this->useLiftWing = $useLiftWing; |
43 | } |
44 | |
45 | /** |
46 | * @see ModelLookup::getModels() |
47 | * |
48 | * @return array[] |
49 | */ |
50 | public function getModels() { |
51 | $modelData = $this->modelLookup->getModels(); |
52 | if ( $modelData === [] ) { |
53 | global $wgOresModels; |
54 | $models = array_keys( array_filter( $wgOresModels ) ); |
55 | if ( $models === [] ) { |
56 | return $modelData; |
57 | } |
58 | |
59 | if ( $this->useLiftWing ) { |
60 | $this->initializeModelsLiftWing( $models ); |
61 | } else { |
62 | $this->initializeModels( $models ); |
63 | } |
64 | $modelData = $this->modelLookup->getModels(); |
65 | } |
66 | |
67 | return $modelData; |
68 | } |
69 | |
70 | private function initializeModels( $models ) { |
71 | $wikiId = ORESService::getWikiID(); |
72 | $response = $this->ORESService->request( [] ); |
73 | if ( !isset( $response[$wikiId] ) || empty( $response[$wikiId]['models'] ) ) { |
74 | $this->logger->error( 'Bad response from ORES when requesting models: ' |
75 | . json_encode( $response ) ); |
76 | return; |
77 | } |
78 | |
79 | foreach ( $models as $model ) { |
80 | $this->initializeModel( $model, $response[$wikiId]['models'] ); |
81 | } |
82 | } |
83 | |
84 | private function initializeModelsLiftWing( $models ) { |
85 | global $wgOresModelVersions; |
86 | if ( !isset( $wgOresModelVersions ) || empty( $wgOresModelVersions['models'] ) ) { |
87 | $this->logger->error( 'Bad response from ORES when requesting models: ' |
88 | . json_encode( $wgOresModelVersions ) ); |
89 | return; |
90 | } |
91 | |
92 | foreach ( $models as $model ) { |
93 | $this->initializeModel( $model, $wgOresModelVersions['models'] ); |
94 | } |
95 | } |
96 | |
97 | private function initializeModel( $model, $modelsData ) { |
98 | if ( !isset( $modelsData[$model] ) || !isset( $modelsData[$model]['version'] ) ) { |
99 | return; |
100 | } |
101 | |
102 | ScoreFetcher::instance()->updateModelVersion( $model, $modelsData[$model]['version'] ); |
103 | } |
104 | |
105 | /** |
106 | * @see ModelLookup::getModelId() |
107 | * @param string $model |
108 | * |
109 | * @throws InvalidArgumentException |
110 | * @return int |
111 | */ |
112 | public function getModelId( $model ) { |
113 | $this->getModels(); |
114 | return $this->modelLookup->getModelId( $model ); |
115 | } |
116 | |
117 | /** |
118 | * @see ModelLookup::getModelVersion() |
119 | * @param string $model |
120 | * |
121 | * @throws InvalidArgumentException |
122 | * @return string |
123 | */ |
124 | public function getModelVersion( $model ) { |
125 | $this->getModels(); |
126 | return $this->modelLookup->getModelVersion( $model ); |
127 | } |
128 | } |