Merge "mediawiki.api: Remove console warning for legacy token type"
[mediawiki.git] / includes / parser / ParserCacheFactory.php
bloba96d22bc8b6d788f9d5ff587428e311bfbf451a7
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.
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
18 * @file
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;
36 /**
37 * Returns an instance of the ParserCache by its name.
38 * @since 1.36
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';
49 /** @var BagOStuff */
50 private $parserCacheBackend;
52 /** @var WANObjectCache */
53 private $revisionOutputCacheBackend;
55 /** @var HookContainer */
56 private $hookContainer;
58 /** @var JsonCodec */
59 private $jsonCodec;
61 /** @var StatsFactory */
62 private $stats;
64 /** @var LoggerInterface */
65 private $logger;
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 */
82 private $options;
84 /**
85 * @internal
87 public const CONSTRUCTOR_OPTIONS = [
88 MainConfigNames::CacheEpoch,
89 MainConfigNames::ParserCacheFilterConfig,
90 MainConfigNames::OldRevisionParserCacheExpireTime,
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
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
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(
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
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(
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
183 $this->revisionOutputCaches[$name] = $cache;
185 return $this->revisionOutputCaches[$name];