Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / api / ApiQueryWatchlist.php
blob3f597511a1325855bba8a18478ce86eda6600b5d
1 <?php
2 /**
5 * Created on Sep 25, 2006
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
24 * @file
27 use MediaWiki\MediaWikiServices;
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.
33 * @ingroup API
35 class ApiQueryWatchlist extends ApiQueryGeneratorBase {
37 public function __construct( ApiQuery $query, $moduleName ) {
38 parent::__construct( $query, $moduleName, 'wl' );
41 public function execute() {
42 $this->run();
45 public function executeGenerator( $resultPageSet ) {
46 $this->run( $resultPageSet );
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;
55 /**
56 * @param ApiPageSet $resultPageSet
57 * @return void
59 private function run( $resultPageSet = null ) {
60 $this->selectNamedDB( 'watchlist', DB_REPLICA, 'watchlist' );
62 $params = $this->extractRequestParams();
64 $user = $this->getUser();
65 $wlowner = $this->getWatchlistUser( $params );
67 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
68 $prop = array_flip( $params['prop'] );
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'] );
83 if ( $this->fld_patrol ) {
84 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
85 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'patrol' );
90 $options = [
91 'dir' => $params['dir'] === 'older'
92 ? WatchedItemQueryService::DIR_OLDER
93 : WatchedItemQueryService::DIR_NEWER,
96 if ( is_null( $resultPageSet ) ) {
97 $options['includeFields'] = $this->getFieldsToInclude();
98 } else {
99 $options['usedInGenerator'] = true;
102 if ( $params['start'] ) {
103 $options['start'] = $params['start'];
105 if ( $params['end'] ) {
106 $options['end'] = $params['end'];
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 ];
119 if ( $wlowner !== $user ) {
120 $options['watchlistOwner'] = $wlowner;
121 $options['watchlistOwnerToken'] = $params['token'];
124 if ( !is_null( $params['namespace'] ) ) {
125 $options['namespaceIds'] = $params['namespace'];
128 if ( $params['allrev'] ) {
129 $options['allRevisions'] = true;
132 if ( !is_null( $params['show'] ) ) {
133 $show = array_flip( $params['show'] );
135 /* Check for conflicting parameters. */
136 if ( $this->showParamsConflicting( $show ) ) {
137 $this->dieWithError( 'apierror-show' );
140 // Check permissions.
141 if ( isset( $show[WatchedItemQueryService::FILTER_PATROLLED] )
142 || isset( $show[WatchedItemQueryService::FILTER_NOT_PATROLLED] )
144 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
145 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
149 $options['filters'] = array_keys( $show );
152 if ( !is_null( $params['type'] ) ) {
153 try {
154 $rcTypes = RecentChange::parseToRCType( $params['type'] );
155 if ( $rcTypes ) {
156 $options['rcTypes'] = $rcTypes;
158 } catch ( Exception $e ) {
159 ApiBase::dieDebug( __METHOD__, $e->getMessage() );
163 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
164 if ( !is_null( $params['user'] ) ) {
165 $options['onlyByUser'] = $params['user'];
167 if ( !is_null( $params['excludeuser'] ) ) {
168 $options['notByUser'] = $params['excludeuser'];
171 $options['limit'] = $params['limit'];
173 Hooks::run( 'ApiQueryWatchlistPrepareWatchedItemQueryServiceOptions', [
174 $this, $params, &$options
175 ] );
177 $ids = [];
178 $count = 0;
179 $watchedItemQuery = MediaWikiServices::getInstance()->getWatchedItemQueryService();
180 $items = $watchedItemQuery->getWatchedItemsWithRecentChangeInfo( $wlowner, $options, $startFrom );
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;
191 } else {
192 if ( $params['allrev'] ) {
193 $ids[] = intval( $recentChangeInfo['rc_this_oldid'] );
194 } else {
195 $ids[] = intval( $recentChangeInfo['rc_cur_id'] );
200 if ( $startFrom !== null ) {
201 $this->setContinueEnumParameter( 'continue', implode( '|', $startFrom ) );
204 if ( is_null( $resultPageSet ) ) {
205 $this->getResult()->addIndexedTagName(
206 [ 'query', $this->getModuleName() ],
207 'item'
209 } elseif ( $params['allrev'] ) {
210 $resultPageSet->populateFromRevisionIDs( $ids );
211 } else {
212 $resultPageSet->populateFromPageIDs( $ids );
216 private function getFieldsToInclude() {
217 $includeFields = [];
218 if ( $this->fld_flags ) {
219 $includeFields[] = WatchedItemQueryService::INCLUDE_FLAGS;
221 if ( $this->fld_user || $this->fld_userid ) {
222 $includeFields[] = WatchedItemQueryService::INCLUDE_USER_ID;
224 if ( $this->fld_user ) {
225 $includeFields[] = WatchedItemQueryService::INCLUDE_USER;
227 if ( $this->fld_comment || $this->fld_parsedcomment ) {
228 $includeFields[] = WatchedItemQueryService::INCLUDE_COMMENT;
230 if ( $this->fld_patrol ) {
231 $includeFields[] = WatchedItemQueryService::INCLUDE_PATROL_INFO;
233 if ( $this->fld_sizes ) {
234 $includeFields[] = WatchedItemQueryService::INCLUDE_SIZES;
236 if ( $this->fld_loginfo ) {
237 $includeFields[] = WatchedItemQueryService::INCLUDE_LOG_INFO;
239 return $includeFields;
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] ) );
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()
261 $user = $this->getUser();
263 /* Our output data. */
264 $vals = [];
265 $type = intval( $recentChangeInfo['rc_type'] );
266 $vals['type'] = RecentChange::parseFromRCType( $type );
267 $anyHidden = false;
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;
276 if ( $type !== RC_LOG ||
277 LogEventsList::userCanBitfield(
278 $recentChangeInfo['rc_deleted'],
279 LogPage::DELETED_ACTION,
280 $user
283 if ( $this->fld_title ) {
284 ApiQueryBase::addTitleInfo( $vals, $title );
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'] );
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;
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'];
311 if ( $this->fld_user ) {
312 $vals['user'] = $recentChangeInfo['rc_user_text'];
315 if ( !$recentChangeInfo['rc_user'] ) {
316 $vals['anon'] = true;
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'];
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'] );
334 /* Add the timestamp. */
335 if ( $this->fld_timestamp ) {
336 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $recentChangeInfo['rc_timestamp'] );
339 if ( $this->fld_notificationtimestamp ) {
340 $vals['notificationtimestamp'] = ( $watchedItem->getNotificationTimestamp() == null )
341 ? ''
342 : wfTimestamp( TS_ISO_8601, $watchedItem->getNotificationTimestamp() );
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;
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'];
360 if ( $this->fld_parsedcomment && isset( $recentChangeInfo['rc_comment'] ) ) {
361 $vals['parsedcomment'] = Linker::formatComment( $recentChangeInfo['rc_comment'], $title );
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 );
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;
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();
389 if ( $anyHidden && ( $recentChangeInfo['rc_deleted'] & Revision::DELETED_RESTRICTED ) ) {
390 $vals['suppressed'] = true;
393 Hooks::run( 'ApiQueryWatchlistExtractOutputData', [
394 $this, $watchedItem, $recentChangeInfo, &$vals
395 ] );
397 return $vals;
400 public function getAllowedParams() {
401 return [
402 'allrev' => false,
403 'start' => [
404 ApiBase::PARAM_TYPE => 'timestamp'
406 'end' => [
407 ApiBase::PARAM_TYPE => 'timestamp'
409 'namespace' => [
410 ApiBase::PARAM_ISMULTI => true,
411 ApiBase::PARAM_TYPE => 'namespace'
413 'user' => [
414 ApiBase::PARAM_TYPE => 'user',
416 'excludeuser' => [
417 ApiBase::PARAM_TYPE => 'user',
419 'dir' => [
420 ApiBase::PARAM_DFLT => 'older',
421 ApiBase::PARAM_TYPE => [
422 'newer',
423 'older'
425 ApiHelp::PARAM_HELP_MSG => 'api-help-param-direction',
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
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',
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,
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()
474 'owner' => [
475 ApiBase::PARAM_TYPE => 'user'
477 'token' => [
478 ApiBase::PARAM_TYPE => 'string'
480 'continue' => [
481 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
486 protected function getExamplesMessages() {
487 return [
488 'action=query&list=watchlist'
489 => 'apihelp-query+watchlist-example-simple',
490 'action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment'
491 => 'apihelp-query+watchlist-example-props',
492 'action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment'
493 => 'apihelp-query+watchlist-example-allrev',
494 'action=query&generator=watchlist&prop=info'
495 => 'apihelp-query+watchlist-example-generator',
496 'action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user'
497 => 'apihelp-query+watchlist-example-generator-rev',
498 'action=query&list=watchlist&wlowner=Example&wltoken=123ABC'
499 => 'apihelp-query+watchlist-example-wlowner',
503 public function getHelpUrls() {
504 return 'https://www.mediawiki.org/wiki/API:Watchlist';