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