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 if (!empty($messages)) {
236 foreach ($messages as $id=>&$message) {
237 if (!isset($this->overview
[$id])) {
238 $this->overview
[$id] = new BananaSpoolHead($id, $message);
239 $head =& $this->overview
[$id];
240 $this->ids
[$head->msgid
] =& $head;
247 foreach ($messages as $id=>&$message) {
248 $msg =& $this->overview
[$id];
249 $parents =& $this->getReferences($message);
250 while (!empty($parents) && ($msg->parent
=== $msg ||
is_null($msg->parent
))) {
251 @$msg->parent
=& array_pop($parents);
254 if (!is_null($msg->parent
)) {
255 $parent =& $msg->parent
;
256 $parent->children
[] =& $msg;
257 while (!is_null($parent)) {
258 $parent->desc +
= $msg->desc
;
259 $parent->time
= $time;
261 if ($parent !== $parent->parent
) {
262 $parent =& $parent->parent
;
272 Banana
::$protocole->updateSpool($messages);
276 public function updateUnread($since)
282 $newpostsids = Banana
::$protocole->getNewIndexes($since);
284 if (empty($newpostsids)) {
288 $newpostsids = array_intersect($newpostsids, array_keys($this->ids
));
289 foreach ($newpostsids as $mid) {
290 $overview =& $this->ids
[$mid];
291 if ($overview->isread
) {
292 $overview->isread
= false
;
293 while (!is_null($overview)) {
294 $overview->descunread++
;
295 $overview =& $overview->parent
;
299 $this->unreadnb +
= count($newpostsids);
301 if (function_exists('hook_listReadMessages')) {
302 $msgs = hook_listReadMessages($this->group
);
303 if (!is_array($msgs)) {
306 foreach ($msgs as $msg) {
307 if (!is_numeric($msg)) {
308 if (!isset($this->ids
[$msg])) {
311 $msg = $this->ids
[$msg]->id
;
313 $this->markAsRead($msg);
318 public function setMode($mode)
322 case Banana
::SPOOL_UNREAD
:
323 $num = max(array_keys($this->overview
));
324 if ($this->overview
[$num]->isread
) {
327 foreach ($this->roots
as &$root) {
328 if ($root->descunread
== 0) {
329 $this->killdesc($root->id
);
336 /** Fetch list of references
338 public function &getReferences(array &$refs)
340 $references = array();
341 if (isset($refs['references'])) {
342 $text = preg_split('/\s/', str_replace('><', '> <', $refs['references']));
343 foreach ($text as $id=>&$value) {
344 if (isset($this->ids
[$value])) {
345 $references[] =& $this->ids
[$value];
348 } elseif (isset($refs['in-reply-to']) && isset($this->ids
[$refs['in-reply-to']])) {
349 $references[] =& $this->ids
[$refs['in-reply-to']];
354 /** Get the tree associated to a given id
356 public function &getTree($id)
358 return BananaTree
::build($id)->show();
361 /** Mark the given id as read
362 * @param id MSGNUM of post
364 public function markAsRead($id)
366 $overview =& $this->overview
[$id];
367 if (!$overview->isread
) {
368 $overview->isread
= true
;
370 while (!is_null($overview)) {
371 $overview->descunread
--;
372 $overview =& $overview->parent
;
377 /** Mark all unread messages as read
379 public function markAllAsRead(array &$array = null
)
381 if (!$this->unreadnb
) {
384 if (is_null($array) && !empty($this->roots
)) {
385 $array =& $this->roots
;
386 } elseif (is_null($array)) {
389 foreach ($array as &$msg) {
391 $this->markAsRead($msg->id
);
392 if (!$this->unreadnb
) {
396 if ($msg->descunread
) {
397 $this->markAllAsRead($msg->children
);
402 /** kill post and childrens
403 * @param $_id MSGNUM of post
405 private function killdesc($_id)
407 $overview =& $this->overview
[$_id];
408 $children =& $overview->children
;
409 if (sizeof($children)) {
410 foreach ($children as &$c) {
411 $this->killdesc($c->id
);
414 unset($this->overview
[$_id]);
415 foreach ($this->roots
as $k=>&$root) {
416 if ($root === $overview) {
417 unset($this->roots
[$k]);
421 unset($this->ids
[$overview->msgid
]);
425 /** delete a post from overview
426 * @param $_id MSGNUM of post
428 public function delid($_id, $write = true
)
430 if (!isset($this->overview
[$_id])) {
434 $overview =& $this->overview
[$_id];
435 // Be sure it is not counted as unread
436 if (!$overview->isread
) {
437 $this->markAsRead($_id);
440 $parent =& $overview->parent
;
442 // Remove from the message tree
443 if (!is_null($parent)) {
445 foreach ($parent->children
as $key=>&$child) {
446 if ($child === $overview) {
447 unset($parent->children
[$key]);
451 if (sizeof($overview->children
)) {
452 foreach ($overview->children
as &$child) {
453 $parent->children
[] =& $child;
454 $child->time
= $time;
455 $child->parent
=& $parent;
458 while (!is_null($parent)) {
460 $parent->time
= $time;
461 $parent =& $parent->parent
;
465 // Remove all refenrences and assign null to the object
466 unset($this->ids
[$overview->msgid
]);
467 unset($this->overview
[$_id]);
468 BananaTree
::kill($_id);
469 foreach ($this->roots
as $k=>&$root) {
470 if ($root === $overview) {
471 unset($this->roots
[$k]);
482 public function formatDate(BananaSpoolHead
$head)
484 $stamp = $head->date
;
485 $today = intval(time() / (24*3600));
486 $dday = intval($stamp / (24*3600));
488 if ($today == $dday) {
490 } elseif ($today == 1 +
$dday) {
491 $format = _b_('hier')." %H:%M";
492 } elseif ($today < 7 +
$dday) {
493 $format = '%a %H:%M';
494 } elseif ($today < 90 +
$dday) {
495 $format = '%a %e %b';
497 $format = '%a %e %b %Y';
499 return strftime($format, $stamp);
502 public function formatSubject(BananaSpoolHead
$head)
504 $subject = $popup = $head->subject
;
506 if (function_exists('hook_formatDisplayHeader')) {
507 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true
);
509 $subject = banana_catchFormats(banana_entities(stripslashes($subject)));
512 if (empty($subject)) {
513 $subject = _b_('(pas de sujet)');
515 if (mb_strlen($subject) > 100) {
516 $subject = mb_substr($subject, 0, 99) . '…';
518 if ($head->id
!== Banana
::$artid) {
519 $subject = Banana
::$page->makeLink(Array('group' => $this->group
, 'artid' => $head->id
,
520 'text' => $subject, 'popup' => $popup));
522 return $subject . $link;
525 public function formatFrom(BananaSpoolHead
$head)
527 return BananaMessage
::formatFrom($head->from
);
530 public function start()
532 if (Banana
::$first) {
533 return Banana
::$first;
535 $first = array_search(Banana
::$artid, $this->roots
);
536 return max(0, $first - Banana
::$spool_tbefore);
540 public function context()
542 return Banana
::$first ? Banana
::$spool_tmax : Banana
::$spool_tcontext;
545 /** Return root message of the given thread
546 * @param id INTEGER id of a message
548 public function &root($id)
550 $parent =& $this->overview
[$id];
551 while (!is_null($parent->parent
)) {
552 $parent =& $parent->parent
;
557 /** Return the last post id with the given subject
560 public function getPostId($subject)
562 $subject = trim($subject);
563 $id = max(array_keys($this->overview
));
564 while (isset($this->overview
[$id])) {
565 $test = $this->overview
[$id]->subject
;
566 if (function_exists('hook_formatDisplayHeader')) {
567 $val = hook_formatDisplayHeader('subject', $test, true
);
568 if (is_array($val)) {
569 $test = banana_html_entity_decode($val[0]);
571 $test = banana_html_entity_decode($val);
575 if ($test == $subject) {
583 /** Returns previous thread root index
584 * @param id INTEGER message number
586 public function prevThread($id)
588 $root =& $this->root($id);
590 foreach ($this->roots
as &$i) {
599 /** Returns next thread root index
600 * @param id INTEGER message number
602 public function nextThread($id)
604 $root =& $this->root($id);
606 foreach ($this->roots
as &$i) {
617 /** Return prev post in the thread
618 * @param id INTEGER message number
620 public function prevPost($id)
622 $parent =& $this->overview
[$id]->parent
;
623 if (is_null($parent)) {
627 foreach ($parent->children
as &$child) {
628 if ($child->id
== $id) {
636 /** Return next post in the thread
637 * @param id INTEGER message number
639 public function nextPost($id)
641 $cur =& $this->overview
[$id];
642 if (count($cur->children
) != 0) {
643 return $cur->children
[0]->id
;
648 $parent =& $cur->parent
;
649 if (is_null($parent)) {
653 foreach ($parent->children
as &$child) {
657 if ($child === $cur) {
666 /** Look for an unread message in the thread rooted by the message
667 * @param id INTEGER message number
669 private function _nextUnread(BananaSpoolHead
$cur)
674 foreach ($cur->children
as &$child) {
675 $unread = $this->_nextUnread($child);
676 if (!is_null($unread)) {
683 /** Find next unread message
684 * @param id INTEGER message number
686 public function nextUnread($id = null
)
688 if (!$this->unreadnb
) {
693 // Look in message children
694 foreach ($this->overview
[$id]->children
as &$child) {
695 $next = $this->_nextUnread($child);
696 if (!is_null($next)) {
702 // Look in current thread
706 $cur =& $this->overview
[$id];
713 $parent =& $cur->parent
;
716 if (!is_null($parent)) {
717 $array =& $parent->children
;
719 $array =& $this->roots
;
721 foreach ($array as &$child) {
723 $next = $this->_nextUnread($child);
724 if (!is_null($next)) {
728 if ($child === $cur) {
733 } while(!is_null($cur));
737 // vim:set et sw=4 sts=4 ts=4 fenc=utf-8: