Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.62% covered (warning)
76.62%
59 / 77
25.00% covered (danger)
25.00%
2 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryPagesInIndex
76.62% covered (warning)
76.62%
59 / 77
25.00% covered (danger)
25.00%
2 / 8
25.11
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
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 executeGenerator
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 run
86.96% covered (warning)
86.96%
20 / 23
0.00% covered (danger)
0.00%
0 / 1
7.11
 getPageInfo
76.92% covered (warning)
76.92%
10 / 13
0.00% covered (danger)
0.00%
0 / 1
5.31
 getAllowedParams
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
1
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace ProofreadPage\Api;
4
5use LogicException;
6use MediaWiki\Api\ApiBase;
7use MediaWiki\Api\ApiPageSet;
8use MediaWiki\Api\ApiQuery;
9use MediaWiki\Api\ApiQueryGeneratorBase;
10use MediaWiki\Title\Title;
11use ProofreadPage\Context;
12use ProofreadPage\Pagination\Pagination;
13use Wikimedia\ParamValidator\ParamValidator;
14use Wikimedia\ParamValidator\TypeDef\EnumDef;
15
16/**
17 * API list module for getting the list of pages in an index
18 */
19class ApiQueryPagesInIndex extends ApiQueryGeneratorBase {
20
21    /**
22     * @var Context
23     */
24    private $context;
25
26    /** @var string API module prefix */
27    private static $prefix = 'prppii';
28
29    public function __construct( ApiQuery $query, string $moduleName ) {
30        parent::__construct( $query, $moduleName, static::$prefix );
31        $this->context = Context::getDefaultContext();
32    }
33
34    /**
35     * @inheritDoc
36     */
37    public function execute() {
38        try {
39            $this->run();
40        } catch ( LogicException $e ) {
41            $this->dieWithException( $e );
42        }
43    }
44
45    /**
46     * @inheritDoc
47     */
48    public function executeGenerator( $resultPageSet ) {
49        try {
50            $this->run( $resultPageSet );
51        } catch ( LogicException $e ) {
52            $this->dieWithException( $e );
53        }
54    }
55
56    /**
57     * Main API logic for gathering the pages in an index
58     * @param ApiPageSet|null $resultPageSet
59     */
60    private function run( ?ApiPageSet $resultPageSet = null ) {
61        $params = $this->extractRequestParams();
62
63        // handles 'missingparam' error if needed
64        $indexTitle = $this->getTitleOrPageId( $params )->getTitle();
65
66        if ( $indexTitle->getNamespace() !== $this->context->getIndexNamespaceId() ) {
67            $this->dieWithError(
68                [ 'apierror-proofreadpage-invalidindex', $indexTitle->getFullText() ] );
69        }
70
71        $prop = array_fill_keys( $params['prop'], true );
72
73        $result = $this->getResult();
74        $pages = [];
75
76        $pagination = $this->context->getPaginationFactory()->getPaginationForIndexTitle( $indexTitle );
77
78        if ( isset( $prop[ 'ids' ] ) ) {
79            // We prefetch the page ids
80            $pagination->prefetchPageLinks();
81        }
82
83        $result->addValue( [ 'query' ], $this->getModuleName(), [] );
84
85        foreach ( $pagination as $key => $pageTitle ) {
86
87            $pages[] = $pageTitle;
88
89            if ( $resultPageSet === null ) {
90                // Not a generator, build the result array
91                $pageInfo = $this->getPageInfo( $pageTitle, $pagination, $prop );
92
93                $fits = $result->addValue( [ 'query', $this->getModuleName() ],
94                    null, $pageInfo );
95
96                if ( !$fits ) {
97                    $this->setContinueEnumParameter( 'continue', $pagination->getPageNumber( $pageTitle ) );
98                    break;
99                }
100            }
101        }
102
103        // for generators
104        if ( $resultPageSet !== null ) {
105            $resultPageSet->populateFromTitles( $pages );
106        }
107    }
108
109    /**
110     * Get returned page info for the query result
111     * @param Title $pageTitle the title of the page in the pagination
112     * @param Pagination $pagination the index pagination
113     * @param array $prop properties array
114     * @return array array of the info
115     */
116    private function getPageInfo( Title $pageTitle, $pagination, array $prop ) {
117        // The index of the page within the index - vital for continuation
118        $pageOffset = $pagination->getPageNumber( $pageTitle );
119        $pageInfo = [
120            'pageoffset' => $pageOffset
121        ];
122
123        if ( isset( $prop[ 'ids' ] ) ) {
124            $pageInfo[ 'pageid' ] = $pageTitle->getId();
125        }
126
127        if ( isset( $prop[ 'title' ] ) ) {
128            $pageInfo[ 'title' ] = $pageTitle->getFullText();
129        }
130
131        if ( isset( $prop[ 'formattedpagenumber' ] ) || isset( $prop[ 'formattedPageNumber' ] ) ) {
132            $pageInfo[ 'formattedPageNumber' ] = $pagination
133                ->getDisplayedPageNumber( $pageOffset )
134                ->getFormattedPageNumber( $this->getLanguage() );
135        }
136
137        return $pageInfo;
138    }
139
140    /**
141     * @inheritDoc
142     */
143    protected function getAllowedParams() {
144        return [
145            'prop' => [
146                ParamValidator::PARAM_ISMULTI => true,
147                ParamValidator::PARAM_DEFAULT => 'ids|title',
148                ParamValidator::PARAM_TYPE => [
149                    'ids',
150                    'title',
151                    'formattedPageNumber',
152                    'formattedpagenumber',
153                ],
154                ApiBase::PARAM_HELP_MSG_PER_VALUE => [
155                    'formattedPageNumber' =>
156                        'apihelp-query+proofreadpagesinindex-paramvalue-prop-formattedpagenumber-deprecated',
157                ],
158                EnumDef::PARAM_DEPRECATED_VALUES => [
159                    'formattedPageNumber' => true,
160                ],
161            ],
162            'title' => [
163                ParamValidator::PARAM_TYPE => 'string',
164            ],
165            'pageid' => [
166                ParamValidator::PARAM_TYPE => 'integer',
167            ]
168        ];
169    }
170
171    /**
172     * @inheritDoc
173     */
174    public function getHelpUrls() {
175        return [
176            'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:ProofreadPage/Index pagination API',
177        ];
178    }
179
180    /**
181     * @inheritDoc
182     */
183    protected function getExamplesMessages() {
184        $prefix = static::$prefix;
185        return [
186            "action=query&list=proofreadpagesinindex&{$prefix}title=Index:Sandbox.djvu"
187                => 'apihelp-query+proofreadpagesinindex-example-1',
188        ];
189    }
190}