2a9ec6dac1cbaa4f44ea2d7c635dbe437aefd650
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)
58 $this->msgid
= @$message['message-id'];
59 $this->date
= $message['date'];
60 $this->subject
= @$message['subject'];
61 $this->from
= $message['from'];
62 $this->color
= sprintf('#%06x', abs(crc32($this->from
) %
0xffffff));
65 $this->descunread
= 0;
66 if (preg_match("/^([^ ]+@[^ ]+) \((.*)\)$/", $this->from
, $regs)) {
67 $this->name
= $regs[2];
69 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+@.+)>$/", $this->from
, $regs)) {
70 $this->name
= preg_replace("/^'(.*)'$/", '\1', $regs[1]);
71 $this->name
= stripslashes($this->name
);
74 $this->name
= preg_replace("/\\\(\(|\))/","\\1", $this->name
);
75 } else if (preg_match("/([^< ]+)@([^> ]+)/", $this->from
, $regs)) {
76 $this->name
= $regs[1];
78 $this->name
= 'Anonymous';
92 public $overview = array();
93 /** array msgid => msgnum */
94 public $ids = array();
96 public $roots = array();
98 /** protocole specific data */
99 public $storage = array();
101 private $unreadnb = 0;
104 * @param $_group STRING group name
105 * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
106 * @param $_since INTEGER time stamp (used for read/unread)
108 protected function __construct($group)
110 $this->version
= BANANA_SPOOL_VERSION
;
111 $this->mode
= Banana
::SPOOL_ALL
;
112 $this->group
= $group;
115 public static function &getSpool($group, $since = 0, $clean = false
)
117 if (!is_null(Banana
::$spool) && Banana
::$spool->group
== $group) {
118 $spool =& Banana
::$spool;
120 $spool =& BananaSpool
::readFromFile($group);
122 if (is_null($spool)) {
123 $spool = new BananaSpool($group);
125 Banana
::$spool =& $spool;
128 $spool->markAllAsRead();
130 $spool->updateUnread($since);
134 public static function getPath($file = null
)
136 $path = Banana
::$spool_root . '/' . Banana
::$protocole->name() . '/' . Banana
::$protocole->filename();
137 if (!is_dir($path)) {
138 if (file_exists($path)) {
141 mkdir($path, 0777, true
);
143 return $path . '/' . $file;
146 private static function spoolFilename()
148 return BananaSpool
::getPath('spool');
151 private static function &readFromFile($group)
154 $file = BananaSpool
::spoolFilename();
155 if (!file_exists($file)) {
158 $spool = unserialize(file_get_contents($file));
159 if ($spool->version
!= BANANA_SPOOL_VERSION ||
$spool->mode
!= Banana
::SPOOL_ALL
) {
163 $spool->markAllAsRead();
167 private function compare($a, $b)
169 return ($b->date
- $a->date
);
172 private function saveToFile()
174 $file = BananaSpool
::spoolFilename();
176 $this->roots
= Array();
177 foreach($this->overview
as &$msg) {
178 if (is_null($msg->parent
)) {
179 $this->roots
[] =& $msg;
182 usort($this->roots
, array($this, 'compare'));
184 if ($this->mode
== Banana
::SPOOL_ALL
) {
185 file_put_contents($file, serialize($this));
189 private function build()
193 // Compute the range of indexes
194 list($msgnum, $first, $last) = Banana
::$protocole->getIndexes();
195 if ($last < $first) {
196 $threshold = $first +
$msgnum - $last;
197 $threshold = (int)(log($threshold)/log(2));
198 $threshold = (2 ^
($threshold +
1)) - 1;
200 if (Banana
::$spool_max && Banana
::$spool_max < $msgnum) {
201 $first = $last - Banana
::$spool_max;
203 $first +
= $threshold;
206 $clean = $this->clean($first, $last, $msgnum);
207 $update = $this->update($first, $last, $msgnum);
209 if ($clean ||
$update) {
214 private function clean(&$first, &$last, $msgnum)
217 if (!empty($this->overview
)) {
218 $mids = array_keys($this->overview
);
219 foreach ($mids as $id) {
220 if (($first <= $last && ($id < $first ||
$id > $last))
221 ||
($first > $last && $id < $first && $id > $last)) {
222 $this->delid($id, false
);
226 if (!empty($this->overview
)) {
227 $first = max(array_keys($this->overview
))+
1;
233 private function update(&$first, &$last, $msgnum)
235 if ($first > $last ||
!$msgnum) {
239 $messages =& Banana
::$protocole->getMessageHeaders($first, $last,
240 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
242 // Build all the new Spool Heads
244 foreach ($messages as $id=>&$message) {
245 if (!isset($this->overview
[$id])) {
246 $this->overview
[$id] = new BananaSpoolHead($id, $message);
247 $head =& $this->overview
[$id];
248 $this->ids
[$head->msgid
] =& $head;
255 foreach ($messages as $id=>&$message) {
256 $msg =& $this->overview
[$id];
257 $parents =& $this->getReferences($message);
258 while (!empty($parents) && ($msg->parent
=== $msg ||
is_null($msg->parent
))) {
259 @$msg->parent
=& array_pop($parents);
262 if (!is_null($msg->parent
)) {
263 $parent =& $msg->parent
;
264 $parent->children
[] =& $msg;
265 while (!is_null($parent)) {
266 $parent->desc +
= $msg->desc
;
267 $parent->time
= $time;
269 if ($parent !== $parent->parent
) {
270 $parent =& $parent->parent
;
277 Banana
::$protocole->updateSpool($messages);
281 public function updateUnread($since)
287 $newpostsids = Banana
::$protocole->getNewIndexes($since);
289 if (empty($newpostsids)) {
293 $newpostsids = array_intersect($newpostsids, array_keys($this->ids
));
294 foreach ($newpostsids as $mid) {
295 $overview =& $this->ids
[$mid];
296 if ($overview->isread
) {
297 $overview->isread
= false
;
298 while (!is_null($overview)) {
299 $overview->descunread++
;
300 $overview =& $overview->parent
;
304 $this->unreadnb +
= count($newpostsids);
307 public function setMode($mode)
311 case Banana
::SPOOL_UNREAD
:
312 $num = max(array_keys($this->overview
));
313 if ($this->overview
[$num]->isread
) {
316 foreach ($this->roots
as &$root) {
317 if ($root->descunread
== 0) {
318 $this->killdesc($root->id
);
325 /** Fetch list of references
327 public function &getReferences(array &$refs)
329 $references = array();
330 if (isset($refs['references'])) {
331 $text = preg_split('/\s/', str_replace('><', '> <', $refs['references']));
332 foreach ($text as $id=>&$value) {
333 if (isset($this->ids
[$value])) {
334 $references[] =& $this->ids
[$value];
337 } elseif (isset($refs['in-reply-to']) && isset($this->ids
[$refs['in-reply-to']])) {
338 $references[] =& $this->ids
[$refs['in-reply-to']];
343 /** Get the tree associated to a given id
345 public function &getTree($id)
347 return BananaTree
::build($id)->show();
350 /** Mark the given id as read
351 * @param id MSGNUM of post
353 public function markAsRead($id)
355 $overview =& $this->overview
[$id];
356 if (!$overview->isread
) {
357 $overview->isread
= true
;
359 while (!is_null($overview)) {
360 $overview->descunread
--;
361 $overview =& $overview->parent
;
366 /** Mark all unread messages as read
368 public function markAllAsRead(array &$array = null
)
370 if (!$this->unreadnb
) {
373 if (is_null($array) && !empty($this->roots
)) {
374 $array =& $this->roots
;
375 } elseif (is_null($array)) {
378 foreach ($array as &$msg) {
380 $this->markAsRead($msg->id
);
381 if (!$this->unreadnb
) {
385 if ($msg->descunread
) {
386 $this->markAllAsRead($msg->children
);
391 /** kill post and childrens
392 * @param $_id MSGNUM of post
394 private function killdesc($_id)
396 $overview =& $this->overview
[$_id];
397 $children =& $overview->children
;
398 if (sizeof($children)) {
399 foreach ($children as &$c) {
400 $this->killdesc($c->id
);
403 unset($this->overview
[$_id]);
404 foreach ($this->roots
as $k=>&$root) {
405 if ($root === $overview) {
406 unset($this->roots
[$k]);
410 unset($this->ids
[$overview->msgid
]);
414 /** delete a post from overview
415 * @param $_id MSGNUM of post
417 public function delid($_id, $write = true
)
419 if (!isset($this->overview
[$_id])) {
423 $overview =& $this->overview
[$_id];
424 // Be sure it is not counted as unread
425 if (!$overview->isread
) {
426 $this->markAsRead($_id);
429 $parent =& $overview->parent
;
431 // Remove from the message tree
432 if (!is_null($parent)) {
434 foreach ($parent->children
as $key=>&$child) {
435 if ($child === $overview) {
436 unset($parent->children
[$key]);
440 if (sizeof($overview->children
)) {
441 foreach ($overview->children
as &$child) {
442 $parent->children
[] =& $child;
443 $child->time
= $time;
444 $child->parent
=& $parent;
447 while (!is_null($parent)) {
449 $parent->time
= $time;
450 $parent =& $parent->parent
;
454 // Remove all refenrences and assign null to the object
455 unset($this->ids
[$overview->msgid
]);
456 unset($this->overview
[$_id]);
457 BananaTree
::kill($_id);
458 foreach ($this->roots
as $k=>&$root) {
459 if ($root === $overview) {
460 unset($this->roots
[$k]);
471 public function formatDate(BananaSpoolHead
$head)
473 $stamp = $head->date
;
474 $today = intval(time() / (24*3600));
475 $dday = intval($stamp / (24*3600));
477 if ($today == $dday) {
479 } elseif ($today == 1 +
$dday) {
480 $format = _b_('hier')." %H:%M";
481 } elseif ($today < 7 +
$dday) {
482 $format = '%a %H:%M';
483 } elseif ($today < 90 +
$dday) {
484 $format = '%a %e %b';
486 $format = '%a %e %b %Y';
488 return strftime($format, $stamp);
491 public function formatSubject(BananaSpoolHead
$head)
493 $subject = $popup = $head->subject
;
495 if (function_exists('hook_formatDisplayHeader')) {
496 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true
);
498 $subject = banana_catchFormats(banana_entities(stripslashes($subject)));
501 if (empty($subject)) {
502 $subject = _b_('(pas de sujet)');
504 if (strlen($subject) > 100) {
505 $subject = substr($subject, 0, 99) . '…';
507 if ($head->id
!== Banana
::$artid) {
508 $subject = Banana
::$page->makeLink(Array('group' => $this->group
, 'artid' => $head->id
,
509 'text' => $subject, 'popup' => $popup));
511 return $subject . $link;
514 public function formatFrom(BananaSpoolHead
$head)
516 return BananaMessage
::formatFrom($head->from
);
519 public function start()
521 if (Banana
::$first) {
522 return Banana
::$first;
524 $first = array_search(Banana
::$artid, $this->roots
);
525 return max(0, $first - Banana
::$spool_tbefore);
529 public function context()
531 return Banana
::$first ? Banana
::$spool_tmax : Banana
::$spool_tcontext;
534 /** Return root message of the given thread
535 * @param id INTEGER id of a message
537 public function &root($id)
539 $parent =& $this->overview
[$id];
540 while (!is_null($parent->parent
)) {
541 $parent =& $parent->parent
;
546 /** Return the last post id with the given subject
549 public function getPostId($subject)
551 $subject = trim($subject);
552 $id = max(array_keys($this->overview
));
553 while (isset($this->overview
[$id])) {
554 $test = $this->overview
[$id]->subject
;
555 if (function_exists('hook_formatDisplayHeader')) {
556 $val = hook_formatDisplayHeader('subject', $test, true
);
557 if (is_array($val)) {
558 $test = banana_html_entity_decode($val[0]);
560 $test = banana_html_entity_decode($val);
564 if ($test == $subject) {
572 /** Returns previous thread root index
573 * @param id INTEGER message number
575 public function prevThread($id)
577 $root =& $this->root($id);
579 foreach ($this->roots
as &$i) {
588 /** Returns next thread root index
589 * @param id INTEGER message number
591 public function nextThread($id)
593 $root =& $this->root($id);
595 foreach ($this->roots
as &$i) {
606 /** Return prev post in the thread
607 * @param id INTEGER message number
609 public function prevPost($id)
611 $parent =& $this->overview
[$id]->parent
;
612 if (is_null($parent)) {
616 foreach ($parent->children
as &$child) {
617 if ($child->id
== $id) {
625 /** Return next post in the thread
626 * @param id INTEGER message number
628 public function nextPost($id)
630 $cur =& $this->overview
[$id];
631 if (count($cur->children
) != 0) {
632 return $cur->children
[0]->id
;
637 $parent =& $cur->parent
;
638 if (is_null($parent)) {
642 foreach ($parent->children
as &$child) {
646 if ($child === $cur) {
655 /** Look for an unread message in the thread rooted by the message
656 * @param id INTEGER message number
658 private function _nextUnread(BananaSpoolHead
$cur)
663 foreach ($cur->children
as &$child) {
664 $unread = $this->_nextUnread($child);
665 if (!is_null($unread)) {
672 /** Find next unread message
673 * @param id INTEGER message number
675 public function nextUnread($id = null
)
677 if (!$this->unreadnb
) {
682 // Look in message children
683 foreach ($this->overview
[$id]->children
as &$child) {
684 $next = $this->_nextUnread($child);
685 if (!is_null($next)) {
691 // Look in current thread
695 $cur =& $this->overview
[$id];
702 $parent =& $cur->parent
;
705 if (!is_null($parent)) {
706 $array =& $parent->children
;
708 $array =& $this->roots
;
710 foreach ($array as &$child) {
712 $next = $this->_nextUnread($child);
713 if (!is_null($next)) {
717 if ($child === $cur) {
722 } while(!is_null($cur));
726 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: