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