3 rcs_id('$Id: dbalib.php,v 1.3 2001-02-12 01:43:10 dairiki Exp $');
10 PadSerializedData($data)
11 UnPadSerializedData($data)
12 RetrievePage($dbi, $pagename, $pagestore)
13 InsertPage($dbi, $pagename, $pagehash)
14 SaveCopyToArchive($dbi, $pagename, $pagehash)
15 IsWikiPage($dbi, $pagename)
16 IsInArchive($dbi, $pagename)
17 InitTitleSearch($dbi, $search)
18 TitleSearchNextMatch($dbi, &$pos)
19 InitFullSearch($dbi, $search)
20 FullSearchNextMatch($dbi, &$pos)
21 IncreaseHitCount($dbi, $pagename)
22 GetHitCount($dbi, $pagename)
23 InitMostPopular($dbi, $limit)
24 MostPopularNextMatch($dbi, &$res)
25 GetAllWikiPagenames($dbi)
29 // Initialize our globals:
30 function _dbname($base)
32 extract($GLOBALS['DBParams']);
33 return "$directory/${database}${prefix}${base}";
36 $WikiPageStore = "wiki";
37 $ArchivePageStore = "archive";
38 $WikiDB = array('wiki' => _dbname('pagesdb'),
39 'archive' => _dbname('archivedb'),
40 'wikilinks' => _dbname('linksdb'),
41 'hottopics' => _dbname('hottopicsdb'),
42 'hitcount' => _dbname('hitcountdb'));
44 if (preg_match('@%/tmp\b@', $DBParams['directory']))
45 $DBWarning = "DBA files are in the /tmp directory.";
47 define('MAX_DBM_ATTEMPTS', $DBParams['timeout']);
49 // open a database and return the handle
50 // loop until we get a handle; php has its own
51 // locking mechanism, thank god.
52 // Suppress ugly error message with @.
54 function OpenDataBase($dbname) {
55 global $WikiDB; // hash of all the DBM file names
58 while (list($key, $file) = each($WikiDB)) {
59 while (($dbi[$key] = @dba_open($file, "c", "gdbm")) < 1) {
61 if ($numattempts > MAX_DBM_ATTEMPTS) {
62 ExitWiki("Cannot open database '$key' : '$file', giving up.");
71 function CloseDataBase($dbi) {
73 while (list($dbmfile, $dbihandle) = each($dbi)) {
74 dba_close($dbihandle);
80 // take a serialized hash, return same padded out to
81 // the next largest number bytes divisible by 500. This
82 // is to save disk space in the long run, since DBM files
84 function PadSerializedData($data) {
85 // calculate the next largest number divisible by 500
86 $nextincr = 500 * ceil(strlen($data) / 500);
88 $data = sprintf("%-${nextincr}s", $data);
92 // strip trailing whitespace from the serialized data
94 function UnPadSerializedData($data) {
98 // Return hash of page + attributes or default
99 function RetrievePage($dbi, $pagename, $pagestore) {
100 if ($data = dba_fetch($pagename, $dbi[$pagestore])) {
101 // unserialize $data into a hash
102 $pagehash = unserialize(UnPadSerializedData($data));
109 // Either insert or replace a key/value (a page)
110 function InsertPage($dbi, $pagename, $pagehash) {
111 $pagedata = PadSerializedData(serialize($pagehash));
113 if (!dba_insert($pagename, $pagedata, $dbi['wiki'])) {
114 if (!dba_replace($pagename, $pagedata, $dbi['wiki'])) {
115 ExitWiki("Error inserting page '$pagename'");
121 // for archiving pages to a seperate dbm
122 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
123 global $ArchivePageStore;
125 $pagedata = PadSerializedData(serialize($pagehash));
127 if (!dba_insert($pagename, $pagedata, $dbi[$ArchivePageStore])) {
128 if (!dba_replace($pagename, $pagedata, $dbi['archive'])) {
129 ExitWiki("Error storing '$pagename' into archive");
135 function IsWikiPage($dbi, $pagename) {
136 return dba_exists($pagename, $dbi['wiki']);
140 function IsInArchive($dbi, $pagename) {
141 return dba_exists($pagename, $dbi['archive']);
145 // setup for title-search
146 function InitTitleSearch($dbi, $search) {
147 $pos['search'] = $search;
148 $pos['key'] = dba_firstkey($dbi['wiki']);
153 // iterating through database
154 function TitleSearchNextMatch($dbi, &$pos) {
155 while ($pos['key']) {
157 $pos['key'] = dba_nextkey($dbi['wiki']);
159 if (eregi($pos['search'], $page)) {
166 // setup for full-text search
167 function InitFullSearch($dbi, $search) {
168 return InitTitleSearch($dbi, $search);
171 //iterating through database
172 function FullSearchNextMatch($dbi, &$pos) {
173 while ($pos['key']) {
175 $pos['key'] = dba_nextkey($dbi['wiki']);
177 $pagedata = dba_fetch($key, $dbi['wiki']);
178 // test the serialized data
179 if (eregi($pos['search'], $pagedata)) {
180 $page['pagename'] = $key;
181 $pagedata = unserialize(UnPadSerializedData($pagedata));
182 $page['content'] = $pagedata['content'];
189 ////////////////////////
190 // new database features
193 function IncreaseHitCount($dbi, $pagename) {
195 if (dba_exists($pagename, $dbi['hitcount'])) {
196 // increase the hit count
197 // echo "$pagename there, incrementing...<br>\n";
198 $count = dba_fetch($pagename, $dbi['hitcount']);
200 dba_replace($pagename, $count, $dbi['hitcount']);
202 // add it, set the hit count to one
203 // echo "adding $pagename to hitcount...<br>\n";
205 dba_insert($pagename, $count, $dbi['hitcount']);
209 function GetHitCount($dbi, $pagename) {
211 if (dba_exists($pagename, $dbi['hitcount'])) {
212 // increase the hit count
213 $count = dba_fetch($pagename, $dbi['hitcount']);
221 function InitMostPopular($dbi, $limit) {
222 // iterate through the whole dbm file for hit counts
223 // sort the results highest to lowest, and return
226 $pagename = dba_firstkey($dbi['hitcount']);
227 $res[$pagename] = dba_fetch($pagename, $dbi['hitcount']);
229 while ($pagename = dba_nextkey($dbi['hitcount'])) {
230 $res[$pagename] = dba_fetch($pagename, $dbi['hitcount']);
231 //echo "got $pagename with value " . $res[$pagename] . "<br>\n";
238 function MostPopularNextMatch($dbi, &$res) {
240 // the return result is a two element array with 'hits'
241 // and 'pagename' as the keys
243 if (count($res) == 0)
246 if (list($pagename, $hits) = each($res)) {
247 //echo "most popular next match called<br>\n";
248 //echo "got $pagename, $hits back<br>\n";
251 "pagename" => $pagename
253 // $dbm_mostpopular_cntr++;
260 function GetAllWikiPagenames($dbi) {
264 $namelist[$ctr] = $key = dba_firstkey($dbi);
266 while ($key = dba_nextkey($dbi)) {
268 $namelist[$ctr] = $key;
277 // c-file-style: "ellemtel"