1 <?php rcs_id('$Id: dbaBase.php,v 1.1 2001-09-18 19:16:23 dairiki Exp $');
3 require_once('lib/WikiDB/backend.php');
5 // FIXME:padding of data? Is it needed? dba_optimize() seems to do a good
6 // job at packing 'gdbm' (and 'db2') databases.
13 * Values: latestversion . ':' . flags . ':' serialized hash of page meta data
14 * Currently flags = 1 if latest version has empty content.
17 * Index: version:pagename
18 * Value: serialized hash of revision meta data, including:
19 * + quasi-meta-data %content
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
29 * Don't keep tables locked the whole time?
32 * list of pagenames for get_all_pages
34 * RecentChanges support:
35 * lists of most recent edits (major, minor, either).
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...).
42 require_once('lib/DbaPartition.php');
44 class WikiDB_backend_dbaBase
45 extends WikiDB_backend
47 function WikiDB_backend_dbaBase (&$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');
63 $this->_db->optimize();
71 $this->_linkdb->rebuild();
76 return $this->_linkdb->check();
79 function get_pagedata($pagename) {
80 $result = $this->_pagedb->get($pagename);
83 list(,,$packed) = explode(':', $result, 3);
84 $data = unserialize($packed);
89 function update_pagedata($pagename, $newdata) {
90 $result = $this->_pagedb->get($pagename);
92 list($latestversion,$flags,$data) = explode(':', $result, 3);
93 $data = unserialize($data);
96 $latestversion = $flags = 0;
100 foreach ($newdata as $key => $val) {
106 $this->_pagedb->set($pagename,
107 (int)$latestversion . ':'
112 function get_latest_version($pagename) {
113 return (int) $this->_pagedb->get($pagename);
116 function get_previous_version($pagename, $version) {
117 $versdb = &$this->_versiondb;
119 while (--$version > 0) {
120 if ($versdb->exists($version . ":$pagename"))
126 function get_versiondata($pagename, $version, $want_content = false) {
127 $data = $this->_versiondb->get((int)$version . ":$pagename");
128 return $data ? unserialize($data) : false;
132 * Delete page from the database.
134 function delete_page($pagename) {
135 $pagedb = &$this->_pagedb;
136 $versdb = &$this->_versiondb;
138 $version = $this->get_latest_version($pagename);
139 while ($version > 0) {
140 $versdb->set($version-- . ":$pagename", false);
142 $pagedb->set($pagename, false);
146 * Delete an old revision of a page.
148 function delete_versiondata($pagename, $version) {
149 $versdb = &$this->_versiondb;
151 $latest = $this->get_latest_version($pagename);
153 assert($version > 0);
154 assert($version <= $latest);
156 $versdb->set((int)$version . ":$pagename", false);
158 if ($version == $latest) {
159 $previous = $this->get_previous_version($version);
161 $pvdata = $this->get_versiondata($pagename, $previous);
162 $is_empty = empty($pvdata['%content']);
166 $this->_update_latest_version($pagename, $previous, $is_empty);
171 * Create a new revision of a page.
173 function set_versiondata($pagename, $version, $data) {
174 $versdb = &$this->_versiondb;
176 $versdb->set((int)$version . ":$pagename", serialize($data));
177 if ($version > $this->get_latest_version($pagename))
178 $this->_update_latest_version($pagename, $version, empty($data['%content']));
181 function _update_latest_version($pagename, $latest, $flags) {
182 $pagedb = &$this->_pagedb;
184 $pdata = $pagedb->get($pagename);
186 list(,,$pagedata) = explode(':',$pdata,3);
188 $pagedata = serialize(array());
190 $pagedb->set($pagename, (int)$latest . ':' . (int)$flags . ":$pagedata");
193 function get_all_pages($include_deleted = false) {
194 $pagedb = &$this->_pagedb;
197 for ($page = $pagedb->firstkey(); $page!== false; $page = $pagedb->nextkey()) {
199 assert(!empty($page));
203 if (!$include_deleted) {
204 list($latestversion,$flags,) = explode(':', $pagedb->get($page), 3);
205 if ($latestversion == 0 || $flags != 0)
206 continue; // current content is empty
210 usort($pages, 'WikiDB_backend_dbaBase_sortbypagename');
211 return new WikiDB_backend_dbaBase_pageiter($this, $pages);
214 function set_links($pagename, $links) {
215 $this->_linkdb->set_links($pagename, $links);
219 function get_links($pagename, $reversed = true) {
222 include_once('lib/WikiDB/backend/dumb/BackLinkIter.php');
223 $pages = $this->get_all_pages();
224 return new WikiDB_backend_dumb_BackLinkIter($this, $pages, $pagename);
227 $links = $this->_linkdb->get_links($pagename, $reversed);
228 return new WikiDB_backend_dbaBase_pageiter($this, $links);
232 function WikiDB_backend_dbaBase_sortbypagename ($a, $b) {
233 $aname = $a['pagename'];
234 $bname = $b['pagename'];
235 return strcasecmp($aname, $bname);
239 class WikiDB_backend_dbaBase_pageiter
240 extends WikiDB_backend_iterator
242 function WikiDB_backend_dbaBase_pageiter(&$backend, &$pages) {
243 $this->_backend = $backend;
244 $this->_pages = $pages ? $pages : array();
248 if ( ! ($next = array_shift($this->_pages)) )
250 return array('pagename' => $next);
254 $this->_pages = array();
258 class WikiDB_backend_dbaBase_linktable
260 function WikiDB_backend_dbaBase_linktable(&$dba) {
264 //FIXME: try stroring link lists as hashes rather than arrays.
265 // (backlink deletion would be faster.)
267 function get_links($page, $reversed = true) {
268 return $this->_get_links($reversed ? 'i' : 'o', $page);
271 function set_links($page, $newlinks) {
273 $oldlinks = $this->_get_links('o', $page);
275 if (!is_array($newlinks)) {
276 assert(empty($newlinks));
280 $newlinks = array_unique($newlinks);
283 $this->_set_links('o', $page, $newlinks);
287 $new = current($newlinks);
288 $old = current($oldlinks);
289 while ($new !== false || $old !== false) {
290 if ($old === false || ($new !== false && $new < $old)) {
291 // $new is a new link (not in $oldlinks).
292 $this->_add_backlink($new, $page);
293 $new = next($newlinks);
295 elseif ($new === false || $old < $new) {
296 // $old is a obsolete link (not in $newlinks).
297 $this->_delete_backlink($old, $page);
298 $old = next($oldlinks);
301 // Unchanged link (in both $newlist and $oldlinks).
302 assert($new == $old);
303 $new = next($newlinks);
304 $old = next($oldlinks);
310 * Rebuild the back-link index.
312 * This should never be needed, but if the database gets hosed for some reason,
313 * this should put it back into a consistent state.
315 * We assume the forward links in the our table are correct, and recalculate
316 * all the backlinks appropriately.
318 function rebuild () {
321 // Delete the backlink tables, make a list of page names.
324 for ($key = $db->firstkey(); $key; $key = $db->nextkey()) {
327 elseif ($key[0] == 'o')
330 trigger_error("Bad key in linktable: '$key'", E_USER_WARNING);
334 foreach ($ikeys as $key) {
337 foreach ($okeys as $key) {
338 $page = substr($key,1);
339 $links = $this->_get_links('o', $page);
341 $this->set_links($page, $links);
348 // FIXME: check for sortedness and uniqueness in links lists.
350 for ($key = $db->firstkey(); $key; $key = $db->nextkey()) {
351 if (strlen($key) < 1 || ($key[0] != 'i' && $key[0] != 'o')) {
352 $errs[] = "Bad key '$key' in table";
355 $page = substr($key, 1);
356 if ($key[0] == 'o') {
358 foreach($this->_get_links('o', $page) as $link) {
359 if (!$this->_has_link('i', $link, $page))
360 $errs[] = "backlink entry missing for link '$page'->'$link'";
364 assert($key[0] == 'i');
366 foreach($this->_get_links('i', $page) as $link) {
367 if (!$this->_has_link('o', $link, $page))
368 $errs[] = "link entry missing for backlink '$page'<-'$link'";
373 return isset($errs) ? $errs : false;
377 function _add_backlink($page, $linkedfrom) {
378 $backlinks = $this->_get_links('i', $page);
379 $backlinks[] = $linkedfrom;
381 $this->_set_links('i', $page, $backlinks);
384 function _delete_backlink($page, $linkedfrom) {
385 $backlinks = $this->_get_links('i', $page);
386 foreach ($backlinks as $key => $backlink) {
387 if ($backlink == $linkedfrom)
388 unset($backlinks[$key]);
390 $this->_set_links('i', $page, $backlinks);
393 function _has_link($which, $page, $link) {
394 $links = $this->_get_links($which, $page);
395 foreach($links as $l) {
402 function _get_links($which, $page) {
403 $data = $this->_db->get($which . $page);
404 return $data ? unserialize($data) : array();
407 function _set_links($which, $page, &$links) {
408 $key = $which . $page;
410 $this->_db->set($key, serialize($links));
412 $this->_db->set($key, false);
416 // (c-file-style: "gnu")
421 // c-hanging-comment-ender-p: nil
422 // indent-tabs-mode: nil