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