Fixes vim mode line.
[banana.git] / banana / feed.inc.php
index 4befd83..081a2f5 100644 (file)
@@ -9,7 +9,7 @@
 
 require_once dirname(__FILE__) . '/banana.inc.php';
 
-define('BANANA_FEED_VERSION', '0.1');
+define('BANANA_FEED_VERSION', '0.1.1');
 
 class BananaFeed
 {
@@ -32,13 +32,21 @@ class BananaFeed
      */
     public $messages = array();
 
+    /** Last update time
+     */
+    public $lastupdate = 0;
+
+    /** Path where the feed is stored
+     */
+    public $path = null;
+
     /** Create an empty feed
      */
     private function __construct()
     {
         $this->version     = BANANA_FEED_VERSION;
         $this->group       = Banana::$group;
-        $this->description = Banana::$protocole->getDescription();
+        $this->description = trim(Banana::$protocole->getDescription());
     }
 
     /** Update the feed, using current settings of Banana
@@ -49,13 +57,13 @@ class BananaFeed
         if (!Banana::$spool || Banana::$spool->group != $this->group) {
             return false;
         }
-        if (!Banana::$spool->ids) {
+        if (empty(Banana::$spool->overview)) {
             $spool_indexes = array();
         } else {
-            $spool_indexes = Banana::$spool->ids;
+            $spool_indexes = array_keys(Banana::$spool->overview);
             sort($spool_indexes, SORT_NUMERIC);
             $spool_indexes = array_slice($spool_indexes, -Banana::$feed_size, Banana::$feed_size);
-        }    
+        }
         $feed_indexes  = array_keys($this->messages);
         $old = array_diff($feed_indexes, $spool_indexes);
         foreach ($old as $key) {
@@ -64,11 +72,14 @@ class BananaFeed
         $new = array_diff($spool_indexes, $feed_indexes);
         foreach ($new as $key) {
             $message =& Banana::$protocole->getMessage($key);
+            if (is_null($message)) {
+                return;
+            }
             $array = array();
             $array['author'] = $message->getAuthorName();
             $array['date']   = $message->getHeaderValue('Date');
             $array['title']  = $message->getHeaderValue('Subject');
-            $array['body']   = $message->toHtml();
+            $array['body']   = $message->getFormattedBody();
             $array['link']   = Banana::$page->makeUrl(array('group' => $this->group, 'artid' => $key));
             if (Banana::$protocole->canSend()) {
                 $array['reply'] = Banana::$page->makeUrl(array('group' => $this->group, 'artid' => $key, 'action' => 'new'));
@@ -76,7 +87,8 @@ class BananaFeed
             $this->messages[$key] = $array;
         }
         uasort($this->messages, Array('BananaFeed', 'compare'));
-        $this->writeToFile();
+        $this->lastupdate = time();
+        $this->saveToFile();
     }
 
     /** Get the spool corresponding with the current settings of Banana
@@ -97,11 +109,7 @@ class BananaFeed
      */
     static private function filename()
     {
-        $file = Banana::$spool_root . '/' . Banana::$protocole->name() . '/';
-        if (!is_dir($file)) {
-            mkdir($file);
-        }
-        return $file . Banana::$protocole->filename() . '_feed';
+        return BananaSpool::getPath('feed');
     }
 
     /** Read a feed from a cache file
@@ -122,7 +130,7 @@ class BananaFeed
 
     /** Write a feed to a cache file
      */
-    private function writeToFile()
+    private function saveToFile()
     {
         $file = BananaFeed::filename();
         file_put_contents($file, serialize($this));
@@ -130,7 +138,7 @@ class BananaFeed
 
     /** Merge to feeds into a new one
      */
-    static public function &merge(&$feed1, &$feed2, $name, $description = null)
+    static public function &merge($feed1, $feed2, $name, $description = null)
     {
         if (!$feed1) {
             $feed  = null;
@@ -171,6 +179,7 @@ class BananaFeed
         }
         uasort($messages, array('BananaFeed', 'compare'));
         $master->messages =& $messages;
+        $master->lastupdate = time();
         return $master;
     }
 
@@ -191,5 +200,5 @@ class BananaFeed
     }
 }
 
-// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
+// vim:set et sw=4 sts=4 ts=4 fenc=utf-8:
 ?>