Debian Codegraph @

extensions/CategoryTree/includes/CategoryCache.php

raw symbols SCIP
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\IConnectionProvider;
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 array $cache = [];
34
35 private IConnectionProvider $dbProvider;
36
37 public function __construct(
38 IConnectionProvider $dbProvider
39 ) {
40 $this->dbProvider = $dbProvider;
41 }
42
43 /**
44 * Get a preloaded Category object or null when the Category does not exists. Loaded the Category on demand,
45 * if not in cache, use self::doQuery when requesting a high number of category
46 */
47 public function getCategory( LinkTarget $categoryTarget ): ?Category {
48 if ( $categoryTarget->getNamespace() !== NS_CATEGORY ) {
49 return null;
50 }
51 $categoryDbKey = $categoryTarget->getDBkey();
52
53 if ( !array_key_exists( $categoryDbKey, $this->cache ) ) {
54 $this->doQuery( [ $categoryTarget ] );
55 }
56
57 return $this->cache[$categoryDbKey];
58 }
59
60 /**
61 * Preloads category counts in this cache
62 * @param LinkTarget[] $linkTargets
63 */
64 public function doQuery( array $linkTargets ): void {
65 $categoryDbKeys = [];
66 foreach ( $linkTargets as $linkTarget ) {
67 if ( $linkTarget->getNamespace() !== NS_CATEGORY ) {
68 continue;
69 }
70 $categoryDbKey = $linkTarget->getDBkey();
71 if ( !array_key_exists( $categoryDbKey, $this->cache ) ) {
72 $categoryDbKeys[] = $categoryDbKey;
73 // To cache db misses, also avoid duplicates in the db query
74 $this->cache[$categoryDbKey] = null;
75 }
76 }
77 if ( $categoryDbKeys === [] ) {
78 return;
79 }
80
81 $rows = $this->dbProvider->getReplicaDatabase()
82 ->newSelectQueryBuilder()
83 ->select( [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ] )
84 ->from( 'category' )
85 ->where( [ 'cat_title' => $categoryDbKeys ] )
86 ->caller( __METHOD__ )
87 ->fetchResultSet();
88
89 $this->fillFromQuery( $rows );
90 }
91
92 public function fillFromQuery( IResultWrapper $rows ): void {
93 foreach ( $rows as $row ) {
94 $this->cache[$row->cat_title] = Category::newFromRow( $row );
95 }
96 }
97
98 }