| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Parser; |
| 23 |
|
| 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; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class ParserCacheFactory { |
| 42 |
|
| 43 |
|
| 44 |
public const DEFAULT_NAME = 'pcache'; |
| 45 |
|
| 46 |
|
| 47 |
public const DEFAULT_RCACHE_NAME = 'rcache'; |
| 48 |
|
| 49 |
|
| 50 |
private $parserCacheBackend; |
| 51 |
|
| 52 |
|
| 53 |
private $revisionOutputCacheBackend; |
| 54 |
|
| 55 |
|
| 56 |
private $hookContainer; |
| 57 |
|
| 58 |
|
| 59 |
private $jsonCodec; |
| 60 |
|
| 61 |
|
| 62 |
private $stats; |
| 63 |
|
| 64 |
|
| 65 |
private $logger; |
| 66 |
|
| 67 |
|
| 68 |
private $titleFactory; |
| 69 |
|
| 70 |
|
| 71 |
private $wikiPageFactory; |
| 72 |
|
| 73 |
private GlobalIdGenerator $globalIdGenerator; |
| 74 |
|
| 75 |
|
| 76 |
private $parserCaches = []; |
| 77 |
|
| 78 |
|
| 79 |
private $revisionOutputCaches = []; |
| 80 |
|
| 81 |
|
| 82 |
private $options; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public const CONSTRUCTOR_OPTIONS = [ |
| 88 |
MainConfigNames::CacheEpoch, |
| 89 |
MainConfigNames::ParserCacheFilterConfig, |
| 90 |
MainConfigNames::OldRevisionParserCacheExpireTime, |
| 91 |
]; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 133 |
|
| 134 |
|
| 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 |
|
| 166 |
|
| 167 |
|
| 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 |
} |