2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-02 03:37:20 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Gateway;
|
|
|
|
|
|
|
|
use Config;
|
2022-11-12 07:19:00 +00:00
|
|
|
use MediaWiki\Extension\Notifications\DbFactory;
|
2021-12-19 01:16:52 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2022-11-02 03:37:20 +00:00
|
|
|
use MWEchoNotifUser;
|
2021-12-19 01:16:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Database gateway which handles direct database interaction with the
|
|
|
|
* echo_notification & echo_event for a user, that wouldn't require
|
|
|
|
* loading data into models
|
|
|
|
*/
|
2022-11-02 03:37:20 +00:00
|
|
|
class UserNotificationGateway {
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
/**
|
2022-11-12 07:19:00 +00:00
|
|
|
* @var DbFactory
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
|
|
|
protected $dbFactory;
|
|
|
|
|
|
|
|
/**
|
2021-12-19 01:16:52 +00:00
|
|
|
* @var UserIdentity
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
|
|
|
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';
|
2022-11-02 03:37:20 +00:00
|
|
|
|
2019-08-30 20:58:41 +00:00
|
|
|
/**
|
|
|
|
* @var Config
|
|
|
|
*/
|
|
|
|
private $config;
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
/**
|
2021-12-19 01:16:52 +00:00
|
|
|
* @param UserIdentity $user
|
2022-11-12 07:19:00 +00:00
|
|
|
* @param DbFactory $dbFactory
|
2019-08-30 20:58:41 +00:00
|
|
|
* @param Config $config
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
2022-11-12 07:19:00 +00:00
|
|
|
public function __construct( UserIdentity $user, DbFactory $dbFactory, Config $config ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->user = $user;
|
|
|
|
$this->dbFactory = $dbFactory;
|
2019-08-30 20:58:41 +00:00
|
|
|
$this->config = $config;
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
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 ) {
|
|
|
|
if ( !$eventIDs ) {
|
2017-10-02 23:07:31 +00:00
|
|
|
return false;
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 07:28:02 +00:00
|
|
|
$dbw = $this->getDB( DB_PRIMARY );
|
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;
|
2019-08-30 20:58:41 +00:00
|
|
|
foreach (
|
|
|
|
array_chunk( $eventIDs, $this->config->get( 'UpdateRowsPerQuery' ) ) as $batch
|
|
|
|
) {
|
2018-08-09 23:33:45 +00:00
|
|
|
$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 ) {
|
|
|
|
if ( !$eventIDs ) {
|
2017-10-02 23:07:31 +00:00
|
|
|
return false;
|
2016-03-04 22:44:22 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 07:28:02 +00:00
|
|
|
$dbw = $this->getDB( DB_PRIMARY );
|
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;
|
2019-08-30 20:58:41 +00:00
|
|
|
foreach (
|
|
|
|
array_chunk( $eventIDs, $this->config->get( 'UpdateRowsPerQuery' ) ) as $batch
|
|
|
|
) {
|
2018-08-09 23:33:45 +00:00
|
|
|
$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() {
|
2021-05-03 07:28:02 +00:00
|
|
|
$dbw = $this->getDB( DB_PRIMARY );
|
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
|
2021-05-13 00:04:54 +00:00
|
|
|
* @param int $dbSource use primary database or replica storage to pull count
|
2016-03-14 13:37:20 +00:00
|
|
|
* @param array $eventTypesToLoad event types to retrieve
|
|
|
|
* @param int $cap Max count
|
2014-07-18 03:58:21 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2020-06-27 10:05:03 +00:00
|
|
|
public function getCappedNotificationCount(
|
|
|
|
$dbSource,
|
|
|
|
array $eventTypesToLoad = [],
|
|
|
|
$cap = MWEchoNotifUser::MAX_BADGE_COUNT
|
|
|
|
) {
|
2014-07-18 03:58:21 +00:00
|
|
|
// double check
|
2021-05-03 07:28:02 +00:00
|
|
|
if ( !in_array( $dbSource, [ DB_REPLICA, DB_PRIMARY ] ) ) {
|
2017-09-24 05:23:47 +00:00
|
|
|
$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
|
|
|
],
|
2019-03-01 20:40:04 +00:00
|
|
|
'1',
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|