From cced14b680fba9d9b84e06fda822a7b7daf3fad6 Mon Sep 17 00:00:00 2001 From: x2000habouzit Date: Sun, 2 Jan 2005 03:00:25 +0000 Subject: [PATCH] whole bunch of changes : - reorganize includes - a lot of speedups (I think we gain between 30 and 60% on the time used to create a page for threads and article) _ some code simplifications --- article.php | 3 +- include/encoding.inc.php | 24 ----- include/{wrapper.inc.php => misc.inc.php} | 14 ++- include/spool.inc.php | 142 +++++++++++++++--------------- locales/fr.inc.php | 2 +- post.php | 3 +- thread.php | 3 +- xface.php | 5 +- 8 files changed, 90 insertions(+), 106 deletions(-) delete mode 100644 include/encoding.inc.php rename include/{wrapper.inc.php => misc.inc.php} (62%) diff --git a/article.php b/article.php index 79d058e..58d9a60 100644 --- a/article.php +++ b/article.php @@ -8,8 +8,7 @@ ********************************************************************************/ require_once("include/session.inc.php"); -require_once("include/encoding.inc.php"); -require_once("include/wrapper.inc.php"); +require_once("include/misc.inc.php"); require_once("include/format.inc.php"); require_once("include/config.inc.php"); require_once("include/NetNNTP.inc.php"); diff --git a/include/encoding.inc.php b/include/encoding.inc.php deleted file mode 100644 index 9d30000..0000000 --- a/include/encoding.inc.php +++ /dev/null @@ -1,24 +0,0 @@ - diff --git a/include/wrapper.inc.php b/include/misc.inc.php similarity index 62% rename from include/wrapper.inc.php rename to include/misc.inc.php index 7f657d9..9731d16 100644 --- a/include/wrapper.inc.php +++ b/include/misc.inc.php @@ -7,7 +7,8 @@ * Copyright: See COPYING files that comes with this distribution ********************************************************************************/ -function wrap($_text,$_prefix="",$_length=72) { +function wrap($_text, $_prefix="", $_length=72) +{ $parts = preg_split("/\n-- ?\n/", $_text); if (count($parts) >1) { $sign = "\n-- \n" . array_pop($parts); @@ -23,4 +24,15 @@ function wrap($_text,$_prefix="",$_length=72) { return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign); } +function _headerdecode($charset, $c, $str) { + $s = ($c == 'Q') ? quoted_printable_decode($str) : base64_decode($str); + $s = iconv($charset, 'iso-8859-15', $s); + return str_replace('_', ' ', $s); +} + +function headerDecode($value) { + $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value); + return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val); +} + ?> diff --git a/include/spool.inc.php b/include/spool.inc.php index b0a73c6..2522f04 100644 --- a/include/spool.inc.php +++ b/include/spool.inc.php @@ -80,6 +80,8 @@ class spool var $group; /** array msgid => msgnum */ var $ids; + /** thread starts */ + var $roots; /** constructor * @param $_nntp RESOURCE NNTP socket filehandle @@ -92,6 +94,8 @@ class spool { global $news; + $do_save = false; + $spool_path = dirname(dirname(__FILE__)).'/spool'; $spoolfile = "$spool_path/spool-$_group.dat"; @@ -111,8 +115,9 @@ class spool $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; } else { @@ -123,6 +128,7 @@ class spool } if (($start<$last) && $groupinfo[0]) { + $do_save = true; $dates = array_map("strtotime", $_nntp->xhdr("Date", "$start-$last")); $subjects = array_map("headerdecode", $_nntp->xhdr("Subject", "$start-$last")); @@ -163,10 +169,10 @@ class spool } } - uasort($this->overview, "spoolcompare"); - file_put_contents($spoolfile, serialize($this)); } + if ($do_save) { $this->save($spoolfile); } + if ($_since) { $newpostsids = $_nntp->newnews($_since, $_group); if (sizeof($newpostsids)) { @@ -186,26 +192,12 @@ class spool if (sizeof($newpostsids)>0) { switch ($_display) { 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 $i) { + if ($this->overview[$i]->descunread==0) { $this->killdesc($i); } } 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; } } } @@ -213,6 +205,24 @@ class spool 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 * @param $_id MSGNUM of post */ @@ -224,9 +234,11 @@ 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 ($msgids) { + if ($msgid) { unset($this->ids[$msgid]); } } @@ -266,8 +278,7 @@ class spool } if ($write) { - $spool_path = dirname(dirname(__FILE__)).'/spool'; - file_put_contents("$spool_path/spool-$_group.dat", serialize($this)); + $this->save(dirname(dirname(__FILE__)).'/spool'); } } } @@ -283,9 +294,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 = 'o'; $spfx_n = '*'; $spfx_Tnd = '+'; @@ -297,52 +307,40 @@ class spool $spfx_e = ' '; $spfx_I = '|'; - 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)) { + if ($_index>=$_first) { + $us = ($_index == $_ref); + $hc = empty($this->overview[$_id]->children); + echo '\n"; echo "" .formatSpoolHeader("date", $this->overview[$_id]->date, $_id, - $this->group, ($_index==$_ref), $this->overview[$_id]->isread) + $this->group, $us, $this->overview[$_id]->isread) ." \n"; - echo "
" - .$_pfx_node.($_head?$spfx_f: - ($this->overview[$_id]->parent_direct?$spfx_s:$spfx_snd)) + echo "
$_pfx_node" + .($hc?($_head?$spfx_f:($this->overview[$_id]->parent_direct?$spfx_s:$spfx_snd)):$spfx_n) ."
" .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." ":"")." \n"; - echo "" - .formatSpoolHeader("from", $this->overview[$_id]->from, $_id, - $this->group, ($_index==$_ref), $this->overview[$_id]->isread) - ." \n"; - return true; - } - $children = $this->overview[$_id]->children; - if (($_index<=$_last) && ($_index>=$_first)) { - echo '\n"; - echo "" - .formatSpoolHeader("date", $this->overview[$_id]->date, $_id, - $this->group, ($_index==$_ref), $this->overview[$_id]->isread) + $this->group, $us, $this->overview[$_id]->isread) ." \n"; - echo "
" - .$_pfx_node.$spfx_n."
" - .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." ":"")." \n"; echo "" .formatSpoolHeader("from", $this->overview[$_id]->from, $_id, - $this->group, ($_index==$_ref), $this->overview[$_id]->isread) + $this->group, $us, $this->overview[$_id]->isread) ." \n"; - } + if ($hc) { + return true; + } + } + $index = $_index+1; + + $children = $this->overview[$_id]->children; 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), @@ -367,10 +365,11 @@ class spool global $css; $index = 1; if (sizeof($this->overview)) { - foreach ($this->overview as $id=>$msg) { - if (is_null($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 { @@ -388,27 +387,26 @@ class spool */ function getndx($_id) { - $ndx = 1; - // on remonte l'arbre + $ndx = 1; + $id_cur = $_id; while (true) { - $id_parent = $this->overview[$_id]->parent; - $id_curr = $_id; + $id_parent = $this->overview[$id_cur]->parent; if (is_null($id_parent)) break; - $pos = array_search($id_curr, $this->overview[$id_parent]->children); + $pos = array_search($id_cur, $this->overview[$id_parent]->children); - for ($i = 0; $i < $_pos ; $i++) { + for ($i = 0; $i < $pos ; $i++) { $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc; } $ndx++; //noeud père + + $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; } diff --git a/locales/fr.inc.php b/locales/fr.inc.php index 2482d6d..3b71209 100644 --- a/locales/fr.inc.php +++ b/locales/fr.inc.php @@ -1,7 +1,7 @@ -- 2.1.4