2 /********************************************************************************
3 * banana/feed.inc.php : Feed Builder
4 * ------------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
10 require_once dirname(__FILE__
) . '/banana.inc.php';
12 define('BANANA_FEED_VERSION', '0.1.1');
28 /** A 'ordered by id' spool of the last messages
29 * Each Message is an array with :
30 * array('author' => author, 'date' => date (UNIX TS), 'title' => subject, 'body' => html body,
31 * 'link' => link, 'reply' => reply)
33 public $messages = array();
37 public $lastupdate = 0;
39 /** Path where the feed is stored
43 /** Create an empty feed
45 private function __construct()
47 $this->version
= BANANA_FEED_VERSION
;
48 $this->group
= Banana
::$group;
49 $this->description
= trim(Banana
::$protocole->getDescription());
52 /** Update the feed, using current settings of Banana
53 * Calling this function suppose that Banana::$spool is the spool of the current box
55 public function update()
57 if (!Banana
::$spool || Banana
::$spool->group
!= $this->group
) {
60 if (empty(Banana
::$spool->overview
)) {
61 $spool_indexes = array();
63 $spool_indexes = array_keys(Banana
::$spool->overview
);
64 sort($spool_indexes, SORT_NUMERIC
);
65 $spool_indexes = array_slice($spool_indexes, -Banana
::$feed_size, Banana
::$feed_size);
67 $feed_indexes = array_keys($this->messages
);
68 $old = array_diff($feed_indexes, $spool_indexes);
69 foreach ($old as $key) {
70 unset($this->messages
[$key]);
72 $new = array_diff($spool_indexes, $feed_indexes);
73 foreach ($new as $key) {
74 $message =& Banana
::$protocole->getMessage($key);
75 if (is_null($message)) {
79 $array['author'] = $message->getAuthorName();
80 $array['date'] = $message->getHeaderValue('Date');
81 $array['title'] = $message->getHeaderValue('Subject');
82 $array['body'] = $message->getFormattedBody();
83 $array['link'] = Banana
::$page->makeUrl(array('group' => $this->group
, 'artid' => $key));
84 if (Banana
::$protocole->canSend()) {
85 $array['reply'] = Banana
::$page->makeUrl(array('group' => $this->group
, 'artid' => $key, 'action' => 'new'));
87 $this->messages
[$key] = $array;
89 uasort($this->messages
, Array('BananaFeed', 'compare'));
90 $this->lastupdate
= time();
94 /** Get the spool corresponding with the current settings of Banana
96 static public function &getFeed()
98 $feed =& BananaFeed
::readFromFile();
100 $feed = new BananaFeed();
102 if (Banana
::$feed_updateOnDemand) {
108 /** Return the cache file name
110 static private function filename()
112 return BananaSpool
::getPath('feed');
115 /** Read a feed from a cache file
117 static private function &readFromFile()
120 $file = BananaFeed
::filename();
121 if (!file_exists($file)) {
124 $feed = unserialize(file_get_contents($file));
125 if ($feed->version
!= BANANA_FEED_VERSION
) {
131 /** Write a feed to a cache file
133 private function saveToFile()
135 $file = BananaFeed
::filename();
136 file_put_contents($file, serialize($this));
139 /** Merge to feeds into a new one
141 static public function &merge(&$feed1, &$feed2, $name, $description = null
)
148 if ($feed1->group
== $name) {
151 } else if ($feed2 && $feed2->group
== $name) {
155 $master = new BananaFeed();
156 $master->group
= $name;
157 $master->description
= $description;
158 foreach ($feed1->messages
as $key=>$message) {
159 $message['title'] = '[' . $feed1->group
. '] ' . $message['title'];
160 $master->messages
[$feed1->group
. '-' . $key] = $message;
168 $m1 = end($master->messages
);
169 $m2 = end($slave->messages
);
170 for ($i = 0 ; $i < 2 * Banana
::$feed_size && ($m1 ||
$m2) ; $i++
) {
171 if ($m2 && (!$m1 ||
$m1['date'] < $m2['date'])) {
172 $m2['title'] = '[' . $feed2->group
. '] ' . $m2['title'];
173 $messages[$slave->group
. '-' . key($slave->messages
)] = $m2;
174 $m2 = prev($slave->messages
);
176 $messages[key($master->messages
)] = $m1;
177 $m1 = prev($master->messages
);
180 uasort($messages, array('BananaFeed', 'compare'));
181 $master->messages
=& $messages;
182 $master->lastupdate
= time();
186 static function compare($a, $b)
188 if ($a['date'] == $b['date']) {
191 return $a['date'] < $b['date'] ?
-1 : 1;
194 /** Generate the feed xml
196 public function toXML()
198 Banana
::$page->assign_by_ref('feed', $this);
199 return Banana
::$page->feed();
203 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: