Add shortkey browsing
[banana.git] / banana / spool.inc.php
CommitLineData
810ac1df 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
d81ff988 10if(!function_exists('file_put_contents')) {
75ff2f64 11 function file_put_contents($filename, $data)
12 {
3ca86dfe 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);
3ca86dfe 20 }
21}
22
d4c19591 23function spoolCompare($a,$b) { return ($b->date>=$a->date); }
01681efd 24
810ac1df 25/** Class spoolhead
26 * class used in thread overviews
27 */
d4c19591 28class BananaSpoolHead
e785d91c 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 */
3ca86dfe 39 var $parent_direct;
e785d91c 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;
810ac1df 48
e785d91c 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 */
810ac1df 57
d4c19591 58 function BananaSpoolHead($_date, $_subject, $_from, $_desc=1, $_read=true, $_descunread=0)
e785d91c 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 }
810ac1df 67}
68
69/** Class spool
70 * builds and updates spool
71 */
72
3ca86dfe 73define("BANANA_SPOOL_VERSION", '0.2');
74
d4c19591 75class BananaSpool
e785d91c 76{
3ca86dfe 77 var $version;
e785d91c 78 /** spool */
79 var $overview;
80 /** group name */
81 var $group;
82 /** array msgid => msgnum */
83 var $ids;
cced14b6 84 /** thread starts */
85 var $roots;
7c111d8d 86 /** test validity */
87 var $valid = true;
810ac1df 88
e785d91c 89 /** constructor
e785d91c 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 */
2dbc0167 94 function BananaSpool($_group, $_display=0, $_since="")
e785d91c 95 {
2dbc0167 96 global $banana;
dd7d1c59 97 $this->group = $_group;
2dbc0167 98 $groupinfo = $banana->nntp->group($_group);
7c111d8d 99 if (!$groupinfo) {
100 $this->valid = false;
101 return null;
102 }
3ca86dfe 103
dd7d1c59 104 $this->_readFromFile();
3ca86dfe 105
dd7d1c59 106 $do_save = false;
fb6428c8 107 $first = $banana->maxspool ? max($groupinfo[2] - $banana->maxspool, $groupinfo[1]) : $groupinfo[1];
108 $last = $groupinfo[2];
109
d5588318 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 {
ab040dd8
PHM
116 $this->delid($id, false);
117 $do_save = true;
118 }
e785d91c 119 }
c42efe2f
PHM
120 if (!empty($this->overview)) {
121 $first = max(array_keys($this->overview))+1;
d5588318 122 }
82c17a91 123 } else {
3ca86dfe 124 unset($this->overview, $this->ids);
3ca86dfe 125 $this->version = BANANA_SPOOL_VERSION;
82c17a91 126 }
e785d91c 127
526f9bd0 128 if ($first<=$last && $groupinfo[0]) {
dd7d1c59 129 $do_save = true;
2dbc0167 130 $this->_updateSpool("$first-$last");
dd7d1c59 131 }
45651257 132
dd7d1c59 133 if ($do_save) { $this->_saveToFile(); }
3ca86dfe 134
2dbc0167 135 $this->_updateUnread($_since, $_display);
dd7d1c59 136 }
137
138 function _readFromFile()
139 {
9090c673 140 $file = $this->_spoolfile();
dd7d1c59 141 if (file_exists($file)) {
d81ff988 142 $temp = unserialize(file_get_contents($file));
143 foreach (get_object_vars($temp) as $key=>$val) {
144 $this->$key = $val;
145 }
dd7d1c59 146 }
147 }
148
149 function _saveToFile()
150 {
9090c673 151 $file = $this->_spoolfile();
dd7d1c59 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 }
2c606d23 160
dd7d1c59 161 file_put_contents($file, serialize($this));
162 }
163
9090c673
PHM
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
2dbc0167 172 function _updateSpool($arg)
dd7d1c59 173 {
2dbc0167 174 global $banana;
bcdfcf99
PHM
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);
dd7d1c59 180
eb31725e 181 if (is_array(@$this->ids)) {
dd7d1c59 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) {
d4c19591 188 $msg = new BananaSpoolHead($dates[$id], $subjects[$id], $froms[$id]);
eb31725e 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));
dd7d1c59 194
195 if (isset($this->overview[$id])) {
196 $msg->desc = $this->overview[$id]->desc;
197 $msg->children = $this->overview[$id]->children;
3316e34e 198 }
dd7d1c59 199 $this->overview[$id] = $msg;
e785d91c 200
dd7d1c59 201 if ($p = $msg->parent) {
202 if (empty($this->overview[$p])) {
d4c19591 203 $this->overview[$p] = new BananaSpoolHead($dates[$p], $subjects[$p], $froms[$p], 1);
4ced5065 204 }
dd7d1c59 205 $this->overview[$p]->children[] = $id;
4ced5065 206
dd7d1c59 207 while ($p) {
208 $this->overview[$p]->desc += $msg->desc;
209 $p = $this->overview[$p]->parent;
e785d91c 210 }
810ac1df 211 }
810ac1df 212 }
dd7d1c59 213 }
e785d91c 214
2dbc0167 215 function _updateUnread($since, $mode)
75ff2f64 216 {
2dbc0167 217 global $banana;
dd7d1c59 218 if (empty($since)) { return; }
219
2dbc0167 220 if (is_array($newpostsids = $banana->nntp->newnews($since, $this->group))) {
382606fb 221 if (!is_array($this->ids)) { $this->ids = array(); }
dd7d1c59 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;
e785d91c 230 }
810ac1df 231 }
dd7d1c59 232
233 if (count($newpostsids)) {
234 switch ($mode) {
e785d91c 235 case 1:
45f3ac9b 236 foreach ($this->roots as $k=>$i) {
cced14b6 237 if ($this->overview[$i]->descunread==0) {
e785d91c 238 $this->killdesc($i);
45f3ac9b 239 unset($this->roots[$k]);
e785d91c 240 }
241 }
242 break;
e785d91c 243 }
810ac1df 244 }
810ac1df 245 }
cced14b6 246 }
247
e785d91c 248 /** kill post and childrens
249 * @param $_id MSGNUM of post
250 */
810ac1df 251
e785d91c 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]);
dd7d1c59 260 if (($msgid = array_search($_id, $this->ids)) !== false) {
e785d91c 261 unset($this->ids[$msgid]);
262 }
810ac1df 263 }
e785d91c 264
265 /** delete a post from overview
266 * @param $_id MSGNUM of post
267 */
268
3ca86dfe 269 function delid($_id, $write=true)
e785d91c 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]);
3ca86dfe 294 $msgid = array_search($_id, $this->ids);
e785d91c 295 if ($msgid) {
296 unset($this->ids[$msgid]);
297 }
298
dd7d1c59 299 if ($write) { $this->_saveToFile(); }
e785d91c 300 }
35ca8036 301 }
810ac1df 302
e785d91c 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
3204d440 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.
e785d91c 316 */
810ac1df 317
65d96b1f 318 function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true)
75ff2f64 319 {
d8e2470c 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);
e785d91c 330
cced14b6 331 if ($_index + $this->overview[$_id]->desc < $_first || $_index > $_last) {
332 return;
810ac1df 333 }
e785d91c 334
65d96b1f 335 $res = '';
336
cced14b6 337 if ($_index>=$_first) {
cced14b6 338 $hc = empty($this->overview[$_id]->children);
339
65d96b1f 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)
dd7d1c59 344 ."</div>";
79405147 345 $subject = $this->overview[$_id]->subject;
346 if (strlen($subject) == 0) {
347 $subject = _b_('(pas de sujet)');
348 }
3204d440 349 $link = null;
350 if (function_exists('hook_getSubject')) {
351 $link = hook_getSubject($subject);
352 }
1fb11000 353 $subject = formatPlainText(htmlentities($subject));
dd7d1c59 354 if ($_index == $_ref) {
3204d440 355 $res .= '<span class="cur">' . $subject . $link . '</span>';
dd7d1c59 356 } else {
0eb1e7ef 357 $res .= makeHREF(Array('group' => $this->group,
d5588318 358 'artid' => $_id),
3204d440 359 $subject,
360 $subject)
361 . $link;
cced14b6 362 }
65d96b1f 363 $res .= "</td>\n<td class='from'>".formatFrom($this->overview[$_id]->from)."</td>\n</tr>";
dd7d1c59 364
65d96b1f 365 if ($hc) { return $res; }
cced14b6 366 }
367
dd7d1c59 368 $_index ++;
cced14b6 369
370 $children = $this->overview[$_id]->children;
e785d91c 371 while ($child = array_shift($children)) {
65d96b1f 372 if ($_index > $_last) { return $res; }
dd7d1c59 373 if ($_index+$this->overview[$child]->desc >= $_first) {
e785d91c 374 if (sizeof($children)) {
65d96b1f 375 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
dd7d1c59 376 $_pfx_end.($this->overview[$child]->parent_direct?$spfx_T:$spfx_Tnd),
3ca86dfe 377 $_pfx_end.$spfx_I, false);
e785d91c 378 } else {
65d96b1f 379 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
dd7d1c59 380 $_pfx_end.($this->overview[$child]->parent_direct?$spfx_L:$spfx_Lnd),
3ca86dfe 381 $_pfx_end.$spfx_e, false);
e785d91c 382 }
383 }
dd7d1c59 384 $_index += $this->overview[$child]->desc;
810ac1df 385 }
65d96b1f 386
387 return $res;
810ac1df 388 }
810ac1df 389
e785d91c 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 */
810ac1df 395
65d96b1f 396 function to_html($_first=0, $_last=0, $_ref = null)
75ff2f64 397 {
65d96b1f 398 $res = '<table class="bicol banana_thread" cellpadding="0" cellspacing="0">';
399
400 if (is_null($_ref)) {
b7d59a47 401 $next = $this->nextUnread();
402 if (!is_null($next)) {
403 $next = '<div class="banana_menu">'
404 . makeImgLink(Array('group' => $this->group,
405 'artid' => $next),
406 'next_unread.gif',
6e0f0e15 407 _b_('Message non-lu suivant'), null, null, null, 'u')
b7d59a47 408 . '</div>';
409 }
410 $new = '<div class="banana_action">'
411 . makeImgLink(Array('group' => $this->group,
412 'action' => 'new'),
413 'post.gif',
6e0f0e15 414 _b_('Nouveau message'), null, null, null, 'p')
b7d59a47 415 . '</div>';
416
417 $res .= '<tr><th>' . $next . _b_('Date') . '</th>';
418 $res .= '<th>' . _b_('Sujet') . '</th>';
419 $res .= '<th>' . $new . _b_('Auteur') . '</th></tr>';
d8e2470c 420 } else {
421 $res .= '<tr><th colspan="3">' . _b_('Aperçu de ')
422 . makeHREF(Array('group' => $this->group),
423 $this->group)
424 . '</th></tr>';
65d96b1f 425 }
426
e785d91c 427 $index = 1;
428 if (sizeof($this->overview)) {
cced14b6 429 foreach ($this->roots as $id) {
65d96b1f 430 $res .= $this->_to_html($id, $index, $_first, $_last, $_ref);
cced14b6 431 $index += $this->overview[$id]->desc ;
dd7d1c59 432 if ($index > $_last) { break; }
e785d91c 433 }
810ac1df 434 } else {
65d96b1f 435 $res .= '<tr><td colspan="3">'._b_('Aucun message dans ce forum').'</td></tr>';
810ac1df 436 }
65d96b1f 437
4f75645f 438 global $banana;
439 if (is_object($banana->groups)) {
440 $res .= '<tr><td colspan="3" class="subs">'
441 . $banana->groups->to_html()
442 . '</td></tr>';
443 }
65d96b1f 444 return $res .= '</table>';
810ac1df 445 }
810ac1df 446
e785d91c 447 /** computes linear post index
448 * @param $_id INTEGER MSGNUM of post
449 * @return INTEGER linear index of post
450 */
810ac1df 451
75ff2f64 452 function getndx($_id)
453 {
cced14b6 454 $ndx = 1;
455 $id_cur = $_id;
4ced5065 456 while (true) {
cced14b6 457 $id_parent = $this->overview[$id_cur]->parent;
4ced5065 458 if (is_null($id_parent)) break;
cced14b6 459 $pos = array_search($id_cur, $this->overview[$id_parent]->children);
4ced5065 460
cced14b6 461 for ($i = 0; $i < $pos ; $i++) {
e785d91c 462 $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc;
463 }
464 $ndx++; //noeud père
cced14b6 465
466 $id_cur = $id_parent;
810ac1df 467 }
cced14b6 468
469 foreach ($this->roots as $i) {
470 if ($i==$id_cur) {
e785d91c 471 break;
472 }
cced14b6 473 $ndx += $this->overview[$i]->desc;
e785d91c 474 }
475 return $ndx;
810ac1df 476 }
d8e2470c 477
478 /** Return root message of the given thread
479 * @param id INTEGER id of a message
480 */
481 function root($id)
482 {
483 $id_cur = $id;
484 while (true) {
485 $id_parent = $this->overview[$id_cur]->parent;
486 if (is_null($id_parent)) break;
487 $id_cur = $id_parent;
488 }
489 return $id_cur;
490 }
491
492 /** Returns previous thread root index
493 * @param id INTEGER message number
494 */
495 function prevThread($id)
496 {
497 $root = $this->root($id);
498 $last = null;
499 foreach ($this->roots as $i) {
500 if ($i == $root) {
501 return $last;
502 }
503 $last = $i;
504 }
505 return $last;
506 }
507
508 /** Returns next thread root index
509 * @param id INTEGER message number
510 */
511 function nextThread($id)
512 {
513 $root = $this->root($id);
514 $ok = false;
515 foreach ($this->roots as $i) {
516 if ($ok) {
517 return $i;
518 }
519 if ($i == $root) {
520 $ok = true;
521 }
522 }
523 return null;
524 }
525
526 /** Return prev post in the thread
527 * @param id INTEGER message number
528 */
529 function prevPost($id)
530 {
531 $parent = $this->overview[$id]->parent;
532 if (is_null($parent)) {
533 return null;
534 }
535 $last = $parent;
536 foreach ($this->overview[$parent]->children as $child) {
537 if ($child == $id) {
538 return $last;
539 }
540 $last = $child;
541 }
542 return null;
543 }
544
545 /** Return next post in the thread
546 * @param id INTEGER message number
547 */
548 function nextPost($id)
549 {
550 if (count($this->overview[$id]->children) != 0) {
551 return $this->overview[$id]->children[0];
552 }
553
554 $cur = $id;
555 while (true) {
556 $parent = $this->overview[$cur]->parent;
557 if (is_null($parent)) {
558 return null;
559 }
560 $ok = false;
561 foreach ($this->overview[$parent]->children as $child) {
562 if ($ok) {
563 return $child;
564 }
565 if ($child == $cur) {
566 $ok = true;
567 }
568 }
569 $cur = $parent;
570 }
571 return null;
572 }
d634c13c 573
574 /** Look for an unread message in the thread rooted by the message
575 * @param id INTEGER message number
576 */
577 function _nextUnread($id)
578 {
579 if (!$this->overview[$id]->isread) {
580 return $id;
581 }
582 foreach ($this->overview[$id]->children as $child) {
bdad7c9d 583 $unread = $this->_nextUnread($child);
584 if (!is_null($unread)) {
585 return $unread;
586 }
d634c13c 587 }
588 return null;
589 }
590
591 /** Find next unread message
592 * @param id INTEGER message number
593 */
b7d59a47 594 function nextUnread($id = null)
d634c13c 595 {
b7d59a47 596 if (!is_null($id)) {
597 // Look in message children
598 foreach ($this->overview[$id]->children as $child) {
599 $next = $this->_nextUnread($child);
600 if (!is_null($next)) {
601 return $next;
602 }
d634c13c 603 }
604 }
605
606 // Look in current thread
607 $cur = $id;
b7d59a47 608 do {
609 $parent = is_null($cur) ? null : $this->overview[$cur]->parent;
610 $ok = is_null($cur) ? true : false;
1e016f3a 611 if (!is_null($parent)) {
d634c13c 612 $array = &$this->overview[$parent]->children;
613 } else {
614 $array = &$this->roots;
615 }
616 foreach ($array as $child) {
617 if ($ok) {
618 $next = $this->_nextUnread($child);
619 if (!is_null($next)) {
620 return $next;
621 }
622 }
623 if ($child == $cur) {
624 $ok = true;
625 }
626 }
627 $cur = $parent;
b7d59a47 628 } while(!is_null($cur));
d634c13c 629 return null;
630 }
810ac1df 631}
632
d5588318 633// vim:set et sw=4 sts=4 ts=4
810ac1df 634?>