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