Add a 'next unread message' button in spool view
[banana.git] / banana / spool.inc.php
1 <?php
2 /********************************************************************************
3 * include/spool.inc.php : spool subroutines
4 * -----------------------
5 *
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
9
10 if(!function_exists('file_put_contents')) {
11 function file_put_contents($filename, $data)
12 {
13 $fp = fopen($filename, 'w');
14 if(!$fp) {
15 trigger_error('file_put_contents cannot write in file '.$filename, E_USER_ERROR);
16 return;
17 }
18 fputs($fp, $data);
19 fclose($fp);
20 }
21 }
22
23 function spoolCompare($a,$b) { return ($b->date>=$a->date); }
24
25 /** Class spoolhead
26 * class used in thread overviews
27 */
28 class BananaSpoolHead
29 {
30 /** date (timestamp) */
31 var $date;
32 /** subject */
33 var $subject;
34 /** author */
35 var $from;
36 /** reference of parent */
37 var $parent;
38 /** paren is direct */
39 var $parent_direct;
40 /** array of children */
41 var $children = Array();
42 /** true if post is read */
43 var $isread;
44 /** number of posts deeper in this branch of tree */
45 var $desc;
46 /** same as desc, but counts only unread posts */
47 var $descunread;
48
49 /** constructor
50 * @param $_date INTEGER timestamp of post
51 * @param $_subject STRING subject of post
52 * @param $_from STRING author of post
53 * @param $_desc INTEGER desc value (1 for a new post)
54 * @param $_read BOOLEAN true if read
55 * @param $_descunread INTEGER descunread value (0 for a new post)
56 */
57
58 function BananaSpoolHead($_date, $_subject, $_from, $_desc=1, $_read=true, $_descunread=0)
59 {
60 $this->date = $_date;
61 $this->subject = $_subject;
62 $this->from = $_from;
63 $this->desc = $_desc;
64 $this->isread = $_read;
65 $this->descunread = $_descunread;
66 }
67 }
68
69 /** Class spool
70 * builds and updates spool
71 */
72
73 define("BANANA_SPOOL_VERSION", '0.2');
74
75 class BananaSpool
76 {
77 var $version;
78 /** spool */
79 var $overview;
80 /** group name */
81 var $group;
82 /** array msgid => msgnum */
83 var $ids;
84 /** thread starts */
85 var $roots;
86 /** test validity */
87 var $valid = true;
88
89 /** constructor
90 * @param $_group STRING group name
91 * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
92 * @param $_since INTEGER time stamp (used for read/unread)
93 */
94 function BananaSpool($_group, $_display=0, $_since="")
95 {
96 global $banana;
97 $this->group = $_group;
98 $groupinfo = $banana->nntp->group($_group);
99 if (!$groupinfo) {
100 $this->valid = false;
101 return null;
102 }
103
104 $this->_readFromFile();
105
106 $do_save = false;
107 $first = $banana->maxspool ? max($groupinfo[2] - $banana->maxspool, $groupinfo[1]) : $groupinfo[1];
108 $last = $groupinfo[2];
109
110 if ($this->version == BANANA_SPOOL_VERSION && is_array($this->overview)) {
111 $mids = array_keys($this->overview);
112 foreach ($mids as $id) {
113 if (($first <= $last && ($id < $first || $id > $last))
114 || ($first > $last && $id < $first && $id > $last))
115 {
116 $this->delid($id, false);
117 $do_save = true;
118 }
119 }
120 if (!empty($this->overview)) {
121 $first = max(array_keys($this->overview))+1;
122 }
123 } else {
124 unset($this->overview, $this->ids);
125 $this->version = BANANA_SPOOL_VERSION;
126 }
127
128 if ($first<=$last && $groupinfo[0]) {
129 $do_save = true;
130 $this->_updateSpool("$first-$last");
131 }
132
133 if ($do_save) { $this->_saveToFile(); }
134
135 $this->_updateUnread($_since, $_display);
136 }
137
138 function _readFromFile()
139 {
140 $file = $this->_spoolfile();
141 if (file_exists($file)) {
142 $temp = unserialize(file_get_contents($file));
143 foreach (get_object_vars($temp) as $key=>$val) {
144 $this->$key = $val;
145 }
146 }
147 }
148
149 function _saveToFile()
150 {
151 $file = $this->_spoolfile();
152 uasort($this->overview, "spoolcompare");
153
154 $this->roots = Array();
155 foreach($this->overview as $id=>$msg) {
156 if (is_null($msg->parent)) {
157 $this->roots[] = $id;
158 }
159 }
160
161 file_put_contents($file, serialize($this));
162 }
163
164 function _spoolfile()
165 {
166 global $banana;
167 $url = parse_url($banana->host);
168 $file = $url['host'].'_'.$url['port'].'_'.$this->group;
169 return dirname(dirname(__FILE__)).'/spool/'.$file;
170 }
171
172 function _updateSpool($arg)
173 {
174 global $banana;
175 $dates = array_map('strtotime', $banana->nntp->xhdr('Date', $arg));
176 $subjects = array_map('headerdecode', $banana->nntp->xhdr('Subject', $arg));
177 $froms = array_map('headerdecode', $banana->nntp->xhdr('From', $arg));
178 $msgids = $banana->nntp->xhdr('Message-ID', $arg);
179 $refs = $banana->nntp->xhdr('References', $arg);
180
181 if (is_array(@$this->ids)) {
182 $this->ids = array_merge($this->ids, array_flip($msgids));
183 } else {
184 $this->ids = array_flip($msgids);
185 }
186
187 foreach ($msgids as $id=>$msgid) {
188 $msg = new BananaSpoolHead($dates[$id], $subjects[$id], $froms[$id]);
189 $refs[$id] = str_replace('><', '> <', @$refs[$id]);
190 $msgrefs = preg_split("/[ \t]/", strtr($refs[$id], $this->ids));
191 $parents = preg_grep('/^\d+$/', $msgrefs);
192 $msg->parent = array_pop($parents);
193 $msg->parent_direct = preg_match('/^\d+$/', array_pop($msgrefs));
194
195 if (isset($this->overview[$id])) {
196 $msg->desc = $this->overview[$id]->desc;
197 $msg->children = $this->overview[$id]->children;
198 }
199 $this->overview[$id] = $msg;
200
201 if ($p = $msg->parent) {
202 if (empty($this->overview[$p])) {
203 $this->overview[$p] = new BananaSpoolHead($dates[$p], $subjects[$p], $froms[$p], 1);
204 }
205 $this->overview[$p]->children[] = $id;
206
207 while ($p) {
208 $this->overview[$p]->desc += $msg->desc;
209 $p = $this->overview[$p]->parent;
210 }
211 }
212 }
213 }
214
215 function _updateUnread($since, $mode)
216 {
217 global $banana;
218 if (empty($since)) { return; }
219
220 if (is_array($newpostsids = $banana->nntp->newnews($since, $this->group))) {
221 if (!is_array($this->ids)) { $this->ids = array(); }
222 $newpostsids = array_intersect($newpostsids, array_keys($this->ids));
223 foreach ($newpostsids as $mid) {
224 $this->overview[$this->ids[$mid]]->isread = false;
225 $this->overview[$this->ids[$mid]]->descunread = 1;
226 $parentmid = $this->ids[$mid];
227 while (isset($parentmid)) {
228 $this->overview[$parentmid]->descunread ++;
229 $parentmid = $this->overview[$parentmid]->parent;
230 }
231 }
232
233 if (count($newpostsids)) {
234 switch ($mode) {
235 case 1:
236 foreach ($this->roots as $k=>$i) {
237 if ($this->overview[$i]->descunread==0) {
238 $this->killdesc($i);
239 unset($this->roots[$k]);
240 }
241 }
242 break;
243 }
244 }
245 }
246 }
247
248 /** kill post and childrens
249 * @param $_id MSGNUM of post
250 */
251
252 function killdesc($_id)
253 {
254 if (sizeof($this->overview[$_id]->children)) {
255 foreach ($this->overview[$_id]->children as $c) {
256 $this->killdesc($c);
257 }
258 }
259 unset($this->overview[$_id]);
260 if (($msgid = array_search($_id, $this->ids)) !== false) {
261 unset($this->ids[$msgid]);
262 }
263 }
264
265 /** delete a post from overview
266 * @param $_id MSGNUM of post
267 */
268
269 function delid($_id, $write=true)
270 {
271 if (isset($this->overview[$_id])) {
272 if (sizeof($this->overview[$_id]->parent)) {
273 $this->overview[$this->overview[$_id]->parent]->children =
274 array_diff($this->overview[$this->overview[$_id]->parent]->children, array($_id));
275 if (sizeof($this->overview[$_id]->children)) {
276 $this->overview[$this->overview[$_id]->parent]->children =
277 array_merge($this->overview[$this->overview[$_id]->parent]->children, $this->overview[$_id]->children);
278 foreach ($this->overview[$_id]->children as $c) {
279 $this->overview[$c]->parent = $this->overview[$_id]->parent;
280 $this->overview[$c]->parent_direct = false;
281 }
282 }
283 $p = $this->overview[$_id]->parent;
284 while ($p) {
285 $this->overview[$p]->desc--;
286 $p = $this->overview[$p]->parent;
287 }
288 } elseif (sizeof($this->overview[$_id]->children)) {
289 foreach ($this->overview[$_id]->children as $c) {
290 $this->overview[$c]->parent = null;
291 }
292 }
293 unset($this->overview[$_id]);
294 $msgid = array_search($_id, $this->ids);
295 if ($msgid) {
296 unset($this->ids[$msgid]);
297 }
298
299 if ($write) { $this->_saveToFile(); }
300 }
301 }
302
303 /** displays children tree of a post
304 * @param $_id INTEGER MSGNUM of post
305 * @param $_index INTEGER linear number of post in the tree
306 * @param $_first INTEGER linear number of first post displayed
307 * @param $_last INTEGER linear number of last post displayed
308 * @param $_ref STRING MSGNUM of current post
309 * @param $_pfx_node STRING prefix used for current node
310 * @param $_pfx_end STRING prefix used for children of current node
311 * @param $_head BOOLEAN true if first post in thread
312 *
313 * If you want to analyse subject, you can define the function hook_getSubject(&$subject) which
314 * take the subject as a reference parameter, transform this subject to be displaid in the spool
315 * view and return a string. This string will be put after the subject.
316 */
317
318 function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true)
319 {
320 $spfx_f = makeImg('k1.gif', 'o', 21, 9);
321 $spfx_n = makeImg('k2.gif', '*', 21, 9);
322 $spfx_Tnd = makeImg('T-direct.gif', '+', 21, 12);
323 $spfx_Lnd = makeImg('L-direct.gif', '`', 21, 12);
324 $spfx_snd = makeImg('s-direct.gif', '-', 21, 5);
325 $spfx_T = makeImg('T.gif', '+', 21, 12);
326 $spfx_L = makeImg('L.gif', '`', 21, 12);
327 $spfx_s = makeImg('s.gif', '-', 21, 5);
328 $spfx_e = makeImg('e.gif', '&nbsp;', 21, 12);
329 $spfx_I = makeImg('I.gif', '|', 21, 12);
330
331 if ($_index + $this->overview[$_id]->desc < $_first || $_index > $_last) {
332 return;
333 }
334
335 $res = '';
336
337 if ($_index>=$_first) {
338 $hc = empty($this->overview[$_id]->children);
339
340 $res .= '<tr class="'.($_index%2?'pair':'impair').($this->overview[$_id]->isread?'':' new')."\">\n";
341 $res .= "<td class='date'>".fancyDate($this->overview[$_id]->date)." </td>\n";
342 $res .= "<td class='subj'>"
343 ."<div class='tree'>$_pfx_node".($hc?($_head?$spfx_f:($this->overview[$_id]->parent_direct?$spfx_s:$spfx_snd)):$spfx_n)
344 ."</div>";
345 $subject = $this->overview[$_id]->subject;
346 if (strlen($subject) == 0) {
347 $subject = _b_('(pas de sujet)');
348 }
349 $link = null;
350 if (function_exists('hook_getSubject')) {
351 $link = hook_getSubject($subject);
352 }
353 $subject = formatPlainText(htmlentities($subject));
354 if ($_index == $_ref) {
355 $res .= '<span class="cur">' . $subject . $link . '</span>';
356 } else {
357 $res .= makeHREF(Array('group' => $this->group,
358 'artid' => $_id),
359 $subject,
360 $subject)
361 . $link;
362 }
363 $res .= "</td>\n<td class='from'>".formatFrom($this->overview[$_id]->from)."</td>\n</tr>";
364
365 if ($hc) { return $res; }
366 }
367
368 $_index ++;
369
370 $children = $this->overview[$_id]->children;
371 while ($child = array_shift($children)) {
372 if ($_index > $_last) { return $res; }
373 if ($_index+$this->overview[$child]->desc >= $_first) {
374 if (sizeof($children)) {
375 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
376 $_pfx_end.($this->overview[$child]->parent_direct?$spfx_T:$spfx_Tnd),
377 $_pfx_end.$spfx_I, false);
378 } else {
379 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
380 $_pfx_end.($this->overview[$child]->parent_direct?$spfx_L:$spfx_Lnd),
381 $_pfx_end.$spfx_e, false);
382 }
383 }
384 $_index += $this->overview[$child]->desc;
385 }
386
387 return $res;
388 }
389
390 /** Displays overview
391 * @param $_first INTEGER MSGNUM of first post
392 * @param $_last INTEGER MSGNUM of last post
393 * @param $_ref STRING MSGNUM of current/selectionned post
394 */
395
396 function to_html($_first=0, $_last=0, $_ref = null)
397 {
398 $res = '<table class="bicol banana_thread" cellpadding="0" cellspacing="0">';
399
400 $new = '<div class="banana_action">'
401 . makeImgLink(Array('group' => $this->group,
402 'action' => 'new'),
403 'post.gif',
404 _b_('Nouveau message'));
405 $new .= '</div>';
406
407 if (is_null($_ref)) {
408 $next = $this->nextUnread();
409 if (!is_null($next)) {
410 $next = '<div class="banana_menu">'
411 . makeImgLink(Array('group' => $this->group,
412 'artid' => $next),
413 'next_unread.gif',
414 _b_('Message non-lu suivant'))
415 . '</div>';
416 }
417 $new = '<div class="banana_action">'
418 . makeImgLink(Array('group' => $this->group,
419 'action' => 'new'),
420 'post.gif',
421 _b_('Nouveau message'))
422 . '</div>';
423
424 $res .= '<tr><th>' . $next . _b_('Date') . '</th>';
425 $res .= '<th>' . _b_('Sujet') . '</th>';
426 $res .= '<th>' . $new . _b_('Auteur') . '</th></tr>';
427 } else {
428 $res .= '<tr><th colspan="3">' . _b_('Aperçu de ')
429 . makeHREF(Array('group' => $this->group),
430 $this->group)
431 . '</th></tr>';
432 }
433
434 $index = 1;
435 if (sizeof($this->overview)) {
436 foreach ($this->roots as $id) {
437 $res .= $this->_to_html($id, $index, $_first, $_last, $_ref);
438 $index += $this->overview[$id]->desc ;
439 if ($index > $_last) { break; }
440 }
441 } else {
442 $res .= '<tr><td colspan="3">'._b_('Aucun message dans ce forum').'</td></tr>';
443 }
444
445 global $banana;
446 if (is_object($banana->groups)) {
447 $res .= '<tr><td colspan="3" class="subs">'
448 . $banana->groups->to_html()
449 . '</td></tr>';
450 }
451 return $res .= '</table>';
452 }
453
454 /** computes linear post index
455 * @param $_id INTEGER MSGNUM of post
456 * @return INTEGER linear index of post
457 */
458
459 function getndx($_id)
460 {
461 $ndx = 1;
462 $id_cur = $_id;
463 while (true) {
464 $id_parent = $this->overview[$id_cur]->parent;
465 if (is_null($id_parent)) break;
466 $pos = array_search($id_cur, $this->overview[$id_parent]->children);
467
468 for ($i = 0; $i < $pos ; $i++) {
469 $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc;
470 }
471 $ndx++; //noeud père
472
473 $id_cur = $id_parent;
474 }
475
476 foreach ($this->roots as $i) {
477 if ($i==$id_cur) {
478 break;
479 }
480 $ndx += $this->overview[$i]->desc;
481 }
482 return $ndx;
483 }
484
485 /** Return root message of the given thread
486 * @param id INTEGER id of a message
487 */
488 function root($id)
489 {
490 $id_cur = $id;
491 while (true) {
492 $id_parent = $this->overview[$id_cur]->parent;
493 if (is_null($id_parent)) break;
494 $id_cur = $id_parent;
495 }
496 return $id_cur;
497 }
498
499 /** Returns previous thread root index
500 * @param id INTEGER message number
501 */
502 function prevThread($id)
503 {
504 $root = $this->root($id);
505 $last = null;
506 foreach ($this->roots as $i) {
507 if ($i == $root) {
508 return $last;
509 }
510 $last = $i;
511 }
512 return $last;
513 }
514
515 /** Returns next thread root index
516 * @param id INTEGER message number
517 */
518 function nextThread($id)
519 {
520 $root = $this->root($id);
521 $ok = false;
522 foreach ($this->roots as $i) {
523 if ($ok) {
524 return $i;
525 }
526 if ($i == $root) {
527 $ok = true;
528 }
529 }
530 return null;
531 }
532
533 /** Return prev post in the thread
534 * @param id INTEGER message number
535 */
536 function prevPost($id)
537 {
538 $parent = $this->overview[$id]->parent;
539 if (is_null($parent)) {
540 return null;
541 }
542 $last = $parent;
543 foreach ($this->overview[$parent]->children as $child) {
544 if ($child == $id) {
545 return $last;
546 }
547 $last = $child;
548 }
549 return null;
550 }
551
552 /** Return next post in the thread
553 * @param id INTEGER message number
554 */
555 function nextPost($id)
556 {
557 if (count($this->overview[$id]->children) != 0) {
558 return $this->overview[$id]->children[0];
559 }
560
561 $cur = $id;
562 while (true) {
563 $parent = $this->overview[$cur]->parent;
564 if (is_null($parent)) {
565 return null;
566 }
567 $ok = false;
568 foreach ($this->overview[$parent]->children as $child) {
569 if ($ok) {
570 return $child;
571 }
572 if ($child == $cur) {
573 $ok = true;
574 }
575 }
576 $cur = $parent;
577 }
578 return null;
579 }
580
581 /** Look for an unread message in the thread rooted by the message
582 * @param id INTEGER message number
583 */
584 function _nextUnread($id)
585 {
586 if (!$this->overview[$id]->isread) {
587 return $id;
588 }
589 foreach ($this->overview[$id]->children as $child) {
590 $unread = $this->_nextUnread($child);
591 if (!is_null($unread)) {
592 return $unread;
593 }
594 }
595 return null;
596 }
597
598 /** Find next unread message
599 * @param id INTEGER message number
600 */
601 function nextUnread($id = null)
602 {
603 if (!is_null($id)) {
604 // Look in message children
605 foreach ($this->overview[$id]->children as $child) {
606 $next = $this->_nextUnread($child);
607 if (!is_null($next)) {
608 return $next;
609 }
610 }
611 }
612
613 // Look in current thread
614 $cur = $id;
615 do {
616 $parent = is_null($cur) ? null : $this->overview[$cur]->parent;
617 $ok = is_null($cur) ? true : false;
618 if (!is_null($parent)) {
619 $array = &$this->overview[$parent]->children;
620 } else {
621 $array = &$this->roots;
622 }
623 foreach ($array as $child) {
624 if ($ok) {
625 $next = $this->_nextUnread($child);
626 if (!is_null($next)) {
627 return $next;
628 }
629 }
630 if ($child == $cur) {
631 $ok = true;
632 }
633 }
634 $cur = $parent;
635 } while(!is_null($cur));
636 return null;
637 }
638 }
639
640 // vim:set et sw=4 sts=4 ts=4
641 ?>