2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* @param EchoEvent
|
|
|
|
* @return int|bool
|
|
|
|
*/
|
|
|
|
public function insert( EchoEvent $event ) {
|
|
|
|
$dbw = $this->dbFactory->getEchoDb( DB_MASTER );
|
|
|
|
|
|
|
|
$id = $dbw->nextSequenceValue( 'echo_event_id' );
|
|
|
|
|
|
|
|
$row = $event->toDbArray();
|
|
|
|
if ( $id ) {
|
|
|
|
$row['event_id'] = $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = $dbw->insert( 'echo_event', $row, __METHOD__ );
|
|
|
|
|
|
|
|
if ( $res ) {
|
|
|
|
if ( !$id ) {
|
|
|
|
$id = $dbw->insertId();
|
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2016-09-07 17:38:12 +00:00
|
|
|
$listeners = $this->getMethodListeners( __FUNCTION__ );
|
|
|
|
foreach ( $listeners as $listener ) {
|
|
|
|
$dbw->onTransactionIdle( $listener );
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $id;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an EchoEvent by id
|
|
|
|
*
|
|
|
|
* @param int
|
|
|
|
* @param boolean
|
2016-05-13 23:39:17 +00:00
|
|
|
* @return EchoEvent|bool false if it wouldn't load/unserialize
|
2014-07-18 03:58:21 +00:00
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function fetchById( $id, $fromMaster = false ) {
|
|
|
|
$db = $fromMaster ? $this->dbFactory->getEchoDb( DB_MASTER ) : $this->dbFactory->getEchoDb( DB_SLAVE );
|
|
|
|
|
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
|
|
|
|
|
|
|
if ( !$row && !$fromMaster ) {
|
|
|
|
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
|
|
|
|
* @return bool|ResultWrapper
|
|
|
|
*/
|
|
|
|
public function toggleDeleted( $eventIds, $deleted ) {
|
|
|
|
$dbw = $this->dbFactory->getEchoDb( DB_MASTER );
|
|
|
|
|
|
|
|
$selectDeleted = $deleted ? 0 : 1;
|
|
|
|
$setDeleted = $deleted ? 1 : 0;
|
|
|
|
$res = $dbw->update(
|
|
|
|
'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__
|
|
|
|
);
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
$dbr = $this->dbFactory->getEchoDb( DB_SLAVE );
|
|
|
|
$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 ) {
|
|
|
|
$dbr = $this->dbFactory->getEchoDb( DB_SLAVE );
|
|
|
|
|
|
|
|
$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
|
|
|
}
|