3 rcs_id('$Id: dbmlib.php,v 1.3 2000-11-01 11:31:41 ahollosi 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
34 while (list($key, $file) = each($WikiDB)) {
35 while (($dbi[$key] = @dbmopen($file, "c")) < 1) {
37 if ($numattempts > MAX_DBM_ATTEMPTS) {
38 ExitWiki("Cannot open database '$key' : '$file', giving up.");
47 function CloseDataBase($dbi) {
49 while (list($dbmfile, $dbihandle) = each($dbi)) {
56 // take a serialized hash, return same padded out to
57 // the next largest number bytes divisible by 500. This
58 // is to save disk space in the long run, since DBM files
60 function PadSerializedData($data) {
61 // calculate the next largest number divisible by 500
62 $nextincr = 500 * ceil(strlen($data) / 500);
64 $data = sprintf("%-${nextincr}s", $data);
68 // strip trailing whitespace from the serialized data
70 function UnPadSerializedData($data) {
76 // Return hash of page + attributes or default
77 function RetrievePage($dbi, $pagename, $pagestore) {
78 if ($data = dbmfetch($dbi[$pagestore], $pagename)) {
79 // unserialize $data into a hash
80 $pagehash = unserialize(UnPadSerializedData($data));
88 // Either insert or replace a key/value (a page)
89 function InsertPage($dbi, $pagename, $pagehash) {
90 $pagedata = PadSerializedData(serialize($pagehash));
92 if (dbminsert($dbi['wiki'], $pagename, $pagedata)) {
93 if (dbmreplace($dbi['wiki'], $pagename, $pagedata)) {
94 ExitWiki("Error inserting page '$pagename'");
100 // for archiving pages to a seperate dbm
101 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
102 global $ArchivePageStore;
104 $pagedata = PadSerializedData(serialize($pagehash));
106 if (dbminsert($dbi[$ArchivePageStore], $pagename, $pagedata)) {
107 if (dbmreplace($dbi['archive'], $pagename, $pagedata)) {
108 ExitWiki("Error storing '$pagename' into archive");
114 function IsWikiPage($dbi, $pagename) {
115 return dbmexists($dbi['wiki'], $pagename);
119 function IsInArchive($dbi, $pagename) {
120 return dbmexists($dbi['archive'], $pagename);
124 // setup for title-search
125 function InitTitleSearch($dbi, $search) {
126 $pos['search'] = $search;
127 $pos['key'] = dbmfirstkey($dbi['wiki']);
132 // iterating through database
133 function TitleSearchNextMatch($dbi, &$pos) {
134 while ($pos['key']) {
136 $pos['key'] = dbmnextkey($dbi['wiki'], $pos['key']);
138 if (eregi($pos['search'], $page)) {
145 // setup for full-text search
146 function InitFullSearch($dbi, $search) {
147 return InitTitleSearch($dbi, $search);
150 //iterating through database
151 function FullSearchNextMatch($dbi, &$pos) {
152 while ($pos['key']) {
154 $pos['key'] = dbmnextkey($dbi['wiki'], $pos['key']);
156 $pagedata = dbmfetch($dbi['wiki'], $key);
157 // test the serialized data
158 if (eregi($pos['search'], $pagedata)) {
159 $page['pagename'] = $key;
160 $pagedata = unserialize(UnPadSerializedData($pagedata));
161 $page['content'] = $pagedata['content'];
168 ////////////////////////
169 // new database features
172 function IncreaseHitCount($dbi, $pagename) {
174 if (dbmexists($dbi['hitcount'], $pagename)) {
175 // increase the hit count
176 // echo "$pagename there, incrementing...<br>\n";
177 $count = dbmfetch($dbi['hitcount'], $pagename);
179 dbmreplace($dbi['hitcount'], $pagename, $count);
181 // add it, set the hit count to one
182 // echo "adding $pagename to hitcount...<br>\n";
184 dbminsert($dbi['hitcount'], $pagename, $count);
188 function GetHitCount($dbi, $pagename) {
190 if (dbmexists($dbi['hitcount'], $pagename)) {
191 // increase the hit count
192 $count = dbmfetch($dbi['hitcount'], $pagename);
200 function InitMostPopular($dbi, $limit) {
201 // iterate through the whole dbm file for hit counts
202 // sort the results highest to lowest, and return
205 $pagename = dbmfirstkey($dbi['hitcount']);
206 $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename);
208 while ($pagename = dbmnextkey($dbi['hitcount'], $pagename)) {
209 $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename);
210 //echo "got $pagename with value " . $res[$pagename] . "<br>\n";
217 function MostPopularNextMatch($dbi, &$res) {
219 // the return result is a two element array with 'hits'
220 // and 'pagename' as the keys
222 if (count($res) == 0)
225 if (list($pagename, $hits) = each($res)) {
226 //echo "most popular next match called<br>\n";
227 //echo "got $pagename, $hits back<br>\n";
230 "pagename" => $pagename
232 // $dbm_mostpopular_cntr++;
239 function GetAllWikiPagenames($dbi) {
243 $namelist[$ctr] = $key = dbmfirstkey($dbi);
245 while ($key = dbmnextkey($dbi, $key)) {
247 $namelist[$ctr] = $key;