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
|
|
|
|
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 );
|
|
|
|
|
|
|
|
$row = $db->selectRow( 'echo_event', '*', array( 'event_id' => $id ), __METHOD__ );
|
|
|
|
|
|
|
|
if ( !$row && !$fromMaster ) {
|
|
|
|
return $this->fetchById( $id, true );
|
|
|
|
} elseif ( !$row ) {
|
|
|
|
throw new MWException( "No EchoEvent found with ID: $id" );
|
|
|
|
}
|
|
|
|
|
|
|
|
return EchoEvent::newFromRow( $row );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of echo events identified by user and bundle hash
|
|
|
|
*
|
|
|
|
* @param $user User
|
|
|
|
* @param $bundleHash string the bundle hash
|
2015-10-29 21:14:35 +00:00
|
|
|
* @param $distributionType string distribution medium: 'web' or 'email'
|
2014-07-18 03:58:21 +00:00
|
|
|
* @param $order string 'ASC'/'DESC'
|
|
|
|
* @param $limit int
|
2015-11-19 18:00:29 +00:00
|
|
|
* @return EchoEvent[]
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
2015-10-29 21:14:35 +00:00
|
|
|
public function fetchByUserBundleHash( User $user, $bundleHash, $distributionType = 'web', $order = 'DESC', $limit = 250 ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
$dbr = $this->dbFactory->getEchoDb( DB_SLAVE );
|
|
|
|
|
|
|
|
// We only display 99+ if the number is over 100, we can do limit 250, this should
|
|
|
|
// be sufficient to return 99 distinct group iterators, avoid select count( distinct )
|
|
|
|
// for the following reason:
|
|
|
|
// 1. it will not scale for large volume data
|
|
|
|
// 2. notification may have random grouping iterator
|
|
|
|
// 3. agent may be anonymous, can't do distinct over two columns: event_agent_id and event_agent_ip
|
2015-10-29 21:14:35 +00:00
|
|
|
if ( $distributionType == 'web' ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
$res = $dbr->select(
|
|
|
|
array( 'echo_notification', 'echo_event' ),
|
2015-08-22 08:03:17 +00:00
|
|
|
array( 'event_agent_id', 'event_agent_ip', 'event_extra',
|
2015-08-26 20:06:34 +00:00
|
|
|
'event_id', 'event_page_id', 'event_type', 'event_variant',
|
|
|
|
'notification_timestamp' ),
|
2014-07-18 03:58:21 +00:00
|
|
|
array(
|
|
|
|
'notification_event=event_id',
|
|
|
|
'notification_user' => $user->getId(),
|
|
|
|
'notification_bundle_base' => 0,
|
|
|
|
'notification_bundle_display_hash' => $bundleHash
|
|
|
|
),
|
|
|
|
__METHOD__,
|
|
|
|
array( 'ORDER BY' => 'notification_timestamp ' . $order, 'LIMIT' => $limit )
|
|
|
|
);
|
|
|
|
// this would be email for now
|
|
|
|
} else {
|
|
|
|
$res = $dbr->select(
|
|
|
|
array( 'echo_email_batch', 'echo_event' ),
|
2015-08-22 08:03:17 +00:00
|
|
|
array( 'event_agent_id', 'event_agent_ip', 'event_extra',
|
2015-08-26 13:28:34 +00:00
|
|
|
'event_id', 'event_page_id', 'event_type', 'event_variant' ),
|
2014-07-18 03:58:21 +00:00
|
|
|
array(
|
|
|
|
'eeb_event_id=event_id',
|
|
|
|
'eeb_user_id' => $user->getId(),
|
|
|
|
'eeb_event_hash' => $bundleHash
|
|
|
|
),
|
|
|
|
__METHOD__,
|
|
|
|
array( 'ORDER BY' => 'eeb_event_id ' . $order, 'LIMIT' => $limit )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-19 18:00:29 +00:00
|
|
|
$data = array();
|
|
|
|
foreach ( $res as $row ) {
|
2016-05-13 23:39:17 +00:00
|
|
|
$event = EchoEvent::newFromRow( $row );
|
|
|
|
if ( $event ) {
|
|
|
|
$data[] = $event;
|
|
|
|
}
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
2015-11-19 18:00:29 +00:00
|
|
|
|
|
|
|
return $data;
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|