db =& JFactory :: getDBO(); $this -> user =& JFactory :: getUser(); $this -> log =& JLog :: getInstance(); $this -> db -> setQuery('SELECT `value` FROM `#__openaire` WHERE `key` = \'atomUrn\';'); $this -> atomUrn = $this -> db -> loadResult(); if ($this -> atomUrn === NULL) $this -> log -> addEntry(array('level' => 'error', 'comment' => ('Error loading Atom URN: ' . $this -> db -> getErrorMsg() . ' (' . $this -> db -> getErrorNum() . ')'))); $this -> db -> setQuery('SELECT `value` FROM `#__openaire` WHERE `key` = \'jdbcUri\';'); $this -> jdbcUri = $this -> db -> loadResult(); if ($this -> jdbcUri === NULL) $this -> log -> addEntry(array('level' => 'error', 'comment' => ('Error loading JDBC URI: ' . $this -> db -> getErrorMsg() . ' (' . $this -> db -> getErrorNum() . ')'))); $this -> databasePrefix = JFactory :: getApplication() -> getCfg('dbprefix'); $this -> db -> setQuery('SELECT `value` FROM `#__openaire` WHERE `key` = \'newsSection\';'); $this -> newsSection = $this -> db -> loadResult(); if ($this -> newsSection === NULL) $this -> log -> addEntry(array('level' => 'error', 'comment' => ('Error loading news section: ' . $this -> db -> getErrorMsg() . ' (' . $this -> db -> getErrorNum() . ')'))); $this -> db -> setQuery('SELECT `value` FROM `#__openaire` WHERE `key` = \'newsCategory\';'); $this -> newsCategory = $this -> db -> loadResult(); if ($this -> newsCategory === NULL) $this -> log -> addEntry(array('level' => 'error', 'comment' => ('Error loading news category: ' . $this -> db -> getErrorMsg() . ' (' . $this -> db -> getErrorNum() . ')'))); $this -> db -> setQuery('SELECT `value` FROM `#__openaire` WHERE `key` = \'newsUser\';'); $this -> newsUser = $this -> db -> loadResult(); if ($this -> newsUser === NULL) $this -> log -> addEntry(array('level' => 'error', 'comment' => ('Error loading news user: ' . $this -> db -> getErrorMsg() . ' (' . $this -> db -> getErrorNum() . ')'))); $this -> db -> setQuery('SELECT `value` FROM `#__openaire` WHERE `key` = \'twitterAccessToken\';'); $this -> twitterAccessToken = $this -> db -> loadResult(); if ($this -> twitterAccessToken === NULL) $this -> log -> addEntry(array('level' => 'error', 'comment' => ('Error loading twitter access token: ' . $this -> db -> getErrorMsg() . ' (' . $this -> db -> getErrorNum() . ')'))); $this -> db -> setQuery('SELECT `value` FROM `#__openaire` WHERE `key` = \'twitterAccessTokenSecret\';'); $this -> twitterAccessTokenSecret = $this -> db -> loadResult(); if ($this -> twitterAccessTokenSecret === NULL) $this -> log -> addEntry(array('level' => 'error', 'comment' => ('Error loading twitter access token secret: ' . $this -> db -> getErrorMsg() . ' (' . $this -> db -> getErrorNum() . ')'))); $this -> db -> setQuery('SELECT`value` FROM `#__openaire` WHERE `key` = \'subscriptionsPageSize\';'); $this -> pageSize = $this -> db -> loadResult(); if ($this -> pageSize === NULL) $this -> log -> addEntry(array('level' => 'error', 'comment' => ('Error loading page size: ' . $this -> db -> getErrorMsg() . ' (' . $this -> db -> getErrorNum() . ')'))); $this -> db -> setQuery('SELECT `value` FROM `#__openaire` WHERE `key` = \'thriftConnectorHost\';'); $host = $this -> db -> loadResult(); if ($host === NULL) $this -> log -> addEntry(array('level' => 'error', 'comment' => ('Error loading Thrift host: ' . $this -> db -> getErrorMsg() . ' (' . $this -> db -> getErrorNum() . ')'))); $this -> db -> setQuery('SELECT `value` FROM `#__openaire` WHERE `key` = \'thriftConnectorPort\';'); $port = $this -> db -> loadResult(); if ($port === NULL) $this -> log -> addEntry(array('level' => 'error', 'comment' => ('Error loading Thrift port: ' . $this -> db -> getErrorMsg() . ' (' . $this -> db -> getErrorNum() . ')'))); $this -> db -> setQuery('SELECT `value` FROM `#__openaire` WHERE `key` = \'thriftConnectorTimeout\';'); $timeout = $this -> db -> loadResult(); if ($timeout === NULL) $this -> log -> addEntry(array('level' => 'error', 'comment' => ('Error loading Thrift timeout: ' . $this -> db -> getErrorMsg() . ' (' . $this -> db -> getErrorNum() . ')'))); // Create a thrift connection (Boiler plate) $socket = new TSocket($host, $port); $socket -> setRecvTimeout($timeout); $this -> transport = new TBufferedTransport($socket); $this -> client = new OpenAireConnectorClient(new TBinaryProtocol($this -> transport)); } public function getTopics($parent, $name, $limit) { $this -> db -> setQuery('SELECT `id`, `name`, `description`, `templateId`, `notificationService`, `queryId`, `resultId` FROM `#__openaire_topic_hierarchy` WHERE `parent` ' . (($parent == 0) ? 'IS NULL' : ('= ' . $this -> db -> quote($parent))) . ' AND `name` LIKE CONCAT(\'%\', ' . $this -> db -> quote($name) . ", '%') ORDER BY `id` LIMIT $limit;"); $topics = $this -> db -> loadAssocList(); if ($topics === NULL) { $this -> log -> addEntry(array('level' => 'error', 'comment' => ("Error retrieving topics with parent $parent, name like \"$name\" and " . " $limit: " . $this -> db -> getErrorMsg() . ' (' . $this -> db -> getErrorNum() . ')'))); return array(); } $this -> log -> addEntry(array('level' => 'info', 'comment' => ('Retrieved ' . count($topics) . " topics with parent $parent, name like \"$name\" and limit $limit"))); return $topics; } public function getFullTopicName($templateId, $notificationService, $queryId, $resultId) { $this -> db -> setQuery('SELECT `parent`, `name` FROM `#__openaire_topic_hierarchy` WHERE `templateId` = ' . $this -> db -> quote($templateId) . ' AND `notificationService` = ' . $this -> db -> quote($notificationService) . ' AND `queryId` = ' . $this -> db -> quote($queryId) . ' AND `resultId` = ' . $this -> db -> quote($resultId) . ';'); $topic = $this -> db -> loadAssoc(); if ($topic === NULL) $this -> log -> addEntry(array('level' => 'error', 'comment' => ("Error retrieving parent and name of topic with template $templateId, notification service $notificationService, " . "query $queryId and result $resultId: " . $this -> db -> getErrorMsg() . ' (' . $this -> db -> getErrorNum() . ')'))); else $this -> log -> addEntry(array('level' => 'info', 'comment' => ("Topic with template $templateId, notification service $notificationService, query $queryId and result " . "$resultId has parent " . $topic['parent'] . ' and name ' . $topic['name']))); return ($topic['parent'] == NULL) ? $topic['name'] : ($this -> getTopicNameById($topic['parent']) . ' ' . $topic['name']); } public function getAlertModes() { $isAdmin = ($this -> user -> usertype == 'Administrator') || ($this -> user -> usertype == 'Super Administrator'); $this -> db -> setQuery('SELECT `id`, `name`, `description`, `batch` FROM `#__openaire_alert_modes`' . ($isAdmin ? '' : ' WHERE `adminOnly` = FALSE') . ';'); $alertModes = $this -> db -> loadAssocList(); if ($alertModes === NULL) { $this -> log -> addEntry(array('level' => 'error', 'comment' => ('Error retrieving alert modes: ' . $this -> db -> getErrorMsg() . ' (' . $this -> db -> getErrorNum() . ')'))); return array(); } $this -> log -> addEntry(array('level' => 'info', 'comment' => ('Retrieved ' . count($alertModes) . ' alert modes for ' . ($isAdmin ? 'administrator' : 'simple user')))); return $alertModes; } public function getSubscriptions() { try { $this -> transport -> open(); $result = array(); foreach ($this -> getAlertModes() as $alertMode) { // for each alert mode that the current user may use list($alertMode['id'], $subscriber) = $this -> getAlertModeAndSubscriber($alertMode['id'], 0); for ($offset = 0; ; $offset += $this -> pageSize) { $subscriptions = $this -> client -> getSubscriptions($alertMode['id'], $subscriber, intval($this -> pageSize), $offset); foreach ($subscriptions as $subscription) { $subscription -> topic = $this -> getFullTopicName($subscription -> templateId, $subscription -> notificationService, $subscription -> queryId, $subscription -> resultId); list($subscription -> alertModeName, $subscription -> batchPeriod) = $this -> getAlertModeNameAndBatchPeriod($subscription -> alertMode, $subscription -> subscriber); $result[] = $subscription; } if (count($subscriptions) < $this -> pageSize) break; } if ($alertMode['batch']) { // alert mode is a batch alert mode too list($alertMode['id'], $subscriber) = $this -> getAlertModeAndSubscriber($alertMode['id'], NULL); for ($offset = 0; ; $offset += $this -> pageSize) { $subscriptions = $this -> client -> getSubscriptions($alertMode['id'], $subscriber, $this -> pageSize, $offset); foreach ($subscriptions as $subscription) { $subscription -> topic = $this -> getFullTopicName($subscription -> templateId, $subscription -> notificationService, $subscription -> queryId, $subscription -> resultId); list($subscription -> alertModeName, $subscription -> batchPeriod) = $this -> getAlertModeNameAndBatchPeriod($subscription -> alertMode, $subscription -> subscriber); $result[] = $subscription; } if (count($subscriptions) < $this -> pageSize) break; } } } $this -> transport -> close(); $this -> log -> addEntry(array('level' => 'info', 'comment' => 'Retrieved ' . count($result) . ' subscriptions')); // sort subscriptions usort($result, function ($subscription1, $subscription2) { $topicComparison = strcmp($subscription1 -> topic, $subscription2 -> topic); $alertModeComparison = strcmp($subscription1 -> alertModeName, $subscription2 -> alertModeName); $batchPeriodComparison = strcmp($subscription1 -> batchPeriod, $subscription2 -> batchPeriod); return ($topicComparison == 0) ? (($alertModeComparison == 0) ? $batchPeriodComparison : $alertModeComparison) : $topicComparison; }); return $result; } catch (Exception $e) { $this -> log -> addEntry(array('level' => 'error', 'comment' => "Error retrieving subscriptions:\n$e")); return array(); } } public function addSubscription($templateId, $notificationService, $queryId, $resultId, $alertMode, $batchPeriod) { try { list($alertMode, $subscriber) = $this -> getAlertModeAndSubscriber($alertMode, $batchPeriod); $this -> transport -> open(); $this -> client -> addSubscription(new AlertSubscription(array('templateId' => $templateId, 'notificationService' => $notificationService, 'queryId' => $queryId, 'resultId' => $resultId, 'alertMode' => $alertMode, 'subscriber' => $subscriber))); $this -> transport -> close(); $this -> log -> addEntry(array('level' => 'info', 'comment' => "Added subscription (template: $templateId, notification service: $notificationService, query: $queryId, result: " . "$resultId, alert mode: $alertMode, subscriber: $subscriber)")); } catch (Exception $e) { $this -> log -> addEntry(array('level' => 'error', 'commment' => ("Error adding subscription (template: $templateId, notification service: $notificationService, query: $queryId, " . "result: $resultId, alert mode: $alertMode, subscriber: $subscriber):\n$e"))); } } public function removeSubscription($templateId, $notificationService, $queryId, $resultId, $alertMode, $subscriber) { try { $this -> transport -> open(); $this -> client -> removeSubscription(new AlertSubscription(array('templateId' => $templateId, 'notificationService' => $notificationService, 'queryId' => $queryId, 'resultId' => $resultId, 'alertMode' => $alertMode, 'subscriber' => $subscriber))); $this -> transport -> close(); $this -> log -> addEntry(array('level' => 'info', 'comment' => "Removed subscription (template: $templateId, notification service: $notificationService, query: $queryId, result: " . "$resultId, alert mode: $alertMode, subscriber: $subscriber)")); } catch (Exception $e) { $this -> log -> addEntry(array('level' => 'error', 'comment' => "Error removing subscription (template: $templateId, notification service: $notificationService, query: $queryId, " . "result: $resultId, alert mode: $alertMode, subscriber: $subscriber):\n$e")); } } public function getAlertResultsTitle($templateId, $notificationService, $queryId, $date, $resultId) { try { $this -> transport -> open(); $title = $this -> client -> getAlertResultsTitle($templateId, $notificationService, $queryId, $resultId); $this -> transport -> close(); $this -> log -> addEntry(array('level' => 'info', 'comment' => "Retrieved title for alert results (template: $templateId, notification service: $notificationService, query: " . "$queryId, result: $resultId)")); return $title; } catch (Exception $e) { $this -> log -> addEntry(array('level' => 'error', 'comment' => "Error retrieving title for alert results (template: $templateId, notification service: $notificationService, query: " . "$queryId, result: $resultId):\n$e")); } } public function countAlertResults($notificationService, $queryId, $date, $resultId) { try { $this -> transport -> open(); $count = $this -> client -> countAlertResults($notificationService, $queryId, $date, $resultId); $this -> transport -> close(); $this -> log -> addEntry(array('level' => 'info', 'comment' => "Counted $count alert results (notification service: $notificationService, query: $queryId, date: $date, result: " . "$resultId)")); return $count; } catch (Exception $e) { $this -> log -> addEntry(array('level' => 'error', 'comment' => "Error counting alert results (template: $templateId, notification service: $notificationService, query: $queryId, " . "result: $resultId):\n$e")); } } public function getAlertResults($notificationService, $queryId, $resultId, $fromDate, $toDate, $limit, $offset) { try { $this -> transport -> open(); $resultPage = $this -> client -> getAlertResults($notificationService, $queryId, $resultId, $fromDate, $toDate, $limit, $offset); $this -> transport -> close(); $this -> log -> addEntry(array('level' => 'info', 'comment' => 'Retrieved alert results (limit: ' . $resultPage -> limit . ', offset: ' . $resultPage -> offset . ', columns: [' . implode(', ', $resultPage -> columns) . '], rows: ' . count($resultPage -> rows) . ") (notification service: $notificationService, query: $queryId, result: $resultId" . ", from date: $fromDate, to date: $toDate, limit: $limit, offset: $offset)")); return $resultPage; } catch (Exception $e) { $this -> log -> addEntry(array('level' => 'error', 'comment' => "Error retrieving alert results (notification service: $notificationService, query: $queryId, result: $resultId" . ", from date: $fromDate, to date: $toDate, limit: $limit, offset: $offset)")); return null; } } private function getTopicNameById($id) { $this -> db -> setQuery('SELECT `parent`, `name` FROM `#__openaire_topic_hierarchy` WHERE `id` = ' . $id . ';'); $topic = $this -> db -> loadAssoc(); if ($topic === NULL) $this -> log -> addEntry(array('level' => 'error', 'comment' => ("Error retrieving parent and name of topic with id $id: " . $this -> db -> getErrorMsg() . ' (' . $this -> db -> getErrorNum() . ')'))); else $this -> log -> addEntry(array('level' => 'info', 'comment' => ("Topic with id $id has parent " . $topic['parent'] . ' and name ' . $topic['name']))); return ($topic['parent'] == NULL) ? $topic['name'] : ($this -> getTopicNameById($topic['parent']) . ' ' . $topic['name']); } private function getAlertModeAndSubscriber($alertMode, $batchPeriod) { switch($alertMode) { case 'Atom': $subscriber = $this -> atomUrn; break; case 'JDBC': $subscriber = $this -> jdbcUri . '&table=' . $this -> databasePrefix . 'openaire_alerts&username=' . urlencode($this -> user -> username) . '&time=$time&title=$title&message=' . '$message&link=$link'; break; case 'Joomla!': $subscriber = $this -> jdbcUri . '&tablePrefix=' . urlencode($this -> databasePrefix) . '§ion=' . $this -> newsSection . '&category=' . $this -> newsCategory . '&userId=' . $this -> newsUser; break; case 'SMTP': $subscriber = 'mailto:' . $this -> user -> email; break; case 'twitter': $subscriber = 'data:accessToken=' . urlencode($this -> twitterAccessToken) . '&accessTokenSecret=' . urlencode($this -> twitterAccessTokenSecret); break; } if (($batchPeriod !== 0) && ($batchPeriod !== '0')) { $alertMode = "Batch $alertMode"; $subscriber = "batch:$subscriber#$batchPeriod"; } $this -> log -> addEntry(array('level' => 'debug', 'comment' => "Alert mode is $alertMode")); $this -> log -> addEntry(array('level' => 'debug', 'comment' => "Subscriber is $subscriber")); return array($alertMode, $subscriber); } private function getAlertModeNameAndBatchPeriod($alertMode, $subscriberUri) { if ((strpos($alertMode, 'Batch ') === 0) && (strpos($subscriberUri, 'batch:') === 0)) { // alert mode is a batch alert mode $alertMode = substr($alertMode, strlen('Batch ')); // ommit 'Batch ' to get alert mode $batchPeriod = substr($subscriberUri, strpos($subscriberUri, '#') + 1); // find batch period switch ($batchPeriod) { case 43200: $batchPeriod = 'monthly'; break; case 21600: $batchPeriod = 'bi-monthly'; break; case 10080: $batchPeriod = 'weekly'; break; case 1440: $batchPeriod = 'daily'; break; case 0: // batch period is either zero or null; make it non-batch case '0': case NULL: $batchPeriod = 'as it happens'; break; default: $batchPeriod = "every $batchPeriod minute(s)"; } } else // alert mode is not a batch alert mode $batchPeriod = 'as it happens'; $this -> db -> setQuery("SELECT `name` FROM `#__openaire_alert_modes` WHERE `id` = '$alertMode';"); $alertModeName = $this -> db -> loadResult(); if ($alertModeName === NULL) { $this -> log -> addEntry(array('level' => 'error', 'comment' => "Error resolving alert mode name and batch period for alert mode $alertMode and subscriber URI $subscriberUri")); return array(NULL, NULL); } $this -> log -> addEntry(array('level' => 'info', 'comment' => "Alert mode name and batch period for alert mode $alertMode and subscriber URI $subscriberUri are $alertModeName and " . "$batchPeriod respectively")); return array($alertModeName, $batchPeriod); } } ?>