Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 54 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
ApiQueryFeatureUsage | |
0.00% |
0 / 54 |
|
0.00% |
0 / 5 |
156 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
72 | |||
getAllowedParams | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\ApiFeatureUsage; |
4 | |
5 | use ApiQuery; |
6 | use ApiQueryBase; |
7 | use MediaWiki\MediaWikiServices; |
8 | use MediaWiki\Utils\MWTimestamp; |
9 | use Wikimedia\ParamValidator\ParamValidator; |
10 | |
11 | class ApiQueryFeatureUsage extends ApiQueryBase { |
12 | |
13 | /** |
14 | * @param ApiQuery $query |
15 | * @param string $moduleName |
16 | */ |
17 | public function __construct( ApiQuery $query, $moduleName ) { |
18 | parent::__construct( $query, $moduleName, 'afu' ); |
19 | } |
20 | |
21 | /** @inheritDoc */ |
22 | public function execute() { |
23 | $params = $this->extractRequestParams(); |
24 | |
25 | $agent = $params['agent'] === null |
26 | ? $this->getMain()->getUserAgent() |
27 | : $params['agent']; |
28 | if ( $agent === '' ) { |
29 | $encParamName = $this->encodeParamName( 'agent' ); |
30 | $this->dieWithError( 'apierror-apifeatureusage-emptyagent', "bad_$encParamName" ); |
31 | } |
32 | |
33 | $conf = MediaWikiServices::getInstance() |
34 | ->getConfigFactory() |
35 | ->makeConfig( 'ApiFeatureUsage' ); |
36 | $engine = ApiFeatureUsageQueryEngine::getEngine( $conf ); |
37 | |
38 | if ( $params['start'] === null || $params['end'] === null ) { |
39 | [ $start, $end ] = $engine->suggestDateRange(); |
40 | } |
41 | if ( $params['start'] !== null ) { |
42 | $start = new MWTimestamp( $params['start'] ); |
43 | $start->setTimezone( 'UTC' ); |
44 | } |
45 | if ( $params['end'] !== null ) { |
46 | $end = new MWTimestamp( $params['end'] ); |
47 | $end->setTimezone( 'UTC' ); |
48 | } |
49 | '@phan-var MWTimestamp $start'; |
50 | '@phan-var MWTimestamp $end'; |
51 | |
52 | $status = $engine->execute( $agent, $start, $end, $params['features'] ); |
53 | if ( !$status->isOk() ) { |
54 | $this->dieStatus( $status ); |
55 | } |
56 | |
57 | $this->addMessagesFromStatus( $status ); |
58 | |
59 | $r = [ |
60 | 'agent' => $agent, |
61 | 'start' => $start->getTimestamp( TS_ISO_8601 ), |
62 | 'end' => $end->getTimestamp( TS_ISO_8601 ), |
63 | 'usage' => $status->value, |
64 | ]; |
65 | |
66 | $this->getResult()->setIndexedTagName( $r['usage'], 'v' ); |
67 | $this->getResult()->addValue( 'query', $this->getModuleName(), $r ); |
68 | } |
69 | |
70 | /** @inheritDoc */ |
71 | public function getAllowedParams() { |
72 | return [ |
73 | 'start' => [ |
74 | ParamValidator::PARAM_TYPE => 'timestamp', |
75 | ], |
76 | 'end' => [ |
77 | ParamValidator::PARAM_TYPE => 'timestamp', |
78 | ], |
79 | 'agent' => [ |
80 | ParamValidator::PARAM_TYPE => 'string', |
81 | ], |
82 | 'features' => [ |
83 | ParamValidator::PARAM_TYPE => 'string', |
84 | ParamValidator::PARAM_ISMULTI => true, |
85 | ], |
86 | ]; |
87 | } |
88 | |
89 | /** @inheritDoc */ |
90 | protected function getExamplesMessages() { |
91 | return [ |
92 | 'action=query&meta=featureusage' |
93 | => 'apihelp-query+featureusage-example-simple', |
94 | ]; |
95 | } |
96 | |
97 | /** @inheritDoc */ |
98 | public function getHelpUrls() { |
99 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:ApiFeatureUsage'; |
100 | } |
101 | |
102 | } |