X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=include%2Fspool.inc.php;h=d7579ce119dafb64daaf4c5de23ea5cdd0253863;hb=01681efdcbe6e290915919d1c03ab6e0a44ba21f;hp=2522f04c9594faa10fd24619c240f4ab33453e01;hpb=cced14b680fba9d9b84e06fda822a7b7daf3fad6;p=banana.git
diff --git a/include/spool.inc.php b/include/spool.inc.php
index 2522f04..d7579ce 100644
--- a/include/spool.inc.php
+++ b/include/spool.inc.php
@@ -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
@@ -89,138 +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); }
- $do_save = false;
-
- $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++) {
$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]) {
- $do_save = true;
+ if ($first<$last && $groupinfo[0]) {
+ $do_save = true;
+ $this->_updateSpool($_nntp, "$first-$last");
+ }
- $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");
+ if ($do_save) { $this->_saveToFile(); }
- if (isset($this->ids)) {
- $this->ids = array_merge($this->ids, array_flip($msgids));
- } else {
- $this->ids = array_flip($msgids);
+ $this->_updateUnread($_nntp, $_since, $_display);
+ }
+
+ 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));
+ }
+
+ 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;
- 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;
+ if ($p = $msg->parent) {
+ if (empty($this->overview[$p])) {
+ $this->overview[$p] = new spoolhead($dates[$p], $subjects[$p], $froms[$p], 1);
}
- $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);
- }
- $this->overview[$msg->parent]->children[] = $id;
+ $this->overview[$p]->children[] = $id;
- while ($p) {
- $this->overview[$p]->desc += $msg->desc;
- $p = $this->overview[$p]->parent;
- }
-
+ while ($p) {
+ $this->overview[$p]->desc += $msg->desc;
+ $p = $this->overview[$p]->parent;
}
}
}
+ }
- if ($do_save) { $this->save($spoolfile); }
-
- 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->roots as $i) {
+ foreach ($this->roots as $k=>$i) {
if ($this->overview[$i]->descunread==0) {
$this->killdesc($i);
+ unset($this->roots[$k]);
}
}
break;
}
}
}
-
- return true;
- }
-
- function cmp($a, $b) {
- return $this->overview[$a]->date < $this->overview[$b]->date;
- }
-
- function save($file)
- {
- 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));
}
/** kill post and childrens
@@ -234,11 +228,8 @@ class spool
$this->killdesc($c);
}
}
- $pos = array_search($_id, $this->roots);
- unset($this->roots[$pos]);
unset($this->overview[$_id]);
- $msgid = array_search($_id, $this->ids);
- if ($msgid) {
+ if (($msgid = array_search($_id, $this->ids)) !== false) {
unset($this->ids[$msgid]);
}
}
@@ -277,9 +268,7 @@ class spool
unset($this->ids[$msgid]);
}
- if ($write) {
- $this->save(dirname(dirname(__FILE__)).'/spool');
- }
+ if ($write) { $this->_saveToFile(); }
}
}
@@ -294,7 +283,7 @@ 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;
$spfx_f = '';
$spfx_n = '
';
@@ -315,43 +304,38 @@ class spool
$us = ($_index == $_ref);
$hc = empty($this->overview[$_id]->children);
- echo '