web pages
[banana.git] / include / 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
10/** Class spoolhead
11 * class used in thread overviews
12 */
13class spoolhead {
14 /** date (timestamp) */
15 var $date;
16 /** subject */
17 var $subject;
18 /** author */
19 var $from;
20 /** reference of parent */
21 var $parent;
22 /** array of children */
23 var $children;
24 /** true if post is read */
25 var $isread;
26 /** number of posts deeper in this branch of tree */
27 var $desc;
28 /** same as desc, but counts only unread posts */
29 var $descunread;
30
31 /** constructor
32 * @param $_date INTEGER timestamp of post
33 * @param $_subject STRING subject of post
34 * @param $_from STRING author of post
35 * @param $_desc INTEGER desc value (1 for a new post)
36 * @param $_read BOOLEAN true if read
37 * @param $_descunread INTEGER descunread value (0 for a new post)
38 */
39
40 function spoolhead($_date,$_subject,$_from,$_desc=1,$_read=true,$_descunread=0) {
41 $this->date=$_date;
42 $this->subject=$_subject;
43 $this->from=$_from;
44 $this->children=array();
45 $this->desc=$_desc;
46 $this->isread=$_read;
47 $this->descunread=$_descunread;
48 }
49}
50
51/** Class spool
52 * builds and updates spool
53 */
54
55class spool {
56# var $date;
57 /** spool */
58 var $overview;
59 /** group name */
60 var $group;
61 /** array msgid => msgnum */
62 var $ids;
63
64 /** constructor
65 * @param $_nntp RESOURCE NNTP socket filehandle
66 * @param $_group STRING group name
67 * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
68 * @param $_since INTEGER time stamp (used for read/unread)
69 */
70
71 function spool(&$_nntp,$_group,$_display=0,$_since=""){
72 $groupinfo = $_nntp->group($_group);
73 if (!$groupinfo) {
74 $this = false;
75 return false;
76 }
77 if (file_exists("/home/basile/dev/banana-dev/spool/spool-$_group.dat")) {
78 $f = fopen("/home/basile/dev/banana-dev/spool/spool-$_group.dat","r");
79 $this = unserialize(fread($f,filesize("/home/basile/dev/banana-dev/spool/spool-$_group.dat")));
80 fclose($f);
81 $keys = array_values($this->ids);
82 rsort($keys);
83 $first = $keys[0]+1;
84 $last = $groupinfo[2];
85 } else {
86 $first = $groupinfo[1];
87 $last = $groupinfo[2];
88 $this->group = $_group;
89 }
90 if (($first<=$groupinfo[2]) && ($groupinfo[0]!=0)) {
91 $dates = array_map("strtotime",
92 $_nntp->xhdr("Date","$first-$last"));
93 $msgids=$_nntp->xhdr("Message-ID","$first-$last");
94 $subjects = array_map("headerdecode",$_nntp->xhdr("Subject",
95 "$first-$last"));
96 $froms = array_map("headerdecode",$_nntp->xhdr("From",
97 "$first-$last"));
98 $refs = $_nntp->xhdr("References","$first-$last");
99# $this->date=$nntp->date;
100 if ($this->ids) {
101 $this->ids=array_merge($this->ids,array_flip($msgids));
102 } else {
103 $this->ids=array_flip($msgids);
104 }
105
106 foreach ($msgids as $id=>$msgid) {
107 $msg = new spoolhead($dates[$id],$subjects[$id],$froms[$id],
108 (isset($this->overview[$id]->desc)?
109 $this->overview[$id]->desc+1:1));
110 $refs[$id]=str_replace("><","> <",$refs[$id]);
111 $msgrefs=preg_split("/( |\t)/",strtr($refs[$id],$this->ids));
112 $parents=preg_grep("/^\d+$/",$msgrefs);
113 $msg->parent=array_pop($parents);
114 $p = $msg->parent;
115 while ($p) {
116 $this->overview[$p]->desc++;
117 $p = $this->overview[$p]->parent;
118 }
119 if ($msg->parent!="")
120 $this->overview[$msg->parent]->children[]=$id;
121 $this->overview[$id] = $msg;
122 }
123 uasort($this->overview,"spoolcompare");
124 $f = fopen("spool/spool-$_group.dat","w");
125 fputs($f,serialize($this));
126 fclose($f);
127 }
128
129 if ($_since != "") {
130 $newpostsids = $_nntp->newnews($_since,$_group);
131 if (sizeof($newpostsids)) {
132 $newpostsids = array_intersect($newpostsids,
133 array_keys($this->ids));
134 if ($newpostsids && !is_null($newpostsids)) {
135 foreach ($newpostsids as $mid) {
136 $this->overview[$this->ids[$mid]]->isread=false;
137 $this->overview[$this->ids[$mid]]->descunread=1;
138 $parentmid = $this->overview[$this->ids[$mid]]->parent;
139 while (!is_null($parentmid)) {
140 $this->overview[$parentmid]->descunread++;
141 $parentmid = $this->overview[$parentmid]->parent;
142 }
143 }
144 }
145 }
146 if (sizeof($newpostsids)>0) {
147 $flipids = array_flip($this->ids);
148 switch ($_display) {
149 case 1:
150 foreach ($this->overview as $i=>$p) {
151 if (!isset($this->overview[$i]->parent) &&
152 ($this->overview[$i]->descunread==0)) {
153 $this->killdesc($i);
154 }
155 }
156 break;
157 case 2:
158 foreach ($this->overview as $i=>$p) {
159 if ($p->isread) {
160 unset($this->overview[$i]);
161 unset($flipids[$i]);
162 }
163 }
164 $this->ids=array_flip($flipids);
165 break;
166 }
167 }
168 }
169 return true;
170 }
171
172 /** kill post and childrens
173 * @param $_id MSGNUM of post
174 */
175
176 function killdesc($_id) {
177 if (sizeof($this->overview[$_id]->children)) {
178 foreach ($this->overview[$_id]->children as $c) {
179 $this->killdesc($c);
180 }
181 }
182 unset($this->overview[$_id]);
183# $flipid=array_flip($this->ids);
184# unset($flipid[$id]);
185# $this->ids=array_flip($flipid);
186 }
187
188 /** delete a post from overview
189 * @param $_id MSGNUM of post
190 */
191
192 function delid($_id) {
193 if (isset($this->overview[$_id])) {
194 if (sizeof($this->overview[$_id]->parent)) {
195 $this->overview[$this->overview[$_id]->parent]->children =
196 array_diff($this->overview[$this->overview[$_id]->parent]->children,
197 array($_id));
198 if (sizeof($this->overview[$_id]->children)) {
199 $this->overview[$this->overview[$_id]->parent]->children =
200 array_merge($this->overview[$this->overview[$_id]->parent]->children,
201 $this->overview[$_id]->children);
202 foreach ($this->overview[$_id]->children as $c) {
203 $this->overview[$c]->parent = $this->overview[$_id]->parent;
204 }
205 }
206 $p = $this->overview[$_id]->parent;
207 while ($p) {
208 $this->overview[$p]->desc--;
209 $p = $this->overview[$p]->parent;
210 }
211 } elseif (sizeof($this->overview[$_id]->children)) {
212 foreach ($this->overview[$_id]->children as $c) {
213 $this->overview[$c]->parent = null;
214 }
215 }
216 unset($this->overview[$_id]);
217 $ids = array_flip($this->ids);
218 unset($ids[$_id]);
219 $this->ids = array_flip($ids);
220 $f = fopen("spool/spool-{$this->group}.dat","w");
221 fputs($f,serialize($this));
222 fclose($f);
223 }
224 }
225
226 /** displays children tree of a post
227 * @param $_id INTEGER MSGNUM of post
228 * @param $_index INTEGER linear number of post in the tree
229 * @param $_first INTEGER linear number of first post displayed
230 * @param $_last INTEGER linear number of last post displayed
231 * @param $_ref STRING MSGNUM of current post
232 * @param $_pfx_node STRING prefix used for current node
233 * @param $_pfx_end STRING prefix used for children of current node
234 * @param $_head BOOLEAN true if first post in thread
235 */
236
237 function disp_desc($_id,$_index="",$_first=0,$_last=0,$_ref="",
238 $_pfx_node="", $_pfx_end="",$_head=true) {
239 $debug = false;
240 $spfx_f = '<img src="img/k1.gif" height="21" width="9" alt="o" />';
241 $spfx_n = '<img src="img/k2.gif" height="21" width="9" alt="*" />';
242 $spfx_T = '<img src="img/T.gif" height="21" width="12" alt="+" />';
243 $spfx_L = '<img src="img/L.gif" height="21" width="12" alt="`" />';
244 $spfx_s = '<img src="img/s.gif" height="21" width="5" alt="-" />';
245 $spfx_e = '<img src="img/e.gif" height="21" width="12" alt="&nbsp;" />';
246 $spfx_I = '<img src="img/I.gif" height="21" width="12"alt="|" />';
247
248 if ($_index == "") $_index = $this->getndx($_id);
249
250 if (!sizeof($this->overview[$_id]->children) && ($_index<=$_last)
251 && ($_index>=$_first)) {
252 echo '<tr class="'.($_index%2?"pair":"impair")."\">\n";
253 echo "<td class=\"date\">"
254 .formatSpoolHeader("date",$this->overview[$_id]->date,$_id,
255 $this->group,($_index==$_ref),$this->overview[$_id]->isread)
256 ." </td>\n";
257 echo "<td class=\"subject\"><div class=\"tree\">"
258 .$_pfx_node.($_head?$spfx_f:$spfx_s)."</div>"
259 .formatSpoolHeader("subject",$this->overview[$_id]->subject,$_id,
260 $this->group,($_index==$_ref),$this->overview[$_id]->isread)
261 .($debug?" $_id $_index ".
262 $this->overview[$_id]->desc." ".$this->overview[$_id]->descunread." ":"")." </td>\n";
263 echo "<td class=\"author\">"
264 .formatSpoolHeader("from",$this->overview[$_id]->from,$_id,
265 $this->group,($_index==$_ref),$this->overview[$_id]->isread)
266 ." </td>\n</tr>";
267 return true;
268 }
269 $children = $this->overview[$_id]->children;
270 if (($_index<=$_last) && ($_index>=$_first)) {
271 echo '<tr class="'.($_index%2?"pair":"impair")."\">\n";
272 echo "<td class=\"date\">"
273 .formatSpoolHeader("date",$this->overview[$_id]->date,$_id,
274 $this->group,($_index==$_ref),$this->overview[$_id]->isread)
275 ." </td>\n";
276 echo "<td class=\"subject\"><div class=\"tree\">"
277 .$_pfx_node.$spfx_n."</div>"
278 .formatSpoolHeader("subject",$this->overview[$_id]->subject,$_id,
279 $this->group,($_index==$_ref),$this->overview[$_id]->isread)
280 .($debug?" $_id $_index ".
281 $this->overview[$_id]->desc." ".$this->overview[$_id]->descunread." ":"")." </td>\n";
282 echo "<td class=\"author\">"
283 .formatSpoolHeader("from",$this->overview[$_id]->from,$_id,
284 $this->group,($_index==$_ref),$this->overview[$_id]->isread)
285 ." </td>\n</tr>";
286 }
287 $index=$_index+1;
288 while ($child = array_shift($children)) {
289 if (($index+$this->overview[$child]->desc-1>=$_first)
290 ||($index<$_last)){
291 if (sizeof($children)) {
292 $this->disp_desc($child,$index,$_first,$_last,$_ref,
293 $_pfx_end.$spfx_T,$_pfx_end.$spfx_I,false);
294 } else {
295 $this->disp_desc($child,$index,$_first,$_last,$_ref,
296 $_pfx_end.$spfx_L,$_pfx_end.$spfx_e,false);
297 }
298 }
299 $index += $this->overview[$child]->desc;
300 }
301 }
302
303 /** Displays overview
304 * @param $_first INTEGER MSGNUM of first post
305 * @param $_last INTEGER MSGNUM of last post
306 * @param $_ref STRING MSGNUM of current/selectionned post
307 */
308
309 function disp($_first=0,$_last=0,$_ref="") {
310 $index=1;
311 if (sizeof($this->overview)) {
312 foreach ($this->overview as $id=>$msg) {
313 if (!sizeof($msg->parent)) {
314 $this->disp_desc($id,$index,$_first,$_last,$_ref);
315 $index += $msg->desc;
316 }
317 }
318 } else {
319 echo "<tr class=\"pair\">\n";
320 echo "\t<td colspan=\"3\">\n";
321 echo "\t\tNo post in this newsgroup\n";
322 echo "\t</td>\n";
323 echo "</tr>\n";
324 }
325 }
326
327 /** computes linear post index
328 * @param $_id INTEGER MSGNUM of post
329 * @return INTEGER linear index of post
330 */
331
332 function getndx($_id) {
333 $ndx = 1;
334 // on remonte l'arbre
335 $id_parent = $this->overview[$_id]->parent;
336 $id_curr = $_id;
337 while (!is_null($id_parent)) {
338 for ($i=0; $i<array_search($id_curr,
339 $this->overview[$id_parent]->children) ; $i++) {
340 $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc;
341 }
342 $ndx++; //noeud père
343 $id_curr = $id_parent;
344 $id_parent = $this->overview[$id_curr]->parent;
345 }
346 // on compte les threads précédents
347 foreach ($this->overview as $i=>$p) {
348 if ($i==$id_curr) {
349 break;
350 }
351 if (is_null($p->parent)) {
352 $ndx += $this->overview[$i]->desc;
353 }
354 }
355 return $ndx;
356 }
357}
358
359?>