2ce0755d20b3fb85272a5010c173b67230b1fe26
2 /********************************************************************************
3 * include/spool.inc.php : spool subroutines
4 * -----------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
10 require_once dirname(__FILE__
) . '/banana.inc.php';
12 define('BANANA_SPOOL_VERSION', '0.6');
15 * class used in thread overviews
19 /** date (timestamp) */
26 /** reference of parent */
27 public $parent = null
;
28 /** paren is direct */
29 public $parent_direct;
30 /** array of children */
31 public $children = Array();
32 /** true if post is read */
34 /** number of posts deeper in this branch of tree */
36 /** same as desc, but counts only unread posts */
40 public $storage = array();
43 * @param $_date INTEGER timestamp of post
44 * @param $_subject STRING subject of post
45 * @param $_from STRING author of post
46 * @param $_desc INTEGER desc value (1 for a new post)
47 * @param $_read BOOLEAN true if read
48 * @param $_descunread INTEGER descunread value (0 for a new post)
50 public function __construct(array &$message)
52 $this->date
= $message['date'];
53 $this->subject
= $message['subject'];
54 $this->from
= $message['from'];
55 $this->color
= sprintf('#%06x', abs(crc32($this->from
) %
0xffffff));
58 $this->descunread
= 0;
72 /** array msgid => msgnum */
76 /** protocole specific data */
77 public $storage = array();
79 private $unreadnb = 0;
82 * @param $_group STRING group name
83 * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
84 * @param $_since INTEGER time stamp (used for read/unread)
86 protected function __construct($group)
88 $this->version
= BANANA_SPOOL_VERSION
;
89 $this->mode
= Banana
::SPOOL_ALL
;
90 $this->group
= $group;
93 public static function &getSpool($group, $since = 0, $clean = false
)
95 if (!is_null(Banana
::$spool) && Banana
::$spool->group
== $group) {
96 $spool =& Banana
::$spool;
98 $spool =& BananaSpool
::readFromFile($group);
100 if (is_null($spool)) {
101 $spool = new BananaSpool($group);
103 Banana
::$spool =& $spool;
106 $spool->markAllAsRead();
108 $spool->updateUnread($since);
112 private static function spoolFilename($group)
114 $file = Banana
::$spool_root . '/' . Banana
::$protocole->name() . '/';
115 if (!is_dir($file)) {
118 return $file . Banana
::$protocole->filename();
121 private static function &readFromFile($group)
124 $file = BananaSpool
::spoolFilename($group);
125 if (!file_exists($file)) {
128 $spool = unserialize(file_get_contents($file));
129 if ($spool->version
!= BANANA_SPOOL_VERSION ||
$spool->mode
!= Banana
::SPOOL_ALL
) {
133 $spool->markAllAsRead();
137 private function compare($a, $b)
139 return ($this->overview
[$b]->date
>= $this->overview
[$a]->date
);
142 private function saveToFile()
144 $file = BananaSpool
::spoolFilename($this->group
);
146 $this->roots
= Array();
147 foreach($this->overview
as $id=>&$msg) {
148 if (is_null($msg->parent
)) {
149 $this->roots
[] = $id;
152 usort($this->roots
, array($this, 'compare'));
154 if ($this->mode
== Banana
::SPOOL_ALL
) {
155 file_put_contents($file, serialize($this));
159 private function build()
163 // Compute the range of indexes
164 list($msgnum, $first, $last) = Banana
::$protocole->getIndexes();
165 if ($last < $first) {
166 $threshold = $first +
$msgnum - $last;
167 $threshold = (int)(log($threshold)/log(2));
168 $threshold = (2 ^
($threshold +
1)) - 1;
170 if (Banana
::$spool_max && Banana
::$spool_max < $msgnum) {
171 $first = $last - Banana
::$spool_max;
173 $first +
= $threshold;
176 $clean = $this->clean($first, $last, $msgnum);
177 $update = $this->update($first, $last, $msgnum);
179 if ($clean ||
$update) {
184 private function clean(&$first, &$last, $msgnum)
187 if (is_array($this->overview
)) {
188 $mids = array_keys($this->overview
);
189 foreach ($mids as $id) {
190 if (($first <= $last && ($id < $first ||
$id > $last))
191 ||
($first > $last && $id < $first && $id > $last)) {
192 $this->delid($id, false
);
196 if (!empty($this->overview
)) {
197 $first = max(array_keys($this->overview
))+
1;
203 private function update(&$first, &$last, $msgnum)
205 if ($first > $last ||
!$msgnum) {
209 $messages =& Banana
::$protocole->getMessageHeaders($first, $last,
210 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
212 if (!is_array($this->ids
)) {
213 $this->ids
= array();
215 foreach ($messages as $id=>&$message) {
216 $this->ids
[$message['message-id']] = $id;
219 if (!is_array($this->overview
)) {
220 $this->overview
= array();
222 foreach ($messages as $id=>&$message) {
223 if (!isset($this->overview
[$id])) {
224 $this->overview
[$id] = new BananaSpoolHead($message);
226 $msg =& $this->overview
[$id];
227 $msgrefs = BananaMessage
::formatReferences($message);
228 $parents = preg_grep('/^\d+$/', $msgrefs);
229 $msg->parent
= array_pop($parents);
230 $msg->parent_direct
= preg_match('/^\d+$/', array_pop($msgrefs));
232 if (!is_null($p = $msg->parent
)) {
233 if (empty($this->overview
[$p])) {
234 $this->overview
[$p] = new BananaSpoolHead($messages[$p]);
236 $this->overview
[$p]->children
[] = $id;
238 while (!is_null($p)) {
239 $this->overview
[$p]->desc +
= $msg->desc
;
240 if ($p != $this->overview
[$p]->parent
) {
241 $p = $this->overview
[$p]->parent
;
248 Banana
::$protocole->updateSpool($messages);
252 public function updateUnread($since)
258 $newpostsids = Banana
::$protocole->getNewIndexes($since);
260 if (empty($newpostsids)) {
264 if (!is_array($this->ids
)) {
265 $this->ids
= array();
267 $newpostsids = array_intersect($newpostsids, array_keys($this->ids
));
268 foreach ($newpostsids as $mid) {
269 $id = $this->ids
[$mid];
270 if ($this->overview
[$id]->isread
) {
271 $this->overview
[$id]->isread
= false
;
274 $this->overview
[$id]->descunread++
;
275 $id = $this->overview
[$id]->parent
;
281 public function setMode($mode)
285 case Banana
::SPOOL_UNREAD
:
286 $num = max(array_keys($this->overview
));
287 if ($this->overview
[$num]->isread
) {
290 foreach ($this->roots
as $k=>$i) {
291 if ($this->overview
[$i]->descunread
== 0) {
293 unset($this->roots
[$k]);
300 /** Mark the given id as read
301 * @param id MSGNUM of post
303 public function markAsRead($id)
305 if (!$this->overview
[$id]->isread
) {
306 $this->overview
[$id]->isread
= true
;
309 $this->overview
[$id]->descunread
--;
310 $id = $this->overview
[$id]->parent
;
315 /** Mark all unread messages as read
317 public function markAllAsRead(array &$array = null
)
319 if (!$this->unreadnb
) {
322 if (is_null($array) && is_array($this->roots
)) {
323 $array =& $this->roots
;
324 } elseif (is_null($array)) {
327 foreach ($array as $id) {
328 if (!$this->overview
[$id]->isread
) {
329 $this->markAsRead($id);
330 if (!$this->unreadnb
) {
334 if ($this->overview
[$id]->descunread
) {
335 $this->markAllAsRead($this->overview
[$id]->children
);
340 /** kill post and childrens
341 * @param $_id MSGNUM of post
343 private function killdesc($_id)
345 if (sizeof($this->overview
[$_id]->children
)) {
346 foreach ($this->overview
[$_id]->children
as $c) {
350 unset($this->overview
[$_id]);
351 if (($msgid = array_search($_id, $this->ids
)) !== false
) {
352 unset($this->ids
[$msgid]);
356 /** delete a post from overview
357 * @param $_id MSGNUM of post
359 public function delid($_id, $write = true
)
361 if (isset($this->overview
[$_id])) {
362 $overview =& $this->overview
[$_id];
363 if (!$overview->isread
) {
364 $this->markAsRead($_id);
366 if ($overview->parent
) {
367 $p = $overview->parent
;
368 $parent =& $this->overview
[$p];
369 $parent->children
= array_diff($parent->children
, array($_id));
370 if (sizeof($overview->children
)) {
371 $parent->children
= array_merge($parent->children
, $overview->children
);
372 foreach ($overview->children
as $c) {
373 $this->overview
[$c]->parent
= $p;
374 $this->overview
[$c]->parent_direct
= false
;
378 $this->overview
[$p]->desc
--;
379 $p = $this->overview
[$p]->parent
;
381 } elseif ($overview->children
) {
382 foreach ($overview->children
as $c) {
383 $this->overview
[$c]->parent
= null
;
387 unset($this->overview
[$_id]);
388 $msgid = array_search($_id, $this->ids
);
389 if ($msgid !== false
) {
390 unset($this->ids
[$msgid]);
392 $msgid = array_search($_id, $this->roots
);
393 if ($msgid !== false
) {
394 unset($this->roots
[$msgid]);
403 private function formatDate($stamp)
405 $today = intval(time() / (24*3600));
406 $dday = intval($stamp / (24*3600));
408 if ($today == $dday) {
410 } elseif ($today == 1 +
$dday) {
411 $format = _b_('hier')." %H:%M";
412 } elseif ($today < 7 +
$dday) {
413 $format = '%a %H:%M';
414 } elseif ($today < 90 +
$dday) {
415 $format = '%a %e %b';
417 $format = '%a %e %b %Y';
419 return strftime($format, $stamp);
422 /** displays children tree of a post
423 * @param $_id INTEGER MSGNUM of post
424 * @param $_index INTEGER linear number of post in the tree
425 * @param $_first INTEGER linear number of first post displayed
426 * @param $_last INTEGER linear number of last post displayed
427 * @param $_ref STRING MSGNUM of current post
428 * @param $_pfx_node STRING prefix used for current node
429 * @param $_pfx_end STRING prefix used for children of current node
430 * @param $_head BOOLEAN true if first post in thread
432 * If you want to analyse subject, you can define the function hook_formatDisplayHeader
434 private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true
, $_pfx_id="")
436 static $spfx_f, $spfx_n, $spfx_Tnd, $spfx_Lnd, $spfx_snd, $spfx_T, $spfx_L, $spfx_s, $spfx_e, $spfx_I;
437 if (!isset($spfx_f)) {
438 $spfx_f = Banana
::$page->makeImg(Array('img' => 'k1', 'alt' => 'o', 'height' => 21, 'width' => 9));
439 $spfx_n = Banana
::$page->makeImg(Array('img' => 'k2', 'alt' => '*', 'height' => 21, 'width' => 9));
440 $spfx_Tnd = Banana
::$page->makeImg(Array('img' => 'T-direct', 'alt' => '+', 'height' => 21, 'width' => 12));
441 $spfx_Lnd = Banana
::$page->makeImg(Array('img' => 'L-direct', 'alt' => '`', 'height' => 21, 'width' => 12));
442 $spfx_snd = Banana
::$page->makeImg(Array('img' => 's-direct', 'alt' => '-', 'height' => 21, 'width' => 5));
443 $spfx_T = Banana
::$page->makeImg(Array('img' => 'T', 'alt' => '+', 'height' => 21, 'width' => 12));
444 $spfx_L = Banana
::$page->makeImg(Array('img' => 'L', 'alt' => '`', 'height' => 21, 'width' => 12));
445 $spfx_s = Banana
::$page->makeImg(Array('img' => 's', 'alt' => '-', 'height' => 21, 'width' => 5));
446 $spfx_e = Banana
::$page->makeImg(Array('img' => 'e', 'alt' => ' ', 'height' => 21, 'width' => 12));
447 $spfx_I = Banana
::$page->makeImg(Array('img' => 'I', 'alt' => '|', 'height' => 21, 'width' => 12));
450 $overview =& $this->overview
[$_id];
451 if ($_index +
$overview->desc
< $_first ||
$_index > $_last) {
456 if ($_index >= $_first) {
457 $hc = empty($overview->children
);
459 $res .= '<tr id="'.$_pfx_id.$_id.'" class="' . ($_index%2 ?
'pair' : 'impair') . ($overview->isread ?
'' : ' new') . "\">\n";
460 $res .= '<td class="date">' . $this->formatDate($overview->date
) . " </td>\n";
461 $res .= '<td class="subj' . ($_index == $_ref ?
' cur' : '') . '"><div class="tree">'
462 . $_pfx_node .($hc ?
($_head ?
$spfx_f : ($overview->parent_direct ?
$spfx_s : $spfx_snd)) : $spfx_n)
464 $popup = $subject = $overview->subject
;
465 if (function_exists('hook_formatDisplayHeader')) {
466 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true
);
468 $subject = banana_catchFormats(banana_entities(stripslashes($subject)));
471 if (empty($subject)) {
472 $subject = _b_('(pas de sujet)');
474 if ($_index != $_ref) {
475 $subject = Banana
::$page->makeLink(Array('group' => $this->group
, 'artid' => $_id,
476 'text' => $subject, 'popup' => $popup));
478 $res .= ' ' . $subject . $link;
479 $res .= "</td>\n<td class='from'>" . BananaMessage
::formatFrom($overview->from
) . "</td>\n</tr>";
487 $children = $overview->children
;
488 while ($child = array_shift($children)) {
489 $overview =& $this->overview
[$child];
490 if ($_index > $_last) {
493 if ($_index +
$overview->desc
>= $_first) {
494 if (sizeof($children)) {
495 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
496 $_pfx_end . ($overview->parent_direct ?
$spfx_T : $spfx_Tnd),
497 $_pfx_end . $spfx_I, false
,$_id.'_');
499 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
500 $_pfx_end . ($overview->parent_direct ?
$spfx_L : $spfx_Lnd),
501 $_pfx_end . $spfx_e, false
,$_id.'_');
504 $_index +
= $overview->desc
;
510 /** Displays overview
511 * @param $_first INTEGER MSGNUM of first post
512 * @param $_last INTEGER MSGNUM of last post
513 * @param $_ref STRING MSGNUM of current/selectionned post
515 public function toHtml($first = 0, $overview = false
)
517 $res = Banana
::$page->makeJs('jquery');
518 $res .= Banana
::$page->makeJs('spool_toggle');
522 $_last = $first + Banana
::$spool_tmax - 1;
525 $_ref = $this->getNdx($first);
526 $_last = $_ref + Banana
::$spool_tafter;
527 $_first = $_ref - Banana
::$spool_tbefore;
533 foreach ($this->roots
as $id) {
534 $res .= $this->_to_html($id, $index, $_first, $_last, $_ref);
535 $index +
= $this->overview
[$id]->desc
;
536 if ($index > $_last) {
544 public function _buildTree($id, BananaSpoolHead
&$head, $current) {
545 $style = 'color:' . $head->color
. '; text-decoration: none';
547 if ($id == $current) {
548 $text = "<strong style=\"$style\">0</strong>";
550 $text = Banana
::$page->makeLink(Array('group' => $this->group
, 'artid' => $id,
551 'text' => 'o', 'popup' => $head->from
,
554 $array = array($text);
555 foreach ($head->children
as $key=>&$child) {
556 list($tpr, $tree) = $this->_buildTree($child, $this->overview
[$child], $current);
557 $last = $key == count($head->children
) - 1;
558 foreach ($tree as $kt=>&$line) {
559 if ($kt == 0 && $key == 0) {
560 $array[0] .= '--' . $line;
561 } else if ($kt == 0 && $last) {
562 $array[] = ' !-' . $line;
563 } else if ($kt == 0) {
564 $array[] = ' |-' . $line;
566 $array[] = ' ' . $line;
568 $array[] = ' | ' . $line;
575 return array($prof, $array);
578 /** build the spool tree associated with the given message
580 public function buildTree($id) {
582 $overview =& $this->overview
[$id];
583 while (!is_null($overview->parent
)) {
584 $pos = $overview->parent
;
585 $overview =& $this->overview
[$pos];
587 list($prof, $tree) = $this->_buildTree($pos, $overview, $id);
588 return implode("\n", $tree);
591 /** computes linear post index
592 * @param $_id INTEGER MSGNUM of post
593 * @return INTEGER linear index of post
595 public function getNdX($_id)
600 $id_parent = $this->overview
[$id_cur]->parent
;
601 if (is_null($id_parent)) break;
602 $pos = array_search($id_cur, $this->overview
[$id_parent]->children
);
604 for ($i = 0; $i < $pos ; $i++
) {
605 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
609 $id_cur = $id_parent;
612 foreach ($this->roots
as $i) {
616 $ndx +
= $this->overview
[$i]->desc
;
621 /** Return root message of the given thread
622 * @param id INTEGER id of a message
624 public function root($id)
628 $id_parent = $this->overview
[$id_cur]->parent
;
629 if (is_null($id_parent)) break;
630 $id_cur = $id_parent;
635 /** Return the last post id with the given subject
638 public function getPostId($subject)
640 $subject = trim($subject);
641 $id = max(array_keys($this->overview
));
642 while (isset($this->overview
[$id])) {
643 $test = $this->overview
[$id]->subject
;
644 if (function_exists('hook_formatDisplayHeader')) {
645 $val = hook_formatDisplayHeader('subject', $test, true
);
646 if (is_array($val)) {
647 $test = banana_html_entity_decode($val[0]);
649 $test = banana_html_entity_decode($val);
653 if ($test == $subject) {
661 /** Returns previous thread root index
662 * @param id INTEGER message number
664 public function prevThread($id)
666 $root = $this->root($id);
668 foreach ($this->roots
as $i) {
677 /** Returns next thread root index
678 * @param id INTEGER message number
680 public function nextThread($id)
682 $root = $this->root($id);
684 foreach ($this->roots
as $i) {
695 /** Return prev post in the thread
696 * @param id INTEGER message number
698 public function prevPost($id)
700 $parent = $this->overview
[$id]->parent
;
701 if (is_null($parent)) {
705 foreach ($this->overview
[$parent]->children
as $child) {
714 /** Return next post in the thread
715 * @param id INTEGER message number
717 public function nextPost($id)
719 if (count($this->overview
[$id]->children
) != 0) {
720 return $this->overview
[$id]->children
[0];
725 $parent = $this->overview
[$cur]->parent
;
726 if (is_null($parent)) {
730 foreach ($this->overview
[$parent]->children
as $child) {
734 if ($child == $cur) {
743 /** Look for an unread message in the thread rooted by the message
744 * @param id INTEGER message number
746 private function _nextUnread($id)
748 if (!$this->overview
[$id]->isread
) {
751 foreach ($this->overview
[$id]->children
as $child) {
752 $unread = $this->_nextUnread($child);
753 if (!is_null($unread)) {
760 /** Find next unread message
761 * @param id INTEGER message number
763 public function nextUnread($id = null
)
765 if (!$this->unreadnb
) {
770 // Look in message children
771 foreach ($this->overview
[$id]->children
as $child) {
772 $next = $this->_nextUnread($child);
773 if (!is_null($next)) {
779 // Look in current thread
782 $parent = is_null($cur) ? null
: $this->overview
[$cur]->parent
;
783 $ok = is_null($cur) ? true
: false
;
784 if (!is_null($parent)) {
785 $array = &$this->overview
[$parent]->children
;
787 $array = &$this->roots
;
789 foreach ($array as $child) {
791 $next = $this->_nextUnread($child);
792 if (!is_null($next)) {
796 if ($child == $cur) {
801 } while(!is_null($cur));
806 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: