2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Database mapper for EchoNotification model
|
|
|
|
*/
|
2014-08-14 18:46:26 +00:00
|
|
|
class EchoNotificationMapper extends EchoAbstractMapper {
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2015-03-16 15:47:13 +00:00
|
|
|
/**
|
|
|
|
* @var EchoTargetPageMapper
|
|
|
|
*/
|
|
|
|
protected $targetPageMapper;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
MWEchoDbFactory $dbFactory = null,
|
|
|
|
EchoTargetPageMapper $targetPageMapper = null
|
|
|
|
) {
|
|
|
|
parent::__construct( $dbFactory );
|
|
|
|
if ( $targetPageMapper === null ) {
|
|
|
|
$targetPageMapper = new EchoTargetPageMapper( $this->dbFactory );
|
|
|
|
}
|
|
|
|
$this->targetPageMapper = $targetPageMapper;
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Insert a notification record
|
|
|
|
* @param EchoNotification
|
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function insert( EchoNotification $notification ) {
|
|
|
|
$dbw = $this->dbFactory->getEchoDb( DB_MASTER );
|
|
|
|
|
|
|
|
$row = $notification->toDbArray();
|
2014-08-07 19:10:37 +00:00
|
|
|
$listeners = $this->getMethodListeners( __FUNCTION__ );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2016-07-22 06:43:21 +00:00
|
|
|
DeferredUpdates::addUpdate( new AtomicSectionUpdate(
|
|
|
|
$dbw,
|
|
|
|
__METHOD__,
|
|
|
|
function ( IDatabase $dbw, $fname ) use ( $row, $listeners ) {
|
|
|
|
// Reset the bundle base if this notification has a display hash
|
|
|
|
// the result of this operation is that all previous notifications
|
|
|
|
// with the same display hash are set to non-base because new record
|
|
|
|
// is becoming the bundle base
|
|
|
|
if ( $row['notification_bundle_display_hash'] ) {
|
|
|
|
$dbw->update(
|
|
|
|
'echo_notification',
|
|
|
|
array( 'notification_bundle_base' => 0 ),
|
|
|
|
array(
|
|
|
|
'notification_user' => $row['notification_user'],
|
|
|
|
'notification_bundle_display_hash' =>
|
|
|
|
$row['notification_bundle_display_hash'],
|
|
|
|
'notification_bundle_base' => 1
|
|
|
|
),
|
|
|
|
$fname
|
|
|
|
);
|
|
|
|
}
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2016-07-22 06:43:21 +00:00
|
|
|
$row['notification_timestamp'] =
|
|
|
|
$dbw->timestamp( $row['notification_timestamp'] );
|
|
|
|
$res = $dbw->insert( 'echo_notification', $row, $fname );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2016-07-22 06:43:21 +00:00
|
|
|
if ( $res ) {
|
|
|
|
foreach ( $listeners as $listener ) {
|
|
|
|
$dbw->onTransactionIdle( $listener );
|
|
|
|
}
|
2014-08-07 19:10:37 +00:00
|
|
|
}
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
2016-07-22 06:43:21 +00:00
|
|
|
) );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract the offset used for notification list
|
|
|
|
* @param $continue String Used for offset
|
|
|
|
* @throws MWException
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
protected function extractQueryOffset( $continue ) {
|
2015-10-01 13:48:52 +00:00
|
|
|
$offset = array(
|
2014-07-18 03:58:21 +00:00
|
|
|
'timestamp' => 0,
|
|
|
|
'offset' => 0,
|
|
|
|
);
|
|
|
|
if ( $continue ) {
|
|
|
|
$values = explode( '|', $continue, 3 );
|
|
|
|
if ( count( $values ) !== 2 ) {
|
|
|
|
throw new MWException( 'Invalid continue param: ' . $continue );
|
|
|
|
}
|
|
|
|
$offset['timestamp'] = (int)$values[0];
|
|
|
|
$offset['offset'] = (int)$values[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $offset;
|
|
|
|
}
|
|
|
|
|
2014-08-14 18:46:26 +00:00
|
|
|
/**
|
2014-09-03 20:56:12 +00:00
|
|
|
* Get unread notifications by user in the amount specified by limit order by
|
|
|
|
* notification timestamp in descending order. We have an index to retrieve
|
|
|
|
* unread notifications but it's not optimized for ordering by timestamp. The
|
|
|
|
* descending order is only allowed if we keep the notification in low volume,
|
|
|
|
* which is done via a deleteJob
|
2014-08-14 18:46:26 +00:00
|
|
|
* @param User $user
|
|
|
|
* @param int $limit
|
2015-12-09 18:41:40 +00:00
|
|
|
* @param string $continue Used for offset
|
2014-08-14 18:46:26 +00:00
|
|
|
* @param string[] $eventTypes
|
2016-06-07 18:48:33 +00:00
|
|
|
* @param Title[] $titles If set, only return notifications for these pages.
|
2016-06-23 22:32:18 +00:00
|
|
|
* To find notifications not associated with any page, add null as an element to this array.
|
2015-12-09 17:00:15 +00:00
|
|
|
* @param int $dbSource Use master or slave database
|
2014-08-14 18:46:26 +00:00
|
|
|
* @return EchoNotification[]
|
|
|
|
*/
|
2016-06-07 18:48:33 +00:00
|
|
|
public function fetchUnreadByUser( User $user, $limit, $continue, array $eventTypes = array(), array $titles = null, $dbSource = DB_SLAVE ) {
|
|
|
|
$conds['notification_read_timestamp'] = null;
|
|
|
|
if ( $titles ) {
|
|
|
|
$conds['event_page_id'] = $this->getIdsForTitles( $titles );
|
|
|
|
if ( !$conds['event_page_id'] ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->fetchByUserInternal( $user, $limit, $continue, $eventTypes, $conds, $dbSource );
|
2014-08-14 18:46:26 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 16:18:30 +00:00
|
|
|
/**
|
|
|
|
* Get read notifications by user in the amount specified by limit order by
|
|
|
|
* notification timestamp in descending order. We have an index to retrieve
|
|
|
|
* unread notifications but it's not optimized for ordering by timestamp. The
|
|
|
|
* descending order is only allowed if we keep the notification in low volume,
|
|
|
|
* which is done via a deleteJob
|
|
|
|
* @param User $user
|
|
|
|
* @param int $limit
|
|
|
|
* @param string $continue Used for offset
|
|
|
|
* @param string[] $eventTypes
|
2016-06-07 18:48:33 +00:00
|
|
|
* @param Title[] $titles If set, only return notifications for these pages.
|
2016-06-23 22:32:18 +00:00
|
|
|
* To find notifications not associated with any page, add null as an element to this array.
|
2015-12-10 16:18:30 +00:00
|
|
|
* @param int $dbSource Use master or slave database
|
|
|
|
* @return EchoNotification[]
|
|
|
|
*/
|
2016-06-07 18:48:33 +00:00
|
|
|
public function fetchReadByUser( User $user, $limit, $continue, array $eventTypes = array(), array $titles = null, $dbSource = DB_SLAVE ) {
|
|
|
|
$conds = array( 'notification_read_timestamp IS NOT NULL' );
|
|
|
|
if ( $titles ) {
|
|
|
|
$conds['event_page_id'] = $this->getIdsForTitles( $titles );
|
|
|
|
if ( !$conds['event_page_id'] ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->fetchByUserInternal( $user, $limit, $continue, $eventTypes, $conds, $dbSource );
|
2015-12-10 16:18:30 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Get Notification by user in batch along with limit, offset etc
|
2014-09-23 00:24:03 +00:00
|
|
|
*
|
|
|
|
* @param User $user the user to get notifications for
|
|
|
|
* @param int $limit The maximum number of notifications to return
|
|
|
|
* @param string $continue Used for offset
|
|
|
|
* @param array $eventTypes Event types to load
|
|
|
|
* @param array $excludeEventIds Event id's to exclude.
|
2016-06-07 18:48:33 +00:00
|
|
|
* @param Title[] $titles If set, only return notifications for these pages.
|
2016-06-23 22:32:18 +00:00
|
|
|
* To find notifications not associated with any page, add null as an element to this array.
|
2014-07-18 03:58:21 +00:00
|
|
|
* @return EchoNotification[]
|
|
|
|
*/
|
2016-06-07 18:48:33 +00:00
|
|
|
public function fetchByUser( User $user, $limit, $continue, array $eventTypes = array(), array $excludeEventIds = array(), array $titles = null ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
$dbr = $this->dbFactory->getEchoDb( DB_SLAVE );
|
|
|
|
|
2015-12-09 17:00:15 +00:00
|
|
|
$conds = array();
|
|
|
|
if ( $excludeEventIds ) {
|
|
|
|
$conds[] = 'event_id NOT IN ( ' . $dbr->makeList( $excludeEventIds ) . ' ) ';
|
|
|
|
}
|
2016-06-07 18:48:33 +00:00
|
|
|
if ( $titles ) {
|
|
|
|
$conds['event_page_id'] = $this->getIdsForTitles( $titles );
|
|
|
|
if ( !$conds['event_page_id'] ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 17:00:15 +00:00
|
|
|
|
2015-12-09 18:41:40 +00:00
|
|
|
return $this->fetchByUserInternal( $user, $limit, $continue, $eventTypes, $conds );
|
2015-12-09 17:00:15 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 18:48:33 +00:00
|
|
|
protected function getIdsForTitles( array $titles ) {
|
|
|
|
$ids = array();
|
|
|
|
foreach ( $titles as $title ) {
|
2016-06-23 22:32:18 +00:00
|
|
|
if ( $title === null ) {
|
|
|
|
$ids[] = null;
|
|
|
|
} elseif ( $title->exists() ) {
|
2016-06-07 18:48:33 +00:00
|
|
|
$ids[] = $title->getArticleId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ids;
|
|
|
|
}
|
|
|
|
|
2015-12-09 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* @param User $user the user to get notifications for
|
|
|
|
* @param int $limit The maximum number of notifications to return
|
2015-12-09 18:41:40 +00:00
|
|
|
* @param string $continue Used for offset
|
2015-12-09 17:00:15 +00:00
|
|
|
* @param array $eventTypes Event types to load
|
|
|
|
* @param array $conds Additional query conditions.
|
|
|
|
* @param int $dbSource Use master or slave database
|
|
|
|
* @return EchoNotification[]
|
|
|
|
*/
|
2015-12-09 18:41:40 +00:00
|
|
|
protected function fetchByUserInternal( User $user, $limit, $continue, array $eventTypes = array(), array $conds = array(), $dbSource = DB_SLAVE ) {
|
2015-12-09 17:00:15 +00:00
|
|
|
$dbr = $this->dbFactory->getEchoDb( $dbSource );
|
|
|
|
|
2014-08-14 18:46:26 +00:00
|
|
|
if ( !$eventTypes ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2014-07-22 21:33:22 +00:00
|
|
|
// There is a problem with querying by event type, if a user has only one or none
|
2015-12-09 17:00:15 +00:00
|
|
|
// flow notification and huge amount other notifications, the lookup of only flow
|
2014-07-22 21:33:22 +00:00
|
|
|
// notification will result in a slow query. Luckily users won't have that many
|
|
|
|
// notifications. We should have some cron job to remove old notifications so
|
|
|
|
// the notification volume is in a reasonable amount for such case. The other option
|
|
|
|
// is to denormalize notification table with event_type and lookup index.
|
2014-07-18 03:58:21 +00:00
|
|
|
$conds = array(
|
|
|
|
'notification_user' => $user->getID(),
|
2014-08-14 18:46:26 +00:00
|
|
|
'event_type' => $eventTypes,
|
2016-03-04 19:23:02 +00:00
|
|
|
'event_deleted' => 0,
|
2015-12-09 17:00:15 +00:00
|
|
|
) + $conds;
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2015-12-09 18:41:40 +00:00
|
|
|
$offset = $this->extractQueryOffset( $continue );
|
|
|
|
|
|
|
|
// Start points are specified
|
|
|
|
if ( $offset['timestamp'] && $offset['offset'] ) {
|
|
|
|
$ts = $dbr->addQuotes( $dbr->timestamp( $offset['timestamp'] ) );
|
|
|
|
// The offset and timestamp are those of the first notification we want to return
|
|
|
|
$conds[] = "notification_timestamp < $ts OR ( notification_timestamp = $ts AND notification_event <= " . $offset['offset'] . " )";
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
$res = $dbr->select(
|
2016-05-28 04:46:23 +00:00
|
|
|
array( 'echo_notification', 'echo_event' ),
|
2014-07-18 03:58:21 +00:00
|
|
|
'*',
|
|
|
|
$conds,
|
|
|
|
__METHOD__,
|
|
|
|
array(
|
|
|
|
'ORDER BY' => 'notification_timestamp DESC, notification_event DESC',
|
|
|
|
'LIMIT' => $limit,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'echo_event' => array( 'LEFT JOIN', 'notification_event=event_id' ),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-03-16 15:47:13 +00:00
|
|
|
// query failure of some sort
|
|
|
|
if ( !$res ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2016-03-04 19:23:02 +00:00
|
|
|
$allNotifications = array();
|
2015-03-16 15:47:13 +00:00
|
|
|
foreach ( $res as $row ) {
|
|
|
|
try {
|
2016-03-04 19:23:02 +00:00
|
|
|
$notification = EchoNotification::newFromRow( $row );
|
|
|
|
if ( $notification ) {
|
|
|
|
$allNotifications[] = $notification;
|
2016-05-13 23:39:17 +00:00
|
|
|
}
|
2015-03-16 15:47:13 +00:00
|
|
|
} catch ( Exception $e ) {
|
|
|
|
$id = isset( $row->event_id ) ? $row->event_id : 'unknown event';
|
|
|
|
wfDebugLog( 'Echo', __METHOD__ . ": Failed initializing event: $id" );
|
|
|
|
MWExceptionHandler::logException( $e );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2016-03-04 19:23:02 +00:00
|
|
|
$data = array();
|
|
|
|
/** @var EchoNotification $notification */
|
|
|
|
foreach ( $allNotifications as $notification ) {
|
|
|
|
$data[ $notification->getEvent()->getId() ] = $notification;
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the last notification in a set of bundle-able notifications by a bundle hash
|
2015-12-09 17:00:15 +00:00
|
|
|
* @param User $user
|
|
|
|
* @param string $bundleHash The hash used to identify a set of bundle-able notifications
|
2014-07-18 03:58:21 +00:00
|
|
|
* @return EchoNotification|bool
|
|
|
|
*/
|
|
|
|
public function fetchNewestByUserBundleHash( User $user, $bundleHash ) {
|
|
|
|
$dbr = $this->dbFactory->getEchoDb( DB_SLAVE );
|
|
|
|
|
|
|
|
$row = $dbr->selectRow(
|
|
|
|
array( 'echo_notification', 'echo_event' ),
|
|
|
|
array( '*' ),
|
|
|
|
array(
|
|
|
|
'notification_user' => $user->getId(),
|
|
|
|
'notification_bundle_hash' => $bundleHash
|
|
|
|
),
|
|
|
|
__METHOD__,
|
|
|
|
array( 'ORDER BY' => 'notification_timestamp DESC', 'LIMIT' => 1 ),
|
|
|
|
array(
|
|
|
|
'echo_event' => array( 'LEFT JOIN', 'notification_event=event_id' ),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
if ( $row ) {
|
|
|
|
return EchoNotification::newFromRow( $row );
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 01:56:13 +00:00
|
|
|
/**
|
2016-06-13 20:41:12 +00:00
|
|
|
* Fetch EchoNotifications by user and event IDs.
|
2016-05-03 01:56:13 +00:00
|
|
|
*
|
|
|
|
* @param User $user
|
2016-06-13 20:41:12 +00:00
|
|
|
* @param int[] $eventIds
|
|
|
|
* @return EchoNotification[]|bool
|
2016-05-03 01:56:13 +00:00
|
|
|
*/
|
2016-06-13 20:41:12 +00:00
|
|
|
public function fetchByUserEvents( User $user, $eventIds ) {
|
2016-05-03 01:56:13 +00:00
|
|
|
$dbr = $this->dbFactory->getEchoDb( DB_SLAVE );
|
|
|
|
|
2016-06-13 20:41:12 +00:00
|
|
|
$result = $dbr->select(
|
2016-05-03 01:56:13 +00:00
|
|
|
array( 'echo_notification', 'echo_event' ),
|
|
|
|
'*',
|
|
|
|
array(
|
|
|
|
'notification_user' => $user->getId(),
|
2016-06-13 20:41:12 +00:00
|
|
|
'notification_event' => $eventIds
|
2016-05-03 01:56:13 +00:00
|
|
|
),
|
2016-06-13 20:41:12 +00:00
|
|
|
__METHOD__,
|
|
|
|
array(),
|
|
|
|
array(
|
|
|
|
'echo_event' => array( 'INNER JOIN', 'notification_event=event_id' ),
|
|
|
|
)
|
2016-05-03 01:56:13 +00:00
|
|
|
);
|
|
|
|
|
2016-06-13 20:41:12 +00:00
|
|
|
if ( $result ) {
|
|
|
|
$notifications = array();
|
|
|
|
foreach ( $result as $row ) {
|
|
|
|
$notifications[] = EchoNotification::newFromRow( $row );
|
|
|
|
}
|
|
|
|
return $notifications;
|
2016-05-03 01:56:13 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-09 22:11:53 +00:00
|
|
|
/**
|
|
|
|
* Fetch a notification by user in the specified offset. The caller should
|
|
|
|
* know that passing a big number for offset is NOT going to work
|
|
|
|
* @param User $user
|
|
|
|
* @param int $offset
|
|
|
|
* @return EchoNotification|bool
|
|
|
|
*/
|
|
|
|
public function fetchByUserOffset( User $user, $offset ) {
|
|
|
|
$dbr = $this->dbFactory->getEchoDb( DB_SLAVE );
|
|
|
|
$row = $dbr->selectRow(
|
|
|
|
array( 'echo_notification', 'echo_event' ),
|
|
|
|
array( '*' ),
|
|
|
|
array(
|
|
|
|
'notification_user' => $user->getId(),
|
2016-03-04 19:23:02 +00:00
|
|
|
'event_deleted' => 0,
|
2014-09-09 22:11:53 +00:00
|
|
|
),
|
|
|
|
__METHOD__,
|
|
|
|
array(
|
|
|
|
'ORDER BY' => 'notification_timestamp DESC, notification_event DESC',
|
|
|
|
'OFFSET' => $offset,
|
|
|
|
'LIMIT' => 1
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'echo_event' => array( 'LEFT JOIN', 'notification_event=event_id' ),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $row ) {
|
|
|
|
return EchoNotification::newFromRow( $row );
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Batch delete notifications by user and eventId offset
|
|
|
|
* @param User $user
|
|
|
|
* @param int $eventId
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function deleteByUserEventOffset( User $user, $eventId ) {
|
|
|
|
$dbw = $this->dbFactory->getEchoDb( DB_MASTER );
|
|
|
|
$res = $dbw->delete(
|
|
|
|
'echo_notification',
|
|
|
|
array(
|
|
|
|
'notification_user' => $user->getId(),
|
|
|
|
'notification_event < ' . (int)$eventId
|
|
|
|
),
|
|
|
|
__METHOD__
|
|
|
|
);
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-09-09 22:11:53 +00:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2016-03-04 19:23:02 +00:00
|
|
|
/**
|
|
|
|
* Fetch ids of users that have notifications for certain events
|
|
|
|
*
|
|
|
|
* @param int[] $eventIds
|
|
|
|
* @return int[]|false
|
|
|
|
*/
|
|
|
|
public function fetchUsersWithNotificationsForEvents( $eventIds ) {
|
|
|
|
$dbr = $this->dbFactory->getEchoDb( DB_SLAVE );
|
|
|
|
|
|
|
|
$res = $dbr->select(
|
|
|
|
array( 'echo_notification' ),
|
|
|
|
array( 'userId' => 'notification_user' ),
|
|
|
|
array(
|
|
|
|
'notification_event' => $eventIds
|
|
|
|
),
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $res ) {
|
|
|
|
$userIds = array();
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$userIds[] = $row->userId;
|
|
|
|
}
|
|
|
|
return $userIds;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|