5b6d3d72238056cf268b1f06f9a10b2370b6aa5b
[banana.git] / banana / feed.inc.php
1 <?php
2 /********************************************************************************
3 * banana/feed.inc.php : Feed Builder
4 * ------------------------
5 *
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
9
10 require_once dirname(__FILE__) . '/banana.inc.php';
11
12 define('BANANA_FEED_VERSION', '0.1');
13
14 class BananaFeed
15 {
16 /** Structure version
17 */
18 private $version;
19
20 /** Feed name
21 */
22 public $group;
23
24 /** Feed description
25 */
26 public $description;
27
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)
32 */
33 public $messages = array();
34
35 /** Last update time
36 */
37 public $lastupdate = 0;
38
39 /** Create an empty feed
40 */
41 private function __construct()
42 {
43 $this->version = BANANA_FEED_VERSION;
44 $this->group = Banana::$group;
45 $this->description = trim(Banana::$protocole->getDescription());
46 }
47
48 /** Update the feed, using current settings of Banana
49 * Calling this function suppose that Banana::$spool is the spool of the current box
50 */
51 public function update()
52 {
53 if (!Banana::$spool || Banana::$spool->group != $this->group) {
54 return false;
55 }
56 if (empty(Banana::$spool->overview)) {
57 $spool_indexes = array();
58 } else {
59 $spool_indexes = array_keys(Banana::$spool->overview);
60 sort($spool_indexes, SORT_NUMERIC);
61 $spool_indexes = array_slice($spool_indexes, -Banana::$feed_size, Banana::$feed_size);
62 }
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]);
67 }
68 $new = array_diff($spool_indexes, $feed_indexes);
69 foreach ($new as $key) {
70 $message =& Banana::$protocole->getMessage($key);
71 if (is_null($message)) {
72 return;
73 }
74 $array = array();
75 $array['author'] = $message->getAuthorName();
76 $array['date'] = $message->getHeaderValue('Date');
77 $array['title'] = $message->getHeaderValue('Subject');
78 $array['body'] = $message->getFormattedBody();
79 $array['link'] = Banana::$page->makeUrl(array('group' => $this->group, 'artid' => $key));
80 if (Banana::$protocole->canSend()) {
81 $array['reply'] = Banana::$page->makeUrl(array('group' => $this->group, 'artid' => $key, 'action' => 'new'));
82 }
83 $this->messages[$key] = $array;
84 }
85 uasort($this->messages, Array('BananaFeed', 'compare'));
86 $this->lastupdate = time();
87 $this->writeToFile();
88 }
89
90 /** Get the spool corresponding with the current settings of Banana
91 */
92 static public function &getFeed()
93 {
94 $feed =& BananaFeed::readFromFile();
95 if (!$feed) {
96 $feed = new BananaFeed();
97 }
98 if (Banana::$feed_updateOnDemand) {
99 $feed->update();
100 }
101 return $feed;
102 }
103
104 /** Return the cache file name
105 */
106 static private function filename()
107 {
108 $file = Banana::$spool_root . '/' . Banana::$protocole->name() . '/';
109 if (!is_dir($file)) {
110 mkdir($file);
111 }
112 return $file . Banana::$protocole->filename() . '_feed';
113 }
114
115 /** Read a feed from a cache file
116 */
117 static private function &readFromFile()
118 {
119 $feed = null;
120 $file = BananaFeed::filename();
121 if (!file_exists($file)) {
122 return $feed;
123 }
124 $feed = unserialize(file_get_contents($file));
125 if ($feed->version != BANANA_FEED_VERSION) {
126 $feed = null;
127 }
128 return $feed;
129 }
130
131 /** Write a feed to a cache file
132 */
133 private function writeToFile()
134 {
135 $file = BananaFeed::filename();
136 file_put_contents($file, serialize($this));
137 }
138
139 /** Merge to feeds into a new one
140 */
141 static public function &merge(&$feed1, &$feed2, $name, $description = null)
142 {
143 if (!$feed1) {
144 $feed = null;
145 $feed1 =& $feed2;
146 $feed2 =& $feed;
147 }
148 if ($feed1->group == $name) {
149 $master =& $feed1;
150 $slave =& $feed2;
151 } else if ($feed2 && $feed2->group == $name) {
152 $master =& $feed2;
153 $slave =& $feed1;
154 } else {
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;
161 }
162 $slave =& $feed2;
163 }
164 if (!$slave) {
165 return $master;
166 }
167 $messages = array();
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);
175 } else {
176 $messages[key($master->messages)] = $m1;
177 $m1 = prev($master->messages);
178 }
179 }
180 uasort($messages, array('BananaFeed', 'compare'));
181 $master->messages =& $messages;
182 $master->lastupdate = time();
183 return $master;
184 }
185
186 static function compare($a, $b)
187 {
188 if ($a['date'] == $b['date']) {
189 return 0;
190 }
191 return $a['date'] < $b['date'] ? -1 : 1;
192 }
193
194 /** Generate the feed xml
195 */
196 public function toXML()
197 {
198 Banana::$page->assign_by_ref('feed', $this);
199 return Banana::$page->feed();
200 }
201 }
202
203 // vim:set et sw=4 sts=4 ts=4 enc=utf-8:
204 ?>