1 <?php rcs_id('$Id: db_filesystem.php,v 1.5 2001-02-12 01:43:10 dairiki Exp $');
7 RetrievePage($dbi, $pagename, $pagestore)
8 InsertPage($dbi, $pagename, $pagehash)
9 SaveCopyToArchive($dbi, $pagename, $pagehash)
10 IsWikiPage($dbi, $pagename)
11 InitTitleSearch($dbi, $search)
12 TitleSearchNextMatch($dbi, $res)
13 InitFullSearch($dbi, $search)
14 FullSearchNextMatch($dbi, $res)
15 IncreaseHitCount($dbi, $pagename)
16 GetHitCount($dbi, $pagename)
17 InitMostPopular($dbi, $limit)
18 MostPopularNextMatch($dbi, $res)
21 $WikiPageStore = "wiki";
22 $ArchivePageStore = "archive";
24 // Initialize our globals:
25 function _dbname($base)
27 extract($GLOBALS['DBParams']);
28 return "$directory/$database/${prefix}${base}";
31 $WikiDB['wiki'] = _dbname('pages');
32 $WikiDB['archive'] = _dbname('archive');
33 $WikiDB['wikilinks'] = _dbname('links');
34 $WikiDB['hottopics'] = _dbname('hottopics');
35 $WikiDB['hitcount'] = _dbname('hitcount');
37 if (preg_match('@%/tmp\b@', $DBParams['directory']))
38 $DBWarning = "Filesystem DB directory is in the /tmp directory.";
40 // open a database and return the handle
41 // loop until we get a handle; php has its own
42 // locking mechanism, thank god.
43 // Suppress ugly error message with @.
46 function OpenDataBase($dbname) {
56 function CloseDataBase($dbi) {
61 // Return hash of page + attributes or default
62 function RetrievePage($dbi, $pagename, $pagestore) {
63 $filename = $dbi[$pagestore] . "/" . $pagename;
64 if ($fd = @fopen($filename, "r")) {
65 $locked = flock($fd, 1); # Read lock
67 ExitWiki("Timeout while obtaining lock. Please try again");
69 if ($data = file($filename)) {
70 // unserialize $data into a hash
71 $pagehash = unserialize(join("\n", $data));
83 // Either insert or replace a key/value (a page)
84 function Filesystem_WritePage($dbi, $pagename, $pagehash) {
85 global $WikiPageStore;
86 $pagedata = serialize($pagehash);
88 if (!file_exists($dbi)) {
89 $d = split("/", $dbi);
91 while(list($key, $val) = each($d)) {
97 $filename = $dbi . "/" . $pagename;
98 if($fd = fopen($filename, 'a')) {
99 $locked = flock($fd,2); #Exclusive blocking lock
101 ExitWiki("Timeout while obtaining lock. Please try again");
104 #Second (actually used) filehandle
105 $fdsafe = fopen($filename, 'w');
106 fwrite($fdsafe, $pagedata);
110 ExitWiki("Error while writing page '$pagename'");
114 function InsertPage($dbi, $pagename, $pagehash) {
115 return Filesystem_WritePage($dbi['wiki'], $pagename, $pagehash);
118 // for archiving pages to a seperate dbm
119 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
120 global $ArchivePageStore;
121 return Filesystem_WritePage($dbi[$ArchivePageStore], $pagename, $pagehash);
125 function IsWikiPage($dbi, $pagename) {
126 return file_exists($dbi['wiki'] . "/" . $pagename);
130 function IsInArchive($dbi, $pagename) {
131 return file_exists($dbi['archive'] . "/" . $pagename);
135 // setup for title-search
136 function InitTitleSearch($dbi, $search) {
137 $pos['search'] = $search;
138 $pos['data'] = GetAllWikiPageNames($dbi['wiki']);
143 // iterating through database
144 function TitleSearchNextMatch($dbi, &$pos) {
145 while (list($key, $page) = each($pos['data'])) {
146 if (eregi($pos['search'], $page)) {
153 // setup for full-text search
154 function InitFullSearch($dbi, $search) {
155 return InitTitleSearch($dbi, $search);
158 //iterating through database
159 function FullSearchNextMatch($dbi, &$pos) {
160 global $WikiPageStore;
161 while (list($key, $page) = each($pos['data'])) {
162 $pagedata = RetrievePage($dbi, $page, $WikiPageStore);
163 if (eregi($pos['search'], serialize($pagedata))) {
170 ////////////////////////
171 // new database features
173 function IncreaseHitCount($dbi, $pagename) {
176 // kluge: we ignore the $dbi for hit counting
179 $hcdb = OpenDataBase($WikiDB['hitcount']);
181 if (dbmexists($hcdb['active'], $pagename)) {
182 // increase the hit count
183 $count = dbmfetch($hcdb['active'], $pagename);
185 dbmreplace($hcdb['active'], $pagename, $count);
187 // add it, set the hit count to one
189 dbminsert($hcdb['active'], $pagename, $count);
192 CloseDataBase($hcdb);
195 function GetHitCount($dbi, $pagename) {
197 // kluge: we ignore the $dbi for hit counting
200 $hcdb = OpenDataBase($WikiDB['hitcount']);
201 if (dbmexists($hcdb['active'], $pagename)) {
202 // increase the hit count
203 $count = dbmfetch($hcdb['active'], $pagename);
209 CloseDataBase($hcdb);
213 function InitMostPopular($dbi, $limit) {
215 $pagename = dbmfirstkey($dbi['hitcount']);
216 $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename);
217 while ($pagename = dbmnextkey($dbi['hitcount'], $pagename)) {
218 $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename);
219 echo "got $pagename with value " . $res[$pagename] . "<br>\n";
227 function MostPopularNextMatch($dbi, $res) {
229 // the return result is a two element array with 'hits'
230 // and 'pagename' as the keys
232 if (list($index1, $index2, $pagename, $hits) = each($res)) {
233 echo "most popular next match called<br>\n";
234 echo "got $pagename, $hits back<br>\n";
237 "pagename" => $pagename
245 function GetAllWikiPagenames($dbi) {
249 while($entry = readdir($d)) {
250 $namelist[$curr++] = $entry;
258 // c-file-style: "ellemtel"