1 <?php rcs_id('$Id: wiki_db_filesystem.php3,v 1.3 2000-08-29 08:02:41 aredridel 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 print "Error: timeout obtaining lock. Please try again"; exit;
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);
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 print "Error: timeout obtaining lock. Please try again"; exit;
85 #Second (actually used) filehandle
86 $fdsafe = fopen($filename, 'w');
87 fwrite($fdsafe, $pagedata);
91 echo "error writing value";
96 function InsertPage($dbi, $pagename, $pagehash) {
97 return Filesystem_WritePage($dbi['wiki'], $pagename, $pagehash);
100 // for archiving pages to a seperate dbm
101 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
102 global $ArchivePageStore;
103 return Filesystem_WritePage($dbi[$ArchivePageStore], $pagename, $pagehash);
107 function IsWikiPage($dbi, $pagename) {
108 return is_file($dbi['wiki'] . "/" . $pagename);
112 function IsInArchive($dbi, $pagename) {
113 return is_file($dbi['archive'] . "/" . $pagename);
117 // setup for title-search
118 function InitTitleSearch($dbi, $search) {
119 $pos['search'] = $search;
120 $pos['data'] = GetAllWikiPageNames($dbi['wiki']);
125 // iterating through database
126 function TitleSearchNextMatch($dbi, &$pos) {
127 while (list($key, $page) = each($pos['data'])) {
128 if (eregi($pos['search'], $page)) {
135 // setup for full-text search
136 function InitFullSearch($dbi, $search) {
137 return InitTitleSearch($dbi, $search);
140 //iterating through database
141 function FullSearchNextMatch($dbi, &$pos) {
142 global $WikiPageStore;
143 while (list($key, $page) = each($pos['data'])) {
144 $pagedata = RetrievePage($dbi, $page, $WikiPageStore);
145 if (eregi($pos['search'], serialize($pagedata))) {
152 ////////////////////////
153 // new database features
155 function IncreaseHitCount($dbi, $pagename) {
158 // kluge: we ignore the $dbi for hit counting
161 $hcdb = OpenDataBase($WikiDB['hitcount']);
163 if (dbmexists($hcdb['active'], $pagename)) {
164 // increase the hit count
165 $count = dbmfetch($hcdb['active'], $pagename);
167 dbmreplace($hcdb['active'], $pagename, $count);
169 // add it, set the hit count to one
171 dbminsert($hcdb['active'], $pagename, $count);
174 CloseDataBase($hcdb);
177 function GetHitCount($dbi, $pagename) {
179 // kluge: we ignore the $dbi for hit counting
182 $hcdb = OpenDataBase($WikiDB['hitcount']);
183 if (dbmexists($hcdb['active'], $pagename)) {
184 // increase the hit count
185 $count = dbmfetch($hcdb['active'], $pagename);
191 CloseDataBase($hcdb);
195 function InitMostPopular($dbi, $limit) {
197 $pagename = dbmfirstkey($dbi['hitcount']);
198 $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename);
199 while ($pagename = dbmnextkey($dbi['hitcount'], $pagename)) {
200 $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename);
201 echo "got $pagename with value " . $res[$pagename] . "<br>\n";
209 function MostPopularNextMatch($dbi, $res) {
211 // the return result is a two element array with 'hits'
212 // and 'pagename' as the keys
214 if (list($index1, $index2, $pagename, $hits) = each($res)) {
215 echo "most popular next match called<br>\n";
216 echo "got $pagename, $hits back<br>\n";
219 "pagename" => $pagename
227 function GetAllWikiPagenames($dbi) {
231 while($entry = readdir($d)) {
232 $namelist[$curr++] = $entry;