Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ParserCacheFilter
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 shouldCache
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
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 MediaWiki\Page\PageRecord;
25use ParserOptions;
26
27/**
28 * Filter that decides whether a ParserOutput object should be stored in the
29 * ParserCache or not. Can be used tweak tradeoffs between storage space and
30 * response latency.
31 *
32 * @since 1.42
33 * @package MediaWiki\Parser
34 */
35class ParserCacheFilter {
36
37    /**
38     * @see MainConfigSchema::ParserCacheFilterConfig
39     */
40    private array $config;
41
42    /**
43     * @param array[] $config See MainConfigSchema::ParserCacheFilterConfig.
44     */
45    public function __construct( array $config ) {
46        $this->config = $config;
47    }
48
49    public function shouldCache(
50        ParserOutput $output,
51        PageRecord $page,
52        ParserOptions $options
53    ): bool {
54        $ns = $page->getNamespace();
55        $cpuMin = $this->config[ $ns ]['minCpuTime']
56            ?? ( $this->config['default']['minCpuTime'] ?? 0 );
57
58        $cpuTime = $output->getTimeProfile( 'cpu' );
59
60        if ( $cpuTime !== null && $cpuTime < $cpuMin ) {
61            return false;
62        }
63
64        return true;
65    }
66
67}