9de8df821ba939be6da903f7e7dcf2f2bcaf8197
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
= stripslashes($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)
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;
100 $spool->updateUnread($since);
104 private static function spoolFilename($group)
106 $file = dirname(dirname(__FILE__
));
107 $file .= '/spool/' . Banana
::$protocole->name() . '/';
108 if (!is_dir($file)) {
111 return $file . Banana
::$protocole->filename();
114 private static function readFromFile($group)
116 $file = BananaSpool
::spoolFilename($group);
117 if (!file_exists($file)) {
120 $spool = unserialize(file_get_contents($file));
121 if ($spool->version
!= BANANA_SPOOL_VERSION ||
$spool->mode
!= Banana
::SPOOL_ALL
) {
127 private function compare($a, $b)
129 return ($b->date
>= $a->date
);
132 private function saveToFile()
134 $file = BananaSpool
::spoolFilename($this->group
);
135 uasort($this->overview
, array($this, 'compare'));
137 $this->roots
= Array();
138 foreach($this->overview
as $id=>$msg) {
139 if (is_null($msg->parent
)) {
140 $this->roots
[] = $id;
144 if ($this->mode
== Banana
::SPOOL_ALL
) {
145 file_put_contents($file, serialize($this));
149 private function build()
153 // Compute the range of indexes
154 list($msgnum, $first, $last) = Banana
::$protocole->getIndexes();
155 if ($last < $first) {
156 $threshold = $firt +
$msgnum - $last;
157 $threshold = (int)(log($threshold)/log(2));
158 $threshold = (2 ^
($threshold +
1)) - 1;
160 if (Banana
::$spool_max && Banana
::$spool_max < $msgnum) {
161 $first = $last - Banana
::$spool_max;
163 $first +
= $threshold;
166 $clean = $this->clean($first, $last, $msgnum);
167 $update = $this->update($first, $last, $msgnum);
169 if ($clean ||
$update) {
174 private function clean(&$first, &$last, $msgnum)
177 if (is_array($this->overview
)) {
178 $mids = array_keys($this->overview
);
179 foreach ($mids as $id) {
180 if (($first <= $last && ($id < $first ||
$id > $last))
181 ||
($first > $last && $id < $first && $id > $last)) {
182 $this->delid($id, false
);
186 if (!empty($this->overview
)) {
187 $first = max(array_keys($this->overview
))+
1;
193 private function update(&$first, &$last, $msgnum)
195 if ($first > $last ||
!$msgnum) {
199 $messages =& Banana
::$protocole->getMessageHeaders($first, $last,
200 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
202 if (!is_array($this->ids
)) {
203 $this->ids
= array();
205 foreach ($messages as $id=>&$message) {
206 $this->ids
[$message['message-id']] = $id;
209 foreach ($messages as $id=>&$message) {
210 if (!isset($this->overview
[$id])) {
211 $this->overview
[$id] = new BananaSpoolHead($message);
213 $msg =& $this->overview
[$id];
214 $msgrefs = BananaMessage
::formatReferences($message);
215 $parents = preg_grep('/^\d+$/', $msgrefs);
216 $msg->parent
= array_pop($parents);
217 $msg->parent_direct
= preg_match('/^\d+$/', array_pop($msgrefs));
219 if (!is_null($p = $msg->parent
)) {
220 if (empty($this->overview
[$p])) {
221 $this->overview
[$p] = new BananaSpoolHead($messages[$p]);
223 $this->overview
[$p]->children
[] = $id;
225 while (!is_null($p)) {
226 $this->overview
[$p]->desc +
= $msg->desc
;
227 if ($p != $this->overview
[$p]->parent
) {
228 $p = $this->overview
[$p]->parent
;
235 Banana
::$protocole->updateSpool($messages);
239 private function updateUnread($since)
245 $newpostsids = Banana
::$protocole->getNewIndexes($since);
247 if (empty($newpostsids)) {
251 if (!is_array($this->ids
)) {
252 $this->ids
= array();
254 $newpostsids = array_intersect($newpostsids, array_keys($this->ids
));
255 foreach ($newpostsids as $mid) {
256 $id = $this->ids
[$mid];
257 if ($this->overview
[$id]->isread
) {
258 $this->overview
[$id]->isread
= false
;
260 $this->overview
[$id]->descunread ++
;
261 $id = $this->overview
[$id]->parent
;
267 public function setMode($mode)
271 case Banana
::SPOOL_UNREAD
:
272 foreach ($this->roots
as $k=>$i) {
273 if ($this->overview
[$i]->descunread
== 0) {
275 unset($this->roots
[$k]);
282 /** Mark the given id as read
283 * @param id MSGNUM of post
285 public function markAsRead($id)
287 if (!$this->overview
[$id]->isread
) {
288 $this->overview
[$id]->isread
= true
;
290 $this->overview
[$id]->descunread
--;
291 $id = $this->overview
[$id]->parent
;
296 /** Mark all unread messages as read
298 public function markAllAsRead(array &$array = null
)
300 if (is_null($array)) {
301 $array =& $this->roots
;
303 foreach ($array as $id) {
304 if (!$this->overview
[$id]->isread
) {
305 $this->markAsRead($id);
307 if ($this->overview
[$id]->descunread
) {
308 $this->markAllAsRead($this->overview
[$id]->children
);
313 /** kill post and childrens
314 * @param $_id MSGNUM of post
316 private function killdesc($_id)
318 if (sizeof($this->overview
[$_id]->children
)) {
319 foreach ($this->overview
[$_id]->children
as $c) {
323 unset($this->overview
[$_id]);
324 if (($msgid = array_search($_id, $this->ids
)) !== false
) {
325 unset($this->ids
[$msgid]);
329 /** delete a post from overview
330 * @param $_id MSGNUM of post
332 public function delid($_id, $write = true
)
334 if (isset($this->overview
[$_id])) {
335 if (sizeof($this->overview
[$_id]->parent
)) {
336 $this->overview
[$this->overview
[$_id]->parent
]->children
=
337 array_diff($this->overview
[$this->overview
[$_id]->parent
]->children
, array($_id));
338 if (sizeof($this->overview
[$_id]->children
)) {
339 $this->overview
[$this->overview
[$_id]->parent
]->children
=
340 array_merge($this->overview
[$this->overview
[$_id]->parent
]->children
, $this->overview
[$_id]->children
);
341 foreach ($this->overview
[$_id]->children
as $c) {
342 $this->overview
[$c]->parent
= $this->overview
[$_id]->parent
;
343 $this->overview
[$c]->parent_direct
= false
;
346 $p = $this->overview
[$_id]->parent
;
348 $this->overview
[$p]->desc
--;
349 $p = $this->overview
[$p]->parent
;
351 } elseif (sizeof($this->overview
[$_id]->children
)) {
352 foreach ($this->overview
[$_id]->children
as $c) {
353 $this->overview
[$c]->parent
= null
;
356 unset($this->overview
[$_id]);
357 $msgid = array_search($_id, $this->ids
);
359 unset($this->ids
[$msgid]);
368 private function formatDate($stamp)
370 $today = intval(time() / (24*3600));
371 $dday = intval($stamp / (24*3600));
373 if ($today == $dday) {
375 } elseif ($today == 1 +
$dday) {
376 $format = _b_('hier')." %H:%M";
377 } elseif ($today < 7 +
$dday) {
378 $format = '%a %H:%M';
380 $format = '%a %e %b';
382 return utf8_encode(strftime($format, $stamp));
385 /** displays children tree of a post
386 * @param $_id INTEGER MSGNUM of post
387 * @param $_index INTEGER linear number of post in the tree
388 * @param $_first INTEGER linear number of first post displayed
389 * @param $_last INTEGER linear number of last post displayed
390 * @param $_ref STRING MSGNUM of current post
391 * @param $_pfx_node STRING prefix used for current node
392 * @param $_pfx_end STRING prefix used for children of current node
393 * @param $_head BOOLEAN true if first post in thread
395 * If you want to analyse subject, you can define the function hook_getSubject(&$subject) which
396 * take the subject as a reference parameter, transform this subject to be displaid in the spool
397 * view and return a string. This string will be put after the subject.
399 private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true
)
401 static $spfx_f, $spfx_n, $spfx_Tnd, $spfx_Lnd, $spfx_snd, $spfx_T, $spfx_L, $spfx_s, $spfx_e, $spfx_I;
402 if (!isset($spfx_f)) {
403 $spfx_f = Banana
::$page->makeImg(Array('img' => 'k1', 'alt' => 'o', 'height' => 21, 'width' => 9));
404 $spfx_n = Banana
::$page->makeImg(Array('img' => 'k2', 'alt' => '*', 'height' => 21, 'width' => 9));
405 $spfx_Tnd = Banana
::$page->makeImg(Array('img' => 'T-direct', 'alt' => '+', 'height' => 21, 'width' => 12));
406 $spfx_Lnd = Banana
::$page->makeImg(Array('img' => 'L-direct', 'alt' => '`', 'height' => 21, 'width' => 12));
407 $spfx_snd = Banana
::$page->makeImg(Array('img' => 's-direct', 'alt' => '-', 'height' => 21, 'width' => 5));
408 $spfx_T = Banana
::$page->makeImg(Array('img' => 'T', 'alt' => '+', 'height' => 21, 'width' => 12));
409 $spfx_L = Banana
::$page->makeImg(Array('img' => 'L', 'alt' => '`', 'height' => 21, 'width' => 12));
410 $spfx_s = Banana
::$page->makeImg(Array('img' => 's', 'alt' => '-', 'height' => 21, 'width' => 5));
411 $spfx_e = Banana
::$page->makeImg(Array('img' => 'e', 'alt' => ' ', 'height' => 21, 'width' => 12));
412 $spfx_I = Banana
::$page->makeImg(Array('img' => 'I', 'alt' => '|', 'height' => 21, 'width' => 12));
415 $overview =& $this->overview
[$_id];
416 if ($_index +
$overview->desc
< $_first ||
$_index > $_last) {
421 if ($_index >= $_first) {
422 $hc = empty($overview->children
);
424 $res .= '<tr class="' . ($_index%2 ?
'pair' : 'impair') . ($overview->isread ?
'' : ' new') . "\">\n";
425 $res .= '<td class="date">' . $this->formatDate($overview->date
) . " </td>\n";
426 $res .= '<td class="subj' . ($_index == $_ref ?
' cur' : '') . '">'
427 . $_pfx_node .($hc ?
($_head ?
$spfx_f : ($overview->parent_direct ?
$spfx_s : $spfx_snd)) : $spfx_n);
428 $subject = $overview->subject
;
429 if (empty($subject)) {
430 $subject = _b_('(pas de sujet)');
433 if (function_exists('hook_getSubject')) {
434 $link = hook_getSubject($subject);
436 $subject = banana_catchFormats($subject);
437 if ($_index != $_ref) {
438 $subject = Banana
::$page->makeLink(Array('group' => $this->group
, 'artid' => $_id,
439 'text' => $subject, 'popup' => $subject));
441 $res .= ' ' . $subject . $link;
442 $res .= "</td>\n<td class='from'>" . BananaMessage
::formatFrom($overview->from
) . "</td>\n</tr>";
450 $children = $overview->children
;
451 while ($child = array_shift($children)) {
452 $overview =& $this->overview
[$child];
453 if ($_index > $_last) {
456 if ($_index +
$overview->desc
>= $_first) {
457 if (sizeof($children)) {
458 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
459 $_pfx_end . ($overview->parent_direct ?
$spfx_T : $spfx_Tnd),
460 $_pfx_end . $spfx_I, false
);
462 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
463 $_pfx_end . ($overview->parent_direct ?
$spfx_L : $spfx_Lnd),
464 $_pfx_end . $spfx_e, false
);
467 $_index +
= $overview->desc
;
473 /** Displays overview
474 * @param $_first INTEGER MSGNUM of first post
475 * @param $_last INTEGER MSGNUM of last post
476 * @param $_ref STRING MSGNUM of current/selectionned post
478 public function toHtml($first = 0, $overview = false
)
484 $_last = $first + Banana
::$spool_tmax - 1;
487 $_ref = $this->getNdx($first);
488 $_last = $_ref + Banana
::$spool_tafter;
489 $_first = $_ref - Banana
::$spool_tbefore;
495 foreach ($this->roots
as $id) {
496 $res .= $this->_to_html($id, $index, $_first, $_last, $_ref);
497 $index +
= $this->overview
[$id]->desc
;
498 if ($index > $_last) {
505 /** computes linear post index
506 * @param $_id INTEGER MSGNUM of post
507 * @return INTEGER linear index of post
509 public function getNdX($_id)
514 $id_parent = $this->overview
[$id_cur]->parent
;
515 if (is_null($id_parent)) break;
516 $pos = array_search($id_cur, $this->overview
[$id_parent]->children
);
518 for ($i = 0; $i < $pos ; $i++
) {
519 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
523 $id_cur = $id_parent;
526 foreach ($this->roots
as $i) {
530 $ndx +
= $this->overview
[$i]->desc
;
535 /** Return root message of the given thread
536 * @param id INTEGER id of a message
538 public function root($id)
542 $id_parent = $this->overview
[$id_cur]->parent
;
543 if (is_null($id_parent)) break;
544 $id_cur = $id_parent;
549 /** Returns previous thread root index
550 * @param id INTEGER message number
552 public function prevThread($id)
554 $root = $this->root($id);
556 foreach ($this->roots
as $i) {
565 /** Returns next thread root index
566 * @param id INTEGER message number
568 public function nextThread($id)
570 $root = $this->root($id);
572 foreach ($this->roots
as $i) {
583 /** Return prev post in the thread
584 * @param id INTEGER message number
586 public function prevPost($id)
588 $parent = $this->overview
[$id]->parent
;
589 if (is_null($parent)) {
593 foreach ($this->overview
[$parent]->children
as $child) {
602 /** Return next post in the thread
603 * @param id INTEGER message number
605 public function nextPost($id)
607 if (count($this->overview
[$id]->children
) != 0) {
608 return $this->overview
[$id]->children
[0];
613 $parent = $this->overview
[$cur]->parent
;
614 if (is_null($parent)) {
618 foreach ($this->overview
[$parent]->children
as $child) {
622 if ($child == $cur) {
631 /** Look for an unread message in the thread rooted by the message
632 * @param id INTEGER message number
634 private function _nextUnread($id)
636 if (!$this->overview
[$id]->isread
) {
639 foreach ($this->overview
[$id]->children
as $child) {
640 $unread = $this->_nextUnread($child);
641 if (!is_null($unread)) {
648 /** Find next unread message
649 * @param id INTEGER message number
651 public function nextUnread($id = null
)
654 // Look in message children
655 foreach ($this->overview
[$id]->children
as $child) {
656 $next = $this->_nextUnread($child);
657 if (!is_null($next)) {
663 // Look in current thread
666 $parent = is_null($cur) ? null
: $this->overview
[$cur]->parent
;
667 $ok = is_null($cur) ? true
: false
;
668 if (!is_null($parent)) {
669 $array = &$this->overview
[$parent]->children
;
671 $array = &$this->roots
;
673 foreach ($array as $child) {
675 $next = $this->_nextUnread($child);
676 if (!is_null($next)) {
680 if ($child == $cur) {
685 } while(!is_null($cur));
690 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: