Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.66% covered (warning)
89.66%
26 / 29
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CategoryCache
89.66% covered (warning)
89.66%
26 / 29
50.00% covered (danger)
50.00%
2 / 4
12.16
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCategory
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 doQuery
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
5.03
 fillFromQuery
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
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
21namespace MediaWiki\Extension\CategoryTree;
22
23use MediaWiki\Category\Category;
24use MediaWiki\Linker\LinkTarget;
25use Wikimedia\Rdbms\IConnectionProvider;
26use Wikimedia\Rdbms\IResultWrapper;
27
28/**
29 * Caches Category::class objects
30 */
31class CategoryCache {
32    /** @var array<string,?Category> Keys are category database names, values are either a Category object or null */
33    private array $cache = [];
34
35    public function __construct(
36        private readonly IConnectionProvider $dbProvider,
37    ) {
38    }
39
40    /**
41     * Get a preloaded Category object or null when the Category does not exists. Loaded the Category on demand,
42     * if not in cache, use self::doQuery when requesting a high number of category
43     */
44    public function getCategory( LinkTarget $categoryTarget ): ?Category {
45        if ( $categoryTarget->getNamespace() !== NS_CATEGORY ) {
46            return null;
47        }
48        $categoryDbKey = $categoryTarget->getDBkey();
49
50        if ( !array_key_exists( $categoryDbKey, $this->cache ) ) {
51            $this->doQuery( [ $categoryTarget ] );
52        }
53
54        return $this->cache[$categoryDbKey];
55    }
56
57    /**
58     * Preloads category counts in this cache
59     * @param LinkTarget[] $linkTargets
60     */
61    public function doQuery( array $linkTargets ): void {
62        $categoryDbKeys = [];
63        foreach ( $linkTargets as $linkTarget ) {
64            if ( $linkTarget->getNamespace() !== NS_CATEGORY ) {
65                continue;
66            }
67            $categoryDbKey = $linkTarget->getDBkey();
68            if ( !array_key_exists( $categoryDbKey, $this->cache ) ) {
69                $categoryDbKeys[] = $categoryDbKey;
70                // To cache db misses, also avoid duplicates in the db query
71                $this->cache[$categoryDbKey] = null;
72            }
73        }
74        if ( $categoryDbKeys === [] ) {
75            return;
76        }
77
78        $rows = $this->dbProvider->getReplicaDatabase()
79            ->newSelectQueryBuilder()
80            ->select( [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ] )
81            ->from( 'category' )
82            ->where( [ 'cat_title' => $categoryDbKeys ] )
83            ->caller( __METHOD__ )
84            ->fetchResultSet();
85
86        $this->fillFromQuery( $rows );
87    }
88
89    public function fillFromQuery( IResultWrapper $rows ): void {
90        foreach ( $rows as $row ) {
91            $category = Category::newFromRow( $row );
92            if ( $category ) {
93                $this->cache[$row->cat_title] = $category;
94            }
95        }
96    }
97
98}