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.4');
15 * class used in thread overviews
19 /** date (timestamp) */
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 */
33 /** number of posts deeper in this branch of tree */
35 /** same as desc, but counts only unread posts */
39 public $storage = array();
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)
49 public function __construct(array &$message)
51 $this->date
= $message['date'];
52 $this->subject
= $message['subject'];
53 $this->from
= $message['from'];
56 $this->descunread
= 0;
70 /** array msgid => msgnum */
75 private $unreadnb = 0;
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)
82 protected function __construct($group)
84 $this->version
= BANANA_SPOOL_VERSION
;
85 $this->mode
= Banana
::SPOOL_ALL
;
86 $this->group
= $group;
89 public static function &getSpool($group, $since = 0, $clean = false
)
91 if (!is_null(Banana
::$spool) && Banana
::$spool->group
== $group) {
92 $spool =& Banana
::$spool;
94 $spool =& BananaSpool
::readFromFile($group);
96 if (is_null($spool)) {
97 $spool = new BananaSpool($group);
99 Banana
::$spool =& $spool;
102 $spool->markAllAsRead();
104 $spool->updateUnread($since);
108 private static function spoolFilename($group)
110 $file = Banana
::$spool_root . '/' . Banana
::$protocole->name() . '/';
111 if (!is_dir($file)) {
114 return $file . Banana
::$protocole->filename();
117 private static function &readFromFile($group)
120 $file = BananaSpool
::spoolFilename($group);
121 if (!file_exists($file)) {
124 $spool = unserialize(file_get_contents($file));
125 if ($spool->version
!= BANANA_SPOOL_VERSION ||
$spool->mode
!= Banana
::SPOOL_ALL
) {
129 $spool->markAllAsRead();
133 private function compare($a, $b)
135 return ($b->date
>= $a->date
);
138 private function saveToFile()
140 $file = BananaSpool
::spoolFilename($this->group
);
141 uasort($this->overview
, array($this, 'compare'));
143 $this->roots
= Array();
144 foreach($this->overview
as $id=>$msg) {
145 if (is_null($msg->parent
)) {
146 $this->roots
[] = $id;
150 if ($this->mode
== Banana
::SPOOL_ALL
) {
151 file_put_contents($file, serialize($this));
155 private function build()
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;
166 if (Banana
::$spool_max && Banana
::$spool_max < $msgnum) {
167 $first = $last - Banana
::$spool_max;
169 $first +
= $threshold;
172 $clean = $this->clean($first, $last, $msgnum);
173 $update = $this->update($first, $last, $msgnum);
175 if ($clean ||
$update) {
180 private function clean(&$first, &$last, $msgnum)
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
);
192 if (!empty($this->overview
)) {
193 $first = max(array_keys($this->overview
))+
1;
199 private function update(&$first, &$last, $msgnum)
201 if ($first > $last ||
!$msgnum) {
205 $messages =& Banana
::$protocole->getMessageHeaders($first, $last,
206 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
208 if (!is_array($this->ids
)) {
209 $this->ids
= array();
211 foreach ($messages as $id=>&$message) {
212 $this->ids
[$message['message-id']] = $id;
215 if (!is_array($this->overview
)) {
216 $this->overview
= array();
218 foreach ($messages as $id=>&$message) {
219 if (!isset($this->overview
[$id])) {
220 $this->overview
[$id] = new BananaSpoolHead($message);
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));
228 if (!is_null($p = $msg->parent
)) {
229 if (empty($this->overview
[$p])) {
230 $this->overview
[$p] = new BananaSpoolHead($messages[$p]);
232 $this->overview
[$p]->children
[] = $id;
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
;
244 Banana
::$protocole->updateSpool($messages);
248 public function updateUnread($since)
254 $newpostsids = Banana
::$protocole->getNewIndexes($since);
256 if (empty($newpostsids)) {
260 if (!is_array($this->ids
)) {
261 $this->ids
= array();
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
;
270 $this->overview
[$id]->descunread++
;
271 $id = $this->overview
[$id]->parent
;
277 public function setMode($mode)
281 case Banana
::SPOOL_UNREAD
:
282 foreach ($this->roots
as $k=>$i) {
283 if ($this->overview
[$i]->descunread
== 0) {
285 unset($this->roots
[$k]);
292 /** Mark the given id as read
293 * @param id MSGNUM of post
295 public function markAsRead($id)
297 if (!$this->overview
[$id]->isread
) {
298 $this->overview
[$id]->isread
= true
;
301 $this->overview
[$id]->descunread
--;
302 $id = $this->overview
[$id]->parent
;
307 /** Mark all unread messages as read
309 public function markAllAsRead(array &$array = null
)
311 if (!$this->unreadnb
) {
314 if (is_null($array) && is_array($this->roots
)) {
315 $array =& $this->roots
;
316 } elseif (is_null($array)) {
319 foreach ($array as $id) {
320 if (!$this->overview
[$id]->isread
) {
321 $this->markAsRead($id);
322 if (!$this->unreadnb
) {
326 if ($this->overview
[$id]->descunread
) {
327 $this->markAllAsRead($this->overview
[$id]->children
);
332 /** kill post and childrens
333 * @param $_id MSGNUM of post
335 private function killdesc($_id)
337 if (sizeof($this->overview
[$_id]->children
)) {
338 foreach ($this->overview
[$_id]->children
as $c) {
342 unset($this->overview
[$_id]);
343 if (($msgid = array_search($_id, $this->ids
)) !== false
) {
344 unset($this->ids
[$msgid]);
348 /** delete a post from overview
349 * @param $_id MSGNUM of post
351 public function delid($_id, $write = true
)
353 if (isset($this->overview
[$_id])) {
354 $overview =& $this->overview
[$_id];
355 if (!$overview->isread
) {
356 $this->markAsRead($_id);
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
;
370 $this->overview
[$p]->desc
--;
371 $p = $this->overview
[$p]->parent
;
373 } elseif ($overview->children
) {
374 foreach ($overview->children
as $c) {
375 $this->overview
[$c]->parent
= null
;
379 unset($this->overview
[$_id]);
380 $msgid = array_search($_id, $this->ids
);
381 if ($msgid !== false
) {
382 unset($this->ids
[$msgid]);
384 $msgid = array_search($_id, $this->roots
);
385 if ($msgid !== false
) {
386 unset($this->roots
[$msgid]);
395 private function formatDate($stamp)
397 $today = intval(time() / (24*3600));
398 $dday = intval($stamp / (24*3600));
400 if ($today == $dday) {
402 } elseif ($today == 1 +
$dday) {
403 $format = _b_('hier')." %H:%M";
404 } elseif ($today < 7 +
$dday) {
405 $format = '%a %H:%M';
407 $format = '%a %e %b';
409 return strftime($format, $stamp);
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
422 * If you want to analyse subject, you can define the function hook_formatDisplayHeader
424 private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true
, $_pfx_id="")
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' => ' ', 'height' => 21, 'width' => 12));
437 $spfx_I = Banana
::$page->makeImg(Array('img' => 'I', 'alt' => '|', 'height' => 21, 'width' => 12));
440 $overview =& $this->overview
[$_id];
441 if ($_index +
$overview->desc
< $_first ||
$_index > $_last) {
446 if ($_index >= $_first) {
447 $hc = empty($overview->children
);
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)
454 $subject = $overview->subject
;
455 if (function_exists('hook_formatDisplayHeader')) {
456 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true
);
458 $subject = banana_catchFormats(banana_htmlentities(stripslashes($subject)));
461 if (empty($subject)) {
462 $subject = _b_('(pas de sujet)');
464 if ($_index != $_ref) {
465 $subject = Banana
::$page->makeLink(Array('group' => $this->group
, 'artid' => $_id,
466 'text' => $subject, 'popup' => $subject));
468 $res .= ' ' . $subject . $link;
469 $res .= "</td>\n<td class='from'>" . BananaMessage
::formatFrom($overview->from
) . "</td>\n</tr>";
477 $children = $overview->children
;
478 while ($child = array_shift($children)) {
479 $overview =& $this->overview
[$child];
480 if ($_index > $_last) {
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.'_');
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.'_');
494 $_index +
= $overview->desc
;
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
505 public function toHtml($first = 0, $overview = false
)
507 $res = Banana
::$page->makeJs('jquery');
508 $res .= Banana
::$page->makeJs('spool_toggle');
512 $_last = $first + Banana
::$spool_tmax - 1;
515 $_ref = $this->getNdx($first);
516 $_last = $_ref + Banana
::$spool_tafter;
517 $_first = $_ref - Banana
::$spool_tbefore;
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) {
533 /** computes linear post index
534 * @param $_id INTEGER MSGNUM of post
535 * @return INTEGER linear index of post
537 public function getNdX($_id)
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
);
546 for ($i = 0; $i < $pos ; $i++
) {
547 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
551 $id_cur = $id_parent;
554 foreach ($this->roots
as $i) {
558 $ndx +
= $this->overview
[$i]->desc
;
563 /** Return root message of the given thread
564 * @param id INTEGER id of a message
566 public function root($id)
570 $id_parent = $this->overview
[$id_cur]->parent
;
571 if (is_null($id_parent)) break;
572 $id_cur = $id_parent;
577 /** Return the last post id with the given subject
580 public function getPostId($subject)
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)) {
595 if ($test == $subject) {
603 /** Returns previous thread root index
604 * @param id INTEGER message number
606 public function prevThread($id)
608 $root = $this->root($id);
610 foreach ($this->roots
as $i) {
619 /** Returns next thread root index
620 * @param id INTEGER message number
622 public function nextThread($id)
624 $root = $this->root($id);
626 foreach ($this->roots
as $i) {
637 /** Return prev post in the thread
638 * @param id INTEGER message number
640 public function prevPost($id)
642 $parent = $this->overview
[$id]->parent
;
643 if (is_null($parent)) {
647 foreach ($this->overview
[$parent]->children
as $child) {
656 /** Return next post in the thread
657 * @param id INTEGER message number
659 public function nextPost($id)
661 if (count($this->overview
[$id]->children
) != 0) {
662 return $this->overview
[$id]->children
[0];
667 $parent = $this->overview
[$cur]->parent
;
668 if (is_null($parent)) {
672 foreach ($this->overview
[$parent]->children
as $child) {
676 if ($child == $cur) {
685 /** Look for an unread message in the thread rooted by the message
686 * @param id INTEGER message number
688 private function _nextUnread($id)
690 if (!$this->overview
[$id]->isread
) {
693 foreach ($this->overview
[$id]->children
as $child) {
694 $unread = $this->_nextUnread($child);
695 if (!is_null($unread)) {
702 /** Find next unread message
703 * @param id INTEGER message number
705 public function nextUnread($id = null
)
707 if (!$this->unreadnb
) {
712 // Look in message children
713 foreach ($this->overview
[$id]->children
as $child) {
714 $next = $this->_nextUnread($child);
715 if (!is_null($next)) {
721 // Look in current thread
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
;
729 $array = &$this->roots
;
731 foreach ($array as $child) {
733 $next = $this->_nextUnread($child);
734 if (!is_null($next)) {
738 if ($child == $cur) {
743 } while(!is_null($cur));
748 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: