Merge "Disallow job pushes from JobQueueGroup to bogus wikis"
[lhc/web/wiklou.git] / includes / api / ApiQueryWatchlist.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 25, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 use MediaWiki\MediaWikiServices;
28
29 /**
30 * This query action allows clients to retrieve a list of recently modified pages
31 * that are part of the logged-in user's watchlist.
32 *
33 * @ingroup API
34 */
35 class ApiQueryWatchlist extends ApiQueryGeneratorBase {
36
37 public function __construct( ApiQuery $query, $moduleName ) {
38 parent::__construct( $query, $moduleName, 'wl' );
39 }
40
41 public function execute() {
42 $this->run();
43 }
44
45 public function executeGenerator( $resultPageSet ) {
46 $this->run( $resultPageSet );
47 }
48
49 private $fld_ids = false, $fld_title = false, $fld_patrol = false,
50 $fld_flags = false, $fld_timestamp = false, $fld_user = false,
51 $fld_comment = false, $fld_parsedcomment = false, $fld_sizes = false,
52 $fld_notificationtimestamp = false, $fld_userid = false,
53 $fld_loginfo = false;
54
55 /**
56 * @param ApiPageSet $resultPageSet
57 * @return void
58 */
59 private function run( $resultPageSet = null ) {
60 $this->selectNamedDB( 'watchlist', DB_REPLICA, 'watchlist' );
61
62 $params = $this->extractRequestParams();
63
64 $user = $this->getUser();
65 $wlowner = $this->getWatchlistUser( $params );
66
67 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
68 $prop = array_flip( $params['prop'] );
69
70 $this->fld_ids = isset( $prop['ids'] );
71 $this->fld_title = isset( $prop['title'] );
72 $this->fld_flags = isset( $prop['flags'] );
73 $this->fld_user = isset( $prop['user'] );
74 $this->fld_userid = isset( $prop['userid'] );
75 $this->fld_comment = isset( $prop['comment'] );
76 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
77 $this->fld_timestamp = isset( $prop['timestamp'] );
78 $this->fld_sizes = isset( $prop['sizes'] );
79 $this->fld_patrol = isset( $prop['patrol'] );
80 $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] );
81 $this->fld_loginfo = isset( $prop['loginfo'] );
82
83 if ( $this->fld_patrol ) {
84 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
85 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'patrol' );
86 }
87 }
88 }
89
90 $options = [
91 'dir' => $params['dir'] === 'older'
92 ? WatchedItemQueryService::DIR_OLDER
93 : WatchedItemQueryService::DIR_NEWER,
94 ];
95
96 if ( is_null( $resultPageSet ) ) {
97 $options['includeFields'] = $this->getFieldsToInclude();
98 } else {
99 $options['usedInGenerator'] = true;
100 }
101
102 if ( $params['start'] ) {
103 $options['start'] = $params['start'];
104 }
105 if ( $params['end'] ) {
106 $options['end'] = $params['end'];
107 }
108
109 $startFrom = null;
110 if ( !is_null( $params['continue'] ) ) {
111 $cont = explode( '|', $params['continue'] );
112 $this->dieContinueUsageIf( count( $cont ) != 2 );
113 $continueTimestamp = $cont[0];
114 $continueId = (int)$cont[1];
115 $this->dieContinueUsageIf( $continueId != $cont[1] );
116 $startFrom = [ $continueTimestamp, $continueId ];
117 }
118
119 if ( $wlowner !== $user ) {
120 $options['watchlistOwner'] = $wlowner;
121 $options['watchlistOwnerToken'] = $params['token'];
122 }
123
124 if ( !is_null( $params['namespace'] ) ) {
125 $options['namespaceIds'] = $params['namespace'];
126 }
127
128 if ( $params['allrev'] ) {
129 $options['allRevisions'] = true;
130 }
131
132 if ( !is_null( $params['show'] ) ) {
133 $show = array_flip( $params['show'] );
134
135 /* Check for conflicting parameters. */
136 if ( $this->showParamsConflicting( $show ) ) {
137 $this->dieWithError( 'apierror-show' );
138 }
139
140 // Check permissions.
141 if ( isset( $show[WatchedItemQueryService::FILTER_PATROLLED] )
142 || isset( $show[WatchedItemQueryService::FILTER_NOT_PATROLLED] )
143 ) {
144 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
145 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
146 }
147 }
148
149 $options['filters'] = array_keys( $show );
150 }
151
152 if ( !is_null( $params['type'] ) ) {
153 try {
154 $rcTypes = RecentChange::parseToRCType( $params['type'] );
155 if ( $rcTypes ) {
156 $options['rcTypes'] = $rcTypes;
157 }
158 } catch ( Exception $e ) {
159 ApiBase::dieDebug( __METHOD__, $e->getMessage() );
160 }
161 }
162
163 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
164 if ( !is_null( $params['user'] ) ) {
165 $options['onlyByUser'] = $params['user'];
166 }
167 if ( !is_null( $params['excludeuser'] ) ) {
168 $options['notByUser'] = $params['excludeuser'];
169 }
170
171 $options['limit'] = $params['limit'];
172
173 Hooks::run( 'ApiQueryWatchlistPrepareWatchedItemQueryServiceOptions', [
174 $this, $params, &$options
175 ] );
176
177 $ids = [];
178 $count = 0;
179 $watchedItemQuery = MediaWikiServices::getInstance()->getWatchedItemQueryService();
180 $items = $watchedItemQuery->getWatchedItemsWithRecentChangeInfo( $wlowner, $options, $startFrom );
181
182 foreach ( $items as list( $watchedItem, $recentChangeInfo ) ) {
183 /** @var WatchedItem $watchedItem */
184 if ( is_null( $resultPageSet ) ) {
185 $vals = $this->extractOutputData( $watchedItem, $recentChangeInfo );
186 $fit = $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
187 if ( !$fit ) {
188 $startFrom = [ $recentChangeInfo['rc_timestamp'], $recentChangeInfo['rc_id'] ];
189 break;
190 }
191 } else {
192 if ( $params['allrev'] ) {
193 $ids[] = intval( $recentChangeInfo['rc_this_oldid'] );
194 } else {
195 $ids[] = intval( $recentChangeInfo['rc_cur_id'] );
196 }
197 }
198 }
199
200 if ( $startFrom !== null ) {
201 $this->setContinueEnumParameter( 'continue', implode( '|', $startFrom ) );
202 }
203
204 if ( is_null( $resultPageSet ) ) {
205 $this->getResult()->addIndexedTagName(
206 [ 'query', $this->getModuleName() ],
207 'item'
208 );
209 } elseif ( $params['allrev'] ) {
210 $resultPageSet->populateFromRevisionIDs( $ids );
211 } else {
212 $resultPageSet->populateFromPageIDs( $ids );
213 }
214 }
215
216 private function getFieldsToInclude() {
217 $includeFields = [];
218 if ( $this->fld_flags ) {
219 $includeFields[] = WatchedItemQueryService::INCLUDE_FLAGS;
220 }
221 if ( $this->fld_user || $this->fld_userid ) {
222 $includeFields[] = WatchedItemQueryService::INCLUDE_USER_ID;
223 }
224 if ( $this->fld_user ) {
225 $includeFields[] = WatchedItemQueryService::INCLUDE_USER;
226 }
227 if ( $this->fld_comment || $this->fld_parsedcomment ) {
228 $includeFields[] = WatchedItemQueryService::INCLUDE_COMMENT;
229 }
230 if ( $this->fld_patrol ) {
231 $includeFields[] = WatchedItemQueryService::INCLUDE_PATROL_INFO;
232 }
233 if ( $this->fld_sizes ) {
234 $includeFields[] = WatchedItemQueryService::INCLUDE_SIZES;
235 }
236 if ( $this->fld_loginfo ) {
237 $includeFields[] = WatchedItemQueryService::INCLUDE_LOG_INFO;
238 }
239 return $includeFields;
240 }
241
242 private function showParamsConflicting( array $show ) {
243 return ( isset( $show[WatchedItemQueryService::FILTER_MINOR] )
244 && isset( $show[WatchedItemQueryService::FILTER_NOT_MINOR] ) )
245 || ( isset( $show[WatchedItemQueryService::FILTER_BOT] )
246 && isset( $show[WatchedItemQueryService::FILTER_NOT_BOT] ) )
247 || ( isset( $show[WatchedItemQueryService::FILTER_ANON] )
248 && isset( $show[WatchedItemQueryService::FILTER_NOT_ANON] ) )
249 || ( isset( $show[WatchedItemQueryService::FILTER_PATROLLED] )
250 && isset( $show[WatchedItemQueryService::FILTER_NOT_PATROLLED] ) )
251 || ( isset( $show[WatchedItemQueryService::FILTER_UNREAD] )
252 && isset( $show[WatchedItemQueryService::FILTER_NOT_UNREAD] ) );
253 }
254
255 private function extractOutputData( WatchedItem $watchedItem, array $recentChangeInfo ) {
256 /* Determine the title of the page that has been changed. */
257 $title = Title::makeTitle(
258 $watchedItem->getLinkTarget()->getNamespace(),
259 $watchedItem->getLinkTarget()->getDBkey()
260 );
261 $user = $this->getUser();
262
263 /* Our output data. */
264 $vals = [];
265 $type = intval( $recentChangeInfo['rc_type'] );
266 $vals['type'] = RecentChange::parseFromRCType( $type );
267 $anyHidden = false;
268
269 /* Create a new entry in the result for the title. */
270 if ( $this->fld_title || $this->fld_ids ) {
271 // These should already have been filtered out of the query, but just in case.
272 if ( $type === RC_LOG && ( $recentChangeInfo['rc_deleted'] & LogPage::DELETED_ACTION ) ) {
273 $vals['actionhidden'] = true;
274 $anyHidden = true;
275 }
276 if ( $type !== RC_LOG ||
277 LogEventsList::userCanBitfield(
278 $recentChangeInfo['rc_deleted'],
279 LogPage::DELETED_ACTION,
280 $user
281 )
282 ) {
283 if ( $this->fld_title ) {
284 ApiQueryBase::addTitleInfo( $vals, $title );
285 }
286 if ( $this->fld_ids ) {
287 $vals['pageid'] = intval( $recentChangeInfo['rc_cur_id'] );
288 $vals['revid'] = intval( $recentChangeInfo['rc_this_oldid'] );
289 $vals['old_revid'] = intval( $recentChangeInfo['rc_last_oldid'] );
290 }
291 }
292 }
293
294 /* Add user data and 'anon' flag, if user is anonymous. */
295 if ( $this->fld_user || $this->fld_userid ) {
296 if ( $recentChangeInfo['rc_deleted'] & Revision::DELETED_USER ) {
297 $vals['userhidden'] = true;
298 $anyHidden = true;
299 }
300 if ( Revision::userCanBitfield(
301 $recentChangeInfo['rc_deleted'],
302 Revision::DELETED_USER,
303 $user
304 ) ) {
305 if ( $this->fld_userid ) {
306 $vals['userid'] = (int)$recentChangeInfo['rc_user'];
307 // for backwards compatibility
308 $vals['user'] = (int)$recentChangeInfo['rc_user'];
309 }
310
311 if ( $this->fld_user ) {
312 $vals['user'] = $recentChangeInfo['rc_user_text'];
313 }
314
315 if ( !$recentChangeInfo['rc_user'] ) {
316 $vals['anon'] = true;
317 }
318 }
319 }
320
321 /* Add flags, such as new, minor, bot. */
322 if ( $this->fld_flags ) {
323 $vals['bot'] = (bool)$recentChangeInfo['rc_bot'];
324 $vals['new'] = $recentChangeInfo['rc_type'] == RC_NEW;
325 $vals['minor'] = (bool)$recentChangeInfo['rc_minor'];
326 }
327
328 /* Add sizes of each revision. (Only available on 1.10+) */
329 if ( $this->fld_sizes ) {
330 $vals['oldlen'] = intval( $recentChangeInfo['rc_old_len'] );
331 $vals['newlen'] = intval( $recentChangeInfo['rc_new_len'] );
332 }
333
334 /* Add the timestamp. */
335 if ( $this->fld_timestamp ) {
336 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $recentChangeInfo['rc_timestamp'] );
337 }
338
339 if ( $this->fld_notificationtimestamp ) {
340 $vals['notificationtimestamp'] = ( $watchedItem->getNotificationTimestamp() == null )
341 ? ''
342 : wfTimestamp( TS_ISO_8601, $watchedItem->getNotificationTimestamp() );
343 }
344
345 /* Add edit summary / log summary. */
346 if ( $this->fld_comment || $this->fld_parsedcomment ) {
347 if ( $recentChangeInfo['rc_deleted'] & Revision::DELETED_COMMENT ) {
348 $vals['commenthidden'] = true;
349 $anyHidden = true;
350 }
351 if ( Revision::userCanBitfield(
352 $recentChangeInfo['rc_deleted'],
353 Revision::DELETED_COMMENT,
354 $user
355 ) ) {
356 if ( $this->fld_comment && isset( $recentChangeInfo['rc_comment'] ) ) {
357 $vals['comment'] = $recentChangeInfo['rc_comment'];
358 }
359
360 if ( $this->fld_parsedcomment && isset( $recentChangeInfo['rc_comment'] ) ) {
361 $vals['parsedcomment'] = Linker::formatComment( $recentChangeInfo['rc_comment'], $title );
362 }
363 }
364 }
365
366 /* Add the patrolled flag */
367 if ( $this->fld_patrol ) {
368 $vals['patrolled'] = $recentChangeInfo['rc_patrolled'] == 1;
369 $vals['unpatrolled'] = ChangesList::isUnpatrolled( (object)$recentChangeInfo, $user );
370 }
371
372 if ( $this->fld_loginfo && $recentChangeInfo['rc_type'] == RC_LOG ) {
373 if ( $recentChangeInfo['rc_deleted'] & LogPage::DELETED_ACTION ) {
374 $vals['actionhidden'] = true;
375 $anyHidden = true;
376 }
377 if ( LogEventsList::userCanBitfield(
378 $recentChangeInfo['rc_deleted'],
379 LogPage::DELETED_ACTION,
380 $user
381 ) ) {
382 $vals['logid'] = intval( $recentChangeInfo['rc_logid'] );
383 $vals['logtype'] = $recentChangeInfo['rc_log_type'];
384 $vals['logaction'] = $recentChangeInfo['rc_log_action'];
385 $vals['logparams'] = LogFormatter::newFromRow( $recentChangeInfo )->formatParametersForApi();
386 }
387 }
388
389 if ( $anyHidden && ( $recentChangeInfo['rc_deleted'] & Revision::DELETED_RESTRICTED ) ) {
390 $vals['suppressed'] = true;
391 }
392
393 Hooks::run( 'ApiQueryWatchlistExtractOutputData', [
394 $this, $watchedItem, $recentChangeInfo, &$vals
395 ] );
396
397 return $vals;
398 }
399
400 public function getAllowedParams() {
401 return [
402 'allrev' => false,
403 'start' => [
404 ApiBase::PARAM_TYPE => 'timestamp'
405 ],
406 'end' => [
407 ApiBase::PARAM_TYPE => 'timestamp'
408 ],
409 'namespace' => [
410 ApiBase::PARAM_ISMULTI => true,
411 ApiBase::PARAM_TYPE => 'namespace'
412 ],
413 'user' => [
414 ApiBase::PARAM_TYPE => 'user',
415 ],
416 'excludeuser' => [
417 ApiBase::PARAM_TYPE => 'user',
418 ],
419 'dir' => [
420 ApiBase::PARAM_DFLT => 'older',
421 ApiBase::PARAM_TYPE => [
422 'newer',
423 'older'
424 ],
425 ApiHelp::PARAM_HELP_MSG => 'api-help-param-direction',
426 ],
427 'limit' => [
428 ApiBase::PARAM_DFLT => 10,
429 ApiBase::PARAM_TYPE => 'limit',
430 ApiBase::PARAM_MIN => 1,
431 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
432 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
433 ],
434 'prop' => [
435 ApiBase::PARAM_ISMULTI => true,
436 ApiBase::PARAM_DFLT => 'ids|title|flags',
437 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
438 ApiBase::PARAM_TYPE => [
439 'ids',
440 'title',
441 'flags',
442 'user',
443 'userid',
444 'comment',
445 'parsedcomment',
446 'timestamp',
447 'patrol',
448 'sizes',
449 'notificationtimestamp',
450 'loginfo',
451 ]
452 ],
453 'show' => [
454 ApiBase::PARAM_ISMULTI => true,
455 ApiBase::PARAM_TYPE => [
456 WatchedItemQueryService::FILTER_MINOR,
457 WatchedItemQueryService::FILTER_NOT_MINOR,
458 WatchedItemQueryService::FILTER_BOT,
459 WatchedItemQueryService::FILTER_NOT_BOT,
460 WatchedItemQueryService::FILTER_ANON,
461 WatchedItemQueryService::FILTER_NOT_ANON,
462 WatchedItemQueryService::FILTER_PATROLLED,
463 WatchedItemQueryService::FILTER_NOT_PATROLLED,
464 WatchedItemQueryService::FILTER_UNREAD,
465 WatchedItemQueryService::FILTER_NOT_UNREAD,
466 ]
467 ],
468 'type' => [
469 ApiBase::PARAM_DFLT => 'edit|new|log|categorize',
470 ApiBase::PARAM_ISMULTI => true,
471 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
472 ApiBase::PARAM_TYPE => RecentChange::getChangeTypes()
473 ],
474 'owner' => [
475 ApiBase::PARAM_TYPE => 'user'
476 ],
477 'token' => [
478 ApiBase::PARAM_TYPE => 'string',
479 ApiBase::PARAM_SENSITIVE => true,
480 ],
481 'continue' => [
482 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
483 ],
484 ];
485 }
486
487 protected function getExamplesMessages() {
488 return [
489 'action=query&list=watchlist'
490 => 'apihelp-query+watchlist-example-simple',
491 'action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment'
492 => 'apihelp-query+watchlist-example-props',
493 'action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment'
494 => 'apihelp-query+watchlist-example-allrev',
495 'action=query&generator=watchlist&prop=info'
496 => 'apihelp-query+watchlist-example-generator',
497 'action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user'
498 => 'apihelp-query+watchlist-example-generator-rev',
499 'action=query&list=watchlist&wlowner=Example&wltoken=123ABC'
500 => 'apihelp-query+watchlist-example-wlowner',
501 ];
502 }
503
504 public function getHelpUrls() {
505 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlist';
506 }
507 }