4213e5bee49d7d402b2344f8a666a23c0c3d2b00
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.5');
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 */
74 /** protocole specific data */
75 public $storage = array();
77 private $unreadnb = 0;
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)
84 protected function __construct($group)
86 $this->version
= BANANA_SPOOL_VERSION
;
87 $this->mode
= Banana
::SPOOL_ALL
;
88 $this->group
= $group;
91 public static function &getSpool($group, $since = 0, $clean = false
)
93 if (!is_null(Banana
::$spool) && Banana
::$spool->group
== $group) {
94 $spool =& Banana
::$spool;
96 $spool =& BananaSpool
::readFromFile($group);
98 if (is_null($spool)) {
99 $spool = new BananaSpool($group);
101 Banana
::$spool =& $spool;
104 $spool->markAllAsRead();
106 $spool->updateUnread($since);
110 private static function spoolFilename($group)
112 $file = Banana
::$spool_root . '/' . Banana
::$protocole->name() . '/';
113 if (!is_dir($file)) {
116 return $file . Banana
::$protocole->filename();
119 private static function &readFromFile($group)
122 $file = BananaSpool
::spoolFilename($group);
123 if (!file_exists($file)) {
126 $spool = unserialize(file_get_contents($file));
127 if ($spool->version
!= BANANA_SPOOL_VERSION ||
$spool->mode
!= Banana
::SPOOL_ALL
) {
131 $spool->markAllAsRead();
135 private function compare($a, $b)
137 return ($this->overview
[$b]->date
>= $this->overview
[$a]->date
);
140 private function saveToFile()
142 $file = BananaSpool
::spoolFilename($this->group
);
144 $this->roots
= Array();
145 foreach($this->overview
as $id=>&$msg) {
146 if (is_null($msg->parent
)) {
147 $this->roots
[] = $id;
150 usort($this->roots
, array($this, 'compare'));
152 if ($this->mode
== Banana
::SPOOL_ALL
) {
153 file_put_contents($file, serialize($this));
157 private function build()
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;
168 if (Banana
::$spool_max && Banana
::$spool_max < $msgnum) {
169 $first = $last - Banana
::$spool_max;
171 $first +
= $threshold;
174 $clean = $this->clean($first, $last, $msgnum);
175 $update = $this->update($first, $last, $msgnum);
177 if ($clean ||
$update) {
182 private function clean(&$first, &$last, $msgnum)
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
);
194 if (!empty($this->overview
)) {
195 $first = max(array_keys($this->overview
))+
1;
201 private function update(&$first, &$last, $msgnum)
203 if ($first > $last ||
!$msgnum) {
207 $messages =& Banana
::$protocole->getMessageHeaders($first, $last,
208 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
210 if (!is_array($this->ids
)) {
211 $this->ids
= array();
213 foreach ($messages as $id=>&$message) {
214 $this->ids
[$message['message-id']] = $id;
217 if (!is_array($this->overview
)) {
218 $this->overview
= array();
220 foreach ($messages as $id=>&$message) {
221 if (!isset($this->overview
[$id])) {
222 $this->overview
[$id] = new BananaSpoolHead($message);
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));
230 if (!is_null($p = $msg->parent
)) {
231 if (empty($this->overview
[$p])) {
232 $this->overview
[$p] = new BananaSpoolHead($messages[$p]);
234 $this->overview
[$p]->children
[] = $id;
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
;
246 Banana
::$protocole->updateSpool($messages);
250 public function updateUnread($since)
256 $newpostsids = Banana
::$protocole->getNewIndexes($since);
258 if (empty($newpostsids)) {
262 if (!is_array($this->ids
)) {
263 $this->ids
= array();
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
;
272 $this->overview
[$id]->descunread++
;
273 $id = $this->overview
[$id]->parent
;
279 public function setMode($mode)
283 case Banana
::SPOOL_UNREAD
:
284 $num = max(array_keys($this->overview
));
285 if ($this->overview
[$num]->isread
) {
288 foreach ($this->roots
as $k=>$i) {
289 if ($this->overview
[$i]->descunread
== 0) {
291 unset($this->roots
[$k]);
298 /** Mark the given id as read
299 * @param id MSGNUM of post
301 public function markAsRead($id)
303 if (!$this->overview
[$id]->isread
) {
304 $this->overview
[$id]->isread
= true
;
307 $this->overview
[$id]->descunread
--;
308 $id = $this->overview
[$id]->parent
;
313 /** Mark all unread messages as read
315 public function markAllAsRead(array &$array = null
)
317 if (!$this->unreadnb
) {
320 if (is_null($array) && is_array($this->roots
)) {
321 $array =& $this->roots
;
322 } elseif (is_null($array)) {
325 foreach ($array as $id) {
326 if (!$this->overview
[$id]->isread
) {
327 $this->markAsRead($id);
328 if (!$this->unreadnb
) {
332 if ($this->overview
[$id]->descunread
) {
333 $this->markAllAsRead($this->overview
[$id]->children
);
338 /** kill post and childrens
339 * @param $_id MSGNUM of post
341 private function killdesc($_id)
343 if (sizeof($this->overview
[$_id]->children
)) {
344 foreach ($this->overview
[$_id]->children
as $c) {
348 unset($this->overview
[$_id]);
349 if (($msgid = array_search($_id, $this->ids
)) !== false
) {
350 unset($this->ids
[$msgid]);
354 /** delete a post from overview
355 * @param $_id MSGNUM of post
357 public function delid($_id, $write = true
)
359 if (isset($this->overview
[$_id])) {
360 $overview =& $this->overview
[$_id];
361 if (!$overview->isread
) {
362 $this->markAsRead($_id);
364 if ($overview->parent
) {
365 $p = $overview->parent
;
366 $parent =& $this->overview
[$p];
367 $parent->children
= array_diff($parent->children
, array($_id));
368 if (sizeof($overview->children
)) {
369 $parent->children
= array_merge($parent->children
, $overview->children
);
370 foreach ($overview->children
as $c) {
371 $this->overview
[$c]->parent
= $p;
372 $this->overview
[$c]->parent_direct
= false
;
376 $this->overview
[$p]->desc
--;
377 $p = $this->overview
[$p]->parent
;
379 } elseif ($overview->children
) {
380 foreach ($overview->children
as $c) {
381 $this->overview
[$c]->parent
= null
;
385 unset($this->overview
[$_id]);
386 $msgid = array_search($_id, $this->ids
);
387 if ($msgid !== false
) {
388 unset($this->ids
[$msgid]);
390 $msgid = array_search($_id, $this->roots
);
391 if ($msgid !== false
) {
392 unset($this->roots
[$msgid]);
401 private function formatDate($stamp)
403 $today = intval(time() / (24*3600));
404 $dday = intval($stamp / (24*3600));
406 if ($today == $dday) {
408 } elseif ($today == 1 +
$dday) {
409 $format = _b_('hier')." %H:%M";
410 } elseif ($today < 7 +
$dday) {
411 $format = '%a %H:%M';
412 } elseif ($today < 90 +
$dday) {
413 $format = '%a %e %b';
415 $format = '%a %e %b %Y';
417 return strftime($format, $stamp);
420 /** displays children tree of a post
421 * @param $_id INTEGER MSGNUM of post
422 * @param $_index INTEGER linear number of post in the tree
423 * @param $_first INTEGER linear number of first post displayed
424 * @param $_last INTEGER linear number of last post displayed
425 * @param $_ref STRING MSGNUM of current post
426 * @param $_pfx_node STRING prefix used for current node
427 * @param $_pfx_end STRING prefix used for children of current node
428 * @param $_head BOOLEAN true if first post in thread
430 * If you want to analyse subject, you can define the function hook_formatDisplayHeader
432 private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true
, $_pfx_id="")
434 static $spfx_f, $spfx_n, $spfx_Tnd, $spfx_Lnd, $spfx_snd, $spfx_T, $spfx_L, $spfx_s, $spfx_e, $spfx_I;
435 if (!isset($spfx_f)) {
436 $spfx_f = Banana
::$page->makeImg(Array('img' => 'k1', 'alt' => 'o', 'height' => 21, 'width' => 9));
437 $spfx_n = Banana
::$page->makeImg(Array('img' => 'k2', 'alt' => '*', 'height' => 21, 'width' => 9));
438 $spfx_Tnd = Banana
::$page->makeImg(Array('img' => 'T-direct', 'alt' => '+', 'height' => 21, 'width' => 12));
439 $spfx_Lnd = Banana
::$page->makeImg(Array('img' => 'L-direct', 'alt' => '`', 'height' => 21, 'width' => 12));
440 $spfx_snd = Banana
::$page->makeImg(Array('img' => 's-direct', 'alt' => '-', 'height' => 21, 'width' => 5));
441 $spfx_T = Banana
::$page->makeImg(Array('img' => 'T', 'alt' => '+', 'height' => 21, 'width' => 12));
442 $spfx_L = Banana
::$page->makeImg(Array('img' => 'L', 'alt' => '`', 'height' => 21, 'width' => 12));
443 $spfx_s = Banana
::$page->makeImg(Array('img' => 's', 'alt' => '-', 'height' => 21, 'width' => 5));
444 $spfx_e = Banana
::$page->makeImg(Array('img' => 'e', 'alt' => ' ', 'height' => 21, 'width' => 12));
445 $spfx_I = Banana
::$page->makeImg(Array('img' => 'I', 'alt' => '|', 'height' => 21, 'width' => 12));
448 $overview =& $this->overview
[$_id];
449 if ($_index +
$overview->desc
< $_first ||
$_index > $_last) {
454 if ($_index >= $_first) {
455 $hc = empty($overview->children
);
457 $res .= '<tr id="'.$_pfx_id.$_id.'" class="' . ($_index%2 ?
'pair' : 'impair') . ($overview->isread ?
'' : ' new') . "\">\n";
458 $res .= '<td class="date">' . $this->formatDate($overview->date
) . " </td>\n";
459 $res .= '<td class="subj' . ($_index == $_ref ?
' cur' : '') . '"><div class="tree">'
460 . $_pfx_node .($hc ?
($_head ?
$spfx_f : ($overview->parent_direct ?
$spfx_s : $spfx_snd)) : $spfx_n)
462 $popup = $subject = $overview->subject
;
463 if (function_exists('hook_formatDisplayHeader')) {
464 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true
);
466 $subject = banana_catchFormats(banana_entities(stripslashes($subject)));
469 if (empty($subject)) {
470 $subject = _b_('(pas de sujet)');
472 if ($_index != $_ref) {
473 $subject = Banana
::$page->makeLink(Array('group' => $this->group
, 'artid' => $_id,
474 'text' => $subject, 'popup' => $popup));
476 $res .= ' ' . $subject . $link;
477 $res .= "</td>\n<td class='from'>" . BananaMessage
::formatFrom($overview->from
) . "</td>\n</tr>";
485 $children = $overview->children
;
486 while ($child = array_shift($children)) {
487 $overview =& $this->overview
[$child];
488 if ($_index > $_last) {
491 if ($_index +
$overview->desc
>= $_first) {
492 if (sizeof($children)) {
493 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
494 $_pfx_end . ($overview->parent_direct ?
$spfx_T : $spfx_Tnd),
495 $_pfx_end . $spfx_I, false
,$_id.'_');
497 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
498 $_pfx_end . ($overview->parent_direct ?
$spfx_L : $spfx_Lnd),
499 $_pfx_end . $spfx_e, false
,$_id.'_');
502 $_index +
= $overview->desc
;
508 /** Displays overview
509 * @param $_first INTEGER MSGNUM of first post
510 * @param $_last INTEGER MSGNUM of last post
511 * @param $_ref STRING MSGNUM of current/selectionned post
513 public function toHtml($first = 0, $overview = false
)
515 $res = Banana
::$page->makeJs('jquery');
516 $res .= Banana
::$page->makeJs('spool_toggle');
520 $_last = $first + Banana
::$spool_tmax - 1;
523 $_ref = $this->getNdx($first);
524 $_last = $_ref + Banana
::$spool_tafter;
525 $_first = $_ref - Banana
::$spool_tbefore;
531 foreach ($this->roots
as $id) {
532 $res .= $this->_to_html($id, $index, $_first, $_last, $_ref);
533 $index +
= $this->overview
[$id]->desc
;
534 if ($index > $_last) {
541 /** computes linear post index
542 * @param $_id INTEGER MSGNUM of post
543 * @return INTEGER linear index of post
545 public function getNdX($_id)
550 $id_parent = $this->overview
[$id_cur]->parent
;
551 if (is_null($id_parent)) break;
552 $pos = array_search($id_cur, $this->overview
[$id_parent]->children
);
554 for ($i = 0; $i < $pos ; $i++
) {
555 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
559 $id_cur = $id_parent;
562 foreach ($this->roots
as $i) {
566 $ndx +
= $this->overview
[$i]->desc
;
571 /** Return root message of the given thread
572 * @param id INTEGER id of a message
574 public function root($id)
578 $id_parent = $this->overview
[$id_cur]->parent
;
579 if (is_null($id_parent)) break;
580 $id_cur = $id_parent;
585 /** Return the last post id with the given subject
588 public function getPostId($subject)
590 $subject = trim($subject);
591 $id = max(array_keys($this->overview
));
592 while (isset($this->overview
[$id])) {
593 $test = $this->overview
[$id]->subject
;
594 if (function_exists('hook_formatDisplayHeader')) {
595 $val = hook_formatDisplayHeader('subject', $test, true
);
596 if (is_array($val)) {
597 $test = banana_html_entity_decode($val[0]);
599 $test = banana_html_entity_decode($val);
603 if ($test == $subject) {
611 /** Returns previous thread root index
612 * @param id INTEGER message number
614 public function prevThread($id)
616 $root = $this->root($id);
618 foreach ($this->roots
as $i) {
627 /** Returns next thread root index
628 * @param id INTEGER message number
630 public function nextThread($id)
632 $root = $this->root($id);
634 foreach ($this->roots
as $i) {
645 /** Return prev post in the thread
646 * @param id INTEGER message number
648 public function prevPost($id)
650 $parent = $this->overview
[$id]->parent
;
651 if (is_null($parent)) {
655 foreach ($this->overview
[$parent]->children
as $child) {
664 /** Return next post in the thread
665 * @param id INTEGER message number
667 public function nextPost($id)
669 if (count($this->overview
[$id]->children
) != 0) {
670 return $this->overview
[$id]->children
[0];
675 $parent = $this->overview
[$cur]->parent
;
676 if (is_null($parent)) {
680 foreach ($this->overview
[$parent]->children
as $child) {
684 if ($child == $cur) {
693 /** Look for an unread message in the thread rooted by the message
694 * @param id INTEGER message number
696 private function _nextUnread($id)
698 if (!$this->overview
[$id]->isread
) {
701 foreach ($this->overview
[$id]->children
as $child) {
702 $unread = $this->_nextUnread($child);
703 if (!is_null($unread)) {
710 /** Find next unread message
711 * @param id INTEGER message number
713 public function nextUnread($id = null
)
715 if (!$this->unreadnb
) {
720 // Look in message children
721 foreach ($this->overview
[$id]->children
as $child) {
722 $next = $this->_nextUnread($child);
723 if (!is_null($next)) {
729 // Look in current thread
732 $parent = is_null($cur) ? null
: $this->overview
[$cur]->parent
;
733 $ok = is_null($cur) ? true
: false
;
734 if (!is_null($parent)) {
735 $array = &$this->overview
[$parent]->children
;
737 $array = &$this->roots
;
739 foreach ($array as $child) {
741 $next = $this->_nextUnread($child);
742 if (!is_null($next)) {
746 if ($child == $cur) {
751 } while(!is_null($cur));
756 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: