View source
<?php
class heartbeatParser {
private $_info = null;
private $_candidates = array();
private $_messages = array();
private $_sets = array();
private $_timespan_gap = 0;
private $_allowed_tags = array(
'a',
'span',
'em',
'strong',
'ul',
'li',
'p',
'img',
);
private function __construct() {
}
public function set_timespan_gap($gap) {
$this->_timespan_gap = $gap;
}
public function build_sets($messages_raw) {
$renewal_time = $_SERVER['REQUEST_TIME'] - $this->_timespan_gap;
$raw_messages = array();
foreach ($messages_raw as $key => $message) {
if ($renewal_time >= $message->timestamp) {
$renewal_time = $message->timestamp - $this->_timespan_gap;
}
$this->_sets[$renewal_time][$key] = new HeartbeatActivity($message);
}
}
public function set_info(HeartbeatInfo $info) {
$this->_info = $info;
}
public function get_info() {
return $this->_info;
}
public function merge_sets() {
$set_count = 0;
foreach ($this->_sets as $message_set) {
$this
->prepare_candidates($message_set, $set_count);
$set_count++;
}
$this
->remove_variables_duplicates();
$this
->rebuild_in_groups();
return $count;
}
public function get_messages($limit = 25) {
if ($limit > 0) {
return array_slice($this->_messages, 0, $limit);
}
return $this->_messages;
}
private function remove_message_duplicates($message_set) {
global $user;
$ow_relations = array();
foreach ($message_set as $key => $message) {
if ($message->concat_args['group_by'] == 'user-user') {
if (!isset($ow_relations[$message->uid_target])) {
$ow_relations[$message->uid_target] = array();
}
if (!in_array($message->uid, $ow_relations[$message->uid_target])) {
$ow_relations[$message->uid][] = $message->uid_target;
}
}
}
foreach ($message_set as $key => $message) {
if ($message->concat_args['group_by'] == 'user-user') {
if (!in_array($message->uid, $ow_relations[$message->uid_target])) {
unset($message_set[$key]);
}
}
}
$holder = array();
foreach ($message_set as $key => $message) {
$id = $message->message_id . '-' . $message->uid . '-' . $message->uid_target . '-' . $message->nid_target;
if (in_array($id, $holder)) {
$message_set[array_search($id, $holder)]->count++;
unset($message_set[$key]);
}
else {
$holder[$key] = $id;
}
}
return $message_set;
}
private function prepare_candidates($message_set, $set_count = 0) {
static $singles = 0;
$message_set = $this
->remove_message_duplicates($message_set);
foreach ($message_set as $key => $message) {
$type = $message->concat_args['type'];
$gap_id = 'BEAT_' . $set_count . '_' . $message->message_id;
if ($type == 'summary' && $this
->extend_gap_id($gap_id, $message, $type, $set_count)) {
if (!isset($this->_candidates[$gap_id])) {
$this->_candidates[$gap_id] = array(
'count' => 0,
'group_target' => $message->concat_args['group_target'],
'variables' => array(),
);
$this->_messages[$gap_id] = $message;
}
$this->_messages[$gap_id]->target_count++;
$this->_candidates[$gap_id]['variables'][] = $message->variables_array;
$this->_candidates[$gap_id]['count']++;
}
elseif ($type == 'count') {
$gap_id = $gap_id . '_count';
$this->_messages[$gap_id] = $message;
$this->_messages[$gap_id]->target_count++;
}
else {
$gap_id .= '_single_' . $singles;
$this->_messages[$gap_id] = $message;
$singles++;
}
}
}
public function remove_broken_messages($messages = array()) {
if ($messages == array()) {
$messages = $this->_messages;
}
return $messages;
}
private function remove_variables_duplicates() {
$uniques = array();
foreach ($this->_candidates as $single_id => $info) {
$uniques[$single_id] = array();
foreach ($info['variables'] as $rid => $value) {
if (!in_array($value, $uniques[$single_id])) {
$uniques[$single_id][] = $value;
}
else {
unset($this->_candidates[$single_id]['variables'][$rid]);
}
}
}
foreach ($this->_candidates as $single_id => $info) {
$this->_candidates[$single_id]['count'] = count($this->_candidates[$single_id]['variables']);
}
return $uniques;
}
private function extend_gap_id(&$gap_id, $message, $type, $set_count) {
if (!empty($message->variables_array['@node_type'])) {
$gap_id .= '_' . $message->variables_array['@node_type'];
}
if ($message->concat_args['group_by'] == 'node') {
$gap_id .= '_' . $message->concat_args['group_target'] . '_node_' . $message->nid_target;
}
else {
if ($message->concat_args['group_by'] == 'user') {
$gap_id .= '_' . $message->concat_args['group_target'] . '_user_' . $message->uid;
}
else {
if ($message->concat_args['group_by'] == 'user-user') {
$gap_id .= '_user_relation';
$gap_id .= '_' . $message->uid;
}
else {
$gap_id .= '_' . $message->uid . '_' . $message->uid_target;
}
}
}
return TRUE;
}
private function rebuild_in_groups() {
foreach ($this->_candidates as $single_id => $info) {
$this->_messages[$single_id]->message = filter_xss($this->_messages[$single_id]->message, $this->_allowed_tags);
$this->_messages[$single_id]->message_concat = filter_xss($this->_messages[$single_id]->message_concat, $this->_allowed_tags);
$message = $this->_messages[$single_id];
if ($this->_candidates[$single_id]['count'] > 1) {
$message_template = $this->_messages[$single_id]->message_concat;
$message_template = str_replace("%times%", $message->target_count, $message_template);
$message_template = str_replace("%count%", $message->target_count, $message_template);
$merged_string = '';
$unique = $info['variables'];
$count = $info['count'];
if ($count > variable_get('heartbeat_activity_grouping_how_many', 6)) {
$count = variable_get('heartbeat_activity_grouping_how_many', 6);
$unique = array_slice($unique, 0, $count);
}
if (preg_match_all("|\\%(.*)\\%(.*)|U", $message_template, $matches)) {
$placeholder = $matches[1][0];
$target = $message->concat_args['group_target'];
$i = 1;
foreach ($unique as $stored_variables) {
if (isset($stored_variables["@" . $target])) {
if ($i == 1) {
$merged_string .= ' ' . $stored_variables["@" . $target];
}
else {
if ($i < $count && $count > 2) {
$merged_string .= ' ' . $message->concat_args['merge_separator'];
$merged_string .= ' ' . $stored_variables["@" . $target];
}
else {
if ($i == $count || $count == 2) {
$merged_string .= ' ' . $message->concat_args['merge_end_separator'];
$merged_string .= ' ' . $stored_variables["@" . $target];
}
}
}
}
$i++;
}
$message_template = str_replace("%" . $placeholder . "%", $merged_string, $message_template);
}
$this->_messages[$single_id]->message = $message_template;
}
}
}
public static function instantiate($type = 'cached') {
static $instances;
if (!$instances) {
$instances = array();
}
if (!isset($instances[$type])) {
$instances[$type] = new HeartbeatParser();
}
return $instances[$type];
}
}