Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 151 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
ApiQueryProtectedTitles | |
0.00% |
0 / 151 |
|
0.00% |
0 / 8 |
1056 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
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 | |||
run | |
0.00% |
0 / 83 |
|
0.00% |
0 / 1 |
552 | |||
getCacheMode | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
getAllowedParams | |
0.00% |
0 / 53 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Copyright © 2009 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 | use MediaWiki\CommentFormatter\RowCommentFormatter; |
24 | use MediaWiki\CommentStore\CommentStore; |
25 | use MediaWiki\MainConfigNames; |
26 | use MediaWiki\Title\Title; |
27 | use Wikimedia\ParamValidator\ParamValidator; |
28 | use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
29 | |
30 | /** |
31 | * Query module to enumerate all create-protected pages. |
32 | * |
33 | * @ingroup API |
34 | */ |
35 | class ApiQueryProtectedTitles extends ApiQueryGeneratorBase { |
36 | |
37 | private CommentStore $commentStore; |
38 | private RowCommentFormatter $commentFormatter; |
39 | |
40 | /** |
41 | * @param ApiQuery $query |
42 | * @param string $moduleName |
43 | * @param CommentStore $commentStore |
44 | * @param RowCommentFormatter $commentFormatter |
45 | */ |
46 | public function __construct( |
47 | ApiQuery $query, |
48 | $moduleName, |
49 | CommentStore $commentStore, |
50 | RowCommentFormatter $commentFormatter |
51 | ) { |
52 | parent::__construct( $query, $moduleName, 'pt' ); |
53 | $this->commentStore = $commentStore; |
54 | $this->commentFormatter = $commentFormatter; |
55 | } |
56 | |
57 | public function execute() { |
58 | $this->run(); |
59 | } |
60 | |
61 | public function executeGenerator( $resultPageSet ) { |
62 | $this->run( $resultPageSet ); |
63 | } |
64 | |
65 | /** |
66 | * @param ApiPageSet|null $resultPageSet |
67 | * @return void |
68 | */ |
69 | private function run( $resultPageSet = null ) { |
70 | $params = $this->extractRequestParams(); |
71 | |
72 | $this->addTables( 'protected_titles' ); |
73 | $this->addFields( [ 'pt_namespace', 'pt_title', 'pt_timestamp' ] ); |
74 | |
75 | $prop = array_fill_keys( $params['prop'], true ); |
76 | $this->addFieldsIf( 'pt_user', isset( $prop['user'] ) || isset( $prop['userid'] ) ); |
77 | $this->addFieldsIf( 'pt_expiry', isset( $prop['expiry'] ) ); |
78 | $this->addFieldsIf( 'pt_create_perm', isset( $prop['level'] ) ); |
79 | |
80 | if ( isset( $prop['comment'] ) || isset( $prop['parsedcomment'] ) ) { |
81 | $commentQuery = $this->commentStore->getJoin( 'pt_reason' ); |
82 | $this->addTables( $commentQuery['tables'] ); |
83 | $this->addFields( $commentQuery['fields'] ); |
84 | $this->addJoinConds( $commentQuery['joins'] ); |
85 | } |
86 | |
87 | $this->addTimestampWhereRange( 'pt_timestamp', $params['dir'], $params['start'], $params['end'] ); |
88 | $this->addWhereFld( 'pt_namespace', $params['namespace'] ); |
89 | $this->addWhereFld( 'pt_create_perm', $params['level'] ); |
90 | |
91 | // Include in ORDER BY for uniqueness |
92 | $this->addWhereRange( 'pt_namespace', $params['dir'], null, null ); |
93 | $this->addWhereRange( 'pt_title', $params['dir'], null, null ); |
94 | |
95 | if ( $params['continue'] !== null ) { |
96 | $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'timestamp', 'int', 'string' ] ); |
97 | $op = ( $params['dir'] === 'newer' ? '>=' : '<=' ); |
98 | $db = $this->getDB(); |
99 | $this->addWhere( $db->buildComparison( $op, [ |
100 | 'pt_timestamp' => $db->timestamp( $cont[0] ), |
101 | 'pt_namespace' => $cont[1], |
102 | 'pt_title' => $cont[2], |
103 | ] ) ); |
104 | } |
105 | |
106 | if ( isset( $prop['user'] ) ) { |
107 | $this->addTables( 'user' ); |
108 | $this->addFields( 'user_name' ); |
109 | $this->addJoinConds( [ 'user' => [ 'LEFT JOIN', |
110 | 'user_id=pt_user' |
111 | ] ] ); |
112 | } |
113 | |
114 | $this->addOption( 'LIMIT', $params['limit'] + 1 ); |
115 | $res = $this->select( __METHOD__ ); |
116 | |
117 | if ( $resultPageSet === null ) { |
118 | $this->executeGenderCacheFromResultWrapper( $res, __METHOD__, 'pt' ); |
119 | if ( isset( $prop['parsedcomment'] ) ) { |
120 | $formattedComments = $this->commentFormatter->formatItems( |
121 | $this->commentFormatter->rows( $res ) |
122 | ->commentKey( 'pt_reason' ) |
123 | ->namespaceField( 'pt_namespace' ) |
124 | ->titleField( 'pt_title' ) |
125 | ); |
126 | } |
127 | } |
128 | |
129 | $count = 0; |
130 | $result = $this->getResult(); |
131 | |
132 | $titles = []; |
133 | |
134 | foreach ( $res as $rowOffset => $row ) { |
135 | if ( ++$count > $params['limit'] ) { |
136 | // We've reached the one extra which shows that there are |
137 | // additional pages to be had. Stop here... |
138 | $this->setContinueEnumParameter( 'continue', |
139 | "$row->pt_timestamp|$row->pt_namespace|$row->pt_title" |
140 | ); |
141 | break; |
142 | } |
143 | |
144 | $title = Title::makeTitle( $row->pt_namespace, $row->pt_title ); |
145 | if ( $resultPageSet === null ) { |
146 | $vals = []; |
147 | ApiQueryBase::addTitleInfo( $vals, $title ); |
148 | if ( isset( $prop['timestamp'] ) ) { |
149 | $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->pt_timestamp ); |
150 | } |
151 | |
152 | if ( isset( $prop['user'] ) && $row->user_name !== null ) { |
153 | $vals['user'] = $row->user_name; |
154 | } |
155 | |
156 | if ( isset( $prop['userid'] ) || /*B/C*/isset( $prop['user'] ) ) { |
157 | $vals['userid'] = (int)$row->pt_user; |
158 | } |
159 | |
160 | if ( isset( $prop['comment'] ) ) { |
161 | $vals['comment'] = $this->commentStore->getComment( 'pt_reason', $row )->text; |
162 | } |
163 | |
164 | if ( isset( $prop['parsedcomment'] ) ) { |
165 | // @phan-suppress-next-line PhanTypeArraySuspiciousNullable |
166 | $vals['parsedcomment'] = $formattedComments[$rowOffset]; |
167 | } |
168 | |
169 | if ( isset( $prop['expiry'] ) ) { |
170 | $vals['expiry'] = ApiResult::formatExpiry( $row->pt_expiry ); |
171 | } |
172 | |
173 | if ( isset( $prop['level'] ) ) { |
174 | $vals['level'] = $row->pt_create_perm; |
175 | } |
176 | |
177 | $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals ); |
178 | if ( !$fit ) { |
179 | $this->setContinueEnumParameter( 'continue', |
180 | "$row->pt_timestamp|$row->pt_namespace|$row->pt_title" |
181 | ); |
182 | break; |
183 | } |
184 | } else { |
185 | $titles[] = $title; |
186 | } |
187 | } |
188 | |
189 | if ( $resultPageSet === null ) { |
190 | $result->addIndexedTagName( |
191 | [ 'query', $this->getModuleName() ], |
192 | $this->getModulePrefix() |
193 | ); |
194 | } else { |
195 | $resultPageSet->populateFromTitles( $titles ); |
196 | } |
197 | } |
198 | |
199 | public function getCacheMode( $params ) { |
200 | if ( $params['prop'] !== null && in_array( 'parsedcomment', $params['prop'] ) ) { |
201 | // MediaWiki\CommentFormatter\CommentFormatter::formatItems() calls wfMessage() among other things |
202 | return 'anon-public-user-private'; |
203 | } else { |
204 | return 'public'; |
205 | } |
206 | } |
207 | |
208 | public function getAllowedParams() { |
209 | return [ |
210 | 'namespace' => [ |
211 | ParamValidator::PARAM_ISMULTI => true, |
212 | ParamValidator::PARAM_TYPE => 'namespace', |
213 | ], |
214 | 'level' => [ |
215 | ParamValidator::PARAM_ISMULTI => true, |
216 | ParamValidator::PARAM_TYPE => array_diff( |
217 | $this->getConfig()->get( MainConfigNames::RestrictionLevels ), [ '' ] ) |
218 | ], |
219 | 'limit' => [ |
220 | ParamValidator::PARAM_DEFAULT => 10, |
221 | ParamValidator::PARAM_TYPE => 'limit', |
222 | IntegerDef::PARAM_MIN => 1, |
223 | IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
224 | IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
225 | ], |
226 | 'dir' => [ |
227 | ParamValidator::PARAM_DEFAULT => 'older', |
228 | ParamValidator::PARAM_TYPE => [ |
229 | 'newer', |
230 | 'older' |
231 | ], |
232 | ApiBase::PARAM_HELP_MSG => 'api-help-param-direction', |
233 | ApiBase::PARAM_HELP_MSG_PER_VALUE => [ |
234 | 'newer' => 'api-help-paramvalue-direction-newer', |
235 | 'older' => 'api-help-paramvalue-direction-older', |
236 | ], |
237 | ], |
238 | 'start' => [ |
239 | ParamValidator::PARAM_TYPE => 'timestamp' |
240 | ], |
241 | 'end' => [ |
242 | ParamValidator::PARAM_TYPE => 'timestamp' |
243 | ], |
244 | 'prop' => [ |
245 | ParamValidator::PARAM_ISMULTI => true, |
246 | ParamValidator::PARAM_DEFAULT => 'timestamp|level', |
247 | ParamValidator::PARAM_TYPE => [ |
248 | 'timestamp', |
249 | 'user', |
250 | 'userid', |
251 | 'comment', |
252 | 'parsedcomment', |
253 | 'expiry', |
254 | 'level' |
255 | ], |
256 | ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
257 | ], |
258 | 'continue' => [ |
259 | ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
260 | ], |
261 | ]; |
262 | } |
263 | |
264 | protected function getExamplesMessages() { |
265 | return [ |
266 | 'action=query&list=protectedtitles' |
267 | => 'apihelp-query+protectedtitles-example-simple', |
268 | 'action=query&generator=protectedtitles&gptnamespace=0&prop=linkshere' |
269 | => 'apihelp-query+protectedtitles-example-generator', |
270 | ]; |
271 | } |
272 | |
273 | public function getHelpUrls() { |
274 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Protectedtitles'; |
275 | } |
276 | } |