mediawiki-extensions-Echo/includes/gateway/UserNotificationGateway.php
Matthew Flaschen cac85b635f Make plural support for large values (100 or more) explicit in l10n
This involves:

* Making this value no longer admin-configurable.
* Changing getNotificationCountForOutput to return only a single value
  Since there is no + in the formatted value anymore, we can actually
  use the same value for both.

  This is a B/C break, but hopefully worth it to simplify the method
  call.

  For now, the excess parameter is just marked unused.  It could be
  removed at some point if the translations are updated.

This must be merged at the same time as:
* Flow - Ibfa56b1af9e8c56b4c5f900e0d487bc09688b2a2
* MobileFrontend - Ibf784b279d56773a227ff261b75f2b26125bbb63 (well, MF
  can be merged first)
* translatewiki - I2a4b6938aed49e4101deb6b9351c43656a863490

Also, change 1 to One/one, per Siebrand on the task.  This can easily
be dropped/undone if we don't want it.

Also, remove reference to no-longer-existent notification-page-linked-bundle

Bug: T127288
Change-Id: Iabeaae747f99980c0610d552f6b38f89d940b890
2016-03-16 18:43:16 -04:00

181 lines
4.1 KiB
PHP

<?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;
/**
* The tables for this gateway
*/
protected static $eventTable = 'echo_event';
protected static $notificationTable = 'echo_notification';
/**
* @param User
* @param MWEchoDbFactory
*/
public function __construct( User $user, MWEchoDbFactory $dbFactory ) {
$this->user = $user;
$this->dbFactory = $dbFactory;
}
/**
* Mark notifications as read
* @param $eventIDs array
* @return boolean
*/
public function markRead( array $eventIDs ) {
if ( !$eventIDs ) {
return;
}
$dbw = $this->dbFactory->getEchoDb( DB_MASTER );
return $dbw->update(
self::$notificationTable,
array( 'notification_read_timestamp' => $dbw->timestamp( wfTimestampNow() ) ),
array(
'notification_user' => $this->user->getId(),
'notification_event' => $eventIDs,
'notification_read_timestamp' => null,
),
__METHOD__
);
}
/**
* Mark notifications as unread
* @param $eventIDs array
* @return boolean
*/
public function markUnRead( array $eventIDs ) {
if ( !$eventIDs ) {
return;
}
$dbw = $this->dbFactory->getEchoDb( DB_MASTER );
return $dbw->update(
self::$notificationTable,
array( 'notification_read_timestamp' => null ),
array(
'notification_user' => $this->user->getId(),
'notification_event' => $eventIDs,
'notification_read_timestamp IS NOT NULL'
),
__METHOD__
);
}
/**
* Mark all notification as read, use MWEchoNotifUer::markAllRead() instead
* @deprecated may need this when running in a job or revive this when we
* have updateJoin()
*/
public function markAllRead() {
$dbw = $this->dbFactory->getEchoDb( DB_MASTER );
return $dbw->update(
self::$notificationTable,
array( 'notification_read_timestamp' => $dbw->timestamp( wfTimestampNow() ) ),
array(
'notification_user' => $this->user->getId(),
'notification_read_timestamp' => null,
'notification_bundle_base' => 1,
),
__METHOD__
);
}
/**
* Get notification count for the types specified
* @param int use master or slave storage to pull count
* @param array event types to retrieve
* @return int
*/
public function getNotificationCount( $dbSource, array $eventTypesToLoad = array() ) {
// double check
if ( !in_array( $dbSource, array( DB_SLAVE, DB_MASTER ) ) ) {
$dbSource = DB_SLAVE;
}
if ( !$eventTypesToLoad ) {
return 0;
}
$db = $this->dbFactory->getEchoDb( $dbSource );
$res = $db->select(
array(
self::$notificationTable,
self::$eventTable
),
array( 'notification_event' ),
array(
'notification_user' => $this->user->getId(),
'notification_bundle_base' => 1,
'notification_read_timestamp' => null,
'event_type' => $eventTypesToLoad,
),
__METHOD__,
array( 'LIMIT' => MWEchoNotifUser::MAX_BADGE_COUNT + 1 ),
array(
'echo_event' => array( 'LEFT JOIN', 'notification_event=event_id' ),
)
);
if ( $res ) {
return $db->numRows( $res );
} else {
return 0;
}
}
/**
* IMPORTANT: should only call this function if the number of unread notification
* is reasonable, for example, unread notification count is less than the max
* display defined in MWEchoNotifUser::MAX_BADGE_COUNT
* @param string
* @return int[]
*/
public function getUnreadNotifications( $type ) {
$dbr = $this->dbFactory->getEchoDb( DB_SLAVE );
$res = $dbr->select(
array(
self::$notificationTable,
self::$eventTable
),
array( 'notification_event' ),
array(
'notification_user' => $this->user->getId(),
'notification_bundle_base' => 1,
'notification_read_timestamp' => null,
'event_type' => $type,
'notification_event = event_id'
),
__METHOD__
);
$eventIds = array();
if ( $res ) {
foreach ( $res as $row ) {
$eventIds[$row->notification_event] = $row->notification_event;
}
}
return $eventIds;
}
}