361a43c9be507a833d36aa296c7d158b0d5bf6ec
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 = dirname(dirname(__FILE__
));
110 $file .= '/spool/' . 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
) {
130 private function compare($a, $b)
132 return ($b->date
>= $a->date
);
135 private function saveToFile()
137 $file = BananaSpool
::spoolFilename($this->group
);
138 uasort($this->overview
, array($this, 'compare'));
140 $this->roots
= Array();
141 foreach($this->overview
as $id=>$msg) {
142 if (is_null($msg->parent
)) {
143 $this->roots
[] = $id;
147 if ($this->mode
== Banana
::SPOOL_ALL
) {
148 file_put_contents($file, serialize($this));
152 private function build()
156 // Compute the range of indexes
157 list($msgnum, $first, $last) = Banana
::$protocole->getIndexes();
158 if ($last < $first) {
159 $threshold = $firt +
$msgnum - $last;
160 $threshold = (int)(log($threshold)/log(2));
161 $threshold = (2 ^
($threshold +
1)) - 1;
163 if (Banana
::$spool_max && Banana
::$spool_max < $msgnum) {
164 $first = $last - Banana
::$spool_max;
166 $first +
= $threshold;
169 $clean = $this->clean($first, $last, $msgnum);
170 $update = $this->update($first, $last, $msgnum);
172 if ($clean ||
$update) {
177 private function clean(&$first, &$last, $msgnum)
180 if (is_array($this->overview
)) {
181 $mids = array_keys($this->overview
);
182 foreach ($mids as $id) {
183 if (($first <= $last && ($id < $first ||
$id > $last))
184 ||
($first > $last && $id < $first && $id > $last)) {
185 $this->delid($id, false
);
189 if (!empty($this->overview
)) {
190 $first = max(array_keys($this->overview
))+
1;
196 private function update(&$first, &$last, $msgnum)
198 if ($first > $last ||
!$msgnum) {
202 $messages =& Banana
::$protocole->getMessageHeaders($first, $last,
203 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
205 if (!is_array($this->ids
)) {
206 $this->ids
= array();
208 foreach ($messages as $id=>&$message) {
209 $this->ids
[$message['message-id']] = $id;
212 if (!is_array($this->overview
)) {
213 $this->overview
= array();
215 foreach ($messages as $id=>&$message) {
216 if (!isset($this->overview
[$id])) {
217 $this->overview
[$id] = new BananaSpoolHead($message);
219 $msg =& $this->overview
[$id];
220 $msgrefs = BananaMessage
::formatReferences($message);
221 $parents = preg_grep('/^\d+$/', $msgrefs);
222 $msg->parent
= array_pop($parents);
223 $msg->parent_direct
= preg_match('/^\d+$/', array_pop($msgrefs));
225 if (!is_null($p = $msg->parent
)) {
226 if (empty($this->overview
[$p])) {
227 $this->overview
[$p] = new BananaSpoolHead($messages[$p]);
229 $this->overview
[$p]->children
[] = $id;
231 while (!is_null($p)) {
232 $this->overview
[$p]->desc +
= $msg->desc
;
233 if ($p != $this->overview
[$p]->parent
) {
234 $p = $this->overview
[$p]->parent
;
241 Banana
::$protocole->updateSpool($messages);
245 private function updateUnread($since)
251 $newpostsids = Banana
::$protocole->getNewIndexes($since);
253 if (empty($newpostsids)) {
257 if (!is_array($this->ids
)) {
258 $this->ids
= array();
260 $newpostsids = array_intersect($newpostsids, array_keys($this->ids
));
261 foreach ($newpostsids as $mid) {
262 $id = $this->ids
[$mid];
263 if ($this->overview
[$id]->isread
) {
264 $this->overview
[$id]->isread
= false
;
266 $this->overview
[$id]->descunread ++
;
267 $id = $this->overview
[$id]->parent
;
273 public function setMode($mode)
277 case Banana
::SPOOL_UNREAD
:
278 foreach ($this->roots
as $k=>$i) {
279 if ($this->overview
[$i]->descunread
== 0) {
281 unset($this->roots
[$k]);
288 /** Mark the given id as read
289 * @param id MSGNUM of post
291 public function markAsRead($id)
293 if (!$this->overview
[$id]->isread
) {
294 $this->overview
[$id]->isread
= true
;
296 $this->overview
[$id]->descunread
--;
297 $id = $this->overview
[$id]->parent
;
302 /** Mark all unread messages as read
304 public function markAllAsRead(array &$array = null
)
306 if (is_null($array)) {
307 $array =& $this->roots
;
309 if (!is_array($this->roots
)) {
312 foreach ($array as $id) {
313 if (!$this->overview
[$id]->isread
) {
314 $this->markAsRead($id);
316 if ($this->overview
[$id]->descunread
) {
317 $this->markAllAsRead($this->overview
[$id]->children
);
322 /** kill post and childrens
323 * @param $_id MSGNUM of post
325 private function killdesc($_id)
327 if (sizeof($this->overview
[$_id]->children
)) {
328 foreach ($this->overview
[$_id]->children
as $c) {
332 unset($this->overview
[$_id]);
333 if (($msgid = array_search($_id, $this->ids
)) !== false
) {
334 unset($this->ids
[$msgid]);
338 /** delete a post from overview
339 * @param $_id MSGNUM of post
341 public function delid($_id, $write = true
)
343 if (isset($this->overview
[$_id])) {
344 if (sizeof($this->overview
[$_id]->parent
)) {
345 $this->overview
[$this->overview
[$_id]->parent
]->children
=
346 array_diff($this->overview
[$this->overview
[$_id]->parent
]->children
, array($_id));
347 if (sizeof($this->overview
[$_id]->children
)) {
348 $this->overview
[$this->overview
[$_id]->parent
]->children
=
349 array_merge($this->overview
[$this->overview
[$_id]->parent
]->children
, $this->overview
[$_id]->children
);
350 foreach ($this->overview
[$_id]->children
as $c) {
351 $this->overview
[$c]->parent
= $this->overview
[$_id]->parent
;
352 $this->overview
[$c]->parent_direct
= false
;
355 $p = $this->overview
[$_id]->parent
;
357 $this->overview
[$p]->desc
--;
358 $p = $this->overview
[$p]->parent
;
360 } elseif (sizeof($this->overview
[$_id]->children
)) {
361 foreach ($this->overview
[$_id]->children
as $c) {
362 $this->overview
[$c]->parent
= null
;
365 unset($this->overview
[$_id]);
366 $msgid = array_search($_id, $this->ids
);
368 unset($this->ids
[$msgid]);
372 $this->markAllAsRead();
378 private function formatDate($stamp)
380 $today = intval(time() / (24*3600));
381 $dday = intval($stamp / (24*3600));
383 if ($today == $dday) {
385 } elseif ($today == 1 +
$dday) {
386 $format = _b_('hier')." %H:%M";
387 } elseif ($today < 7 +
$dday) {
388 $format = '%a %H:%M';
390 $format = '%a %e %b';
392 return utf8_encode(strftime($format, $stamp));
395 /** displays children tree of a post
396 * @param $_id INTEGER MSGNUM of post
397 * @param $_index INTEGER linear number of post in the tree
398 * @param $_first INTEGER linear number of first post displayed
399 * @param $_last INTEGER linear number of last post displayed
400 * @param $_ref STRING MSGNUM of current post
401 * @param $_pfx_node STRING prefix used for current node
402 * @param $_pfx_end STRING prefix used for children of current node
403 * @param $_head BOOLEAN true if first post in thread
405 * If you want to analyse subject, you can define the function hook_formatDisplayHeader
407 private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true
)
409 static $spfx_f, $spfx_n, $spfx_Tnd, $spfx_Lnd, $spfx_snd, $spfx_T, $spfx_L, $spfx_s, $spfx_e, $spfx_I;
410 if (!isset($spfx_f)) {
411 $spfx_f = Banana
::$page->makeImg(Array('img' => 'k1', 'alt' => 'o', 'height' => 21, 'width' => 9));
412 $spfx_n = Banana
::$page->makeImg(Array('img' => 'k2', 'alt' => '*', 'height' => 21, 'width' => 9));
413 $spfx_Tnd = Banana
::$page->makeImg(Array('img' => 'T-direct', 'alt' => '+', 'height' => 21, 'width' => 12));
414 $spfx_Lnd = Banana
::$page->makeImg(Array('img' => 'L-direct', 'alt' => '`', 'height' => 21, 'width' => 12));
415 $spfx_snd = Banana
::$page->makeImg(Array('img' => 's-direct', 'alt' => '-', 'height' => 21, 'width' => 5));
416 $spfx_T = Banana
::$page->makeImg(Array('img' => 'T', 'alt' => '+', 'height' => 21, 'width' => 12));
417 $spfx_L = Banana
::$page->makeImg(Array('img' => 'L', 'alt' => '`', 'height' => 21, 'width' => 12));
418 $spfx_s = Banana
::$page->makeImg(Array('img' => 's', 'alt' => '-', 'height' => 21, 'width' => 5));
419 $spfx_e = Banana
::$page->makeImg(Array('img' => 'e', 'alt' => ' ', 'height' => 21, 'width' => 12));
420 $spfx_I = Banana
::$page->makeImg(Array('img' => 'I', 'alt' => '|', 'height' => 21, 'width' => 12));
423 $overview =& $this->overview
[$_id];
424 if ($_index +
$overview->desc
< $_first ||
$_index > $_last) {
429 if ($_index >= $_first) {
430 $hc = empty($overview->children
);
432 $res .= '<tr class="' . ($_index%2 ?
'pair' : 'impair') . ($overview->isread ?
'' : ' new') . "\">\n";
433 $res .= '<td class="date">' . $this->formatDate($overview->date
) . " </td>\n";
434 $res .= '<td class="subj' . ($_index == $_ref ?
' cur' : '') . '">'
435 . $_pfx_node .($hc ?
($_head ?
$spfx_f : ($overview->parent_direct ?
$spfx_s : $spfx_snd)) : $spfx_n);
436 $subject = $overview->subject
;
437 if (function_exists('hook_formatDisplayHeader')) {
438 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true
);
440 $subject = banana_catchFormats(banana_htmlentities(stripslashes($subject)));
443 if (empty($subject)) {
444 $subject = _b_('(pas de sujet)');
446 if ($_index != $_ref) {
447 $subject = Banana
::$page->makeLink(Array('group' => $this->group
, 'artid' => $_id,
448 'text' => $subject, 'popup' => $subject));
450 $res .= ' ' . $subject . $link;
451 $res .= "</td>\n<td class='from'>" . BananaMessage
::formatFrom($overview->from
) . "</td>\n</tr>";
459 $children = $overview->children
;
460 while ($child = array_shift($children)) {
461 $overview =& $this->overview
[$child];
462 if ($_index > $_last) {
465 if ($_index +
$overview->desc
>= $_first) {
466 if (sizeof($children)) {
467 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
468 $_pfx_end . ($overview->parent_direct ?
$spfx_T : $spfx_Tnd),
469 $_pfx_end . $spfx_I, false
);
471 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
472 $_pfx_end . ($overview->parent_direct ?
$spfx_L : $spfx_Lnd),
473 $_pfx_end . $spfx_e, false
);
476 $_index +
= $overview->desc
;
482 /** Displays overview
483 * @param $_first INTEGER MSGNUM of first post
484 * @param $_last INTEGER MSGNUM of last post
485 * @param $_ref STRING MSGNUM of current/selectionned post
487 public function toHtml($first = 0, $overview = false
)
493 $_last = $first + Banana
::$spool_tmax - 1;
496 $_ref = $this->getNdx($first);
497 $_last = $_ref + Banana
::$spool_tafter;
498 $_first = $_ref - Banana
::$spool_tbefore;
504 foreach ($this->roots
as $id) {
505 $res .= $this->_to_html($id, $index, $_first, $_last, $_ref);
506 $index +
= $this->overview
[$id]->desc
;
507 if ($index > $_last) {
514 /** computes linear post index
515 * @param $_id INTEGER MSGNUM of post
516 * @return INTEGER linear index of post
518 public function getNdX($_id)
523 $id_parent = $this->overview
[$id_cur]->parent
;
524 if (is_null($id_parent)) break;
525 $pos = array_search($id_cur, $this->overview
[$id_parent]->children
);
527 for ($i = 0; $i < $pos ; $i++
) {
528 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
532 $id_cur = $id_parent;
535 foreach ($this->roots
as $i) {
539 $ndx +
= $this->overview
[$i]->desc
;
544 /** Return root message of the given thread
545 * @param id INTEGER id of a message
547 public function root($id)
551 $id_parent = $this->overview
[$id_cur]->parent
;
552 if (is_null($id_parent)) break;
553 $id_cur = $id_parent;
558 /** Returns previous thread root index
559 * @param id INTEGER message number
561 public function prevThread($id)
563 $root = $this->root($id);
565 foreach ($this->roots
as $i) {
574 /** Returns next thread root index
575 * @param id INTEGER message number
577 public function nextThread($id)
579 $root = $this->root($id);
581 foreach ($this->roots
as $i) {
592 /** Return prev post in the thread
593 * @param id INTEGER message number
595 public function prevPost($id)
597 $parent = $this->overview
[$id]->parent
;
598 if (is_null($parent)) {
602 foreach ($this->overview
[$parent]->children
as $child) {
611 /** Return next post in the thread
612 * @param id INTEGER message number
614 public function nextPost($id)
616 if (count($this->overview
[$id]->children
) != 0) {
617 return $this->overview
[$id]->children
[0];
622 $parent = $this->overview
[$cur]->parent
;
623 if (is_null($parent)) {
627 foreach ($this->overview
[$parent]->children
as $child) {
631 if ($child == $cur) {
640 /** Look for an unread message in the thread rooted by the message
641 * @param id INTEGER message number
643 private function _nextUnread($id)
645 if (!$this->overview
[$id]->isread
) {
648 foreach ($this->overview
[$id]->children
as $child) {
649 $unread = $this->_nextUnread($child);
650 if (!is_null($unread)) {
657 /** Find next unread message
658 * @param id INTEGER message number
660 public function nextUnread($id = null
)
663 // Look in message children
664 foreach ($this->overview
[$id]->children
as $child) {
665 $next = $this->_nextUnread($child);
666 if (!is_null($next)) {
672 // Look in current thread
675 $parent = is_null($cur) ? null
: $this->overview
[$cur]->parent
;
676 $ok = is_null($cur) ? true
: false
;
677 if (!is_null($parent)) {
678 $array = &$this->overview
[$parent]->children
;
680 $array = &$this->roots
;
682 foreach ($array as $child) {
684 $next = $this->_nextUnread($child);
685 if (!is_null($next)) {
689 if ($child == $cur) {
694 } while(!is_null($cur));
699 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: