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