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();
35 /** Create an empty feed
37 private function __construct()
39 $this->version
= BANANA_FEED_VERSION
;
40 $this->group
= Banana
::$group;
41 $this->description
= Banana
::$protocole->getDescription();
44 /** Update the feed, using current settings of Banana
45 * Calling this function suppose that Banana::$spool is the spool of the current box
47 public function update()
49 if (!Banana
::$spool || Banana
::$spool->group
!= $this->group
) {
52 if (!Banana
::$spool->ids
) {
53 $spool_indexes = array();
55 $spool_indexes = Banana
::$spool->ids
;
56 sort($spool_indexes, SORT_NUMERIC
);
57 $spool_indexes = array_slice($spool_indexes, -Banana
::$feed_size, Banana
::$feed_size);
59 $feed_indexes = array_keys($this->messages
);
60 $old = array_diff($feed_indexes, $spool_indexes);
61 foreach ($old as $key) {
62 unset($this->messages
[$key]);
64 $new = array_diff($spool_indexes, $feed_indexes);
65 foreach ($new as $key) {
66 $message =& Banana
::$protocole->getMessage($key);
68 $array['author'] = $message->getAuthorName();
69 $array['date'] = $message->getHeaderValue('Date');
70 $array['title'] = $message->getHeaderValue('Subject');
71 $array['body'] = $message->toHtml();
72 $array['link'] = Banana
::$page->makeUrl(array('group' => $this->group
, 'artid' => $key));
73 if (Banana
::$protocole->canSend()) {
74 $array['reply'] = Banana
::$page->makeUrl(array('group' => $this->group
, 'artid' => $key, 'action' => 'new'));
76 $this->messages
[$key] = $array;
81 /** Get the spool corresponding with the current settings of Banana
83 static public function &getFeed()
85 $feed =& BananaFeed
::readFromFile();
87 $feed = new BananaFeed();
89 if (Banana
::$feed_updateOnDemand) {
95 /** Return the cache file name
97 static private function filename()
99 $file = Banana
::$spool_root . '/' . Banana
::$protocole->name() . '/';
100 if (!is_dir($file)) {
103 return $file . Banana
::$protocole->filename() . '_feed';
106 /** Read a feed from a cache file
108 static private function &readFromFile()
111 $file = BananaFeed
::filename();
112 if (!file_exists($file)) {
115 $feed = unserialize(file_get_contents($file));
116 if ($feed->version
!= BANANA_FEED_VERSION
) {
122 /** Write a feed to a cache file
124 private function writeToFile()
126 $file = BananaFeed
::filename();
127 file_put_contents($file, serialize($this));
130 /** Merge to feeds into a new one
132 static public function &merge(&$feed1, &$feed2, $name, $description = null
)
139 if ($feed1->group
== $name) {
142 } else if ($feed2 && $feed2->group
== $name) {
146 $master = new BananaFeed();
147 $master->group
= $name;
148 $master->description
= $description;
149 foreach ($feed1->messages
as $key=>$message) {
150 $message['title'] = '[' . $feed1->group
. '] ' . $message['title'];
151 $master->messages
[$feed1->group
. '-' . $key] = $message;
159 $m1 = reset($master->messages
);
160 $m2 = reset($slave->messages
);
161 for ($i = 0 ; $i < 2 * Banana
::$feed_size && ($m1 ||
$m2) ; $i++
) {
162 if ($m2 && (!$m1 ||
$m1['date'] < $m2['date'])) {
163 $m2['title'] = '[' . $feed2->group
. '] ' . $m2['title'];
164 $messages[$slave->group
. '-' . key($slave->messages
)] = $m2;
165 $m2 = next($slave->messages
);
167 $messages[key($master->messages
)] = $m1;
168 $m1 = next($master->messages
);
171 $master->messages
=& $messages;
175 /** Generate the feed xml
177 public function toXML()
179 Banana
::$page->assign_by_ref('feed', $this);
180 return Banana
::$page->feed();
184 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: