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