]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/dbmlib.php
fixed E_NOTICE warnings
[SourceForge/phpwiki.git] / lib / dbmlib.php
1 <?php  
2
3    rcs_id('$Id: dbmlib.php,v 1.3 2000-11-01 11:31:41 ahollosi 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       reset($WikiDB);
34       while (list($key, $file) = each($WikiDB)) {
35          while (($dbi[$key] = @dbmopen($file, "c")) < 1) {
36             $numattempts++;
37             if ($numattempts > MAX_DBM_ATTEMPTS) {
38                ExitWiki("Cannot open database '$key' : '$file', giving up.");
39             }
40             sleep(1);
41          }
42       }
43       return $dbi;
44    }
45
46
47    function CloseDataBase($dbi) {
48       reset($dbi);
49       while (list($dbmfile, $dbihandle) = each($dbi)) {
50          dbmclose($dbihandle);
51       }
52       return;
53    }
54
55
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
59    // leak memory.
60    function PadSerializedData($data) {
61       // calculate the next largest number divisible by 500
62       $nextincr = 500 * ceil(strlen($data) / 500);
63       // pad with spaces
64       $data = sprintf("%-${nextincr}s", $data);
65       return $data;
66    }
67
68    // strip trailing whitespace from the serialized data 
69    // structure.
70    function UnPadSerializedData($data) {
71       return chop($data);
72    }
73
74
75
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));
81          return $pagehash;
82       } else {
83          return -1;
84       }
85    }
86
87
88    // Either insert or replace a key/value (a page)
89    function InsertPage($dbi, $pagename, $pagehash) {
90       $pagedata = PadSerializedData(serialize($pagehash));
91
92       if (dbminsert($dbi['wiki'], $pagename, $pagedata)) {
93          if (dbmreplace($dbi['wiki'], $pagename, $pagedata)) {
94             ExitWiki("Error inserting page '$pagename'");
95          }
96       } 
97    }
98
99
100    // for archiving pages to a seperate dbm
101    function SaveCopyToArchive($dbi, $pagename, $pagehash) {
102       global $ArchivePageStore;
103
104       $pagedata = PadSerializedData(serialize($pagehash));
105
106       if (dbminsert($dbi[$ArchivePageStore], $pagename, $pagedata)) {
107          if (dbmreplace($dbi['archive'], $pagename, $pagedata)) {
108             ExitWiki("Error storing '$pagename' into archive");
109          }
110       } 
111    }
112
113
114    function IsWikiPage($dbi, $pagename) {
115       return dbmexists($dbi['wiki'], $pagename);
116    }
117
118
119    function IsInArchive($dbi, $pagename) {
120       return dbmexists($dbi['archive'], $pagename);
121    }
122
123
124    // setup for title-search
125    function InitTitleSearch($dbi, $search) {
126       $pos['search'] = $search;
127       $pos['key'] = dbmfirstkey($dbi['wiki']);
128
129       return $pos;
130    }
131
132    // iterating through database
133    function TitleSearchNextMatch($dbi, &$pos) {
134       while ($pos['key']) {
135          $page = $pos['key'];
136          $pos['key'] = dbmnextkey($dbi['wiki'], $pos['key']);
137
138          if (eregi($pos['search'], $page)) {
139             return $page;
140          }
141       }
142       return 0;
143    }
144
145    // setup for full-text search
146    function InitFullSearch($dbi, $search) {
147       return InitTitleSearch($dbi, $search);
148    }
149
150    //iterating through database
151    function FullSearchNextMatch($dbi, &$pos) {
152       while ($pos['key']) {
153          $key = $pos['key'];
154          $pos['key'] = dbmnextkey($dbi['wiki'], $pos['key']);
155
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'];
162             return $page;
163          }
164       }
165       return 0;
166    }
167
168    ////////////////////////
169    // new database features
170
171
172    function IncreaseHitCount($dbi, $pagename) {
173
174       if (dbmexists($dbi['hitcount'], $pagename)) {
175          // increase the hit count
176          // echo "$pagename there, incrementing...<br>\n";
177          $count = dbmfetch($dbi['hitcount'], $pagename);
178          $count++;
179          dbmreplace($dbi['hitcount'], $pagename, $count);
180       } else {
181          // add it, set the hit count to one
182          // echo "adding $pagename to hitcount...<br>\n";
183          $count = 1;
184          dbminsert($dbi['hitcount'], $pagename, $count);
185       }
186    }
187
188    function GetHitCount($dbi, $pagename) {
189
190       if (dbmexists($dbi['hitcount'], $pagename)) {
191          // increase the hit count
192          $count = dbmfetch($dbi['hitcount'], $pagename);
193          return $count;
194       } else {
195          return 0;
196       }
197    }
198
199
200    function InitMostPopular($dbi, $limit) {
201       // iterate through the whole dbm file for hit counts
202       // sort the results highest to lowest, and return 
203       // n..$limit results
204
205       $pagename = dbmfirstkey($dbi['hitcount']);
206       $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename);
207
208       while ($pagename = dbmnextkey($dbi['hitcount'], $pagename)) {
209          $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename);
210          //echo "got $pagename with value " . $res[$pagename] . "<br>\n";
211       }
212
213       arsort($res);
214       return($res);
215    }
216
217    function MostPopularNextMatch($dbi, &$res) {
218
219       // the return result is a two element array with 'hits'
220       // and 'pagename' as the keys
221
222       if (count($res) == 0)
223          return 0;
224
225       if (list($pagename, $hits) = each($res)) {
226          //echo "most popular next match called<br>\n";
227          //echo "got $pagename, $hits back<br>\n";
228          $nextpage = array(
229             "hits" => $hits,
230             "pagename" => $pagename
231          );
232          // $dbm_mostpopular_cntr++;
233          return $nextpage;
234       } else {
235          return 0;
236       }
237    } 
238
239    function GetAllWikiPagenames($dbi) {
240       $namelist = array();
241       $ctr = 0;
242
243       $namelist[$ctr] = $key = dbmfirstkey($dbi);
244
245       while ($key = dbmnextkey($dbi, $key)) {
246          $ctr++;
247          $namelist[$ctr] = $key;
248       }
249
250       return $namelist;
251    }
252
253 ?>