| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 30 |
|
| 31 |
class CategoryCache { |
| 32 |
|
| 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 |
|
| 45 |
|
| 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 |
|
| 62 |
|
| 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 |
|
| 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 |
} |