]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - wiki_dbmlib.php3
bringing documentation up to date
[SourceForge/phpwiki.git] / wiki_dbmlib.php3
1 <?php  
2
3    rcs_id('$Id: wiki_dbmlib.php3,v 1.13 2000-09-04 05:47:35 wainstead Exp $');
4
5    /*
6       Database functions:
7
8       OpenDataBase($table)
9       CloseDataBase($dbi)
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)
22    */
23
24
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 @.
29
30    function OpenDataBase($dbname) {
31       global $WikiDB; // hash of all the DBM file names
32
33       ksort($WikiDB);
34       reset($WikiDB);
35
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...
41                exit();
42             }
43             $numattempts++;
44             sleep(1);
45          }
46       }
47
48       return $dbi;
49    }
50
51
52    function CloseDataBase($dbi) {
53
54       ksort($dbi);
55       reset($dbi);
56       while (list($dbmfile, $dbihandle) = each($dbi)) {
57          dbmclose($dbi[$dbihandle]);
58       }
59       return;
60    }
61
62
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
66    // leak memory.
67    function PadSerializedData($data) {
68       // calculate the next largest number divisible by 500
69       $nextincr = 500 * ceil(strlen($data) / 500);
70       // pad with spaces
71       $data = sprintf("%-${nextincr}s", $data);
72       return $data;
73    }
74
75    // strip trailing whitespace from the serialized data 
76    // structure.
77    function UnPadSerializedData($data) {
78       return chop($data);
79    }
80
81
82
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));
88          return $pagehash;
89       } else {
90          return -1;
91       }
92    }
93
94
95    // Either insert or replace a key/value (a page)
96    function InsertPage($dbi, $pagename, $pagehash) {
97       $pagedata = PadSerializedData(serialize($pagehash));
98
99       if (dbminsert($dbi['wiki'], $pagename, $pagedata)) {
100          if (dbmreplace($dbi['wiki'], $pagename, $pagedata)) {
101             echo "error writing value";
102             exit();
103          }
104       } 
105    }
106
107
108    // for archiving pages to a seperate dbm
109    function SaveCopyToArchive($dbi, $pagename, $pagehash) {
110       global $ArchivePageStore;
111
112       $pagedata = PadSerializedData(serialize($pagehash));
113
114       if (dbminsert($dbi[$ArchivePageStore], $pagename, $pagedata)) {
115          if (dbmreplace($dbi['archive'], $pagename, $pagedata)) {
116             echo "error writing value";
117             exit();
118          }
119       } 
120    }
121
122
123    function IsWikiPage($dbi, $pagename) {
124       return dbmexists($dbi['wiki'], $pagename);
125    }
126
127
128    function IsInArchive($dbi, $pagename) {
129       return dbmexists($dbi['archive'], $pagename);
130    }
131
132
133    // setup for title-search
134    function InitTitleSearch($dbi, $search) {
135       $pos['search'] = $search;
136       $pos['key'] = dbmfirstkey($dbi['wiki']);
137
138       return $pos;
139    }
140
141    // iterating through database
142    function TitleSearchNextMatch($dbi, &$pos) {
143       while ($pos['key']) {
144          $page = $pos['key'];
145          $pos['key'] = dbmnextkey($dbi['wiki'], $pos['key']);
146
147          if (eregi($pos['search'], $page)) {
148             return $page;
149          }
150       }
151       return 0;
152    }
153
154    // setup for full-text search
155    function InitFullSearch($dbi, $search) {
156       return InitTitleSearch($dbi, $search);
157    }
158
159    //iterating through database
160    function FullSearchNextMatch($dbi, &$pos) {
161       while ($pos['key']) {
162          $key = $pos['key'];
163          $pos['key'] = dbmnextkey($dbi['wiki'], $pos['key']);
164
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'];
171             return $page;
172          }
173       }
174       return 0;
175    }
176
177    ////////////////////////
178    // new database features
179
180
181    function IncreaseHitCount($dbi, $pagename) {
182
183       if (dbmexists($dbi['hitcount'], $pagename)) {
184          // increase the hit count
185          echo "$pagename there, incrementing...<br>\n";
186          $count = dbmfetch($dbi['hitcount'], $pagename);
187          $count++;
188          dbmreplace($dbi['hitcount'], $pagename, $count);
189       } else {
190          // add it, set the hit count to one
191          // echo "adding $pagename to hitcount...<br>\n";
192          $count = 1;
193          dbminsert($dbi['hitcount'], $pagename, $count);
194       }
195    }
196
197    function GetHitCount($dbi, $pagename) {
198
199       if (dbmexists($dbi['hitcount'], $pagename)) {
200          // increase the hit count
201          $count = dbmfetch($dbi['hitcount'], $pagename);
202          return $count;
203       } else {
204          return 0;
205       }
206    }
207
208    function cmp($a,$b) {   
209        if ($a == $b) return 0;
210        return ($a > $b) ? -1 : 1;
211    }
212
213    function InitMostPopular($dbi, $limit) {
214
215       // iterate through the whole dbm file for hit counts
216       // sort the results highest to lowest, and return 
217       // n..$limit results
218
219       $pagename = dbmfirstkey($dbi['hitcount']);
220
221       $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename);
222
223       while ($pagename = dbmnextkey($dbi['hitcount'], $pagename)) {
224          $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename);
225          //echo "got $pagename with value " . $res[$pagename] . "<br>\n";
226       }
227
228       uasort($res, cmp);
229       return($res);
230    }
231
232    function MostPopularNextMatch($dbi, &$res) {
233
234       // the return result is a two element array with 'hits'
235       // and 'pagename' as the keys
236
237       if (count($res) == 0)
238          return 0;
239
240       if (list($pagename, $hits) = each($res)) {
241          //echo "most popular next match called<br>\n";
242          //echo "got $pagename, $hits back<br>\n";
243          $nextpage = array(
244             "hits" => $hits,
245             "pagename" => $pagename
246          );
247          $dbm_mostpopular_cntr++;
248          return $nextpage;
249       } else {
250          return 0;
251       }
252    } 
253
254    function GetAllWikiPagenames($dbi) {
255       $namelist = array();
256       $ctr = 0;
257
258       $namelist[$ctr] = $key = dbmfirstkey($dbi);
259
260       while ($key = dbmnextkey($dbi, $key)) {
261          $ctr++;
262          $namelist[$ctr] = $key;
263       }
264
265       return $namelist;
266    }
267
268 ?>