2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Database gateway which handles direct database interaction with the
|
|
|
|
* echo_notification & echo_event for a user, that wouldn't require
|
|
|
|
* loading data into models
|
|
|
|
*/
|
|
|
|
class EchoUserNotificationGateway {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var MWEchoDbFactory
|
|
|
|
*/
|
|
|
|
protected $dbFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/**
|
2017-08-23 14:38:58 +00:00
|
|
|
* The tables for this gateway.
|
|
|
|
*
|
|
|
|
* @var string
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
|
|
|
protected static $eventTable = 'echo_event';
|
2017-08-23 14:38:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The tables for this gateway.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-07-18 03:58:21 +00:00
|
|
|
protected static $notificationTable = 'echo_notification';
|
|
|
|
|
|
|
|
/**
|
2017-08-23 14:38:58 +00:00
|
|
|
* @param User $user
|
|
|
|
* @param MWEchoDbFactory $dbFactory
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
|
|
|
public function __construct( User $user, MWEchoDbFactory $dbFactory ) {
|
|
|
|
$this->user = $user;
|
|
|
|
$this->dbFactory = $dbFactory;
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:42:13 +00:00
|
|
|
public function getDB( $dbSource ) {
|
|
|
|
return $this->dbFactory->getEchoDb( $dbSource );
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Mark notifications as read
|
2017-08-23 14:38:58 +00:00
|
|
|
* @param array $eventIDs
|
2017-10-02 23:07:31 +00:00
|
|
|
* @return bool Returns true when data has been updated in DB, false on
|
|
|
|
* failure, or when there was nothing to update
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
|
|
|
public function markRead( array $eventIDs ) {
|
2018-08-09 23:33:45 +00:00
|
|
|
global $wgUpdateRowsPerQuery;
|
2014-07-18 03:58:21 +00:00
|
|
|
if ( !$eventIDs ) {
|
2017-10-02 23:07:31 +00:00
|
|
|
return false;
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 17:42:13 +00:00
|
|
|
$dbw = $this->getDB( DB_MASTER );
|
2018-02-22 03:07:45 +00:00
|
|
|
if ( $dbw->isReadOnly() ) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2018-08-09 23:33:45 +00:00
|
|
|
$success = true;
|
|
|
|
foreach ( array_chunk( $eventIDs, $wgUpdateRowsPerQuery ) as $batch ) {
|
|
|
|
$success = $dbw->update(
|
|
|
|
self::$notificationTable,
|
|
|
|
[ 'notification_read_timestamp' => $dbw->timestamp( wfTimestampNow() ) ],
|
|
|
|
[
|
|
|
|
'notification_user' => $this->user->getId(),
|
|
|
|
'notification_event' => $batch,
|
|
|
|
'notification_read_timestamp' => null,
|
|
|
|
],
|
|
|
|
__METHOD__
|
|
|
|
) && $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $success;
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 22:44:22 +00:00
|
|
|
/**
|
|
|
|
* Mark notifications as unread
|
2017-08-23 14:38:58 +00:00
|
|
|
* @param array $eventIDs
|
2017-10-02 23:07:31 +00:00
|
|
|
* @return bool Returns true when data has been updated in DB, false on
|
|
|
|
* failure, or when there was nothing to update
|
2016-03-04 22:44:22 +00:00
|
|
|
*/
|
|
|
|
public function markUnRead( array $eventIDs ) {
|
2018-08-09 23:33:45 +00:00
|
|
|
global $wgUpdateRowsPerQuery;
|
2016-03-04 22:44:22 +00:00
|
|
|
if ( !$eventIDs ) {
|
2017-10-02 23:07:31 +00:00
|
|
|
return false;
|
2016-03-04 22:44:22 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 17:42:13 +00:00
|
|
|
$dbw = $this->getDB( DB_MASTER );
|
2018-08-24 10:15:12 +00:00
|
|
|
if ( $dbw->isReadOnly() ) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-04 22:44:22 +00:00
|
|
|
|
2018-08-24 10:15:12 +00:00
|
|
|
$success = true;
|
2018-08-09 23:33:45 +00:00
|
|
|
foreach ( array_chunk( $eventIDs, $wgUpdateRowsPerQuery ) as $batch ) {
|
|
|
|
$success = $dbw->update(
|
|
|
|
self::$notificationTable,
|
|
|
|
[ 'notification_read_timestamp' => null ],
|
|
|
|
[
|
|
|
|
'notification_user' => $this->user->getId(),
|
|
|
|
'notification_event' => $batch,
|
|
|
|
'notification_read_timestamp IS NOT NULL'
|
|
|
|
],
|
|
|
|
__METHOD__
|
|
|
|
) && $success;
|
|
|
|
}
|
|
|
|
return $success;
|
2016-03-04 22:44:22 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
2018-08-09 23:33:45 +00:00
|
|
|
* Mark all notification as read, use MWEchoNotifUser::markAllRead() instead
|
2014-08-13 22:00:25 +00:00
|
|
|
* @deprecated may need this when running in a job or revive this when we
|
|
|
|
* have updateJoin()
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
|
|
|
public function markAllRead() {
|
2018-05-25 17:42:13 +00:00
|
|
|
$dbw = $this->getDB( DB_MASTER );
|
2018-08-24 10:15:12 +00:00
|
|
|
if ( $dbw->isReadOnly() ) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2018-10-26 19:40:33 +00:00
|
|
|
$dbw->update(
|
2014-07-18 03:58:21 +00:00
|
|
|
self::$notificationTable,
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'notification_read_timestamp' => $dbw->timestamp( wfTimestampNow() ) ],
|
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
'notification_user' => $this->user->getId(),
|
2015-10-01 13:48:52 +00:00
|
|
|
'notification_read_timestamp' => null,
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2014-07-18 03:58:21 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
2018-10-26 19:40:33 +00:00
|
|
|
|
|
|
|
return true;
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-22 21:33:22 +00:00
|
|
|
* Get notification count for the types specified
|
2016-03-14 13:37:20 +00:00
|
|
|
* @param int $dbSource use master or slave storage to pull count
|
|
|
|
* @param array $eventTypesToLoad event types to retrieve
|
|
|
|
* @param int $cap Max count
|
2014-07-18 03:58:21 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2016-12-05 18:51:07 +00:00
|
|
|
public function getCappedNotificationCount( $dbSource, array $eventTypesToLoad = [], $cap = MWEchoNotifUser::MAX_BADGE_COUNT ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
// double check
|
2017-09-24 05:23:47 +00:00
|
|
|
if ( !in_array( $dbSource, [ DB_REPLICA, DB_MASTER ] ) ) {
|
|
|
|
$dbSource = DB_REPLICA;
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$eventTypesToLoad ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:42:13 +00:00
|
|
|
$db = $this->getDB( $dbSource );
|
2016-03-14 13:37:20 +00:00
|
|
|
return $db->selectRowCount(
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
self::$notificationTable,
|
|
|
|
self::$eventTable
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
[ '1' ],
|
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
'notification_user' => $this->user->getId(),
|
|
|
|
'notification_read_timestamp' => null,
|
2016-03-04 19:23:02 +00:00
|
|
|
'event_deleted' => 0,
|
2014-07-18 03:58:21 +00:00
|
|
|
'event_type' => $eventTypesToLoad,
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2014-07-18 03:58:21 +00:00
|
|
|
__METHOD__,
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'LIMIT' => $cap ],
|
|
|
|
[
|
|
|
|
'echo_event' => [ 'LEFT JOIN', 'notification_event=event_id' ],
|
|
|
|
]
|
2014-07-18 03:58:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IMPORTANT: should only call this function if the number of unread notification
|
|
|
|
* is reasonable, for example, unread notification count is less than the max
|
2016-03-09 04:50:31 +00:00
|
|
|
* display defined in MWEchoNotifUser::MAX_BADGE_COUNT
|
2017-08-23 14:38:58 +00:00
|
|
|
* @param string $type
|
2014-07-18 03:58:21 +00:00
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
public function getUnreadNotifications( $type ) {
|
2018-05-25 17:42:13 +00:00
|
|
|
$dbr = $this->getDB( DB_REPLICA );
|
2014-07-18 03:58:21 +00:00
|
|
|
$res = $dbr->select(
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
self::$notificationTable,
|
|
|
|
self::$eventTable
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
[ 'notification_event' ],
|
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
'notification_user' => $this->user->getId(),
|
|
|
|
'notification_read_timestamp' => null,
|
2016-03-04 19:23:02 +00:00
|
|
|
'event_deleted' => 0,
|
2014-07-18 03:58:21 +00:00
|
|
|
'event_type' => $type,
|
|
|
|
'notification_event = event_id'
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2014-07-18 03:58:21 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$eventIds = [];
|
2014-07-18 03:58:21 +00:00
|
|
|
if ( $res ) {
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$eventIds[$row->notification_event] = $row->notification_event;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $eventIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|