e02edbfe |
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 | /** Create an empty feed |
36 | */ |
37 | private function __construct() |
38 | { |
39 | $this->version = BANANA_FEED_VERSION; |
40 | $this->group = Banana::$group; |
41 | $this->description = Banana::$protocole->getDescription(); |
42 | } |
43 | |
44 | /** Update the feed, using current settings of Banana |
45 | * Calling this function suppose that Banana::$spool is the spool of the current box |
46 | */ |
47 | public function update() |
48 | { |
49 | if (!Banana::$spool || Banana::$spool->group != $this->group) { |
50 | return false; |
51 | } |
52 | if (!Banana::$spool->ids) { |
53 | $spool_indexes = array(); |
54 | } else { |
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); |
58 | } |
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]); |
63 | } |
64 | $new = array_diff($spool_indexes, $feed_indexes); |
65 | foreach ($new as $key) { |
66 | $message =& Banana::$protocole->getMessage($key); |
67 | $array = array(); |
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')); |
75 | } |
76 | $this->messages[$key] = $array; |
77 | } |
78 | $this->writeToFile(); |
79 | } |
80 | |
81 | /** Get the spool corresponding with the current settings of Banana |
82 | */ |
83 | static public function &getFeed() |
84 | { |
85 | $feed =& BananaFeed::readFromFile(); |
86 | if (!$feed) { |
87 | $feed = new BananaFeed(); |
88 | } |
89 | if (Banana::$feed_updateOnDemand) { |
90 | $feed->update(); |
91 | } |
92 | return $feed; |
93 | } |
94 | |
95 | /** Return the cache file name |
96 | */ |
97 | static private function filename() |
98 | { |
99 | $file = Banana::$spool_root . '/' . Banana::$protocole->name() . '/'; |
100 | if (!is_dir($file)) { |
101 | mkdir($file); |
102 | } |
103 | return $file . Banana::$protocole->filename() . '_feed'; |
104 | } |
105 | |
106 | /** Read a feed from a cache file |
107 | */ |
108 | static private function &readFromFile() |
109 | { |
110 | $feed = null; |
111 | $file = BananaFeed::filename(); |
112 | if (!file_exists($file)) { |
113 | return $feed; |
114 | } |
115 | $feed = unserialize(file_get_contents($file)); |
116 | if ($feed->version != BANANA_FEED_VERSION) { |
117 | $feed = null; |
118 | } |
119 | return $feed; |
120 | } |
121 | |
122 | /** Write a feed to a cache file |
123 | */ |
124 | private function writeToFile() |
125 | { |
126 | $file = BananaFeed::filename(); |
127 | file_put_contents($file, serialize($this)); |
128 | } |
129 | |
130 | /** Merge to feeds into a new one |
131 | */ |
132 | static public function &merge(&$feed1, &$feed2, $name, $description = null) |
133 | { |
134 | if (!$feed1) { |
135 | $feed = null; |
136 | $feed1 =& $feed2; |
137 | $feed2 =& $feed; |
138 | } |
139 | if ($feed1->group == $name) { |
140 | $master =& $feed1; |
141 | $slave =& $feed2; |
142 | } else if ($feed2 && $feed2->group == $name) { |
143 | $master =& $feed2; |
144 | $slave =& $feed1; |
145 | } else { |
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; |
152 | } |
153 | $slave =& $feed2; |
154 | } |
155 | if (!$slave) { |
156 | return $master; |
157 | } |
158 | $messages = array(); |
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); |
166 | } else { |
167 | $messages[key($master->messages)] = $m1; |
168 | $m1 = next($master->messages); |
169 | } |
170 | } |
171 | $master->messages =& $messages; |
172 | return $master; |
173 | } |
174 | |
175 | /** Generate the feed xml |
176 | */ |
177 | public function toXML() |
178 | { |
179 | Banana::$page->assign_by_ref('feed', $this); |
180 | return Banana::$page->feed(); |
181 | } |
182 | } |
183 | |
184 | // vim:set et sw=4 sts=4 ts=4 enc=utf-8: |
185 | ?> |