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