require_once dirname(__FILE__) . '/banana.inc.php';
-define('BANANA_FEED_VERSION', '0.1');
+define('BANANA_FEED_VERSION', '0.1.1');
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
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) {
$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'));
}
$this->messages[$key] = $array;
}
- $this->writeToFile();
+ uasort($this->messages, Array('BananaFeed', 'compare'));
+ $this->lastupdate = time();
+ $this->saveToFile();
}
/** Get the spool corresponding with the current settings of Banana
*/
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
/** Write a feed to a cache file
*/
- private function writeToFile()
+ private function saveToFile()
{
$file = BananaFeed::filename();
file_put_contents($file, serialize($this));
/** 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;
return $master;
}
$messages = array();
- $m1 = reset($master->messages);
- $m2 = reset($slave->messages);
+ $m1 = end($master->messages);
+ $m2 = end($slave->messages);
for ($i = 0 ; $i < 2 * Banana::$feed_size && ($m1 || $m2) ; $i++) {
if ($m2 && (!$m1 || $m1['date'] < $m2['date'])) {
$m2['title'] = '[' . $feed2->group . '] ' . $m2['title'];
$messages[$slave->group . '-' . key($slave->messages)] = $m2;
- $m2 = next($slave->messages);
+ $m2 = prev($slave->messages);
} else {
$messages[key($master->messages)] = $m1;
- $m1 = next($master->messages);
+ $m1 = prev($master->messages);
}
}
+ uasort($messages, array('BananaFeed', 'compare'));
$master->messages =& $messages;
+ $master->lastupdate = time();
return $master;
}
+ static function compare($a, $b)
+ {
+ if ($a['date'] == $b['date']) {
+ return 0;
+ }
+ return $a['date'] < $b['date'] ? -1 : 1;
+ }
+
/** Generate the feed xml
*/
public function toXML()