From ab7c8077646d4929babbd9ecf949d4ae77d97f6b Mon Sep 17 00:00:00 2001 From: wainstead Date: Fri, 19 Jan 2001 22:20:30 +0000 Subject: [PATCH] Joel Uckelman contributed code changes including a new database library file 'dbalib.php' that uses the new interface library for DBM files. The changes in config.php still default to the dbmlib.php library for now, and the user must set 'dba' in config.php if they are using PHP 4.0.4 or later. Tested this (just barely) on a newly built PHP 4.0.4p1 on my RH6.2 box. git-svn-id: svn://svn.code.sf.net/p/phpwiki/code/trunk@371 96ab9672-09ca-45d6-a79d-3d69d39ca109 --- lib/config.php | 35 +++++-- lib/dbalib.php | 260 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 286 insertions(+), 9 deletions(-) create mode 100644 lib/dbalib.php diff --git a/lib/config.php b/lib/config.php index 280851dac..7b5528b1e 100644 --- a/lib/config.php +++ b/lib/config.php @@ -1,16 +1,21 @@ \n"; }; } - rcs_id('$Id: config.php,v 1.19 2001-01-04 18:32:43 ahollosi Exp $'); + rcs_id('$Id: config.php,v 1.20 2001-01-19 22:20:30 wainstead Exp $'); // end essential internal stuff ///////////////////////////////////////////////////////////////////// + // Part One: // Constants and settings. Edit the values below for your site. ///////////////////////////////////////////////////////////////////// @@ -28,13 +33,17 @@ $LANG="C"; ///////////////////////////////////////////////////////////////////// + // Part Two: // Database section - // set your database here and edit the according section below - $WhichDatabase = 'dbm'; // use one of "dbm", "mysql", "pgsql", "msql", - // or "file" + // set your database here and edit the according section below. + // For PHP 4.0.4 and later you must use "dba" if you are using + // DBM files for storage. "dbm" uses the older deprecated interface. + + $WhichDatabase = 'dbm'; // use one of "dbm", "dba", "mysql", + // "pgsql", "msql", or "file" - // DBM settings (default) - if ($WhichDatabase == 'dbm') { + // DBM (default) and DBA settings + if ($WhichDatabase == 'dbm' or 'dba') { $DBMdir = "/tmp"; $WikiPageStore = "wiki"; $ArchivePageStore = "archive"; @@ -45,7 +54,11 @@ $WikiDB['hitcount'] = "$DBMdir/wikihitcountdb"; // try this many times if the dbm is unavailable define("MAX_DBM_ATTEMPTS", 20); - include "lib/dbmlib.php"; + if ($WhichDatabase == 'dbm') { + include "lib/dbmlib.php"; + } else { + include "lib/dbalib.php"; + } // MySQL settings -- see INSTALL.mysql for details on using MySQL } elseif ($WhichDatabase == 'mysql') { @@ -101,7 +114,9 @@ ///////////////////////////////////////////////////////////////////// - // Miscellanious + // Part Three: + // Miscellaneous + ///////////////////////////////////////////////////////////////////// // logo image (path relative to index.php) $logo = "images/wikibase.png"; @@ -137,7 +152,9 @@ ///////////////////////////////////////////////////////////////////// + // Part Four: // Original pages and layout + ///////////////////////////////////////////////////////////////////// // need to define localization function first -- skip this if (!function_exists ('gettext')) { diff --git a/lib/dbalib.php b/lib/dbalib.php new file mode 100644 index 000000000..24f26fb81 --- /dev/null +++ b/lib/dbalib.php @@ -0,0 +1,260 @@ + MAX_DBM_ATTEMPTS) { + ExitWiki("Cannot open database '$key' : '$file', giving +up."); + } + sleep(1); + } + } + return $dbi; + } + + + function CloseDataBase($dbi) { + reset($dbi); + while (list($dbmfile, $dbihandle) = each($dbi)) { + dba_close($dbihandle); + } + return; + } + + + // take a serialized hash, return same padded out to + // the next largest number bytes divisible by 500. This + // is to save disk space in the long run, since DBM files + // leak memory. + function PadSerializedData($data) { + // calculate the next largest number divisible by 500 + $nextincr = 500 * ceil(strlen($data) / 500); + // pad with spaces + $data = sprintf("%-${nextincr}s", $data); + return $data; + } + + // strip trailing whitespace from the serialized data + // structure. + function UnPadSerializedData($data) { + return chop($data); + } + + + + // Return hash of page + attributes or default + function RetrievePage($dbi, $pagename, $pagestore) { + if ($data = dba_fetch($pagename, $dbi[$pagestore])) { + // unserialize $data into a hash + $pagehash = unserialize(UnPadSerializedData($data)); + return $pagehash; + } else { + return -1; + } + } + + + // Either insert or replace a key/value (a page) + function InsertPage($dbi, $pagename, $pagehash) { + $pagedata = PadSerializedData(serialize($pagehash)); + + if (!dba_insert($pagename, $pagedata, $dbi['wiki'])) { + if (!dba_replace($pagename, $pagedata, $dbi['wiki'])) { + ExitWiki("Error inserting page '$pagename'"); + } + } + } + + + // for archiving pages to a seperate dbm + function SaveCopyToArchive($dbi, $pagename, $pagehash) { + global $ArchivePageStore; + + $pagedata = PadSerializedData(serialize($pagehash)); + + if (!dba_insert($pagename, $pagedata, $dbi[$ArchivePageStore])) { + if (!dba_replace($pagename, $pagedata, $dbi['archive'])) { + ExitWiki("Error storing '$pagename' into archive"); + } + } + } + + + function IsWikiPage($dbi, $pagename) { + return dba_exists($pagename, $dbi['wiki']); + } + + + function IsInArchive($dbi, $pagename) { + return dba_exists($pagename, $dbi['archive']); + } + + + // setup for title-search + function InitTitleSearch($dbi, $search) { + $pos['search'] = $search; + $pos['key'] = dba_firstkey($dbi['wiki']); + + return $pos; + } + + // iterating through database + function TitleSearchNextMatch($dbi, &$pos) { + while ($pos['key']) { + $page = $pos['key']; + $pos['key'] = dba_nextkey($dbi['wiki']); + + if (eregi($pos['search'], $page)) { + return $page; + } + } + return 0; + } + + // setup for full-text search + function InitFullSearch($dbi, $search) { + return InitTitleSearch($dbi, $search); + } + + //iterating through database + function FullSearchNextMatch($dbi, &$pos) { + while ($pos['key']) { + $key = $pos['key']; + $pos['key'] = dba_nextkey($dbi['wiki']); + + $pagedata = dba_fetch($key, $dbi['wiki']); + // test the serialized data + if (eregi($pos['search'], $pagedata)) { + $page['pagename'] = $key; + $pagedata = unserialize(UnPadSerializedData($pagedata)); + $page['content'] = $pagedata['content']; + return $page; + } + } + return 0; + } + + //////////////////////// + // new database features + + + function IncreaseHitCount($dbi, $pagename) { + + if (dba_exists($pagename, $dbi['hitcount'])) { + // increase the hit count + // echo "$pagename there, incrementing...
\n"; + $count = dba_fetch($pagename, $dbi['hitcount']); + $count++; + dba_replace($pagename, $count, $dbi['hitcount']); + } else { + // add it, set the hit count to one + // echo "adding $pagename to hitcount...
\n"; + $count = 1; + dba_insert($pagename, $count, $dbi['hitcount']); + } + } + + function GetHitCount($dbi, $pagename) { + + if (dba_exists($pagename, $dbi['hitcount'])) { + // increase the hit count + $count = dba_fetch($pagename, $dbi['hitcount']); + return $count; + } else { + return 0; + } + } + + + function InitMostPopular($dbi, $limit) { + // iterate through the whole dbm file for hit counts + // sort the results highest to lowest, and return + // n..$limit results + + $pagename = dba_firstkey($dbi['hitcount']); + $res[$pagename] = dba_fetch($pagename, $dbi['hitcount']); + + while ($pagename = dba_nextkey($dbi['hitcount'])) { + $res[$pagename] = dba_fetch($pagename, $dbi['hitcount']); + //echo "got $pagename with value " . $res[$pagename] . "
\n"; + } + + arsort($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; + + $namelist[$ctr] = $key = dba_firstkey($dbi); + + while ($key = dba_nextkey($dbi)) { + $ctr++; + $namelist[$ctr] = $key; + } + + return $namelist; + } + +?> + + -- 2.45.0