]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/db_filesystem.php
Added comment about the warnings on loading; updated the info about
[SourceForge/phpwiki.git] / lib / db_filesystem.php
1 <?php  rcs_id('$Id: db_filesystem.php,v 1.4 2001-01-01 23:18:46 ahollosi Exp $');
2    /*
3       Database functions:
4
5       OpenDataBase($table)
6       CloseDataBase($dbi)
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)
19    */
20
21
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 @.
26
27    function OpenDataBase($dbname) {
28       global $WikiDB;
29
30       ksort($WikiDB);
31       reset($WikiDB);
32
33       return $WikiDB;
34    }
35
36
37    function CloseDataBase($dbi) {
38       return;
39    }
40
41    
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
47          if (!$locked) { 
48             ExitWiki("Timeout while obtaining lock. Please try again"); 
49          }
50          if ($data = file($filename)) {
51             // unserialize $data into a hash
52             $pagehash = unserialize(join("\n", $data));
53                  }      
54                  fclose($fd);
55                  if($data) {
56                     return $pagehash;
57                  }
58       } else {
59          return -1;
60       }
61    }
62
63
64    // Either insert or replace a key/value (a page)
65    function Filesystem_WritePage($dbi, $pagename, $pagehash) {
66       global $WikiPageStore;
67       $pagedata = serialize($pagehash);
68
69       if (!file_exists($dbi)) {
70              $d = split("/", $dbi);
71                  $dt = "";
72                  while(list($key, $val) = each($d)) {
73                         $dt = $dt.$val."/";
74                     @mkdir($dt, 0755);
75                  }
76           }
77
78       $filename = $dbi . "/" . $pagename;
79       if($fd = fopen($filename, 'a')) { 
80          $locked = flock($fd,2); #Exclusive blocking lock 
81          if (!$locked) { 
82             ExitWiki("Timeout while obtaining lock. Please try again"); 
83          } 
84
85          #Second (actually used) filehandle 
86          $fdsafe = fopen($filename, 'w'); 
87          fwrite($fdsafe, $pagedata); 
88          fclose($fdsafe); 
89          fclose($fd);
90       } else {
91          ExitWiki("Error while writing page '$pagename'");
92       }
93    }
94
95    function InsertPage($dbi, $pagename, $pagehash) {
96       return Filesystem_WritePage($dbi['wiki'], $pagename, $pagehash);
97    }
98
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);
103    }
104
105
106    function IsWikiPage($dbi, $pagename) {
107       return file_exists($dbi['wiki'] . "/" . $pagename);
108    }
109
110
111    function IsInArchive($dbi, $pagename) {
112       return file_exists($dbi['archive'] . "/" . $pagename);
113    }
114
115
116    // setup for title-search
117    function InitTitleSearch($dbi, $search) { 
118       $pos['search'] = $search;
119       $pos['data'] = GetAllWikiPageNames($dbi['wiki']);
120
121       return $pos;
122    }
123
124    // iterating through database
125    function TitleSearchNextMatch($dbi, &$pos) { 
126       while (list($key, $page) = each($pos['data'])) {
127          if (eregi($pos['search'], $page)) {
128             return $page;
129          }
130       }
131       return 0;
132    }
133
134    // setup for full-text search
135    function InitFullSearch($dbi, $search) { 
136       return InitTitleSearch($dbi, $search);
137    }
138
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))) {
145                 return $pagedata;
146                  }
147           }
148       return 0;
149    }
150
151    ////////////////////////
152    // new database features
153
154    function IncreaseHitCount($dbi, $pagename) {
155       return;
156 return;
157       // kluge: we ignore the $dbi for hit counting
158       global $WikiDB;
159
160       $hcdb = OpenDataBase($WikiDB['hitcount']);
161
162       if (dbmexists($hcdb['active'], $pagename)) {
163          // increase the hit count
164          $count = dbmfetch($hcdb['active'], $pagename);
165          $count++;
166          dbmreplace($hcdb['active'], $pagename, $count);
167       } else {
168          // add it, set the hit count to one
169          $count = 1;
170          dbminsert($hcdb['active'], $pagename, $count);
171       }
172
173       CloseDataBase($hcdb);
174    }
175
176    function GetHitCount($dbi, $pagename) {
177       return;
178       // kluge: we ignore the $dbi for hit counting
179       global $WikiDB;
180
181       $hcdb = OpenDataBase($WikiDB['hitcount']);
182       if (dbmexists($hcdb['active'], $pagename)) {
183          // increase the hit count
184          $count = dbmfetch($hcdb['active'], $pagename);
185          return $count;
186       } else {
187          return 0;
188       }
189
190       CloseDataBase($hcdb);
191    }
192
193
194    function InitMostPopular($dbi, $limit) {
195      return;
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";
201       }
202
203       rsort($res);
204       reset($res);
205       return($res);
206    }
207
208    function MostPopularNextMatch($dbi, $res) {
209       return;
210       // the return result is a two element array with 'hits'
211       // and 'pagename' as the keys
212
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";
216          $nextpage = array(
217             "hits" => $hits,
218             "pagename" => $pagename
219          );
220          return $nextpage;
221       } else {
222          return 0;
223       }
224    } 
225
226    function GetAllWikiPagenames($dbi) {
227       $namelist = array();
228           $d = opendir($dbi);
229           $curr = 0;
230           while($entry = readdir($d)) {
231          $namelist[$curr++] = $entry;
232           }
233
234       return $namelist;
235    }
236 ?>