610d192b6c964cd58967673983345fd881a8dcc9
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;
69 /** array msgid => msgnum */
76 * @param $_group STRING group name
77 * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
78 * @param $_since INTEGER time stamp (used for read/unread)
80 protected function __construct($group)
82 $this->version
= BANANA_SPOOL_VERSION
;
83 $this->group
= $group;
86 public static function getSpool($group, $since = 0)
88 if (!is_null(Banana
::$spool) && Banana
::$spool->group
== $group) {
89 $spool = Banana
::$spool;
91 $spool = BananaSpool
::readFromFile($group);
93 if (is_null($spool)) {
94 $spool = new BananaSpool($group);
96 Banana
::$spool =& $spool;
98 $spool->updateUnread($since);
102 private static function spoolFilename($group)
104 $file = dirname(dirname(__FILE__
));
105 $file .= '/spool/' . Banana
::$protocole->name() . '/';
106 if (!is_dir($file)) {
109 $url = parse_url(Banana
::$host);
110 if (isset($url['host'])) {
111 $file .= $url['host'] . '_';
113 if (isset($url['port'])) {
114 $file .= $url['port'] . '_';
120 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
) {
133 private function compare($a, $b)
135 return ($b->date
>= $a->date
);
138 private function saveToFile()
140 $file = BananaSpool
::spoolFilename($this->group
);
141 uasort($this->overview
, array($this, 'compare'));
143 $this->roots
= Array();
144 foreach($this->overview
as $id=>$msg) {
145 if (is_null($msg->parent
)) {
146 $this->roots
[] = $id;
150 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
::$maxspool && Banana
::$maxspool < $msgnum) {
165 $first = $last - Banana
::$maxspool;
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))
187 $this->delid($id, false
);
191 if (!empty($this->overview
)) {
192 $first = max(array_keys($this->overview
))+
1;
198 private function update(&$first, &$last, $msgnum)
200 if ($first >= $last ||
!$msgnum) {
204 $messages =& Banana
::$protocole->getMessageHeaders($first, $last,
205 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
207 if (!is_array($this->ids
)) {
208 $this->ids
= array();
210 foreach ($messages as $id=>&$message) {
211 $this->ids
[$message['message-id']] = $id;
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
;
264 $this->overview
[$id]->descunread
= 1;
266 $this->overview
[$id]->descunread ++
;
267 $id = $this->overview
[$id]->parent
;
273 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 /** kill post and childrens
288 * @param $_id MSGNUM of post
290 private function killdesc($_id)
292 if (sizeof($this->overview
[$_id]->children
)) {
293 foreach ($this->overview
[$_id]->children
as $c) {
297 unset($this->overview
[$_id]);
298 if (($msgid = array_search($_id, $this->ids
)) !== false
) {
299 unset($this->ids
[$msgid]);
303 /** delete a post from overview
304 * @param $_id MSGNUM of post
306 public function delid($_id, $write = true
)
308 if (isset($this->overview
[$_id])) {
309 if (sizeof($this->overview
[$_id]->parent
)) {
310 $this->overview
[$this->overview
[$_id]->parent
]->children
=
311 array_diff($this->overview
[$this->overview
[$_id]->parent
]->children
, array($_id));
312 if (sizeof($this->overview
[$_id]->children
)) {
313 $this->overview
[$this->overview
[$_id]->parent
]->children
=
314 array_merge($this->overview
[$this->overview
[$_id]->parent
]->children
, $this->overview
[$_id]->children
);
315 foreach ($this->overview
[$_id]->children
as $c) {
316 $this->overview
[$c]->parent
= $this->overview
[$_id]->parent
;
317 $this->overview
[$c]->parent_direct
= false
;
320 $p = $this->overview
[$_id]->parent
;
322 $this->overview
[$p]->desc
--;
323 $p = $this->overview
[$p]->parent
;
325 } elseif (sizeof($this->overview
[$_id]->children
)) {
326 foreach ($this->overview
[$_id]->children
as $c) {
327 $this->overview
[$c]->parent
= null
;
330 unset($this->overview
[$_id]);
331 $msgid = array_search($_id, $this->ids
);
333 unset($this->ids
[$msgid]);
342 private function formatDate($stamp)
344 $today = intval(time() / (24*3600));
345 $dday = intval($stamp / (24*3600));
347 if ($today == $dday) {
349 } elseif ($today == 1 +
$dday) {
350 $format = _b_('hier')." %H:%M";
351 } elseif ($today < 7 +
$dday) {
352 $format = '%a %H:%M';
354 $format = '%a %e %b';
356 return utf8_encode(strftime($format, $stamp));
359 /** displays children tree of a post
360 * @param $_id INTEGER MSGNUM of post
361 * @param $_index INTEGER linear number of post in the tree
362 * @param $_first INTEGER linear number of first post displayed
363 * @param $_last INTEGER linear number of last post displayed
364 * @param $_ref STRING MSGNUM of current post
365 * @param $_pfx_node STRING prefix used for current node
366 * @param $_pfx_end STRING prefix used for children of current node
367 * @param $_head BOOLEAN true if first post in thread
369 * If you want to analyse subject, you can define the function hook_getSubject(&$subject) which
370 * take the subject as a reference parameter, transform this subject to be displaid in the spool
371 * view and return a string. This string will be put after the subject.
373 private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true
)
375 static $spfx_f, $spfx_n, $spfx_Tnd, $spfx_Lnd, $spfx_snd, $spfx_T, $spfx_L, $spfx_s, $spfx_e, $spfx_I;
376 if (!isset($spfx_f)) {
377 $spfx_f = Banana
::$page->makeImg(Array('img' => 'k1', 'alt' => 'o', 'height' => 21, 'width' => 9));
378 $spfx_n = Banana
::$page->makeImg(Array('img' => 'k2', 'alt' => '*', 'height' => 21, 'width' => 9));
379 $spfx_Tnd = Banana
::$page->makeImg(Array('img' => 'T-direct', 'alt' => '+', 'height' => 21, 'width' => 12));
380 $spfx_Lnd = Banana
::$page->makeImg(Array('img' => 'L-direct', 'alt' => '`', 'height' => 21, 'width' => 12));
381 $spfx_snd = Banana
::$page->makeImg(Array('img' => 's-direct', 'alt' => '-', 'height' => 21, 'width' => 5));
382 $spfx_T = Banana
::$page->makeImg(Array('img' => 'T', 'alt' => '+', 'height' => 21, 'width' => 12));
383 $spfx_L = Banana
::$page->makeImg(Array('img' => 'L', 'alt' => '`', 'height' => 21, 'width' => 12));
384 $spfx_s = Banana
::$page->makeImg(Array('img' => 's', 'alt' => '-', 'height' => 21, 'width' => 5));
385 $spfx_e = Banana
::$page->makeImg(Array('img' => 'e', 'alt' => ' ', 'height' => 21, 'width' => 12));
386 $spfx_I = Banana
::$page->makeImg(Array('img' => 'I', 'alt' => '|', 'height' => 21, 'width' => 12));
389 $overview =& $this->overview
[$_id];
390 if ($_index +
$overview->desc
< $_first ||
$_index > $_last) {
395 if ($_index >= $_first) {
396 $hc = empty($overview->children
);
398 $res .= '<tr class="' . ($_index%2 ?
'pair' : 'impair') . ($overview->isread ?
'' : ' new') . "\">\n";
399 $res .= '<td class="date">' . $this->formatDate($overview->date
) . " </td>\n";
400 $res .= '<td class="subj' . ($_index == $_ref ?
' cur' : '') . '">'
401 . $_pfx_node .($hc ?
($_head ?
$spfx_f : ($overview->parent_direct ?
$spfx_s : $spfx_snd)) : $spfx_n);
402 $subject = $overview->subject
;
403 if (empty($subject)) {
404 $subject = _b_('(pas de sujet)');
407 if (function_exists('hook_getSubject')) {
408 $link = hook_getSubject($subject);
410 $subject = banana_catchFormats($subject);
411 if ($_index != $_ref) {
412 $subject = Banana
::$page->makeLink(Array('group' => $this->group
, 'artid' => $_id,
413 'text' => $subject, 'popup' => $subject));
415 $res .= ' ' . $subject . $link;
416 $res .= "</td>\n<td class='from'>" . BananaMessage
::formatFrom($overview->from
) . "</td>\n</tr>";
424 $children = $overview->children
;
425 while ($child = array_shift($children)) {
426 $overview =& $this->overview
[$child];
427 if ($_index > $_last) {
430 if ($_index +
$overview->desc
>= $_first) {
431 if (sizeof($children)) {
432 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
433 $_pfx_end . ($overview->parent_direct ?
$spfx_T : $spfx_Tnd),
434 $_pfx_end . $spfx_I, false
);
436 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
437 $_pfx_end . ($overview->parent_direct ?
$spfx_L : $spfx_Lnd),
438 $_pfx_end . $spfx_e, false
);
441 $_index +
= $overview->desc
;
447 /** Displays overview
448 * @param $_first INTEGER MSGNUM of first post
449 * @param $_last INTEGER MSGNUM of last post
450 * @param $_ref STRING MSGNUM of current/selectionned post
452 public function toHtml($first = 0, $overview = false
)
458 $_last = $first + Banana
::$tmax - 1;
461 $_ref = $this->getNdx($first);
462 $_last = $_ref + Banana
::$tafter;
463 $_first = $_ref - Banana
::$tbefore;
469 foreach ($this->roots
as $id) {
470 $res .= $this->_to_html($id, $index, $_first, $_last, $_ref);
471 $index +
= $this->overview
[$id]->desc
;
472 if ($index > $_last) {
479 /** computes linear post index
480 * @param $_id INTEGER MSGNUM of post
481 * @return INTEGER linear index of post
483 public function getNdX($_id)
488 $id_parent = $this->overview
[$id_cur]->parent
;
489 if (is_null($id_parent)) break;
490 $pos = array_search($id_cur, $this->overview
[$id_parent]->children
);
492 for ($i = 0; $i < $pos ; $i++
) {
493 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
497 $id_cur = $id_parent;
500 foreach ($this->roots
as $i) {
504 $ndx +
= $this->overview
[$i]->desc
;
509 /** Return root message of the given thread
510 * @param id INTEGER id of a message
512 public function root($id)
516 $id_parent = $this->overview
[$id_cur]->parent
;
517 if (is_null($id_parent)) break;
518 $id_cur = $id_parent;
523 /** Returns previous thread root index
524 * @param id INTEGER message number
526 public function prevThread($id)
528 $root = $this->root($id);
530 foreach ($this->roots
as $i) {
539 /** Returns next thread root index
540 * @param id INTEGER message number
542 public function nextThread($id)
544 $root = $this->root($id);
546 foreach ($this->roots
as $i) {
557 /** Return prev post in the thread
558 * @param id INTEGER message number
560 public function prevPost($id)
562 $parent = $this->overview
[$id]->parent
;
563 if (is_null($parent)) {
567 foreach ($this->overview
[$parent]->children
as $child) {
576 /** Return next post in the thread
577 * @param id INTEGER message number
579 public function nextPost($id)
581 if (count($this->overview
[$id]->children
) != 0) {
582 return $this->overview
[$id]->children
[0];
587 $parent = $this->overview
[$cur]->parent
;
588 if (is_null($parent)) {
592 foreach ($this->overview
[$parent]->children
as $child) {
596 if ($child == $cur) {
605 /** Look for an unread message in the thread rooted by the message
606 * @param id INTEGER message number
608 private function _nextUnread($id)
610 if (!$this->overview
[$id]->isread
) {
613 foreach ($this->overview
[$id]->children
as $child) {
614 $unread = $this->_nextUnread($child);
615 if (!is_null($unread)) {
622 /** Find next unread message
623 * @param id INTEGER message number
625 public function nextUnread($id = null
)
628 // Look in message children
629 foreach ($this->overview
[$id]->children
as $child) {
630 $next = $this->_nextUnread($child);
631 if (!is_null($next)) {
637 // Look in current thread
640 $parent = is_null($cur) ? null
: $this->overview
[$cur]->parent
;
641 $ok = is_null($cur) ? true
: false
;
642 if (!is_null($parent)) {
643 $array = &$this->overview
[$parent]->children
;
645 $array = &$this->roots
;
647 foreach ($array as $child) {
649 $next = $this->_nextUnread($child);
650 if (!is_null($next)) {
654 if ($child == $cur) {
659 } while(!is_null($cur));
664 // vim:set et sw=4 sts=4 ts=4