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 ) {
|
2019-04-17 12:12:56 +00:00
|
|
|
$dbw->onTransactionCommitOrIdle( $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
|
|
|
|
2019-03-02 20:25:33 +00:00
|
|
|
$row = $db->selectRow( 'echo_event', EchoEvent::selectFields(), [ '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 = [];
|
2019-04-10 20:09:40 +00:00
|
|
|
$seenEventIds = [];
|
2017-09-24 05:23:47 +00:00
|
|
|
$dbr = $this->dbFactory->getEchoDb( DB_REPLICA );
|
2019-04-10 20:09:40 +00:00
|
|
|
|
|
|
|
// From echo_event
|
|
|
|
$res = $dbr->select(
|
|
|
|
[ 'echo_event' ],
|
|
|
|
EchoEvent::selectFields(),
|
|
|
|
[ 'event_page_id' => $pageId ]
|
|
|
|
);
|
|
|
|
if ( $res ) {
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$event = EchoEvent::newFromRow( $row );
|
|
|
|
$events[] = $event;
|
|
|
|
$seenEventIds[] = $event->getId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// From echo_target_page
|
|
|
|
$conds = [ 'etp_page' => $pageId ];
|
|
|
|
if ( $seenEventIds ) {
|
|
|
|
// Some events have both a title and target page(s).
|
|
|
|
// Skip the events that were already found in the echo_event table (the query above).
|
|
|
|
$conds[] = 'event_id NOT IN ( ' . $dbr->makeList( $seenEventIds ) . ' )';
|
|
|
|
}
|
2016-07-27 14:52:18 +00:00
|
|
|
$res = $dbr->select(
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'echo_event', 'echo_target_page' ],
|
2019-03-02 20:25:33 +00:00
|
|
|
EchoEvent::selectFields(),
|
2019-04-10 20:09:40 +00:00
|
|
|
$conds,
|
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 );
|
2019-03-02 20:25:33 +00:00
|
|
|
$fields = array_merge( EchoEvent::selectFields(), [ 'notification_timestamp' ] );
|
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' ],
|
2019-03-02 20:25:33 +00:00
|
|
|
$fields,
|
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;
|
|
|
|
}
|
|
|
|
|
2019-04-23 00:16:21 +00:00
|
|
|
/**
|
|
|
|
* Find out which of the given event IDs are orphaned, and delete them.
|
|
|
|
*
|
|
|
|
* An event is orphaned if it is not referred to by any rows in the echo_notification or
|
|
|
|
* echo_email_batch tables. If $ignoreUserId is set, rows for that user are not considered when
|
|
|
|
* determining orphanhood; if $ignoreUserTable is set, this only applies to that table.
|
|
|
|
* Use this when you've just recently deleted rows related to this user on the master, so that
|
|
|
|
* this function won't refuse to delete recently-orphaned events because it still sees the
|
|
|
|
* recently-deleted rows on the replica.
|
|
|
|
*
|
|
|
|
* @param array $eventIds Event IDs to check to see if they have become orphaned
|
|
|
|
* @param int|null $ignoreUserId Allow events to be deleted if the only referring rows
|
|
|
|
* have this user ID
|
|
|
|
* @param string|null $ignoreUserTable Restrict $ignoreUserId to this table only
|
|
|
|
* ('echo_notification' or 'echo_email_batch')
|
|
|
|
*/
|
|
|
|
public function deleteOrphanedEvents( array $eventIds, $ignoreUserId = null, $ignoreUserTable = null ) {
|
|
|
|
$dbw = $this->dbFactory->getEchoDb( DB_MASTER );
|
|
|
|
$dbr = $this->dbFactory->getEchoDb( DB_REPLICA );
|
|
|
|
|
|
|
|
$notifJoinConds = [];
|
|
|
|
$emailJoinConds = [];
|
|
|
|
if ( $ignoreUserId !== null ) {
|
|
|
|
if ( $ignoreUserTable === null || $ignoreUserTable === 'echo_notification' ) {
|
|
|
|
$notifJoinConds[] = 'notification_user != ' . $dbr->addQuotes( $ignoreUserId );
|
|
|
|
}
|
|
|
|
if ( $ignoreUserTable === null || $ignoreUserTable === 'echo_email_batch' ) {
|
|
|
|
$emailJoinConds[] = 'eeb_user_id != ' . $dbr->addQuotes( $ignoreUserId );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$orphanedEventIds = $dbr->selectFieldValues(
|
|
|
|
[ 'echo_event', 'echo_notification', 'echo_email_batch' ],
|
|
|
|
'event_id',
|
|
|
|
[
|
|
|
|
'event_id' => $eventIds,
|
|
|
|
'notification_timestamp' => null,
|
|
|
|
'eeb_user_id' => null
|
|
|
|
],
|
|
|
|
__METHOD__,
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'echo_notification' => [ 'LEFT JOIN', array_merge( [
|
|
|
|
'notification_event=event_id'
|
|
|
|
], $notifJoinConds ) ],
|
|
|
|
'echo_email_batch' => [ 'LEFT JOIN', array_merge( [
|
|
|
|
'eeb_event_id=event_id'
|
|
|
|
], $emailJoinConds ) ]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
if ( $orphanedEventIds ) {
|
|
|
|
$dbw->delete( 'echo_event', [ 'event_id' => $orphanedEventIds ] );
|
|
|
|
$dbw->delete( 'echo_target_page', [ 'etp_event' => $orphanedEventIds ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|