示例#1
0
 /**
  * Send a Notification to a user by email
  *
  * @param $user User to notify.
  * @param $event EchoEvent to notify about.
  * @return bool
  */
 public static function notifyWithEmail($user, $event)
 {
     // No valid email address or email notification
     if (!$user->isEmailConfirmed() || $user->getOption('echo-email-frequency') < 0) {
         return false;
     }
     // Final check on whether to send email for this user & event
     if (!wfRunHooks('EchoAbortEmailNotification', array($user, $event))) {
         return false;
     }
     // See if the user wants to receive emails for this category or the user is eligible to receive this email
     if (in_array($event->getType(), EchoNotificationController::getUserEnabledEvents($user, 'email'))) {
         global $wgEchoEnableEmailBatch, $wgEchoNotifications, $wgNotificationSender, $wgNotificationSenderName, $wgNotificationReplyName, $wgEchoBundleEmailInterval;
         $priority = EchoNotificationController::getNotificationPriority($event->getType());
         $bundleString = $bundleHash = '';
         // We should have bundling for email digest as long as either web or email bundling is on, for example, talk page
         // email bundling is off, but if a user decides to receive email digest, we should bundle those messages
         if (!empty($wgEchoNotifications[$event->getType()]['bundle']['web']) || !empty($wgEchoNotifications[$event->getType()]['bundle']['email'])) {
             wfRunHooks('EchoGetBundleRules', array($event, &$bundleString));
         }
         if ($bundleString) {
             $bundleHash = md5($bundleString);
         }
         MWEchoEventLogging::logSchemaEcho($user, $event, 'email');
         // email digest notification ( weekly or daily )
         if ($wgEchoEnableEmailBatch && $user->getOption('echo-email-frequency') > 0) {
             // always create a unique event hash for those events don't support bundling
             // this is mainly for group by
             if (!$bundleHash) {
                 $bundleHash = md5($event->getType() . '-' . $event->getId());
             }
             MWEchoEmailBatch::addToQueue($user->getId(), $event->getId(), $priority, $bundleHash);
             return true;
         }
         $addedToQueue = false;
         // only send bundle email if email bundling is on
         if ($wgEchoBundleEmailInterval && $bundleHash && !empty($wgEchoNotifications[$event->getType()]['bundle']['email'])) {
             $bundler = MWEchoEmailBundler::newFromUserHash($user, $bundleHash);
             if ($bundler) {
                 $addedToQueue = $bundler->addToEmailBatch($event->getId(), $priority);
             }
         }
         // send single notification if the email wasn't added to queue for bundling
         if (!$addedToQueue) {
             // instant email notification
             $toAddress = new MailAddress($user);
             $fromAddress = new MailAddress($wgNotificationSender, $wgNotificationSenderName);
             $replyAddress = new MailAddress($wgNotificationSender, $wgNotificationReplyName);
             // Since we are sending a single email, should set the bundle hash to null
             // if it is set with a value from somewhere else
             $event->setBundleHash(null);
             $email = EchoNotificationController::formatNotification($event, $user, 'email', 'email');
             $subject = $email['subject'];
             $body = $email['body'];
             UserMailer::send($toAddress, $fromAddress, $subject, $body, $replyAddress);
             MWEchoEventLogging::logSchemaEchoMail($user, 'single');
         }
     }
     return true;
 }
 public function setUp()
 {
     parent::setUp();
     global $wgEchoNotifications;
     $this->setMwGlobals('wgAllowHTMLEmail', true);
     $event = $this->mockEvent('edit-user-talk');
     $event->expects($this->any())->method('getTitle')->will($this->returnValue(Title::newMainPage()));
     $formatter = EchoNotificationFormatter::factory($wgEchoNotifications[$event->getType()]);
     $formatter->setOutputFormat('email');
     $user = User::newFromId(1);
     $user->setName('Test');
     $user->setOption('echo-email-format', EchoHooks::EMAIL_FORMAT_HTML);
     $this->emailSingle = new EchoEmailSingle($formatter, $event, $user);
     $content[$event->getCategory()][] = EchoNotificationController::formatNotification($event, $user, 'email', 'emaildigest');
     $this->emailDigest = new EchoEmailDigest(User::newFromId(2), $content);
 }
 protected function format(EchoEvent $event, $format, $user = false, $type = 'web')
 {
     global $wgEchoNotifications;
     if ($user === false) {
         $user = User::newFromName('Notification-formatter-test');
     }
     // Notification users can not be anonymous, use a fake user id
     return EchoNotificationController::formatNotification($event, $user, $format, $type);
 }
 /**
  * Get a list of notifications based on the passed parameters
  *
  * @param $user User the user to get notifications for
  * @param $format string/bool false to not format any notifications, string to a specific output format
  * @param $limit int The maximum number of notifications to return
  * @param $continue string Used for offset
  * @return array
  */
 public static function getNotifications($user, $format = false, $limit = 20, $continue = null)
 {
     global $wgEchoBackend;
     $output = array();
     // TODO: Make 'web' based on a new API param?
     $res = $wgEchoBackend->loadNotifications($user, $limit, $continue, 'web');
     foreach ($res as $row) {
         $event = EchoEvent::newFromRow($row);
         if ($row->notification_bundle_base && $row->notification_bundle_display_hash) {
             $event->setBundleHash($row->notification_bundle_display_hash);
         }
         $timestampMw = self::getUserLocalTime($user, $row->notification_timestamp);
         // Start creating date section header
         $now = wfTimestamp();
         $dateFormat = substr($timestampMw, 0, 8);
         if (substr(self::getUserLocalTime($user, $now), 0, 8) === $dateFormat) {
             // 'Today'
             $date = wfMessage('echo-date-today')->escaped();
         } elseif (substr(self::getUserLocalTime($user, $now - 86400), 0, 8) === $dateFormat) {
             // 'Yesterday'
             $date = wfMessage('echo-date-yesterday')->escaped();
         } else {
             // 'May 10' or '10 May' (depending on user's date format preference)
             $lang = RequestContext::getMain()->getLanguage();
             $dateFormat = $lang->getDateFormatString('pretty', $user->getDatePreference() ?: 'default');
             $date = $lang->sprintfDate($dateFormat, $timestampMw);
         }
         // End creating date section header
         $thisEvent = array('id' => $event->getId(), 'type' => $event->getType(), 'category' => $event->getCategory(), 'timestamp' => array('utcunix' => wfTimestamp(TS_UNIX, $row->notification_timestamp), 'unix' => self::getUserLocalTime($user, $row->notification_timestamp, TS_UNIX), 'mw' => $timestampMw, 'date' => $date));
         if ($event->getVariant()) {
             $thisEvent['variant'] = $event->getVariant();
         }
         if ($event->getTitle()) {
             $thisEvent['title'] = array('full' => $event->getTitle()->getPrefixedText(), 'namespace' => $event->getTitle()->getNSText(), 'namespace-key' => $event->getTitle()->getNamespace(), 'text' => $event->getTitle()->getText());
         }
         if ($event->getAgent()) {
             if ($event->userCan(Revision::DELETED_USER, $user)) {
                 $thisEvent['agent'] = array('id' => $event->getAgent()->getId(), 'name' => $event->getAgent()->getName());
             } else {
                 $thisEvent['agent'] = array('userhidden' => '');
             }
         }
         //if ( $row->notification_read_timestamp ) {
         $thisEvent['read'] = $row->notification_read_timestamp;
         //}
         if ($format) {
             $thisEvent['*'] = EchoNotificationController::formatNotification($event, $user, $format);
         }
         $output[$event->getID()] = $thisEvent;
     }
     return $output;
 }
示例#5
0
 /**
  * Generate the content for bundle email
  * @return string
  */
 protected function generateEmailContent()
 {
     if (!$this->baseEvent) {
         return '';
     }
     $this->baseEvent->setBundleHash($this->bundleHash);
     return EchoNotificationController::formatNotification($this->baseEvent, $this->mUser, 'email', 'email');
 }
示例#6
0
 /**
  * Add individual event template to the big email content
  */
 protected function appendContent($event, $hash)
 {
     // get the category for this event
     $category = $event->getCategory();
     $event->setBundleHash($hash);
     $email = EchoNotificationController::formatNotification($event, $this->mUser, 'email', 'emaildigest');
     if (!isset($this->content[$category])) {
         $this->content[$category] = array();
     }
     $this->content[$category][] = $email;
 }