1 <?php rcs_id('$Id: db_filesystem.php,v 1.4 2001-01-01 23:18:46 ahollosi 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)
22 // open a database and return the handle
23 // loop until we get a handle; php has its own
24 // locking mechanism, thank god.
25 // Suppress ugly error message with @.
27 function OpenDataBase($dbname) {
37 function CloseDataBase($dbi) {
42 // Return hash of page + attributes or default
43 function RetrievePage($dbi, $pagename, $pagestore) {
44 $filename = $dbi[$pagestore] . "/" . $pagename;
45 if ($fd = @fopen($filename, "r")) {
46 $locked = flock($fd, 1); # Read lock
48 ExitWiki("Timeout while obtaining lock. Please try again");
50 if ($data = file($filename)) {
51 // unserialize $data into a hash
52 $pagehash = unserialize(join("\n", $data));
64 // Either insert or replace a key/value (a page)
65 function Filesystem_WritePage($dbi, $pagename, $pagehash) {
66 global $WikiPageStore;
67 $pagedata = serialize($pagehash);
69 if (!file_exists($dbi)) {
70 $d = split("/", $dbi);
72 while(list($key, $val) = each($d)) {
78 $filename = $dbi . "/" . $pagename;
79 if($fd = fopen($filename, 'a')) {
80 $locked = flock($fd,2); #Exclusive blocking lock
82 ExitWiki("Timeout while obtaining lock. Please try again");
85 #Second (actually used) filehandle
86 $fdsafe = fopen($filename, 'w');
87 fwrite($fdsafe, $pagedata);
91 ExitWiki("Error while writing page '$pagename'");
95 function InsertPage($dbi, $pagename, $pagehash) {
96 return Filesystem_WritePage($dbi['wiki'], $pagename, $pagehash);
99 // for archiving pages to a seperate dbm
100 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
101 global $ArchivePageStore;
102 return Filesystem_WritePage($dbi[$ArchivePageStore], $pagename, $pagehash);
106 function IsWikiPage($dbi, $pagename) {
107 return file_exists($dbi['wiki'] . "/" . $pagename);
111 function IsInArchive($dbi, $pagename) {
112 return file_exists($dbi['archive'] . "/" . $pagename);
116 // setup for title-search
117 function InitTitleSearch($dbi, $search) {
118 $pos['search'] = $search;
119 $pos['data'] = GetAllWikiPageNames($dbi['wiki']);
124 // iterating through database
125 function TitleSearchNextMatch($dbi, &$pos) {
126 while (list($key, $page) = each($pos['data'])) {
127 if (eregi($pos['search'], $page)) {
134 // setup for full-text search
135 function InitFullSearch($dbi, $search) {
136 return InitTitleSearch($dbi, $search);
139 //iterating through database
140 function FullSearchNextMatch($dbi, &$pos) {
141 global $WikiPageStore;
142 while (list($key, $page) = each($pos['data'])) {
143 $pagedata = RetrievePage($dbi, $page, $WikiPageStore);
144 if (eregi($pos['search'], serialize($pagedata))) {
151 ////////////////////////
152 // new database features
154 function IncreaseHitCount($dbi, $pagename) {
157 // kluge: we ignore the $dbi for hit counting
160 $hcdb = OpenDataBase($WikiDB['hitcount']);
162 if (dbmexists($hcdb['active'], $pagename)) {
163 // increase the hit count
164 $count = dbmfetch($hcdb['active'], $pagename);
166 dbmreplace($hcdb['active'], $pagename, $count);
168 // add it, set the hit count to one
170 dbminsert($hcdb['active'], $pagename, $count);
173 CloseDataBase($hcdb);
176 function GetHitCount($dbi, $pagename) {
178 // kluge: we ignore the $dbi for hit counting
181 $hcdb = OpenDataBase($WikiDB['hitcount']);
182 if (dbmexists($hcdb['active'], $pagename)) {
183 // increase the hit count
184 $count = dbmfetch($hcdb['active'], $pagename);
190 CloseDataBase($hcdb);
194 function InitMostPopular($dbi, $limit) {
196 $pagename = dbmfirstkey($dbi['hitcount']);
197 $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename);
198 while ($pagename = dbmnextkey($dbi['hitcount'], $pagename)) {
199 $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename);
200 echo "got $pagename with value " . $res[$pagename] . "<br>\n";
208 function MostPopularNextMatch($dbi, $res) {
210 // the return result is a two element array with 'hits'
211 // and 'pagename' as the keys
213 if (list($index1, $index2, $pagename, $hits) = each($res)) {
214 echo "most popular next match called<br>\n";
215 echo "got $pagename, $hits back<br>\n";
218 "pagename" => $pagename
226 function GetAllWikiPagenames($dbi) {
230 while($entry = readdir($d)) {
231 $namelist[$curr++] = $entry;