Merge "Add CollationFa"
[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 $options['rcTypes'] = RecentChange::parseToRCType( $params['type'] );
155 } catch ( Exception $e ) {
156 ApiBase::dieDebug( __METHOD__, $e->getMessage() );
157 }
158 }
159
160 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
161 if ( !is_null( $params['user'] ) ) {
162 $options['onlyByUser'] = $params['user'];
163 }
164 if ( !is_null( $params['excludeuser'] ) ) {
165 $options['notByUser'] = $params['excludeuser'];
166 }
167
168 $options['limit'] = $params['limit'];
169
170 Hooks::run( 'ApiQueryWatchlistPrepareWatchedItemQueryServiceOptions', [
171 $this, $params, &$options
172 ] );
173
174 $ids = [];
175 $count = 0;
176 $watchedItemQuery = MediaWikiServices::getInstance()->getWatchedItemQueryService();
177 $items = $watchedItemQuery->getWatchedItemsWithRecentChangeInfo( $wlowner, $options, $startFrom );
178
179 foreach ( $items as list ( $watchedItem, $recentChangeInfo ) ) {
180 /** @var WatchedItem $watchedItem */
181 if ( is_null( $resultPageSet ) ) {
182 $vals = $this->extractOutputData( $watchedItem, $recentChangeInfo );
183 $fit = $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
184 if ( !$fit ) {
185 $startFrom = [ $recentChangeInfo['rc_timestamp'], $recentChangeInfo['rc_id'] ];
186 break;
187 }
188 } else {
189 if ( $params['allrev'] ) {
190 $ids[] = intval( $recentChangeInfo['rc_this_oldid'] );
191 } else {
192 $ids[] = intval( $recentChangeInfo['rc_cur_id'] );
193 }
194 }
195 }
196
197 if ( $startFrom !== null ) {
198 $this->setContinueEnumParameter( 'continue', implode( '|', $startFrom ) );
199 }
200
201 if ( is_null( $resultPageSet ) ) {
202 $this->getResult()->addIndexedTagName(
203 [ 'query', $this->getModuleName() ],
204 'item'
205 );
206 } elseif ( $params['allrev'] ) {
207 $resultPageSet->populateFromRevisionIDs( $ids );
208 } else {
209 $resultPageSet->populateFromPageIDs( $ids );
210 }
211 }
212
213 private function getFieldsToInclude() {
214 $includeFields = [];
215 if ( $this->fld_flags ) {
216 $includeFields[] = WatchedItemQueryService::INCLUDE_FLAGS;
217 }
218 if ( $this->fld_user || $this->fld_userid ) {
219 $includeFields[] = WatchedItemQueryService::INCLUDE_USER_ID;
220 }
221 if ( $this->fld_user ) {
222 $includeFields[] = WatchedItemQueryService::INCLUDE_USER;
223 }
224 if ( $this->fld_comment || $this->fld_parsedcomment ) {
225 $includeFields[] = WatchedItemQueryService::INCLUDE_COMMENT;
226 }
227 if ( $this->fld_patrol ) {
228 $includeFields[] = WatchedItemQueryService::INCLUDE_PATROL_INFO;
229 }
230 if ( $this->fld_sizes ) {
231 $includeFields[] = WatchedItemQueryService::INCLUDE_SIZES;
232 }
233 if ( $this->fld_loginfo ) {
234 $includeFields[] = WatchedItemQueryService::INCLUDE_LOG_INFO;
235 }
236 return $includeFields;
237 }
238
239 private function showParamsConflicting( array $show ) {
240 return ( isset( $show[WatchedItemQueryService::FILTER_MINOR] )
241 && isset( $show[WatchedItemQueryService::FILTER_NOT_MINOR] ) )
242 || ( isset( $show[WatchedItemQueryService::FILTER_BOT] )
243 && isset( $show[WatchedItemQueryService::FILTER_NOT_BOT] ) )
244 || ( isset( $show[WatchedItemQueryService::FILTER_ANON] )
245 && isset( $show[WatchedItemQueryService::FILTER_NOT_ANON] ) )
246 || ( isset( $show[WatchedItemQueryService::FILTER_PATROLLED] )
247 && isset( $show[WatchedItemQueryService::FILTER_NOT_PATROLLED] ) )
248 || ( isset( $show[WatchedItemQueryService::FILTER_UNREAD] )
249 && isset( $show[WatchedItemQueryService::FILTER_NOT_UNREAD] ) );
250 }
251
252 private function extractOutputData( WatchedItem $watchedItem, array $recentChangeInfo ) {
253 /* Determine the title of the page that has been changed. */
254 $title = Title::makeTitle(
255 $watchedItem->getLinkTarget()->getNamespace(),
256 $watchedItem->getLinkTarget()->getDBkey()
257 );
258 $user = $this->getUser();
259
260 /* Our output data. */
261 $vals = [];
262 $type = intval( $recentChangeInfo['rc_type'] );
263 $vals['type'] = RecentChange::parseFromRCType( $type );
264 $anyHidden = false;
265
266 /* Create a new entry in the result for the title. */
267 if ( $this->fld_title || $this->fld_ids ) {
268 // These should already have been filtered out of the query, but just in case.
269 if ( $type === RC_LOG && ( $recentChangeInfo['rc_deleted'] & LogPage::DELETED_ACTION ) ) {
270 $vals['actionhidden'] = true;
271 $anyHidden = true;
272 }
273 if ( $type !== RC_LOG ||
274 LogEventsList::userCanBitfield(
275 $recentChangeInfo['rc_deleted'],
276 LogPage::DELETED_ACTION,
277 $user
278 )
279 ) {
280 if ( $this->fld_title ) {
281 ApiQueryBase::addTitleInfo( $vals, $title );
282 }
283 if ( $this->fld_ids ) {
284 $vals['pageid'] = intval( $recentChangeInfo['rc_cur_id'] );
285 $vals['revid'] = intval( $recentChangeInfo['rc_this_oldid'] );
286 $vals['old_revid'] = intval( $recentChangeInfo['rc_last_oldid'] );
287 }
288 }
289 }
290
291 /* Add user data and 'anon' flag, if user is anonymous. */
292 if ( $this->fld_user || $this->fld_userid ) {
293 if ( $recentChangeInfo['rc_deleted'] & Revision::DELETED_USER ) {
294 $vals['userhidden'] = true;
295 $anyHidden = true;
296 }
297 if ( Revision::userCanBitfield(
298 $recentChangeInfo['rc_deleted'],
299 Revision::DELETED_USER,
300 $user
301 ) ) {
302 if ( $this->fld_userid ) {
303 $vals['userid'] = (int)$recentChangeInfo['rc_user'];
304 // for backwards compatibility
305 $vals['user'] = (int)$recentChangeInfo['rc_user'];
306 }
307
308 if ( $this->fld_user ) {
309 $vals['user'] = $recentChangeInfo['rc_user_text'];
310 }
311
312 if ( !$recentChangeInfo['rc_user'] ) {
313 $vals['anon'] = true;
314 }
315 }
316 }
317
318 /* Add flags, such as new, minor, bot. */
319 if ( $this->fld_flags ) {
320 $vals['bot'] = (bool)$recentChangeInfo['rc_bot'];
321 $vals['new'] = $recentChangeInfo['rc_type'] == RC_NEW;
322 $vals['minor'] = (bool)$recentChangeInfo['rc_minor'];
323 }
324
325 /* Add sizes of each revision. (Only available on 1.10+) */
326 if ( $this->fld_sizes ) {
327 $vals['oldlen'] = intval( $recentChangeInfo['rc_old_len'] );
328 $vals['newlen'] = intval( $recentChangeInfo['rc_new_len'] );
329 }
330
331 /* Add the timestamp. */
332 if ( $this->fld_timestamp ) {
333 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $recentChangeInfo['rc_timestamp'] );
334 }
335
336 if ( $this->fld_notificationtimestamp ) {
337 $vals['notificationtimestamp'] = ( $watchedItem->getNotificationTimestamp() == null )
338 ? ''
339 : wfTimestamp( TS_ISO_8601, $watchedItem->getNotificationTimestamp() );
340 }
341
342 /* Add edit summary / log summary. */
343 if ( $this->fld_comment || $this->fld_parsedcomment ) {
344 if ( $recentChangeInfo['rc_deleted'] & Revision::DELETED_COMMENT ) {
345 $vals['commenthidden'] = true;
346 $anyHidden = true;
347 }
348 if ( Revision::userCanBitfield(
349 $recentChangeInfo['rc_deleted'],
350 Revision::DELETED_COMMENT,
351 $user
352 ) ) {
353 if ( $this->fld_comment && isset( $recentChangeInfo['rc_comment'] ) ) {
354 $vals['comment'] = $recentChangeInfo['rc_comment'];
355 }
356
357 if ( $this->fld_parsedcomment && isset( $recentChangeInfo['rc_comment'] ) ) {
358 $vals['parsedcomment'] = Linker::formatComment( $recentChangeInfo['rc_comment'], $title );
359 }
360 }
361 }
362
363 /* Add the patrolled flag */
364 if ( $this->fld_patrol ) {
365 $vals['patrolled'] = $recentChangeInfo['rc_patrolled'] == 1;
366 $vals['unpatrolled'] = ChangesList::isUnpatrolled( (object)$recentChangeInfo, $user );
367 }
368
369 if ( $this->fld_loginfo && $recentChangeInfo['rc_type'] == RC_LOG ) {
370 if ( $recentChangeInfo['rc_deleted'] & LogPage::DELETED_ACTION ) {
371 $vals['actionhidden'] = true;
372 $anyHidden = true;
373 }
374 if ( LogEventsList::userCanBitfield(
375 $recentChangeInfo['rc_deleted'],
376 LogPage::DELETED_ACTION,
377 $user
378 ) ) {
379 $vals['logid'] = intval( $recentChangeInfo['rc_logid'] );
380 $vals['logtype'] = $recentChangeInfo['rc_log_type'];
381 $vals['logaction'] = $recentChangeInfo['rc_log_action'];
382 $vals['logparams'] = LogFormatter::newFromRow( $recentChangeInfo )->formatParametersForApi();
383 }
384 }
385
386 if ( $anyHidden && ( $recentChangeInfo['rc_deleted'] & Revision::DELETED_RESTRICTED ) ) {
387 $vals['suppressed'] = true;
388 }
389
390 Hooks::run( 'ApiQueryWatchlistExtractOutputData', [
391 $this, $watchedItem, $recentChangeInfo, &$vals
392 ] );
393
394 return $vals;
395 }
396
397 public function getAllowedParams() {
398 return [
399 'allrev' => false,
400 'start' => [
401 ApiBase::PARAM_TYPE => 'timestamp'
402 ],
403 'end' => [
404 ApiBase::PARAM_TYPE => 'timestamp'
405 ],
406 'namespace' => [
407 ApiBase::PARAM_ISMULTI => true,
408 ApiBase::PARAM_TYPE => 'namespace'
409 ],
410 'user' => [
411 ApiBase::PARAM_TYPE => 'user',
412 ],
413 'excludeuser' => [
414 ApiBase::PARAM_TYPE => 'user',
415 ],
416 'dir' => [
417 ApiBase::PARAM_DFLT => 'older',
418 ApiBase::PARAM_TYPE => [
419 'newer',
420 'older'
421 ],
422 ApiHelp::PARAM_HELP_MSG => 'api-help-param-direction',
423 ],
424 'limit' => [
425 ApiBase::PARAM_DFLT => 10,
426 ApiBase::PARAM_TYPE => 'limit',
427 ApiBase::PARAM_MIN => 1,
428 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
429 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
430 ],
431 'prop' => [
432 ApiBase::PARAM_ISMULTI => true,
433 ApiBase::PARAM_DFLT => 'ids|title|flags',
434 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
435 ApiBase::PARAM_TYPE => [
436 'ids',
437 'title',
438 'flags',
439 'user',
440 'userid',
441 'comment',
442 'parsedcomment',
443 'timestamp',
444 'patrol',
445 'sizes',
446 'notificationtimestamp',
447 'loginfo',
448 ]
449 ],
450 'show' => [
451 ApiBase::PARAM_ISMULTI => true,
452 ApiBase::PARAM_TYPE => [
453 WatchedItemQueryService::FILTER_MINOR,
454 WatchedItemQueryService::FILTER_NOT_MINOR,
455 WatchedItemQueryService::FILTER_BOT,
456 WatchedItemQueryService::FILTER_NOT_BOT,
457 WatchedItemQueryService::FILTER_ANON,
458 WatchedItemQueryService::FILTER_NOT_ANON,
459 WatchedItemQueryService::FILTER_PATROLLED,
460 WatchedItemQueryService::FILTER_NOT_PATROLLED,
461 WatchedItemQueryService::FILTER_UNREAD,
462 WatchedItemQueryService::FILTER_NOT_UNREAD,
463 ]
464 ],
465 'type' => [
466 ApiBase::PARAM_DFLT => 'edit|new|log|categorize',
467 ApiBase::PARAM_ISMULTI => true,
468 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
469 ApiBase::PARAM_TYPE => RecentChange::getChangeTypes()
470 ],
471 'owner' => [
472 ApiBase::PARAM_TYPE => 'user'
473 ],
474 'token' => [
475 ApiBase::PARAM_TYPE => 'string'
476 ],
477 'continue' => [
478 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
479 ],
480 ];
481 }
482
483 protected function getExamplesMessages() {
484 return [
485 'action=query&list=watchlist'
486 => 'apihelp-query+watchlist-example-simple',
487 'action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment'
488 => 'apihelp-query+watchlist-example-props',
489 'action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment'
490 => 'apihelp-query+watchlist-example-allrev',
491 'action=query&generator=watchlist&prop=info'
492 => 'apihelp-query+watchlist-example-generator',
493 'action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user'
494 => 'apihelp-query+watchlist-example-generator-rev',
495 'action=query&list=watchlist&wlowner=Example&wltoken=123ABC'
496 => 'apihelp-query+watchlist-example-wlowner',
497 ];
498 }
499
500 public function getHelpUrls() {
501 return 'https://www.mediawiki.org/wiki/API:Watchlist';
502 }
503 }