Debian Codegraph @

extensions/Echo/includes/Gateway/UserNotificationGateway.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Gateway;
4
5 use MediaWiki\Config\Config;
6 use MediaWiki\Extension\Notifications\DbFactory;
7 use MediaWiki\Extension\Notifications\NotifUser;
8 use MediaWiki\User\UserIdentity;
9
10 /**
11 * Database gateway which handles direct database interaction with the
12 * echo_notification & echo_event for a user, that wouldn't require
13 * loading data into models
14 */
15 class UserNotificationGateway {
16
17 /**
18 * @var DbFactory
19 */
20 protected $dbFactory;
21
22 /**
23 * @var UserIdentity
24 */
25 protected $user;
26
27 /**
28 * The tables for this gateway.
29 *
30 * @var string
31 */
32 protected static $eventTable = 'echo_event';
33
34 /**
35 * The tables for this gateway.
36 *
37 * @var string
38 */
39 protected static $notificationTable = 'echo_notification';
40
41 /**
42 * @var Config
43 */
44 private $config;
45
46 public function __construct( UserIdentity $user, DbFactory $dbFactory, Config $config ) {
47 $this->user = $user;
48 $this->dbFactory = $dbFactory;
49 $this->config = $config;
50 }
51
52 public function getDB( $dbSource ) {
53 return $this->dbFactory->getEchoDb( $dbSource );
54 }
55
56 /**
57 * Mark notifications as read
58 * @param array $eventIDs
59 * @return bool Returns true when data has been updated in DB, false on
60 * failure, or when there was nothing to update
61 */
62 public function markRead( array $eventIDs ) {
63 if ( !$eventIDs ) {
64 return false;
65 }
66
67 $dbw = $this->getDB( DB_PRIMARY );
68 if ( $dbw->isReadOnly() ) {
69 return false;
70 }
71
72 $success = true;
73 foreach (
74 array_chunk( $eventIDs, $this->config->get( 'UpdateRowsPerQuery' ) ) as $batch
75 ) {
76 $dbw->newUpdateQueryBuilder()
77 ->update( self::$notificationTable )
78 ->set( [ 'notification_read_timestamp' => $dbw->timestamp( wfTimestampNow() ) ] )
79 ->where( [
80 'notification_user' => $this->user->getId(),
81 'notification_event' => $batch,
82 'notification_read_timestamp' => null,
83 ] )
84 ->caller( __METHOD__ )
85 ->execute();
86 $success = $dbw->affectedRows() && $success;
87 }
88
89 return $success;
90 }
91
92 /**
93 * Mark notifications as unread
94 * @param array $eventIDs
95 * @return bool Returns true when data has been updated in DB, false on
96 * failure, or when there was nothing to update
97 */
98 public function markUnRead( array $eventIDs ) {
99 if ( !$eventIDs ) {
100 return false;
101 }
102
103 $dbw = $this->getDB( DB_PRIMARY );
104 if ( $dbw->isReadOnly() ) {
105 return false;
106 }
107
108 $success = true;
109 foreach (
110 array_chunk( $eventIDs, $this->config->get( 'UpdateRowsPerQuery' ) ) as $batch
111 ) {
112 $dbw->newUpdateQueryBuilder()
113 ->update( self::$notificationTable )
114 ->set( [ 'notification_read_timestamp' => null ] )
115 ->where( [
116 'notification_user' => $this->user->getId(),
117 'notification_event' => $batch,
118 $dbw->expr( 'notification_read_timestamp', '!=', null ),
119 ] )
120 ->caller( __METHOD__ )
121 ->execute();
122 $success = $dbw->affectedRows() && $success;
123 }
124 return $success;
125 }
126
127 /**
128 * Mark all notification as read, use NotifUser::markAllRead() instead
129 * @deprecated may need this when running in a job or revive this when we
130 * have updateJoin()
131 */
132 public function markAllRead() {
133 $dbw = $this->getDB( DB_PRIMARY );
134 if ( $dbw->isReadOnly() ) {
135 return false;
136 }
137
138 $dbw->newUpdateQueryBuilder()
139 ->update( self::$notificationTable )
140 ->set( [ 'notification_read_timestamp' => $dbw->timestamp( wfTimestampNow() ) ] )
141 ->where( [
142 'notification_user' => $this->user->getId(),
143 'notification_read_timestamp' => null,
144 ] )
145 ->caller( __METHOD__ )
146 ->execute();
147
148 return true;
149 }
150
151 /**
152 * Get notification count for the types specified
153 * @param int $dbSource use primary database or replica storage to pull count
154 * @param array $eventTypesToLoad event types to retrieve
155 * @param int $cap Max count
156 * @return int
157 */
158 public function getCappedNotificationCount(
159 $dbSource,
160 array $eventTypesToLoad = [],
161 $cap = NotifUser::MAX_BADGE_COUNT
162 ) {
163 // double check
164 if ( !in_array( $dbSource, [ DB_REPLICA, DB_PRIMARY ] ) ) {
165 $dbSource = DB_REPLICA;
166 }
167
168 if ( !$eventTypesToLoad ) {
169 return 0;
170 }
171
172 $db = $this->getDB( $dbSource );
173 return $db->newSelectQueryBuilder()
174 ->select( '1' )
175 ->from( self::$notificationTable )
176 ->leftJoin( self::$eventTable, null, 'notification_event=event_id' )
177 ->where( [
178 'notification_user' => $this->user->getId(),
179 'notification_read_timestamp' => null,
180 'event_deleted' => 0,
181 'event_type' => $eventTypesToLoad,
182 ] )
183 ->limit( $cap )
184 ->caller( __METHOD__ )
185 ->fetchRowCount();
186 }
187
188 /**
189 * IMPORTANT: should only call this function if the number of unread notification
190 * is reasonable, for example, unread notification count is less than the max
191 * display defined in oNotifUser::MAX_BADGE_COUNT
192 * @param string $type
193 * @return int[]
194 */
195 public function getUnreadNotifications( $type ) {
196 $dbr = $this->getDB( DB_REPLICA );
197 $res = $dbr->newSelectQueryBuilder()
198 ->select( 'notification_event' )
199 ->from( self::$notificationTable )
200 ->join( self::$eventTable, null, 'notification_event = event_id' )
201 ->where( [
202 'notification_user' => $this->user->getId(),
203 'notification_read_timestamp' => null,
204 'event_deleted' => 0,
205 'event_type' => $type,
206 ] )
207 ->caller( __METHOD__ )
208 ->fetchResultSet();
209
210 $eventIds = [];
211 foreach ( $res as $row ) {
212 $eventIds[$row->notification_event] = $row->notification_event;
213 }
214
215 return $eventIds;
216 }
217
218 }