Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 94 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
ApiQueryQueryPage | |
0.00% |
0 / 93 |
|
0.00% |
0 / 9 |
812 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
executeGenerator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSpecialPage | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
run | |
0.00% |
0 / 47 |
|
0.00% |
0 / 1 |
306 | |||
getCacheMode | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getAllowedParams | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
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 | |
23 | namespace MediaWiki\Api; |
24 | |
25 | use MediaWiki\MainConfigNames; |
26 | use MediaWiki\SpecialPage\QueryPage; |
27 | use MediaWiki\SpecialPage\SpecialPageFactory; |
28 | use MediaWiki\Title\Title; |
29 | use Wikimedia\ParamValidator\ParamValidator; |
30 | use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
31 | |
32 | /** |
33 | * Query module to get the results of a QueryPage-based special page |
34 | * |
35 | * @ingroup API |
36 | */ |
37 | class ApiQueryQueryPage extends ApiQueryGeneratorBase { |
38 | |
39 | /** |
40 | * @var string[] list of special page names |
41 | */ |
42 | private $queryPages; |
43 | |
44 | private SpecialPageFactory $specialPageFactory; |
45 | |
46 | public function __construct( |
47 | ApiQuery $query, |
48 | string $moduleName, |
49 | SpecialPageFactory $specialPageFactory |
50 | ) { |
51 | parent::__construct( $query, $moduleName, 'qp' ); |
52 | $this->queryPages = array_values( array_diff( |
53 | array_column( QueryPage::getPages(), 1 ), // [ class, name ] |
54 | $this->getConfig()->get( MainConfigNames::APIUselessQueryPages ) |
55 | ) ); |
56 | $this->specialPageFactory = $specialPageFactory; |
57 | } |
58 | |
59 | public function execute() { |
60 | $this->run(); |
61 | } |
62 | |
63 | public function executeGenerator( $resultPageSet ) { |
64 | $this->run( $resultPageSet ); |
65 | } |
66 | |
67 | /** |
68 | * @param string $name |
69 | * @return QueryPage |
70 | */ |
71 | private function getSpecialPage( $name ): QueryPage { |
72 | $qp = $this->specialPageFactory->getPage( $name ); |
73 | if ( !$qp ) { |
74 | self::dieDebug( |
75 | __METHOD__, |
76 | 'SpecialPageFactory failed to create special page ' . $name |
77 | ); |
78 | } |
79 | if ( !( $qp instanceof QueryPage ) ) { |
80 | self::dieDebug( |
81 | __METHOD__, |
82 | 'Special page ' . $name . ' is not a QueryPage' |
83 | ); |
84 | } |
85 | // @phan-suppress-next-line PhanTypeMismatchReturnNullable T240141 |
86 | return $qp; |
87 | } |
88 | |
89 | /** |
90 | * @param ApiPageSet|null $resultPageSet Set when used as a generator, null otherwise |
91 | */ |
92 | public function run( $resultPageSet = null ) { |
93 | $params = $this->extractRequestParams(); |
94 | $result = $this->getResult(); |
95 | |
96 | $qp = $this->getSpecialPage( $params['page'] ); |
97 | if ( !$qp->userCanExecute( $this->getUser() ) ) { |
98 | $this->dieWithError( 'apierror-specialpage-cantexecute' ); |
99 | } |
100 | |
101 | if ( $resultPageSet === null ) { |
102 | $r = [ 'name' => $params['page'] ]; |
103 | if ( $qp->isCached() ) { |
104 | if ( !$qp->isCacheable() ) { |
105 | $r['disabled'] = true; |
106 | } else { |
107 | $r['cached'] = true; |
108 | $ts = $qp->getCachedTimestamp(); |
109 | if ( $ts ) { |
110 | $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts ); |
111 | } |
112 | $r['maxresults'] = $this->getConfig()->get( MainConfigNames::QueryCacheLimit ); |
113 | } |
114 | } |
115 | $result->addValue( [ 'query' ], $this->getModuleName(), $r ); |
116 | } |
117 | |
118 | if ( $qp->isCached() && !$qp->isCacheable() ) { |
119 | // Disabled query page, don't run the query |
120 | return; |
121 | } |
122 | |
123 | $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 ); |
124 | $count = 0; |
125 | $titles = []; |
126 | foreach ( $res as $row ) { |
127 | if ( ++$count > $params['limit'] ) { |
128 | // We've had enough |
129 | $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] ); |
130 | break; |
131 | } |
132 | |
133 | $title = Title::makeTitle( $row->namespace, $row->title ); |
134 | if ( $resultPageSet === null ) { |
135 | $data = []; |
136 | if ( isset( $row->value ) ) { |
137 | $data['value'] = $row->value; |
138 | if ( $qp->usesTimestamps() ) { |
139 | $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value ); |
140 | } |
141 | } |
142 | self::addTitleInfo( $data, $title ); |
143 | |
144 | foreach ( $row as $field => $value ) { |
145 | if ( !in_array( $field, [ 'namespace', 'title', 'value', 'qc_type' ] ) ) { |
146 | $data['databaseResult'][$field] = $value; |
147 | } |
148 | } |
149 | |
150 | $fit = $result->addValue( [ 'query', $this->getModuleName(), 'results' ], null, $data ); |
151 | if ( !$fit ) { |
152 | $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 ); |
153 | break; |
154 | } |
155 | } else { |
156 | $titles[] = $title; |
157 | } |
158 | } |
159 | if ( $resultPageSet === null ) { |
160 | $result->addIndexedTagName( |
161 | [ 'query', $this->getModuleName(), 'results' ], |
162 | 'page' |
163 | ); |
164 | } else { |
165 | $resultPageSet->populateFromTitles( $titles ); |
166 | } |
167 | } |
168 | |
169 | public function getCacheMode( $params ) { |
170 | $qp = $this->getSpecialPage( $params['page'] ); |
171 | if ( $qp->getRestriction() != '' ) { |
172 | return 'private'; |
173 | } |
174 | |
175 | return 'public'; |
176 | } |
177 | |
178 | public function getAllowedParams() { |
179 | return [ |
180 | 'page' => [ |
181 | ParamValidator::PARAM_TYPE => $this->queryPages, |
182 | ParamValidator::PARAM_REQUIRED => true |
183 | ], |
184 | 'offset' => [ |
185 | ParamValidator::PARAM_DEFAULT => 0, |
186 | ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
187 | ], |
188 | 'limit' => [ |
189 | ParamValidator::PARAM_DEFAULT => 10, |
190 | ParamValidator::PARAM_TYPE => 'limit', |
191 | IntegerDef::PARAM_MIN => 1, |
192 | IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
193 | IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
194 | ], |
195 | ]; |
196 | } |
197 | |
198 | protected function getExamplesMessages() { |
199 | return [ |
200 | 'action=query&list=querypage&qppage=Ancientpages' |
201 | => 'apihelp-query+querypage-example-ancientpages', |
202 | ]; |
203 | } |
204 | |
205 | public function getHelpUrls() { |
206 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Querypage'; |
207 | } |
208 | } |
209 | |
210 | /** @deprecated class alias since 1.43 */ |
211 | class_alias( ApiQueryQueryPage::class, 'ApiQueryQueryPage' ); |