ace19f7277dde077404d0aba500faba7a184384c
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';
11 require_once dirname(__FILE__
) . '/tree.inc.php';
13 define('BANANA_SPOOL_VERSION', '0.5.14');
16 * class used in thread overviews
23 /** date (timestamp) */
31 /** reference of parent */
32 public $parent = null
;
33 /** array of children */
34 public $children = Array();
35 /** true if post is read */
37 /** number of posts deeper in this branch of tree */
39 /** same as desc, but counts only unread posts */
41 /** last time the number of children has been updated */
45 public $storage = array();
48 * @param $_date INTEGER timestamp of post
49 * @param $_subject STRING subject of post
50 * @param $_from STRING author of post
51 * @param $_desc INTEGER desc value (1 for a new post)
52 * @param $_read BOOLEAN true if read
53 * @param $_descunread INTEGER descunread value (0 for a new post)
55 public function __construct($id, array &$message)
57 list($name, $from) = BananaMessage
::extractMail($message['from']);
59 $this->msgid
= @$message['message-id'];
60 $this->date
= $message['date'];
61 $this->subject
= @$message['subject'];
62 $this->from
= $message['from'];
63 $this->color
= sprintf('#%06x', abs(crc32($from) %
0xffffff));
66 $this->descunread
= 0;
68 if ($name === $from) {
69 $this->name
= 'Anonymous';
83 public $overview = array();
84 /** array msgid => msgnum */
85 public $ids = array();
87 public $roots = array();
89 /** protocole specific data */
90 public $storage = array();
92 private $unreadnb = 0;
95 * @param $_group STRING group name
96 * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
97 * @param $_since INTEGER time stamp (used for read/unread)
99 protected function __construct($group)
101 $this->version
= BANANA_SPOOL_VERSION
;
102 $this->mode
= Banana
::SPOOL_ALL
;
103 $this->group
= $group;
106 public static function &getSpool($group, $since = 0, $clean = false
)
108 if (!is_null(Banana
::$spool) && Banana
::$spool->group
== $group) {
109 $spool =& Banana
::$spool;
111 $spool =& BananaSpool
::readFromFile($group);
113 if (is_null($spool)) {
114 $spool = new BananaSpool($group);
116 Banana
::$spool =& $spool;
119 $spool->markAllAsRead();
121 $spool->updateUnread($since);
125 public static function getPath($file = null
)
127 $path = Banana
::$spool_root . '/' . Banana
::$protocole->name() . '/' . Banana
::$protocole->filename();
128 if (!is_dir($path)) {
129 if (file_exists($path)) {
132 mkdir($path, 0777, true
);
134 return $path . '/' . $file;
137 private static function spoolFilename()
139 return BananaSpool
::getPath('spool');
142 private static function &readFromFile($group)
145 $file = BananaSpool
::spoolFilename();
146 if (!file_exists($file)) {
149 $spool = unserialize(file_get_contents($file));
150 if ($spool->version
!= BANANA_SPOOL_VERSION ||
$spool->mode
!= Banana
::SPOOL_ALL
) {
154 $spool->markAllAsRead();
158 private function compare($a, $b)
160 return ($b->date
- $a->date
);
163 private function saveToFile()
165 $file = BananaSpool
::spoolFilename();
167 $this->roots
= Array();
168 foreach($this->overview
as &$msg) {
169 if (is_null($msg->parent
)) {
170 $this->roots
[] =& $msg;
173 usort($this->roots
, array($this, 'compare'));
175 if ($this->mode
== Banana
::SPOOL_ALL
) {
176 file_put_contents($file, serialize($this));
180 private function build()
184 // Compute the range of indexes
185 list($msgnum, $first, $last) = Banana
::$protocole->getIndexes();
186 if ($last < $first) {
187 $threshold = $first +
$msgnum - $last;
188 $threshold = (int)(log($threshold)/log(2));
189 $threshold = (2 ^
($threshold +
1)) - 1;
191 if (Banana
::$spool_max && Banana
::$spool_max < $msgnum) {
192 $first = $last - Banana
::$spool_max;
194 $first +
= $threshold;
197 $clean = $this->clean($first, $last, $msgnum);
198 $update = $this->update($first, $last, $msgnum);
200 if ($clean ||
$update) {
205 private function clean(&$first, &$last, $msgnum)
208 if (!empty($this->overview
)) {
209 $mids = array_keys($this->overview
);
210 foreach ($mids as $id) {
211 if (($first <= $last && ($id < $first ||
$id > $last))
212 ||
($first > $last && $id < $first && $id > $last)) {
213 $this->delid($id, false
);
217 if (!empty($this->overview
)) {
218 $first = max(array_keys($this->overview
))+
1;
224 private function update(&$first, &$last, $msgnum)
226 if ($first > $last ||
!$msgnum) {
230 $messages =& Banana
::$protocole->getMessageHeaders($first, $last,
231 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
233 // Build all the new Spool Heads
235 foreach ($messages as $id=>&$message) {
236 if (!isset($this->overview
[$id])) {
237 $this->overview
[$id] = new BananaSpoolHead($id, $message);
238 $head =& $this->overview
[$id];
239 $this->ids
[$head->msgid
] =& $head;
246 foreach ($messages as $id=>&$message) {
247 $msg =& $this->overview
[$id];
248 $parents =& $this->getReferences($message);
249 while (!empty($parents) && ($msg->parent
=== $msg ||
is_null($msg->parent
))) {
250 @$msg->parent
=& array_pop($parents);
253 if (!is_null($msg->parent
)) {
254 $parent =& $msg->parent
;
255 $parent->children
[] =& $msg;
256 while (!is_null($parent)) {
257 $parent->desc +
= $msg->desc
;
258 $parent->time
= $time;
260 if ($parent !== $parent->parent
) {
261 $parent =& $parent->parent
;
268 Banana
::$protocole->updateSpool($messages);
272 public function updateUnread($since)
278 $newpostsids = Banana
::$protocole->getNewIndexes($since);
280 if (empty($newpostsids)) {
284 $newpostsids = array_intersect($newpostsids, array_keys($this->ids
));
285 foreach ($newpostsids as $mid) {
286 $overview =& $this->ids
[$mid];
287 if ($overview->isread
) {
288 $overview->isread
= false
;
289 while (!is_null($overview)) {
290 $overview->descunread++
;
291 $overview =& $overview->parent
;
295 $this->unreadnb +
= count($newpostsids);
297 if (function_exists('hook_listReadMessages')) {
298 $msgs = hook_listReadMessages($this->group
);
299 if (!is_array($msgs)) {
302 foreach ($msgs as $msg) {
303 if (!is_numeric($msg)) {
304 if (!isset($this->ids
[$msg])) {
307 $msg = $this->ids
[$msg]->id
;
309 $this->markAsRead($msg);
314 public function setMode($mode)
318 case Banana
::SPOOL_UNREAD
:
319 $num = max(array_keys($this->overview
));
320 if ($this->overview
[$num]->isread
) {
323 foreach ($this->roots
as &$root) {
324 if ($root->descunread
== 0) {
325 $this->killdesc($root->id
);
332 /** Fetch list of references
334 public function &getReferences(array &$refs)
336 $references = array();
337 if (isset($refs['references'])) {
338 $text = preg_split('/\s/', str_replace('><', '> <', $refs['references']));
339 foreach ($text as $id=>&$value) {
340 if (isset($this->ids
[$value])) {
341 $references[] =& $this->ids
[$value];
344 } elseif (isset($refs['in-reply-to']) && isset($this->ids
[$refs['in-reply-to']])) {
345 $references[] =& $this->ids
[$refs['in-reply-to']];
350 /** Get the tree associated to a given id
352 public function &getTree($id)
354 return BananaTree
::build($id)->show();
357 /** Mark the given id as read
358 * @param id MSGNUM of post
360 public function markAsRead($id)
362 $overview =& $this->overview
[$id];
363 if (!$overview->isread
) {
364 $overview->isread
= true
;
366 while (!is_null($overview)) {
367 $overview->descunread
--;
368 $overview =& $overview->parent
;
373 /** Mark all unread messages as read
375 public function markAllAsRead(array &$array = null
)
377 if (!$this->unreadnb
) {
380 if (is_null($array) && !empty($this->roots
)) {
381 $array =& $this->roots
;
382 } elseif (is_null($array)) {
385 foreach ($array as &$msg) {
387 $this->markAsRead($msg->id
);
388 if (!$this->unreadnb
) {
392 if ($msg->descunread
) {
393 $this->markAllAsRead($msg->children
);
398 /** kill post and childrens
399 * @param $_id MSGNUM of post
401 private function killdesc($_id)
403 $overview =& $this->overview
[$_id];
404 $children =& $overview->children
;
405 if (sizeof($children)) {
406 foreach ($children as &$c) {
407 $this->killdesc($c->id
);
410 unset($this->overview
[$_id]);
411 foreach ($this->roots
as $k=>&$root) {
412 if ($root === $overview) {
413 unset($this->roots
[$k]);
417 unset($this->ids
[$overview->msgid
]);
421 /** delete a post from overview
422 * @param $_id MSGNUM of post
424 public function delid($_id, $write = true
)
426 if (!isset($this->overview
[$_id])) {
430 $overview =& $this->overview
[$_id];
431 // Be sure it is not counted as unread
432 if (!$overview->isread
) {
433 $this->markAsRead($_id);
436 $parent =& $overview->parent
;
438 // Remove from the message tree
439 if (!is_null($parent)) {
441 foreach ($parent->children
as $key=>&$child) {
442 if ($child === $overview) {
443 unset($parent->children
[$key]);
447 if (sizeof($overview->children
)) {
448 foreach ($overview->children
as &$child) {
449 $parent->children
[] =& $child;
450 $child->time
= $time;
451 $child->parent
=& $parent;
454 while (!is_null($parent)) {
456 $parent->time
= $time;
457 $parent =& $parent->parent
;
461 // Remove all refenrences and assign null to the object
462 unset($this->ids
[$overview->msgid
]);
463 unset($this->overview
[$_id]);
464 BananaTree
::kill($_id);
465 foreach ($this->roots
as $k=>&$root) {
466 if ($root === $overview) {
467 unset($this->roots
[$k]);
478 public function formatDate(BananaSpoolHead
$head)
480 $stamp = $head->date
;
481 $today = intval(time() / (24*3600));
482 $dday = intval($stamp / (24*3600));
484 if ($today == $dday) {
486 } elseif ($today == 1 +
$dday) {
487 $format = _b_('hier')." %H:%M";
488 } elseif ($today < 7 +
$dday) {
489 $format = '%a %H:%M';
490 } elseif ($today < 90 +
$dday) {
491 $format = '%a %e %b';
493 $format = '%a %e %b %Y';
495 return strftime($format, $stamp);
498 public function formatSubject(BananaSpoolHead
$head)
500 $subject = $popup = $head->subject
;
502 if (function_exists('hook_formatDisplayHeader')) {
503 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true
);
505 $subject = banana_catchFormats(banana_entities(stripslashes($subject)));
508 if (empty($subject)) {
509 $subject = _b_('(pas de sujet)');
511 if (mb_strlen($subject) > 100) {
512 $subject = mb_substr($subject, 0, 99) . '…';
514 if ($head->id
!== Banana
::$artid) {
515 $subject = Banana
::$page->makeLink(Array('group' => $this->group
, 'artid' => $head->id
,
516 'text' => $subject, 'popup' => $popup));
518 return $subject . $link;
521 public function formatFrom(BananaSpoolHead
$head)
523 return BananaMessage
::formatFrom($head->from
);
526 public function start()
528 if (Banana
::$first) {
529 return Banana
::$first;
531 $first = array_search(Banana
::$artid, $this->roots
);
532 return max(0, $first - Banana
::$spool_tbefore);
536 public function context()
538 return Banana
::$first ? Banana
::$spool_tmax : Banana
::$spool_tcontext;
541 /** Return root message of the given thread
542 * @param id INTEGER id of a message
544 public function &root($id)
546 $parent =& $this->overview
[$id];
547 while (!is_null($parent->parent
)) {
548 $parent =& $parent->parent
;
553 /** Return the last post id with the given subject
556 public function getPostId($subject)
558 $subject = trim($subject);
559 $id = max(array_keys($this->overview
));
560 while (isset($this->overview
[$id])) {
561 $test = $this->overview
[$id]->subject
;
562 if (function_exists('hook_formatDisplayHeader')) {
563 $val = hook_formatDisplayHeader('subject', $test, true
);
564 if (is_array($val)) {
565 $test = banana_html_entity_decode($val[0]);
567 $test = banana_html_entity_decode($val);
571 if ($test == $subject) {
579 /** Returns previous thread root index
580 * @param id INTEGER message number
582 public function prevThread($id)
584 $root =& $this->root($id);
586 foreach ($this->roots
as &$i) {
595 /** Returns next thread root index
596 * @param id INTEGER message number
598 public function nextThread($id)
600 $root =& $this->root($id);
602 foreach ($this->roots
as &$i) {
613 /** Return prev post in the thread
614 * @param id INTEGER message number
616 public function prevPost($id)
618 $parent =& $this->overview
[$id]->parent
;
619 if (is_null($parent)) {
623 foreach ($parent->children
as &$child) {
624 if ($child->id
== $id) {
632 /** Return next post in the thread
633 * @param id INTEGER message number
635 public function nextPost($id)
637 $cur =& $this->overview
[$id];
638 if (count($cur->children
) != 0) {
639 return $cur->children
[0]->id
;
644 $parent =& $cur->parent
;
645 if (is_null($parent)) {
649 foreach ($parent->children
as &$child) {
653 if ($child === $cur) {
662 /** Look for an unread message in the thread rooted by the message
663 * @param id INTEGER message number
665 private function _nextUnread(BananaSpoolHead
$cur)
670 foreach ($cur->children
as &$child) {
671 $unread = $this->_nextUnread($child);
672 if (!is_null($unread)) {
679 /** Find next unread message
680 * @param id INTEGER message number
682 public function nextUnread($id = null
)
684 if (!$this->unreadnb
) {
689 // Look in message children
690 foreach ($this->overview
[$id]->children
as &$child) {
691 $next = $this->_nextUnread($child);
692 if (!is_null($next)) {
698 // Look in current thread
702 $cur =& $this->overview
[$id];
709 $parent =& $cur->parent
;
712 if (!is_null($parent)) {
713 $array =& $parent->children
;
715 $array =& $this->roots
;
717 foreach ($array as &$child) {
719 $next = $this->_nextUnread($child);
720 if (!is_null($next)) {
724 if ($child === $cur) {
729 } while(!is_null($cur));
733 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: