summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlessandro Cosentino <cosenal@gmail.com>2012-05-15 14:49:53 -0400
committerAlessandro Cosentino <cosenal@gmail.com>2012-05-15 14:49:53 -0400
commit4353c8ab583b7b10369290dec016883a35b2c185 (patch)
tree4d09d5011a2039c368e9e53e23babd095e67dec7
parent72770c15d3e5b18b620720a259b7c6ecd6274157 (diff)
decoupled model class Feed and mapper class FeedMapper
-rw-r--r--appinfo/app.php1
-rw-r--r--lib/feed.php57
-rw-r--r--lib/feedmapper.php71
-rw-r--r--templates/main.php8
4 files changed, 93 insertions, 44 deletions
diff --git a/appinfo/app.php b/appinfo/app.php
index 303b601b1..213295ac0 100644
--- a/appinfo/app.php
+++ b/appinfo/app.php
@@ -23,6 +23,7 @@
OC::$CLASSPATH['OC_News_Item'] = 'apps/news/lib/item.php';
OC::$CLASSPATH['OC_News_Feed'] = 'apps/news/lib/feed.php';
+OC::$CLASSPATH['OC_News_FeedMapper'] = 'apps/news/lib/feedmapper.php';
$l = new OC_l10n('news');
diff --git a/lib/feed.php b/lib/feed.php
index cd38fb95a..bd2e3b1e4 100644
--- a/lib/feed.php
+++ b/lib/feed.php
@@ -20,60 +20,31 @@
*
*/
-
/**
* This class models a feed.
*/
-class OC_News_Feed extends SimplePie_Core{
+class OC_News_Feed {
private $url;
- private $feed_id;
+ private $spfeed; //we encapsulate a SimplePie_Core object
public function __construct($url){
- parent::__construct();
$this->url = $url;
- $this->set_item_class('OC_News_Item');
- $this->set_feed_url( $url );
- $this->enable_cache( false );
+ $this->spfeed = new SimplePie_Core();
+ $this->spfeed->set_item_class('OC_News_Item');
+ $this->spfeed->set_feed_url( $url );
+ $this->spfeed->enable_cache( false );
//FIXME: figure out if constructor is the right place for these
- $this->init();
- $this->handle_content_type();
+ $this->spfeed->init();
+ $this->spfeed->handle_content_type();
}
-
- public function saveToDB() {
- $CONFIG_DBTYPE = OCP\Config::getSystemValue( "dbtype", "sqlite" );
- if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){
- $_ut = "strftime('%s','now')";
- } elseif($CONFIG_DBTYPE == 'pgsql') {
- $_ut = 'date_part(\'epoch\',now())::integer';
- } else {
- $_ut = "UNIX_TIMESTAMP()";
- }
-
- //FIXME: Detect when user adds a known feed
- $query = OCP\DB::prepare("
- INSERT INTO *PREFIX*news_feeds
- (url, title, user_id, added, lastmodified)
- VALUES (?, ?, ?, $_ut, $_ut)
- ");
-
- $title = $this->get_title();
- if(empty($title)) {
- $l = OC_L10N::get('news');
- $title = $l->t('no title');
- }
+ public function getUrl(){
+ return $this->url;
+ }
- $params=array(
- htmlspecialchars_decode($this->url),
- htmlspecialchars_decode($title),
- OCP\USER::getUser()
- );
- $query->execute($params);
-
- $feed_id = OCP\DB::insertid('*PREFIX*news_feeds');
-
- return $feed_id;
+ public function getTitle(){
+ return $this->spfeed->get_title();
}
-} \ No newline at end of file
+}
diff --git a/lib/feedmapper.php b/lib/feedmapper.php
new file mode 100644
index 000000000..e0c81a2b3
--- /dev/null
+++ b/lib/feedmapper.php
@@ -0,0 +1,71 @@
+<?php
+/**
+* ownCloud - News app
+*
+* @author Alessandro Cosentino
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*
+*/
+
+/**
+ * This class maps a feed to an entry in the feeds table of the database.
+ * It follows the Data Mapper pattern.
+ */
+class OC_News_FeedMapper {
+
+ /**
+ * @brief Save the feed into the database
+ * @returns The id of the feed in the database table.
+ */
+ public function insert(OC_News_Feed $feed) {
+ $CONFIG_DBTYPE = OCP\Config::getSystemValue( "dbtype", "sqlite" );
+ if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){
+ $_ut = "strftime('%s','now')";
+ } elseif($CONFIG_DBTYPE == 'pgsql') {
+ $_ut = 'date_part(\'epoch\',now())::integer';
+ } else {
+ $_ut = "UNIX_TIMESTAMP()";
+ }
+
+ //FIXME: Detect when user adds a known feed
+ //FIXME: specify folder where you want to add
+ $query = OCP\DB::prepare("
+ INSERT INTO *PREFIX*news_feeds
+ (url, title, added, lastmodified)
+ VALUES (?, ?, $_ut, $_ut)
+ ");
+
+ $title = $feed->getTitle();
+
+ if(empty($title)) {
+ $l = OC_L10N::get('news');
+ $title = $l->t('no title');
+ }
+
+ $params=array(
+ htmlspecialchars_decode($feed->getUrl()),
+ htmlspecialchars_decode($title)
+
+ //FIXME: user_id is going to move to the folder properties
+ //OCP\USER::getUser()
+ );
+ $query->execute($params);
+
+ $feed_id = OCP\DB::insertid('*PREFIX*news_feeds');
+
+ return $feed_id;
+ }
+} \ No newline at end of file
diff --git a/templates/main.php b/templates/main.php
index 771d83b3e..f6eabb799 100644
--- a/templates/main.php
+++ b/templates/main.php
@@ -1,10 +1,15 @@
<?php
$feed = new OC_News_Feed( 'http://algorithmsforthekitchen.com/blog/?feed=rss2' );
-$feed->saveToDB();
+$mapper = new OC_News_FeedMapper();
+$feed_id = $mapper->insert($feed);
+echo "<br>" . $feed->getTitle() . "<br>";
+
+/*
$item = $feed->get_item(1);
+
if ($item->isRead()) {
echo $l->t('Read');
}
@@ -24,3 +29,4 @@ if ($item->isRead()) {
else {
echo $l->t('Unread');
}
+*/ \ No newline at end of file