]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/dbaBase.php
Bogus page names weren't being deleted by "Excorcise WikiDB" button.
[SourceForge/phpwiki.git] / lib / WikiDB / backend / dbaBase.php
1 <?php rcs_id('$Id: dbaBase.php,v 1.5 2003-02-22 00:28:34 dairiki Exp $');
2
3 require_once('lib/WikiDB/backend.php');
4
5 // FIXME:padding of data?  Is it needed?  dba_optimize() seems to do a good
6 // job at packing 'gdbm' (and 'db2') databases.
7
8 /*
9  * Tables:
10  *
11  *  page:
12  *   Index: pagename
13  *  Values: latestversion . ':' . flags . ':' serialized hash of page meta data
14  *           Currently flags = 1 if latest version has empty content.
15  *
16  *  version
17  *   Index: version:pagename
18  *   Value: serialized hash of revision meta data, including:
19  *          + quasi-meta-data %content
20  *
21  *  links
22  *   index: 'o' . pagename
23  *   value: serialized list of pages (names) which pagename links to.
24  *   index: 'i' . pagename
25  *   value: serialized list of pages which link to pagename
26  *
27  *  TODO:
28  *
29  *  Don't keep tables locked the whole time?
30  *
31  *  index table with:
32  *   list of pagenames for get_all_pages
33  *   mostpopular list?
34  *   RecentChanges support: 
35  *     lists of most recent edits (major, minor, either).
36  *   
37  *
38  *  Separate hit table, so we don't have to update the whole page entry
39  *  each time we get a hit.  (Maybe not so important though...).
40  */     
41
42 require_once('lib/DbaPartition.php');
43
44 class WikiDB_backend_dbaBase
45 extends WikiDB_backend
46 {
47     function WikiDB_backend_dbaBase (&$dba) {
48         $this->_db = &$dba;
49         // FIXME: page and version tables should be in their own files, probably.
50         // We'll pack them all in one for now (testing).
51         $this->_pagedb = new DbaPartition($dba, 'p');
52         $this->_versiondb = new DbaPartition($dba, 'v');
53         $linkdbpart = new DbaPartition($dba, 'l');
54         $this->_linkdb = new WikiDB_backend_dbaBase_linktable($linkdbpart);
55         $this->_dbdb = new DbaPartition($dba, 'd');
56     }
57
58     function close() {
59         $this->_db->close();
60     }
61
62     function optimize() {
63         $this->_db->optimize();
64     }
65
66     function sync() {
67         $this->_db->sync();
68     }
69
70     function rebuild() {
71         $this->_linkdb->rebuild();
72         $this->optimize();
73     }
74     
75     function check() {
76         return $this->_linkdb->check();
77     }
78
79     function get_pagedata($pagename) {
80         $result = $this->_pagedb->get($pagename);
81         if (!$result)
82             return false;
83         list(,,$packed) = explode(':', $result, 3);
84         $data = unserialize($packed);
85         return $data;
86     }
87
88             
89     function update_pagedata($pagename, $newdata) {
90         $result = $this->_pagedb->get($pagename);
91         if ($result) {
92             list($latestversion,$flags,$data) = explode(':', $result, 3);
93             $data = unserialize($data);
94         }
95         else {
96             $latestversion = $flags = 0;
97             $data = array();
98         }
99         
100         foreach ($newdata as $key => $val) {
101             if (empty($val))
102                 unset($data[$key]);
103             else
104                 $data[$key] = $val;
105         }
106         $this->_pagedb->set($pagename,
107                             (int)$latestversion . ':'
108                             . (int)$flags . ':'
109                             . serialize($data));
110     }
111
112     function get_latest_version($pagename) {
113         return (int) $this->_pagedb->get($pagename);
114     }
115
116     function get_previous_version($pagename, $version) {
117         $versdb = &$this->_versiondb;
118
119         while (--$version > 0) {
120             if ($versdb->exists($version . ":$pagename"))
121                 return $version;
122         }
123         return false;
124     }
125         
126     function get_versiondata($pagename, $version, $want_content = false) {
127         $data = $this->_versiondb->get((int)$version . ":$pagename");
128         return $data ? unserialize($data) : false;
129     }
130         
131     /**
132      * Delete page from the database.
133      */
134     function delete_page($pagename) {
135         $pagedb = &$this->_pagedb;
136         $versdb = &$this->_versiondb;
137
138         $version = $this->get_latest_version($pagename);
139         while ($version > 0) {
140             $versdb->set($version-- . ":$pagename", false);
141         }
142         $pagedb->set($pagename, false);
143
144         $this->set_links($pagename, false);
145     }
146             
147     /**
148      * Delete an old revision of a page.
149      */
150     function delete_versiondata($pagename, $version) {
151         $versdb = &$this->_versiondb;
152
153         $latest = $this->get_latest_version($pagename);
154
155         assert($version > 0);
156         assert($version <= $latest);
157         
158         $versdb->set((int)$version . ":$pagename", false);
159
160         if ($version == $latest) {
161             $previous = $this->get_previous_version($version);
162             if ($previous> 0) {
163                 $pvdata = $this->get_versiondata($pagename, $previous);
164                 $is_empty = empty($pvdata['%content']);
165             }
166             else
167                 $is_empty = true;
168             $this->_update_latest_version($pagename, $previous, $is_empty);
169         }
170     }
171
172     /**
173      * Create a new revision of a page.
174      */
175     function set_versiondata($pagename, $version, $data) {
176         $versdb = &$this->_versiondb;
177
178         $versdb->set((int)$version . ":$pagename", serialize($data));
179         if ($version > $this->get_latest_version($pagename))
180             $this->_update_latest_version($pagename, $version, empty($data['%content']));
181     }
182
183     function _update_latest_version($pagename, $latest, $flags) {
184         $pagedb = &$this->_pagedb;
185
186         $pdata = $pagedb->get($pagename);
187         if ($pdata)
188             list(,,$pagedata) = explode(':',$pdata,3);
189         else
190             $pagedata = serialize(array());
191         
192         $pagedb->set($pagename, (int)$latest . ':' . (int)$flags . ":$pagedata");
193     }
194
195     function get_all_pages($include_deleted = false) {
196         $pagedb = &$this->_pagedb;
197
198         $pages = array();
199         for ($page = $pagedb->firstkey(); $page!== false; $page = $pagedb->nextkey()) {
200             if (!$page) {
201                 assert(!empty($page));
202                 continue;
203             }
204             
205             if (!$include_deleted) {
206                 list($latestversion,$flags,) = explode(':', $pagedb->get($page), 3);
207                 if ($latestversion == 0 || $flags != 0)
208                     continue;   // current content is empty 
209             }
210             $pages[] = $page;
211         }
212         usort($pages, 'WikiDB_backend_dbaBase_sortbypagename');
213         return new WikiDB_backend_dbaBase_pageiter($this, $pages);
214     }
215
216     function set_links($pagename, $links) {
217         $this->_linkdb->set_links($pagename, $links);
218     }
219     
220
221     function get_links($pagename, $reversed = true) {
222         /*
223         if ($reversed) {
224             include_once('lib/WikiDB/backend/dumb/BackLinkIter.php');
225             $pages = $this->get_all_pages();
226             return new WikiDB_backend_dumb_BackLinkIter($this, $pages, $pagename);
227         }
228         */
229         $links = $this->_linkdb->get_links($pagename, $reversed);
230         return new WikiDB_backend_dbaBase_pageiter($this, $links);
231     }
232 };
233
234 function WikiDB_backend_dbaBase_sortbypagename ($a, $b) {
235     $aname = $a['pagename'];
236     $bname = $b['pagename'];
237     return strcasecmp($aname, $bname);
238 }
239
240
241 class WikiDB_backend_dbaBase_pageiter
242 extends WikiDB_backend_iterator
243 {
244     function WikiDB_backend_dbaBase_pageiter(&$backend, &$pages) {
245         $this->_backend = $backend;
246         $this->_pages = $pages ? $pages : array();
247     }
248
249     function next() {
250         if ( ! ($next = array_shift($this->_pages)) )
251             return false;
252         return array('pagename' => $next);
253     }
254             
255     function free() {
256         $this->_pages = array();
257     }
258 };
259
260 class WikiDB_backend_dbaBase_linktable 
261 {
262     function WikiDB_backend_dbaBase_linktable(&$dba) {
263         $this->_db = &$dba;
264     }
265
266     //FIXME: try stroring link lists as hashes rather than arrays.
267     // (backlink deletion would be faster.)
268     
269     function get_links($page, $reversed = true) {
270         return $this->_get_links($reversed ? 'i' : 'o', $page);
271     }
272     
273     function set_links($page, $newlinks) {
274
275         $oldlinks = $this->_get_links('o', $page);
276
277         if (!is_array($newlinks)) {
278             assert(empty($newlinks));
279             $newlinks = array();
280         }
281         else {
282             $newlinks = array_unique($newlinks);
283         }
284         sort($newlinks);
285         $this->_set_links('o', $page, $newlinks);
286
287         reset($newlinks);
288         reset($oldlinks);
289         $new = current($newlinks);
290         $old = current($oldlinks);
291         while ($new !== false || $old !== false) {
292             if ($old === false || ($new !== false && $new < $old)) {
293                 // $new is a new link (not in $oldlinks).
294                 $this->_add_backlink($new, $page);
295                 $new = next($newlinks);
296             }
297             elseif ($new === false || $old < $new) {
298                 // $old is a obsolete link (not in $newlinks).
299                 $this->_delete_backlink($old, $page);
300                 $old = next($oldlinks);
301             }
302             else {
303                 // Unchanged link (in both $newlist and $oldlinks).
304                 assert($new == $old);
305                 $new = next($newlinks);
306                 $old = next($oldlinks);
307             }
308         }
309     }
310
311     /**
312      * Rebuild the back-link index.
313      *
314      * This should never be needed, but if the database gets hosed for some reason,
315      * this should put it back into a consistent state.
316      *
317      * We assume the forward links in the our table are correct, and recalculate
318      * all the backlinks appropriately.
319      */
320     function rebuild () {
321         $db = &$this->_db;
322
323         // Delete the backlink tables, make a list of page names.
324         $okeys = array();
325         $ikeys = array();
326         for ($key = $db->firstkey(); $key; $key = $db->nextkey()) {
327             if ($key[0] == 'i')
328                 $ikeys[] = $key;
329             elseif ($key[0] == 'o')
330                 $okeys[] = $key;
331             else {
332                 trigger_error("Bad key in linktable: '$key'", E_USER_WARNING);
333                 $ikeys[] = $key;
334             }
335         }
336         foreach ($ikeys as $key) {
337             $db->delete($key);
338         }
339         foreach ($okeys as $key) {
340             $page = substr($key,1);
341             $links = $this->_get_links('o', $page);
342             $db->delete($key);
343             $this->set_links($page, $links);
344         }
345     }
346
347     function check() {
348         $db = &$this->_db;
349
350         // FIXME: check for sortedness and uniqueness in links lists.
351
352         for ($key = $db->firstkey(); $key; $key = $db->nextkey()) {
353             if (strlen($key) < 1 || ($key[0] != 'i' && $key[0] != 'o')) {
354                 $errs[] = "Bad key '$key' in table";
355                 continue;
356             }
357             $page = substr($key, 1);
358             if ($key[0] == 'o') {
359                 // Forward links.
360                 foreach($this->_get_links('o', $page) as $link) {
361                     if (!$this->_has_link('i', $link, $page))
362                         $errs[] = "backlink entry missing for link '$page'->'$link'";
363                 }
364             }
365             else {
366                 assert($key[0] == 'i');
367                 // Backlinks.
368                 foreach($this->_get_links('i', $page) as $link) {
369                     if (!$this->_has_link('o', $link, $page))
370                         $errs[] = "link entry missing for backlink '$page'<-'$link'";
371                 }
372             }
373         }
374
375         return isset($errs) ? $errs : false;
376     }
377     
378         
379     function _add_backlink($page, $linkedfrom) {
380         $backlinks = $this->_get_links('i', $page);
381         $backlinks[] = $linkedfrom;
382         sort($backlinks);
383         $this->_set_links('i', $page, $backlinks);
384     }
385     
386     function _delete_backlink($page, $linkedfrom) {
387         $backlinks = $this->_get_links('i', $page);
388         foreach ($backlinks as $key => $backlink) {
389             if ($backlink == $linkedfrom)
390                 unset($backlinks[$key]);
391         }
392         $this->_set_links('i', $page, $backlinks);
393     }
394     
395     function _has_link($which, $page, $link) {
396         $links = $this->_get_links($which, $page);
397         foreach($links as $l) {
398             if ($l == $link)
399                 return true;
400         }
401         return false;
402     }
403     
404     function _get_links($which, $page) {
405         $data = $this->_db->get($which . $page);
406         return $data ? unserialize($data) : array();
407     }
408
409     function _set_links($which, $page, &$links) {
410         $key = $which . $page;
411         if ($links)
412             $this->_db->set($key, serialize($links));
413         else
414             $this->_db->set($key, false);
415     }
416 }
417
418 // (c-file-style: "gnu")
419 // Local Variables:
420 // mode: php
421 // tab-width: 8
422 // c-basic-offset: 4
423 // c-hanging-comment-ender-p: nil
424 // indent-tabs-mode: nil
425 // End:   
426 ?>