Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
ApiQueryPageViews | |
0.00% |
0 / 46 |
|
0.00% |
0 / 7 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
20 | |||
addData | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
getCacheMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAllowedParams | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\PageViewInfo; |
4 | |
5 | use ApiBase; |
6 | use ApiQueryBase; |
7 | use ApiResult; |
8 | use MediaWiki\MediaWikiServices; |
9 | use MediaWiki\Title\Title; |
10 | |
11 | /** |
12 | * Expose PageViewService::getPageData(). |
13 | */ |
14 | class ApiQueryPageViews extends ApiQueryBase { |
15 | public function __construct( $query, $moduleName ) { |
16 | parent::__construct( $query, $moduleName, 'pvip' ); |
17 | } |
18 | |
19 | public function execute() { |
20 | $params = $this->extractRequestParams(); |
21 | $continue = $params['continue']; |
22 | $titles = $this->getPageSet()->getMissingTitles() |
23 | + $this->getPageSet()->getSpecialTitles() |
24 | + $this->getPageSet()->getGoodTitles(); |
25 | |
26 | // sort titles alphabetically and discard those already processed in a previous request |
27 | $indexToTitle = array_map( static function ( Title $t ) { |
28 | return $t->getPrefixedDBkey(); |
29 | }, $titles ); |
30 | asort( $indexToTitle ); |
31 | $indexToTitle = array_filter( $indexToTitle, static function ( $title ) use ( $continue ) { |
32 | return $title >= $continue; |
33 | } ); |
34 | $titleToIndex = array_flip( $indexToTitle ); |
35 | $titles = array_filter( array_values( array_map( static function ( $index ) use ( $titles ) { |
36 | return $titles[$index] ?? null; |
37 | }, $titleToIndex ) ) ); |
38 | |
39 | /** @var PageViewService $service */ |
40 | $service = MediaWikiServices::getInstance()->getService( 'PageViewService' ); |
41 | $metric = Hooks::getApiMetricsMap()[$params['metric']]; |
42 | $status = $service->getPageData( $titles, $params['days'], $metric ); |
43 | if ( $status->isOK() ) { |
44 | $this->addMessagesFromStatus( Hooks::makeWarningsOnlyStatus( $status ) ); |
45 | $data = $status->getValue(); |
46 | foreach ( $titles as $title ) { |
47 | $index = $titleToIndex[$title->getPrefixedDBkey()]; |
48 | $fit = $this->addData( $index, $title, $data ); |
49 | if ( !$fit ) { |
50 | $this->setContinueEnumParameter( 'continue', $title->getPrefixedDBkey() ); |
51 | break; |
52 | } |
53 | } |
54 | } else { |
55 | $this->dieStatus( $status ); |
56 | } |
57 | } |
58 | |
59 | /** |
60 | * @param int $index Pageset ID (real or fake) |
61 | * @param Title $title |
62 | * @param array $data Data for all titles. |
63 | * @return bool Success. |
64 | */ |
65 | protected function addData( $index, Title $title, array $data ) { |
66 | if ( !isset( $data[$title->getPrefixedDBkey()] ) ) { |
67 | // PageViewService retains the ordering of titles so the first missing title means we |
68 | // have run out of data. |
69 | return false; |
70 | } |
71 | $value = $data[$title->getPrefixedDBkey()]; |
72 | ApiResult::setArrayType( $value, 'kvp', 'date' ); |
73 | ApiResult::setIndexedTagName( $value, 'count' ); |
74 | return $this->addPageSubItems( $index, $value ); |
75 | } |
76 | |
77 | public function getCacheMode( $params ) { |
78 | return 'public'; |
79 | } |
80 | |
81 | public function getAllowedParams() { |
82 | return Hooks::getApiMetricsHelp( PageViewService::SCOPE_ARTICLE ) + Hooks::getApiDaysHelp() + [ |
83 | 'continue' => [ |
84 | ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
85 | ], |
86 | ]; |
87 | } |
88 | |
89 | protected function getExamplesMessages() { |
90 | return [ |
91 | 'action=query&titles=Main_Page&prop=pageviews' => 'apihelp-query+pageviews-example', |
92 | ]; |
93 | } |
94 | |
95 | public function getHelpUrls() { |
96 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:PageViewInfo'; |
97 | } |
98 | } |