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.
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.
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
19 * @ingroup Cache Parser
22 namespace MediaWiki\Parser
;
24 use MediaWiki\Config\ServiceOptions
;
25 use MediaWiki\HookContainer\HookContainer
;
26 use MediaWiki\Json\JsonCodec
;
27 use MediaWiki\MainConfigNames
;
28 use MediaWiki\Page\WikiPageFactory
;
29 use MediaWiki\Title\TitleFactory
;
30 use Psr\Log\LoggerInterface
;
31 use Wikimedia\ObjectCache\BagOStuff
;
32 use Wikimedia\ObjectCache\WANObjectCache
;
33 use Wikimedia\Stats\StatsFactory
;
34 use Wikimedia\UUID\GlobalIdGenerator
;
37 * Returns an instance of the ParserCache by its name.
39 * @package MediaWiki\Parser
41 class ParserCacheFactory
{
43 /** @var string name of ParserCache for the default parser */
44 public const DEFAULT_NAME
= 'pcache';
46 /** @var string name of RevisionOutputCache for the default parser */
47 public const DEFAULT_RCACHE_NAME
= 'rcache';
50 private $parserCacheBackend;
52 /** @var WANObjectCache */
53 private $revisionOutputCacheBackend;
55 /** @var HookContainer */
56 private $hookContainer;
61 /** @var StatsFactory */
64 /** @var LoggerInterface */
67 /** @var TitleFactory */
68 private $titleFactory;
70 /** @var WikiPageFactory */
71 private $wikiPageFactory;
73 private GlobalIdGenerator
$globalIdGenerator;
75 /** @var ParserCache[] */
76 private $parserCaches = [];
78 /** @var RevisionOutputCache[] */
79 private $revisionOutputCaches = [];
81 /** @var ServiceOptions */
87 public const CONSTRUCTOR_OPTIONS
= [
88 MainConfigNames
::CacheEpoch
,
89 MainConfigNames
::ParserCacheFilterConfig
,
90 MainConfigNames
::OldRevisionParserCacheExpireTime
,
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
105 public function __construct(
106 BagOStuff
$parserCacheBackend,
107 WANObjectCache
$revisionOutputCacheBackend,
108 HookContainer
$hookContainer,
109 JsonCodec
$jsonCodec,
111 LoggerInterface
$logger,
112 ServiceOptions
$options,
113 TitleFactory
$titleFactory,
114 WikiPageFactory
$wikiPageFactory,
115 GlobalIdGenerator
$globalIdGenerator
117 $this->parserCacheBackend
= $parserCacheBackend;
118 $this->revisionOutputCacheBackend
= $revisionOutputCacheBackend;
119 $this->hookContainer
= $hookContainer;
120 $this->jsonCodec
= $jsonCodec;
121 $this->stats
= $stats;
122 $this->logger
= $logger;
124 $options->assertRequiredOptions( self
::CONSTRUCTOR_OPTIONS
);
125 $this->options
= $options;
126 $this->titleFactory
= $titleFactory;
127 $this->wikiPageFactory
= $wikiPageFactory;
128 $this->globalIdGenerator
= $globalIdGenerator;
132 * Get a ParserCache instance by $name.
133 * @param string $name
134 * @return ParserCache
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(
141 $this->parserCacheBackend
,
142 $this->options
->get( MainConfigNames
::CacheEpoch
),
143 $this->hookContainer
,
148 $this->wikiPageFactory
,
149 $this->globalIdGenerator
152 $filterConfig = $this->options
->get( MainConfigNames
::ParserCacheFilterConfig
);
154 if ( isset( $filterConfig[$name] ) ) {
155 $filter = new ParserCacheFilter( $filterConfig[$name] );
156 $cache->setFilter( $filter );
159 $this->parserCaches
[$name] = $cache;
161 return $this->parserCaches
[$name];
165 * Get a RevisionOutputCache instance by $name.
166 * @param string $name
167 * @return RevisionOutputCache
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(
174 $this->revisionOutputCacheBackend
,
175 $this->options
->get( MainConfigNames
::OldRevisionParserCacheExpireTime
),
176 $this->options
->get( MainConfigNames
::CacheEpoch
),
180 $this->globalIdGenerator
183 $this->revisionOutputCaches
[$name] = $cache;
185 return $this->revisionOutputCaches
[$name];