summaryrefslogtreecommitdiff
blob: 59dd68a027dd1e2f1c8a5d0ae23dca0cbb1b4cd1 (plain)
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
<?php

use MediaWiki\MediaWikiServices;

/**
 * Entity that represents a notification target user
 */
class MWEchoNotifUser {

	/**
	 * Notification target user
	 * @var User
	 */
	private $mUser;

	/**
	 * Object cache
	 * @var WANObjectCache
	 */
	private $cache;

	/**
	 * Database access gateway
	 * @var EchoUserNotificationGateway
	 */
	private $userNotifGateway;

	/**
	 * Notification mapper
	 * @var EchoNotificationMapper
	 */
	private $notifMapper;

	/**
	 * Target page mapper
	 * @var EchoTargetPageMapper
	 */
	private $targetPageMapper;

	/**
	 * @var EchoForeignNotifications
	 */
	private $foreignNotifications = null;

	/**
	 * @var array[]|null
	 */
	private $localCountsAndTimestamps;

	/**
	 * @var array[]|null
	 */
	private $globalCountsAndTimestamps;

	/**
	 * @var array[]|null
	 */
	private $mForeignData = null;

	// The max notification count shown in badge

	// The max number shown in bundled message, eg, <user> and 99+ others <action>.
	// This is really a totally separate thing, and could be its own constant.

	// WARNING: If you change this, you should also change all references in the
	// i18n messages (100 and 99) in all repositories using Echo.
	const MAX_BADGE_COUNT = 99;

	const CACHE_TTL = 86400;
	const CACHE_KEY = 'echo-notification-counts';
	const CHECK_KEY = 'echo-notification-updated';

	/**
	 * Usually client code doesn't need to initialize the object directly
	 * because it could be obtained from factory method newFromUser()
	 * @param User $user
	 * @param WANObjectCache $cache
	 * @param EchoUserNotificationGateway $userNotifGateway
	 * @param EchoNotificationMapper $notifMapper
	 * @param EchoTargetPageMapper $targetPageMapper
	 */
	public function __construct(
		User $user,
		WANObjectCache $cache,
		EchoUserNotificationGateway $userNotifGateway,
		EchoNotificationMapper $notifMapper,
		EchoTargetPageMapper $targetPageMapper
	) {
		$this->mUser = $user;
		$this->userNotifGateway = $userNotifGateway;
		$this->cache = $cache;
		$this->notifMapper = $notifMapper;
		$this->targetPageMapper = $targetPageMapper;
	}

	/**
	 * Factory method
	 * @param User $user
	 * @throws MWException
	 * @return MWEchoNotifUser
	 */
	public static function newFromUser( User $user ) {
		if ( $user->isAnon() ) {
			throw new MWException( 'User must be logged in to view notification!' );
		}

		return new MWEchoNotifUser(
			$user,
			MediaWikiServices::getInstance()->getMainWANObjectCache(),
			new EchoUserNotificationGateway( $user, MWEchoDbFactory::newFromDefault() ),
			new EchoNotificationMapper(),
			new EchoTargetPageMapper()
		);
	}

	/**
	 * Mark all edit-user-talk notifications as read. This is called when a user visits their user talk page.
	 */
	public function clearUserTalkNotifications() {
		$this->markRead(
			$this->userNotifGateway->getUnreadNotifications(
				'edit-user-talk'
			)
		);
	}

	/**
	 * Get message count for this user.
	 *
	 * @return int
	 */
	public function getMessageCount() {
		return $this->getNotificationCount( EchoAttributeManager::MESSAGE );
	}

	/**
	 * Get alert count for this user.
	 *
	 * @return int
	 */
	public function getAlertCount() {
		return $this->getNotificationCount( EchoAttributeManager::ALERT );
	}

	/**
	 * Get the number of unread local notifications in a given section. This does not include
	 * foreign notifications, even if the user has cross-wiki notifications enabled.
	 *
	 * @param string $section Notification section
	 * @return int
	 */
	public function getLocalNotificationCount( $section = EchoAttributeManager::ALL ) {
		return $this->getNotificationCount( $section, false );
	}

	/**
	 * Retrieves number of unread notifications that a user has, would return
	 * MWEchoNotifUser::MAX_BADGE_COUNT + 1 at most.
	 *
	 * If $wgEchoCrossWikiNotifications is disabled, the $global parameter is ignored.
	 *
	 * @param string $section Notification section
	 * @param bool|string $global Whether to include foreign notifications. If set to 'preference', uses the user's preference.
	 * @return int
	 */
	public function getNotificationCount( $section = EchoAttributeManager::ALL, $global = 'preference' ) {
		if ( $this->mUser->isAnon() ) {
			return 0;
		}

		global $wgEchoCrossWikiNotifications;
		if ( !$wgEchoCrossWikiNotifications ) {
			// Ignore the $global parameter
			$global = false;
		}

		if ( $global === 'preference' ) {
			$global = $this->getForeignNotifications()->isEnabledByUser();
		}

		$data = $this->getCountsAndTimestamps( $global );
		$count = $data[$global ? 'global' : 'local'][$section]['count'];
		return (int)$count;
	}

	/**
	 * Get the timestamp of the latest unread alert
	 *
	 * @return bool|MWTimestamp Timestamp of latest unread alert, or false if there are no unread alerts.
	 */
	public function getLastUnreadAlertTime() {
		return $this->getLastUnreadNotificationTime( EchoAttributeManager::ALERT );
	}

	/**
	 * Get the timestamp of the latest unread message
	 *
	 * @return bool|MWTimestamp
	 */
	public function getLastUnreadMessageTime() {
		return $this->getLastUnreadNotificationTime( EchoAttributeManager::MESSAGE );
	}

	/**
	 * Returns the timestamp of the last unread notification.
	 *
	 * If $wgEchoCrossWikiNotifications is disabled, the $global parameter is ignored.
	 *
	 * @param string $section Notification section
	 * @param bool|string $global Whether to include foreign notifications. If set to 'preference', uses the user's preference.
	 * @return bool|MWTimestamp Timestamp of latest unread message, or false if there are no unread messages.
	 */
	public function getLastUnreadNotificationTime( $section = EchoAttributeManager::ALL, $global = 'preference' ) {
		if ( $this->mUser->isAnon() ) {
			return false;
		}

		global $wgEchoCrossWikiNotifications;
		if ( !$wgEchoCrossWikiNotifications ) {
			// Ignore the $global parameter
			$global = false;
		}

		if ( $global === 'preference' ) {
			$global = $this->getForeignNotifications()->isEnabledByUser();
		}

		$data = $this->getCountsAndTimestamps( $global );
		$timestamp = $data[$global ? 'global' : 'local'][$section]['timestamp'];
		return $timestamp === -1 ? false : new MWTimestamp( $timestamp );
	}

	/**
	 * Mark one or more notifications read for a user.
	 * @param array $eventIds Array of event IDs to mark read
	 * @return bool Returns true when data has been updated in DB, false on
	 *   failure, or when there was nothing to update
	 */
	public function markRead( $eventIds ) {
		$eventIds = array_filter( (array)$eventIds, 'is_numeric' );
		if ( !$eventIds || wfReadOnly() ) {
			return false;
		}

		$updated = $this->userNotifGateway->markRead( $eventIds );
		if ( $updated ) {
			// Update notification count in cache
			$this->resetNotificationCount();

			// After this 'mark read', is there any unread edit-user-talk
			// remaining?  If not, we should clear the newtalk flag.
			if ( $this->mUser->getNewtalk() ) {
				$attributeManager = EchoAttributeManager::newFromGlobalVars();
				$categoryMap = $attributeManager->getEventsByCategory();
				$usertalkTypes = $categoryMap['edit-user-talk'];
				$unreadEditUserTalk = $this->notifMapper->fetchUnreadByUser( $this->mUser, 1, null, $usertalkTypes, null, DB_MASTER );
				if ( $unreadEditUserTalk === [] ) {
					$this->mUser->setNewtalk( false );
				}
			}
		}

		return $updated;
	}

	/**
	 * Mark one or more notifications unread for a user.
	 * @param array $eventIds Array of event IDs to mark unread
	 * @return bool Returns true when data has been updated in DB, false on
	 *   failure, or when there was nothing to update
	 */
	public function markUnRead( $eventIds ) {
		$eventIds = array_filter( (array)$eventIds, 'is_numeric' );
		if ( !$eventIds || wfReadOnly() ) {
			return false;
		}

		$updated = $this->userNotifGateway->markUnRead( $eventIds );
		if ( $updated ) {
			// Update notification count in cache
			$this->resetNotificationCount();

			// After this 'mark unread', is there any unread edit-user-talk?
			// If so, we should add the edit-user-talk flag
			if ( !$this->mUser->getNewtalk() ) {
				$attributeManager = EchoAttributeManager::newFromGlobalVars();
				$categoryMap = $attributeManager->getEventsByCategory();
				$usertalkTypes = $categoryMap['edit-user-talk'];
				$unreadEditUserTalk = $this->notifMapper->fetchUnreadByUser( $this->mUser, 1, null, $usertalkTypes, null, DB_MASTER );
				if ( $unreadEditUserTalk !== [] ) {
					$this->mUser->setNewtalk( true );
				}
			}
		}

		return $updated;
	}

	/**
	 * Attempt to mark all or sections of notifications as read, this only
	 * updates up to $wgEchoMaxUpdateCount records per request, see more
	 * detail about this in Echo.php, the other reason is that mediawiki
	 * database interface doesn't support updateJoin() that would update
	 * across multiple tables, we would visit this later
	 *
	 * @param string[] $sections
	 * @return bool
	 */
	public function markAllRead( array $sections = [ EchoAttributeManager::ALL ] ) {
		if ( wfReadOnly() ) {
			return false;
		}

		global $wgEchoMaxUpdateCount;

		// Mark all sections as read if this is the case
		if ( in_array( EchoAttributeManager::ALL, $sections ) ) {
			$sections = EchoAttributeManager::$sections;
		}

		$attributeManager = EchoAttributeManager::newFromGlobalVars();
		$eventTypes = $attributeManager->getUserEnabledEventsbySections( $this->mUser, 'web', $sections );

		$notifs = $this->notifMapper->fetchUnreadByUser( $this->mUser, $wgEchoMaxUpdateCount, null, $eventTypes );

		$eventIds = array_filter(
			array_map( function ( EchoNotification $notif ) {
				// This should not happen at all, but use 0 in
				// such case so to keep the code running
				if ( $notif->getEvent() ) {
					return $notif->getEvent()->getId();
				} else {
					return 0;
				}
			}, $notifs )
		);

		$updated = $this->markRead( $eventIds );
		if ( $updated ) {
			// Delete records from echo_target_page
			/**
			 * Keep the 'echo_target_page' records so they can be used for moderation.
			 */
			// $this->targetPageMapper->deleteByUserEvents( $this->mUser, $eventIds );
		}

		return $updated;
	}

	/**
	 * Mark one of more notifications as read on a foreign wiki.
	 *
	 * @param int[] $eventIds Event IDs to mark as read
	 * @param string $wiki Wiki name
	 */
	public function markReadForeign( array $eventIds, $wiki ) {
		$foreignReq = new EchoForeignWikiRequest(
			$this->mUser,
			[
				'action' => 'echomarkread',
				'list' => implode( '|', $eventIds ),
			],
			[ $wiki ],
			'wikis',
			'csrf'
		);
		$foreignReq->execute();
	}

	/**
	 * Get information about a set of unread notifications on a foreign wiki.
	 *
	 * @param int[] $eventIds Event IDs to look up. Only unread notifications can be found.
	 * @param string $wiki Wiki name
	 * @return array[] Array of notification data as returned by api.php, keyed by event ID
	 */
	public function getForeignNotificationInfo( array $eventIds, $wiki ) {
		$foreignReq = new EchoForeignWikiRequest(
			$this->mUser,
			[
				'action' => 'query',
				'meta' => 'notifications',
				'notprop' => 'list',
				'notfilter' => '!read',
				'notlimit' => 'max'
			],
			[ $wiki ],
			'notwikis'
		);
		$foreignResults = $foreignReq->execute();
		$list = $foreignResults[$wiki]['query']['notifications']['list'] ?? [];

		$result = [];
		foreach ( $list as $notif ) {
			if ( in_array( $notif['id'], $eventIds ) ) {
				$result[$notif['id']] = $notif;
			}
		}
		return $result;
	}

	/**
	 * Invalidate cache and update echo_unread_wikis if x-wiki notifications is enabled.
	 *
	 * This updates the user's touched timestamp, as well as the value returned by getGlobalUpdateTime().
	 *
	 * NOTE: Consider calling this function from a deferred update, since it will read from and write to
	 * the master DB if cross-wiki notifications are enabled.
	 */
	public function resetNotificationCount() {
		global $wgEchoCrossWikiNotifications;

		// Delete cached local counts and timestamps
		$localMemcKey = $this->getMemcKey( self::CACHE_KEY );
		$this->cache->delete( $localMemcKey );

		// Update the user touched timestamp for the local user
		$this->mUser->invalidateCache();

		if ( $wgEchoCrossWikiNotifications ) {
			// Delete cached global counts and timestamps
			$globalMemcKey = $this->getGlobalMemcKey( self::CACHE_KEY );
			if ( $globalMemcKey !== false ) {
				$this->cache->delete( $globalMemcKey );
			}

			$uw = EchoUnreadWikis::newFromUser( $this->mUser );
			if ( $uw ) {
				// Immediately compute new local counts and timestamps
				$newLocalData = $this->computeLocalCountsAndTimestamps( DB_MASTER );
				// Write the new values to the echo_unread_wikis table
				$alertTs = $newLocalData[EchoAttributeManager::ALERT]['timestamp'];
				$messageTs = $newLocalData[EchoAttributeManager::MESSAGE]['timestamp'];
				$uw->updateCount(
					wfWikiID(),
					$newLocalData[EchoAttributeManager::ALERT]['count'],
					$alertTs === -1 ? false : new MWTimestamp( $alertTs ),
					$newLocalData[EchoAttributeManager::MESSAGE]['count'],
					$messageTs === -1 ? false : new MWTimestamp( $messageTs )
				);
				// We could set() $newLocalData into the cache here, but we don't because that seems risky;
				// instead we let it be recomputed on demand
			}

			// Update the global touched timestamp
			$checkKey = $this->getGlobalMemcKey( self::CHECK_KEY );
			if ( $checkKey ) {
				$this->cache->touchCheckKey( $checkKey );
			}
		}
	}

	/**
	 * Get the timestamp of the last time the global notification counts/timestamps were updated, if available.
	 *
	 * If the timestamp of the last update is not known, this will return the current timestamp.
	 * If the user is not attached, this will return false.
	 *
	 * @return string|false MW timestamp of the last update, or false if the user is not attached
	 */
	public function getGlobalUpdateTime() {
		$key = $this->getGlobalMemcKey( self::CHECK_KEY );
		if ( $key === false ) {
			return false;
		}
		return wfTimestamp( TS_MW, $this->cache->getCheckKeyTime( $key ) );
	}

	/**
	 * Get the number of notifications in each section, and the timestamp of the latest notification in
	 * each section. This returns the raw data structure that is stored in the cache; unless you want
	 * all of this information, you're probably looking for getNotificationCount(),
	 * getLastUnreadNotificationTime() or one of its wrappers.
	 *
	 * The returned data structure looks like:
	 * [
	 *   'local' => [
	 *     'alert' => [ 'count' => N, 'timestamp' => TS ],
	 *     'message' => [ 'count' = N, 'timestamp' => TS ],
	 *     'all' => [ 'count' => N, 'timestamp' => TS ],
	 *   ],
	 *   'global' => [
	 *     'alert' => [ 'count' => N, 'timestamp' => TS ],
	 *     'message' => [ 'count' = N, 'timestamp' => TS ],
	 *     'all' => [ 'count' => N, 'timestamp' => TS ],
	 *   ],
	 * ]
	 * Where N is a number and TS is a timestamp in TS_MW format or -1. If $includeGlobal is false,
	 * the 'global' key will not be present.
	 *
	 * @param bool $includeGlobal Whether to include cross-wiki notifications as well
	 * @return array[]
	 */
	public function getCountsAndTimestamps( $includeGlobal = false ) {
		if ( $this->localCountsAndTimestamps === null ) {
			$this->localCountsAndTimestamps = $this->cache->getWithSetCallback(
				$this->getMemcKey( self::CACHE_KEY ),
				self::CACHE_TTL,
				function ( $oldValue, &$ttl, array &$setOpts ) {
					$dbr = $this->userNotifGateway->getDB( DB_REPLICA );
					$setOpts += Database::getCacheSetOptions( $dbr );
					return $this->computeLocalCountsAndTimestamps();
				}
			);
		}
		$result = [ 'local' => $this->localCountsAndTimestamps ];

		if ( $includeGlobal ) {
			if ( $this->globalCountsAndTimestamps === null ) {
				$memcKey = $this->getGlobalMemcKey( self::CACHE_KEY );
				// If getGlobalMemcKey returns false, we don't have a global user ID
				// In that case, don't compute data that we can't cache or store
				if ( $memcKey !== false ) {
					$this->globalCountsAndTimestamps = $this->cache->getWithSetCallback(
						$memcKey,
						self::CACHE_TTL,
						function ( $oldValue, &$ttl, array &$setOpts ) {
							$dbr = $this->userNotifGateway->getDB( DB_REPLICA );
							$setOpts += Database::getCacheSetOptions( $dbr );
							return $this->computeGlobalCountsAndTimestamps();
						}
					);
				}
			}
			$result['global'] = $this->globalCountsAndTimestamps;
		}
		return $result;
	}

	/**
	 * Compute the counts and timestamps for the local notifications in each section.
	 * @param int $dbSource DB_REPLICA or DB_MASTER
	 * @return array[] [ 'alert' => [ 'count' => N, 'timestamp' => TS ], ... ]
	 */
	protected function computeLocalCountsAndTimestamps( $dbSource = DB_REPLICA ) {
		$attributeManager = EchoAttributeManager::newFromGlobalVars();
		$result = [];
		$totals = [ 'count' => 0, 'timestamp' => -1 ];

		foreach ( EchoAttributeManager::$sections as $section ) {
			$eventTypesToLoad = $attributeManager->getUserEnabledEventsbySections( $this->mUser, 'web', [ $section ] );

			$count = (int)$this->userNotifGateway->getCappedNotificationCount( $dbSource, $eventTypesToLoad, self::MAX_BADGE_COUNT + 1 );
			$result[$section]['count'] = $count;
			$totals['count'] += $count;

			$notifications = $this->notifMapper->fetchUnreadByUser( $this->mUser, 1, null, $eventTypesToLoad, null, $dbSource );
			if ( $notifications ) {
				$notification = reset( $notifications );
				$timestamp = $notification->getTimestamp();
			} else {
				$timestamp = -1;
			}
			$result[$section]['timestamp'] = $timestamp;
			$totals['timestamp'] = max( $totals['timestamp'], $timestamp );
		}
		$totals['count'] = self::capNotificationCount( $totals['count'] );
		$result[EchoAttributeManager::ALL] = $totals;
		return $result;
	}

	/**
	 * Compute the global counts and timestamps for each section.
	 *
	 * This calls getCountsAndTimestamps() to get data about local notifications, which may end up
	 * calling computeLocalCountsAndTimestamps() if there's a cache miss.
	 * @return array[] [ 'alert' => [ 'count' => N, 'timestamp' => TS ], ... ]
	 */
	protected function computeGlobalCountsAndTimestamps() {
		$localData = $this->getCountsAndTimestamps()['local'];
		$result = [];
		$totals = [ 'count' => 0, 'timestamp' => -1 ];
		foreach ( EchoAttributeManager::$sections as $section ) {
			$localCount = $localData[$section]['count'];
			$globalCount = self::capNotificationCount( $localCount + $this->getForeignCount( $section ) );
			$result[$section]['count'] = $globalCount;
			$totals['count'] += $globalCount;

			$localTimestamp = $localData[$section]['timestamp'];
			$foreignTimestamp = $this->getForeignTimestamp( $section );
			$globalTimestamp = max( $localTimestamp, $foreignTimestamp ? $foreignTimestamp->getTimestamp( TS_MW ) : -1 );
			$result[$section]['timestamp'] = $globalTimestamp;
			$totals['timestamp'] = max( $totals['timestamp'], $globalTimestamp );
		}
		$totals['count'] = self::capNotificationCount( $totals['count'] );
		$result[EchoAttributeManager::ALL] = $totals;
		return $result;
	}

	/**
	 * Get the user's email notification format
	 * @return string
	 */
	public function getEmailFormat() {
		global $wgAllowHTMLEmail;

		if ( $wgAllowHTMLEmail ) {
			return $this->mUser->getOption( 'echo-email-format' );
		} else {
			return EchoEmailFormat::PLAIN_TEXT;
		}
	}

	/**
	 * Build a cache key for local use (local to this wiki)
	 *
	 * @param string $key Key, typically prefixed with echo-notification-
	 * @return string Cache key
	 */
	protected function getMemcKey( $key ) {
		global $wgEchoCacheVersion;
		return wfMemcKey( $key, $this->mUser->getId(), $wgEchoCacheVersion );
	}

	/**
	 * Build a cache key for global use
	 *
	 * @param string $key Key, typically prefixed with echo-notification-
	 * @return string|false Memcached key, or false if one could not be generated
	 */
	protected function getGlobalMemcKey( $key ) {
		global $wgEchoCacheVersion;
		$lookup = CentralIdLookup::factory();
		$globalId = $lookup->centralIdFromLocalUser( $this->mUser, CentralIdLookup::AUDIENCE_RAW );
		if ( !$globalId ) {
			return false;
		}
		return wfGlobalCacheKey( $key, $globalId, $wgEchoCacheVersion );
	}

	/**
	 * Lazy-construct an EchoForeignNotifications instance. This instance is force-enabled, so it
	 * returns information about cross-wiki notifications even if the user has them disabled.
	 * @return EchoForeignNotifications
	 */
	protected function getForeignNotifications() {
		if ( !$this->foreignNotifications ) {
			$this->foreignNotifications = new EchoForeignNotifications( $this->mUser, true );
		}
		return $this->foreignNotifications;
	}

	/**
	 * Get data about foreign notifications from the foreign wikis' APIs.
	 *
	 * This is used when $wgEchoSectionTransition or $wgEchoBundleTransition is enabled,
	 * to deal with untrustworthy echo_unread_wikis entries. This method fetches the list of
	 * wikis that have any unread notifications at all from the echo_unread_wikis table, then
	 * queries their APIs to find the per-section counts and timestamps for those wikis.
	 *
	 * The results of this function are cached in the NotifUser object.
	 * @return array[] [ (str) wiki => [ (str) section => [ 'count' => (int) count, 'timestamp' => (str) ts ] ] ]
	 */
	protected function getForeignData() {
		if ( $this->mForeignData ) {
			return $this->mForeignData;
		}

		$potentialWikis = $this->getForeignNotifications()->getWikis( EchoAttributeManager::ALL );
		$foreignReq = new EchoForeignWikiRequest(
			$this->mUser,
			[
				'action' => 'query',
				'meta' => 'notifications',
				'notprop' => 'count|list',
				'notgroupbysection' => '1',
				'notunreadfirst' => '1',
			],
			$potentialWikis,
			'notwikis'
		);
		$foreignResults = $foreignReq->execute();

		$this->mForeignData = [];
		foreach ( $foreignResults as $wiki => $result ) {
			if ( !isset( $result['query']['notifications'] ) ) {
				continue;
			}
			$data = $result['query']['notifications'];
			foreach ( EchoAttributeManager::$sections as $section ) {
				if ( isset( $data[$section]['rawcount'] ) ) {
					$this->mForeignData[$wiki][$section]['count'] = $data[$section]['rawcount'];
				}
				if ( isset( $data[$section]['list'][0] ) ) {
					$this->mForeignData[$wiki][$section]['timestamp'] = $data[$section]['list'][0]['timestamp']['mw'];
				}
			}
		}
		return $this->mForeignData;
	}

	/**
	 * Get the number of foreign notifications in a given section.
	 * @param string $section One of EchoAttributeManager::$sections
	 * @return int Number of foreign notifications
	 */
	protected function getForeignCount( $section = EchoAttributeManager::ALL ) {
		global $wgEchoSectionTransition, $wgEchoBundleTransition;
		$count = 0;
		if (
			// In section transition mode, we don't trust the individual echo_unread_wikis rows
			// but we do trust that alert+message=all. In bundle transition mode, we don't trust
			// that either, but we do trust that wikis with rows in the table have unread notifications
			// and wikis without rows in the table don't.
			( $wgEchoSectionTransition && $section !== EchoAttributeManager::ALL ) ||
			$wgEchoBundleTransition
		) {
			$foreignData = $this->getForeignData();
			foreach ( $foreignData as $data ) {
				if ( $section === EchoAttributeManager::ALL ) {
					foreach ( $data as $subData ) {
						if ( isset( $subData['count'] ) ) {
							$count += $subData['count'];
						}
					}
				} elseif ( isset( $data[$section]['count'] ) ) {
					$count += $data[$section]['count'];
				}
			}
		} else {
			$count += $this->getForeignNotifications()->getCount( $section );
		}
		return self::capNotificationCount( $count );
	}

	/**
	 * Get the timestamp of the most recent foreign notification in a given section.
	 * @param string $section One of EchoAttributeManager::$sections
	 * @return MWTimestamp|false Timestamp of the most recent foreign notification, or false if
	 *  there aren't any
	 */
	protected function getForeignTimestamp( $section = EchoAttributeManager::ALL ) {
		global $wgEchoSectionTransition, $wgEchoBundleTransition;

		if (
			// In section transition mode, we don't trust the individual echo_unread_wikis rows
			// but we do trust that alert+message=all. In bundle transition mode, we don't trust
			// that either, but we do trust that wikis with rows in the table have unread notifications
			// and wikis without rows in the table don't.
			( $wgEchoSectionTransition && $section !== EchoAttributeManager::ALL ) ||
			$wgEchoBundleTransition
		) {
			$foreignTime = -1;
			$foreignData = $this->getForeignData();
			foreach ( $foreignData as $data ) {
				if ( $section === EchoAttributeManager::ALL ) {
					foreach ( $data as $subData ) {
						if ( isset( $subData['timestamp'] ) ) {
							$foreignTime = max( $foreignTime, $subData['timestamp'] );
						}
					}
				} elseif ( isset( $data[$section]['timestamp'] ) ) {
					$foreignTime = max( $foreignTime, $data[$section]['timestamp'] );
				}
			}
			$foreignTime = $foreignTime === -1 ? false : new MWTimestamp( $foreignTime );
		} else {
			$foreignTime = $this->getForeignNotifications()->getTimestamp( $section );
		}
		return $foreignTime;
	}

	/**
	 * Helper function to produce the capped number of notifications
	 * based on the value of MWEchoNotifUser::MAX_BADGE_COUNT
	 *
	 * @param int $number Raw notification count to cap
	 * @return int Capped notification count
	 */
	public static function capNotificationCount( $number ) {
		return min( $number, self::MAX_BADGE_COUNT + 1 );
	}
}