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