From 367f43654cc05089f9cbaa93175f2d764e848ac9 Mon Sep 17 00:00:00 2001 From: wainstead Date: Wed, 31 Jan 2001 03:11:25 +0000 Subject: [PATCH] Going back to version 1.5, this time with the signature Jan Hidder used for InsertPage() that I removed, thus causing a bug. It feels slower now though. git-svn-id: svn://svn.code.sf.net/p/phpwiki/code/trunk@379 96ab9672-09ca-45d6-a79d-3d69d39ca109 --- lib/dbmlib.php | 296 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 261 insertions(+), 35 deletions(-) diff --git a/lib/dbmlib.php b/lib/dbmlib.php index d3773c4c9..5ea020eaa 100644 --- a/lib/dbmlib.php +++ b/lib/dbmlib.php @@ -1,28 +1,24 @@ \n"; $count = 1; dbminsert($dbi['hitcount'], $pagename, $count); } } + function GetHitCount($dbi, $pagename) { if (dbmexists($dbi['hitcount'], $pagename)) { @@ -206,40 +243,66 @@ // sort the results highest to lowest, and return // n..$limit results + // Because sorting all the pages may be a lot of work + // we only get the top $limit. A page is only added if it's score is + // higher than the lowest score in the list. If the list is full then + // one of the pages with the lowest scores is removed. + $pagename = dbmfirstkey($dbi['hitcount']); - $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename); + $score = dbmfetch($dbi['hitcount'], $pagename); + $res = array($pagename => (int) $score); + $lowest = $score; while ($pagename = dbmnextkey($dbi['hitcount'], $pagename)) { - $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename); - //echo "got $pagename with value " . $res[$pagename] . "
\n"; + $score = dbmfetch($dbi['hitcount'], $pagename); + if (count($res) < $limit) { // room left in $res? + if ($score < $lowest) + $lowest = $score; + $res[$pagename] = (int) $score; // add page to $res + } elseif ($score > $lowest) { + $oldres = $res; // save old result + $res = array(); + $removed = 0; // nothing removed yet + $newlowest = $score; // new lowest score + $res[$pagename] = (int) $score; // add page to $res + reset($oldres); + while(list($pname, $pscore) = each($oldres)) { + if (!$removed and ($pscore = $lowest)) + $removed = 1; // don't copy this entry + else { + $res[$pname] = (int) $pscore; + if ($pscore < $newlowest) + $newlowest = $pscore; + } + } + $lowest = $newlowest; + } } - - arsort($res); + + arsort($res); // sort + reset($res); + return($res); } + function MostPopularNextMatch($dbi, &$res) { // the return result is a two element array with 'hits' // and 'pagename' as the keys - if (count($res) == 0) - return 0; - if (list($pagename, $hits) = each($res)) { - //echo "most popular next match called
\n"; - //echo "got $pagename, $hits back
\n"; $nextpage = array( "hits" => $hits, "pagename" => $pagename ); - // $dbm_mostpopular_cntr++; return $nextpage; } else { return 0; } } + function GetAllWikiPagenames($dbi) { $namelist = array(); $ctr = 0; @@ -254,4 +317,167 @@ return $namelist; } + + //////////////////////////////////////////// + // functionality for the wikilinks DBM file + + // format of the 'wikilinks' DBM file : + // pagename => + // { tolinks => ( pagename => 1}, fromlinks => { pagename => 1 } } + + // takes a page name, returns array of scored incoming and outgoing links + function GetWikiPageLinks($dbi, $pagename) { + + $linkinfo = RetrievePage($dbi, $pagename, 'wikilinks'); + if (is_array($linkinfo)) { // page exists? + $tolinks = $linkinfo['tolinks']; // outgoing links + $fromlinks = $linkinfo['fromlinks']; // incoming links + } else { // new page, but pages may already point to it + // create info for page + $tolinks = array(); + $fromlinks = array(); + // look up pages that link to $pagename + $pname = dbmfirstkey($dbi['wikilinks']); + while ($pname) { + $linkinfo = RetrievePage($dbi, $pname, 'wikilinks'); + if ($linkinfo['tolinks'][$pagename]) // $pname links to $pagename? + $fromlinks[$pname] = 1; + $pname = dbmnextkey($dbi['wikilinks'], $pname); + } + } + + // get and sort the outgoing links + $outlinks = array(); + reset($tolinks); // look up scores for tolinks + while(list($tolink, $dummy) = each($tolinks)) { + $toPage = RetrievePage($dbi, $tolink, 'wikilinks'); + if (is_array($toPage)) // link to internal page? + $outlinks[$tolink] = count($toPage['fromlinks']); + } + arsort($outlinks); // sort on score + $links['out'] = array(); + reset($outlinks); // convert to right format + while(list($link, $score) = each($outlinks)) + $links['out'][] = array($link, $score); + + // get and sort the incoming links + $inlinks = array(); + reset($fromlinks); // look up scores for fromlinks + while(list($fromlink, $dummy) = each($fromlinks)) { + $fromPage = RetrievePage($dbi, $fromlink, 'wikilinks'); + $inlinks[$fromlink] = count($fromPage['fromlinks']); + } + arsort($inlinks); // sort on score + $links['in'] = array(); + reset($inlinks); // convert to right format + while(list($link, $score) = each($inlinks)) + $links['in'][] = array($link, $score); + + // sort all the incoming and outgoing links + $allLinks = $outlinks; // copy the outlinks + reset($inlinks); // add the inlinks + while(list($key, $value) = each($inlinks)) + $allLinks[$key] = $value; + reset($allLinks); // lookup hits + while(list($key, $value) = each($allLinks)) + $allLinks[$key] = (int) dbmfetch($dbi['hitcount'], $key); + arsort($allLinks); // sort on hits + $links['popular'] = array(); + reset($allLinks); // convert to right format + while(list($link, $hits) = each($allLinks)) + $links['popular'][] = array($link, $hits); + + return $links; + } + + + // takes page name, list of links it contains + // the $linklist is an array where the keys are the page names + function SetWikiPageLinks($dbi, $pagename, $linklist) { + + $cache = array(); + + // Phase 1: fetch the relevant pairs from 'wikilinks' into $cache + // --------------------------------------------------------------- + + // first the info for $pagename + $linkinfo = RetrievePage($dbi, $pagename, 'wikilinks'); + if (is_array($linkinfo)) // page exists? + $cache[$pagename] = $linkinfo; + else { + // create info for page + $cache[$pagename] = array( 'fromlinks' => array(), + 'tolinks' => array() + ); + // look up pages that link to $pagename + $pname = dbmfirstkey($dbi['wikilinks']); + while ($pname) { + $linkinfo = RetrievePage($dbi, $pname, 'wikilinks'); + if ($linkinfo['tolinks'][$pagename]) + $cache[$pagename]['fromlinks'][$pname] = 1; + $pname = dbmnextkey($dbi['wikilinks'], $pname); + } + } + + // then the info for the pages that $pagename used to point to + $oldTolinks = $cache[$pagename]['tolinks']; + reset($oldTolinks); + while (list($link, $dummy) = each($oldTolinks)) { + $linkinfo = RetrievePage($dbi, $link, 'wikilinks'); + if (is_array($linkinfo)) + $cache[$link] = $linkinfo; + } + + // finally the info for the pages that $pagename will point to + reset($linklist); + while (list($link, $dummy) = each($linklist)) { + $linkinfo = RetrievePage($dbi, $link, 'wikilinks'); + if (is_array($linkinfo)) + $cache[$link] = $linkinfo; + } + + // Phase 2: delete the old links + // --------------------------------------------------------------- + + // delete the old tolinks for $pagename + // $cache[$pagename]['tolinks'] = array(); + // (overwritten anyway in Phase 3) + + // remove $pagename from the fromlinks of pages in $oldTolinks + + reset($oldTolinks); + while (list($oldTolink, $dummy) = each($oldTolinks)) { + if ($cache[$oldTolink]) { // links to existing page? + $oldFromlinks = $cache[$oldTolink]['fromlinks']; + $cache[$oldTolink]['fromlinks'] = array(); // erase fromlinks + reset($oldFromlinks); // comp. new fr.links + while (list($fromlink, $dummy) = each($oldFromlinks)) { + if ($fromlink != $pagename) + $cache[$oldTolink]['fromlinks'][$fromlink] = 1; + } + } + } + + // Phase 3: add the new links + // --------------------------------------------------------------- + + // set the new tolinks for $pagename + $cache[$pagename]['tolinks'] = $linklist; + + // add $pagename to the fromlinks of pages in $linklist + reset($linklist); + while (list($link, $dummy) = each($linklist)) { + if ($cache[$link]) // existing page? + $cache[$link]['fromlinks'][$pagename] = 1; + } + + // Phase 4: write $cache back to 'wikilinks' + // --------------------------------------------------------------- + + reset($cache); + while (list($link,$fromAndTolinks) = each($cache)) + InsertPage($dbi, $link, $fromAndTolinks, 'wikilinks'); + + } + ?> -- 2.45.0