fold/unfold threads with javascript
[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 $spool = null;
120 $file = BananaSpool::spoolFilename($group);
121 if (!file_exists($file)) {
122 return $spool;
123 }
124 $spool = unserialize(file_get_contents($file));
125 if ($spool->version != BANANA_SPOOL_VERSION || $spool->mode != Banana::SPOOL_ALL) {
126 $spool = null;
127 return $spool;
128 }
129 $spool->markAllAsRead();
130 return $spool;
131 }
132
133 private function compare($a, $b)
134 {
135 return ($b->date >= $a->date);
136 }
137
138 private function saveToFile()
139 {
140 $file = BananaSpool::spoolFilename($this->group);
141 uasort($this->overview, array($this, 'compare'));
142
143 $this->roots = Array();
144 foreach($this->overview as $id=>$msg) {
145 if (is_null($msg->parent)) {
146 $this->roots[] = $id;
147 }
148 }
149
150 if ($this->mode == Banana::SPOOL_ALL) {
151 file_put_contents($file, serialize($this));
152 }
153 }
154
155 private function build()
156 {
157 $threshold = 0;
158
159 // Compute the range of indexes
160 list($msgnum, $first, $last) = Banana::$protocole->getIndexes();
161 if ($last < $first) {
162 $threshold = $firt + $msgnum - $last;
163 $threshold = (int)(log($threshold)/log(2));
164 $threshold = (2 ^ ($threshold + 1)) - 1;
165 }
166 if (Banana::$spool_max && Banana::$spool_max < $msgnum) {
167 $first = $last - Banana::$spool_max;
168 if ($first < 0) {
169 $first += $threshold;
170 }
171 }
172 $clean = $this->clean($first, $last, $msgnum);
173 $update = $this->update($first, $last, $msgnum);
174
175 if ($clean || $update) {
176 $this->saveToFile();
177 }
178 }
179
180 private function clean(&$first, &$last, $msgnum)
181 {
182 $do_save = false;
183 if (is_array($this->overview)) {
184 $mids = array_keys($this->overview);
185 foreach ($mids as $id) {
186 if (($first <= $last && ($id < $first || $id > $last))
187 || ($first > $last && $id < $first && $id > $last)) {
188 $this->delid($id, false);
189 $do_save = true;
190 }
191 }
192 if (!empty($this->overview)) {
193 $first = max(array_keys($this->overview))+1;
194 }
195 }
196 return $do_save;
197 }
198
199 private function update(&$first, &$last, $msgnum)
200 {
201 if ($first > $last || !$msgnum) {
202 return false;
203 }
204
205 $messages =& Banana::$protocole->getMessageHeaders($first, $last,
206 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
207
208 if (!is_array($this->ids)) {
209 $this->ids = array();
210 }
211 foreach ($messages as $id=>&$message) {
212 $this->ids[$message['message-id']] = $id;
213 }
214
215 if (!is_array($this->overview)) {
216 $this->overview = array();
217 }
218 foreach ($messages as $id=>&$message) {
219 if (!isset($this->overview[$id])) {
220 $this->overview[$id] = new BananaSpoolHead($message);
221 }
222 $msg =& $this->overview[$id];
223 $msgrefs = BananaMessage::formatReferences($message);
224 $parents = preg_grep('/^\d+$/', $msgrefs);
225 $msg->parent = array_pop($parents);
226 $msg->parent_direct = preg_match('/^\d+$/', array_pop($msgrefs));
227
228 if (!is_null($p = $msg->parent)) {
229 if (empty($this->overview[$p])) {
230 $this->overview[$p] = new BananaSpoolHead($messages[$p]);
231 }
232 $this->overview[$p]->children[] = $id;
233
234 while (!is_null($p)) {
235 $this->overview[$p]->desc += $msg->desc;
236 if ($p != $this->overview[$p]->parent) {
237 $p = $this->overview[$p]->parent;
238 } else {
239 $p = null;
240 }
241 }
242 }
243 }
244 Banana::$protocole->updateSpool($messages);
245 return true;
246 }
247
248 public function updateUnread($since)
249 {
250 if (empty($since)) {
251 return;
252 }
253
254 $newpostsids = Banana::$protocole->getNewIndexes($since);
255
256 if (empty($newpostsids)) {
257 return;
258 }
259
260 if (!is_array($this->ids)) {
261 $this->ids = array();
262 }
263 $newpostsids = array_intersect($newpostsids, array_keys($this->ids));
264 foreach ($newpostsids as $mid) {
265 $id = $this->ids[$mid];
266 if ($this->overview[$id]->isread) {
267 $this->overview[$id]->isread = false;
268 $this->unreadnb++;
269 while (isset($id)) {
270 $this->overview[$id]->descunread++;
271 $id = $this->overview[$id]->parent;
272 }
273 }
274 }
275 }
276
277 public function setMode($mode)
278 {
279 $this->mode = $mode;
280 switch ($mode) {
281 case Banana::SPOOL_UNREAD:
282 foreach ($this->roots as $k=>$i) {
283 if ($this->overview[$i]->descunread == 0) {
284 $this->killdesc($i);
285 unset($this->roots[$k]);
286 }
287 }
288 break;
289 }
290 }
291
292 /** Mark the given id as read
293 * @param id MSGNUM of post
294 */
295 public function markAsRead($id)
296 {
297 if (!$this->overview[$id]->isread) {
298 $this->overview[$id]->isread = true;
299 $this->unreadnb--;
300 while (isset($id)) {
301 $this->overview[$id]->descunread--;
302 $id = $this->overview[$id]->parent;
303 }
304 }
305 }
306
307 /** Mark all unread messages as read
308 */
309 public function markAllAsRead(array &$array = null)
310 {
311 if (!$this->unreadnb) {
312 return;
313 }
314 if (is_null($array) && is_array($this->roots)) {
315 $array =& $this->roots;
316 } elseif (is_null($array)) {
317 return;
318 }
319 foreach ($array as $id) {
320 if (!$this->overview[$id]->isread) {
321 $this->markAsRead($id);
322 if (!$this->unreadnb) {
323 return;
324 }
325 }
326 if ($this->overview[$id]->descunread) {
327 $this->markAllAsRead($this->overview[$id]->children);
328 }
329 }
330 }
331
332 /** kill post and childrens
333 * @param $_id MSGNUM of post
334 */
335 private function killdesc($_id)
336 {
337 if (sizeof($this->overview[$_id]->children)) {
338 foreach ($this->overview[$_id]->children as $c) {
339 $this->killdesc($c);
340 }
341 }
342 unset($this->overview[$_id]);
343 if (($msgid = array_search($_id, $this->ids)) !== false) {
344 unset($this->ids[$msgid]);
345 }
346 }
347
348 /** delete a post from overview
349 * @param $_id MSGNUM of post
350 */
351 public function delid($_id, $write = true)
352 {
353 if (isset($this->overview[$_id])) {
354 $overview =& $this->overview[$_id];
355 if (!$overview->isread) {
356 $this->markAsRead($_id);
357 }
358 if ($overview->parent) {
359 $p = $overview->parent;
360 $parent =& $this->overview[$p];
361 $parent->children = array_diff($parent->children, array($_id));
362 if (sizeof($overview->children)) {
363 $parent->children = array_merge($parent->children, $overview->children);
364 foreach ($overview->children as $c) {
365 $this->overview[$c]->parent = $p;
366 $this->overview[$c]->parent_direct = false;
367 }
368 }
369 while (isset($p)) {
370 $this->overview[$p]->desc--;
371 $p = $this->overview[$p]->parent;
372 }
373 } elseif ($overview->children) {
374 foreach ($overview->children as $c) {
375 $this->overview[$c]->parent = null;
376 }
377 }
378 unset($overview);
379 unset($this->overview[$_id]);
380 $msgid = array_search($_id, $this->ids);
381 if ($msgid !== false) {
382 unset($this->ids[$msgid]);
383 }
384 $msgid = array_search($_id, $this->roots);
385 if ($msgid !== false) {
386 unset($this->roots[$msgid]);
387 }
388
389 if ($write) {
390 $this->saveToFile();
391 }
392 }
393 }
394
395 private function formatDate($stamp)
396 {
397 $today = intval(time() / (24*3600));
398 $dday = intval($stamp / (24*3600));
399
400 if ($today == $dday) {
401 $format = "%H:%M";
402 } elseif ($today == 1 + $dday) {
403 $format = _b_('hier')." %H:%M";
404 } elseif ($today < 7 + $dday) {
405 $format = '%a %H:%M';
406 } else {
407 $format = '%a %e %b';
408 }
409 return strftime($format, $stamp);
410 }
411
412 /** displays children tree of a post
413 * @param $_id INTEGER MSGNUM of post
414 * @param $_index INTEGER linear number of post in the tree
415 * @param $_first INTEGER linear number of first post displayed
416 * @param $_last INTEGER linear number of last post displayed
417 * @param $_ref STRING MSGNUM of current post
418 * @param $_pfx_node STRING prefix used for current node
419 * @param $_pfx_end STRING prefix used for children of current node
420 * @param $_head BOOLEAN true if first post in thread
421 *
422 * If you want to analyse subject, you can define the function hook_formatDisplayHeader
423 */
424 private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true, $_pfx_id="")
425 {
426 static $spfx_f, $spfx_n, $spfx_Tnd, $spfx_Lnd, $spfx_snd, $spfx_T, $spfx_L, $spfx_s, $spfx_e, $spfx_I;
427 if (!isset($spfx_f)) {
428 $spfx_f = Banana::$page->makeImg(Array('img' => 'k1', 'alt' => 'o', 'height' => 21, 'width' => 9));
429 $spfx_n = Banana::$page->makeImg(Array('img' => 'k2', 'alt' => '*', 'height' => 21, 'width' => 9));
430 $spfx_Tnd = Banana::$page->makeImg(Array('img' => 'T-direct', 'alt' => '+', 'height' => 21, 'width' => 12));
431 $spfx_Lnd = Banana::$page->makeImg(Array('img' => 'L-direct', 'alt' => '`', 'height' => 21, 'width' => 12));
432 $spfx_snd = Banana::$page->makeImg(Array('img' => 's-direct', 'alt' => '-', 'height' => 21, 'width' => 5));
433 $spfx_T = Banana::$page->makeImg(Array('img' => 'T', 'alt' => '+', 'height' => 21, 'width' => 12));
434 $spfx_L = Banana::$page->makeImg(Array('img' => 'L', 'alt' => '`', 'height' => 21, 'width' => 12));
435 $spfx_s = Banana::$page->makeImg(Array('img' => 's', 'alt' => '-', 'height' => 21, 'width' => 5));
436 $spfx_e = Banana::$page->makeImg(Array('img' => 'e', 'alt' => '&nbsp;', 'height' => 21, 'width' => 12));
437 $spfx_I = Banana::$page->makeImg(Array('img' => 'I', 'alt' => '|', 'height' => 21, 'width' => 12));
438 }
439
440 $overview =& $this->overview[$_id];
441 if ($_index + $overview->desc < $_first || $_index > $_last) {
442 return '';
443 }
444
445 $res = '';
446 if ($_index >= $_first) {
447 $hc = empty($overview->children);
448
449 $res .= '<tr id="'.$_pfx_id.$_id.'" class="' . ($_index%2 ? 'pair' : 'impair') . ($overview->isread ? '' : ' new') . "\">\n";
450 $res .= '<td class="date">' . $this->formatDate($overview->date) . " </td>\n";
451 $res .= '<td class="subj' . ($_index == $_ref ? ' cur' : '') . '"><div class="tree">'
452 . $_pfx_node .($hc ? ($_head ? $spfx_f : ($overview->parent_direct ? $spfx_s : $spfx_snd)) : $spfx_n)
453 . '</div>';
454 $subject = $overview->subject;
455 if (function_exists('hook_formatDisplayHeader')) {
456 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true);
457 } else {
458 $subject = banana_catchFormats(banana_htmlentities(stripslashes($subject)));
459 $link = null;
460 }
461 if (empty($subject)) {
462 $subject = _b_('(pas de sujet)');
463 }
464 if ($_index != $_ref) {
465 $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $_id,
466 'text' => $subject, 'popup' => $subject));
467 }
468 $res .= '&nbsp;' . $subject . $link;
469 $res .= "</td>\n<td class='from'>" . BananaMessage::formatFrom($overview->from) . "</td>\n</tr>";
470
471 if ($hc) {
472 return $res;
473 }
474 }
475
476 $_index ++;
477 $children = $overview->children;
478 while ($child = array_shift($children)) {
479 $overview =& $this->overview[$child];
480 if ($_index > $_last) {
481 return $res;
482 }
483 if ($_index + $overview->desc >= $_first) {
484 if (sizeof($children)) {
485 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
486 $_pfx_end . ($overview->parent_direct ? $spfx_T : $spfx_Tnd),
487 $_pfx_end . $spfx_I, false,$_id.'_');
488 } else {
489 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
490 $_pfx_end . ($overview->parent_direct ? $spfx_L : $spfx_Lnd),
491 $_pfx_end . $spfx_e, false,$_id.'_');
492 }
493 }
494 $_index += $overview->desc;
495 }
496
497 return $res;
498 }
499
500 /** Displays overview
501 * @param $_first INTEGER MSGNUM of first post
502 * @param $_last INTEGER MSGNUM of last post
503 * @param $_ref STRING MSGNUM of current/selectionned post
504 */
505 public function toHtml($first = 0, $overview = false)
506 {
507 $res = Banana::$page->makeJs('jquery');
508 $res .= Banana::$page->makeJs('spool_toggle');
509
510 if (!$overview) {
511 $_first = $first;
512 $_last = $first + Banana::$spool_tmax - 1;
513 $_ref = null;
514 } else {
515 $_ref = $this->getNdx($first);
516 $_last = $_ref + Banana::$spool_tafter;
517 $_first = $_ref - Banana::$spool_tbefore;
518 if ($_first < 0) {
519 $_last -= $_first;
520 }
521 }
522 $index = 1;
523 foreach ($this->roots as $id) {
524 $res .= $this->_to_html($id, $index, $_first, $_last, $_ref);
525 $index += $this->overview[$id]->desc ;
526 if ($index > $_last) {
527 break;
528 }
529 }
530 return $res;
531 }
532
533 /** computes linear post index
534 * @param $_id INTEGER MSGNUM of post
535 * @return INTEGER linear index of post
536 */
537 public function getNdX($_id)
538 {
539 $ndx = 1;
540 $id_cur = $_id;
541 while (true) {
542 $id_parent = $this->overview[$id_cur]->parent;
543 if (is_null($id_parent)) break;
544 $pos = array_search($id_cur, $this->overview[$id_parent]->children);
545
546 for ($i = 0; $i < $pos ; $i++) {
547 $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc;
548 }
549 $ndx++; //noeud père
550
551 $id_cur = $id_parent;
552 }
553
554 foreach ($this->roots as $i) {
555 if ($i==$id_cur) {
556 break;
557 }
558 $ndx += $this->overview[$i]->desc;
559 }
560 return $ndx;
561 }
562
563 /** Return root message of the given thread
564 * @param id INTEGER id of a message
565 */
566 public function root($id)
567 {
568 $id_cur = $id;
569 while (true) {
570 $id_parent = $this->overview[$id_cur]->parent;
571 if (is_null($id_parent)) break;
572 $id_cur = $id_parent;
573 }
574 return $id_cur;
575 }
576
577 /** Return the last post id with the given subject
578 * @param subject
579 */
580 public function getPostId($subject)
581 {
582 $subject = trim($subject);
583 $id = max(array_keys($this->overview));
584 while (isset($this->overview[$id])) {
585 $test = $this->overview[$id]->subject;
586 if (function_exists('hook_formatDisplayHeader')) {
587 $val = hook_formatDisplayHeader('subject', $test, true);
588 if (is_array($val)) {
589 $test = $val[0];
590 } else {
591 $test = $val;
592 }
593 }
594 $test = trim($test);
595 if ($test == $subject) {
596 return $id;
597 }
598 $id--;
599 }
600 return -1;
601 }
602
603 /** Returns previous thread root index
604 * @param id INTEGER message number
605 */
606 public function prevThread($id)
607 {
608 $root = $this->root($id);
609 $last = null;
610 foreach ($this->roots as $i) {
611 if ($i == $root) {
612 return $last;
613 }
614 $last = $i;
615 }
616 return $last;
617 }
618
619 /** Returns next thread root index
620 * @param id INTEGER message number
621 */
622 public function nextThread($id)
623 {
624 $root = $this->root($id);
625 $ok = false;
626 foreach ($this->roots as $i) {
627 if ($ok) {
628 return $i;
629 }
630 if ($i == $root) {
631 $ok = true;
632 }
633 }
634 return null;
635 }
636
637 /** Return prev post in the thread
638 * @param id INTEGER message number
639 */
640 public function prevPost($id)
641 {
642 $parent = $this->overview[$id]->parent;
643 if (is_null($parent)) {
644 return null;
645 }
646 $last = $parent;
647 foreach ($this->overview[$parent]->children as $child) {
648 if ($child == $id) {
649 return $last;
650 }
651 $last = $child;
652 }
653 return null;
654 }
655
656 /** Return next post in the thread
657 * @param id INTEGER message number
658 */
659 public function nextPost($id)
660 {
661 if (count($this->overview[$id]->children) != 0) {
662 return $this->overview[$id]->children[0];
663 }
664
665 $cur = $id;
666 while (true) {
667 $parent = $this->overview[$cur]->parent;
668 if (is_null($parent)) {
669 return null;
670 }
671 $ok = false;
672 foreach ($this->overview[$parent]->children as $child) {
673 if ($ok) {
674 return $child;
675 }
676 if ($child == $cur) {
677 $ok = true;
678 }
679 }
680 $cur = $parent;
681 }
682 return null;
683 }
684
685 /** Look for an unread message in the thread rooted by the message
686 * @param id INTEGER message number
687 */
688 private function _nextUnread($id)
689 {
690 if (!$this->overview[$id]->isread) {
691 return $id;
692 }
693 foreach ($this->overview[$id]->children as $child) {
694 $unread = $this->_nextUnread($child);
695 if (!is_null($unread)) {
696 return $unread;
697 }
698 }
699 return null;
700 }
701
702 /** Find next unread message
703 * @param id INTEGER message number
704 */
705 public function nextUnread($id = null)
706 {
707 if (!$this->unreadnb) {
708 return null;
709 }
710
711 if (!is_null($id)) {
712 // Look in message children
713 foreach ($this->overview[$id]->children as $child) {
714 $next = $this->_nextUnread($child);
715 if (!is_null($next)) {
716 return $next;
717 }
718 }
719 }
720
721 // Look in current thread
722 $cur = $id;
723 do {
724 $parent = is_null($cur) ? null : $this->overview[$cur]->parent;
725 $ok = is_null($cur) ? true : false;
726 if (!is_null($parent)) {
727 $array = &$this->overview[$parent]->children;
728 } else {
729 $array = &$this->roots;
730 }
731 foreach ($array as $child) {
732 if ($ok) {
733 $next = $this->_nextUnread($child);
734 if (!is_null($next)) {
735 return $next;
736 }
737 }
738 if ($child == $cur) {
739 $ok = true;
740 }
741 }
742 $cur = $parent;
743 } while(!is_null($cur));
744 return null;
745 }
746 }
747
748 // vim:set et sw=4 sts=4 ts=4 enc=utf-8:
749 ?>