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