Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 93
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryQueryPage
0.00% covered (danger)
0.00%
0 / 93
0.00% covered (danger)
0.00%
0 / 9
812
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 executeGenerator
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSpecialPage
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
 run
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 1
306
 getCacheMode
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
2
 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/**
3 * Copyright © 2010 Roan Kattouw <roan.kattouw@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23use MediaWiki\MainConfigNames;
24use MediaWiki\SpecialPage\QueryPage;
25use MediaWiki\SpecialPage\SpecialPageFactory;
26use MediaWiki\Title\Title;
27use Wikimedia\ParamValidator\ParamValidator;
28use Wikimedia\ParamValidator\TypeDef\IntegerDef;
29
30/**
31 * Query module to get the results of a QueryPage-based special page
32 *
33 * @ingroup API
34 */
35class ApiQueryQueryPage extends ApiQueryGeneratorBase {
36
37    /**
38     * @var string[] list of special page names
39     */
40    private $queryPages;
41
42    private SpecialPageFactory $specialPageFactory;
43
44    /**
45     * @param ApiQuery $query
46     * @param string $moduleName
47     * @param SpecialPageFactory $specialPageFactory
48     */
49    public function __construct(
50        ApiQuery $query,
51        $moduleName,
52        SpecialPageFactory $specialPageFactory
53    ) {
54        parent::__construct( $query, $moduleName, 'qp' );
55        $this->queryPages = array_values( array_diff(
56            array_column( QueryPage::getPages(), 1 ), // [ class, name ]
57            $this->getConfig()->get( MainConfigNames::APIUselessQueryPages )
58        ) );
59        $this->specialPageFactory = $specialPageFactory;
60    }
61
62    public function execute() {
63        $this->run();
64    }
65
66    public function executeGenerator( $resultPageSet ) {
67        $this->run( $resultPageSet );
68    }
69
70    /**
71     * @param string $name
72     * @return QueryPage
73     */
74    private function getSpecialPage( $name ): QueryPage {
75        $qp = $this->specialPageFactory->getPage( $name );
76        if ( !$qp ) {
77            self::dieDebug(
78                __METHOD__,
79                'SpecialPageFactory failed to create special page ' . $name
80            );
81        }
82        if ( !( $qp instanceof QueryPage ) ) {
83            self::dieDebug(
84                __METHOD__,
85                'Special page ' . $name . ' is not a QueryPage'
86            );
87        }
88        // @phan-suppress-next-line PhanTypeMismatchReturnNullable T240141
89        return $qp;
90    }
91
92    /**
93     * @param ApiPageSet|null $resultPageSet Set when used as a generator, null otherwise
94     */
95    public function run( $resultPageSet = null ) {
96        $params = $this->extractRequestParams();
97        $result = $this->getResult();
98
99        $qp = $this->getSpecialPage( $params['page'] );
100        if ( !$qp->userCanExecute( $this->getUser() ) ) {
101            $this->dieWithError( 'apierror-specialpage-cantexecute' );
102        }
103
104        if ( $resultPageSet === null ) {
105            $r = [ 'name' => $params['page'] ];
106            if ( $qp->isCached() ) {
107                if ( !$qp->isCacheable() ) {
108                    $r['disabled'] = true;
109                } else {
110                    $r['cached'] = true;
111                    $ts = $qp->getCachedTimestamp();
112                    if ( $ts ) {
113                        $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
114                    }
115                    $r['maxresults'] = $this->getConfig()->get( MainConfigNames::QueryCacheLimit );
116                }
117            }
118            $result->addValue( [ 'query' ], $this->getModuleName(), $r );
119        }
120
121        if ( $qp->isCached() && !$qp->isCacheable() ) {
122            // Disabled query page, don't run the query
123            return;
124        }
125
126        $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
127        $count = 0;
128        $titles = [];
129        foreach ( $res as $row ) {
130            if ( ++$count > $params['limit'] ) {
131                // We've had enough
132                $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
133                break;
134            }
135
136            $title = Title::makeTitle( $row->namespace, $row->title );
137            if ( $resultPageSet === null ) {
138                $data = [];
139                if ( isset( $row->value ) ) {
140                    $data['value'] = $row->value;
141                    if ( $qp->usesTimestamps() ) {
142                        $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
143                    }
144                }
145                self::addTitleInfo( $data, $title );
146
147                foreach ( $row as $field => $value ) {
148                    if ( !in_array( $field, [ 'namespace', 'title', 'value', 'qc_type' ] ) ) {
149                        $data['databaseResult'][$field] = $value;
150                    }
151                }
152
153                $fit = $result->addValue( [ 'query', $this->getModuleName(), 'results' ], null, $data );
154                if ( !$fit ) {
155                    $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
156                    break;
157                }
158            } else {
159                $titles[] = $title;
160            }
161        }
162        if ( $resultPageSet === null ) {
163            $result->addIndexedTagName(
164                [ 'query', $this->getModuleName(), 'results' ],
165                'page'
166            );
167        } else {
168            $resultPageSet->populateFromTitles( $titles );
169        }
170    }
171
172    public function getCacheMode( $params ) {
173        $qp = $this->getSpecialPage( $params['page'] );
174        if ( $qp->getRestriction() != '' ) {
175            return 'private';
176        }
177
178        return 'public';
179    }
180
181    public function getAllowedParams() {
182        return [
183            'page' => [
184                ParamValidator::PARAM_TYPE => $this->queryPages,
185                ParamValidator::PARAM_REQUIRED => true
186            ],
187            'offset' => [
188                ParamValidator::PARAM_DEFAULT => 0,
189                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
190            ],
191            'limit' => [
192                ParamValidator::PARAM_DEFAULT => 10,
193                ParamValidator::PARAM_TYPE => 'limit',
194                IntegerDef::PARAM_MIN => 1,
195                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
196                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
197            ],
198        ];
199    }
200
201    protected function getExamplesMessages() {
202        return [
203            'action=query&list=querypage&qppage=Ancientpages'
204                => 'apihelp-query+querypage-example-ancientpages',
205        ];
206    }
207
208    public function getHelpUrls() {
209        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Querypage';
210    }
211}