Some improvements, and fix spoolgen.php
[banana.git] / banana / spool.inc.php
CommitLineData
810ac1df 1<?php
2/********************************************************************************
3* include/spool.inc.php : spool subroutines
4* -----------------------
5*
6* This file is part of the banana distribution
7* Copyright: See COPYING files that comes with this distribution
8********************************************************************************/
9
7027794f 10require_once dirname(__FILE__) . '/banana.inc.php';
3ca86dfe 11
7027794f 12define('BANANA_SPOOL_VERSION', '0.3');
01681efd 13
810ac1df 14/** Class spoolhead
15 * class used in thread overviews
16 */
d4c19591 17class BananaSpoolHead
e785d91c 18{
19 /** date (timestamp) */
7027794f 20 public $date;
e785d91c 21 /** subject */
7027794f 22 public $subject;
e785d91c 23 /** author */
7027794f 24 public $from;
e785d91c 25 /** reference of parent */
7027794f 26 public $parent = null;
e785d91c 27 /** paren is direct */
7027794f 28 public $parent_direct;
e785d91c 29 /** array of children */
7027794f 30 public $children = Array();
e785d91c 31 /** true if post is read */
7027794f 32 public $isread;
e785d91c 33 /** number of posts deeper in this branch of tree */
7027794f 34 public $desc;
e785d91c 35 /** same as desc, but counts only unread posts */
7027794f 36 public $descunread;
37
38 /** storage data */
39 public $storage = array();
810ac1df 40
e785d91c 41 /** constructor
42 * @param $_date INTEGER timestamp of post
43 * @param $_subject STRING subject of post
44 * @param $_from STRING author of post
45 * @param $_desc INTEGER desc value (1 for a new post)
46 * @param $_read BOOLEAN true if read
47 * @param $_descunread INTEGER descunread value (0 for a new post)
48 */
7027794f 49 public function __construct(array &$message)
e785d91c 50 {
7027794f 51 $this->date = $message['date'];
52 $this->subject = stripslashes($message['subject']);
53 $this->from = $message['from'];
54 $this->desc = 1;
55 $this->isread = true;
56 $this->descunread = 0;
e785d91c 57 }
810ac1df 58}
59
3ca86dfe 60
d4c19591 61class BananaSpool
e785d91c 62{
7027794f 63 private $version;
64
e785d91c 65 /** group name */
7027794f 66 public $group;
67 /** spool */
68 public $overview;
e785d91c 69 /** array msgid => msgnum */
7027794f 70 public $ids;
cced14b6 71 /** thread starts */
7027794f 72 public $roots;
73
810ac1df 74
e785d91c 75 /** constructor
e785d91c 76 * @param $_group STRING group name
77 * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
78 * @param $_since INTEGER time stamp (used for read/unread)
79 */
7027794f 80 protected function __construct($group)
e785d91c 81 {
7027794f 82 $this->version = BANANA_SPOOL_VERSION;
83 $this->group = $group;
84 }
fb6428c8 85
7027794f 86 public static function getSpool($group, $since = 0)
87 {
88 if (!is_null(Banana::$spool) && Banana::$spool->group == $group) {
89 $spool = Banana::$spool;
82c17a91 90 } else {
7027794f 91 $spool = BananaSpool::readFromFile($group);
92 }
93 if (is_null($spool)) {
94 $spool = new BananaSpool($group);
82c17a91 95 }
7027794f 96 Banana::$spool =& $spool;
97 $spool->build();
98 $spool->updateUnread($since);
99 return $spool;
100 }
e785d91c 101
7027794f 102 private static function spoolFilename($group)
103 {
104 $file = dirname(dirname(__FILE__));
105 $file .= '/spool/' . Banana::$protocole->name() . '/';
106 if (!is_dir($file)) {
107 mkdir($file);
dd7d1c59 108 }
7027794f 109 $url = parse_url(Banana::$host);
110 if (isset($url['host'])) {
111 $file .= $url['host'] . '_';
112 }
113 if (isset($url['port'])) {
114 $file .= $url['port'] . '_';
115 }
116 $file .= $group;
117 return $file;
dd7d1c59 118 }
119
7027794f 120 private static function readFromFile($group)
dd7d1c59 121 {
7027794f 122 $file = BananaSpool::spoolFilename($group);
123 if (!file_exists($file)) {
124 return null;
dd7d1c59 125 }
7027794f 126 $spool = unserialize(file_get_contents($file));
127 if ($spool->version != BANANA_SPOOL_VERSION) {
128 return null;
129 }
130 return $spool;
131 }
132
133 private function compare($a, $b)
134 {
135 return ($b->date >= $a->date);
dd7d1c59 136 }
137
7027794f 138 private function saveToFile()
dd7d1c59 139 {
7027794f 140 $file = BananaSpool::spoolFilename($this->group);
141 uasort($this->overview, array($this, 'compare'));
dd7d1c59 142
143 $this->roots = Array();
144 foreach($this->overview as $id=>$msg) {
145 if (is_null($msg->parent)) {
146 $this->roots[] = $id;
147 }
148 }
2c606d23 149
dd7d1c59 150 file_put_contents($file, serialize($this));
151 }
152
7027794f 153 private function build()
154 {
155 $threshold = 0;
156
157 // Compute the range of indexes
158 list($msgnum, $first, $last) = Banana::$protocole->getIndexes();
159 if ($last < $first) {
160 $threshold = $firt + $msgnum - $last;
161 $threshold = (int)(log($threshold)/log(2));
162 $threshold = (2 ^ ($threshold + 1)) - 1;
163 }
164 if (Banana::$maxspool && Banana::$maxspool < $msgnum) {
165 $first = $last - Banana::$maxspool;
166 if ($first < 0) {
167 $first += $threshold;
168 }
169 }
170 $clean = $this->clean($first, $last, $msgnum);
171 $update = $this->update($first, $last, $msgnum);
172
173 if ($clean || $update) {
174 $this->saveToFile();
175 }
176 }
177
178 private function clean(&$first, &$last, $msgnum)
9090c673 179 {
7027794f 180 $do_save = false;
181 if (is_array($this->overview)) {
182 $mids = array_keys($this->overview);
183 foreach ($mids as $id) {
184 if (($first <= $last && ($id < $first || $id > $last))
185 || ($first > $last && $id < $first && $id > $last))
186 {
187 $this->delid($id, false);
188 $do_save = true;
189 }
190 }
191 if (!empty($this->overview)) {
192 $first = max(array_keys($this->overview))+1;
193 }
194 }
195 return $do_save;
9090c673
PHM
196 }
197
7027794f 198 private function update(&$first, &$last, $msgnum)
dd7d1c59 199 {
7027794f 200 if ($first >= $last || !$msgnum) {
201 return false;
dd7d1c59 202 }
203
7027794f 204 $messages =& Banana::$protocole->getMessageHeaders($first, $last,
205 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
dd7d1c59 206
7027794f 207 if (!is_array($this->ids)) {
208 $this->ids = array();
209 }
210 foreach ($messages as $id=>&$message) {
211 $this->ids[$message['message-id']] = $id;
212 }
213
214 foreach ($messages as $id=>&$message) {
215 if (!isset($this->overview[$id])) {
216 $this->overview[$id] = new BananaSpoolHead($message);
3316e34e 217 }
7027794f 218 $msg =& $this->overview[$id];
219 $msgrefs = BananaMessage::formatReferences($message);
220 $parents = preg_grep('/^\d+$/', $msgrefs);
221 $msg->parent = array_pop($parents);
222 $msg->parent_direct = preg_match('/^\d+$/', array_pop($msgrefs));
e785d91c 223
7027794f 224 if (!is_null($p = $msg->parent)) {
dd7d1c59 225 if (empty($this->overview[$p])) {
7027794f 226 $this->overview[$p] = new BananaSpoolHead($messages[$p]);
4ced5065 227 }
dd7d1c59 228 $this->overview[$p]->children[] = $id;
4ced5065 229
7027794f 230 while (!is_null($p)) {
dd7d1c59 231 $this->overview[$p]->desc += $msg->desc;
7027794f 232 if ($p != $this->overview[$p]->parent) {
233 $p = $this->overview[$p]->parent;
234 } else {
235 $p = null;
236 }
e785d91c 237 }
810ac1df 238 }
810ac1df 239 }
7027794f 240 Banana::$protocole->updateSpool($messages);
241 return true;
dd7d1c59 242 }
e785d91c 243
7027794f 244 private function updateUnread($since)
75ff2f64 245 {
7027794f 246 if (empty($since)) {
247 return;
248 }
249
250 $newpostsids = Banana::$protocole->getNewIndexes($since);
251
252 if (empty($newpostsids)) {
253 return;
254 }
255
256 if (!is_array($this->ids)) {
257 $this->ids = array();
258 }
259 $newpostsids = array_intersect($newpostsids, array_keys($this->ids));
260 foreach ($newpostsids as $mid) {
261 $id = $this->ids[$mid];
262 if ($this->overview[$id]->isread) {
263 $this->overview[$id]->isread = false;
264 $this->overview[$id]->descunread = 1;
265 while (isset($id)) {
266 $this->overview[$id]->descunread ++;
267 $id = $this->overview[$id]->parent;
e785d91c 268 }
810ac1df 269 }
7027794f 270 }
271 }
dd7d1c59 272
7027794f 273 public function setMode($mode)
274 {
275 switch ($mode) {
276 case Banana::SPOOL_UNREAD:
277 foreach ($this->roots as $k=>$i) {
278 if ($this->overview[$i]->descunread == 0) {
279 $this->killdesc($i);
280 unset($this->roots[$k]);
e785d91c 281 }
810ac1df 282 }
7027794f 283 break;
810ac1df 284 }
cced14b6 285 }
286
e785d91c 287 /** kill post and childrens
288 * @param $_id MSGNUM of post
289 */
7027794f 290 private function killdesc($_id)
e785d91c 291 {
292 if (sizeof($this->overview[$_id]->children)) {
293 foreach ($this->overview[$_id]->children as $c) {
294 $this->killdesc($c);
295 }
296 }
297 unset($this->overview[$_id]);
dd7d1c59 298 if (($msgid = array_search($_id, $this->ids)) !== false) {
e785d91c 299 unset($this->ids[$msgid]);
300 }
810ac1df 301 }
e785d91c 302
303 /** delete a post from overview
304 * @param $_id MSGNUM of post
305 */
7027794f 306 public function delid($_id, $write = true)
e785d91c 307 {
308 if (isset($this->overview[$_id])) {
309 if (sizeof($this->overview[$_id]->parent)) {
310 $this->overview[$this->overview[$_id]->parent]->children =
311 array_diff($this->overview[$this->overview[$_id]->parent]->children, array($_id));
312 if (sizeof($this->overview[$_id]->children)) {
313 $this->overview[$this->overview[$_id]->parent]->children =
314 array_merge($this->overview[$this->overview[$_id]->parent]->children, $this->overview[$_id]->children);
315 foreach ($this->overview[$_id]->children as $c) {
316 $this->overview[$c]->parent = $this->overview[$_id]->parent;
317 $this->overview[$c]->parent_direct = false;
318 }
319 }
320 $p = $this->overview[$_id]->parent;
321 while ($p) {
322 $this->overview[$p]->desc--;
323 $p = $this->overview[$p]->parent;
324 }
325 } elseif (sizeof($this->overview[$_id]->children)) {
326 foreach ($this->overview[$_id]->children as $c) {
327 $this->overview[$c]->parent = null;
328 }
329 }
330 unset($this->overview[$_id]);
3ca86dfe 331 $msgid = array_search($_id, $this->ids);
e785d91c 332 if ($msgid) {
333 unset($this->ids[$msgid]);
334 }
335
7027794f 336 if ($write) {
337 $this->saveToFile();
338 }
e785d91c 339 }
35ca8036 340 }
810ac1df 341
7027794f 342 private function formatDate($stamp)
343 {
344 $today = intval(time() / (24*3600));
345 $dday = intval($stamp / (24*3600));
346
347 if ($today == $dday) {
348 $format = "%H:%M";
349 } elseif ($today == 1 + $dday) {
350 $format = _b_('hier')." %H:%M";
351 } elseif ($today < 7 + $dday) {
352 $format = '%a %H:%M';
353 } else {
354 $format = '%a %e %b';
355 }
356 return utf8_encode(strftime($format, $stamp));
357 }
358
e785d91c 359 /** displays children tree of a post
360 * @param $_id INTEGER MSGNUM of post
361 * @param $_index INTEGER linear number of post in the tree
362 * @param $_first INTEGER linear number of first post displayed
363 * @param $_last INTEGER linear number of last post displayed
364 * @param $_ref STRING MSGNUM of current post
365 * @param $_pfx_node STRING prefix used for current node
366 * @param $_pfx_end STRING prefix used for children of current node
367 * @param $_head BOOLEAN true if first post in thread
3204d440 368 *
369 * If you want to analyse subject, you can define the function hook_getSubject(&$subject) which
370 * take the subject as a reference parameter, transform this subject to be displaid in the spool
371 * view and return a string. This string will be put after the subject.
e785d91c 372 */
7027794f 373 private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true)
75ff2f64 374 {
7027794f 375 static $spfx_f, $spfx_n, $spfx_Tnd, $spfx_Lnd, $spfx_snd, $spfx_T, $spfx_L, $spfx_s, $spfx_e, $spfx_I;
376 if (!isset($spfx_f)) {
377 $spfx_f = Banana::$page->makeImg(Array('img' => 'k1', 'alt' => 'o', 'height' => 21, 'width' => 9));
378 $spfx_n = Banana::$page->makeImg(Array('img' => 'k2', 'alt' => '*', 'height' => 21, 'width' => 9));
379 $spfx_Tnd = Banana::$page->makeImg(Array('img' => 'T-direct', 'alt' => '+', 'height' => 21, 'width' => 12));
380 $spfx_Lnd = Banana::$page->makeImg(Array('img' => 'L-direct', 'alt' => '`', 'height' => 21, 'width' => 12));
381 $spfx_snd = Banana::$page->makeImg(Array('img' => 's-direct', 'alt' => '-', 'height' => 21, 'width' => 5));
382 $spfx_T = Banana::$page->makeImg(Array('img' => 'T', 'alt' => '+', 'height' => 21, 'width' => 12));
383 $spfx_L = Banana::$page->makeImg(Array('img' => 'L', 'alt' => '`', 'height' => 21, 'width' => 12));
384 $spfx_s = Banana::$page->makeImg(Array('img' => 's', 'alt' => '-', 'height' => 21, 'width' => 5));
385 $spfx_e = Banana::$page->makeImg(Array('img' => 'e', 'alt' => '&nbsp;', 'height' => 21, 'width' => 12));
386 $spfx_I = Banana::$page->makeImg(Array('img' => 'I', 'alt' => '|', 'height' => 21, 'width' => 12));
810ac1df 387 }
e785d91c 388
7027794f 389 $overview =& $this->overview[$_id];
390 if ($_index + $overview->desc < $_first || $_index > $_last) {
391 return '';
392 }
cced14b6 393
7027794f 394 $res = '';
395 if ($_index >= $_first) {
396 $hc = empty($overview->children);
397
398 $res .= '<tr class="' . ($_index%2 ? 'pair' : 'impair') . ($overview->isread ? '' : ' new') . "\">\n";
399 $res .= '<td class="date">' . $this->formatDate($overview->date) . " </td>\n";
400 $res .= '<td class="subj' . ($_index == $_ref ? ' cur' : '') . '">'
401 . $_pfx_node .($hc ? ($_head ? $spfx_f : ($overview->parent_direct ? $spfx_s : $spfx_snd)) : $spfx_n);
402 $subject = $overview->subject;
403 if (empty($subject)) {
79405147 404 $subject = _b_('(pas de sujet)');
405 }
3204d440 406 $link = null;
407 if (function_exists('hook_getSubject')) {
408 $link = hook_getSubject($subject);
409 }
7027794f 410 $subject = banana_catchFormats($subject);
411 if ($_index != $_ref) {
412 $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $_id,
413 'text' => $subject, 'popup' => $subject));
cced14b6 414 }
7027794f 415 $res .= '&nbsp;' . $subject . $link;
416 $res .= "</td>\n<td class='from'>" . BananaMessage::formatFrom($overview->from) . "</td>\n</tr>";
dd7d1c59 417
7027794f 418 if ($hc) {
419 return $res;
420 }
cced14b6 421 }
422
dd7d1c59 423 $_index ++;
7027794f 424 $children = $overview->children;
e785d91c 425 while ($child = array_shift($children)) {
7027794f 426 $overview =& $this->overview[$child];
427 if ($_index > $_last) {
428 return $res;
429 }
430 if ($_index + $overview->desc >= $_first) {
e785d91c 431 if (sizeof($children)) {
65d96b1f 432 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
7027794f 433 $_pfx_end . ($overview->parent_direct ? $spfx_T : $spfx_Tnd),
434 $_pfx_end . $spfx_I, false);
e785d91c 435 } else {
65d96b1f 436 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
7027794f 437 $_pfx_end . ($overview->parent_direct ? $spfx_L : $spfx_Lnd),
438 $_pfx_end . $spfx_e, false);
e785d91c 439 }
440 }
7027794f 441 $_index += $overview->desc;
810ac1df 442 }
65d96b1f 443
444 return $res;
810ac1df 445 }
810ac1df 446
e785d91c 447 /** Displays overview
448 * @param $_first INTEGER MSGNUM of first post
449 * @param $_last INTEGER MSGNUM of last post
450 * @param $_ref STRING MSGNUM of current/selectionned post
451 */
7027794f 452 public function toHtml($first = 0, $overview = false)
75ff2f64 453 {
7027794f 454 $res = '';
455
456 if (!$overview) {
457 $_first = $first;
458 $_last = $first + Banana::$tmax - 1;
459 $_ref = null;
d8e2470c 460 } else {
7027794f 461 $_ref = $this->getNdx($first);
462 $_last = $_ref + Banana::$tafter;
463 $_first = $_ref - Banana::$tbefore;
464 if ($_first < 0) {
465 $_last -= $_first;
466 }
65d96b1f 467 }
e785d91c 468 $index = 1;
7027794f 469 foreach ($this->roots as $id) {
470 $res .= $this->_to_html($id, $index, $_first, $_last, $_ref);
471 $index += $this->overview[$id]->desc ;
472 if ($index > $_last) {
473 break;
e785d91c 474 }
4f75645f 475 }
7027794f 476 return $res;
810ac1df 477 }
810ac1df 478
e785d91c 479 /** computes linear post index
480 * @param $_id INTEGER MSGNUM of post
481 * @return INTEGER linear index of post
482 */
7027794f 483 public function getNdX($_id)
75ff2f64 484 {
cced14b6 485 $ndx = 1;
486 $id_cur = $_id;
4ced5065 487 while (true) {
cced14b6 488 $id_parent = $this->overview[$id_cur]->parent;
4ced5065 489 if (is_null($id_parent)) break;
cced14b6 490 $pos = array_search($id_cur, $this->overview[$id_parent]->children);
4ced5065 491
cced14b6 492 for ($i = 0; $i < $pos ; $i++) {
e785d91c 493 $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc;
494 }
495 $ndx++; //noeud père
cced14b6 496
497 $id_cur = $id_parent;
810ac1df 498 }
cced14b6 499
500 foreach ($this->roots as $i) {
501 if ($i==$id_cur) {
e785d91c 502 break;
503 }
cced14b6 504 $ndx += $this->overview[$i]->desc;
e785d91c 505 }
506 return $ndx;
810ac1df 507 }
d8e2470c 508
509 /** Return root message of the given thread
510 * @param id INTEGER id of a message
511 */
7027794f 512 public function root($id)
513 {
d8e2470c 514 $id_cur = $id;
515 while (true) {
516 $id_parent = $this->overview[$id_cur]->parent;
517 if (is_null($id_parent)) break;
518 $id_cur = $id_parent;
519 }
520 return $id_cur;
521 }
522
523 /** Returns previous thread root index
524 * @param id INTEGER message number
525 */
7027794f 526 public function prevThread($id)
d8e2470c 527 {
528 $root = $this->root($id);
529 $last = null;
530 foreach ($this->roots as $i) {
531 if ($i == $root) {
532 return $last;
533 }
534 $last = $i;
535 }
536 return $last;
537 }
538
539 /** Returns next thread root index
540 * @param id INTEGER message number
541 */
7027794f 542 public function nextThread($id)
d8e2470c 543 {
544 $root = $this->root($id);
545 $ok = false;
546 foreach ($this->roots as $i) {
547 if ($ok) {
548 return $i;
549 }
550 if ($i == $root) {
551 $ok = true;
552 }
553 }
554 return null;
555 }
556
557 /** Return prev post in the thread
558 * @param id INTEGER message number
559 */
7027794f 560 public function prevPost($id)
d8e2470c 561 {
562 $parent = $this->overview[$id]->parent;
563 if (is_null($parent)) {
564 return null;
565 }
566 $last = $parent;
567 foreach ($this->overview[$parent]->children as $child) {
568 if ($child == $id) {
569 return $last;
570 }
571 $last = $child;
572 }
573 return null;
574 }
575
576 /** Return next post in the thread
577 * @param id INTEGER message number
578 */
7027794f 579 public function nextPost($id)
d8e2470c 580 {
581 if (count($this->overview[$id]->children) != 0) {
582 return $this->overview[$id]->children[0];
583 }
584
585 $cur = $id;
586 while (true) {
587 $parent = $this->overview[$cur]->parent;
588 if (is_null($parent)) {
589 return null;
590 }
591 $ok = false;
592 foreach ($this->overview[$parent]->children as $child) {
593 if ($ok) {
594 return $child;
595 }
596 if ($child == $cur) {
597 $ok = true;
598 }
599 }
600 $cur = $parent;
601 }
602 return null;
603 }
d634c13c 604
605 /** Look for an unread message in the thread rooted by the message
606 * @param id INTEGER message number
607 */
7027794f 608 private function _nextUnread($id)
d634c13c 609 {
610 if (!$this->overview[$id]->isread) {
611 return $id;
612 }
613 foreach ($this->overview[$id]->children as $child) {
bdad7c9d 614 $unread = $this->_nextUnread($child);
615 if (!is_null($unread)) {
616 return $unread;
617 }
d634c13c 618 }
619 return null;
620 }
621
622 /** Find next unread message
623 * @param id INTEGER message number
624 */
7027794f 625 public function nextUnread($id = null)
d634c13c 626 {
b7d59a47 627 if (!is_null($id)) {
628 // Look in message children
629 foreach ($this->overview[$id]->children as $child) {
630 $next = $this->_nextUnread($child);
631 if (!is_null($next)) {
632 return $next;
633 }
d634c13c 634 }
635 }
636
637 // Look in current thread
638 $cur = $id;
b7d59a47 639 do {
640 $parent = is_null($cur) ? null : $this->overview[$cur]->parent;
641 $ok = is_null($cur) ? true : false;
1e016f3a 642 if (!is_null($parent)) {
d634c13c 643 $array = &$this->overview[$parent]->children;
644 } else {
645 $array = &$this->roots;
646 }
647 foreach ($array as $child) {
648 if ($ok) {
649 $next = $this->_nextUnread($child);
650 if (!is_null($next)) {
651 return $next;
652 }
653 }
654 if ($child == $cur) {
655 $ok = true;
656 }
657 }
658 $cur = $parent;
b7d59a47 659 } while(!is_null($cur));
d634c13c 660 return null;
661 }
810ac1df 662}
663
d5588318 664// vim:set et sw=4 sts=4 ts=4
810ac1df 665?>