Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.56% covered (success)
95.56%
43 / 45
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ParserCacheFactory
95.56% covered (success)
95.56%
43 / 45
66.67% covered (warning)
66.67%
2 / 3
6
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
11 / 11
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 * 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 * @ingroup Cache Parser
20 */
21
22namespace MediaWiki\Parser;
23
24use MediaWiki\Config\ServiceOptions;
25use MediaWiki\HookContainer\HookContainer;
26use MediaWiki\Json\JsonCodec;
27use MediaWiki\MainConfigNames;
28use MediaWiki\Page\WikiPageFactory;
29use MediaWiki\Title\TitleFactory;
30use Psr\Log\LoggerInterface;
31use Wikimedia\ObjectCache\BagOStuff;
32use Wikimedia\ObjectCache\WANObjectCache;
33use Wikimedia\Stats\StatsFactory;
34use Wikimedia\UUID\GlobalIdGenerator;
35
36/**
37 * Returns an instance of the ParserCache by its name.
38 * @since 1.36
39 * @package MediaWiki\Parser
40 */
41class ParserCacheFactory {
42
43    /** @var string name of ParserCache for the default parser */
44    public const DEFAULT_NAME = 'pcache';
45
46    /** @var string name of RevisionOutputCache for the default parser */
47    public const DEFAULT_RCACHE_NAME = 'rcache';
48
49    /** @var BagOStuff */
50    private $parserCacheBackend;
51
52    /** @var WANObjectCache */
53    private $revisionOutputCacheBackend;
54
55    /** @var HookContainer */
56    private $hookContainer;
57
58    /** @var JsonCodec */
59    private $jsonCodec;
60
61    /** @var StatsFactory */
62    private $stats;
63
64    /** @var LoggerInterface */
65    private $logger;
66
67    /** @var TitleFactory */
68    private $titleFactory;
69
70    /** @var WikiPageFactory */
71    private $wikiPageFactory;
72
73    private GlobalIdGenerator $globalIdGenerator;
74
75    /** @var ParserCache[] */
76    private $parserCaches = [];
77
78    /** @var RevisionOutputCache[] */
79    private $revisionOutputCaches = [];
80
81    /** @var ServiceOptions */
82    private $options;
83
84    /**
85     * @internal
86     */
87    public const CONSTRUCTOR_OPTIONS = [
88        MainConfigNames::CacheEpoch,
89        MainConfigNames::ParserCacheFilterConfig,
90        MainConfigNames::OldRevisionParserCacheExpireTime,
91    ];
92
93    /**
94     * @param BagOStuff $parserCacheBackend
95     * @param WANObjectCache $revisionOutputCacheBackend
96     * @param HookContainer $hookContainer
97     * @param JsonCodec $jsonCodec
98     * @param StatsFactory $stats
99     * @param LoggerInterface $logger
100     * @param ServiceOptions $options
101     * @param TitleFactory $titleFactory
102     * @param WikiPageFactory $wikiPageFactory
103     * @param GlobalIdGenerator $globalIdGenerator
104     */
105    public function __construct(
106        BagOStuff $parserCacheBackend,
107        WANObjectCache $revisionOutputCacheBackend,
108        HookContainer $hookContainer,
109        JsonCodec $jsonCodec,
110        StatsFactory $stats,
111        LoggerInterface $logger,
112        ServiceOptions $options,
113        TitleFactory $titleFactory,
114        WikiPageFactory $wikiPageFactory,
115        GlobalIdGenerator $globalIdGenerator
116    ) {
117        $this->parserCacheBackend = $parserCacheBackend;
118        $this->revisionOutputCacheBackend = $revisionOutputCacheBackend;
119        $this->hookContainer = $hookContainer;
120        $this->jsonCodec = $jsonCodec;
121        $this->stats = $stats;
122        $this->logger = $logger;
123
124        $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
125        $this->options = $options;
126        $this->titleFactory = $titleFactory;
127        $this->wikiPageFactory = $wikiPageFactory;
128        $this->globalIdGenerator = $globalIdGenerator;
129    }
130
131    /**
132     * Get a ParserCache instance by $name.
133     * @param string $name
134     * @return ParserCache
135     */
136    public function getParserCache( string $name ): ParserCache {
137        if ( !isset( $this->parserCaches[$name] ) ) {
138            $this->logger->debug( "Creating ParserCache instance for {$name}" );
139            $cache = new ParserCache(
140                $name,
141                $this->parserCacheBackend,
142                $this->options->get( MainConfigNames::CacheEpoch ),
143                $this->hookContainer,
144                $this->jsonCodec,
145                $this->stats,
146                $this->logger,
147                $this->titleFactory,
148                $this->wikiPageFactory,
149                $this->globalIdGenerator
150            );
151
152            $filterConfig = $this->options->get( MainConfigNames::ParserCacheFilterConfig );
153
154            if ( isset( $filterConfig[$name] ) ) {
155                $filter = new ParserCacheFilter( $filterConfig[$name] );
156                $cache->setFilter( $filter );
157            }
158
159            $this->parserCaches[$name] = $cache;
160        }
161        return $this->parserCaches[$name];
162    }
163
164    /**
165     * Get a RevisionOutputCache instance by $name.
166     * @param string $name
167     * @return RevisionOutputCache
168     */
169    public function getRevisionOutputCache( string $name ): RevisionOutputCache {
170        if ( !isset( $this->revisionOutputCaches[$name] ) ) {
171            $this->logger->debug( "Creating RevisionOutputCache instance for {$name}" );
172            $cache = new RevisionOutputCache(
173                $name,
174                $this->revisionOutputCacheBackend,
175                $this->options->get( MainConfigNames::OldRevisionParserCacheExpireTime ),
176                $this->options->get( MainConfigNames::CacheEpoch ),
177                $this->jsonCodec,
178                $this->stats,
179                $this->logger,
180                $this->globalIdGenerator
181            );
182
183            $this->revisionOutputCaches[$name] = $cache;
184        }
185        return $this->revisionOutputCaches[$name];
186    }
187}