Debian Codegraph @

extensions/Echo/includes/Jobs/NotificationDeleteJob.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Jobs;
4
5 use Job;
6 use JobQueueGroup;
7 use MediaWiki\Extension\Notifications\Mapper\NotificationMapper;
8 use MediaWiki\Extension\Notifications\NotifUser;
9 use MediaWiki\Title\Title;
10 use MediaWiki\User\User;
11
12 /**
13 * This job is created when sending notifications to the target users. The purpose
14 * of this job is to delete older notifications when the number of notifications a
15 * user has is more than $wgEchoMaxUpdateCount, it does not make sense to have tons
16 * of notifications in the history while users wouldn't bother to click 'load more'
17 * like 100 times to see them. What we gain from this is we could run expensive
18 * queries otherwise that would requires adding index and data denormalization.
19 *
20 * The initial job contains multiple users, which will in turn have individual jobs
21 * queued for them.
22 */
23 class NotificationDeleteJob extends Job {
24 private JobQueueGroup $jobQueueGroup;
25
26 public function __construct(
27 Title $title,
28 array $params,
29 JobQueueGroup $jobQueueGroup
30 ) {
31 parent::__construct( 'EchoNotificationDeleteJob', $title, $params );
32 $this->jobQueueGroup = $jobQueueGroup;
33 }
34
35 /**
36 * Run the job of finding & deleting older notifications
37 * @return true
38 */
39 public function run() {
40 global $wgEchoMaxUpdateCount;
41 if ( count( $this->params['userIds'] ) > 1 ) {
42 // If there are multiple users, queue a single job for each one
43 $jobs = [];
44 foreach ( $this->params['userIds'] as $userId ) {
45 $jobs[] = new NotificationDeleteJob(
46 $this->title,
47 [ 'userIds' => [ $userId ] ],
48 $this->jobQueueGroup
49 );
50 }
51 $this->jobQueueGroup->push( $jobs );
52
53 return true;
54 }
55
56 $notifMapper = new NotificationMapper();
57
58 // Back-compat for older jobs which used [ $userId => $userId ];
59 $userIds = array_values( $this->params['userIds'] );
60 $userId = $userIds[0];
61 $user = User::newFromId( $userId );
62 $notif = $notifMapper->fetchByUserOffset( $user, $wgEchoMaxUpdateCount );
63 if ( $notif ) {
64 $notifMapper->deleteByUserEventOffset(
65 $user, $notif->getEvent()->getId()
66 );
67 $notifUser = NotifUser::newFromUser( $user );
68 $notifUser->resetNotificationCount();
69 }
70
71 return true;
72 }
73
74 }