]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/dbaBase.php
* revert to the wikidb ref passing. there's no memory abuse there.
[SourceForge/phpwiki.git] / lib / WikiDB / backend / dbaBase.php
1 <?php rcs_id('$Id: dbaBase.php,v 1.11 2004-11-09 17:11:17 rurban 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  *  Don't keep tables locked the whole time
29  *
30  *  index table with:
31  *   list of pagenames for get_all_pages
32  *   mostpopular list?
33  *   RecentChanges support: 
34  *     lists of most recent edits (major, minor, either).
35  *   
36  *
37  *  Separate hit table, so we don't have to update the whole page entry
38  *  each time we get a hit.  (Maybe not so important though...).
39  */     
40
41 require_once('lib/DbaPartition.php');
42
43 class WikiDB_backend_dbaBase
44 extends WikiDB_backend
45 {
46     function WikiDB_backend_dbaBase (&$dba) {
47         $this->_db = &$dba;
48         // TODO: page and version tables should be in their own files, probably.
49         // We'll pack them all in one for now (testing).
50         // 2004-07-09 10:07:30 rurban: It's fast enough this way.
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 sortable_columns() {
59         return array('pagename','mtime'/*,'author_id','author'*/);
60     }
61     
62     function close() {
63         $this->_db->close();
64     }
65
66     function optimize() {
67         $this->_db->optimize();
68     }
69
70     function sync() {
71         $this->_db->sync();
72     }
73
74     function rebuild() {
75         $this->_linkdb->rebuild();
76         $this->optimize();
77     }
78     
79     function check() {
80         return $this->_linkdb->check();
81     }
82
83     function get_pagedata($pagename) {
84         $result = $this->_pagedb->get($pagename);
85         if (!$result)
86             return false;
87         list(,,$packed) = explode(':', $result, 3);
88         $data = unserialize($packed);
89         return $data;
90     }
91             
92     function update_pagedata($pagename, $newdata) {
93         $result = $this->_pagedb->get($pagename);
94         if ($result) {
95             list($latestversion,$flags,$data) = explode(':', $result, 3);
96             $data = unserialize($data);
97         }
98         else {
99             $latestversion = $flags = 0;
100             $data = array();
101         }
102         
103         foreach ($newdata as $key => $val) {
104             if (empty($val))
105                 unset($data[$key]);
106             else
107                 $data[$key] = $val;
108         }
109         $this->_pagedb->set($pagename,
110                             (int)$latestversion . ':'
111                             . (int)$flags . ':'
112                             . serialize($data));
113     }
114
115     function get_latest_version($pagename) {
116         return (int) $this->_pagedb->get($pagename);
117     }
118
119     function get_previous_version($pagename, $version) {
120         $versdb = &$this->_versiondb;
121
122         while (--$version > 0) {
123             if ($versdb->exists($version . ":$pagename"))
124                 return $version;
125         }
126         return false;
127     }
128
129     //check $want_content
130     function get_versiondata($pagename, $version, $want_content=false) {
131         $data = $this->_versiondb->get((int)$version . ":$pagename");
132         if (empty($data)) return false;
133         else {
134             $data = unserialize($data);
135             if (!$want_content)
136                 $data['%content'] = !empty($data['%content']);
137             return $data;
138         }
139     }
140         
141     /**
142      * Delete page from the database.
143      */
144     function delete_page($pagename) {
145         $pagedb = &$this->_pagedb;
146         $versdb = &$this->_versiondb;
147
148         $version = $this->get_latest_version($pagename);
149         while ($version > 0) {
150             $versdb->set($version-- . ":$pagename", false);
151         }
152         $pagedb->set($pagename, false);
153
154         $this->set_links($pagename, false);
155     }
156
157     function rename_page($pagename, $to) {
158         $data = get_pagedata($pagename);
159         if (isset($data['pagename']))
160           $data['pagename'] = $to;
161         //$vdata = get_versiondata($pagename, $version, 1);
162         //$this->delete_page($pagename);
163         $this->update_pagedata($to, $data);
164         return true;
165     }
166             
167     /**
168      * Delete an old revision of a page.
169      */
170     function delete_versiondata($pagename, $version) {
171         $versdb = &$this->_versiondb;
172
173         $latest = $this->get_latest_version($pagename);
174
175         assert($version > 0);
176         assert($version <= $latest);
177         
178         $versdb->set((int)$version . ":$pagename", false);
179
180         if ($version == $latest) {
181             $previous = $this->get_previous_version($version);
182             if ($previous> 0) {
183                 $pvdata = $this->get_versiondata($pagename, $previous);
184                 $is_empty = empty($pvdata['%content']);
185             }
186             else
187                 $is_empty = true;
188             $this->_update_latest_version($pagename, $previous, $is_empty);
189         }
190     }
191
192     /**
193      * Create a new revision of a page.
194      */
195     function set_versiondata($pagename, $version, $data) {
196         $versdb = &$this->_versiondb;
197
198         $versdb->set((int)$version . ":$pagename", serialize($data));
199         if ($version > $this->get_latest_version($pagename))
200             $this->_update_latest_version($pagename, $version, empty($data['%content']));
201     }
202
203     function _update_latest_version($pagename, $latest, $flags) {
204         $pagedb = &$this->_pagedb;
205
206         $pdata = $pagedb->get($pagename);
207         if ($pdata)
208             list(,,$pagedata) = explode(':',$pdata,3);
209         else
210             $pagedata = serialize(array());
211         
212         $pagedb->set($pagename, (int)$latest . ':' . (int)$flags . ":$pagedata");
213     }
214
215     //FIXME: support limit
216     function get_all_pages($include_empty = false, $sortby=false, $limit=false) {
217         $pagedb = &$this->_pagedb;
218         $pages = array();
219         for ($page = $pagedb->firstkey(); $page!== false; $page = $pagedb->nextkey()) {
220             if (!$page) {
221                 assert(!empty($page));
222                 continue;
223             }
224             
225             if (!$include_empty) {
226                 if (!($data = $pagedb->get($page))) continue;
227                 list($latestversion,$flags,) = explode(':', $data, 3);
228                 unset($data);
229                 if ($latestversion == 0 || $flags != 0)
230                     continue;   // current content is empty 
231             }
232             $pages[] = $page;
233         }
234         $sortby = $this->sortby($sortby, 'db');
235         if ($sortby and !strstr($sortby, "hits ")) { // check for which column to sortby
236             usort($pages, 'WikiDB_backend_dbaBase_sortby_'.str_replace(' ','_',$sortby));
237         }
238         return new WikiDB_backend_dbaBase_pageiter($this, $pages);
239     }
240
241     function set_links($pagename, $links) {
242         $this->_linkdb->set_links($pagename, $links);
243     }
244
245     function get_links($pagename, $reversed = true) {
246         /*
247         if ($reversed) {
248             include_once('lib/WikiDB/backend/dumb/BackLinkIter.php');
249             $pages = $this->get_all_pages();
250             return new WikiDB_backend_dumb_BackLinkIter($this, $pages, $pagename);
251         }
252         */
253         $links = $this->_linkdb->get_links($pagename, $reversed);
254         return new WikiDB_backend_dbaBase_pageiter($this, $links);
255     }
256 };
257
258 function WikiDB_backend_dbaBase_sortby_pagename_ASC ($a, $b) {
259     return strcasecmp($a, $b);
260 }
261 function WikiDB_backend_dbaBase_sortby_pagename_DESC ($a, $b) {
262     return strcasecmp($b, $a);
263 }
264 function WikiDB_backend_dbaBase_sortby_mtime_ASC ($a, $b) {
265     return WikiDB_backend_dbaBase_sortby_num($a, $b, 'mtime');
266 }
267 function WikiDB_backend_dbaBase_sortby_mtime_DESC ($a, $b) {
268     return WikiDB_backend_dbaBase_sortby_num($b, $a, 'mtime');
269 }
270 /*
271 function WikiDB_backend_dbaBase_sortby_hits_ASC ($a, $b) {
272     return WikiDB_backend_dbaBase_sortby_num($a, $b, 'hits');
273 }
274 function WikiDB_backend_dbaBase_sortby_hits_DESC ($a, $b) {
275     return WikiDB_backend_dbaBase_sortby_num($b, $a, 'hits');
276 }
277 */
278 function WikiDB_backend_dbaBase_sortby_num($aname, $bname, $field) {
279     global $request;
280     $dbi = $request->getDbh();
281     // fields are stored in versiondata
282     $av = $dbi->_backend->get_latest_version($aname);
283     $bv = $dbi->_backend->get_latest_version($bname);
284     $a = $dbi->_backend->get_versiondata($aname, $av, false);
285     if (!$a) return 0;
286     $b = $dbi->_backend->get_versiondata($bname, $bv, false);
287     if (!$b) return 0;
288     if ((!isset($a[$field]) && !isset($b[$field])) || ($a[$field] === $b[$field])) {
289         return 0; 
290     } else {
291         return (!isset($a[$field]) || ($a[$field] < $b[$field])) ? -1 : 1;
292     }
293 }
294
295 class WikiDB_backend_dbaBase_pageiter
296 extends WikiDB_backend_iterator
297 {
298     function WikiDB_backend_dbaBase_pageiter(&$backend, &$pages) {
299         $this->_backend = $backend;
300         $this->_pages = $pages ? $pages : array();
301     }
302
303     function next() {
304         if ( ! ($next = array_shift($this->_pages)) )
305             return false;
306         return array('pagename' => $next);
307     }
308             
309     function count() {
310         return count($this->_pages);
311     }
312
313     function free() {
314         $this->_pages = array();
315     }
316 };
317
318 class WikiDB_backend_dbaBase_linktable 
319 {
320     function WikiDB_backend_dbaBase_linktable(&$dba) {
321         $this->_db = &$dba;
322     }
323
324     //FIXME: try stroring link lists as hashes rather than arrays.
325     // (backlink deletion would be faster.)
326     
327     function get_links($page, $reversed = true) {
328         return $this->_get_links($reversed ? 'i' : 'o', $page);
329     }
330     
331     function set_links($page, $newlinks) {
332
333         $oldlinks = $this->_get_links('o', $page);
334
335         if (!is_array($newlinks)) {
336             assert(empty($newlinks));
337             $newlinks = array();
338         }
339         else {
340             $newlinks = array_unique($newlinks);
341         }
342         sort($newlinks);
343         $this->_set_links('o', $page, $newlinks);
344
345         reset($newlinks);
346         reset($oldlinks);
347         $new = current($newlinks);
348         $old = current($oldlinks);
349         while ($new !== false || $old !== false) {
350             if ($old === false || ($new !== false && $new < $old)) {
351                 // $new is a new link (not in $oldlinks).
352                 $this->_add_backlink($new, $page);
353                 $new = next($newlinks);
354             }
355             elseif ($new === false || $old < $new) {
356                 // $old is a obsolete link (not in $newlinks).
357                 $this->_delete_backlink($old, $page);
358                 $old = next($oldlinks);
359             }
360             else {
361                 // Unchanged link (in both $newlist and $oldlinks).
362                 assert($new == $old);
363                 $new = next($newlinks);
364                 $old = next($oldlinks);
365             }
366         }
367     }
368
369     /**
370      * Rebuild the back-link index.
371      *
372      * This should never be needed, but if the database gets hosed for some reason,
373      * this should put it back into a consistent state.
374      *
375      * We assume the forward links in the our table are correct, and recalculate
376      * all the backlinks appropriately.
377      */
378     function rebuild () {
379         $db = &$this->_db;
380
381         // Delete the backlink tables, make a list of page names.
382         $okeys = array();
383         $ikeys = array();
384         for ($key = $db->firstkey(); $key; $key = $db->nextkey()) {
385             if ($key[0] == 'i')
386                 $ikeys[] = $key;
387             elseif ($key[0] == 'o')
388                 $okeys[] = $key;
389             else {
390                 trigger_error("Bad key in linktable: '$key'", E_USER_WARNING);
391                 $ikeys[] = $key;
392             }
393         }
394         foreach ($ikeys as $key) {
395             $db->delete($key);
396         }
397         foreach ($okeys as $key) {
398             $page = substr($key,1);
399             $links = $this->_get_links('o', $page);
400             $db->delete($key);
401             $this->set_links($page, $links);
402         }
403     }
404
405     function check() {
406         $db = &$this->_db;
407
408         // FIXME: check for sortedness and uniqueness in links lists.
409
410         for ($key = $db->firstkey(); $key; $key = $db->nextkey()) {
411             if (strlen($key) < 1 || ($key[0] != 'i' && $key[0] != 'o')) {
412                 $errs[] = "Bad key '$key' in table";
413                 continue;
414             }
415             $page = substr($key, 1);
416             if ($key[0] == 'o') {
417                 // Forward links.
418                 foreach($this->_get_links('o', $page) as $link) {
419                     if (!$this->_has_link('i', $link, $page))
420                         $errs[] = "backlink entry missing for link '$page'->'$link'";
421                 }
422             }
423             else {
424                 assert($key[0] == 'i');
425                 // Backlinks.
426                 foreach($this->_get_links('i', $page) as $link) {
427                     if (!$this->_has_link('o', $link, $page))
428                         $errs[] = "link entry missing for backlink '$page'<-'$link'";
429                 }
430             }
431         }
432
433         return isset($errs) ? $errs : false;
434     }
435     
436         
437     function _add_backlink($page, $linkedfrom) {
438         $backlinks = $this->_get_links('i', $page);
439         $backlinks[] = $linkedfrom;
440         sort($backlinks);
441         $this->_set_links('i', $page, $backlinks);
442     }
443     
444     function _delete_backlink($page, $linkedfrom) {
445         $backlinks = $this->_get_links('i', $page);
446         foreach ($backlinks as $key => $backlink) {
447             if ($backlink == $linkedfrom)
448                 unset($backlinks[$key]);
449         }
450         $this->_set_links('i', $page, $backlinks);
451     }
452     
453     function _has_link($which, $page, $link) {
454         $links = $this->_get_links($which, $page);
455         foreach($links as $l) {
456             if ($l == $link)
457                 return true;
458         }
459         return false;
460     }
461     
462     function _get_links($which, $page) {
463         $data = $this->_db->get($which . $page);
464         return $data ? unserialize($data) : array();
465     }
466
467     function _set_links($which, $page, &$links) {
468         $key = $which . $page;
469         if ($links)
470             $this->_db->set($key, serialize($links));
471         else
472             $this->_db->set($key, false);
473     }
474 }
475
476 // (c-file-style: "gnu")
477 // Local Variables:
478 // mode: php
479 // tab-width: 8
480 // c-basic-offset: 4
481 // c-hanging-comment-ender-p: nil
482 // indent-tabs-mode: nil
483 // End:   
484 ?>