Commit | Line | Data |
---|---|---|
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 | ||
7027794f | 10 | require_once dirname(__FILE__) . '/banana.inc.php'; |
3ca86dfe | 11 | |
7027794f | 12 | define('BANANA_SPOOL_VERSION', '0.3'); |
01681efd | 13 | |
810ac1df | 14 | /** Class spoolhead |
15 | * class used in thread overviews | |
16 | */ | |
d4c19591 | 17 | class BananaSpoolHead |
e785d91c | 18 | { |
19 | /** date (timestamp) */ | |
7027794f | 20 | public $date; |
e785d91c | 21 | /** subject */ |
7027794f | 22 | public $subject; |
e785d91c | 23 | /** author */ |
7027794f | 24 | public $from; |
e785d91c | 25 | /** reference of parent */ |
7027794f | 26 | public $parent = null; |
e785d91c | 27 | /** paren is direct */ |
7027794f | 28 | public $parent_direct; |
e785d91c | 29 | /** array of children */ |
7027794f | 30 | public $children = Array(); |
e785d91c | 31 | /** true if post is read */ |
7027794f | 32 | public $isread; |
e785d91c | 33 | /** number of posts deeper in this branch of tree */ |
7027794f | 34 | public $desc; |
e785d91c | 35 | /** same as desc, but counts only unread posts */ |
7027794f | 36 | public $descunread; |
37 | ||
38 | /** storage data */ | |
39 | public $storage = array(); | |
810ac1df | 40 | |
e785d91c | 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 | */ | |
7027794f | 49 | public function __construct(array &$message) |
e785d91c | 50 | { |
7027794f | 51 | $this->date = $message['date']; |
b87c9103 | 52 | $this->subject = $message['subject']; |
7027794f | 53 | $this->from = $message['from']; |
54 | $this->desc = 1; | |
55 | $this->isread = true; | |
56 | $this->descunread = 0; | |
e785d91c | 57 | } |
810ac1df | 58 | } |
59 | ||
3ca86dfe | 60 | |
d4c19591 | 61 | class BananaSpool |
e785d91c | 62 | { |
7027794f | 63 | private $version; |
e9360b11 | 64 | private $mode; |
7027794f | 65 | |
e785d91c | 66 | /** group name */ |
7027794f | 67 | public $group; |
68 | /** spool */ | |
69 | public $overview; | |
e785d91c | 70 | /** array msgid => msgnum */ |
7027794f | 71 | public $ids; |
cced14b6 | 72 | /** thread starts */ |
7027794f | 73 | public $roots; |
74 | ||
810ac1df | 75 | |
e785d91c | 76 | /** constructor |
e785d91c | 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 | */ | |
7027794f | 81 | protected function __construct($group) |
e785d91c | 82 | { |
7027794f | 83 | $this->version = BANANA_SPOOL_VERSION; |
e9360b11 | 84 | $this->mode = Banana::SPOOL_ALL; |
7027794f | 85 | $this->group = $group; |
86 | } | |
fb6428c8 | 87 | |
b3d11736 | 88 | public static function getSpool($group, $since = 0, $clean = false) |
7027794f | 89 | { |
90 | if (!is_null(Banana::$spool) && Banana::$spool->group == $group) { | |
b3d11736 | 91 | $spool =& Banana::$spool; |
82c17a91 | 92 | } else { |
7027794f | 93 | $spool = BananaSpool::readFromFile($group); |
94 | } | |
95 | if (is_null($spool)) { | |
96 | $spool = new BananaSpool($group); | |
82c17a91 | 97 | } |
7027794f | 98 | Banana::$spool =& $spool; |
99 | $spool->build(); | |
b3d11736 | 100 | if ($clean) { |
101 | $spool->markAllAsRead(); | |
102 | } | |
7027794f | 103 | $spool->updateUnread($since); |
104 | return $spool; | |
105 | } | |
e785d91c | 106 | |
7027794f | 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); | |
dd7d1c59 | 113 | } |
e9360b11 | 114 | return $file . Banana::$protocole->filename(); |
dd7d1c59 | 115 | } |
116 | ||
7027794f | 117 | private static function readFromFile($group) |
dd7d1c59 | 118 | { |
7027794f | 119 | $file = BananaSpool::spoolFilename($group); |
120 | if (!file_exists($file)) { | |
121 | return null; | |
dd7d1c59 | 122 | } |
7027794f | 123 | $spool = unserialize(file_get_contents($file)); |
e9360b11 | 124 | if ($spool->version != BANANA_SPOOL_VERSION || $spool->mode != Banana::SPOOL_ALL) { |
7027794f | 125 | return null; |
126 | } | |
127 | return $spool; | |
128 | } | |
129 | ||
130 | private function compare($a, $b) | |
131 | { | |
132 | return ($b->date >= $a->date); | |
dd7d1c59 | 133 | } |
134 | ||
7027794f | 135 | private function saveToFile() |
dd7d1c59 | 136 | { |
7027794f | 137 | $file = BananaSpool::spoolFilename($this->group); |
138 | uasort($this->overview, array($this, 'compare')); | |
dd7d1c59 | 139 | |
140 | $this->roots = Array(); | |
141 | foreach($this->overview as $id=>$msg) { | |
142 | if (is_null($msg->parent)) { | |
143 | $this->roots[] = $id; | |
144 | } | |
145 | } | |
2c606d23 | 146 | |
e9360b11 | 147 | if ($this->mode == Banana::SPOOL_ALL) { |
148 | file_put_contents($file, serialize($this)); | |
149 | } | |
dd7d1c59 | 150 | } |
151 | ||
7027794f | 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 | } | |
e9360b11 | 163 | if (Banana::$spool_max && Banana::$spool_max < $msgnum) { |
164 | $first = $last - Banana::$spool_max; | |
7027794f | 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) | |
9090c673 | 178 | { |
7027794f | 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)) | |
e9360b11 | 184 | || ($first > $last && $id < $first && $id > $last)) { |
7027794f | 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; | |
9090c673 PHM |
194 | } |
195 | ||
7027794f | 196 | private function update(&$first, &$last, $msgnum) |
dd7d1c59 | 197 | { |
2f0aa8ce | 198 | if ($first > $last || !$msgnum) { |
7027794f | 199 | return false; |
dd7d1c59 | 200 | } |
201 | ||
7027794f | 202 | $messages =& Banana::$protocole->getMessageHeaders($first, $last, |
203 | array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To')); | |
dd7d1c59 | 204 | |
7027794f | 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); | |
3316e34e | 215 | } |
7027794f | 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)); | |
e785d91c | 221 | |
7027794f | 222 | if (!is_null($p = $msg->parent)) { |
dd7d1c59 | 223 | if (empty($this->overview[$p])) { |
7027794f | 224 | $this->overview[$p] = new BananaSpoolHead($messages[$p]); |
4ced5065 | 225 | } |
dd7d1c59 | 226 | $this->overview[$p]->children[] = $id; |
4ced5065 | 227 | |
7027794f | 228 | while (!is_null($p)) { |
dd7d1c59 | 229 | $this->overview[$p]->desc += $msg->desc; |
7027794f | 230 | if ($p != $this->overview[$p]->parent) { |
231 | $p = $this->overview[$p]->parent; | |
232 | } else { | |
233 | $p = null; | |
234 | } | |
e785d91c | 235 | } |
810ac1df | 236 | } |
810ac1df | 237 | } |
7027794f | 238 | Banana::$protocole->updateSpool($messages); |
239 | return true; | |
dd7d1c59 | 240 | } |
e785d91c | 241 | |
7027794f | 242 | private function updateUnread($since) |
75ff2f64 | 243 | { |
7027794f | 244 | if (empty($since)) { |
245 | return; | |
246 | } | |
247 | ||
248 | $newpostsids = Banana::$protocole->getNewIndexes($since); | |
b3d11736 | 249 | |
7027794f | 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; | |
7027794f | 262 | while (isset($id)) { |
263 | $this->overview[$id]->descunread ++; | |
264 | $id = $this->overview[$id]->parent; | |
e785d91c | 265 | } |
810ac1df | 266 | } |
7027794f | 267 | } |
268 | } | |
dd7d1c59 | 269 | |
7027794f | 270 | public function setMode($mode) |
271 | { | |
e9360b11 | 272 | $this->mode = $mode; |
7027794f | 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]); | |
e785d91c | 279 | } |
810ac1df | 280 | } |
7027794f | 281 | break; |
810ac1df | 282 | } |
cced14b6 | 283 | } |
284 | ||
e9360b11 | 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 | ||
e785d91c | 316 | /** kill post and childrens |
317 | * @param $_id MSGNUM of post | |
318 | */ | |
7027794f | 319 | private function killdesc($_id) |
e785d91c | 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]); | |
dd7d1c59 | 327 | if (($msgid = array_search($_id, $this->ids)) !== false) { |
e785d91c | 328 | unset($this->ids[$msgid]); |
329 | } | |
810ac1df | 330 | } |
e785d91c | 331 | |
332 | /** delete a post from overview | |
333 | * @param $_id MSGNUM of post | |
334 | */ | |
7027794f | 335 | public function delid($_id, $write = true) |
e785d91c | 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]); | |
3ca86dfe | 360 | $msgid = array_search($_id, $this->ids); |
e785d91c | 361 | if ($msgid) { |
362 | unset($this->ids[$msgid]); | |
363 | } | |
364 | ||
7027794f | 365 | if ($write) { |
366 | $this->saveToFile(); | |
367 | } | |
e785d91c | 368 | } |
35ca8036 | 369 | } |
810ac1df | 370 | |
7027794f | 371 | private function formatDate($stamp) |
372 | { | |
373 | $today = intval(time() / (24*3600)); | |
374 | $dday = intval($stamp / (24*3600)); | |
375 | ||
376 | if ($today == $dday) { | |
377 | $format = "%H:%M"; | |
378 | } elseif ($today == 1 + $dday) { | |
379 | $format = _b_('hier')." %H:%M"; | |
380 | } elseif ($today < 7 + $dday) { | |
381 | $format = '%a %H:%M'; | |
382 | } else { | |
383 | $format = '%a %e %b'; | |
384 | } | |
385 | return utf8_encode(strftime($format, $stamp)); | |
386 | } | |
387 | ||
e785d91c | 388 | /** displays children tree of a post |
389 | * @param $_id INTEGER MSGNUM of post | |
390 | * @param $_index INTEGER linear number of post in the tree | |
391 | * @param $_first INTEGER linear number of first post displayed | |
392 | * @param $_last INTEGER linear number of last post displayed | |
393 | * @param $_ref STRING MSGNUM of current post | |
394 | * @param $_pfx_node STRING prefix used for current node | |
395 | * @param $_pfx_end STRING prefix used for children of current node | |
396 | * @param $_head BOOLEAN true if first post in thread | |
3204d440 | 397 | * |
b87c9103 | 398 | * If you want to analyse subject, you can define the function hook_formatDisplayHeader |
e785d91c | 399 | */ |
7027794f | 400 | private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true) |
75ff2f64 | 401 | { |
7027794f | 402 | static $spfx_f, $spfx_n, $spfx_Tnd, $spfx_Lnd, $spfx_snd, $spfx_T, $spfx_L, $spfx_s, $spfx_e, $spfx_I; |
403 | if (!isset($spfx_f)) { | |
404 | $spfx_f = Banana::$page->makeImg(Array('img' => 'k1', 'alt' => 'o', 'height' => 21, 'width' => 9)); | |
405 | $spfx_n = Banana::$page->makeImg(Array('img' => 'k2', 'alt' => '*', 'height' => 21, 'width' => 9)); | |
406 | $spfx_Tnd = Banana::$page->makeImg(Array('img' => 'T-direct', 'alt' => '+', 'height' => 21, 'width' => 12)); | |
407 | $spfx_Lnd = Banana::$page->makeImg(Array('img' => 'L-direct', 'alt' => '`', 'height' => 21, 'width' => 12)); | |
408 | $spfx_snd = Banana::$page->makeImg(Array('img' => 's-direct', 'alt' => '-', 'height' => 21, 'width' => 5)); | |
409 | $spfx_T = Banana::$page->makeImg(Array('img' => 'T', 'alt' => '+', 'height' => 21, 'width' => 12)); | |
410 | $spfx_L = Banana::$page->makeImg(Array('img' => 'L', 'alt' => '`', 'height' => 21, 'width' => 12)); | |
411 | $spfx_s = Banana::$page->makeImg(Array('img' => 's', 'alt' => '-', 'height' => 21, 'width' => 5)); | |
412 | $spfx_e = Banana::$page->makeImg(Array('img' => 'e', 'alt' => ' ', 'height' => 21, 'width' => 12)); | |
413 | $spfx_I = Banana::$page->makeImg(Array('img' => 'I', 'alt' => '|', 'height' => 21, 'width' => 12)); | |
810ac1df | 414 | } |
e785d91c | 415 | |
7027794f | 416 | $overview =& $this->overview[$_id]; |
417 | if ($_index + $overview->desc < $_first || $_index > $_last) { | |
418 | return ''; | |
419 | } | |
cced14b6 | 420 | |
7027794f | 421 | $res = ''; |
422 | if ($_index >= $_first) { | |
423 | $hc = empty($overview->children); | |
424 | ||
425 | $res .= '<tr class="' . ($_index%2 ? 'pair' : 'impair') . ($overview->isread ? '' : ' new') . "\">\n"; | |
426 | $res .= '<td class="date">' . $this->formatDate($overview->date) . " </td>\n"; | |
427 | $res .= '<td class="subj' . ($_index == $_ref ? ' cur' : '') . '">' | |
428 | . $_pfx_node .($hc ? ($_head ? $spfx_f : ($overview->parent_direct ? $spfx_s : $spfx_snd)) : $spfx_n); | |
429 | $subject = $overview->subject; | |
b87c9103 | 430 | if (function_exists('hook_formatDisplayHeader')) { |
431 | list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true); | |
432 | } else { | |
433 | $subject = banana_catchFormats(stripslashes($subject)); | |
434 | $link = null; | |
435 | } | |
7027794f | 436 | if (empty($subject)) { |
79405147 | 437 | $subject = _b_('(pas de sujet)'); |
438 | } | |
7027794f | 439 | if ($_index != $_ref) { |
440 | $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $_id, | |
441 | 'text' => $subject, 'popup' => $subject)); | |
cced14b6 | 442 | } |
7027794f | 443 | $res .= ' ' . $subject . $link; |
444 | $res .= "</td>\n<td class='from'>" . BananaMessage::formatFrom($overview->from) . "</td>\n</tr>"; | |
dd7d1c59 | 445 | |
7027794f | 446 | if ($hc) { |
447 | return $res; | |
448 | } | |
cced14b6 | 449 | } |
450 | ||
dd7d1c59 | 451 | $_index ++; |
7027794f | 452 | $children = $overview->children; |
e785d91c | 453 | while ($child = array_shift($children)) { |
7027794f | 454 | $overview =& $this->overview[$child]; |
455 | if ($_index > $_last) { | |
456 | return $res; | |
457 | } | |
458 | if ($_index + $overview->desc >= $_first) { | |
e785d91c | 459 | if (sizeof($children)) { |
65d96b1f | 460 | $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref, |
7027794f | 461 | $_pfx_end . ($overview->parent_direct ? $spfx_T : $spfx_Tnd), |
462 | $_pfx_end . $spfx_I, false); | |
e785d91c | 463 | } else { |
65d96b1f | 464 | $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref, |
7027794f | 465 | $_pfx_end . ($overview->parent_direct ? $spfx_L : $spfx_Lnd), |
466 | $_pfx_end . $spfx_e, false); | |
e785d91c | 467 | } |
468 | } | |
7027794f | 469 | $_index += $overview->desc; |
810ac1df | 470 | } |
65d96b1f | 471 | |
472 | return $res; | |
810ac1df | 473 | } |
810ac1df | 474 | |
e785d91c | 475 | /** Displays overview |
476 | * @param $_first INTEGER MSGNUM of first post | |
477 | * @param $_last INTEGER MSGNUM of last post | |
478 | * @param $_ref STRING MSGNUM of current/selectionned post | |
479 | */ | |
7027794f | 480 | public function toHtml($first = 0, $overview = false) |
75ff2f64 | 481 | { |
7027794f | 482 | $res = ''; |
483 | ||
484 | if (!$overview) { | |
485 | $_first = $first; | |
e9360b11 | 486 | $_last = $first + Banana::$spool_tmax - 1; |
7027794f | 487 | $_ref = null; |
d8e2470c | 488 | } else { |
7027794f | 489 | $_ref = $this->getNdx($first); |
e9360b11 | 490 | $_last = $_ref + Banana::$spool_tafter; |
491 | $_first = $_ref - Banana::$spool_tbefore; | |
7027794f | 492 | if ($_first < 0) { |
493 | $_last -= $_first; | |
494 | } | |
65d96b1f | 495 | } |
e785d91c | 496 | $index = 1; |
7027794f | 497 | foreach ($this->roots as $id) { |
498 | $res .= $this->_to_html($id, $index, $_first, $_last, $_ref); | |
499 | $index += $this->overview[$id]->desc ; | |
500 | if ($index > $_last) { | |
501 | break; | |
e785d91c | 502 | } |
4f75645f | 503 | } |
7027794f | 504 | return $res; |
810ac1df | 505 | } |
810ac1df | 506 | |
e785d91c | 507 | /** computes linear post index |
508 | * @param $_id INTEGER MSGNUM of post | |
509 | * @return INTEGER linear index of post | |
510 | */ | |
7027794f | 511 | public function getNdX($_id) |
75ff2f64 | 512 | { |
cced14b6 | 513 | $ndx = 1; |
514 | $id_cur = $_id; | |
4ced5065 | 515 | while (true) { |
cced14b6 | 516 | $id_parent = $this->overview[$id_cur]->parent; |
4ced5065 | 517 | if (is_null($id_parent)) break; |
cced14b6 | 518 | $pos = array_search($id_cur, $this->overview[$id_parent]->children); |
4ced5065 | 519 | |
cced14b6 | 520 | for ($i = 0; $i < $pos ; $i++) { |
e785d91c | 521 | $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc; |
522 | } | |
598a1c53 | 523 | $ndx++; //noeud père |
cced14b6 | 524 | |
525 | $id_cur = $id_parent; | |
810ac1df | 526 | } |
cced14b6 | 527 | |
528 | foreach ($this->roots as $i) { | |
529 | if ($i==$id_cur) { | |
e785d91c | 530 | break; |
531 | } | |
cced14b6 | 532 | $ndx += $this->overview[$i]->desc; |
e785d91c | 533 | } |
534 | return $ndx; | |
810ac1df | 535 | } |
d8e2470c | 536 | |
537 | /** Return root message of the given thread | |
538 | * @param id INTEGER id of a message | |
539 | */ | |
7027794f | 540 | public function root($id) |
541 | { | |
d8e2470c | 542 | $id_cur = $id; |
543 | while (true) { | |
544 | $id_parent = $this->overview[$id_cur]->parent; | |
545 | if (is_null($id_parent)) break; | |
546 | $id_cur = $id_parent; | |
547 | } | |
548 | return $id_cur; | |
549 | } | |
550 | ||
551 | /** Returns previous thread root index | |
552 | * @param id INTEGER message number | |
553 | */ | |
7027794f | 554 | public function prevThread($id) |
d8e2470c | 555 | { |
556 | $root = $this->root($id); | |
557 | $last = null; | |
558 | foreach ($this->roots as $i) { | |
559 | if ($i == $root) { | |
560 | return $last; | |
561 | } | |
562 | $last = $i; | |
563 | } | |
564 | return $last; | |
565 | } | |
566 | ||
567 | /** Returns next thread root index | |
568 | * @param id INTEGER message number | |
569 | */ | |
7027794f | 570 | public function nextThread($id) |
d8e2470c | 571 | { |
572 | $root = $this->root($id); | |
573 | $ok = false; | |
574 | foreach ($this->roots as $i) { | |
575 | if ($ok) { | |
576 | return $i; | |
577 | } | |
578 | if ($i == $root) { | |
579 | $ok = true; | |
580 | } | |
581 | } | |
582 | return null; | |
583 | } | |
584 | ||
585 | /** Return prev post in the thread | |
586 | * @param id INTEGER message number | |
587 | */ | |
7027794f | 588 | public function prevPost($id) |
d8e2470c | 589 | { |
590 | $parent = $this->overview[$id]->parent; | |
591 | if (is_null($parent)) { | |
592 | return null; | |
593 | } | |
594 | $last = $parent; | |
595 | foreach ($this->overview[$parent]->children as $child) { | |
596 | if ($child == $id) { | |
597 | return $last; | |
598 | } | |
599 | $last = $child; | |
600 | } | |
601 | return null; | |
602 | } | |
603 | ||
604 | /** Return next post in the thread | |
605 | * @param id INTEGER message number | |
606 | */ | |
7027794f | 607 | public function nextPost($id) |
d8e2470c | 608 | { |
609 | if (count($this->overview[$id]->children) != 0) { | |
610 | return $this->overview[$id]->children[0]; | |
611 | } | |
612 | ||
613 | $cur = $id; | |
614 | while (true) { | |
615 | $parent = $this->overview[$cur]->parent; | |
616 | if (is_null($parent)) { | |
617 | return null; | |
618 | } | |
619 | $ok = false; | |
620 | foreach ($this->overview[$parent]->children as $child) { | |
621 | if ($ok) { | |
622 | return $child; | |
623 | } | |
624 | if ($child == $cur) { | |
625 | $ok = true; | |
626 | } | |
627 | } | |
628 | $cur = $parent; | |
629 | } | |
630 | return null; | |
631 | } | |
d634c13c | 632 | |
633 | /** Look for an unread message in the thread rooted by the message | |
634 | * @param id INTEGER message number | |
635 | */ | |
7027794f | 636 | private function _nextUnread($id) |
d634c13c | 637 | { |
638 | if (!$this->overview[$id]->isread) { | |
639 | return $id; | |
640 | } | |
641 | foreach ($this->overview[$id]->children as $child) { | |
bdad7c9d | 642 | $unread = $this->_nextUnread($child); |
643 | if (!is_null($unread)) { | |
644 | return $unread; | |
645 | } | |
d634c13c | 646 | } |
647 | return null; | |
648 | } | |
649 | ||
650 | /** Find next unread message | |
651 | * @param id INTEGER message number | |
652 | */ | |
7027794f | 653 | public function nextUnread($id = null) |
d634c13c | 654 | { |
b7d59a47 | 655 | if (!is_null($id)) { |
656 | // Look in message children | |
657 | foreach ($this->overview[$id]->children as $child) { | |
658 | $next = $this->_nextUnread($child); | |
659 | if (!is_null($next)) { | |
660 | return $next; | |
661 | } | |
d634c13c | 662 | } |
663 | } | |
664 | ||
665 | // Look in current thread | |
666 | $cur = $id; | |
b7d59a47 | 667 | do { |
668 | $parent = is_null($cur) ? null : $this->overview[$cur]->parent; | |
669 | $ok = is_null($cur) ? true : false; | |
1e016f3a | 670 | if (!is_null($parent)) { |
d634c13c | 671 | $array = &$this->overview[$parent]->children; |
672 | } else { | |
673 | $array = &$this->roots; | |
674 | } | |
675 | foreach ($array as $child) { | |
676 | if ($ok) { | |
677 | $next = $this->_nextUnread($child); | |
678 | if (!is_null($next)) { | |
679 | return $next; | |
680 | } | |
681 | } | |
682 | if ($child == $cur) { | |
683 | $ok = true; | |
684 | } | |
685 | } | |
686 | $cur = $parent; | |
b7d59a47 | 687 | } while(!is_null($cur)); |
d634c13c | 688 | return null; |
689 | } | |
810ac1df | 690 | } |
691 | ||
598a1c53 | 692 | // vim:set et sw=4 sts=4 ts=4 enc=utf-8: |
810ac1df | 693 | ?> |