Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.89% |
24 / 27 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
CategoryCache | |
88.89% |
24 / 27 |
|
50.00% |
2 / 4 |
11.17 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCategory | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
doQuery | |
88.89% |
16 / 18 |
|
0.00% |
0 / 1 |
5.03 | |||
fillFromQuery | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 |
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 2 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 along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\Extension\CategoryTree; |
22 | |
23 | use MediaWiki\Category\Category; |
24 | use MediaWiki\Linker\LinkTarget; |
25 | use Wikimedia\Rdbms\ILoadBalancer; |
26 | use Wikimedia\Rdbms\IResultWrapper; |
27 | |
28 | /** |
29 | * Caches Category::class objects |
30 | */ |
31 | class CategoryCache { |
32 | /** @var (?Category)[] Keys are category database names, values are either a Category object or null */ |
33 | private $cache = []; |
34 | |
35 | /** @var ILoadBalancer */ |
36 | private $loadBalancer; |
37 | |
38 | /** |
39 | * @param ILoadBalancer $loadBalancer |
40 | */ |
41 | public function __construct( |
42 | ILoadBalancer $loadBalancer |
43 | ) { |
44 | $this->loadBalancer = $loadBalancer; |
45 | } |
46 | |
47 | /** |
48 | * Get a preloaded Category object or null when the Category does not exists. Loaded the Category on demand, |
49 | * if not in cache, use self::doQuery when requesting a high number of category |
50 | * @param LinkTarget $categoryTarget |
51 | * @return ?Category |
52 | */ |
53 | public function getCategory( LinkTarget $categoryTarget ): ?Category { |
54 | if ( $categoryTarget->getNamespace() !== NS_CATEGORY ) { |
55 | return null; |
56 | } |
57 | $categoryDbKey = $categoryTarget->getDBkey(); |
58 | |
59 | if ( !array_key_exists( $categoryDbKey, $this->cache ) ) { |
60 | $this->doQuery( [ $categoryTarget ] ); |
61 | } |
62 | |
63 | return $this->cache[$categoryDbKey]; |
64 | } |
65 | |
66 | /** |
67 | * Preloads category counts in this cache |
68 | * @param LinkTarget[] $linkTargets |
69 | */ |
70 | public function doQuery( array $linkTargets ): void { |
71 | $categoryDbKeys = []; |
72 | foreach ( $linkTargets as $linkTarget ) { |
73 | if ( $linkTarget->getNamespace() !== NS_CATEGORY ) { |
74 | continue; |
75 | } |
76 | $categoryDbKey = $linkTarget->getDBkey(); |
77 | if ( !array_key_exists( $categoryDbKey, $this->cache ) ) { |
78 | $categoryDbKeys[] = $categoryDbKey; |
79 | // To cache db misses, also avoid duplicates in the db query |
80 | $this->cache[$categoryDbKey] = null; |
81 | } |
82 | } |
83 | if ( $categoryDbKeys === [] ) { |
84 | return; |
85 | } |
86 | |
87 | $rows = $this->loadBalancer->getConnection( ILoadBalancer::DB_REPLICA ) |
88 | ->newSelectQueryBuilder() |
89 | ->select( [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ] ) |
90 | ->from( 'category' ) |
91 | ->where( [ 'cat_title' => $categoryDbKeys ] ) |
92 | ->caller( __METHOD__ ) |
93 | ->fetchResultSet(); |
94 | |
95 | $this->fillFromQuery( $rows ); |
96 | } |
97 | |
98 | /** |
99 | * @param IResultWrapper $rows |
100 | */ |
101 | public function fillFromQuery( IResultWrapper $rows ) { |
102 | foreach ( $rows as $row ) { |
103 | $this->cache[$row->cat_title] = Category::newFromRow( $row ); |
104 | } |
105 | } |
106 | |
107 | } |