]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - wiki_db_filesystem.php3
bringing documentation up to date
[SourceForge/phpwiki.git] / wiki_db_filesystem.php3
1 <?php  rcs_id('$Id: wiki_db_filesystem.php3,v 1.3 2000-08-29 08:02:41 aredridel 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             print "Error: timeout obtaining lock. Please try again"; exit; 
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 (!is_dir($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             print "Error: timeout obtaining lock. Please try again"; exit; 
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          echo "error writing value";
92          exit();
93           }
94    }
95
96    function InsertPage($dbi, $pagename, $pagehash) {
97       return Filesystem_WritePage($dbi['wiki'], $pagename, $pagehash);
98    }
99
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);
104    }
105
106
107    function IsWikiPage($dbi, $pagename) {
108       return is_file($dbi['wiki'] . "/" . $pagename);
109    }
110
111
112    function IsInArchive($dbi, $pagename) {
113       return is_file($dbi['archive'] . "/" . $pagename);
114    }
115
116
117    // setup for title-search
118    function InitTitleSearch($dbi, $search) { 
119       $pos['search'] = $search;
120       $pos['data'] = GetAllWikiPageNames($dbi['wiki']);
121
122       return $pos;
123    }
124
125    // iterating through database
126    function TitleSearchNextMatch($dbi, &$pos) { 
127       while (list($key, $page) = each($pos['data'])) {
128          if (eregi($pos['search'], $page)) {
129             return $page;
130          }
131       }
132       return 0;
133    }
134
135    // setup for full-text search
136    function InitFullSearch($dbi, $search) { 
137       return InitTitleSearch($dbi, $search);
138    }
139
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))) {
146                 return $pagedata;
147                  }
148           }
149       return 0;
150    }
151
152    ////////////////////////
153    // new database features
154
155    function IncreaseHitCount($dbi, $pagename) {
156       return;
157 return;
158       // kluge: we ignore the $dbi for hit counting
159       global $WikiDB;
160
161       $hcdb = OpenDataBase($WikiDB['hitcount']);
162
163       if (dbmexists($hcdb['active'], $pagename)) {
164          // increase the hit count
165          $count = dbmfetch($hcdb['active'], $pagename);
166          $count++;
167          dbmreplace($hcdb['active'], $pagename, $count);
168       } else {
169          // add it, set the hit count to one
170          $count = 1;
171          dbminsert($hcdb['active'], $pagename, $count);
172       }
173
174       CloseDataBase($hcdb);
175    }
176
177    function GetHitCount($dbi, $pagename) {
178       return;
179       // kluge: we ignore the $dbi for hit counting
180       global $WikiDB;
181
182       $hcdb = OpenDataBase($WikiDB['hitcount']);
183       if (dbmexists($hcdb['active'], $pagename)) {
184          // increase the hit count
185          $count = dbmfetch($hcdb['active'], $pagename);
186          return $count;
187       } else {
188          return 0;
189       }
190
191       CloseDataBase($hcdb);
192    }
193
194
195    function InitMostPopular($dbi, $limit) {
196      return;
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";
202       }
203
204       rsort($res);
205       reset($res);
206       return($res);
207    }
208
209    function MostPopularNextMatch($dbi, $res) {
210       return;
211       // the return result is a two element array with 'hits'
212       // and 'pagename' as the keys
213
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";
217          $nextpage = array(
218             "hits" => $hits,
219             "pagename" => $pagename
220          );
221          return $nextpage;
222       } else {
223          return 0;
224       }
225    } 
226
227    function GetAllWikiPagenames($dbi) {
228       $namelist = array();
229           $d = opendir($dbi);
230           $curr = 0;
231           while($entry = readdir($d)) {
232          $namelist[$curr++] = $entry;
233           }
234
235       return $namelist;
236    }
237 ?>