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;
|
|
|
|
|
2014-08-07 00:55:24 +00:00
|
|
|
/**
|
2015-03-16 15:47:13 +00:00
|
|
|
* The target page object for the notification if there is one. Null means
|
|
|
|
* the information has not been loaded.
|
|
|
|
*
|
|
|
|
* @var EchoTargetPage[]|null
|
2014-08-07 00:55:24 +00:00
|
|
|
*/
|
2015-03-16 15:47:13 +00:00
|
|
|
protected $targetPages;
|
2014-08-07 00:55:24 +00:00
|
|
|
|
2013-03-05 01:31:41 +00:00
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2015-10-01 13:48:52 +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();
|
|
|
|
}
|
|
|
|
|
2015-10-29 11:23:31 +00:00
|
|
|
// @Todo - Database insert logic should not be inside the model
|
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'] ) ) {
|
2015-06-01 18:36:35 +00:00
|
|
|
Hooks::run( 'EchoGetBundleRules', array( $this->event, &$bundleKey ) );
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
2014-08-07 19:10:37 +00:00
|
|
|
|
|
|
|
// The list of event ids to be removed from echo_target_page,
|
|
|
|
// this is mainly for bundled notifications when an event is
|
|
|
|
// no longer the bundle base
|
|
|
|
$eventIds = array();
|
2013-01-15 23:21:39 +00:00
|
|
|
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();
|
2014-08-07 19:10:37 +00:00
|
|
|
$lastEvent = $lastNotif->getEvent();
|
|
|
|
if ( $lastEvent ) {
|
|
|
|
$eventIds[] = $lastEvent->getId();
|
|
|
|
}
|
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-08-07 19:10:37 +00:00
|
|
|
// Create a target page object if specified by event
|
|
|
|
$event = $this->event;
|
|
|
|
$user = $this->user;
|
2015-03-16 15:47:13 +00:00
|
|
|
$targetPages = self::resolveTargetPages( $event->getExtraParam( 'target-page' ) );
|
|
|
|
if ( $targetPages ) {
|
2015-10-01 13:48:52 +00:00
|
|
|
$notifMapper->attachListener( 'insert', 'add-target-page', function () use ( $event, $user, $eventIds, $targetPages ) {
|
2015-03-16 15:47:13 +00:00
|
|
|
$targetMapper = new EchoTargetPageMapper();
|
|
|
|
if ( $eventIds ) {
|
|
|
|
$targetMapper->deleteByUserEvents( $user, $eventIds );
|
2014-08-26 22:27:48 +00:00
|
|
|
}
|
2015-03-16 15:47:13 +00:00
|
|
|
foreach ( $targetPages as $title ) {
|
2014-08-07 19:10:37 +00:00
|
|
|
$targetPage = EchoTargetPage::create( $user, $title, $event );
|
|
|
|
if ( $targetPage ) {
|
|
|
|
$targetMapper->insert( $targetPage );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2015-06-16 00:35:05 +00:00
|
|
|
$notifUser = MWEchoNotifUser::newFromUser( $user );
|
2015-08-13 00:54:16 +00:00
|
|
|
$section = $this->event->getSection();
|
2015-06-16 00:35:05 +00:00
|
|
|
|
2014-08-12 20:37:04 +00:00
|
|
|
// Add listener to refresh notification count upon insert
|
|
|
|
$notifMapper->attachListener( 'insert', 'refresh-notif-count',
|
2015-10-01 13:48:52 +00:00
|
|
|
function () use ( $notifUser, $section ) {
|
2015-06-16 00:35:05 +00:00
|
|
|
$notifUser->resetNotificationCount( DB_MASTER );
|
2015-08-13 00:54:16 +00:00
|
|
|
if ( $section === EchoAttributeManager::MESSAGE && !$notifUser->hasMessages() ) {
|
|
|
|
$notifUser->cacheHasMessages();
|
|
|
|
}
|
2014-08-12 20:37:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
$notifMapper->insert( $this );
|
2013-05-24 22:51:47 +00:00
|
|
|
|
2015-06-16 00:35:05 +00:00
|
|
|
if ( $event->getType() === 'edit-user-talk' ) {
|
|
|
|
$notifUser->flagCacheWithNewTalkNotification();
|
|
|
|
}
|
2015-06-01 18:36:35 +00:00
|
|
|
Hooks::run( 'EchoCreateNotificationComplete', array( $this ) );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
2013-03-01 00:26:59 +00:00
|
|
|
|
2015-03-16 15:47:13 +00:00
|
|
|
/**
|
|
|
|
* @param int[]|int|false $targetPageIds
|
|
|
|
* @return Title[]
|
|
|
|
*/
|
|
|
|
protected static function resolveTargetPages( $targetPageIds ) {
|
|
|
|
if ( !$targetPageIds ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
if ( !is_array( $targetPageIds ) ) {
|
|
|
|
$targetPageIds = array( $targetPageIds );
|
|
|
|
}
|
|
|
|
$result = array();
|
|
|
|
foreach ( $targetPageIds as $targetPageId ) {
|
|
|
|
// Make sure the target-page id is a valid id
|
|
|
|
$title = Title::newFromID( $targetPageId );
|
|
|
|
// Try master if there is no match
|
|
|
|
if ( !$title ) {
|
|
|
|
$title = Title::newFromID( $targetPageId, Title::GAID_FOR_UPDATE );
|
|
|
|
}
|
|
|
|
if ( $title ) {
|
|
|
|
$result[] = $title;
|
|
|
|
}
|
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2015-03-16 15:47:13 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Load a notification record from std class
|
|
|
|
* @param stdClass
|
2015-03-16 15:47:13 +00:00
|
|
|
* @param EchoTargetPage[]|null An array of EchoTargetPage instances, or null if not loaded.
|
2014-07-18 03:58:21 +00:00
|
|
|
* @return EchoNotification
|
|
|
|
*/
|
2015-03-16 15:47:13 +00:00
|
|
|
public static function newFromRow( $row, $targetPages = null ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
$notification = new EchoNotification();
|
|
|
|
|
|
|
|
if ( property_exists( $row, 'event_type' ) ) {
|
|
|
|
$notification->event = EchoEvent::newFromRow( $row );
|
|
|
|
} else {
|
|
|
|
$notification->event = EchoEvent::newFromID( $row->notification_event );
|
|
|
|
}
|
2014-08-07 00:55:24 +00:00
|
|
|
|
2015-03-16 15:47:13 +00:00
|
|
|
$notification->targetPages = $targetPages;
|
2014-07-18 03:58:21 +00:00
|
|
|
$notification->user = User::newFromId( $row->notification_user );
|
2014-08-18 22:49:26 +00:00
|
|
|
// Notification timestamp should never be empty
|
|
|
|
$notification->timestamp = wfTimestamp( TS_MW, $row->notification_timestamp );
|
|
|
|
// Only convert to MW format if it is not empty, otherwise
|
|
|
|
// wfTimestamp would use current timestamp for empty cases
|
|
|
|
if ( $row->notification_read_timestamp ) {
|
|
|
|
$notification->readTimestamp = wfTimestamp( TS_MW, $row->notification_read_timestamp );
|
|
|
|
}
|
2014-07-18 03:58:21 +00:00
|
|
|
$notification->bundleBase = $row->notification_bundle_base;
|
|
|
|
$notification->bundleHash = $row->notification_bundle_hash;
|
|
|
|
$notification->bundleDisplayHash = $row->notification_bundle_display_hash;
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-08-07 00:55:24 +00:00
|
|
|
|
|
|
|
/**
|
2015-03-16 15:47:13 +00:00
|
|
|
* Getter method. Returns an array of EchoTargetPages, or null if they have
|
|
|
|
* not been loaded.
|
|
|
|
*
|
|
|
|
* @return EchoTargetPage[]|null
|
2014-08-07 00:55:24 +00:00
|
|
|
*/
|
2015-03-16 15:47:13 +00:00
|
|
|
public function getTargetPages() {
|
|
|
|
return $this->targetPages;
|
2014-08-07 00:55:24 +00:00
|
|
|
}
|
2012-08-30 16:04:39 +00:00
|
|
|
}
|