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');
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 /** Create an empty feed
41 private function __construct()
43 $this->version
= BANANA_FEED_VERSION
;
44 $this->group
= Banana
::$group;
45 $this->description
= trim(Banana
::$protocole->getDescription());
48 /** Update the feed, using current settings of Banana
49 * Calling this function suppose that Banana::$spool is the spool of the current box
51 public function update()
53 if (!Banana
::$spool || Banana
::$spool->group
!= $this->group
) {
56 if (!Banana
::$spool->ids
) {
57 $spool_indexes = array();
59 $spool_indexes = Banana
::$spool->ids
;
60 sort($spool_indexes, SORT_NUMERIC
);
61 $spool_indexes = array_slice($spool_indexes, -Banana
::$feed_size, Banana
::$feed_size);
63 $feed_indexes = array_keys($this->messages
);
64 $old = array_diff($feed_indexes, $spool_indexes);
65 foreach ($old as $key) {
66 unset($this->messages
[$key]);
68 $new = array_diff($spool_indexes, $feed_indexes);
69 foreach ($new as $key) {
70 $message =& Banana
::$protocole->getMessage($key);
72 $array['author'] = $message->getAuthorName();
73 $array['date'] = $message->getHeaderValue('Date');
74 $array['title'] = $message->getHeaderValue('Subject');
75 $array['body'] = $message->toHtml();
76 $array['link'] = Banana
::$page->makeUrl(array('group' => $this->group
, 'artid' => $key));
77 if (Banana
::$protocole->canSend()) {
78 $array['reply'] = Banana
::$page->makeUrl(array('group' => $this->group
, 'artid' => $key, 'action' => 'new'));
80 $this->messages
[$key] = $array;
82 uasort($this->messages
, Array('BananaFeed', 'compare'));
83 $this->lastupdate
= time();
87 /** Get the spool corresponding with the current settings of Banana
89 static public function &getFeed()
91 $feed =& BananaFeed
::readFromFile();
93 $feed = new BananaFeed();
95 if (Banana
::$feed_updateOnDemand) {
101 /** Return the cache file name
103 static private function filename()
105 $file = Banana
::$spool_root . '/' . Banana
::$protocole->name() . '/';
106 if (!is_dir($file)) {
109 return $file . Banana
::$protocole->filename() . '_feed';
112 /** Read a feed from a cache file
114 static private function &readFromFile()
117 $file = BananaFeed
::filename();
118 if (!file_exists($file)) {
121 $feed = unserialize(file_get_contents($file));
122 if ($feed->version
!= BANANA_FEED_VERSION
) {
128 /** Write a feed to a cache file
130 private function writeToFile()
132 $file = BananaFeed
::filename();
133 file_put_contents($file, serialize($this));
136 /** Merge to feeds into a new one
138 static public function &merge(&$feed1, &$feed2, $name, $description = null
)
145 if ($feed1->group
== $name) {
148 } else if ($feed2 && $feed2->group
== $name) {
152 $master = new BananaFeed();
153 $master->group
= $name;
154 $master->description
= $description;
155 foreach ($feed1->messages
as $key=>$message) {
156 $message['title'] = '[' . $feed1->group
. '] ' . $message['title'];
157 $master->messages
[$feed1->group
. '-' . $key] = $message;
165 $m1 = end($master->messages
);
166 $m2 = end($slave->messages
);
167 for ($i = 0 ; $i < 2 * Banana
::$feed_size && ($m1 ||
$m2) ; $i++
) {
168 if ($m2 && (!$m1 ||
$m1['date'] < $m2['date'])) {
169 $m2['title'] = '[' . $feed2->group
. '] ' . $m2['title'];
170 $messages[$slave->group
. '-' . key($slave->messages
)] = $m2;
171 $m2 = prev($slave->messages
);
173 $messages[key($master->messages
)] = $m1;
174 $m1 = prev($master->messages
);
177 uasort($messages, array('BananaFeed', 'compare'));
178 $master->messages
=& $messages;
179 $master->lastupdate
= time();
183 static function compare($a, $b)
185 if ($a['date'] == $b['date']) {
188 return $a['date'] < $b['date'] ?
-1 : 1;
191 /** Generate the feed xml
193 public function toXML()
195 Banana
::$page->assign_by_ref('feed', $this);
196 return Banana
::$page->feed();
200 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: