Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.73% covered (success)
92.73%
102 / 110
50.00% covered (danger)
50.00%
5 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
RevisionOutputCache
92.73% covered (success)
92.73%
102 / 110
50.00% covered (danger)
50.00%
5 / 10
30.35
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 getContentModelFromRevision
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 incrementStats
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 incrementRenderReasonStats
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 makeParserOutputKey
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
 makeParserOutputKeyOptionalRevId
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 get
95.65% covered (success)
95.65%
22 / 23
0.00% covered (danger)
0.00%
0 / 1
6
 save
96.97% covered (success)
96.97%
32 / 33
0.00% covered (danger)
0.00%
0 / 1
10
 restoreFromJson
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 encodeAsJson
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Cache for outputs of the PHP parser
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Cache Parser
22 */
23
24namespace MediaWiki\Parser;
25
26use InvalidArgumentException;
27use JsonException;
28use MediaWiki\Json\JsonCodec;
29use MediaWiki\Revision\RevisionRecord;
30use MediaWiki\Revision\SlotRecord;
31use MediaWiki\Utils\MWTimestamp;
32use Psr\Log\LoggerInterface;
33use Wikimedia\ObjectCache\WANObjectCache;
34use Wikimedia\Stats\StatsFactory;
35use Wikimedia\UUID\GlobalIdGenerator;
36
37/**
38 * Cache for ParserOutput objects.
39 * The cache is split per ParserOptions.
40 *
41 * @since 1.36
42 * @ingroup Cache Parser
43 */
44class RevisionOutputCache {
45
46    /** @var string The name of this cache. Used as a root of the cache key. */
47    private $name;
48
49    /** @var WANObjectCache */
50    private $cache;
51
52    /**
53     * Anything cached prior to this is invalidated
54     *
55     * @var string
56     */
57    private $cacheEpoch;
58
59    /**
60     * Expiry time for cache entries.
61     *
62     * @var int
63     */
64    private $cacheExpiry;
65
66    /** @var JsonCodec */
67    private $jsonCodec;
68
69    /** @var StatsFactory */
70    private $stats;
71
72    /** @var LoggerInterface */
73    private $logger;
74
75    private GlobalIdGenerator $globalIdGenerator;
76
77    /**
78     * @param string $name
79     * @param WANObjectCache $cache
80     * @param int $cacheExpiry Expiry for ParserOutput in $cache.
81     * @param string $cacheEpoch Anything before this timestamp is invalidated
82     * @param JsonCodec $jsonCodec
83     * @param StatsFactory $stats
84     * @param LoggerInterface $logger
85     * @param GlobalIdGenerator $globalIdGenerator
86     */
87    public function __construct(
88        string $name,
89        WANObjectCache $cache,
90        int $cacheExpiry,
91        string $cacheEpoch,
92        JsonCodec $jsonCodec,
93        StatsFactory $stats,
94        LoggerInterface $logger,
95        GlobalIdGenerator $globalIdGenerator
96    ) {
97        $this->name = $name;
98        $this->cache = $cache;
99        $this->cacheExpiry = $cacheExpiry;
100        $this->cacheEpoch = $cacheEpoch;
101        $this->jsonCodec = $jsonCodec;
102        $this->stats = $stats;
103        $this->logger = $logger;
104        $this->globalIdGenerator = $globalIdGenerator;
105    }
106
107    /**
108     * @param RevisionRecord $revision
109     * @return string
110     */
111    private function getContentModelFromRevision( RevisionRecord $revision ) {
112        if ( !$revision->hasSlot( SlotRecord::MAIN ) ) {
113            return 'missing';
114        }
115        return str_replace( '.', '_', $revision->getMainContentModel() );
116    }
117
118    /**
119     * @param RevisionRecord $revision
120     * @param string $status e.g. hit, miss etc.
121     * @param string|null $reason
122     */
123    private function incrementStats( RevisionRecord $revision, string $status, ?string $reason = null ) {
124        $contentModel = $this->getContentModelFromRevision( $revision );
125        $metricSuffix = $reason ? "{$status}_{$reason}" : $status;
126
127        $this->stats->getCounter( 'RevisionOutputCache_operation_total' )
128            ->setLabel( 'name', $this->name )
129            ->setLabel( 'contentModel', $contentModel )
130            ->setLabel( 'status', $status )
131            ->setLabel( 'reason', $reason ?: 'n/a' )
132            ->copyToStatsdAt( "RevisionOutputCache.{$this->name}.{$metricSuffix}" )
133            ->increment();
134    }
135
136    /**
137     * @param RevisionRecord $revision
138     * @param string $renderReason
139     */
140    private function incrementRenderReasonStats( RevisionRecord $revision, $renderReason ) {
141        $contentModel = $this->getContentModelFromRevision( $revision );
142        $renderReason = preg_replace( '/\W+/', '_', $renderReason );
143
144        $this->stats->getCounter( 'RevisionOutputCache_render_total' )
145            ->setLabel( 'name', $this->name )
146            ->setLabel( 'contentModel', $contentModel )
147            ->setLabel( 'reason', $renderReason )
148            ->increment();
149    }
150
151    /**
152     * Get a key that will be used by this cache to store the content
153     * for a given page considering the given options and the array of
154     * used options.
155     *
156     * If there is a possibility the revision does not have a revision id, use
157     * makeParserOutputKeyOptionalRevId() instead.
158     *
159     * @warning The exact format of the key is considered internal and is subject
160     * to change, thus should not be used as storage or long-term caching key.
161     * This is intended to be used for logging or keying something transient.
162     *
163     * @param RevisionRecord $revision
164     * @param ParserOptions $options
165     * @param array|null $usedOptions currently ignored
166     * @return string
167     * @internal
168     */
169    public function makeParserOutputKey(
170        RevisionRecord $revision,
171        ParserOptions $options,
172        ?array $usedOptions = null
173    ): string {
174        $usedOptions = ParserOptions::allCacheVaryingOptions();
175
176        $revId = $revision->getId();
177        if ( !$revId ) {
178            // If RevId is null, this would probably be unsafe to use as a cache key.
179            throw new InvalidArgumentException( "Revision must have an id number" );
180        }
181        $hash = $options->optionsHash( $usedOptions );
182        return $this->cache->makeKey( $this->name, $revId, $hash );
183    }
184
185    /**
186     * Get a key that will be used for locks or pool counter
187     *
188     * Similar to makeParserOutputKey except the revision id might be null,
189     * in which case it is unsafe to cache, but still needs a key for things like
190     * poolcounter.
191     *
192     * @warning The exact format of the key is considered internal and is subject
193     * to change, thus should not be used as storage or long-term caching key.
194     * This is intended to be used for logging or keying something transient.
195     *
196     * @param RevisionRecord $revision
197     * @param ParserOptions $options
198     * @param array|null $usedOptions currently ignored
199     * @return string
200     * @internal
201     */
202    public function makeParserOutputKeyOptionalRevId(
203        RevisionRecord $revision,
204        ParserOptions $options,
205        ?array $usedOptions = null
206    ): string {
207        $usedOptions = ParserOptions::allCacheVaryingOptions();
208
209        // revId may be null.
210        $revId = (string)$revision->getId();
211        $hash = $options->optionsHash( $usedOptions );
212        return $this->cache->makeKey( $this->name, $revId, $hash );
213    }
214
215    /**
216     * Retrieve the ParserOutput from cache.
217     * false if not found or outdated.
218     *
219     * @param RevisionRecord $revision
220     * @param ParserOptions $parserOptions
221     *
222     * @return ParserOutput|false False on failure
223     */
224    public function get( RevisionRecord $revision, ParserOptions $parserOptions ) {
225        if ( $this->cacheExpiry <= 0 ) {
226            // disabled
227            return false;
228        }
229
230        if ( !$parserOptions->isSafeToCache() ) {
231            $this->incrementStats( $revision, 'miss', 'unsafe' );
232            return false;
233        }
234
235        $cacheKey = $this->makeParserOutputKey( $revision, $parserOptions );
236        $json = $this->cache->get( $cacheKey );
237
238        if ( $json === false ) {
239            $this->incrementStats( $revision, 'miss', 'absent' );
240            return false;
241        }
242
243        $output = $this->restoreFromJson( $json, $cacheKey );
244        if ( $output === null ) {
245            $this->incrementStats( $revision, 'miss', 'unserialize' );
246            return false;
247        }
248
249        $cacheTime = (int)MWTimestamp::convert( TS_UNIX, $output->getCacheTime() );
250        $expiryTime = (int)MWTimestamp::convert( TS_UNIX, $this->cacheEpoch );
251        $expiryTime = max( $expiryTime, (int)MWTimestamp::now( TS_UNIX ) - $this->cacheExpiry );
252
253        if ( $cacheTime < $expiryTime ) {
254            $this->incrementStats( $revision, 'miss', 'expired' );
255            return false;
256        }
257
258        $this->logger->debug( 'old-revision cache hit' );
259        $this->incrementStats( $revision, 'hit' );
260        return $output;
261    }
262
263    /**
264     * @param ParserOutput $output
265     * @param RevisionRecord $revision
266     * @param ParserOptions $parserOptions
267     * @param string|null $cacheTime TS_MW timestamp when the output was generated
268     */
269    public function save(
270        ParserOutput $output,
271        RevisionRecord $revision,
272        ParserOptions $parserOptions,
273        ?string $cacheTime = null
274    ) {
275        if ( !$output->hasText() ) {
276            throw new InvalidArgumentException( 'Attempt to cache a ParserOutput with no text set!' );
277        }
278
279        if ( $this->cacheExpiry <= 0 ) {
280            // disabled
281            return;
282        }
283
284        $cacheKey = $this->makeParserOutputKey( $revision, $parserOptions );
285
286        // Ensure cache properties are set in the ParserOutput
287        // T350538: These should be turned into assertions that the
288        // properties are already present (and the $cacheTime argument
289        // removed).
290        if ( $cacheTime ) {
291            $output->setCacheTime( $cacheTime );
292        } else {
293            $cacheTime = $output->getCacheTime();
294        }
295        if ( !$output->getCacheRevisionId() ) {
296            $output->setCacheRevisionId( $revision->getId() );
297        }
298        if ( !$output->getRenderId() ) {
299            $output->setRenderId( $this->globalIdGenerator->newUUIDv1() );
300        }
301        if ( !$output->getRevisionTimestamp() ) {
302            $output->setRevisionTimestamp( $revision->getTimestamp() );
303        }
304
305        $msg = "Saved in RevisionOutputCache with key $cacheKey" .
306            " and timestamp $cacheTime" .
307            " and revision id {$revision->getId()}.";
308
309        $output->addCacheMessage( $msg );
310
311        // The ParserOutput might be dynamic and have been marked uncacheable by the parser.
312        $output->updateCacheExpiry( $this->cacheExpiry );
313
314        $expiry = $output->getCacheExpiry();
315        if ( $expiry <= 0 ) {
316            $this->incrementStats( $revision, 'save', 'uncacheable' );
317            return;
318        }
319
320        if ( !$parserOptions->isSafeToCache() ) {
321            $this->incrementStats( $revision, 'save', 'unsafe' );
322            return;
323        }
324
325        $json = $this->encodeAsJson( $output, $cacheKey );
326        if ( $json === null ) {
327            $this->incrementStats( $revision, 'save', 'nonserializable' );
328            return;
329        }
330
331        $this->cache->set( $cacheKey, $json, $expiry );
332        $this->incrementStats( $revision, 'save', 'success' );
333        $this->incrementRenderReasonStats( $revision, $parserOptions->getRenderReason() );
334    }
335
336    /**
337     * @param string $jsonData
338     * @param string $key
339     * @return CacheTime|ParserOutput|null
340     */
341    private function restoreFromJson( string $jsonData, string $key ) {
342        try {
343            /** @var CacheTime $obj */
344            $obj = $this->jsonCodec->deserialize( $jsonData, ParserOutput::class );
345            return $obj;
346        } catch ( JsonException $e ) {
347            $this->logger->error( 'Unable to deserialize JSON', [
348                'name' => $this->name,
349                'cache_key' => $key,
350                'message' => $e->getMessage()
351            ] );
352            return null;
353        }
354    }
355
356    /**
357     * @param CacheTime $obj
358     * @param string $key
359     * @return string|null
360     */
361    private function encodeAsJson( CacheTime $obj, string $key ) {
362        try {
363            return $this->jsonCodec->serialize( $obj );
364        } catch ( JsonException $e ) {
365            $this->logger->error( 'Unable to serialize JSON', [
366                'name' => $this->name,
367                'cache_key' => $key,
368                'message' => $e->getMessage(),
369            ] );
370            return null;
371        }
372    }
373}