Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.29% covered (success)
94.29%
33 / 35
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ParserCacheFactory
94.29% covered (success)
94.29%
33 / 35
66.67% covered (warning)
66.67%
2 / 3
6.01
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
 getParserCache
90.00% covered (success)
90.00%
18 / 20
0.00% covered (danger)
0.00%
0 / 1
3.01
 getRevisionOutputCache
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 * @ingroup Cache Parser
6 */
7
8namespace MediaWiki\Parser;
9
10use MediaWiki\Config\ServiceOptions;
11use MediaWiki\HookContainer\HookContainer;
12use MediaWiki\Json\JsonCodec;
13use MediaWiki\MainConfigNames;
14use MediaWiki\Page\WikiPageFactory;
15use MediaWiki\Title\TitleFactory;
16use Psr\Log\LoggerInterface;
17use Wikimedia\ObjectCache\BagOStuff;
18use Wikimedia\ObjectCache\WANObjectCache;
19use Wikimedia\Stats\StatsFactory;
20use Wikimedia\UUID\GlobalIdGenerator;
21
22/**
23 * Returns an instance of the ParserCache by its name.
24 * @since 1.36
25 */
26class ParserCacheFactory {
27
28    /** @var string name of ParserCache for the default parser */
29    public const DEFAULT_NAME = 'pcache';
30
31    /** @var string name of RevisionOutputCache for the default parser */
32    public const DEFAULT_RCACHE_NAME = 'rcache';
33
34    /** @var ParserCache[] */
35    private $parserCaches = [];
36
37    /** @var RevisionOutputCache[] */
38    private $revisionOutputCaches = [];
39
40    /**
41     * @internal
42     */
43    public const CONSTRUCTOR_OPTIONS = [
44        MainConfigNames::CacheEpoch,
45        MainConfigNames::ParserCacheFilterConfig,
46        MainConfigNames::OldRevisionParserCacheExpireTime,
47    ];
48
49    public function __construct(
50        private readonly BagOStuff $parserCacheBackend,
51        private readonly WANObjectCache $revisionOutputCacheBackend,
52        private readonly HookContainer $hookContainer,
53        private readonly JsonCodec $jsonCodec,
54        private readonly StatsFactory $stats,
55        private readonly LoggerInterface $logger,
56        private readonly ServiceOptions $options,
57        private readonly TitleFactory $titleFactory,
58        private readonly WikiPageFactory $wikiPageFactory,
59        private readonly GlobalIdGenerator $globalIdGenerator,
60    ) {
61        $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
62    }
63
64    /**
65     * Get a ParserCache instance by $name.
66     * @param string $name
67     * @return ParserCache
68     */
69    public function getParserCache( string $name ): ParserCache {
70        if ( !isset( $this->parserCaches[$name] ) ) {
71            $this->logger->debug( "Creating ParserCache instance for {$name}" );
72            $cache = new ParserCache(
73                $name,
74                $this->parserCacheBackend,
75                $this->options->get( MainConfigNames::CacheEpoch ),
76                $this->hookContainer,
77                $this->jsonCodec,
78                $this->stats,
79                $this->logger,
80                $this->titleFactory,
81                $this->wikiPageFactory,
82                $this->globalIdGenerator
83            );
84
85            $filterConfig = $this->options->get( MainConfigNames::ParserCacheFilterConfig );
86
87            if ( isset( $filterConfig[$name] ) ) {
88                $filter = new ParserCacheFilter( $filterConfig[$name] );
89                $cache->setFilter( $filter );
90            }
91
92            $this->parserCaches[$name] = $cache;
93        }
94        return $this->parserCaches[$name];
95    }
96
97    /**
98     * Get a RevisionOutputCache instance by $name.
99     * @param string $name
100     * @return RevisionOutputCache
101     */
102    public function getRevisionOutputCache( string $name ): RevisionOutputCache {
103        if ( !isset( $this->revisionOutputCaches[$name] ) ) {
104            $this->logger->debug( "Creating RevisionOutputCache instance for {$name}" );
105            $cache = new RevisionOutputCache(
106                $name,
107                $this->revisionOutputCacheBackend,
108                $this->options->get( MainConfigNames::OldRevisionParserCacheExpireTime ),
109                $this->options->get( MainConfigNames::CacheEpoch ),
110                $this->jsonCodec,
111                $this->stats,
112                $this->logger,
113                $this->globalIdGenerator
114            );
115
116            $this->revisionOutputCaches[$name] = $cache;
117        }
118        return $this->revisionOutputCaches[$name];
119    }
120}