From ce78c973f33e7200d0776914aa51c7beabb848ef Mon Sep 17 00:00:00 2001 From: bsitu Date: Tue, 7 May 2013 16:27:46 -0700 Subject: [PATCH] Add new EventLogging schema: EchoMail along with code refactor Change-Id: I7f9ec99d27b69abb14aa89540e424f1e0bcd8160 --- Echo.php | 10 ++++- Hooks.php | 13 ------ Notifier.php | 56 ++--------------------- includes/EmailBatch.php | 3 ++ includes/EmailBundler.php | 1 + includes/EventLogging.php | 94 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 109 insertions(+), 68 deletions(-) create mode 100644 includes/EventLogging.php diff --git a/Echo.php b/Echo.php index 99489689b..56fec5675 100644 --- a/Echo.php +++ b/Echo.php @@ -51,6 +51,7 @@ $wgAutoloadClasses['MWEchoEmailBatch'] = $dir . 'includes/EmailBatch.php'; $wgAutoloadClasses['MWDbEchoEmailBatch'] = $dir . 'includes/DbEmailBatch.php'; $wgAutoloadClasses['MWEchoEmailBundler'] = $dir . 'includes/EmailBundler.php'; $wgAutoloadClasses['MWDbEchoEmailBundler'] = $dir . 'includes/DbEmailBundler.php'; +$wgAutoloadClasses['MWEchoEventLogging'] = $dir . 'includes/EventLogging.php'; // Formatters $wgAutoloadClasses['EchoNotificationFormatter'] = $dir . 'formatters/NotificationFormatter.php'; @@ -473,11 +474,16 @@ $wgDefaultUserOptions['echo-subscriptions-web-article-linked'] = false; // Echo Configuration for EventLogging $wgEchoConfig = array( - 'version' => '1.1', + 'version' => '1.2', + // default all eventlogging off, overwrite them in site configuration 'eventlogging' => array ( 'Echo' => array ( 'enabled' => false, 'revision' => 5423520 - ) + ), + 'EchoMail' => array ( + 'enabled' => false, + 'revision' => 5467650 + ), ) ); diff --git a/Hooks.php b/Hooks.php index 0f65134a0..efabd3281 100644 --- a/Hooks.php +++ b/Hooks.php @@ -49,19 +49,6 @@ class EchoHooks { return true; } - /** - * Attempt to log the event - * @param $schema string - * @param $data array - */ - public static function logEvent( $schema, $data ) { - global $wgEchoConfig; - - if ( !empty( $wgEchoConfig['eventlogging'][$schema]['enabled'] ) ) { - efLogServerSideEvent( $schema, $wgEchoConfig['eventlogging'][$schema]['revision'], $data ); - } - } - /** * @param $updater DatabaseUpdater object * @return bool true in all cases diff --git a/Notifier.php b/Notifier.php index 55c3d9b9b..c6b45d710 100644 --- a/Notifier.php +++ b/Notifier.php @@ -21,62 +21,11 @@ class EchoNotifier { EchoNotification::create( array( 'user' => $user, 'event' => $event ) ); - self::logEvent( $user, $event, 'web' ); + MWEchoEventLogging::logSchemaEcho( $user, $event, 'web' ); EchoNotificationController::resetNotificationCount( $user, DB_MASTER ); } - /** - * Store Event Logging data for web or email notifications - * - * @param $user User being notified. - * @param $event EchoEvent to log detail about. - * @param $deliveryMethod string containing either 'web' or 'email' - */ - public static function logEvent( $user, $event, $deliveryMethod ) { - global $wgEchoConfig, $wgEchoNotifications; - if ( !$wgEchoConfig['eventlogging']['Echo']['enabled'] ) { - // Only attempt event logging if Echo schema is enabled - return; - } - - $agent = $event->getAgent(); - // Typically an event should always have an agent, but agent could be - // null if the data is corrupted - if ( $agent ) { - $sender = $agent->isAnon() ? $agent->getName() : $agent->getId(); - } else { - $sender = -1; - } - - if ( isset( $wgEchoNotifications[$event->getType()]['group'] ) ) { - $group = $wgEchoNotifications[$event->getType()]['group']; - } else { - $group = 'neutral'; - } - $data = array ( - 'version' => $wgEchoConfig['version'], - 'eventId' => $event->getId(), - 'notificationType' => $event->getType(), - 'notificationGroup' => $group, - 'sender' => (string)$sender, - 'recipientUserId' => $user->getId(), - 'recipientEditCount' => (int)$user->getEditCount() - ); - // Add the source if it exists. (This is mostly for the Thanks extension.) - $extra = $event->getExtra(); - if ( isset( $extra['source'] ) ) { - $data['eventSource'] = (string)$extra['source']; - } - if( $deliveryMethod == 'email' ) { - $data['deliveryMethod'] = 'email'; - } else { - // whitelist valid delivery methods so it is always valid - $data['deliveryMethod'] = 'web'; - } - EchoHooks::logEvent( 'Echo', $data ); - } - /** * Send a Notification to a user by email * @@ -112,7 +61,7 @@ class EchoNotifier { $bundleHash = md5( $bundleString ); } - self::logEvent( $user, $event, 'email' ); + MWEchoEventLogging::logSchemaEcho( $user, $event, 'email' ); // email digest notification ( weekly or daily ) if ( $wgEchoEnableEmailBatch && $user->getOption( 'echo-email-frequency' ) > 0 ) { @@ -149,6 +98,7 @@ class EchoNotifier { $body = $email['body']; UserMailer::send( $toAddress, $fromAddress, $subject, $body, $replyAddress ); + MWEchoEventLogging::logSchemaEchoMail( $user, 'single' ); } } diff --git a/includes/EmailBatch.php b/includes/EmailBatch.php index f238b22d4..69a7fb2ee 100644 --- a/includes/EmailBatch.php +++ b/includes/EmailBatch.php @@ -226,8 +226,10 @@ abstract class MWEchoEmailBatch { // @Todo - replace them with the CONSTANT in 33810 once it is merged if ( $this->mUser->getOption( 'echo-email-frequency' ) == 7 ) { $frequency = 'weekly'; + $emailDeliveryMode = 'weekly_digest'; } else { $frequency = 'daily'; + $emailDeliveryMode = 'daily_digest'; } // email subject @@ -251,6 +253,7 @@ abstract class MWEchoEmailBatch { // @Todo Push the email to job queue or just send it out directly? UserMailer::send( $toAddress, $fromAddress, $subject, $body, $replyAddress ); + MWEchoEventLogging::logSchemaEchoMail( $this->mUser, $emailDeliveryMode ); } /** diff --git a/includes/EmailBundler.php b/includes/EmailBundler.php index 89d36dd65..103c05c10 100644 --- a/includes/EmailBundler.php +++ b/includes/EmailBundler.php @@ -245,6 +245,7 @@ abstract class MWEchoEmailBundler { // Schedule a email job or just send the email directly? UserMailer::send( $toAddress, $fromAddress, $content['subject'], $content['body'], $replyAddress ); + MWEchoEventLogging::logSchemaEchoMail( $this->mUser, 'bundle' ); } /** diff --git a/includes/EventLogging.php b/includes/EventLogging.php new file mode 100644 index 000000000..0f9b3496d --- /dev/null +++ b/includes/EventLogging.php @@ -0,0 +1,94 @@ +getAgent(); + // Typically an event should always have an agent, but agent could be + // null if the data is corrupted + if ( $agent ) { + $sender = $agent->isAnon() ? $agent->getName() : $agent->getId(); + } else { + $sender = -1; + } + + if ( isset( $wgEchoNotifications[$event->getType()]['group'] ) ) { + $group = $wgEchoNotifications[$event->getType()]['group']; + } else { + $group = 'neutral'; + } + $data = array ( + 'version' => $wgEchoConfig['version'], + 'eventId' => $event->getId(), + 'notificationType' => $event->getType(), + 'notificationGroup' => $group, + 'sender' => (string)$sender, + 'recipientUserId' => $user->getId(), + 'recipientEditCount' => (int)$user->getEditCount() + ); + // Add the source if it exists. (This is mostly for the Thanks extension.) + $extra = $event->getExtra(); + if ( isset( $extra['source'] ) ) { + $data['eventSource'] = (string)$extra['source']; + } + if( $deliveryMethod == 'email' ) { + $data['deliveryMethod'] = 'email'; + } else { + // whitelist valid delivery methods so it is always valid + $data['deliveryMethod'] = 'web'; + } + + self::actuallyLogTheEvent( 'Echo', $data ); + } + + /** + * Functions for logging the event for Schema:EchoEmail + * @param $user User + * @param $emailDeliveryMode string + */ + public static function logSchemaEchoMail( $user, $emailDeliveryMode = 'single' ) { + global $wgEchoConfig; + + if ( !$wgEchoConfig['eventlogging']['EchoMail']['enabled'] ) { + // Only attempt event logging if EchoMail schema is enabled + return; + } + + $data = array ( + 'version' => $wgEchoConfig['version'], + 'recipientUserId' => $user->getId(), + 'emailDeliveryMode' => $emailDeliveryMode + ); + + self::actuallyLogTheEvent( 'EchoMail', $data ); + } + +}