78cd27b3 |
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 | |
ab02e8a9 |
10 | require_once dirname(__FILE__) . '/banana.inc.php'; |
78cd27b3 |
11 | |
ab02e8a9 |
12 | define('BANANA_SPOOL_VERSION', '0.3'); |
78cd27b3 |
13 | |
14 | /** Class spoolhead |
15 | * class used in thread overviews |
16 | */ |
17 | class BananaSpoolHead |
18 | { |
19 | /** date (timestamp) */ |
ab02e8a9 |
20 | public $date; |
78cd27b3 |
21 | /** subject */ |
ab02e8a9 |
22 | public $subject; |
78cd27b3 |
23 | /** author */ |
ab02e8a9 |
24 | public $from; |
78cd27b3 |
25 | /** reference of parent */ |
ab02e8a9 |
26 | public $parent = null; |
78cd27b3 |
27 | /** paren is direct */ |
ab02e8a9 |
28 | public $parent_direct; |
78cd27b3 |
29 | /** array of children */ |
ab02e8a9 |
30 | public $children = Array(); |
78cd27b3 |
31 | /** true if post is read */ |
ab02e8a9 |
32 | public $isread; |
78cd27b3 |
33 | /** number of posts deeper in this branch of tree */ |
ab02e8a9 |
34 | public $desc; |
78cd27b3 |
35 | /** same as desc, but counts only unread posts */ |
ab02e8a9 |
36 | public $descunread; |
37 | |
38 | /** storage data */ |
39 | public $storage = array(); |
78cd27b3 |
40 | |
41 | /** constructor |
42 | * @param $_date INTEGER timestamp of post |
43 | * @param $_subject STRING subject of post |
44 | * @param $_from STRING author of post |
45 | * @param $_desc INTEGER desc value (1 for a new post) |
46 | * @param $_read BOOLEAN true if read |
47 | * @param $_descunread INTEGER descunread value (0 for a new post) |
48 | */ |
ab02e8a9 |
49 | public function __construct(array &$message) |
78cd27b3 |
50 | { |
ab02e8a9 |
51 | $this->date = $message['date']; |
d7718cc5 |
52 | $this->subject = $message['subject']; |
ab02e8a9 |
53 | $this->from = $message['from']; |
54 | $this->desc = 1; |
55 | $this->isread = true; |
56 | $this->descunread = 0; |
78cd27b3 |
57 | } |
58 | } |
59 | |
78cd27b3 |
60 | |
61 | class BananaSpool |
62 | { |
ab02e8a9 |
63 | private $version; |
7972645b |
64 | private $mode; |
ab02e8a9 |
65 | |
78cd27b3 |
66 | /** group name */ |
ab02e8a9 |
67 | public $group; |
68 | /** spool */ |
69 | public $overview; |
78cd27b3 |
70 | /** array msgid => msgnum */ |
ab02e8a9 |
71 | public $ids; |
78cd27b3 |
72 | /** thread starts */ |
ab02e8a9 |
73 | public $roots; |
74 | |
78cd27b3 |
75 | |
76 | /** constructor |
77 | * @param $_group STRING group name |
78 | * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts |
79 | * @param $_since INTEGER time stamp (used for read/unread) |
80 | */ |
ab02e8a9 |
81 | protected function __construct($group) |
78cd27b3 |
82 | { |
ab02e8a9 |
83 | $this->version = BANANA_SPOOL_VERSION; |
7972645b |
84 | $this->mode = Banana::SPOOL_ALL; |
ab02e8a9 |
85 | $this->group = $group; |
86 | } |
9d18c838 |
87 | |
4d20320e |
88 | public static function getSpool($group, $since = 0, $clean = false) |
ab02e8a9 |
89 | { |
90 | if (!is_null(Banana::$spool) && Banana::$spool->group == $group) { |
4d20320e |
91 | $spool =& Banana::$spool; |
78cd27b3 |
92 | } else { |
ab02e8a9 |
93 | $spool = BananaSpool::readFromFile($group); |
94 | } |
95 | if (is_null($spool)) { |
96 | $spool = new BananaSpool($group); |
78cd27b3 |
97 | } |
ab02e8a9 |
98 | Banana::$spool =& $spool; |
99 | $spool->build(); |
4d20320e |
100 | if ($clean) { |
101 | $spool->markAllAsRead(); |
102 | } |
ab02e8a9 |
103 | $spool->updateUnread($since); |
104 | return $spool; |
105 | } |
78cd27b3 |
106 | |
ab02e8a9 |
107 | private static function spoolFilename($group) |
108 | { |
109 | $file = dirname(dirname(__FILE__)); |
110 | $file .= '/spool/' . Banana::$protocole->name() . '/'; |
111 | if (!is_dir($file)) { |
112 | mkdir($file); |
78cd27b3 |
113 | } |
7972645b |
114 | return $file . Banana::$protocole->filename(); |
78cd27b3 |
115 | } |
116 | |
ab02e8a9 |
117 | private static function readFromFile($group) |
78cd27b3 |
118 | { |
ab02e8a9 |
119 | $file = BananaSpool::spoolFilename($group); |
120 | if (!file_exists($file)) { |
121 | return null; |
78cd27b3 |
122 | } |
ab02e8a9 |
123 | $spool = unserialize(file_get_contents($file)); |
7972645b |
124 | if ($spool->version != BANANA_SPOOL_VERSION || $spool->mode != Banana::SPOOL_ALL) { |
ab02e8a9 |
125 | return null; |
126 | } |
127 | return $spool; |
128 | } |
129 | |
130 | private function compare($a, $b) |
131 | { |
132 | return ($b->date >= $a->date); |
78cd27b3 |
133 | } |
134 | |
ab02e8a9 |
135 | private function saveToFile() |
78cd27b3 |
136 | { |
ab02e8a9 |
137 | $file = BananaSpool::spoolFilename($this->group); |
138 | uasort($this->overview, array($this, 'compare')); |
78cd27b3 |
139 | |
140 | $this->roots = Array(); |
141 | foreach($this->overview as $id=>$msg) { |
142 | if (is_null($msg->parent)) { |
143 | $this->roots[] = $id; |
144 | } |
145 | } |
01972ace |
146 | |
7972645b |
147 | if ($this->mode == Banana::SPOOL_ALL) { |
148 | file_put_contents($file, serialize($this)); |
149 | } |
78cd27b3 |
150 | } |
151 | |
ab02e8a9 |
152 | private function build() |
153 | { |
154 | $threshold = 0; |
155 | |
156 | // Compute the range of indexes |
157 | list($msgnum, $first, $last) = Banana::$protocole->getIndexes(); |
158 | if ($last < $first) { |
159 | $threshold = $firt + $msgnum - $last; |
160 | $threshold = (int)(log($threshold)/log(2)); |
161 | $threshold = (2 ^ ($threshold + 1)) - 1; |
162 | } |
7972645b |
163 | if (Banana::$spool_max && Banana::$spool_max < $msgnum) { |
164 | $first = $last - Banana::$spool_max; |
ab02e8a9 |
165 | if ($first < 0) { |
166 | $first += $threshold; |
167 | } |
168 | } |
169 | $clean = $this->clean($first, $last, $msgnum); |
170 | $update = $this->update($first, $last, $msgnum); |
171 | |
172 | if ($clean || $update) { |
173 | $this->saveToFile(); |
174 | } |
175 | } |
176 | |
177 | private function clean(&$first, &$last, $msgnum) |
78cd27b3 |
178 | { |
ab02e8a9 |
179 | $do_save = false; |
180 | if (is_array($this->overview)) { |
181 | $mids = array_keys($this->overview); |
182 | foreach ($mids as $id) { |
183 | if (($first <= $last && ($id < $first || $id > $last)) |
7972645b |
184 | || ($first > $last && $id < $first && $id > $last)) { |
ab02e8a9 |
185 | $this->delid($id, false); |
186 | $do_save = true; |
187 | } |
188 | } |
189 | if (!empty($this->overview)) { |
190 | $first = max(array_keys($this->overview))+1; |
191 | } |
192 | } |
193 | return $do_save; |
78cd27b3 |
194 | } |
195 | |
ab02e8a9 |
196 | private function update(&$first, &$last, $msgnum) |
78cd27b3 |
197 | { |
ded3974d |
198 | if ($first > $last || !$msgnum) { |
ab02e8a9 |
199 | return false; |
78cd27b3 |
200 | } |
201 | |
ab02e8a9 |
202 | $messages =& Banana::$protocole->getMessageHeaders($first, $last, |
203 | array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To')); |
78cd27b3 |
204 | |
ab02e8a9 |
205 | if (!is_array($this->ids)) { |
206 | $this->ids = array(); |
207 | } |
208 | foreach ($messages as $id=>&$message) { |
209 | $this->ids[$message['message-id']] = $id; |
210 | } |
211 | |
212 | foreach ($messages as $id=>&$message) { |
213 | if (!isset($this->overview[$id])) { |
214 | $this->overview[$id] = new BananaSpoolHead($message); |
78cd27b3 |
215 | } |
ab02e8a9 |
216 | $msg =& $this->overview[$id]; |
217 | $msgrefs = BananaMessage::formatReferences($message); |
218 | $parents = preg_grep('/^\d+$/', $msgrefs); |
219 | $msg->parent = array_pop($parents); |
220 | $msg->parent_direct = preg_match('/^\d+$/', array_pop($msgrefs)); |
78cd27b3 |
221 | |
ab02e8a9 |
222 | if (!is_null($p = $msg->parent)) { |
78cd27b3 |
223 | if (empty($this->overview[$p])) { |
ab02e8a9 |
224 | $this->overview[$p] = new BananaSpoolHead($messages[$p]); |
78cd27b3 |
225 | } |
226 | $this->overview[$p]->children[] = $id; |
227 | |
ab02e8a9 |
228 | while (!is_null($p)) { |
78cd27b3 |
229 | $this->overview[$p]->desc += $msg->desc; |
ab02e8a9 |
230 | if ($p != $this->overview[$p]->parent) { |
231 | $p = $this->overview[$p]->parent; |
232 | } else { |
233 | $p = null; |
234 | } |
78cd27b3 |
235 | } |
236 | } |
237 | } |
ab02e8a9 |
238 | Banana::$protocole->updateSpool($messages); |
239 | return true; |
78cd27b3 |
240 | } |
241 | |
ab02e8a9 |
242 | private function updateUnread($since) |
78cd27b3 |
243 | { |
ab02e8a9 |
244 | if (empty($since)) { |
245 | return; |
246 | } |
247 | |
248 | $newpostsids = Banana::$protocole->getNewIndexes($since); |
4d20320e |
249 | |
ab02e8a9 |
250 | if (empty($newpostsids)) { |
251 | return; |
252 | } |
253 | |
254 | if (!is_array($this->ids)) { |
255 | $this->ids = array(); |
256 | } |
257 | $newpostsids = array_intersect($newpostsids, array_keys($this->ids)); |
258 | foreach ($newpostsids as $mid) { |
259 | $id = $this->ids[$mid]; |
260 | if ($this->overview[$id]->isread) { |
261 | $this->overview[$id]->isread = false; |
ab02e8a9 |
262 | while (isset($id)) { |
263 | $this->overview[$id]->descunread ++; |
264 | $id = $this->overview[$id]->parent; |
78cd27b3 |
265 | } |
266 | } |
ab02e8a9 |
267 | } |
268 | } |
78cd27b3 |
269 | |
ab02e8a9 |
270 | public function setMode($mode) |
271 | { |
7972645b |
272 | $this->mode = $mode; |
ab02e8a9 |
273 | switch ($mode) { |
274 | case Banana::SPOOL_UNREAD: |
275 | foreach ($this->roots as $k=>$i) { |
276 | if ($this->overview[$i]->descunread == 0) { |
277 | $this->killdesc($i); |
278 | unset($this->roots[$k]); |
78cd27b3 |
279 | } |
280 | } |
ab02e8a9 |
281 | break; |
78cd27b3 |
282 | } |
283 | } |
284 | |
7972645b |
285 | /** Mark the given id as read |
286 | * @param id MSGNUM of post |
287 | */ |
288 | public function markAsRead($id) |
289 | { |
290 | if (!$this->overview[$id]->isread) { |
291 | $this->overview[$id]->isread = true; |
292 | while (isset($id)) { |
293 | $this->overview[$id]->descunread--; |
294 | $id = $this->overview[$id]->parent; |
295 | } |
296 | } |
297 | } |
298 | |
299 | /** Mark all unread messages as read |
300 | */ |
301 | public function markAllAsRead(array &$array = null) |
302 | { |
303 | if (is_null($array)) { |
304 | $array =& $this->roots; |
305 | } |
306 | foreach ($array as $id) { |
307 | if (!$this->overview[$id]->isread) { |
308 | $this->markAsRead($id); |
309 | } |
310 | if ($this->overview[$id]->descunread) { |
311 | $this->markAllAsRead($this->overview[$id]->children); |
312 | } |
313 | } |
314 | } |
315 | |
78cd27b3 |
316 | /** kill post and childrens |
317 | * @param $_id MSGNUM of post |
318 | */ |
ab02e8a9 |
319 | private function killdesc($_id) |
78cd27b3 |
320 | { |
321 | if (sizeof($this->overview[$_id]->children)) { |
322 | foreach ($this->overview[$_id]->children as $c) { |
323 | $this->killdesc($c); |
324 | } |
325 | } |
326 | unset($this->overview[$_id]); |
327 | if (($msgid = array_search($_id, $this->ids)) !== false) { |
328 | unset($this->ids[$msgid]); |
329 | } |
330 | } |
331 | |
332 | /** delete a post from overview |
333 | * @param $_id MSGNUM of post |
334 | */ |
ab02e8a9 |
335 | public function delid($_id, $write = true) |
78cd27b3 |
336 | { |
337 | if (isset($this->overview[$_id])) { |
338 | if (sizeof($this->overview[$_id]->parent)) { |
339 | $this->overview[$this->overview[$_id]->parent]->children = |
340 | array_diff($this->overview[$this->overview[$_id]->parent]->children, array($_id)); |
341 | if (sizeof($this->overview[$_id]->children)) { |
342 | $this->overview[$this->overview[$_id]->parent]->children = |
343 | array_merge($this->overview[$this->overview[$_id]->parent]->children, $this->overview[$_id]->children); |
344 | foreach ($this->overview[$_id]->children as $c) { |
345 | $this->overview[$c]->parent = $this->overview[$_id]->parent; |
346 | $this->overview[$c]->parent_direct = false; |
347 | } |
348 | } |
349 | $p = $this->overview[$_id]->parent; |
350 | while ($p) { |
351 | $this->overview[$p]->desc--; |
352 | $p = $this->overview[$p]->parent; |
353 | } |
354 | } elseif (sizeof($this->overview[$_id]->children)) { |
355 | foreach ($this->overview[$_id]->children as $c) { |
356 | $this->overview[$c]->parent = null; |
357 | } |
358 | } |
359 | unset($this->overview[$_id]); |
360 | $msgid = array_search($_id, $this->ids); |
361 | if ($msgid) { |
362 | unset($this->ids[$msgid]); |
363 | } |
364 | |
ab02e8a9 |
365 | if ($write) { |
e0f557df |
366 | $this->markAllAsRead(); |
ab02e8a9 |
367 | $this->saveToFile(); |
368 | } |
78cd27b3 |
369 | } |
370 | } |
371 | |
ab02e8a9 |
372 | private function formatDate($stamp) |
373 | { |
374 | $today = intval(time() / (24*3600)); |
375 | $dday = intval($stamp / (24*3600)); |
376 | |
377 | if ($today == $dday) { |
378 | $format = "%H:%M"; |
379 | } elseif ($today == 1 + $dday) { |
380 | $format = _b_('hier')." %H:%M"; |
381 | } elseif ($today < 7 + $dday) { |
382 | $format = '%a %H:%M'; |
383 | } else { |
384 | $format = '%a %e %b'; |
385 | } |
386 | return utf8_encode(strftime($format, $stamp)); |
387 | } |
388 | |
78cd27b3 |
389 | /** displays children tree of a post |
390 | * @param $_id INTEGER MSGNUM of post |
391 | * @param $_index INTEGER linear number of post in the tree |
392 | * @param $_first INTEGER linear number of first post displayed |
393 | * @param $_last INTEGER linear number of last post displayed |
394 | * @param $_ref STRING MSGNUM of current post |
395 | * @param $_pfx_node STRING prefix used for current node |
396 | * @param $_pfx_end STRING prefix used for children of current node |
397 | * @param $_head BOOLEAN true if first post in thread |
e2347ffc |
398 | * |
d7718cc5 |
399 | * If you want to analyse subject, you can define the function hook_formatDisplayHeader |
78cd27b3 |
400 | */ |
ab02e8a9 |
401 | private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true) |
78cd27b3 |
402 | { |
ab02e8a9 |
403 | static $spfx_f, $spfx_n, $spfx_Tnd, $spfx_Lnd, $spfx_snd, $spfx_T, $spfx_L, $spfx_s, $spfx_e, $spfx_I; |
404 | if (!isset($spfx_f)) { |
405 | $spfx_f = Banana::$page->makeImg(Array('img' => 'k1', 'alt' => 'o', 'height' => 21, 'width' => 9)); |
406 | $spfx_n = Banana::$page->makeImg(Array('img' => 'k2', 'alt' => '*', 'height' => 21, 'width' => 9)); |
407 | $spfx_Tnd = Banana::$page->makeImg(Array('img' => 'T-direct', 'alt' => '+', 'height' => 21, 'width' => 12)); |
408 | $spfx_Lnd = Banana::$page->makeImg(Array('img' => 'L-direct', 'alt' => '`', 'height' => 21, 'width' => 12)); |
409 | $spfx_snd = Banana::$page->makeImg(Array('img' => 's-direct', 'alt' => '-', 'height' => 21, 'width' => 5)); |
410 | $spfx_T = Banana::$page->makeImg(Array('img' => 'T', 'alt' => '+', 'height' => 21, 'width' => 12)); |
411 | $spfx_L = Banana::$page->makeImg(Array('img' => 'L', 'alt' => '`', 'height' => 21, 'width' => 12)); |
412 | $spfx_s = Banana::$page->makeImg(Array('img' => 's', 'alt' => '-', 'height' => 21, 'width' => 5)); |
413 | $spfx_e = Banana::$page->makeImg(Array('img' => 'e', 'alt' => ' ', 'height' => 21, 'width' => 12)); |
414 | $spfx_I = Banana::$page->makeImg(Array('img' => 'I', 'alt' => '|', 'height' => 21, 'width' => 12)); |
78cd27b3 |
415 | } |
416 | |
ab02e8a9 |
417 | $overview =& $this->overview[$_id]; |
418 | if ($_index + $overview->desc < $_first || $_index > $_last) { |
419 | return ''; |
420 | } |
78cd27b3 |
421 | |
ab02e8a9 |
422 | $res = ''; |
423 | if ($_index >= $_first) { |
424 | $hc = empty($overview->children); |
425 | |
426 | $res .= '<tr class="' . ($_index%2 ? 'pair' : 'impair') . ($overview->isread ? '' : ' new') . "\">\n"; |
427 | $res .= '<td class="date">' . $this->formatDate($overview->date) . " </td>\n"; |
428 | $res .= '<td class="subj' . ($_index == $_ref ? ' cur' : '') . '">' |
429 | . $_pfx_node .($hc ? ($_head ? $spfx_f : ($overview->parent_direct ? $spfx_s : $spfx_snd)) : $spfx_n); |
430 | $subject = $overview->subject; |
d7718cc5 |
431 | if (function_exists('hook_formatDisplayHeader')) { |
432 | list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true); |
433 | } else { |
dc5684a2 |
434 | $subject = banana_catchFormats(banana_htmlentities(stripslashes($subject))); |
d7718cc5 |
435 | $link = null; |
436 | } |
ab02e8a9 |
437 | if (empty($subject)) { |
71d6c865 |
438 | $subject = _b_('(pas de sujet)'); |
439 | } |
ab02e8a9 |
440 | if ($_index != $_ref) { |
441 | $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $_id, |
442 | 'text' => $subject, 'popup' => $subject)); |
78cd27b3 |
443 | } |
ab02e8a9 |
444 | $res .= ' ' . $subject . $link; |
445 | $res .= "</td>\n<td class='from'>" . BananaMessage::formatFrom($overview->from) . "</td>\n</tr>"; |
78cd27b3 |
446 | |
ab02e8a9 |
447 | if ($hc) { |
448 | return $res; |
449 | } |
78cd27b3 |
450 | } |
451 | |
452 | $_index ++; |
ab02e8a9 |
453 | $children = $overview->children; |
78cd27b3 |
454 | while ($child = array_shift($children)) { |
ab02e8a9 |
455 | $overview =& $this->overview[$child]; |
456 | if ($_index > $_last) { |
457 | return $res; |
458 | } |
459 | if ($_index + $overview->desc >= $_first) { |
78cd27b3 |
460 | if (sizeof($children)) { |
461 | $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref, |
ab02e8a9 |
462 | $_pfx_end . ($overview->parent_direct ? $spfx_T : $spfx_Tnd), |
463 | $_pfx_end . $spfx_I, false); |
78cd27b3 |
464 | } else { |
465 | $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref, |
ab02e8a9 |
466 | $_pfx_end . ($overview->parent_direct ? $spfx_L : $spfx_Lnd), |
467 | $_pfx_end . $spfx_e, false); |
78cd27b3 |
468 | } |
469 | } |
ab02e8a9 |
470 | $_index += $overview->desc; |
78cd27b3 |
471 | } |
472 | |
473 | return $res; |
474 | } |
475 | |
476 | /** Displays overview |
477 | * @param $_first INTEGER MSGNUM of first post |
478 | * @param $_last INTEGER MSGNUM of last post |
479 | * @param $_ref STRING MSGNUM of current/selectionned post |
480 | */ |
ab02e8a9 |
481 | public function toHtml($first = 0, $overview = false) |
78cd27b3 |
482 | { |
ab02e8a9 |
483 | $res = ''; |
484 | |
485 | if (!$overview) { |
486 | $_first = $first; |
7972645b |
487 | $_last = $first + Banana::$spool_tmax - 1; |
ab02e8a9 |
488 | $_ref = null; |
2ba32e89 |
489 | } else { |
ab02e8a9 |
490 | $_ref = $this->getNdx($first); |
7972645b |
491 | $_last = $_ref + Banana::$spool_tafter; |
492 | $_first = $_ref - Banana::$spool_tbefore; |
ab02e8a9 |
493 | if ($_first < 0) { |
494 | $_last -= $_first; |
495 | } |
78cd27b3 |
496 | } |
78cd27b3 |
497 | $index = 1; |
ab02e8a9 |
498 | foreach ($this->roots as $id) { |
499 | $res .= $this->_to_html($id, $index, $_first, $_last, $_ref); |
500 | $index += $this->overview[$id]->desc ; |
501 | if ($index > $_last) { |
502 | break; |
78cd27b3 |
503 | } |
e230b41a |
504 | } |
ab02e8a9 |
505 | return $res; |
78cd27b3 |
506 | } |
507 | |
508 | /** computes linear post index |
509 | * @param $_id INTEGER MSGNUM of post |
510 | * @return INTEGER linear index of post |
511 | */ |
ab02e8a9 |
512 | public function getNdX($_id) |
78cd27b3 |
513 | { |
514 | $ndx = 1; |
515 | $id_cur = $_id; |
516 | while (true) { |
517 | $id_parent = $this->overview[$id_cur]->parent; |
518 | if (is_null($id_parent)) break; |
519 | $pos = array_search($id_cur, $this->overview[$id_parent]->children); |
520 | |
521 | for ($i = 0; $i < $pos ; $i++) { |
522 | $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc; |
523 | } |
d8d416c4 |
524 | $ndx++; //noeud père |
78cd27b3 |
525 | |
526 | $id_cur = $id_parent; |
527 | } |
528 | |
529 | foreach ($this->roots as $i) { |
530 | if ($i==$id_cur) { |
531 | break; |
532 | } |
533 | $ndx += $this->overview[$i]->desc; |
534 | } |
535 | return $ndx; |
536 | } |
2ba32e89 |
537 | |
538 | /** Return root message of the given thread |
539 | * @param id INTEGER id of a message |
540 | */ |
ab02e8a9 |
541 | public function root($id) |
542 | { |
2ba32e89 |
543 | $id_cur = $id; |
544 | while (true) { |
545 | $id_parent = $this->overview[$id_cur]->parent; |
546 | if (is_null($id_parent)) break; |
547 | $id_cur = $id_parent; |
548 | } |
549 | return $id_cur; |
550 | } |
551 | |
552 | /** Returns previous thread root index |
553 | * @param id INTEGER message number |
554 | */ |
ab02e8a9 |
555 | public function prevThread($id) |
2ba32e89 |
556 | { |
557 | $root = $this->root($id); |
558 | $last = null; |
559 | foreach ($this->roots as $i) { |
560 | if ($i == $root) { |
561 | return $last; |
562 | } |
563 | $last = $i; |
564 | } |
565 | return $last; |
566 | } |
567 | |
568 | /** Returns next thread root index |
569 | * @param id INTEGER message number |
570 | */ |
ab02e8a9 |
571 | public function nextThread($id) |
2ba32e89 |
572 | { |
573 | $root = $this->root($id); |
574 | $ok = false; |
575 | foreach ($this->roots as $i) { |
576 | if ($ok) { |
577 | return $i; |
578 | } |
579 | if ($i == $root) { |
580 | $ok = true; |
581 | } |
582 | } |
583 | return null; |
584 | } |
585 | |
586 | /** Return prev post in the thread |
587 | * @param id INTEGER message number |
588 | */ |
ab02e8a9 |
589 | public function prevPost($id) |
2ba32e89 |
590 | { |
591 | $parent = $this->overview[$id]->parent; |
592 | if (is_null($parent)) { |
593 | return null; |
594 | } |
595 | $last = $parent; |
596 | foreach ($this->overview[$parent]->children as $child) { |
597 | if ($child == $id) { |
598 | return $last; |
599 | } |
600 | $last = $child; |
601 | } |
602 | return null; |
603 | } |
604 | |
605 | /** Return next post in the thread |
606 | * @param id INTEGER message number |
607 | */ |
ab02e8a9 |
608 | public function nextPost($id) |
2ba32e89 |
609 | { |
610 | if (count($this->overview[$id]->children) != 0) { |
611 | return $this->overview[$id]->children[0]; |
612 | } |
613 | |
614 | $cur = $id; |
615 | while (true) { |
616 | $parent = $this->overview[$cur]->parent; |
617 | if (is_null($parent)) { |
618 | return null; |
619 | } |
620 | $ok = false; |
621 | foreach ($this->overview[$parent]->children as $child) { |
622 | if ($ok) { |
623 | return $child; |
624 | } |
625 | if ($child == $cur) { |
626 | $ok = true; |
627 | } |
628 | } |
629 | $cur = $parent; |
630 | } |
631 | return null; |
632 | } |
dc12019c |
633 | |
634 | /** Look for an unread message in the thread rooted by the message |
635 | * @param id INTEGER message number |
636 | */ |
ab02e8a9 |
637 | private function _nextUnread($id) |
dc12019c |
638 | { |
639 | if (!$this->overview[$id]->isread) { |
640 | return $id; |
641 | } |
642 | foreach ($this->overview[$id]->children as $child) { |
6aa1f5e4 |
643 | $unread = $this->_nextUnread($child); |
644 | if (!is_null($unread)) { |
645 | return $unread; |
646 | } |
dc12019c |
647 | } |
648 | return null; |
649 | } |
650 | |
651 | /** Find next unread message |
652 | * @param id INTEGER message number |
653 | */ |
ab02e8a9 |
654 | public function nextUnread($id = null) |
dc12019c |
655 | { |
27db62eb |
656 | if (!is_null($id)) { |
657 | // Look in message children |
658 | foreach ($this->overview[$id]->children as $child) { |
659 | $next = $this->_nextUnread($child); |
660 | if (!is_null($next)) { |
661 | return $next; |
662 | } |
dc12019c |
663 | } |
664 | } |
665 | |
666 | // Look in current thread |
667 | $cur = $id; |
27db62eb |
668 | do { |
669 | $parent = is_null($cur) ? null : $this->overview[$cur]->parent; |
670 | $ok = is_null($cur) ? true : false; |
a02dd64a |
671 | if (!is_null($parent)) { |
dc12019c |
672 | $array = &$this->overview[$parent]->children; |
673 | } else { |
674 | $array = &$this->roots; |
675 | } |
676 | foreach ($array as $child) { |
677 | if ($ok) { |
678 | $next = $this->_nextUnread($child); |
679 | if (!is_null($next)) { |
680 | return $next; |
681 | } |
682 | } |
683 | if ($child == $cur) { |
684 | $ok = true; |
685 | } |
686 | } |
687 | $cur = $parent; |
27db62eb |
688 | } while(!is_null($cur)); |
dc12019c |
689 | return null; |
690 | } |
78cd27b3 |
691 | } |
692 | |
d8d416c4 |
693 | // vim:set et sw=4 sts=4 ts=4 enc=utf-8: |
78cd27b3 |
694 | ?> |