Completed
Branch master (a553dc)
by
unknown
26:33
created

ApiQueryWatchlist::showParamsConflicting()   B

Complexity

Conditions 10
Paths 47

Size

Total Lines 12
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 11
c 1
b 0
f 0
nc 47
nop 1
dl 0
loc 12
rs 7.2765

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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,
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
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_SLAVE, '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->dieUsage( 'patrol property is not available', '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
		if ( !is_null( $params['continue'] ) ) {
110
			$cont = explode( '|', $params['continue'] );
111
			$this->dieContinueUsageIf( count( $cont ) != 2 );
112
			$continueTimestamp = $cont[0];
113
			$continueId = (int)$cont[1];
114
			$this->dieContinueUsageIf( $continueId != $cont[1] );
115
			$options['startFrom'] = [ $continueTimestamp, $continueId ];
116
		}
117
118
		if ( $wlowner !== $user ) {
119
			$options['watchlistOwner'] = $wlowner;
120
			$options['watchlistOwnerToken'] = $params['token'];
121
		}
122
123
		if ( !is_null( $params['namespace'] ) ) {
124
			$options['namespaceIds'] = $params['namespace'];
125
		}
126
127
		if ( $params['allrev'] ) {
128
			$options['allRevisions'] = true;
129
		}
130
131
		if ( !is_null( $params['show'] ) ) {
132
			$show = array_flip( $params['show'] );
133
134
			/* Check for conflicting parameters. */
135
			if ( $this->showParamsConflicting( $show ) ) {
136
				$this->dieUsageMsg( 'show' );
137
			}
138
139
			// Check permissions.
140
			if ( isset( $show[WatchedItemQueryService::FILTER_PATROLLED] )
141
				|| isset( $show[WatchedItemQueryService::FILTER_NOT_PATROLLED] )
142
			) {
143
				if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
144
					$this->dieUsage(
145
						'You need the patrol right to request the patrolled flag',
146
						'permissiondenied'
147
					);
148
				}
149
			}
150
151
			$options['filters'] = array_keys( $show );
152
		}
153
154 View Code Duplication
		if ( !is_null( $params['type'] ) ) {
155
			try {
156
				$options['rcTypes'] = RecentChange::parseToRCType( $params['type'] );
157
			} catch ( Exception $e ) {
158
				ApiBase::dieDebug( __METHOD__, $e->getMessage() );
159
			}
160
		}
161
162 View Code Duplication
		if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
163
			$this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
164
		}
165
		if ( !is_null( $params['user'] ) ) {
166
			$options['onlyByUser'] = $params['user'];
167
		}
168
		if ( !is_null( $params['excludeuser'] ) ) {
169
			$options['notByUser'] = $params['excludeuser'];
170
		}
171
172
		$options['limit'] = $params['limit'] + 1;
173
174
		$ids = [];
175
		$count = 0;
176
		$watchedItemQuery = MediaWikiServices::getInstance()->getWatchedItemQueryService();
177
		$items = $watchedItemQuery->getWatchedItemsWithRecentChangeInfo( $wlowner, $options );
178
179
		foreach ( $items as list ( $watchedItem, $recentChangeInfo ) ) {
180
			/** @var WatchedItem $watchedItem */
181 View Code Duplication
			if ( ++$count > $params['limit'] ) {
182
				// We've reached the one extra which shows that there are
183
				// additional pages to be had. Stop here...
184
				$this->setContinueEnumParameter(
185
					'continue',
186
					$recentChangeInfo['rc_timestamp'] . '|' . $recentChangeInfo['rc_id']
187
				);
188
				break;
189
			}
190
191
			if ( is_null( $resultPageSet ) ) {
192
				$vals = $this->extractOutputData( $watchedItem, $recentChangeInfo );
193
				$fit = $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
194 View Code Duplication
				if ( !$fit ) {
195
					$this->setContinueEnumParameter(
196
						'continue',
197
						$recentChangeInfo['rc_timestamp'] . '|' . $recentChangeInfo['rc_id']
198
					);
199
					break;
200
				}
201
			} else {
202
				if ( $params['allrev'] ) {
203
					$ids[] = intval( $recentChangeInfo['rc_this_oldid'] );
204
				} else {
205
					$ids[] = intval( $recentChangeInfo['rc_cur_id'] );
206
				}
207
			}
208
		}
209
210 View Code Duplication
		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 View Code Duplication
			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 View Code Duplication
			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 View Code Duplication
			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
				if ( $this->fld_comment && isset( $recentChangeInfo['rc_comment'] ) ) {
363
					$vals['comment'] = $recentChangeInfo['rc_comment'];
364
				}
365
366
				if ( $this->fld_parsedcomment && isset( $recentChangeInfo['rc_comment'] ) ) {
367
					$vals['parsedcomment'] = Linker::formatComment( $recentChangeInfo['rc_comment'], $title );
368
				}
369
			}
370
		}
371
372
		/* Add the patrolled flag */
373 View Code Duplication
		if ( $this->fld_patrol ) {
374
			$vals['patrolled'] = $recentChangeInfo['rc_patrolled'] == 1;
375
			$vals['unpatrolled'] = ChangesList::isUnpatrolled( (object)$recentChangeInfo, $user );
376
		}
377
378
		if ( $this->fld_loginfo && $recentChangeInfo['rc_type'] == RC_LOG ) {
379 View Code Duplication
			if ( $recentChangeInfo['rc_deleted'] & LogPage::DELETED_ACTION ) {
380
				$vals['actionhidden'] = true;
381
				$anyHidden = true;
382
			}
383
			if ( LogEventsList::userCanBitfield(
384
				$recentChangeInfo['rc_deleted'],
385
				LogPage::DELETED_ACTION,
386
				$user
387
			) ) {
388
				$vals['logid'] = intval( $recentChangeInfo['rc_logid'] );
389
				$vals['logtype'] = $recentChangeInfo['rc_log_type'];
390
				$vals['logaction'] = $recentChangeInfo['rc_log_action'];
391
				$vals['logparams'] = LogFormatter::newFromRow( $recentChangeInfo )->formatParametersForApi();
392
			}
393
		}
394
395
		if ( $anyHidden && ( $recentChangeInfo['rc_deleted'] & Revision::DELETED_RESTRICTED ) ) {
396
			$vals['suppressed'] = true;
397
		}
398
399
		return $vals;
400
	}
401
402
	public function getAllowedParams() {
403
		return [
404
			'allrev' => false,
405
			'start' => [
406
				ApiBase::PARAM_TYPE => 'timestamp'
407
			],
408
			'end' => [
409
				ApiBase::PARAM_TYPE => 'timestamp'
410
			],
411
			'namespace' => [
412
				ApiBase::PARAM_ISMULTI => true,
413
				ApiBase::PARAM_TYPE => 'namespace'
414
			],
415
			'user' => [
416
				ApiBase::PARAM_TYPE => 'user',
417
			],
418
			'excludeuser' => [
419
				ApiBase::PARAM_TYPE => 'user',
420
			],
421
			'dir' => [
422
				ApiBase::PARAM_DFLT => 'older',
423
				ApiBase::PARAM_TYPE => [
424
					'newer',
425
					'older'
426
				],
427
				ApiHelp::PARAM_HELP_MSG => 'api-help-param-direction',
428
			],
429
			'limit' => [
430
				ApiBase::PARAM_DFLT => 10,
431
				ApiBase::PARAM_TYPE => 'limit',
432
				ApiBase::PARAM_MIN => 1,
433
				ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
434
				ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
435
			],
436
			'prop' => [
437
				ApiBase::PARAM_ISMULTI => true,
438
				ApiBase::PARAM_DFLT => 'ids|title|flags',
439
				ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
440
				ApiBase::PARAM_TYPE => [
441
					'ids',
442
					'title',
443
					'flags',
444
					'user',
445
					'userid',
446
					'comment',
447
					'parsedcomment',
448
					'timestamp',
449
					'patrol',
450
					'sizes',
451
					'notificationtimestamp',
452
					'loginfo',
453
				]
454
			],
455
			'show' => [
456
				ApiBase::PARAM_ISMULTI => true,
457
				ApiBase::PARAM_TYPE => [
458
					WatchedItemQueryService::FILTER_MINOR,
459
					WatchedItemQueryService::FILTER_NOT_MINOR,
460
					WatchedItemQueryService::FILTER_BOT,
461
					WatchedItemQueryService::FILTER_NOT_BOT,
462
					WatchedItemQueryService::FILTER_ANON,
463
					WatchedItemQueryService::FILTER_NOT_ANON,
464
					WatchedItemQueryService::FILTER_PATROLLED,
465
					WatchedItemQueryService::FILTER_NOT_PATROLLED,
466
					WatchedItemQueryService::FILTER_UNREAD,
467
					WatchedItemQueryService::FILTER_NOT_UNREAD,
468
				]
469
			],
470
			'type' => [
471
				ApiBase::PARAM_DFLT => 'edit|new|log|categorize',
472
				ApiBase::PARAM_ISMULTI => true,
473
				ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
474
				ApiBase::PARAM_TYPE => RecentChange::getChangeTypes()
475
			],
476
			'owner' => [
477
				ApiBase::PARAM_TYPE => 'user'
478
			],
479
			'token' => [
480
				ApiBase::PARAM_TYPE => 'string'
481
			],
482
			'continue' => [
483
				ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
484
			],
485
		];
486
	}
487
488
	protected function getExamplesMessages() {
489
		return [
490
			'action=query&list=watchlist'
491
				=> 'apihelp-query+watchlist-example-simple',
492
			'action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment'
493
				=> 'apihelp-query+watchlist-example-props',
494
			'action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment'
495
				=> 'apihelp-query+watchlist-example-allrev',
496
			'action=query&generator=watchlist&prop=info'
497
				=> 'apihelp-query+watchlist-example-generator',
498
			'action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user'
499
				=> 'apihelp-query+watchlist-example-generator-rev',
500
			'action=query&list=watchlist&wlowner=Example&wltoken=123ABC'
501
				=> 'apihelp-query+watchlist-example-wlowner',
502
		];
503
	}
504
505
	public function getHelpUrls() {
506
		return 'https://www.mediawiki.org/wiki/API:Watchlist';
507
	}
508
}
509