Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
IndexQualityStatsLookup | |
0.00% |
0 / 18 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isIndexTitleInCache | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getStatsForIndexTitle | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
fetchStatsForIndexTitle | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace ProofreadPage\Index; |
4 | |
5 | use MediaWiki\Title\Title; |
6 | use Wikimedia\Rdbms\ILoadBalancer; |
7 | |
8 | /** |
9 | * @license GPL-2.0-or-later |
10 | */ |
11 | class IndexQualityStatsLookup { |
12 | |
13 | /** @var ILoadBalancer */ |
14 | private $loadBalancer; |
15 | |
16 | /** @var PagesQualityStats[] */ |
17 | private $cache = []; |
18 | |
19 | /** |
20 | * @param ILoadBalancer $loadBalancer |
21 | */ |
22 | public function __construct( ILoadBalancer $loadBalancer ) { |
23 | $this->loadBalancer = $loadBalancer; |
24 | } |
25 | |
26 | /** |
27 | * Report if the given index page's stats are cached already |
28 | * @param Title $indexTitle the title of an index page |
29 | * @return bool true if the stat for this index are already cached |
30 | */ |
31 | public function isIndexTitleInCache( Title $indexTitle ): bool { |
32 | return array_key_exists( $indexTitle->getPrefixedDBkey(), $this->cache ); |
33 | } |
34 | |
35 | /** |
36 | * @param Title $indexTitle |
37 | * @return PagesQualityStats |
38 | */ |
39 | public function getStatsForIndexTitle( Title $indexTitle ): PagesQualityStats { |
40 | $cacheKey = $indexTitle->getPrefixedDBkey(); |
41 | if ( !array_key_exists( $cacheKey, $this->cache ) ) { |
42 | $this->cache[$cacheKey] = $this->fetchStatsForIndexTitle( $indexTitle ); |
43 | } |
44 | return $this->cache[$cacheKey]; |
45 | } |
46 | |
47 | /** |
48 | * @param Title $indexTitle |
49 | * @return PagesQualityStats |
50 | */ |
51 | private function fetchStatsForIndexTitle( Title $indexTitle ): PagesQualityStats { |
52 | $row = $this->loadBalancer->getConnection( ILoadBalancer::DB_REPLICA )->selectRow( |
53 | [ 'pr_index' ], |
54 | [ 'pr_count', 'pr_q0', 'pr_q1', 'pr_q2', 'pr_q3', 'pr_q4' ], |
55 | [ 'pr_page_id' => $indexTitle->getArticleID() ], |
56 | __METHOD__ |
57 | ); |
58 | if ( !$row ) { |
59 | return new PagesQualityStats( 0, [] ); |
60 | } |
61 | return new PagesQualityStats( |
62 | $row->pr_count, |
63 | [ $row->pr_q0, $row->pr_q1, $row->pr_q2, $row->pr_q3, $row->pr_q4 ] |
64 | ); |
65 | } |
66 | } |