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