2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class EchoNotification {
|
2013-03-05 01:31:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var EchoEvent
|
|
|
|
*/
|
|
|
|
protected $event;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $timestamp;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $readTimestamp;
|
2012-04-27 15:14:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do not use this constructor.
|
|
|
|
*/
|
2013-03-05 01:31:41 +00:00
|
|
|
protected function __construct() {}
|
2012-04-27 15:14:24 +00:00
|
|
|
|
|
|
|
/**
|
2013-03-05 01:31:41 +00:00
|
|
|
* Creates an EchoNotification object based on event and user
|
|
|
|
* @param $info array The following keys are required:
|
|
|
|
* - 'event' The EchoEvent being notified about.
|
|
|
|
* - 'user' The User being notified.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @throws MWException
|
|
|
|
* @return EchoNotification
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
2013-03-05 01:31:41 +00:00
|
|
|
public static function create( array $info ) {
|
|
|
|
$obj = new EchoNotification();
|
2012-04-27 15:14:24 +00:00
|
|
|
static $validFields = array( 'event', 'user' );
|
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
foreach ( $validFields as $field ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( isset( $info[$field] ) ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
$obj->$field = $info[$field];
|
|
|
|
} else {
|
|
|
|
throw new MWException( "Field $field is required" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 01:31:41 +00:00
|
|
|
if ( !$obj->user instanceof User && !$obj->user instanceof StubObject ) {
|
|
|
|
throw new MWException( 'Invalid user parameter, expected: User/StubObject object' );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$obj->event instanceof EchoEvent ) {
|
2013-03-05 01:31:41 +00:00
|
|
|
throw new MWException( 'Invalid event parameter, expected: EchoEvent object' );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
// Notification timestamp should be the same as event timestamp
|
|
|
|
$obj->timestamp = $obj->event->getTimestamp();
|
|
|
|
// Safe fallback
|
|
|
|
if ( !$obj->timestamp ) {
|
|
|
|
$obj->timestamp = wfTimestampNow();
|
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
$obj->insert();
|
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-15 23:21:39 +00:00
|
|
|
* Adds this new notification object to the backend storage.
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
|
|
|
protected function insert() {
|
2013-01-15 23:21:39 +00:00
|
|
|
global $wgEchoBackend, $wgEchoNotifications;
|
2012-04-27 15:14:24 +00:00
|
|
|
|
|
|
|
$row = array(
|
|
|
|
'notification_event' => $this->event->getId(),
|
|
|
|
'notification_user' => $this->user->getId(),
|
2013-01-15 23:21:39 +00:00
|
|
|
'notification_timestamp' => $this->timestamp,
|
2012-08-01 19:53:05 +00:00
|
|
|
'notification_read_timestamp' => $this->readTimestamp,
|
2013-01-15 23:21:39 +00:00
|
|
|
'notification_bundle_hash' => '',
|
|
|
|
'notification_bundle_display_hash' => ''
|
2012-04-27 15:14:24 +00:00
|
|
|
);
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
// Get the bundle key for this event if web bundling is enabled
|
|
|
|
$bundleKey = '';
|
|
|
|
if ( !empty( $wgEchoNotifications[$this->event->getType()]['bundle']['web'] ) ) {
|
|
|
|
wfRunHooks( 'EchoGetBundleRules', array( $this->event, &$bundleKey ) );
|
|
|
|
}
|
|
|
|
if ( $bundleKey ) {
|
|
|
|
$hash = md5( $bundleKey );
|
|
|
|
$row['notification_bundle_hash'] = $hash;
|
|
|
|
|
|
|
|
$lastStat = $wgEchoBackend->getLastBundleStat( $this->user, $hash );
|
|
|
|
|
|
|
|
// Use a new display hash if:
|
|
|
|
// 1. there was no last bundle notification
|
|
|
|
// 2. last bundle notification with the same hash was read
|
|
|
|
if ( $lastStat && !$lastStat->notification_read_timestamp ) {
|
|
|
|
$row['notification_bundle_display_hash'] = $lastStat->notification_bundle_display_hash;
|
|
|
|
} else {
|
|
|
|
$row['notification_bundle_display_hash'] = md5( $bundleKey . '-display-hash-' . wfTimestampNow() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 23:12:29 +00:00
|
|
|
$wgEchoBackend->createNotification( $row );
|
2013-05-24 22:51:47 +00:00
|
|
|
|
|
|
|
wfRunHooks( 'EchoCreateNotificationComplete', array( $this ) );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
2013-03-01 00:26:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter method
|
|
|
|
* @return EchoEvent The event for this notification
|
|
|
|
*/
|
|
|
|
public function getEvent() {
|
|
|
|
return $this->event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter method
|
|
|
|
* @return User The recipient of this notification
|
|
|
|
*/
|
|
|
|
public function getUser() {
|
|
|
|
return $this->user;
|
|
|
|
}
|
2013-03-05 01:31:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter method
|
|
|
|
* @return string Notification creation timestamp
|
|
|
|
*/
|
|
|
|
public function getTimestamp() {
|
|
|
|
return $this->timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter method
|
|
|
|
* @return string|null Notification read timestamp
|
|
|
|
*/
|
|
|
|
public function getReadTimestamp() {
|
|
|
|
return $this->readTimestamp;
|
|
|
|
}
|
2012-08-30 16:04:39 +00:00
|
|
|
}
|