]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/db_filesystem.php
Remove debugging output cruft which should never have made it to the CVS
[SourceForge/phpwiki.git] / lib / db_filesystem.php
1 <?php  rcs_id('$Id: db_filesystem.php,v 1.4.2.2 2001-08-18 02:05:28 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       MakeBackLinkSearchRegexp($pagename)
16       InitBackLinkSearch($dbi, $pagename) 
17       BackLinkSearchNextMatch($dbi, &$pos) 
18       IncreaseHitCount($dbi, $pagename)
19       GetHitCount($dbi, $pagename)
20       InitMostPopular($dbi, $limit)
21       MostPopularNextMatch($dbi, $res)
22    */
23
24
25    // open a database and return the handle
26    // loop until we get a handle; php has its own
27    // locking mechanism, thank god.
28    // Suppress ugly error message with @.
29
30    function OpenDataBase($dbname) {
31       global $WikiDB;
32
33       ksort($WikiDB);
34       reset($WikiDB);
35
36       return $WikiDB;
37    }
38
39
40    function CloseDataBase($dbi) {
41       return;
42    }
43
44    
45    // Return hash of page + attributes or default
46    function RetrievePage($dbi, $pagename, $pagestore) {
47       $filename = $dbi[$pagestore] . "/" . $pagename;
48       if ($fd = @fopen($filename, "r")) {
49          $locked = flock($fd, 1); # Read lock
50          if (!$locked) { 
51             ExitWiki("Timeout while obtaining lock. Please try again"); 
52          }
53          if ($data = file($filename)) {
54             // unserialize $data into a hash
55             $pagehash = unserialize(join("\n", $data));
56                  }      
57                  fclose($fd);
58                  if($data) {
59                     return $pagehash;
60                  }
61       } else {
62          return -1;
63       }
64    }
65
66
67    // Either insert or replace a key/value (a page)
68    function Filesystem_WritePage($dbi, $pagename, $pagehash) {
69       global $WikiPageStore;
70       $pagedata = serialize($pagehash);
71
72       if (!file_exists($dbi)) {
73              $d = split("/", $dbi);
74                  $dt = "";
75                  while(list($key, $val) = each($d)) {
76                         $dt = $dt.$val."/";
77                     @mkdir($dt, 0755);
78                  }
79           }
80
81       $filename = $dbi . "/" . $pagename;
82       if($fd = fopen($filename, 'a')) { 
83          $locked = flock($fd,2); #Exclusive blocking lock 
84          if (!$locked) { 
85             ExitWiki("Timeout while obtaining lock. Please try again"); 
86          } 
87
88          #Second (actually used) filehandle 
89          $fdsafe = fopen($filename, 'w'); 
90          fwrite($fdsafe, $pagedata); 
91          fclose($fdsafe); 
92          fclose($fd);
93       } else {
94          ExitWiki("Error while writing page '$pagename'");
95       }
96    }
97
98    function InsertPage($dbi, $pagename, $pagehash) {
99       return Filesystem_WritePage($dbi['wiki'], $pagename, $pagehash);
100    }
101
102    // for archiving pages to a seperate dbm
103    function SaveCopyToArchive($dbi, $pagename, $pagehash) {
104       global $ArchivePageStore;
105       return Filesystem_WritePage($dbi[$ArchivePageStore], $pagename, $pagehash);
106    }
107
108
109    function IsWikiPage($dbi, $pagename) {
110       return file_exists($dbi['wiki'] . "/" . $pagename);
111    }
112
113
114    function IsInArchive($dbi, $pagename) {
115       return file_exists($dbi['archive'] . "/" . $pagename);
116    }
117
118
119    // setup for title-search
120    function InitTitleSearch($dbi, $search) { 
121       $pos['search'] = $search;
122       $pos['data'] = GetAllWikiPageNames($dbi['wiki']);
123
124       return $pos;
125    }
126
127    // iterating through database
128    function TitleSearchNextMatch($dbi, &$pos) { 
129       while (list($key, $page) = each($pos['data'])) {
130          if (eregi($pos['search'], $page)) {
131             return $page;
132          }
133       }
134       return 0;
135    }
136
137    // setup for full-text search
138    function InitFullSearch($dbi, $search) { 
139       return InitTitleSearch($dbi, $search);
140    }
141
142    //iterating through database
143    function FullSearchNextMatch($dbi, &$pos) { 
144       global $WikiPageStore;
145       while (list($key, $page) = each($pos['data'])) {
146          $pagedata = RetrievePage($dbi, $page, $WikiPageStore);
147          if (eregi($pos['search'], serialize($pagedata))) {
148                 return $pagedata;
149                  }
150           }
151       return 0;
152    }
153
154    ////////////////////////
155    // new database features
156
157    // Compute PCRE suitable for searching for links to the given page.
158    function MakeBackLinkSearchRegexp($pagename) {
159       global $WikiNameRegexp;
160
161       // Note that in (at least some) PHP 3.x's, preg_quote only takes
162       // (at most) one argument.  Also it doesn't quote '/'s.
163       // It does quote '='s, so we'll use that for the delimeter.
164       $quoted_pagename = preg_quote($pagename);
165       if (preg_match("/^$WikiNameRegexp\$/", $pagename)) {
166          # FIXME: This may need modification for non-standard (non-english) $WikiNameRegexp.
167          return "=(?<![A-Za-z0-9!])$quoted_pagename(?![A-Za-z0-9])=";
168       }
169       else {
170          // Note from author: Sorry. :-/
171          return ( '='
172                   . '(?<!\[)\[(?!\[)' // Single, isolated '['
173                   . '([^]|]*\|)?'     // Optional stuff followed by '|'
174                   . '\s*'             // Optional space
175                   . $quoted_pagename  // Pagename
176                   . '\s*\]=' );       // Optional space, followed by ']'
177          // FIXME: the above regexp is still not quite right.
178          // Consider the text: " [ [ test page ]".  This is a link to a page
179          // named '[ test page'.  The above regexp will recognize this
180          // as a link either to '[ test page' (good) or to 'test page' (wrong).
181       } 
182    }
183
184    // setup for back-link search
185    function InitBackLinkSearch($dbi, $pagename) {
186       return InitTitleSearch($dbi, MakeBackLinkSearchRegexp($pagename));
187    }
188
189    // iterating through back-links
190    function BackLinkSearchNextMatch($dbi, &$pos) {
191       global $WikiPageStore;
192       while (list($key, $page) = each($pos['data'])) {
193          $pagedata = RetrievePage($dbi, $page, $WikiPageStore);
194          
195          while (list($i, $line) = each($pagedata['content'])) {
196             if (preg_match($pos['search'], $line))
197                return $page;
198          }
199       }
200       return 0;
201    }
202
203    function IncreaseHitCount($dbi, $pagename) {
204       return;
205 return;
206       // kluge: we ignore the $dbi for hit counting
207       global $WikiDB;
208
209       $hcdb = OpenDataBase($WikiDB['hitcount']);
210
211       if (dbmexists($hcdb['active'], $pagename)) {
212          // increase the hit count
213          $count = dbmfetch($hcdb['active'], $pagename);
214          $count++;
215          dbmreplace($hcdb['active'], $pagename, $count);
216       } else {
217          // add it, set the hit count to one
218          $count = 1;
219          dbminsert($hcdb['active'], $pagename, $count);
220       }
221
222       CloseDataBase($hcdb);
223    }
224
225    function GetHitCount($dbi, $pagename) {
226       return;
227       // kluge: we ignore the $dbi for hit counting
228       global $WikiDB;
229
230       $hcdb = OpenDataBase($WikiDB['hitcount']);
231       if (dbmexists($hcdb['active'], $pagename)) {
232          // increase the hit count
233          $count = dbmfetch($hcdb['active'], $pagename);
234          return $count;
235       } else {
236          return 0;
237       }
238
239       CloseDataBase($hcdb);
240    }
241
242
243    function InitMostPopular($dbi, $limit) {
244      return;
245       $pagename = dbmfirstkey($dbi['hitcount']);
246       $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename);
247       while ($pagename = dbmnextkey($dbi['hitcount'], $pagename)) {
248          $res[$pagename] = dbmfetch($dbi['hitcount'], $pagename);
249          echo "got $pagename with value " . $res[$pagename] . "<br>\n";
250       }
251
252       rsort($res);
253       reset($res);
254       return($res);
255    }
256
257    function MostPopularNextMatch($dbi, $res) {
258       return;
259       // the return result is a two element array with 'hits'
260       // and 'pagename' as the keys
261
262       if (list($index1, $index2, $pagename, $hits) = each($res)) {
263          echo "most popular next match called<br>\n";
264          echo "got $pagename, $hits back<br>\n";
265          $nextpage = array(
266             "hits" => $hits,
267             "pagename" => $pagename
268          );
269          return $nextpage;
270       } else {
271          return 0;
272       }
273    } 
274
275    function GetAllWikiPagenames($dbi) {
276       $namelist = array();
277       $d = opendir($dbi);
278       while($entry = readdir($d)) {
279          if ($entry != '.' && $entry != '..')
280             $namelist[] = $entry;
281       }
282
283       return $namelist;
284    }
285 ?>