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