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.3');
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 */
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)
81 protected function __construct($group)
83 $this->version
= BANANA_SPOOL_VERSION
;
84 $this->mode
= Banana
::SPOOL_ALL
;
85 $this->group
= $group;
88 public static function getSpool($group, $since = 0, $clean = false
)
90 if (!is_null(Banana
::$spool) && Banana
::$spool->group
== $group) {
91 $spool =& Banana
::$spool;
93 $spool = BananaSpool
::readFromFile($group);
95 if (is_null($spool)) {
96 $spool = new BananaSpool($group);
98 Banana
::$spool =& $spool;
101 $spool->markAllAsRead();
103 $spool->updateUnread($since);
107 private static function spoolFilename($group)
109 $file = Banana
::$spool_root . '/' . Banana
::$protocole->name() . '/';
110 if (!is_dir($file)) {
113 return $file . Banana
::$protocole->filename();
116 private static function readFromFile($group)
118 $file = BananaSpool
::spoolFilename($group);
119 if (!file_exists($file)) {
122 $spool = unserialize(file_get_contents($file));
123 if ($spool->version
!= BANANA_SPOOL_VERSION ||
$spool->mode
!= Banana
::SPOOL_ALL
) {
129 private function compare($a, $b)
131 return ($b->date
>= $a->date
);
134 private function saveToFile()
136 $file = BananaSpool
::spoolFilename($this->group
);
137 uasort($this->overview
, array($this, 'compare'));
139 $this->roots
= Array();
140 foreach($this->overview
as $id=>$msg) {
141 if (is_null($msg->parent
)) {
142 $this->roots
[] = $id;
146 if ($this->mode
== Banana
::SPOOL_ALL
) {
147 file_put_contents($file, serialize($this));
151 private function build()
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;
162 if (Banana
::$spool_max && Banana
::$spool_max < $msgnum) {
163 $first = $last - Banana
::$spool_max;
165 $first +
= $threshold;
168 $clean = $this->clean($first, $last, $msgnum);
169 $update = $this->update($first, $last, $msgnum);
171 if ($clean ||
$update) {
176 private function clean(&$first, &$last, $msgnum)
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
);
188 if (!empty($this->overview
)) {
189 $first = max(array_keys($this->overview
))+
1;
195 private function update(&$first, &$last, $msgnum)
197 if ($first > $last ||
!$msgnum) {
201 $messages =& Banana
::$protocole->getMessageHeaders($first, $last,
202 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
204 if (!is_array($this->ids
)) {
205 $this->ids
= array();
207 foreach ($messages as $id=>&$message) {
208 $this->ids
[$message['message-id']] = $id;
211 if (!is_array($this->overview
)) {
212 $this->overview
= array();
214 foreach ($messages as $id=>&$message) {
215 if (!isset($this->overview
[$id])) {
216 $this->overview
[$id] = new BananaSpoolHead($message);
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));
224 if (!is_null($p = $msg->parent
)) {
225 if (empty($this->overview
[$p])) {
226 $this->overview
[$p] = new BananaSpoolHead($messages[$p]);
228 $this->overview
[$p]->children
[] = $id;
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
;
240 Banana
::$protocole->updateSpool($messages);
244 private function updateUnread($since)
250 $newpostsids = Banana
::$protocole->getNewIndexes($since);
252 if (empty($newpostsids)) {
256 if (!is_array($this->ids
)) {
257 $this->ids
= array();
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
;
265 $this->overview
[$id]->descunread ++
;
266 $id = $this->overview
[$id]->parent
;
272 public function setMode($mode)
276 case Banana
::SPOOL_UNREAD
:
277 foreach ($this->roots
as $k=>$i) {
278 if ($this->overview
[$i]->descunread
== 0) {
280 unset($this->roots
[$k]);
287 /** Mark the given id as read
288 * @param id MSGNUM of post
290 public function markAsRead($id)
292 if (!$this->overview
[$id]->isread
) {
293 $this->overview
[$id]->isread
= true
;
295 $this->overview
[$id]->descunread
--;
296 $id = $this->overview
[$id]->parent
;
301 /** Mark all unread messages as read
303 public function markAllAsRead(array &$array = null
)
305 if (is_null($array)) {
306 $array =& $this->roots
;
308 if (!is_array($this->roots
)) {
311 foreach ($array as $id) {
312 if (!$this->overview
[$id]->isread
) {
313 $this->markAsRead($id);
315 if ($this->overview
[$id]->descunread
) {
316 $this->markAllAsRead($this->overview
[$id]->children
);
321 /** kill post and childrens
322 * @param $_id MSGNUM of post
324 private function killdesc($_id)
326 if (sizeof($this->overview
[$_id]->children
)) {
327 foreach ($this->overview
[$_id]->children
as $c) {
331 unset($this->overview
[$_id]);
332 if (($msgid = array_search($_id, $this->ids
)) !== false
) {
333 unset($this->ids
[$msgid]);
337 /** delete a post from overview
338 * @param $_id MSGNUM of post
340 public function delid($_id, $write = true
)
342 if (isset($this->overview
[$_id])) {
343 if (sizeof($this->overview
[$_id]->parent
)) {
344 $this->overview
[$this->overview
[$_id]->parent
]->children
=
345 array_diff($this->overview
[$this->overview
[$_id]->parent
]->children
, array($_id));
346 if (sizeof($this->overview
[$_id]->children
)) {
347 $this->overview
[$this->overview
[$_id]->parent
]->children
=
348 array_merge($this->overview
[$this->overview
[$_id]->parent
]->children
, $this->overview
[$_id]->children
);
349 foreach ($this->overview
[$_id]->children
as $c) {
350 $this->overview
[$c]->parent
= $this->overview
[$_id]->parent
;
351 $this->overview
[$c]->parent_direct
= false
;
354 $p = $this->overview
[$_id]->parent
;
356 $this->overview
[$p]->desc
--;
357 $p = $this->overview
[$p]->parent
;
359 } elseif (sizeof($this->overview
[$_id]->children
)) {
360 foreach ($this->overview
[$_id]->children
as $c) {
361 $this->overview
[$c]->parent
= null
;
364 unset($this->overview
[$_id]);
365 $msgid = array_search($_id, $this->ids
);
366 if ($msgid !== false
) {
367 unset($this->ids
[$msgid]);
369 $msgid = array_search($_id, $this->roots
);
370 if ($msgid !== false
) {
371 unset($this->roots
[$msgid]);
375 $this->markAllAsRead();
381 private function formatDate($stamp)
383 $today = intval(time() / (24*3600));
384 $dday = intval($stamp / (24*3600));
386 if ($today == $dday) {
388 } elseif ($today == 1 +
$dday) {
389 $format = _b_('hier')." %H:%M";
390 } elseif ($today < 7 +
$dday) {
391 $format = '%a %H:%M';
393 $format = '%a %e %b';
395 return utf8_encode(strftime($format, $stamp));
398 /** displays children tree of a post
399 * @param $_id INTEGER MSGNUM of post
400 * @param $_index INTEGER linear number of post in the tree
401 * @param $_first INTEGER linear number of first post displayed
402 * @param $_last INTEGER linear number of last post displayed
403 * @param $_ref STRING MSGNUM of current post
404 * @param $_pfx_node STRING prefix used for current node
405 * @param $_pfx_end STRING prefix used for children of current node
406 * @param $_head BOOLEAN true if first post in thread
408 * If you want to analyse subject, you can define the function hook_formatDisplayHeader
410 private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true
)
412 static $spfx_f, $spfx_n, $spfx_Tnd, $spfx_Lnd, $spfx_snd, $spfx_T, $spfx_L, $spfx_s, $spfx_e, $spfx_I;
413 if (!isset($spfx_f)) {
414 $spfx_f = Banana
::$page->makeImg(Array('img' => 'k1', 'alt' => 'o', 'height' => 21, 'width' => 9));
415 $spfx_n = Banana
::$page->makeImg(Array('img' => 'k2', 'alt' => '*', 'height' => 21, 'width' => 9));
416 $spfx_Tnd = Banana
::$page->makeImg(Array('img' => 'T-direct', 'alt' => '+', 'height' => 21, 'width' => 12));
417 $spfx_Lnd = Banana
::$page->makeImg(Array('img' => 'L-direct', 'alt' => '`', 'height' => 21, 'width' => 12));
418 $spfx_snd = Banana
::$page->makeImg(Array('img' => 's-direct', 'alt' => '-', 'height' => 21, 'width' => 5));
419 $spfx_T = Banana
::$page->makeImg(Array('img' => 'T', 'alt' => '+', 'height' => 21, 'width' => 12));
420 $spfx_L = Banana
::$page->makeImg(Array('img' => 'L', 'alt' => '`', 'height' => 21, 'width' => 12));
421 $spfx_s = Banana
::$page->makeImg(Array('img' => 's', 'alt' => '-', 'height' => 21, 'width' => 5));
422 $spfx_e = Banana
::$page->makeImg(Array('img' => 'e', 'alt' => ' ', 'height' => 21, 'width' => 12));
423 $spfx_I = Banana
::$page->makeImg(Array('img' => 'I', 'alt' => '|', 'height' => 21, 'width' => 12));
426 $overview =& $this->overview
[$_id];
427 if ($_index +
$overview->desc
< $_first ||
$_index > $_last) {
432 if ($_index >= $_first) {
433 $hc = empty($overview->children
);
435 $res .= '<tr class="' . ($_index%2 ?
'pair' : 'impair') . ($overview->isread ?
'' : ' new') . "\">\n";
436 $res .= '<td class="date">' . $this->formatDate($overview->date
) . " </td>\n";
437 $res .= '<td class="subj' . ($_index == $_ref ?
' cur' : '') . '">'
438 . $_pfx_node .($hc ?
($_head ?
$spfx_f : ($overview->parent_direct ?
$spfx_s : $spfx_snd)) : $spfx_n);
439 $subject = $overview->subject
;
440 if (function_exists('hook_formatDisplayHeader')) {
441 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true
);
443 $subject = banana_catchFormats(banana_htmlentities(stripslashes($subject)));
446 if (empty($subject)) {
447 $subject = _b_('(pas de sujet)');
449 if ($_index != $_ref) {
450 $subject = Banana
::$page->makeLink(Array('group' => $this->group
, 'artid' => $_id,
451 'text' => $subject, 'popup' => $subject));
453 $res .= ' ' . $subject . $link;
454 $res .= "</td>\n<td class='from'>" . BananaMessage
::formatFrom($overview->from
) . "</td>\n</tr>";
462 $children = $overview->children
;
463 while ($child = array_shift($children)) {
464 $overview =& $this->overview
[$child];
465 if ($_index > $_last) {
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
);
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
);
479 $_index +
= $overview->desc
;
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
490 public function toHtml($first = 0, $overview = false
)
496 $_last = $first + Banana
::$spool_tmax - 1;
499 $_ref = $this->getNdx($first);
500 $_last = $_ref + Banana
::$spool_tafter;
501 $_first = $_ref - Banana
::$spool_tbefore;
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) {
517 /** computes linear post index
518 * @param $_id INTEGER MSGNUM of post
519 * @return INTEGER linear index of post
521 public function getNdX($_id)
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
);
530 for ($i = 0; $i < $pos ; $i++
) {
531 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
535 $id_cur = $id_parent;
538 foreach ($this->roots
as $i) {
542 $ndx +
= $this->overview
[$i]->desc
;
547 /** Return root message of the given thread
548 * @param id INTEGER id of a message
550 public function root($id)
554 $id_parent = $this->overview
[$id_cur]->parent
;
555 if (is_null($id_parent)) break;
556 $id_cur = $id_parent;
561 /** Returns previous thread root index
562 * @param id INTEGER message number
564 public function prevThread($id)
566 $root = $this->root($id);
568 foreach ($this->roots
as $i) {
577 /** Returns next thread root index
578 * @param id INTEGER message number
580 public function nextThread($id)
582 $root = $this->root($id);
584 foreach ($this->roots
as $i) {
595 /** Return prev post in the thread
596 * @param id INTEGER message number
598 public function prevPost($id)
600 $parent = $this->overview
[$id]->parent
;
601 if (is_null($parent)) {
605 foreach ($this->overview
[$parent]->children
as $child) {
614 /** Return next post in the thread
615 * @param id INTEGER message number
617 public function nextPost($id)
619 if (count($this->overview
[$id]->children
) != 0) {
620 return $this->overview
[$id]->children
[0];
625 $parent = $this->overview
[$cur]->parent
;
626 if (is_null($parent)) {
630 foreach ($this->overview
[$parent]->children
as $child) {
634 if ($child == $cur) {
643 /** Look for an unread message in the thread rooted by the message
644 * @param id INTEGER message number
646 private function _nextUnread($id)
648 if (!$this->overview
[$id]->isread
) {
651 foreach ($this->overview
[$id]->children
as $child) {
652 $unread = $this->_nextUnread($child);
653 if (!is_null($unread)) {
660 /** Find next unread message
661 * @param id INTEGER message number
663 public function nextUnread($id = null
)
666 // Look in message children
667 foreach ($this->overview
[$id]->children
as $child) {
668 $next = $this->_nextUnread($child);
669 if (!is_null($next)) {
675 // Look in current thread
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
;
683 $array = &$this->roots
;
685 foreach ($array as $child) {
687 $next = $this->_nextUnread($child);
688 if (!is_null($next)) {
692 if ($child == $cur) {
697 } while(!is_null($cur));
702 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: