2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
2014-08-14 18:46:26 +00:00
|
|
|
class EchoNotification extends EchoAbstractEntity {
|
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
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Determine whether this is a bundle base. Default is 1,
|
|
|
|
* which means it's a bundle base
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $bundleBase = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The hash used to determine if a set of event could be bundled
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $bundleHash = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The hash used to bundle events to display
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $bundleDisplayHash = '';
|
|
|
|
|
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() {
|
2014-07-18 03:58:21 +00:00
|
|
|
global $wgEchoNotifications;
|
2012-04-27 15:14:24 +00:00
|
|
|
|
2014-08-14 18:46:26 +00:00
|
|
|
$notifMapper = new EchoNotificationMapper();
|
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 );
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->bundleHash = $hash;
|
|
|
|
$lastNotif = $notifMapper->fetchNewestByUserBundleHash( $this->user, $hash );
|
2013-01-15 23:21:39 +00:00
|
|
|
|
|
|
|
// Use a new display hash if:
|
|
|
|
// 1. there was no last bundle notification
|
|
|
|
// 2. last bundle notification with the same hash was read
|
2014-07-18 03:58:21 +00:00
|
|
|
if ( $lastNotif && !$lastNotif->getReadTimestamp() ) {
|
|
|
|
$this->bundleDisplayHash = $lastNotif->getBundleDisplayHash();
|
2013-01-15 23:21:39 +00:00
|
|
|
} else {
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->bundleDisplayHash = md5( $bundleKey . '-display-hash-' . wfTimestampNow() );
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
$notifMapper->insert( $this );
|
2013-05-24 22:51:47 +00:00
|
|
|
|
2014-08-05 21:50:54 +00:00
|
|
|
// Clear applicable section status from cache upon new notification creation
|
|
|
|
MWEchoNotifUser::newFromUser( $this->user )->clearSectionStatusCache(
|
|
|
|
$this->event->getSection()
|
|
|
|
);
|
|
|
|
|
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
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Load a notification record from std class
|
|
|
|
* @param stdClass
|
|
|
|
* @return EchoNotification
|
|
|
|
*/
|
|
|
|
public static function newFromRow( $row ) {
|
|
|
|
$notification = new EchoNotification();
|
|
|
|
|
|
|
|
if ( property_exists( $row, 'event_type' ) ) {
|
|
|
|
$notification->event = EchoEvent::newFromRow( $row );
|
|
|
|
} else {
|
|
|
|
$notification->event = EchoEvent::newFromID( $row->notification_event );
|
|
|
|
}
|
|
|
|
$notification->user = User::newFromId( $row->notification_user );
|
|
|
|
$notification->timestamp = $row->notification_timestamp;
|
|
|
|
$notification->readTimestamp = $row->notification_read_timestamp;
|
|
|
|
$notification->bundleBase = $row->notification_bundle_base;
|
|
|
|
$notification->bundleHash = $row->notification_bundle_hash;
|
|
|
|
$notification->bundleDisplayHash = $row->notification_bundle_display_hash;
|
|
|
|
return $notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert object property to database row array
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function toDbArray() {
|
|
|
|
return array(
|
|
|
|
'notification_event' => $this->event->getId(),
|
|
|
|
'notification_user' => $this->user->getId(),
|
|
|
|
'notification_timestamp' => $this->timestamp,
|
|
|
|
'notification_read_timestamp' => $this->readTimestamp,
|
|
|
|
'notification_bundle_base' => $this->bundleBase,
|
|
|
|
'notification_bundle_hash' => $this->bundleHash,
|
|
|
|
'notification_bundle_display_hash' => $this->bundleDisplayHash
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter method
|
|
|
|
* @return int Notification bundle base
|
|
|
|
*/
|
|
|
|
public function getBundleBase() {
|
|
|
|
return $this->bundleBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter method
|
|
|
|
* @return string|null Notification bundle hash
|
|
|
|
*/
|
|
|
|
public function getBundleHash() {
|
|
|
|
return $this->bundleHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter method
|
|
|
|
* @return string|null Notification bundle display hash
|
|
|
|
*/
|
|
|
|
public function getBundleDisplayHash() {
|
|
|
|
return $this->bundleDisplayHash;
|
|
|
|
}
|
2012-08-30 16:04:39 +00:00
|
|
|
}
|