Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.48% covered (warning)
84.48%
49 / 58
62.50% covered (warning)
62.50%
5 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryMostViewed
84.48% covered (warning)
84.48%
49 / 58
62.50% covered (warning)
62.50%
5 / 8
16.96
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 executeGenerator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 run
88.24% covered (warning)
88.24%
30 / 34
0.00% covered (danger)
0.00%
0 / 1
9.13
 getCacheMode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAllowedParams
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\PageViewInfo;
4
5use MediaWiki\Api\ApiBase;
6use MediaWiki\Api\ApiPageSet;
7use MediaWiki\Api\ApiQuery;
8use MediaWiki\Api\ApiQueryGeneratorBase;
9use MediaWiki\Title\Title;
10use Wikimedia\ParamValidator\ParamValidator;
11use Wikimedia\ParamValidator\TypeDef\IntegerDef;
12
13/**
14 * Expose PageViewService::getTopPages().
15 */
16class ApiQueryMostViewed extends ApiQueryGeneratorBase {
17
18    private PageViewService $pageViewService;
19
20    public function __construct(
21        ApiQuery $query,
22        string $moduleName,
23        PageViewService $pageViewService
24    ) {
25        parent::__construct( $query, $moduleName, 'pvim' );
26        $this->pageViewService = $pageViewService;
27    }
28
29    public function execute() {
30        $this->run();
31    }
32
33    /** @inheritDoc */
34    public function executeGenerator( $resultPageSet ) {
35        $this->run( $resultPageSet );
36    }
37
38    /**
39     * @param ApiPageSet|null $resultPageSet
40     */
41    private function run( ?ApiPageSet $resultPageSet = null ) {
42        $params = $this->extractRequestParams();
43        $metric = Hooks::getApiMetricsMap()[$params['metric']];
44        $status = $this->pageViewService->getTopPages( $metric );
45
46        if ( $status->isOK() ) {
47            $this->addMessagesFromStatus( Hooks::makeWarningsOnlyStatus( $status ) );
48            $limit = $params['limit'];
49            $offset = $params['offset'];
50
51            $data = $status->getValue();
52            if ( count( $data ) > $offset + $limit ) {
53                $this->setContinueEnumParameter( 'offset', $offset + $limit );
54            }
55            $data = array_slice( $data, $offset, $limit, true );
56
57            if ( $resultPageSet ) {
58                $titles = [];
59                foreach ( $data as $titleText => $_ ) {
60                    $title = Title::newFromText( $titleText );
61                    // Page View API may return invalid titles (T225853)
62                    if ( $title ) {
63                        $titles[] = $title;
64                    }
65                }
66                $resultPageSet->populateFromTitles( $titles );
67            } else {
68                $result = $this->getResult();
69                $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
70                foreach ( $data as $titleText => $titleData ) {
71                    $item = [];
72                    $title = Title::newFromText( $titleText );
73                    if ( !$title ) {
74                        // Page View API may return invalid titles (T208691)
75                        $offset++;
76                        continue;
77                    }
78                    self::addTitleInfo( $item, $title );
79                    $item['count'] = $titleData;
80                    $fits = $result->addValue( [ 'query', $this->getModuleName() ], null, $item );
81                    if ( !$fits ) {
82                        $this->setContinueEnumParameter( 'offset', $offset );
83                        break;
84                    }
85                    $offset++;
86                }
87            }
88        } else {
89            $this->dieStatus( $status );
90        }
91    }
92
93    /** @inheritDoc */
94    public function getCacheMode( $params ) {
95        return 'public';
96    }
97
98    /** @inheritDoc */
99    public function getAllowedParams() {
100        return Hooks::getApiMetricsHelp( PageViewService::SCOPE_TOP ) + [
101            'limit' => [
102                ParamValidator::PARAM_DEFAULT => 10,
103                ParamValidator::PARAM_TYPE => 'limit',
104                IntegerDef::PARAM_MIN => 1,
105                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
106                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
107            ],
108            'offset' => [
109                ParamValidator::PARAM_DEFAULT => 0,
110                ParamValidator::PARAM_TYPE => 'integer',
111                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
112            ],
113        ];
114    }
115
116    /** @inheritDoc */
117    protected function getExamplesMessages() {
118        return [
119            'action=query&list=mostviewed' => 'apihelp-query+mostviewed-example',
120            'action=query&generator=mostviewed&prop=pageviews' => 'apihelp-query+mostviewed-example2',
121        ];
122    }
123
124    /** @inheritDoc */
125    public function getHelpUrls() {
126        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:PageViewInfo';
127    }
128}