1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @author DannyS712
*/
namespace MediaWiki\Watchlist;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\HookContainer\HookRunner;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\Page\PageIdentity;
use MediaWiki\Page\PageReference;
use MediaWiki\Page\WikiPageFactory;
use MediaWiki\Permissions\Authority;
use MediaWiki\Revision\RevisionLookup;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Status\Status;
use MediaWiki\Title\NamespaceInfo;
use MediaWiki\User\TalkPageNotificationManager;
use MediaWiki\User\User;
use MediaWiki\User\UserFactory;
use MediaWiki\User\UserIdentity;
use StatusValue;
use Wikimedia\ParamValidator\TypeDef\ExpiryDef;
use Wikimedia\Rdbms\ReadOnlyMode;
/**
* WatchlistManager service
*
* @since 1.35
*/
class WatchlistManager {
/**
* @internal For use by ServiceWiring
*/
public const OPTION_ENOTIF = 'isEnotifEnabled';
/** @var bool */
private $isEnotifEnabled;
/** @var HookRunner */
private $hookRunner;
/** @var ReadOnlyMode */
private $readOnlyMode;
/** @var RevisionLookup */
private $revisionLookup;
/** @var TalkPageNotificationManager */
private $talkPageNotificationManager;
/** @var WatchedItemStoreInterface */
private $watchedItemStore;
/** @var UserFactory */
private $userFactory;
/** @var NamespaceInfo */
private $nsInfo;
/** @var WikiPageFactory */
private $wikiPageFactory;
/**
* @var array
*
* Cache for getTitleNotificationTimestamp
*
* Keys need to reflect both the specific user and the title:
*
* Since only users have watchlists, the user is represented with `u⧼user id⧽`
*
* Since the method accepts LinkTarget objects, cannot rely on the object's toString,
* since it is different for TitleValue and Title. Implement a simplified string
* representation of the string that TitleValue uses: `⧼namespace number⧽:⧼db key⧽`
*
* Entries are in the form of
* u⧼user id⧽-⧼namespace number⧽:⧼db key⧽ => ⧼timestamp or false⧽
*/
private $notificationTimestampCache = [];
/**
* @param array{isEnotifEnabled:bool} $options
* @param HookContainer $hookContainer
* @param ReadOnlyMode $readOnlyMode
* @param RevisionLookup $revisionLookup
* @param TalkPageNotificationManager $talkPageNotificationManager
* @param WatchedItemStoreInterface $watchedItemStore
* @param UserFactory $userFactory
* @param NamespaceInfo $nsInfo
* @param WikiPageFactory $wikiPageFactory
*/
public function __construct(
array $options,
HookContainer $hookContainer,
ReadOnlyMode $readOnlyMode,
RevisionLookup $revisionLookup,
TalkPageNotificationManager $talkPageNotificationManager,
WatchedItemStoreInterface $watchedItemStore,
UserFactory $userFactory,
NamespaceInfo $nsInfo,
WikiPageFactory $wikiPageFactory
) {
$this->isEnotifEnabled = $options[ self::OPTION_ENOTIF ];
$this->hookRunner = new HookRunner( $hookContainer );
$this->readOnlyMode = $readOnlyMode;
$this->revisionLookup = $revisionLookup;
$this->talkPageNotificationManager = $talkPageNotificationManager;
$this->watchedItemStore = $watchedItemStore;
$this->userFactory = $userFactory;
$this->nsInfo = $nsInfo;
$this->wikiPageFactory = $wikiPageFactory;
}
/**
* Resets all of the given user's page-change notification timestamps.
* If e-notif e-mails are on, they will receive notification mails on
* the next change of any watched page.
*
* @note If the user doesn't have 'editmywatchlist', this will do nothing.
*
* @param Authority|UserIdentity $performer deprecated passing UserIdentity since 1.37
*/
public function clearAllUserNotifications( $performer ) {
if ( $this->readOnlyMode->isReadOnly() ) {
// Cannot change anything in read only
return;
}
if ( !$performer instanceof Authority ) {
$performer = $this->userFactory->newFromUserIdentity( $performer );
}
$user = $performer->getUser();
// NOTE: Has to be before `editmywatchlist` user right check, to ensure
// anonymous / temporary users have their talk page notifications cleared (T345031).
if ( !$this->isEnotifEnabled ) {
$this->talkPageNotificationManager->removeUserHasNewMessages( $user );
return;
}
if ( !$performer->isAllowed( 'editmywatchlist' ) ) {
// User isn't allowed to edit the watchlist
return;
}
if ( !$user->isRegistered() ) {
return;
}
$this->watchedItemStore->resetAllNotificationTimestampsForUser( $user );
// We also need to clear here the "you have new message" notification for the own
// user_talk page; it's cleared one page view later in WikiPage::doViewUpdates().
}
/**
* Clear the user's notification timestamp for the given title.
* If e-notif e-mails are on, they will receive notification mails on
* the next change of the page if it's watched etc.
*
* @note If the user doesn't have 'editmywatchlist', this will do nothing.
*
* @param Authority|UserIdentity $performer deprecated passing UserIdentity since 1.37
* @param LinkTarget|PageIdentity $title deprecated passing LinkTarget since 1.37
* @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
* @param RevisionRecord|null $oldRev The revision record associated with $oldid, or null if
* the latest revision is used
*/
public function clearTitleUserNotifications(
$performer,
$title,
int $oldid = 0,
?RevisionRecord $oldRev = null
) {
if ( $this->readOnlyMode->isReadOnly() ) {
// Cannot change anything in read only
return;
}
if ( !$performer instanceof Authority ) {
$performer = $this->userFactory->newFromUserIdentity( $performer );
}
$userIdentity = $performer->getUser();
$userTalkPage = (
$title->getNamespace() === NS_USER_TALK &&
$title->getDBkey() === strtr( $userIdentity->getName(), ' ', '_' )
);
if ( $userTalkPage ) {
if ( !$oldid ) {
$oldRev = null;
} elseif ( !$oldRev ) {
$oldRev = $this->revisionLookup->getRevisionById( $oldid );
}
// NOTE: Has to be called before isAllowed() check, to ensure users with no watchlist
// access (by default, temporary and anonymous users) can clear their talk page
// notification (T345031).
$this->talkPageNotificationManager->clearForPageView( $userIdentity, $oldRev );
}
if ( !$this->isEnotifEnabled ) {
return;
}
if ( !$userIdentity->isRegistered() ) {
// Nothing else to do
return;
}
// NOTE: Has to be checked after the TalkPageNotificationManager::clearForPageView call, to
// ensure users with no watchlist access (by default, temporary and anonymous users) can
// clear their talk page notification (T345031).
if ( !$performer->isAllowed( 'editmywatchlist' ) ) {
// User isn't allowed to edit the watchlist
return;
}
// Only update the timestamp if the page is being watched.
// The query to find out if it is watched is cached both in memcached and per-invocation,
// and when it does have to be executed, it can be on a replica DB
// If this is the user's newtalk page, we always update the timestamp
$force = $userTalkPage ? 'force' : '';
$this->watchedItemStore->resetNotificationTimestamp( $userIdentity, $title, $force, $oldid );
}
/**
* Get the timestamp when this page was updated since the user last saw it.
*
* @param UserIdentity $user
* @param LinkTarget|PageIdentity $title deprecated passing LinkTarget since 1.37
* @return string|bool|null String timestamp, false if not watched, null if nothing is unseen
*/
public function getTitleNotificationTimestamp( UserIdentity $user, $title ) {
if ( !$user->isRegistered() ) {
return false;
}
$cacheKey = 'u' . $user->getId() . '-' .
$title->getNamespace() . ':' . $title->getDBkey();
// avoid isset here, as it'll return false for null entries
if ( array_key_exists( $cacheKey, $this->notificationTimestampCache ) ) {
return $this->notificationTimestampCache[ $cacheKey ];
}
$watchedItem = $this->watchedItemStore->getWatchedItem( $user, $title );
if ( $watchedItem ) {
$timestamp = $watchedItem->getNotificationTimestamp();
} else {
$timestamp = false;
}
$this->notificationTimestampCache[ $cacheKey ] = $timestamp;
return $timestamp;
}
/**
* @since 1.37
* @param PageReference $target
* @return bool
*/
public function isWatchable( PageReference $target ): bool {
if ( !$this->nsInfo->isWatchable( $target->getNamespace() ) ) {
return false;
}
if ( $target instanceof PageIdentity && !$target->canExist() ) {
// Catch "improper" Title instances
return false;
}
return true;
}
/**
* Check if the page is watched by the user.
* @since 1.37
* @param UserIdentity $userIdentity
* @param PageIdentity $target
* @return bool
*/
public function isWatchedIgnoringRights( UserIdentity $userIdentity, PageIdentity $target ): bool {
if ( $this->isWatchable( $target ) ) {
return $this->watchedItemStore->isWatched( $userIdentity, $target );
}
return false;
}
/**
* Check if the page is watched by the user and the user has permission to view their
* watchlist.
* @since 1.37
* @param Authority $performer
* @param PageIdentity $target
* @return bool
*/
public function isWatched( Authority $performer, PageIdentity $target ): bool {
if ( $performer->isAllowed( 'viewmywatchlist' ) ) {
return $this->isWatchedIgnoringRights( $performer->getUser(), $target );
}
return false;
}
/**
* Check if the article is temporarily watched by the user.
* @since 1.37
* @param UserIdentity $userIdentity
* @param PageIdentity $target
* @return bool
*/
public function isTempWatchedIgnoringRights( UserIdentity $userIdentity, PageIdentity $target ): bool {
if ( $this->isWatchable( $target ) ) {
return $this->watchedItemStore->isTempWatched( $userIdentity, $target );
}
return false;
}
/**
* Check if the page is temporarily watched by the user and the user has permission to view
* their watchlist.
* @since 1.37
* @param Authority $performer
* @param PageIdentity $target
* @return bool
*/
public function isTempWatched( Authority $performer, PageIdentity $target ): bool {
if ( $performer->isAllowed( 'viewmywatchlist' ) ) {
return $this->isTempWatchedIgnoringRights( $performer->getUser(), $target );
}
return false;
}
/**
* Watch a page. Calls the WatchArticle and WatchArticleComplete hooks.
* @since 1.37
* @param UserIdentity $userIdentity
* @param PageIdentity $target
* @param string|null $expiry Optional expiry timestamp in any format acceptable to wfTimestamp(),
* null will not create expiries, or leave them unchanged should they already exist.
* @return StatusValue
*/
public function addWatchIgnoringRights(
UserIdentity $userIdentity,
PageIdentity $target,
?string $expiry = null
): StatusValue {
if ( !$this->isWatchable( $target ) ) {
return StatusValue::newFatal( 'watchlistnotwatchable' );
}
$wikiPage = $this->wikiPageFactory->newFromTitle( $target );
$title = $wikiPage->getTitle();
// TODO: update hooks to take Authority
$status = Status::newFatal( 'hookaborted' );
$user = $this->userFactory->newFromUserIdentity( $userIdentity );
if ( $this->hookRunner->onWatchArticle( $user, $wikiPage, $status, $expiry ) ) {
$status = StatusValue::newGood();
$this->watchedItemStore->addWatch( $userIdentity, $this->nsInfo->getSubjectPage( $title ), $expiry );
if ( $this->nsInfo->canHaveTalkPage( $title ) ) {
$this->watchedItemStore->addWatch( $userIdentity, $this->nsInfo->getTalkPage( $title ), $expiry );
}
$this->hookRunner->onWatchArticleComplete( $user, $wikiPage );
}
// eventually user_touched should be factored out of User and this should be replaced
$user->invalidateCache();
return $status;
}
/**
* Watch a page if the user has permission to edit their watchlist.
* Calls the WatchArticle and WatchArticleComplete hooks.
* @since 1.37
* @param Authority $performer
* @param PageIdentity $target
* @param string|null $expiry Optional expiry timestamp in any format acceptable to wfTimestamp(),
* null will not create expiries, or leave them unchanged should they already exist.
* @return StatusValue
*/
public function addWatch(
Authority $performer,
PageIdentity $target,
?string $expiry = null
): StatusValue {
if ( !$performer->isAllowed( 'editmywatchlist' ) ) {
// TODO: this function should be moved out of User
return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
}
return $this->addWatchIgnoringRights( $performer->getUser(), $target, $expiry );
}
/**
* Stop watching a page. Calls the UnwatchArticle and UnwatchArticleComplete hooks.
* @since 1.37
* @param UserIdentity $userIdentity
* @param PageIdentity $target
* @return StatusValue
*/
public function removeWatchIgnoringRights(
UserIdentity $userIdentity,
PageIdentity $target
): StatusValue {
if ( !$this->isWatchable( $target ) ) {
return StatusValue::newFatal( 'watchlistnotwatchable' );
}
$wikiPage = $this->wikiPageFactory->newFromTitle( $target );
$title = $wikiPage->getTitle();
// TODO: update hooks to take Authority
$status = Status::newFatal( 'hookaborted' );
$user = $this->userFactory->newFromUserIdentity( $userIdentity );
if ( $this->hookRunner->onUnwatchArticle( $user, $wikiPage, $status ) ) {
$status = StatusValue::newGood();
$this->watchedItemStore->removeWatch( $userIdentity, $this->nsInfo->getSubjectPage( $title ) );
if ( $this->nsInfo->canHaveTalkPage( $title ) ) {
$this->watchedItemStore->removeWatch( $userIdentity, $this->nsInfo->getTalkPage( $title ) );
}
$this->hookRunner->onUnwatchArticleComplete( $user, $wikiPage );
}
// eventually user_touched should be factored out of User and this should be replaced
$user->invalidateCache();
return $status;
}
/**
* Stop watching a page if the user has permission to edit their watchlist.
* Calls the UnwatchArticle and UnwatchArticleComplete hooks.
* @since 1.37
* @param Authority $performer
* @param PageIdentity $target
* @return StatusValue
*/
public function removeWatch(
Authority $performer,
PageIdentity $target
): StatusValue {
if ( !$performer->isAllowed( 'editmywatchlist' ) ) {
// TODO: this function should be moved out of User
return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
}
return $this->removeWatchIgnoringRights( $performer->getUser(), $target );
}
/**
* Watch or unwatch a page, calling watch/unwatch hooks as appropriate.
* Checks before watching or unwatching to see if the page is already in the requested watch
* state and if the expiry is the same so it does not act unnecessarily.
*
* @param bool $watch Whether to watch or unwatch the page
* @param Authority $performer who is watching/unwatching
* @param PageIdentity $target Page to watch/unwatch
* @param string|null $expiry Optional expiry timestamp in any format acceptable to wfTimestamp(),
* null will not create expiries, or leave them unchanged should they already exist.
* @return StatusValue
* @since 1.37
*/
public function setWatch(
bool $watch,
Authority $performer,
PageIdentity $target,
?string $expiry = null
): StatusValue {
// User must be registered, and (T371091) not a temp user
if ( !$performer->getUser()->isRegistered() || $performer->isTemp() ) {
return StatusValue::newGood();
}
// User must be either changing the watch state or at least the expiry.
// Only call addWatchIgnoringRights() or removeWatch() if there's been a change in the watched status.
$oldWatchedItem = $this->watchedItemStore->getWatchedItem( $performer->getUser(), $target );
$changingWatchStatus = (bool)$oldWatchedItem !== $watch;
if ( $oldWatchedItem && $expiry !== null ) {
// If there's an old watched item, a non-null change to the expiry requires an UPDATE.
$oldWatchPeriod = $oldWatchedItem->getExpiry() ?? 'infinity';
$changingWatchStatus = $changingWatchStatus ||
$oldWatchPeriod !== ExpiryDef::normalizeExpiry( $expiry, TS_MW );
}
if ( $changingWatchStatus ) {
// If the user doesn't have 'editmywatchlist', we still want to
// allow them to add but not remove items via edits and such.
if ( $watch ) {
return $this->addWatchIgnoringRights( $performer->getUser(), $target, $expiry );
} else {
return $this->removeWatch( $performer, $target );
}
}
return StatusValue::newGood();
}
}
|