2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Model;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2022-11-13 06:43:40 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Bundleable;
|
2023-06-06 21:02:02 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Hooks\HookRunner;
|
2022-11-02 20:47:04 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Mapper\NotificationMapper;
|
2023-08-15 20:21:38 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Notifier;
|
2022-11-13 07:48:43 +00:00
|
|
|
use MediaWiki\Extension\Notifications\NotifUser;
|
2020-05-15 16:58:14 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\User;
|
2022-11-02 21:34:17 +00:00
|
|
|
use stdClass;
|
2020-05-15 16:58:14 +00:00
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
class Notification extends AbstractEntity implements Bundleable {
|
2013-03-05 01:31:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/**
|
2022-11-02 21:34:17 +00:00
|
|
|
* @var Event
|
2013-03-05 01:31:41 +00:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2022-11-02 21:34:17 +00:00
|
|
|
* @var TargetPage[]|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;
|
|
|
|
|
|
|
|
/**
|
2022-01-05 19:08:47 +00:00
|
|
|
* @var string|null
|
2013-03-05 01:31:41 +00:00
|
|
|
*/
|
|
|
|
protected $readTimestamp;
|
2012-04-27 15:14:24 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* The hash used to determine if a set of event could be bundled
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $bundleHash = '';
|
|
|
|
|
2016-03-04 19:23:02 +00:00
|
|
|
/**
|
2022-11-02 21:34:17 +00:00
|
|
|
* @var Notification[]
|
2016-03-04 19:23:02 +00:00
|
|
|
*/
|
|
|
|
protected $bundledNotifications;
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
2022-11-02 21:34:17 +00:00
|
|
|
* Creates an Notification object based on event and user
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param array $info The following keys are required:
|
2022-11-02 21:34:17 +00:00
|
|
|
* - 'event' The Event being notified about.
|
2013-03-05 01:31:41 +00:00
|
|
|
* - 'user' The User being notified.
|
2022-11-02 21:34:17 +00:00
|
|
|
* @return Notification
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
2013-03-05 01:31:41 +00:00
|
|
|
public static function create( array $info ) {
|
2022-11-02 21:34:17 +00:00
|
|
|
$obj = new Notification();
|
2016-12-05 18:51:07 +00:00
|
|
|
static $validFields = [ 'event', 'user' ];
|
2012-04-27 15:14:24 +00:00
|
|
|
|
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 {
|
2023-06-07 17:46:16 +00:00
|
|
|
throw new InvalidArgumentException( "Field $field is required" );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 22:39:23 +00:00
|
|
|
if ( !$obj->user instanceof User ) {
|
|
|
|
throw new InvalidArgumentException( 'Invalid user parameter, expected: User object' );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
if ( !$obj->event instanceof Event ) {
|
|
|
|
throw new InvalidArgumentException( 'Invalid event parameter, expected: Event 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
|
|
|
|
2022-11-02 20:47:04 +00:00
|
|
|
$notifMapper = new NotificationMapper();
|
2012-04-27 15:14:24 +00:00
|
|
|
|
2023-04-27 21:12:22 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
2023-06-06 21:02:02 +00:00
|
|
|
$hookRunner = new HookRunner( $services->getHookContainer() );
|
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'] ) ) {
|
2023-08-15 20:21:38 +00:00
|
|
|
Notifier::getBundleRules( $this->event, $bundleKey );
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
2014-08-07 19:10:37 +00:00
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
if ( $bundleKey ) {
|
|
|
|
$hash = md5( $bundleKey );
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->bundleHash = $hash;
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 07:48:43 +00:00
|
|
|
$notifUser = NotifUser::newFromUser( $this->user );
|
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',
|
2021-05-04 16:06:42 +00:00
|
|
|
static function () use ( $notifUser ) {
|
2018-05-25 17:45:49 +00:00
|
|
|
$notifUser->resetNotificationCount();
|
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
|
|
|
|
2016-10-13 18:55:34 +00:00
|
|
|
if ( $this->event->getCategory() === 'edit-user-talk' ) {
|
2023-04-27 21:12:22 +00:00
|
|
|
$services->getTalkPageNotificationManager()
|
2020-05-15 16:58:14 +00:00
|
|
|
->setUserHasNewMessages( $this->user );
|
2015-06-16 00:35:05 +00:00
|
|
|
}
|
2023-06-06 21:02:02 +00:00
|
|
|
$hookRunner->onEchoCreateNotificationComplete( $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
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param stdClass $row
|
2022-11-02 21:34:17 +00:00
|
|
|
* @param TargetPage[]|null $targetPages An array of TargetPage instances, or null if not loaded.
|
|
|
|
* @return Notification|false False if failed to load/unserialize
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
2019-10-23 10:44:35 +00:00
|
|
|
public static function newFromRow( $row, array $targetPages = null ) {
|
2022-11-02 21:34:17 +00:00
|
|
|
$notification = new Notification();
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
if ( property_exists( $row, 'event_type' ) ) {
|
2022-11-02 21:34:17 +00:00
|
|
|
$notification->event = Event::newFromRow( $row );
|
2014-07-18 03:58:21 +00:00
|
|
|
} else {
|
2022-11-02 21:34:17 +00:00
|
|
|
$notification->event = Event::newFromID( $row->notification_event );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
2014-08-07 00:55:24 +00:00
|
|
|
|
2016-05-13 23:39:17 +00:00
|
|
|
if ( $notification->event === false ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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 );
|
2022-01-05 19:08:47 +00:00
|
|
|
$notification->readTimestamp = wfTimestampOrNull( TS_MW, $row->notification_read_timestamp );
|
2014-07-18 03:58:21 +00:00
|
|
|
$notification->bundleHash = $row->notification_bundle_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() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
2014-07-18 03:58:21 +00:00
|
|
|
'notification_event' => $this->event->getId(),
|
|
|
|
'notification_user' => $this->user->getId(),
|
|
|
|
'notification_timestamp' => $this->timestamp,
|
|
|
|
'notification_read_timestamp' => $this->readTimestamp,
|
|
|
|
'notification_bundle_hash' => $this->bundleHash,
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
2013-03-01 00:26:59 +00:00
|
|
|
/**
|
|
|
|
* Getter method
|
2022-11-02 21:34:17 +00:00
|
|
|
* @return Event The event for this notification
|
2013-03-01 00:26:59 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
2016-03-04 19:23:02 +00:00
|
|
|
public function isRead() {
|
|
|
|
return $this->getReadTimestamp() !== null;
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Getter method
|
|
|
|
* @return string|null Notification bundle hash
|
|
|
|
*/
|
|
|
|
public function getBundleHash() {
|
|
|
|
return $this->bundleHash;
|
|
|
|
}
|
|
|
|
|
2014-08-07 00:55:24 +00:00
|
|
|
/**
|
2022-11-02 21:34:17 +00:00
|
|
|
* Getter method. Returns an array of TargetPage's, or null if they have
|
2015-03-16 15:47:13 +00:00
|
|
|
* not been loaded.
|
|
|
|
*
|
2022-11-02 21:34:17 +00:00
|
|
|
* @return TargetPage[]|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
|
|
|
}
|
2016-03-04 19:23:02 +00:00
|
|
|
|
2019-10-23 10:44:35 +00:00
|
|
|
public function setBundledNotifications( array $notifications ) {
|
2016-03-04 19:23:02 +00:00
|
|
|
$this->bundledNotifications = $notifications;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBundledNotifications() {
|
|
|
|
return $this->bundledNotifications;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-09 15:20:55 +00:00
|
|
|
* @inheritDoc
|
2016-03-04 19:23:02 +00:00
|
|
|
*/
|
|
|
|
public function canBeBundled() {
|
|
|
|
return !$this->isRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-09 15:20:55 +00:00
|
|
|
* @inheritDoc
|
2016-03-04 19:23:02 +00:00
|
|
|
*/
|
|
|
|
public function getBundlingKey() {
|
|
|
|
return $this->getBundleHash();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-09 15:20:55 +00:00
|
|
|
* @inheritDoc
|
2016-03-04 19:23:02 +00:00
|
|
|
*/
|
2019-10-23 10:44:35 +00:00
|
|
|
public function setBundledElements( array $bundleables ) {
|
2016-03-04 19:23:02 +00:00
|
|
|
$this->setBundledNotifications( $bundleables );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-09 15:20:55 +00:00
|
|
|
* @inheritDoc
|
2016-03-04 19:23:02 +00:00
|
|
|
*/
|
|
|
|
public function getSortingKey() {
|
2016-07-06 16:48:46 +00:00
|
|
|
return ( $this->isRead() ? '0' : '1' ) . '_' . $this->getTimestamp();
|
2016-03-04 19:23:02 +00:00
|
|
|
}
|
2019-03-02 20:25:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the list of fields that should be selected to create
|
2022-11-02 21:34:17 +00:00
|
|
|
* a new event with Notification::newFromRow
|
2019-03-02 20:25:33 +00:00
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public static function selectFields() {
|
2022-11-02 21:34:17 +00:00
|
|
|
return array_merge( Event::selectFields(), [
|
2019-03-02 20:25:33 +00:00
|
|
|
'notification_event',
|
|
|
|
'notification_user',
|
|
|
|
'notification_timestamp',
|
|
|
|
'notification_read_timestamp',
|
|
|
|
'notification_bundle_hash',
|
|
|
|
] );
|
|
|
|
}
|
2012-08-30 16:04:39 +00:00
|
|
|
}
|
2022-11-02 21:34:17 +00:00
|
|
|
|
|
|
|
class_alias( Notification::class, 'EchoNotification' );
|