ed898e21903f6e6b8f4836e0c6447887f6f58a1b
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)
119 $file = BananaSpool
::spoolFilename($group);
120 if (!file_exists($file)) {
123 $spool = unserialize(file_get_contents($file));
124 if ($spool->version
!= BANANA_SPOOL_VERSION ||
$spool->mode
!= Banana
::SPOOL_ALL
) {
127 $spool->markAllAsRead();
131 private function compare($a, $b)
133 return ($b->date
>= $a->date
);
136 private function saveToFile()
138 $file = BananaSpool
::spoolFilename($this->group
);
139 uasort($this->overview
, array($this, 'compare'));
141 $this->roots
= Array();
142 foreach($this->overview
as $id=>$msg) {
143 if (is_null($msg->parent
)) {
144 $this->roots
[] = $id;
148 if ($this->mode
== Banana
::SPOOL_ALL
) {
149 file_put_contents($file, serialize($this));
153 private function build()
157 // Compute the range of indexes
158 list($msgnum, $first, $last) = Banana
::$protocole->getIndexes();
159 if ($last < $first) {
160 $threshold = $firt +
$msgnum - $last;
161 $threshold = (int)(log($threshold)/log(2));
162 $threshold = (2 ^
($threshold +
1)) - 1;
164 if (Banana
::$spool_max && Banana
::$spool_max < $msgnum) {
165 $first = $last - Banana
::$spool_max;
167 $first +
= $threshold;
170 $clean = $this->clean($first, $last, $msgnum);
171 $update = $this->update($first, $last, $msgnum);
173 if ($clean ||
$update) {
178 private function clean(&$first, &$last, $msgnum)
181 if (is_array($this->overview
)) {
182 $mids = array_keys($this->overview
);
183 foreach ($mids as $id) {
184 if (($first <= $last && ($id < $first ||
$id > $last))
185 ||
($first > $last && $id < $first && $id > $last)) {
186 $this->delid($id, false
);
190 if (!empty($this->overview
)) {
191 $first = max(array_keys($this->overview
))+
1;
197 private function update(&$first, &$last, $msgnum)
199 if ($first > $last ||
!$msgnum) {
203 $messages =& Banana
::$protocole->getMessageHeaders($first, $last,
204 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
206 if (!is_array($this->ids
)) {
207 $this->ids
= array();
209 foreach ($messages as $id=>&$message) {
210 $this->ids
[$message['message-id']] = $id;
213 if (!is_array($this->overview
)) {
214 $this->overview
= array();
216 foreach ($messages as $id=>&$message) {
217 if (!isset($this->overview
[$id])) {
218 $this->overview
[$id] = new BananaSpoolHead($message);
220 $msg =& $this->overview
[$id];
221 $msgrefs = BananaMessage
::formatReferences($message);
222 $parents = preg_grep('/^\d+$/', $msgrefs);
223 $msg->parent
= array_pop($parents);
224 $msg->parent_direct
= preg_match('/^\d+$/', array_pop($msgrefs));
226 if (!is_null($p = $msg->parent
)) {
227 if (empty($this->overview
[$p])) {
228 $this->overview
[$p] = new BananaSpoolHead($messages[$p]);
230 $this->overview
[$p]->children
[] = $id;
232 while (!is_null($p)) {
233 $this->overview
[$p]->desc +
= $msg->desc
;
234 if ($p != $this->overview
[$p]->parent
) {
235 $p = $this->overview
[$p]->parent
;
242 Banana
::$protocole->updateSpool($messages);
246 private function updateUnread($since)
252 $newpostsids = Banana
::$protocole->getNewIndexes($since);
254 if (empty($newpostsids)) {
258 if (!is_array($this->ids
)) {
259 $this->ids
= array();
261 $newpostsids = array_intersect($newpostsids, array_keys($this->ids
));
262 foreach ($newpostsids as $mid) {
263 $id = $this->ids
[$mid];
264 if ($this->overview
[$id]->isread
) {
265 $this->overview
[$id]->isread
= false
;
268 $this->overview
[$id]->descunread++
;
269 $id = $this->overview
[$id]->parent
;
275 public function setMode($mode)
279 case Banana
::SPOOL_UNREAD
:
280 foreach ($this->roots
as $k=>$i) {
281 if ($this->overview
[$i]->descunread
== 0) {
283 unset($this->roots
[$k]);
290 /** Mark the given id as read
291 * @param id MSGNUM of post
293 public function markAsRead($id)
295 if (!$this->overview
[$id]->isread
) {
296 $this->overview
[$id]->isread
= true
;
299 $this->overview
[$id]->descunread
--;
300 $id = $this->overview
[$id]->parent
;
305 /** Mark all unread messages as read
307 public function markAllAsRead(array &$array = null
)
309 if (!$this->unreadnb
) {
312 if (is_null($array) && is_array($this->roots
)) {
313 $array =& $this->roots
;
314 } elseif (is_null($array)) {
317 foreach ($array as $id) {
318 if (!$this->overview
[$id]->isread
) {
319 $this->markAsRead($id);
320 if (!$this->unreadnb
) {
324 if ($this->overview
[$id]->descunread
) {
325 $this->markAllAsRead($this->overview
[$id]->children
);
330 /** kill post and childrens
331 * @param $_id MSGNUM of post
333 private function killdesc($_id)
335 if (sizeof($this->overview
[$_id]->children
)) {
336 foreach ($this->overview
[$_id]->children
as $c) {
340 unset($this->overview
[$_id]);
341 if (($msgid = array_search($_id, $this->ids
)) !== false
) {
342 unset($this->ids
[$msgid]);
346 /** delete a post from overview
347 * @param $_id MSGNUM of post
349 public function delid($_id, $write = true
)
351 if (isset($this->overview
[$_id])) {
352 $overview =& $this->overview
[$_id];
353 if (!$overview->isread
) {
354 $this->markAsRead($_id);
356 if ($overview->parent
) {
357 $p = $overview->parent
;
358 $parent =& $this->overview
[$p];
359 $parent->children
= array_diff($parent->children
, array($_id));
360 if (sizeof($overview->children
)) {
361 $parent->children
= array_merge($parent->children
, $overview->children
);
362 foreach ($overview->children
as $c) {
363 $this->overview
[$c]->parent
= $p;
364 $this->overview
[$c]->parent_direct
= false
;
368 $this->overview
[$p]->desc
--;
369 $p = $this->overview
[$p]->parent
;
371 } elseif ($overview->children
) {
372 foreach ($overview->children
as $c) {
373 $this->overview
[$c]->parent
= null
;
377 unset($this->overview
[$_id]);
378 $msgid = array_search($_id, $this->ids
);
379 if ($msgid !== false
) {
380 unset($this->ids
[$msgid]);
382 $msgid = array_search($_id, $this->roots
);
383 if ($msgid !== false
) {
384 unset($this->roots
[$msgid]);
393 private function formatDate($stamp)
395 $today = intval(time() / (24*3600));
396 $dday = intval($stamp / (24*3600));
398 if ($today == $dday) {
400 } elseif ($today == 1 +
$dday) {
401 $format = _b_('hier')." %H:%M";
402 } elseif ($today < 7 +
$dday) {
403 $format = '%a %H:%M';
405 $format = '%a %e %b';
407 return strftime($format, $stamp);
410 /** displays children tree of a post
411 * @param $_id INTEGER MSGNUM of post
412 * @param $_index INTEGER linear number of post in the tree
413 * @param $_first INTEGER linear number of first post displayed
414 * @param $_last INTEGER linear number of last post displayed
415 * @param $_ref STRING MSGNUM of current post
416 * @param $_pfx_node STRING prefix used for current node
417 * @param $_pfx_end STRING prefix used for children of current node
418 * @param $_head BOOLEAN true if first post in thread
420 * If you want to analyse subject, you can define the function hook_formatDisplayHeader
422 private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true
)
424 static $spfx_f, $spfx_n, $spfx_Tnd, $spfx_Lnd, $spfx_snd, $spfx_T, $spfx_L, $spfx_s, $spfx_e, $spfx_I;
425 if (!isset($spfx_f)) {
426 $spfx_f = Banana
::$page->makeImg(Array('img' => 'k1', 'alt' => 'o', 'height' => 21, 'width' => 9));
427 $spfx_n = Banana
::$page->makeImg(Array('img' => 'k2', 'alt' => '*', 'height' => 21, 'width' => 9));
428 $spfx_Tnd = Banana
::$page->makeImg(Array('img' => 'T-direct', 'alt' => '+', 'height' => 21, 'width' => 12));
429 $spfx_Lnd = Banana
::$page->makeImg(Array('img' => 'L-direct', 'alt' => '`', 'height' => 21, 'width' => 12));
430 $spfx_snd = Banana
::$page->makeImg(Array('img' => 's-direct', 'alt' => '-', 'height' => 21, 'width' => 5));
431 $spfx_T = Banana
::$page->makeImg(Array('img' => 'T', 'alt' => '+', 'height' => 21, 'width' => 12));
432 $spfx_L = Banana
::$page->makeImg(Array('img' => 'L', 'alt' => '`', 'height' => 21, 'width' => 12));
433 $spfx_s = Banana
::$page->makeImg(Array('img' => 's', 'alt' => '-', 'height' => 21, 'width' => 5));
434 $spfx_e = Banana
::$page->makeImg(Array('img' => 'e', 'alt' => ' ', 'height' => 21, 'width' => 12));
435 $spfx_I = Banana
::$page->makeImg(Array('img' => 'I', 'alt' => '|', 'height' => 21, 'width' => 12));
438 $overview =& $this->overview
[$_id];
439 if ($_index +
$overview->desc
< $_first ||
$_index > $_last) {
444 if ($_index >= $_first) {
445 $hc = empty($overview->children
);
447 $res .= '<tr class="' . ($_index%2 ?
'pair' : 'impair') . ($overview->isread ?
'' : ' new') . "\">\n";
448 $res .= '<td class="date">' . $this->formatDate($overview->date
) . " </td>\n";
449 $res .= '<td class="subj' . ($_index == $_ref ?
' cur' : '') . '"><div class="tree">'
450 . $_pfx_node .($hc ?
($_head ?
$spfx_f : ($overview->parent_direct ?
$spfx_s : $spfx_snd)) : $spfx_n)
452 $subject = $overview->subject
;
453 if (function_exists('hook_formatDisplayHeader')) {
454 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true
);
456 $subject = banana_catchFormats(banana_htmlentities(stripslashes($subject)));
459 if (empty($subject)) {
460 $subject = _b_('(pas de sujet)');
462 if ($_index != $_ref) {
463 $subject = Banana
::$page->makeLink(Array('group' => $this->group
, 'artid' => $_id,
464 'text' => $subject, 'popup' => $subject));
466 $res .= ' ' . $subject . $link;
467 $res .= "</td>\n<td class='from'>" . BananaMessage
::formatFrom($overview->from
) . "</td>\n</tr>";
475 $children = $overview->children
;
476 while ($child = array_shift($children)) {
477 $overview =& $this->overview
[$child];
478 if ($_index > $_last) {
481 if ($_index +
$overview->desc
>= $_first) {
482 if (sizeof($children)) {
483 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
484 $_pfx_end . ($overview->parent_direct ?
$spfx_T : $spfx_Tnd),
485 $_pfx_end . $spfx_I, false
);
487 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
488 $_pfx_end . ($overview->parent_direct ?
$spfx_L : $spfx_Lnd),
489 $_pfx_end . $spfx_e, false
);
492 $_index +
= $overview->desc
;
498 /** Displays overview
499 * @param $_first INTEGER MSGNUM of first post
500 * @param $_last INTEGER MSGNUM of last post
501 * @param $_ref STRING MSGNUM of current/selectionned post
503 public function toHtml($first = 0, $overview = false
)
509 $_last = $first + Banana
::$spool_tmax - 1;
512 $_ref = $this->getNdx($first);
513 $_last = $_ref + Banana
::$spool_tafter;
514 $_first = $_ref - Banana
::$spool_tbefore;
520 foreach ($this->roots
as $id) {
521 $res .= $this->_to_html($id, $index, $_first, $_last, $_ref);
522 $index +
= $this->overview
[$id]->desc
;
523 if ($index > $_last) {
530 /** computes linear post index
531 * @param $_id INTEGER MSGNUM of post
532 * @return INTEGER linear index of post
534 public function getNdX($_id)
539 $id_parent = $this->overview
[$id_cur]->parent
;
540 if (is_null($id_parent)) break;
541 $pos = array_search($id_cur, $this->overview
[$id_parent]->children
);
543 for ($i = 0; $i < $pos ; $i++
) {
544 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
548 $id_cur = $id_parent;
551 foreach ($this->roots
as $i) {
555 $ndx +
= $this->overview
[$i]->desc
;
560 /** Return root message of the given thread
561 * @param id INTEGER id of a message
563 public function root($id)
567 $id_parent = $this->overview
[$id_cur]->parent
;
568 if (is_null($id_parent)) break;
569 $id_cur = $id_parent;
574 /** Returns previous thread root index
575 * @param id INTEGER message number
577 public function prevThread($id)
579 $root = $this->root($id);
581 foreach ($this->roots
as $i) {
590 /** Returns next thread root index
591 * @param id INTEGER message number
593 public function nextThread($id)
595 $root = $this->root($id);
597 foreach ($this->roots
as $i) {
608 /** Return prev post in the thread
609 * @param id INTEGER message number
611 public function prevPost($id)
613 $parent = $this->overview
[$id]->parent
;
614 if (is_null($parent)) {
618 foreach ($this->overview
[$parent]->children
as $child) {
627 /** Return next post in the thread
628 * @param id INTEGER message number
630 public function nextPost($id)
632 if (count($this->overview
[$id]->children
) != 0) {
633 return $this->overview
[$id]->children
[0];
638 $parent = $this->overview
[$cur]->parent
;
639 if (is_null($parent)) {
643 foreach ($this->overview
[$parent]->children
as $child) {
647 if ($child == $cur) {
656 /** Look for an unread message in the thread rooted by the message
657 * @param id INTEGER message number
659 private function _nextUnread($id)
661 if (!$this->overview
[$id]->isread
) {
664 foreach ($this->overview
[$id]->children
as $child) {
665 $unread = $this->_nextUnread($child);
666 if (!is_null($unread)) {
673 /** Find next unread message
674 * @param id INTEGER message number
676 public function nextUnread($id = null
)
678 if (!$this->unreadnb
) {
683 // Look in message children
684 foreach ($this->overview
[$id]->children
as $child) {
685 $next = $this->_nextUnread($child);
686 if (!is_null($next)) {
692 // Look in current thread
695 $parent = is_null($cur) ? null
: $this->overview
[$cur]->parent
;
696 $ok = is_null($cur) ? true
: false
;
697 if (!is_null($parent)) {
698 $array = &$this->overview
[$parent]->children
;
700 $array = &$this->roots
;
702 foreach ($array as $child) {
704 $next = $this->_nextUnread($child);
705 if (!is_null($next)) {
709 if ($child == $cur) {
714 } while(!is_null($cur));
719 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: