Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
QueryStatsActionApi
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 5
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getCacheMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 validateTargetParamater
n/a
0 / 0
n/a
0 / 0
0
 loadStatistics
n/a
0 / 0
n/a
0 / 0
0
 makeStatsItem
n/a
0 / 0
n/a
0 / 0
0
 execute
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
42
 makeItem
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getCacheRebuildJob
n/a
0 / 0
n/a
0 / 0
0
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Statistics;
5
6use ApiBase;
7use ApiQuery;
8use ApiQueryBase;
9use DeferredUpdates;
10use IJobSpecification;
11use JobQueueGroup;
12use MessageGroupStats;
13use Wikimedia\ParamValidator\ParamValidator;
14
15/**
16 * A base module for querying message group related stats.
17 *
18 * @ingroup API TranslateAPI
19 * @author Niklas Laxström
20 * @license GPL-2.0-or-later
21 * @since 2012-11-30
22 */
23abstract class QueryStatsActionApi extends ApiQueryBase {
24    private JobQueueGroup $jobQueueGroup;
25
26    public function __construct(
27        ApiQuery $queryModule,
28        string $moduleName,
29        string $paramPrefix,
30        JobQueueGroup $jobQueueGroup
31    ) {
32        parent::__construct( $queryModule, $moduleName, $paramPrefix );
33        $this->jobQueueGroup = $jobQueueGroup;
34    }
35
36    public function getCacheMode( $params ): string {
37        return 'public';
38    }
39
40    /**
41     * Implement this to implement input validation and return the name of the target that
42     * is then given to loadStats.
43     */
44    abstract protected function validateTargetParamater( array $params ): string;
45
46    /**
47     * Implement this to load stats.
48     * @param string $target
49     * @param int $flags See MessageGroupStats for possible flags
50     * @return array[]
51     */
52    abstract protected function loadStatistics( string $target, int $flags = 0 ): array;
53
54    /** Implement this to load each individual stat item */
55    abstract protected function makeStatsItem( string $item, array $stats ): ?array;
56
57    public function execute() {
58        $params = $this->extractRequestParams();
59
60        $target = $this->validateTargetParamater( $params );
61        $cache = $this->loadStatistics( $target, MessageGroupStats::FLAG_CACHE_ONLY );
62
63        $result = $this->getResult();
64        $incomplete = false;
65
66        foreach ( $cache as $item => $stats ) {
67            if ( $item < $params['offset'] ) {
68                continue;
69            }
70
71            if ( $stats[MessageGroupStats::TOTAL] === null ) {
72                $incomplete = true;
73                $this->setContinueEnumParameter( 'offset', $item );
74                break;
75            }
76
77            $data = $this->makeStatsItem( $item, $stats );
78            if ( $data === null ) {
79                continue;
80            }
81            $result->addValue( [ 'query', $this->getModuleName() ], null, $data );
82        }
83
84        $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'stats' );
85
86        if ( $incomplete ) {
87            DeferredUpdates::addCallableUpdate( function () use ( $target ): void {
88                $this->jobQueueGroup->push( $this->getCacheRebuildJob( $target ) );
89            } );
90        }
91    }
92
93    protected function makeItem( array $stats ): array {
94        return [
95            'total' => $stats[MessageGroupStats::TOTAL],
96            'translated' => $stats[MessageGroupStats::TRANSLATED],
97            'fuzzy' => $stats[MessageGroupStats::FUZZY],
98            'proofread' => $stats[MessageGroupStats::PROOFREAD],
99        ];
100    }
101
102    abstract protected function getCacheRebuildJob( string $target ): IJobSpecification;
103
104    protected function getAllowedParams(): array {
105        return [
106            'offset' => [
107                ParamValidator::PARAM_DEFAULT => '0',
108                ParamValidator::PARAM_TYPE => 'string',
109                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
110            ],
111        ];
112    }
113}