3 rcs_id('$Id: wiki_dbmlib.php3,v 1.13 2000-09-04 05:47:35 wainstead Exp $');
10 RetrievePage($dbi, $pagename, $pagestore)
11 InsertPage($dbi, $pagename, $pagehash)
12 SaveCopyToArchive($dbi, $pagename, $pagehash)
13 IsWikiPage($dbi, $pagename)
14 InitTitleSearch($dbi, $search)
15 TitleSearchNextMatch($dbi, $res)
16 InitFullSearch($dbi, $search)
17 FullSearchNextMatch($dbi, $res)
18 IncreaseHitCount($dbi, $pagename)
19 GetHitCount($dbi, $pagename)
20 InitMostPopular($dbi, $limit)
21 MostPopularNextMatch($dbi, $res)
25 // open a database and return the handle
26 // loop until we get a handle; php has its own
27 // locking mechanism, thank god.
28 // Suppress ugly error message with @.
30 function OpenDataBase($dbname) {
31 global $WikiDB; // hash of all the DBM file names
36 while (list($key, $file) = each($WikiDB)) {
37 while (($dbi[$key] = @dbmopen($file, "c")) < 1) {
38 if ($numattempts > MAX_DBM_ATTEMPTS) {
39 echo "Cannot open database '$key' : '$file', giving up.";
40 // we should close the files here... but...
52 function CloseDataBase($dbi) {
56 while (list($dbmfile, $dbihandle) = each($dbi)) {
57 dbmclose($dbi[$dbihandle]);
63 // take a serialized hash, return same padded out to
64 // the next largest number bytes divisible by 500. This
65 // is to save disk space in the long run, since DBM files
67 function PadSerializedData($data) {
68 // calculate the next largest number divisible by 500
69 $nextincr = 500 * ceil(strlen($data) / 500);
71 $data = sprintf("%-${nextincr}s", $data);
75 // strip trailing whitespace from the serialized data
77 function UnPadSerializedData($data) {
83 // Return hash of page + attributes or default
84 function RetrievePage($dbi, $pagename, $pagestore) {
85 if ($data = dbmfetch($dbi[$pagestore], $pagename)) {
86 // unserialize $data into a hash
87 $pagehash = unserialize(UnPadSerializedData($data));
95 // Either insert or replace a key/value (a page)
96 function InsertPage($dbi, $pagename, $pagehash) {
97 $pagedata = PadSerializedData(serialize($pagehash));
99 if (dbminsert($dbi['wiki'], $pagename, $pagedata)) {
100 if (dbmreplace($dbi['wiki'], $pagename, $pagedata)) {
101 echo "error writing value";
108 // for archiving pages to a seperate dbm
109 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
110 global $ArchivePageStore;
112 $pagedata = PadSerializedData(serialize($pagehash));
114 if (dbminsert($dbi[$ArchivePageStore], $pagename, $pagedata)) {
115 if (dbmreplace($dbi['archive'], $pagename, $pagedata)) {
116 echo "error writing value";
123 function IsWikiPage($dbi, $pagename) {
124 return dbmexists($dbi['wiki'], $pagename);
128 function IsInArchive($dbi, $pagename) {
129 return dbmexists($dbi['archive'], $pagename);
133 // setup for title-search
134 function InitTitleSearch($dbi, $search) {
135 $pos['search'] = $search;
136 $pos['key'] = dbmfirstkey($dbi['wiki']);
141 // iterating through database
142 function TitleSearchNextMatch($dbi, &$pos) {
143 while ($pos['key']) {
145 $pos['key'] = dbmnextkey($dbi['wiki'], $pos['key']);
147 if (eregi($pos['search'], $page)) {
154 // setup for full-text search
155 function InitFullSearch($dbi, $search) {
156 return InitTitleSearch($dbi, $search);
159 //iterating through database
160 function FullSearchNextMatch($dbi, &$pos) {
161 while ($pos['key']) {
163 $pos['key'] = dbmnextkey($dbi['wiki'], $pos['key']);
165 $pagedata = dbmfetch($dbi['wiki'], $key);
166 // test the serialized data
167 if (eregi($pos['search'], $pagedata)) {
168 $page['pagename'] = $key;
169 $pagedata = unserialize(UnPadSerializedData($pagedata));
170 $page['content'] = $pagedata['content'];
177 ////////////////////////
178 // new database features
181 function IncreaseHitCount($dbi, $pagename) {
183 if (dbmexists($dbi['hitcount'], $pagename)) {
184 // increase the hit count
185 echo "$pagename there, incrementing...<br>\n";
186 $count = dbmfetch($dbi['hitcount'], $pagename);
188 dbmreplace($dbi['hitcount'], $pagename, $count);
190 // add it, set the hit count to one
191 // echo "adding $pagename to hitcount...<br>\n";
193 dbminsert($dbi['hitcount'], $pagename, $count);
197 function GetHitCount($dbi, $pagename) {
199 if (dbmexists($dbi['hitcount'], $pagename)) {
200 // increase the hit count
201 $count = dbmfetch($dbi['hitcount'], $pagename);
208 function cmp($a,$b) {
209 if ($a == $b) return 0;
210 return ($a > $b) ? -1 : 1;
213 function InitMostPopular($dbi, $limit) {
215 // iterate through the whole dbm file for hit counts
216 // sort the results highest to lowest, and return
219 $pagename = dbmfirstkey($dbi['hitcount']);
221 $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename);
223 while ($pagename = dbmnextkey($dbi['hitcount'], $pagename)) {
224 $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename);
225 //echo "got $pagename with value " . $res[$pagename] . "<br>\n";
232 function MostPopularNextMatch($dbi, &$res) {
234 // the return result is a two element array with 'hits'
235 // and 'pagename' as the keys
237 if (count($res) == 0)
240 if (list($pagename, $hits) = each($res)) {
241 //echo "most popular next match called<br>\n";
242 //echo "got $pagename, $hits back<br>\n";
245 "pagename" => $pagename
247 $dbm_mostpopular_cntr++;
254 function GetAllWikiPagenames($dbi) {
258 $namelist[$ctr] = $key = dbmfirstkey($dbi);
260 while ($key = dbmnextkey($dbi, $key)) {
262 $namelist[$ctr] = $key;