4befd830a7486ab2aafe2a6c880149c76869727e
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;
78 uasort($this->messages
, Array('BananaFeed', 'compare'));
82 /** Get the spool corresponding with the current settings of Banana
84 static public function &getFeed()
86 $feed =& BananaFeed
::readFromFile();
88 $feed = new BananaFeed();
90 if (Banana
::$feed_updateOnDemand) {
96 /** Return the cache file name
98 static private function filename()
100 $file = Banana
::$spool_root . '/' . Banana
::$protocole->name() . '/';
101 if (!is_dir($file)) {
104 return $file . Banana
::$protocole->filename() . '_feed';
107 /** Read a feed from a cache file
109 static private function &readFromFile()
112 $file = BananaFeed
::filename();
113 if (!file_exists($file)) {
116 $feed = unserialize(file_get_contents($file));
117 if ($feed->version
!= BANANA_FEED_VERSION
) {
123 /** Write a feed to a cache file
125 private function writeToFile()
127 $file = BananaFeed
::filename();
128 file_put_contents($file, serialize($this));
131 /** Merge to feeds into a new one
133 static public function &merge(&$feed1, &$feed2, $name, $description = null
)
140 if ($feed1->group
== $name) {
143 } else if ($feed2 && $feed2->group
== $name) {
147 $master = new BananaFeed();
148 $master->group
= $name;
149 $master->description
= $description;
150 foreach ($feed1->messages
as $key=>$message) {
151 $message['title'] = '[' . $feed1->group
. '] ' . $message['title'];
152 $master->messages
[$feed1->group
. '-' . $key] = $message;
160 $m1 = end($master->messages
);
161 $m2 = end($slave->messages
);
162 for ($i = 0 ; $i < 2 * Banana
::$feed_size && ($m1 ||
$m2) ; $i++
) {
163 if ($m2 && (!$m1 ||
$m1['date'] < $m2['date'])) {
164 $m2['title'] = '[' . $feed2->group
. '] ' . $m2['title'];
165 $messages[$slave->group
. '-' . key($slave->messages
)] = $m2;
166 $m2 = prev($slave->messages
);
168 $messages[key($master->messages
)] = $m1;
169 $m1 = prev($master->messages
);
172 uasort($messages, array('BananaFeed', 'compare'));
173 $master->messages
=& $messages;
177 static function compare($a, $b)
179 if ($a['date'] == $b['date']) {
182 return $a['date'] < $b['date'] ?
-1 : 1;
185 /** Generate the feed xml
187 public function toXML()
189 Banana
::$page->assign_by_ref('feed', $this);
190 return Banana
::$page->feed();
194 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: