2013-01-15 23:21:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Database backend for echo notification
|
|
|
|
*/
|
|
|
|
class MWDbEchoBackend extends MWEchoBackend {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $row array
|
|
|
|
*/
|
|
|
|
public function createNotification( $row ) {
|
2013-04-18 23:01:12 +00:00
|
|
|
$dbw = MWEchoDbFactory::getDB( DB_MASTER );
|
2013-01-15 23:21:39 +00:00
|
|
|
|
2013-05-16 00:29:21 +00:00
|
|
|
$fname = __METHOD__;
|
|
|
|
$dbw->onTransactionIdle(
|
|
|
|
function() use ( $dbw, $row, $fname ) {
|
|
|
|
$dbw->begin( $fname );
|
|
|
|
// reset the base if this notification has a display hash
|
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$row['notification_timestamp'] = $dbw->timestamp( $row['notification_timestamp'] );
|
|
|
|
$dbw->insert( 'echo_notification', $row, $fname );
|
|
|
|
$dbw->commit( $fname );
|
|
|
|
}
|
|
|
|
);
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $user User the user to get notifications for
|
|
|
|
* @param $limit int The maximum number of notifications to return
|
2013-05-03 23:58:56 +00:00
|
|
|
* @param $continue string Used for offset
|
2013-01-14 23:52:46 +00:00
|
|
|
* @param $outputFormat string The output format of the notifications (web,
|
|
|
|
* email, etc.)
|
2013-01-15 23:21:39 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2013-05-03 23:58:56 +00:00
|
|
|
public function loadNotifications( $user, $limit, $continue, $outputFormat = 'web' ) {
|
2013-04-18 23:01:12 +00:00
|
|
|
$dbr = MWEchoDbFactory::getDB( DB_SLAVE );
|
|
|
|
|
2013-04-28 18:07:28 +00:00
|
|
|
$eventTypesToLoad = EchoNotificationController::getUserEnabledEvents( $user, $outputFormat );
|
2013-01-14 23:52:46 +00:00
|
|
|
if ( !$eventTypesToLoad ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
// Look for notifications with base = 1
|
2013-01-15 23:21:39 +00:00
|
|
|
$conds = array(
|
|
|
|
'notification_user' => $user->getID(),
|
2013-01-14 23:52:46 +00:00
|
|
|
'event_type' => $eventTypesToLoad,
|
2013-01-15 23:21:39 +00:00
|
|
|
'notification_bundle_base' => 1
|
2013-01-15 23:21:39 +00:00
|
|
|
);
|
|
|
|
|
2013-05-03 23:58:56 +00:00
|
|
|
$offset = $this->extractQueryOffset( $continue );
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
// Start points are specified
|
2013-05-03 23:58:56 +00:00
|
|
|
if ( $offset['timestamp'] && $offset['offset'] ) {
|
2013-05-08 18:31:37 +00:00
|
|
|
$ts = $dbr->addQuotes( $dbr->timestamp( $offset['timestamp'] ) );
|
|
|
|
$conds[] = "notification_timestamp < $ts OR ( notification_timestamp = $ts AND notification_event < " . $offset['offset'] . " )";
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 23:01:12 +00:00
|
|
|
$res = $dbr->select(
|
2013-01-15 23:21:39 +00:00
|
|
|
array( 'echo_notification', 'echo_event' ),
|
|
|
|
'*',
|
|
|
|
$conds,
|
|
|
|
__METHOD__,
|
|
|
|
array(
|
|
|
|
'ORDER BY' => 'notification_timestamp DESC, notification_event DESC',
|
|
|
|
'LIMIT' => $limit,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'echo_event' => array( 'LEFT JOIN', 'notification_event=event_id' ),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return iterator_to_array( $res, false );
|
|
|
|
}
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
/**
|
|
|
|
* @param $user User
|
|
|
|
* @param $bundleHash string the bundle hash
|
2013-03-06 00:04:48 +00:00
|
|
|
* @param $type string
|
2013-01-15 23:21:39 +00:00
|
|
|
* @return ResultWrapper|bool
|
|
|
|
*/
|
2013-03-06 00:04:48 +00:00
|
|
|
public function getRawBundleData( $user, $bundleHash, $type = 'web' ) {
|
2013-04-18 23:01:12 +00:00
|
|
|
$dbr = MWEchoDbFactory::getDB( DB_SLAVE );
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
// We only display 99+ if the number is over 100, we can do limit 250, this should be sufficient
|
|
|
|
// to return 99 distinct group iterators, avoid select count( distinct ) for the folliwng:
|
|
|
|
// 1. it will not scale for large volume data
|
|
|
|
// 2. notification may have random grouping iterator
|
|
|
|
// 2. agent may be anonymous, can't do distinct over two columens: event_agent_id and event_agent_ip
|
2013-03-06 00:04:48 +00:00
|
|
|
if ( $type == 'web' ) {
|
2013-04-18 23:01:12 +00:00
|
|
|
$res = $dbr->select(
|
2013-03-06 00:04:48 +00:00
|
|
|
array( 'echo_notification', 'echo_event' ),
|
|
|
|
array(
|
|
|
|
'event_agent_id',
|
|
|
|
'event_agent_ip',
|
|
|
|
'event_extra',
|
|
|
|
'event_page_namespace',
|
|
|
|
'event_page_title'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'notification_event=event_id',
|
|
|
|
'notification_user' => $user->getId(),
|
|
|
|
'notification_bundle_base' => 0,
|
|
|
|
'notification_bundle_display_hash' => $bundleHash
|
|
|
|
),
|
|
|
|
__METHOD__,
|
|
|
|
array( 'ORDER BY' => 'notification_timestamp DESC', 'LIMIT' => 250 )
|
|
|
|
);
|
|
|
|
// this would be email for now
|
|
|
|
} else {
|
2013-04-18 23:01:12 +00:00
|
|
|
$res = $dbr->select(
|
2013-03-06 00:04:48 +00:00
|
|
|
array( 'echo_email_batch', 'echo_event' ),
|
|
|
|
array(
|
|
|
|
'event_agent_id',
|
|
|
|
'event_agent_ip',
|
|
|
|
'event_extra',
|
|
|
|
'event_page_namespace',
|
|
|
|
'event_page_title'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'eeb_event_id=event_id',
|
|
|
|
'eeb_user_id' => $user->getId(),
|
|
|
|
'eeb_event_hash' => $bundleHash
|
|
|
|
),
|
|
|
|
__METHOD__,
|
|
|
|
array( 'ORDER BY' => 'eeb_event_id DESC', 'LIMIT' => 250 )
|
|
|
|
);
|
|
|
|
}
|
2013-01-15 23:21:39 +00:00
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the last bundle stat - read_timestamp & bundle_display_hash
|
|
|
|
* @param $user User
|
|
|
|
* @param $bundleHash string The hash used to identify a set of bundle-able events
|
|
|
|
* @return ResultWrapper|bool
|
|
|
|
*/
|
|
|
|
public function getLastBundleStat( $user, $bundleHash ) {
|
2013-04-18 23:01:12 +00:00
|
|
|
$dbr = MWEchoDbFactory::getDB( DB_SLAVE );
|
|
|
|
|
|
|
|
$res = $dbr->selectRow(
|
2013-01-15 23:21:39 +00:00
|
|
|
array( 'echo_notification' ),
|
|
|
|
array( 'notification_read_timestamp', 'notification_bundle_display_hash' ),
|
|
|
|
array(
|
|
|
|
'notification_user' => $user->getId(),
|
|
|
|
'notification_bundle_hash' => $bundleHash
|
|
|
|
),
|
|
|
|
__METHOD__,
|
|
|
|
array( 'ORDER BY' => 'notification_timestamp DESC', 'LIMIT' => 1 )
|
|
|
|
);
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
/**
|
|
|
|
* @param $row array
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function createEvent( $row ) {
|
2013-04-18 23:01:12 +00:00
|
|
|
$dbw = MWEchoDbFactory::getDB( DB_MASTER );
|
|
|
|
|
|
|
|
$id = $dbw->nextSequenceValue( 'echo_event_id' );
|
2013-01-15 23:21:39 +00:00
|
|
|
|
2013-04-17 20:58:13 +00:00
|
|
|
if ( $id ) {
|
|
|
|
$row['event_id'] = $id;
|
|
|
|
}
|
|
|
|
|
2013-04-18 23:01:12 +00:00
|
|
|
$dbw->insert( 'echo_event', $row, __METHOD__ );
|
2013-01-15 23:21:39 +00:00
|
|
|
|
|
|
|
if ( !$id ) {
|
2013-04-18 23:01:12 +00:00
|
|
|
$id = $dbw->insertId();
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $id int
|
|
|
|
* @param $fromMaster bool
|
|
|
|
* @return ResultWrapper
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function loadEvent( $id, $fromMaster = false ) {
|
2013-04-18 23:01:12 +00:00
|
|
|
$db = $fromMaster ? MWEchoDbFactory::getDB( DB_MASTER ) : MWEchoDbFactory::getDB( DB_SLAVE );
|
2013-01-15 23:21:39 +00:00
|
|
|
|
|
|
|
$row = $db->selectRow( 'echo_event', '*', array( 'event_id' => $id ), __METHOD__ );
|
|
|
|
|
|
|
|
if ( !$row && !$fromMaster ) {
|
|
|
|
return $this->loadEvent( $id, true );
|
|
|
|
} elseif ( !$row ) {
|
|
|
|
throw new MWException( "No EchoEvent found with ID: $id" );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $event EchoEvent
|
|
|
|
*/
|
|
|
|
public function updateEventExtra( $event ) {
|
2013-04-18 23:01:12 +00:00
|
|
|
$dbw = MWEchoDbFactory::getDB( DB_MASTER );
|
|
|
|
|
|
|
|
$dbw->update(
|
2013-01-15 23:21:39 +00:00
|
|
|
'echo_event',
|
|
|
|
array( 'event_extra' => $event->serializeExtra() ),
|
|
|
|
array( 'event_id' => $event->getId() ),
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $user User
|
|
|
|
* @param $eventIDs array
|
|
|
|
*/
|
|
|
|
public function markRead( $user, $eventIDs ) {
|
|
|
|
if ( !$eventIDs ) {
|
|
|
|
return;
|
|
|
|
}
|
2013-04-18 23:01:12 +00:00
|
|
|
|
|
|
|
$dbw = MWEchoDbFactory::getDB( DB_MASTER );
|
|
|
|
|
|
|
|
$dbw->update(
|
2013-01-15 23:21:39 +00:00
|
|
|
'echo_notification',
|
2013-04-18 23:01:12 +00:00
|
|
|
array( 'notification_read_timestamp' => $dbw->timestamp( wfTimestampNow() ) ),
|
2013-01-15 23:21:39 +00:00
|
|
|
array(
|
|
|
|
'notification_user' => $user->getId(),
|
|
|
|
'notification_event' => $eventIDs,
|
2013-05-04 00:46:30 +00:00
|
|
|
'notification_read_timestamp' => null,
|
2013-01-15 23:21:39 +00:00
|
|
|
),
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-04-17 01:00:21 +00:00
|
|
|
/**
|
|
|
|
* @param $user User
|
|
|
|
*/
|
|
|
|
public function markAllRead( $user ) {
|
2013-04-18 23:01:12 +00:00
|
|
|
$dbw = MWEchoDbFactory::getDB( DB_MASTER );
|
|
|
|
|
|
|
|
$dbw->update(
|
2013-04-17 01:00:21 +00:00
|
|
|
'echo_notification',
|
2013-04-18 23:01:12 +00:00
|
|
|
array( 'notification_read_timestamp' => $dbw->timestamp( wfTimestampNow() ) ),
|
2013-04-17 01:00:21 +00:00
|
|
|
array(
|
|
|
|
'notification_user' => $user->getId(),
|
|
|
|
'notification_read_timestamp' => NULL,
|
|
|
|
'notification_bundle_base' => 1,
|
|
|
|
),
|
|
|
|
__METHOD__,
|
|
|
|
array( 'LIMIT' => 500 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
/**
|
|
|
|
* @param $user User object to check notifications for
|
|
|
|
* @param $dbSource string use master or slave storage to pull count
|
2013-01-15 23:21:39 +00:00
|
|
|
* @return int
|
2013-01-15 23:21:39 +00:00
|
|
|
*/
|
|
|
|
public function getNotificationCount( $user, $dbSource ) {
|
|
|
|
// double check
|
|
|
|
if ( !in_array( $dbSource, array( DB_SLAVE, DB_MASTER ) ) ) {
|
|
|
|
$dbSource = DB_SLAVE;
|
|
|
|
}
|
|
|
|
|
2013-04-28 18:07:28 +00:00
|
|
|
$eventTypesToLoad = EchoNotificationController::getUserEnabledEvents( $user, 'web' );
|
2013-02-14 19:14:08 +00:00
|
|
|
|
|
|
|
if ( !$eventTypesToLoad ) {
|
2013-05-17 21:16:14 +00:00
|
|
|
return 0;
|
2013-02-14 19:14:08 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
global $wgEchoMaxNotificationCount;
|
|
|
|
|
2013-04-09 22:13:39 +00:00
|
|
|
$db = MWEchoDbFactory::getDB( $dbSource );
|
2013-01-15 23:21:39 +00:00
|
|
|
$res = $db->select(
|
2013-01-15 23:21:39 +00:00
|
|
|
array( 'echo_notification', 'echo_event' ),
|
2013-01-15 23:21:39 +00:00
|
|
|
array( 'notification_event' ),
|
2013-01-15 23:21:39 +00:00
|
|
|
array(
|
|
|
|
'notification_user' => $user->getId(),
|
2013-01-15 23:21:39 +00:00
|
|
|
'notification_bundle_base' => 1,
|
2013-01-15 23:21:39 +00:00
|
|
|
'notification_read_timestamp' => null,
|
2013-02-14 19:14:08 +00:00
|
|
|
'event_type' => $eventTypesToLoad,
|
2013-01-15 23:21:39 +00:00
|
|
|
),
|
|
|
|
__METHOD__,
|
2013-01-15 23:21:39 +00:00
|
|
|
array( 'LIMIT' => $wgEchoMaxNotificationCount + 1 ),
|
2013-01-15 23:21:39 +00:00
|
|
|
array(
|
|
|
|
'echo_event' => array( 'LEFT JOIN', 'notification_event=event_id' ),
|
|
|
|
)
|
|
|
|
);
|
2013-01-15 23:21:39 +00:00
|
|
|
return $db->numRows( $res );
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|