old header were to complicated for nothin. drop class Header, and treat it from Post
[banana.git] / include / spool.inc.php
index c19104c..d7579ce 100644 (file)
@@ -16,10 +16,10 @@ if(!function_exists('_file_put_contents')) {
         }
         fputs($fp, $data);
         fclose($fp);
-
     }
 }
 
+function spoolcompare($a,$b) { return ($b->date>=$a->date); }
 
 /** Class spoolhead
  *  class used in thread overviews
@@ -80,6 +80,8 @@ class spool
     var $group;
     /**  array msgid => msgnum */
     var $ids;
+    /** thread starts */
+    var $roots;
 
     /** constructor
      * @param $_nntp RESOURCE NNTP socket filehandle
@@ -87,126 +89,132 @@ class spool
      * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
      * @param $_since INTEGER time stamp (used for read/unread)
      */
-
     function spool(&$_nntp, $_group, $_display=0, $_since="")
     {
         global $news;
+        $this->group = $_group;
+        $groupinfo   = $_nntp->group($_group);
+        if (!$groupinfo) { return ($this = null); }
 
-        $spool_path = dirname(dirname(__FILE__)).'/spool';
-        $spoolfile  = "$spool_path/spool-$_group.dat";
-
-        $groupinfo  = $_nntp->group($_group);
-        $first      = max($groupinfo[2]-$news['maxspool'], $groupinfo[1]);
-        $last       = $groupinfo[2];
-
-        if (!$groupinfo) {
-            $this = null;
-            return false;
-        }
-        if (file_exists($spoolfile)) {
-            $this   = unserialize(file_get_contents($spoolfile));
-        }
+        $this->_readFromFile();
 
+        $do_save = false;
+        $first   = max($groupinfo[2]-$news['maxspool'], $groupinfo[1]);
+        $last    = $groupinfo[2];
         if ($this->version == BANANA_SPOOL_VERSION) {
-            $keys   = array_values($this->ids);
-            rsort($keys);
-            // remove expired messages
-            for ($id=min(array_keys($this->overview)); $id<$first; $id++) { 
+            for ($id = min(array_keys($this->overview)); $id<$first; $id++) { 
                 $this->delid($id, false);
+                $do_save = true;
             }
-            $start  = max(array_keys($this->overview))+1;
+            $first = max(array_keys($this->overview))+1;
         } else {
             unset($this->overview, $this->ids);
-            $this->group   = $_group;
             $this->version = BANANA_SPOOL_VERSION;
-            $start         = $first;
         }
 
-        if (($start<$last) && $groupinfo[0]) {
+        if ($first<$last && $groupinfo[0]) {
+            $do_save = true;
+            $this->_updateSpool($_nntp, "$first-$last");
+        }
+
+        if ($do_save) { $this->_saveToFile(); }
 
-            $dates    = array_map("strtotime",    $_nntp->xhdr("Date",    "$start-$last"));
-            $subjects = array_map("headerdecode", $_nntp->xhdr("Subject", "$start-$last"));
-            $froms    = array_map("headerdecode", $_nntp->xhdr("From",    "$start-$last"));
-            $msgids   = $_nntp->xhdr("Message-ID", "$start-$last");
-            $refs     = $_nntp->xhdr("References", "$start-$last");
+        $this->_updateUnread($_nntp, $_since, $_display);
+    }
 
-            if (isset($this->ids)) {
-                $this->ids = array_merge($this->ids, array_flip($msgids));
-            } else {
-                $this->ids = array_flip($msgids);
+    function _readFromFile()
+    {
+        $file = dirname(dirname(__FILE__))."/spool/spool-{$this->group}.dat";
+        if (file_exists($file)) {
+            $this = unserialize(file_get_contents($file));
+        }
+    }
+
+    function _saveToFile()
+    {
+        $file = dirname(dirname(__FILE__))."/spool/spool-{$this->group}.dat";
+        uasort($this->overview, "spoolcompare");
+
+        $this->roots = Array();
+        foreach($this->overview as $id=>$msg) {
+            if (is_null($msg->parent)) {
+                $this->roots[] = $id;
             }
+        }
+        
+        file_put_contents($file, serialize($this));
+    }
 
-            foreach ($msgids as $id=>$msgid) {
-                $msg                = new spoolhead($dates[$id], $subjects[$id], $froms[$id], 1);
-                $refs[$id]          = str_replace("><", "> <", $refs[$id]);
-                $msgrefs            = preg_split("/( |\t)/", strtr($refs[$id], $this->ids));
-                $parents            = preg_grep("/^\d+$/", $msgrefs);
-                $msg->parent        = array_pop($parents);
-                $msg->parent_direct = preg_match("/^\d+$/", array_pop($msgrefs));
-            
-                $p = $msg->parent;
-                while ($p) {
-                    if (isset($this->overview[$p])) {
-                        $this->overview[$p]->desc++;
-                        $p = $this->overview[$p]->parent;
-                    } else {
-                        $this->overview[$p] = new spoolhead($dates[$p], $subjects[$p], $froms[$p], 1);
-                        break;
-                    }
+    function _updateSpool(&$_nntp, $arg)
+    {
+        $dates    = array_map(strtotime,    $_nntp->xhdr("Date",    $arg));
+        $subjects = array_map(headerdecode, $_nntp->xhdr("Subject", $arg));
+        $froms    = array_map(headerdecode, $_nntp->xhdr("From",    $arg));
+        $msgids   = $_nntp->xhdr("Message-ID", $arg);
+        $refs     = $_nntp->xhdr("References", $arg);
+
+        if (is_array($this->ids)) {
+            $this->ids = array_merge($this->ids, array_flip($msgids));
+        } else {
+            $this->ids = array_flip($msgids);
+        }
+
+        foreach ($msgids as $id=>$msgid) {
+            $msg                = new spoolhead($dates[$id], $subjects[$id], $froms[$id]);
+            $refs[$id]          = str_replace('><', '> <', $refs[$id]);
+            $msgrefs            = preg_split("/[ \t]/", strtr($refs[$id], $this->ids));
+            $parents            = preg_grep('/^\d+$/', $msgrefs);
+            $msg->parent        = array_pop($parents);
+            $msg->parent_direct = preg_match('/^\d+$/', array_pop($msgrefs));
+
+            if (isset($this->overview[$id])) {
+                $msg->desc     = $this->overview[$id]->desc;
+                $msg->children = $this->overview[$id]->children;
+            }
+            $this->overview[$id] = $msg;
+
+            if ($p = $msg->parent) {
+                if (empty($this->overview[$p])) {
+                    $this->overview[$p] = new spoolhead($dates[$p], $subjects[$p], $froms[$p], 1);
                 }
-                if ($msg->parent) {
-                    $this->overview[$msg->parent]->children[] = $id;
+                $this->overview[$p]->children[] = $id;
+
+                while ($p) {
+                    $this->overview[$p]->desc += $msg->desc;
+                    $p = $this->overview[$p]->parent;
                 }
-                $this->overview[$id] = $msg;
             }
-            uasort($this->overview, "spoolcompare");
-            file_put_contents($spoolfile, serialize($this));
         }
+    }
 
-        if ($_since) {
-            $newpostsids = $_nntp->newnews($_since, $_group);
-            if (sizeof($newpostsids)) {
-                $newpostsids = array_intersect($newpostsids, array_keys($this->ids));
-                if ($newpostsids && !is_null($newpostsids)) {
-                    foreach ($newpostsids as $mid) {
-                        $this->overview[$this->ids[$mid]]->isread     = false;
-                        $this->overview[$this->ids[$mid]]->descunread = 1;
-                        $parentmid = $this->ids[$mid];
-                        while (isset($parentmid)) {
-                            $this->overview[$parentmid]->descunread ++;
-                            $parentmid = $this->overview[$parentmid]->parent;
-                        }
-                    }
+    function _updateUnread(&$nntp, $since, $mode) {
+        if (empty($since)) { return; }
+
+        if (is_array($newpostsids = $nntp->newnews($since, $this->group))) {
+            $newpostsids = array_intersect($newpostsids, array_keys($this->ids));
+            foreach ($newpostsids as $mid) {
+                $this->overview[$this->ids[$mid]]->isread     = false;
+                $this->overview[$this->ids[$mid]]->descunread = 1;
+                $parentmid = $this->ids[$mid];
+                while (isset($parentmid)) {
+                    $this->overview[$parentmid]->descunread ++;
+                    $parentmid = $this->overview[$parentmid]->parent;
                 }
             }
-            if (sizeof($newpostsids)>0) {
-                switch ($_display) {
+
+            if (count($newpostsids)) {
+                switch ($mode) {
                     case 1:
-                        foreach ($this->overview as $i=>$p) {
-                            if (isset($this->overview[$i]) &&
-                                    !isset($this->overview[$i]->parent) && 
-                                    ($this->overview[$i]->descunread==0))
-                            {
+                        foreach ($this->roots as $k=>$i) {
+                            if ($this->overview[$i]->descunread==0) {
                                 $this->killdesc($i);
+                                unset($this->roots[$k]);
                             }
                         }
                         break;
-
-                    case 2:
-                        $flipids = array_flip($this->ids);
-                        foreach ($this->overview as $i=>$p) {
-                            if ($p->isread) {
-                                unset($this->overview[$i]);
-                                unset($flipids[$i]);
-                            }
-                        }
-                        $this->ids = array_flip($flipids);
-                        break;
                 }
             }
         }
-        
-        return true;
     }
 
     /** kill post and childrens
@@ -221,8 +229,7 @@ class spool
             }
         }
         unset($this->overview[$_id]);
-        $msgid = array_search($_id, $this->ids);
-        if ($msgids) {
+        if (($msgid = array_search($_id, $this->ids)) !== false) {
             unset($this->ids[$msgid]);
         }
     }
@@ -261,10 +268,7 @@ class spool
                 unset($this->ids[$msgid]);
             }
             
-            if ($write) {
-                $spool_path = dirname(dirname(__FILE__)).'/spool';
-                file_put_contents("$spool_path/spool-$_group.dat", serialize($this));
-            }
+            if ($write) { $this->_saveToFile(); }
         }
     }
 
@@ -279,9 +283,8 @@ class spool
      * @param $_head BOOLEAN true if first post in thread
      */
 
-    function disp_desc($_id, $_index="", $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true) {
+    function _disp_desc($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true) {
         global $css;
-        $debug    = false;
         $spfx_f   = '<img src="img/k1.gif" height="21" width="9" alt="o" />'; 
         $spfx_n   = '<img src="img/k2.gif" height="21" width="9" alt="*" />'; 
         $spfx_Tnd = '<img src="img/T-direct.gif" height="21" width="12" alt="+" />';
@@ -293,63 +296,46 @@ class spool
         $spfx_e   = '<img src="img/e.gif" height="21" width="12" alt="&nbsp;" />';
         $spfx_I   = '<img src="img/I.gif" height="21" width="12"alt="|" />';
 
-        if ($_index == "") {
-            $_index = $this->getndx($_id);
+        if ($_index + $this->overview[$_id]->desc < $_first || $_index > $_last) {
+            return;
         }
 
-        if (!sizeof($this->overview[$_id]->children) && ($_index<=$_last) && ($_index>=$_first)) {
-            echo '<tr class="'.($_index%2?$css["pair"]:$css["impair"])."\">\n";
-            echo "<td class=\"{$css['date']}\">"
-                .formatSpoolHeader("date", $this->overview[$_id]->date, $_id,
-                        $this->group, ($_index==$_ref), $this->overview[$_id]->isread)
-                ." </td>\n";
-            echo "<td class=\"{$css['subject']}\"><div class=\"{$css['tree']}\">"
-                .$_pfx_node.($_head?$spfx_f:
-                        ($this->overview[$_id]->parent_direct?$spfx_s:$spfx_snd))
-                ."</div>"
-                .formatSpoolHeader("subject", $this->overview[$_id]->subject, $_id,
-                        $this->group, ($_index==$_ref), $this->overview[$_id]->isread)
-                .($debug?" $_id $_index ".
-                        $this->overview[$_id]->desc." ".$this->overview[$_id]->descunread." ":"")." </td>\n";
-            echo "<td class=\"{$css['author']}\">"
-                .formatSpoolHeader("from", $this->overview[$_id]->from, $_id,
-                        $this->group, ($_index==$_ref), $this->overview[$_id]->isread)
-                ." </td>\n</tr>";
-            return true;
+        if ($_index>=$_first) {
+            $us = ($_index == $_ref);
+            $hc = empty($this->overview[$_id]->children);
+
+            echo '<tr class="'.($_index%2?$css["pair"]:$css["impair"]).($this->overview[$_id]->isread?'':' new')."\">\n";
+            echo "<td class=\"{$css['date']}\">".fancyDate($this->overview[$_id]->date)." </td>\n";
+            echo "<td class=\"{$css['subject']}\">"
+                ."<div class=\"{$css['tree']}\">$_pfx_node".($hc?($_head?$spfx_f:($this->overview[$_id]->parent_direct?$spfx_s:$spfx_snd)):$spfx_n)
+                ."</div>";
+            if ($_index == $_ref) {
+                echo '<span class="isref">'.htmlentities($this->overview[$_id]->subject).'</span>';
+            } else {
+                echo "<a href='article.php?group={$this->group}&amp;id=$_id'>".htmlentities($this->overview[$_id]->subject).'</a>';
+            }
+            echo "</td>\n<td class=\"{$css['author']}\">".formatFrom($this->overview[$_id]->from)."</td>\n</tr>";
+
+            if ($hc) { return; }
         } 
+
+        $_index ++;
+
         $children = $this->overview[$_id]->children;
-        if (($_index<=$_last) && ($_index>=$_first)) {
-            echo '<tr class="'.($_index%2?$css["pair"]:$css["impair"])."\">\n";
-            echo "<td class=\"{$css['date']}\">"
-                .formatSpoolHeader("date", $this->overview[$_id]->date, $_id,
-                        $this->group, ($_index==$_ref), $this->overview[$_id]->isread)
-                ." </td>\n";
-            echo "<td class=\"{$css['subject']}\"><div class=\"{$css['tree']}\">"
-                .$_pfx_node.$spfx_n."</div>"
-                .formatSpoolHeader("subject", $this->overview[$_id]->subject, $_id,
-                        $this->group, ($_index==$_ref), $this->overview[$_id]->isread)
-                .($debug?" $_id $_index ".
-                        $this->overview[$_id]->desc." ".$this->overview[$_id]->descunread." ":"")." </td>\n";
-            echo "<td class=\"{$css['author']}\">"
-                .formatSpoolHeader("from", $this->overview[$_id]->from, $_id,
-                        $this->group, ($_index==$_ref), $this->overview[$_id]->isread)
-                ." </td>\n</tr>";
-        }
-        $index=$_index+1;
         while ($child = array_shift($children)) {
-            if (($index+$this->overview[$child]->desc-1>=$_first)
-                    ||($index<$_last)){
+            if ($_index > $_last) { return; }
+            if ($_index+$this->overview[$child]->desc >= $_first) {
                 if (sizeof($children)) {
-                    $this->disp_desc($child, $index, $_first, $_last, $_ref, $_pfx_end.
-                            ($this->overview[$child]->parent_direct?$spfx_T:$spfx_Tnd),
+                    $this->_disp_desc($child, $_index, $_first, $_last, $_ref,
+                            $_pfx_end.($this->overview[$child]->parent_direct?$spfx_T:$spfx_Tnd),
                             $_pfx_end.$spfx_I, false);
                 } else {
-                    $this->disp_desc($child, $index, $_first, $_last, $_ref, $_pfx_end.
-                            ($this->overview[$child]->parent_direct?$spfx_L:$spfx_Lnd),
+                    $this->_disp_desc($child, $_index, $_first, $_last, $_ref,
+                            $_pfx_end.($this->overview[$child]->parent_direct?$spfx_L:$spfx_Lnd),
                             $_pfx_end.$spfx_e, false);
                 }
             }
-            $index += $this->overview[$child]->desc;
+            $_index += $this->overview[$child]->desc;
         }
     }
 
@@ -363,11 +349,10 @@ class spool
         global $css;
         $index = 1;
         if (sizeof($this->overview)) {
-            foreach ($this->overview as $id=>$msg) {
-                if (!isset($msg->parent)) {
-                    $this->disp_desc($id, $index, $_first, $_last, $_ref);
-                    $index += $msg->desc;
-                }
+            foreach ($this->roots as $id) {
+                $this->_disp_desc($id, $index, $_first, $_last, $_ref);
+                $index += $this->overview[$id]->desc ;
+                if ($index > $_last) { break; }
             }
         } else {
             echo "<tr class=\"{$css['pair']}\">\n";
@@ -384,26 +369,26 @@ class spool
      */
 
     function getndx($_id) {
-        $ndx = 1;
-        // on remonte l'arbre
-        $id_parent = $this->overview[$_id]->parent;
-        $id_curr   = $_id;
-        while (!is_null($id_parent)) {
-            for ($i=0; $i<array_search($id_curr, $this->overview[$id_parent]->children) ; $i++) {
+        $ndx    = 1;
+        $id_cur = $_id;
+        while (true) {
+            $id_parent = $this->overview[$id_cur]->parent;
+            if (is_null($id_parent)) break;
+            $pos       = array_search($id_cur, $this->overview[$id_parent]->children);
+        
+            for ($i = 0; $i < $pos ; $i++) {
                 $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc;
             }
             $ndx++; //noeud père
-            $id_curr   = $id_parent;
-            $id_parent = $this->overview[$id_curr]->parent;
+
+            $id_cur = $id_parent;
         }
-        // on compte les threads précédents
-        foreach ($this->overview as $i=>$p) {
-            if ($i==$id_curr) {
+
+        foreach ($this->roots as $i) {
+            if ($i==$id_cur) {
                 break;
             }
-            if (is_null($p->parent)) {
-                $ndx += $this->overview[$i]->desc;
-            }
+            $ndx += $this->overview[$i]->desc;
         }
         return $ndx;
     }