| 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 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class UserNotificationGateway { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
protected $dbFactory; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
protected $user; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
protected static $eventTable = 'echo_event'; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
protected static $notificationTable = 'echo_notification'; |
| 40 |
|
| 41 |
|
| 42 |
|
| 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 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 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 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 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 |
|
| 129 |
|
| 130 |
|
| 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 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public function getCappedNotificationCount( |
| 159 |
$dbSource, |
| 160 |
array $eventTypesToLoad = [], |
| 161 |
$cap = NotifUser::MAX_BADGE_COUNT |
| 162 |
) { |
| 163 |
|
| 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 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 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 |
} |