2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
2018-04-05 22:19:36 +00:00
|
|
|
use Wikimedia\Rdbms\IResultWrapper;
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Database mapper for EchoEvent model, which is an immutable class, there should
|
|
|
|
* not be any update to it
|
|
|
|
*/
|
2014-08-14 18:46:26 +00:00
|
|
|
class EchoEventMapper extends EchoAbstractMapper {
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert an event record
|
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param EchoEvent $event
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return int|false
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
|
|
|
public function insert( EchoEvent $event ) {
|
|
|
|
$dbw = $this->dbFactory->getEchoDb( DB_MASTER );
|
|
|
|
|
|
|
|
$row = $event->toDbArray();
|
|
|
|
|
2018-10-26 21:36:39 +00:00
|
|
|
$dbw->insert( 'echo_event', $row, __METHOD__ );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2018-10-26 21:36:39 +00:00
|
|
|
$id = $dbw->insertId();
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2018-10-26 21:36:39 +00:00
|
|
|
$listeners = $this->getMethodListeners( __FUNCTION__ );
|
|
|
|
foreach ( $listeners as $listener ) {
|
|
|
|
$dbw->onTransactionIdle( $listener );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
2018-10-26 21:36:39 +00:00
|
|
|
|
|
|
|
return $id;
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an EchoEvent by id
|
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param int $id
|
|
|
|
* @param bool $fromMaster
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return EchoEvent|false False if it wouldn't load/unserialize
|
2014-07-18 03:58:21 +00:00
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function fetchById( $id, $fromMaster = false ) {
|
2017-09-24 05:23:47 +00:00
|
|
|
$db = $fromMaster ? $this->dbFactory->getEchoDb( DB_MASTER ) : $this->dbFactory->getEchoDb( DB_REPLICA );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$row = $db->selectRow( 'echo_event', '*', [ 'event_id' => $id ], __METHOD__ );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2017-05-05 00:49:33 +00:00
|
|
|
// If the row was not found, fall back on the master if it makes sense to do so
|
|
|
|
if ( !$row && !$fromMaster && $this->dbFactory->canRetryMaster() ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
return $this->fetchById( $id, true );
|
|
|
|
} elseif ( !$row ) {
|
|
|
|
throw new MWException( "No EchoEvent found with ID: $id" );
|
|
|
|
}
|
|
|
|
|
|
|
|
return EchoEvent::newFromRow( $row );
|
|
|
|
}
|
|
|
|
|
2016-03-04 19:23:02 +00:00
|
|
|
/**
|
|
|
|
* @param int[] $eventIds
|
|
|
|
* @param bool $deleted
|
2018-04-05 22:19:36 +00:00
|
|
|
* @return bool|IResultWrapper
|
2016-03-04 19:23:02 +00:00
|
|
|
*/
|
|
|
|
public function toggleDeleted( $eventIds, $deleted ) {
|
|
|
|
$dbw = $this->dbFactory->getEchoDb( DB_MASTER );
|
|
|
|
|
|
|
|
$selectDeleted = $deleted ? 0 : 1;
|
|
|
|
$setDeleted = $deleted ? 1 : 0;
|
2018-10-26 19:46:58 +00:00
|
|
|
$dbw->update(
|
2016-03-04 19:23:02 +00:00
|
|
|
'echo_event',
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2016-03-04 19:23:02 +00:00
|
|
|
'event_deleted' => $setDeleted,
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
[
|
2016-03-04 19:23:02 +00:00
|
|
|
'event_deleted' => $selectDeleted,
|
|
|
|
'event_id' => $eventIds,
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2016-03-04 19:23:02 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
|
2018-10-26 19:46:58 +00:00
|
|
|
return true;
|
2016-03-04 19:23:02 +00:00
|
|
|
}
|
|
|
|
|
2016-07-27 14:52:18 +00:00
|
|
|
/**
|
|
|
|
* Fetch events associated with a page
|
|
|
|
*
|
|
|
|
* @param int $pageId
|
|
|
|
* @return EchoEvent[] Events
|
|
|
|
*/
|
|
|
|
public function fetchByPage( $pageId ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$events = [];
|
2016-07-27 14:52:18 +00:00
|
|
|
|
2017-09-24 05:23:47 +00:00
|
|
|
$dbr = $this->dbFactory->getEchoDb( DB_REPLICA );
|
2016-07-27 14:52:18 +00:00
|
|
|
$res = $dbr->select(
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'echo_event', 'echo_target_page' ],
|
|
|
|
[ '*' ],
|
|
|
|
[
|
2016-07-27 14:52:18 +00:00
|
|
|
'etp_page' => $pageId
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2016-07-27 14:52:18 +00:00
|
|
|
__METHOD__,
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'GROUP BY' => 'etp_event' ],
|
|
|
|
[ 'echo_target_page' => [ 'INNER JOIN', 'event_id=etp_event' ] ]
|
2016-07-27 14:52:18 +00:00
|
|
|
);
|
|
|
|
if ( $res ) {
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$events[] = EchoEvent::newFromRow( $row );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $events;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch event IDs associated with a page
|
|
|
|
*
|
|
|
|
* @param int $pageId
|
|
|
|
* @return int[] Event IDs
|
|
|
|
*/
|
|
|
|
public function fetchIdsByPage( $pageId ) {
|
|
|
|
$events = $this->fetchByPage( $pageId );
|
|
|
|
$eventIds = array_map(
|
|
|
|
function ( EchoEvent $event ) {
|
|
|
|
return $event->getId();
|
|
|
|
},
|
|
|
|
$events
|
|
|
|
);
|
|
|
|
return $eventIds;
|
|
|
|
}
|
|
|
|
|
2016-03-04 19:23:02 +00:00
|
|
|
/**
|
|
|
|
* Fetch events unread by a user and associated with a page
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @param int $pageId
|
|
|
|
* @return EchoEvent[]
|
|
|
|
*/
|
|
|
|
public function fetchUnreadByUserAndPage( User $user, $pageId ) {
|
2017-09-24 05:23:47 +00:00
|
|
|
$dbr = $this->dbFactory->getEchoDb( DB_REPLICA );
|
2016-03-04 19:23:02 +00:00
|
|
|
|
|
|
|
$res = $dbr->select(
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'echo_event', 'echo_notification', 'echo_target_page' ],
|
2016-03-04 19:23:02 +00:00
|
|
|
'*',
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2016-03-04 19:23:02 +00:00
|
|
|
'event_deleted' => 0,
|
2016-09-07 17:38:12 +00:00
|
|
|
'notification_user' => $user->getId(),
|
2016-03-04 19:23:02 +00:00
|
|
|
'notification_read_timestamp' => null,
|
2016-09-07 17:38:12 +00:00
|
|
|
'etp_page' => $pageId,
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2016-03-04 19:23:02 +00:00
|
|
|
__METHOD__,
|
|
|
|
null,
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
|
|
|
'echo_target_page' => [ 'INNER JOIN', 'etp_event=event_id' ],
|
|
|
|
'echo_notification' => [ 'INNER JOIN', [ 'notification_event=event_id' ] ],
|
|
|
|
]
|
2016-03-04 19:23:02 +00:00
|
|
|
);
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$data = [];
|
2016-03-04 19:23:02 +00:00
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$data[] = EchoEvent::newFromRow( $row );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|