Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented scheduled feed fetching

Added translations
Extended Admin UI
Added JS file
  • Loading branch information...
commit 68e5ee647abf54a432bac4a89a433be10cf4056b 1 parent 6de3c48
@halfdan authored
View
25 API.php
@@ -49,11 +49,27 @@ public function getFeeds($idSites = array()) {
Piwik_Common::prefixTable("feedannotation"),
implode(",", $idSites)
);
- $feeds = Piwik_FetchAll($query);
+
+ $db = Zend_Registry::get('db');
+ $feeds = $db->fetchAll($query);
return $feeds;
}
+ public function addFeed($idSite, $url) {
+ Piwik::checkUserHasAdminAccess(array($idSite));
+
+ if($this->isValidFeedUrl($url)) {
+ $query = sprintf("INSERT INTO %s (idsite, feed_url) VALUES (?, ?)",
+ Piwik_Common::prefixTable("feedannotation")
+ );
+ $db = Zend_Registry::get('db');
+ $db->query($query, array($idSite, $url));
+ } else {
+ throw new Piwik_FeedAnnotation_InvalidFeedException(sprintf("Feed URL not valid: %s", $url));
+ }
+ }
+
/**
* Checks whether the URL returns a parsable feed (RSS/Atom)
*
@@ -68,4 +84,11 @@ public function isValidFeedUrl($url) {
return false;
}
}
+}
+
+/**
+ * Custom exception that is thrown when an invalid feed URL is specified.
+ */
+class Piwik_FeedAnnotation_InvalidFeedException extends Exception {
+
}
View
1  Controller.php
@@ -15,6 +15,7 @@ function index()
$idSitesAvailable = Piwik_SitesManager_API::getInstance()->getSitesWithAdminAccess();
+ $view->feeds = Piwik_FeedAnnotation_API::getInstance()->getFeeds(array($idSite));
$view->idSiteSelected = $idSite;
$view->idSitesAvailable = $idSitesAvailable;
$view->menu = Piwik_GetAdminMenu();
View
57 FeedAnnotation.php
@@ -92,19 +92,72 @@ public function getScheduledTasks($notification)
{
$tasks = &$notification->getNotificationObject();
- $cacheDataByArchiveNameReportsTask = new Piwik_ScheduledTask(
+ $updateFeedAnnotationsTask = new Piwik_ScheduledTask(
$this,
'updateFeedAnnotations',
null,
new Piwik_ScheduledTime_Daily()
);
- $tasks[] = $cacheDataByArchiveNameReportsTask;
+ $tasks[] = $updateFeedAnnotationsTask;
}
/**
* Fetches configured feeds and creates/updates Annotations.
*/
public function updateFeedAnnotations() {
+ $feeds = Piwik_FeedAnnotation_API::getInstance()->getFeeds();
+
+ foreach($feeds as $feed) {
+ try {
+ $this->processFeedUrl($feed);
+ } catch (Zend_Feed_Exception $ex) {
+ Zend_Registry::get('logger_exception')->logEvent( $ex );
+ }
+ }
+ }
+
+ /**
+ * Fetches a feed by its URL and adds new feed items as annotations.
+ * New items are determined by last_processed date and time.
+ *
+ * @param $idSite
+ * @param $url
+ */
+ private function processFeedUrl($feed) {
+ $lastProcessed = $feed['last_processed'];
+ $idSite = $feed['idsite'];
+ $idFeed = $feed['idfeed'];
+ $url = $feed['feed_url'];
+
+ $feedData = Zend_Feed::import($url);
+
+ $db = Zend_Registry::get('db');
+
+ foreach($feedData as $feedEntry) {
+ $date = null;
+ if(!empty($feedEntry->published)) {
+ // Atom Feed (RFC 3339 Date)
+ $date = new DateTime($feedEntry->published);
+ } else if(!empty($feedEntry->pubDate)) {
+ // RSS 2.0 Feed (RFC 822 Date)
+ $date = new DateTime($feedEntry->pubDate);
+ } else {
+ Piwik::log(sprintf("Feed %s contains invalid entries. Skipping entry.", $url));
+ continue;
+ }
+
+ $title = (string)$feedEntry->title;
+
+ // If feed was never processed or entry is newer than $lastProcessed
+ if(!$lastProcessed || $date > new DateTime($lastProcessed)) {
+ Piwik_Annotations_API::getInstance()->add($idSite, $date->format('Y-m-d'), $title);
+ }
+ }
+ // Update last_processed
+ $db->update(Piwik_Common::prefixTable("feedannotation"),
+ array("last_processed" => new Zend_Db_Expr('now()')),
+ sprintf('idfeed = %d', $idFeed)
+ );
}
}
View
9 lang/en.php
@@ -13,7 +13,10 @@
'FeedAnnotation_PluginDescription' => 'Plugin to automatically create Annotations by polling a feed (Atom/RSS)',
'FeedAnnotation_MenuGeneralSettings' => 'Feed Annotations',
'FeedAnnotation_Manage' => 'Manage Feed Annotations',
- 'FeedAnnotation_AdminDescription' => 'Here you can add and remove feeds from your websites that are automatically fetched. Feed ttems will automatically be added as Annotations on a daily basis.',
+ 'FeedAnnotation_AdminDescription' => 'Here you can add and remove feeds from your websites. Feeds are automatically fetched on a daily basis and new feed items will be added as Annotations.',
'FeedAnnotation_FeedUrl' => 'Feed URL',
- 'FeedAnnotation_LastProcessed' => 'Last Processed'
-);
+ 'FeedAnnotation_LastProcessed' => 'Last Processed',
+ 'FeedAnnotation_AddFeed' => 'Add Feed',
+ 'FeedAnnotation_Never' => 'Never',
+ 'FeedAnnotation_Website' => 'Website',
+);
View
13 templates/feedannotation.js
@@ -0,0 +1,13 @@
+/**
+ * Created with JetBrains PhpStorm.
+ * User: halfdan
+ * Date: 2/27/13
+ * Time: 6:29 PM
+ * To change this template use File | Settings | File Templates.
+ */
+$(document).ready(function() {
+ $("#new_feed_url").focusout(function() {
+ var url = $("#new_feed_url").val();
+ $.ajax
+ });
+});
View
21 templates/index.tpl
@@ -1,13 +1,17 @@
{include file="CoreAdminHome/templates/header.tpl"}
+<script type="text/javascript" src="plugins/FeedAnnotation/templates/feedannotation.js"></script>
+
<h2>{'FeedAnnotation_Manage'|translate}</h2>
<p>{'FeedAnnotation_AdminDescription'|translate}</p>
-<section>
+<section class="sites_selector_container">
+ <span style="line-height: 30px">{'FeedAnnotation_Website'|translate}:</span>
{include file="CoreHome/templates/sites_selection.tpl"
idSite=$idSiteSelected sites=$idSitesAvailable showAllSitesItem=false
- showSelectedSite=true siteSelectorId="feedAnnotationSiteSelect"
+ showSelectedSite=false siteSelectorId="feedAnnotationSiteSelect"
switchSiteOnSelect=true}
+ <p>SELECTED SITE= {$idSiteSelected}</p>
</section>
<div class="entityContainer">
@@ -19,8 +23,21 @@
</tr>
</thead>
<tbody>
+ {foreach from=$feeds key=i item=feed}
+ <tr>
+ <td>{$feed.feed_url}</td>
+ <td>{if $feed.last_processed}{$feed.last_processed|date_format}{else}{'FeedAnnotation_Never'|translate}{/if}</td>
+ </tr>
+ {/foreach}
</tbody>
</table>
</div>
+<h3>{'FeedAnnotation_AddFeed'|translate}</h3>
+
+<form action="">
+ <label>{'FeedAnnotation_FeedUrl'|translate} <input type="text" id="new_feed_url" placeholder="Feed URL" /></label>
+ <input type="hidden" value="{$idSiteSelected}" />
+ <input type="submit" value="Save" class="submit" />
+</form>
{include file="CoreAdminHome/templates/footer.tpl"}
Please sign in to comment.
Something went wrong with that request. Please try again.