]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/db_filesystem.php
log
[SourceForge/phpwiki.git] / lib / db_filesystem.php
1 <?php  rcs_id('$Id: db_filesystem.php,v 1.5 2001-02-12 01:43:10 dairiki 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 $WikiPageStore = "wiki";
22 $ArchivePageStore = "archive";
23
24 // Initialize our globals:
25 function _dbname($base)
26 {
27   extract($GLOBALS['DBParams']);
28   return "$directory/$database/${prefix}${base}";
29 }
30
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');
36
37 if (preg_match('@%/tmp\b@', $DBParams['directory']))
38    $DBWarning = "Filesystem DB directory is in the /tmp directory.";
39    
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 @.
44
45
46    function OpenDataBase($dbname) {
47       global $WikiDB;
48
49       ksort($WikiDB);
50       reset($WikiDB);
51
52       return $WikiDB;
53    }
54
55
56    function CloseDataBase($dbi) {
57       return;
58    }
59
60    
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
66          if (!$locked) { 
67             ExitWiki("Timeout while obtaining lock. Please try again"); 
68          }
69          if ($data = file($filename)) {
70             // unserialize $data into a hash
71             $pagehash = unserialize(join("\n", $data));
72                  }      
73                  fclose($fd);
74                  if($data) {
75                     return $pagehash;
76                  }
77       } else {
78          return -1;
79       }
80    }
81
82
83    // Either insert or replace a key/value (a page)
84    function Filesystem_WritePage($dbi, $pagename, $pagehash) {
85       global $WikiPageStore;
86       $pagedata = serialize($pagehash);
87
88       if (!file_exists($dbi)) {
89              $d = split("/", $dbi);
90                  $dt = "";
91                  while(list($key, $val) = each($d)) {
92                         $dt = $dt.$val."/";
93                     @mkdir($dt, 0755);
94                  }
95           }
96
97       $filename = $dbi . "/" . $pagename;
98       if($fd = fopen($filename, 'a')) { 
99          $locked = flock($fd,2); #Exclusive blocking lock 
100          if (!$locked) { 
101             ExitWiki("Timeout while obtaining lock. Please try again"); 
102          } 
103
104          #Second (actually used) filehandle 
105          $fdsafe = fopen($filename, 'w'); 
106          fwrite($fdsafe, $pagedata); 
107          fclose($fdsafe); 
108          fclose($fd);
109       } else {
110          ExitWiki("Error while writing page '$pagename'");
111       }
112    }
113
114    function InsertPage($dbi, $pagename, $pagehash) {
115       return Filesystem_WritePage($dbi['wiki'], $pagename, $pagehash);
116    }
117
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);
122    }
123
124
125    function IsWikiPage($dbi, $pagename) {
126       return file_exists($dbi['wiki'] . "/" . $pagename);
127    }
128
129
130    function IsInArchive($dbi, $pagename) {
131       return file_exists($dbi['archive'] . "/" . $pagename);
132    }
133
134
135    // setup for title-search
136    function InitTitleSearch($dbi, $search) { 
137       $pos['search'] = $search;
138       $pos['data'] = GetAllWikiPageNames($dbi['wiki']);
139
140       return $pos;
141    }
142
143    // iterating through database
144    function TitleSearchNextMatch($dbi, &$pos) { 
145       while (list($key, $page) = each($pos['data'])) {
146          if (eregi($pos['search'], $page)) {
147             return $page;
148          }
149       }
150       return 0;
151    }
152
153    // setup for full-text search
154    function InitFullSearch($dbi, $search) { 
155       return InitTitleSearch($dbi, $search);
156    }
157
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))) {
164                 return $pagedata;
165                  }
166           }
167       return 0;
168    }
169
170    ////////////////////////
171    // new database features
172
173    function IncreaseHitCount($dbi, $pagename) {
174       return;
175 return;
176       // kluge: we ignore the $dbi for hit counting
177       global $WikiDB;
178
179       $hcdb = OpenDataBase($WikiDB['hitcount']);
180
181       if (dbmexists($hcdb['active'], $pagename)) {
182          // increase the hit count
183          $count = dbmfetch($hcdb['active'], $pagename);
184          $count++;
185          dbmreplace($hcdb['active'], $pagename, $count);
186       } else {
187          // add it, set the hit count to one
188          $count = 1;
189          dbminsert($hcdb['active'], $pagename, $count);
190       }
191
192       CloseDataBase($hcdb);
193    }
194
195    function GetHitCount($dbi, $pagename) {
196       return;
197       // kluge: we ignore the $dbi for hit counting
198       global $WikiDB;
199
200       $hcdb = OpenDataBase($WikiDB['hitcount']);
201       if (dbmexists($hcdb['active'], $pagename)) {
202          // increase the hit count
203          $count = dbmfetch($hcdb['active'], $pagename);
204          return $count;
205       } else {
206          return 0;
207       }
208
209       CloseDataBase($hcdb);
210    }
211
212
213    function InitMostPopular($dbi, $limit) {
214      return;
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";
220       }
221
222       rsort($res);
223       reset($res);
224       return($res);
225    }
226
227    function MostPopularNextMatch($dbi, $res) {
228       return;
229       // the return result is a two element array with 'hits'
230       // and 'pagename' as the keys
231
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";
235          $nextpage = array(
236             "hits" => $hits,
237             "pagename" => $pagename
238          );
239          return $nextpage;
240       } else {
241          return 0;
242       }
243    } 
244
245    function GetAllWikiPagenames($dbi) {
246       $namelist = array();
247           $d = opendir($dbi);
248           $curr = 0;
249           while($entry = readdir($d)) {
250          $namelist[$curr++] = $entry;
251           }
252
253       return $namelist;
254    }
255 // For emacs users
256 // Local Variables:
257 // mode: php
258 // c-file-style: "ellemtel"
259 // End:   
260 ?>