Fixes deprecated features in PHP 5.3.x.
[banana.git] / banana / feed.inc.php
CommitLineData
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
10require_once dirname(__FILE__) . '/banana.inc.php';
11
dcb7f48d 12define('BANANA_FEED_VERSION', '0.1.1');
e02edbfe 13
14class 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
cd58d954 35 /** Last update time
36 */
37 public $lastupdate = 0;
38
dcb7f48d 39 /** Path where the feed is stored
40 */
41 public $path = null;
42
e02edbfe 43 /** Create an empty feed
44 */
45 private function __construct()
46 {
47 $this->version = BANANA_FEED_VERSION;
48 $this->group = Banana::$group;
cd58d954 49 $this->description = trim(Banana::$protocole->getDescription());
e02edbfe 50 }
51
52 /** Update the feed, using current settings of Banana
53 * Calling this function suppose that Banana::$spool is the spool of the current box
54 */
55 public function update()
56 {
57 if (!Banana::$spool || Banana::$spool->group != $this->group) {
58 return false;
59 }
954b9478 60 if (empty(Banana::$spool->overview)) {
e02edbfe 61 $spool_indexes = array();
62 } else {
954b9478 63 $spool_indexes = array_keys(Banana::$spool->overview);
e02edbfe 64 sort($spool_indexes, SORT_NUMERIC);
65 $spool_indexes = array_slice($spool_indexes, -Banana::$feed_size, Banana::$feed_size);
954b9478 66 }
e02edbfe 67 $feed_indexes = array_keys($this->messages);
68 $old = array_diff($feed_indexes, $spool_indexes);
69 foreach ($old as $key) {
70 unset($this->messages[$key]);
71 }
72 $new = array_diff($spool_indexes, $feed_indexes);
73 foreach ($new as $key) {
74 $message =& Banana::$protocole->getMessage($key);
954b9478 75 if (is_null($message)) {
76 return;
77 }
e02edbfe 78 $array = array();
79 $array['author'] = $message->getAuthorName();
80 $array['date'] = $message->getHeaderValue('Date');
81 $array['title'] = $message->getHeaderValue('Subject');
579ce5c9 82 $array['body'] = $message->getFormattedBody();
e02edbfe 83 $array['link'] = Banana::$page->makeUrl(array('group' => $this->group, 'artid' => $key));
84 if (Banana::$protocole->canSend()) {
85 $array['reply'] = Banana::$page->makeUrl(array('group' => $this->group, 'artid' => $key, 'action' => 'new'));
86 }
87 $this->messages[$key] = $array;
88 }
8879bc6c 89 uasort($this->messages, Array('BananaFeed', 'compare'));
cd58d954 90 $this->lastupdate = time();
d3b026ab 91 $this->saveToFile();
e02edbfe 92 }
93
94 /** Get the spool corresponding with the current settings of Banana
95 */
96 static public function &getFeed()
97 {
98 $feed =& BananaFeed::readFromFile();
99 if (!$feed) {
100 $feed = new BananaFeed();
101 }
102 if (Banana::$feed_updateOnDemand) {
103 $feed->update();
104 }
105 return $feed;
106 }
107
108 /** Return the cache file name
109 */
110 static private function filename()
111 {
dcb7f48d 112 return BananaSpool::getPath('feed');
e02edbfe 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 */
d3b026ab 133 private function saveToFile()
e02edbfe 134 {
135 $file = BananaFeed::filename();
136 file_put_contents($file, serialize($this));
137 }
138
139 /** Merge to feeds into a new one
140 */
8a30c7d6 141 static public function &merge($feed1, $feed2, $name, $description = null)
e02edbfe 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();
8879bc6c 168 $m1 = end($master->messages);
169 $m2 = end($slave->messages);
e02edbfe 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;
8879bc6c 174 $m2 = prev($slave->messages);
e02edbfe 175 } else {
176 $messages[key($master->messages)] = $m1;
8879bc6c 177 $m1 = prev($master->messages);
e02edbfe 178 }
179 }
8879bc6c 180 uasort($messages, array('BananaFeed', 'compare'));
e02edbfe 181 $master->messages =& $messages;
cd58d954 182 $master->lastupdate = time();
e02edbfe 183 return $master;
184 }
185
8879bc6c 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
e02edbfe 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?>