From 9132f6a27d8f26bfbf333a3d6d36361d1e0a2fa3 Mon Sep 17 00:00:00 2001 From: Alessandro Cosentino Date: Thu, 16 Aug 2012 01:13:16 -0400 Subject: saves and shows datetime of items --- lib/item.php | 16 +++++++++++++++- lib/itemmapper.php | 17 +++++++++-------- lib/utils.php | 32 +++++++++++++++++++++++++++++--- 3 files changed, 53 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/item.php b/lib/item.php index acef41c92..e64eaa044 100644 --- a/lib/item.php +++ b/lib/item.php @@ -33,7 +33,8 @@ class Item { private $status; //a bit-field set with status flags private $id; //id of the item in the database table private $author; - + private $date; //date is stored in the Unix format + public function __construct($url, $title, $guid, $body, $id = null){ $this->title = $title; $this->url = $url; @@ -98,6 +99,10 @@ class Item { public function setStatus($status){ $this->status = $status; } + + /* change the following method with set/get magic methods + * http://www.php.net/manual/en/language.oop5.overloading.php#object.get + */ public function getTitle(){ return $this->title; @@ -130,4 +135,13 @@ class Item { public function setAuthor($author){ $this->author = $author; } + + public function getDate(){ + return $this->date; + } + + //TODO: check if the parameter is in the Unix format + public function setDate($date){ + $this->date = $date; + } } diff --git a/lib/itemmapper.php b/lib/itemmapper.php index 95488b14f..dcce2a640 100644 --- a/lib/itemmapper.php +++ b/lib/itemmapper.php @@ -37,14 +37,12 @@ class ItemMapper { $url = $row['url']; $title = $row['title']; $guid = $row['guid']; - $status = $row['status']; $body = $row['body']; $id = $row['id']; $item = new Item($url, $title, $guid, $body, $id); - $item->setStatus($status); - $date = $row['date']; - $author = $row['author']; - $item->setAuthor($author); + $item->setStatus($row['status']); + $item->setAuthor($row['author']); + $item->setDate(Utils::dbtimestampToUnixtime($row['pub_date'])); return $item; } @@ -54,7 +52,7 @@ class ItemMapper { * @param feedid The id of the feed in the database table. */ public function findAll($feedid){ - $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE feed_id = ?'); + $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE feed_id = ? ORDER BY pub_date DESC'); $result = $stmt->execute(array($feedid)); $items = array(); @@ -153,8 +151,8 @@ class ItemMapper { $stmt = \OCP\DB::prepare(' INSERT INTO ' . self::tableName . - '(url, title, body, guid, guid_hash, feed_id, status) - VALUES (?, ?, ?, ?, ?, ?, ?) + '(url, title, body, guid, guid_hash, pub_date, feed_id, status) + VALUES (?, ?, ?, ?, ?, ?, ?, ?) '); if(empty($title)) { @@ -167,12 +165,15 @@ class ItemMapper { $body = $l->t('no body'); } + $pub_date = Utils::unixtimeToDbtimestamp($item->getDate()); + $params=array( $item->getUrl(), $title, $body, $guid, $guid_hash, + $pub_date, $feedid, $status ); diff --git a/lib/utils.php b/lib/utils.php index 7ad9f1929..1ecc92245 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -17,7 +17,27 @@ namespace OCA\News; require_once('news/3rdparty/SimplePie/autoloader.php'); class Utils { - + + /** + * @brief Transform a date from UNIX timestamp format to MDB2 timestamp format + * @param dbtimestamp + * @returns + */ + public static function unixtimeToDbtimestamp($unixtime) { + $dt = \DateTime::createFromFormat('U', $unixtime); + return $dt->format('Y-m-d H:i:s'); + } + + /** + * @brief Transform a date from MDB2 timestamp format to UNIX timestamp format + * @param dbtimestamp + * @returns + */ + public static function dbtimestampToUnixtime($dbtimestamp) { + $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $dbtimestamp); + return $dt->format('U'); + } + /** * @brief Fetch a feed from remote * @param url remote url of the feed @@ -32,7 +52,7 @@ class Utils { return null; } - //I understand this try-catch sucks, but SimplePie gives weird errors sometimes + //temporary try-catch to bypass SimplePie bugs try { $spfeed->handle_content_type(); $title = $spfeed->get_title(); @@ -44,11 +64,17 @@ class Utils { $itemTitle = $spitem->get_title(); $itemGUID = $spitem->get_id(); $itemBody = $spitem->get_content(); - $itemAuthor = $spitem->get_author(); $item = new Item($itemUrl, $itemTitle, $itemGUID, $itemBody); + + $itemAuthor = $spitem->get_author(); if ($itemAuthor !== null) { $item->setAuthor($itemAuthor->get_name()); } + + //date in Item is stored in UNIX timestamp format + $itemDate = $spitem->get_date('U'); + $item->setDate($itemDate); + $items[] = $item; } } -- cgit v1.2.3