/** * Function for logging the event for Schema:Echo * @param $user User being notified. * @param $event EchoEvent to log detail about. * @param $deliveryMethod string containing either 'web' or 'email' */ public static function logSchemaEcho(User $user, EchoEvent $event, $deliveryMethod) { global $wgEchoConfig, $wgEchoNotifications; if (!$wgEchoConfig['eventlogging']['Echo']['enabled']) { // Only attempt event logging if Echo schema is enabled return; } // Notifications under system category should have -1 as sender id if ($event->getCategory() === 'system') { $sender = -1; } else { $agent = $event->getAgent(); 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'; } // Add revision ID if it exists $rev = $event->getRevision(); if ($rev) { $data['revisionId'] = $rev->getId(); } self::actuallyLogTheEvent('Echo', $data); }
/** * Get subscribers for the echo notifications * @param EchoEvent $event * @param type $users * @return boolean */ public function onEchoGetDefaultNotifiedUsers($event, &$users) { $aTmpUsers = array_unique(array_merge(BsConfig::getUsersForVar($this->aEchoPrefix['web'] . $event->getType() . '-cat', '1', false, false), BsConfig::getUsersForVar($this->aEchoPrefix['email'] . $event->getType() . '-cat', '1', false, false))); foreach ($aTmpUsers as $index => $user) { if (!$user->getOption('MW::Notifications::Active', false)) { continue; } if ($event->getTitle() instanceof Title) { if (!$event->getTitle()->userCan('read', $user)) { continue; } if (is_array($user->getOption('MW::Notifications::NotifyNS', array()))) { if (!in_array($event->getTitle()->getNamespace(), $user->getOption('MW::Notifications::NotifyNS', array()))) { continue; } } } if ($event->getAgent() instanceof User) { if ($event->getAgent()->getRequest()->getVal('wpMinoredit', false) && $user->getOption('MW::Notifications::NotifyNoMinor', false)) { continue; } } $users[] = $user; } return true; }
/** * Implements per-user whitelist sourced from a user wiki page * * @param $event EchoEvent The event to test for inclusion in whitelist * @param $user User The user that owns the whitelist * @return boolean True when the event agent is in the user whitelist */ protected static function isWhitelistedByUser(EchoEvent $event, User $user) { global $wgEchoPerUserWhitelistFormat, $wgMemc; if ($wgEchoPerUserWhitelistFormat === null || !$event->getAgent()) { return false; } $userId = $user->getID(); if ($userId === 0) { return false; // anonymous user } if (!isset(self::$userWhitelist[$userId])) { self::$userWhitelist[$userId] = new EchoContainmentSet(); self::$userWhitelist[$userId]->addOnWiki(NS_USER, sprintf($wgEchoPerUserWhitelistFormat, $user->getName()), $wgMemc, wfMemcKey("echo_on_wiki_whitelist_" . $userId)); } return self::$userWhitelist[$userId]->contains($event->getAgent()->getName()); }
/** * Creates a link to the user page (user given by event) * @param EchoEvent $event * @param Message $message * @param type $props * @return type */ public function setUserpageLink($event, $message, $props = array()) { if (isset($props['created']) && $props['created']) { unset($props['created']); $aExtra = $event->getExtra(); $oUser = User::newFromName($aExtra['user']); if (is_object($oUser)) { $title = $oUser->getUserPage(); } else { $title = null; } } else { $title = $event->getAgent()->getUserPage(); } if ($title === null) { $message->params("'''" . wfMessage('bs-echo-unknown-user') . "'''")->parse(); } else { $this->buildLink($title, $message, $props, false); } }
/** * Echo! * * @param $event EchoEvent * @return bool */ public static function onBeforeEchoEventInsert(EchoEvent $event) { // Don't spam a user with mention notifications if it's a MassMessage if ($event->getType() == 'mention' && $event->getAgent() && $event->getAgent()->getId() == MassMessage::getMessengerUser()->getId()) { return false; } return true; }
/** * Helper function for getLink() * * @param EchoEvent $event * @param User $user The user receiving the notification * @param String $destination The destination type for the link, e.g. 'agent' * @return Array including target and query parameters */ protected function getLinkParams($event, $user, $destination) { $target = null; $query = array(); // Set up link parameters based on the destination switch ($destination) { case 'agent': if ($event->getAgent()) { $target = $event->getAgent()->getUserPage(); } break; case 'title': $target = $event->getTitle(); break; case 'section': $target = $event->getTitle(); if ($target) { $fragment = $this->formatSubjectAnchor($event); if ($fragment) { $target->setFragment("#{$fragment}"); } } break; case 'diff': $eventData = $event->getExtra(); if (isset($eventData['revid']) && $event->getTitle()) { $target = $event->getTitle(); // Explicitly set fragment to empty string for diff links, $title is // passed around by reference, it may end up using fragment set from // other parameters $target->setFragment('#'); $query = array('oldid' => 'prev', 'diff' => $eventData['revid']); if ($event->getBundleHash()) { // First try cache data from preivous query if (isset($this->bundleData['last-raw-data'])) { $stat = $this->bundleData['last-raw-data']; // Then try to query the storage } else { global $wgEchoBackend; $stat = $wgEchoBackend->getRawBundleData($user, $event->getBundleHash(), $this->distributionType, 'ASC', 1); if ($stat) { $stat = $stat->current(); } } if ($stat) { $extra = $stat->event_extra ? unserialize($stat->event_extra) : array(); if (isset($extra['revid'])) { $oldId = $target->getPreviousRevisionID($extra['revid']); // The diff engine doesn't provide a way to diff against a null revision. // In this case, just fall back old id to the first revision if (!$oldId) { $oldId = $extra['revid']; } if ($oldId < $eventData['revid']) { $query['oldid'] = $oldId; } } } } } break; } return array($target, $query); }