]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/DbaListSet.php
Remove rcs_id
[SourceForge/phpwiki.git] / lib / DbaListSet.php
1 <?php // $Id$
2
3 class DbaListSet
4 {
5     function DbaListSet(&$dbh) {
6         $this->_dbh = &$dbh;
7     }
8
9     function create_sequence($seq) {
10         $dbh = &$this->_dbh;
11
12         if (!$dbh->exists('max_key')) {
13             // echo "initializing DbaListSet";
14             // FIXME: check to see if it's really empty?
15             $dbh->insert('max_key', 0);
16         }
17
18         $key = "s" . urlencode($seq);
19         assert(intval($key) == 0 && !strstr($key, ':'));
20         if (!$dbh->exists($key))
21             $dbh->insert($key, "$key:$key:");
22     }
23
24     function delete_sequence($seq) {
25         $key = "s" . urlencode($seq);
26         for ($i = $this->firstkey($seq); $i; $i = $next) {
27             $next = $this->next($i);
28             $this->delete($i);
29         }
30         $this->_dbh->delete($key);
31     }
32
33     function firstkey($seq) {
34         $key = "s" . urlencode($seq);
35         list(, $next) =  explode(':', $this->_dbh->fetch($key), 3);
36         return intval($next);
37     }
38
39     function lastkey($seq) {
40         $key = "s" . urlencode($seq);
41         list($prev) =  explode(':', $this->_dbh->fetch($key), 3);
42         return intval($prev);
43     }
44
45
46     function next($i) {
47         list( , $next, ) = explode(':', $this->_dbh->fetch(intval($i)), 3);
48         return intval($next);
49     }
50
51     function prev(&$i) {
52         list( $prev , , ) = explode(':', $this->_dbh->fetch(intval($i)), 3);
53         return intval($prev);
54     }
55   
56     function exists($i) {
57         $i = intval($i);
58         return $i && $this->_dbh->exists($i);
59     }
60
61     function fetch($i) {
62         list(, , $data) = explode(':', $this->_dbh->fetch(intval($i)), 3);
63         return $data;
64     }
65
66     function replace($i, $data) {
67         $dbh = &$this->_dbh;
68         list($prev, $next,) = explode(':', $dbh->fetch(intval($i)), 3);
69         $dbh->replace($i, "$prev:$next:$data");
70     }
71   
72     function insert_before($i, $data) {
73         assert(intval($i));
74         return $this->_insert_before_nc($i, $data);
75     }
76
77     function insert_after($i, $data) {
78         assert(intval($i));
79         return $this->_insert_after_nc($i, $data);
80     }
81   
82     function append($seq, $data) {
83         $key = "s" . urlencode($seq);
84         $this->_insert_before_nc($key, $data);
85     }
86
87     function prepend($seq, $data) {
88         $key = "s" . urlencode($seq);
89         $this->_insert_after_nc($key, $data);
90     }
91   
92     function _insert_before_nc($i, &$data) {
93         $newkey = $this->_new_key();
94         $old_prev = $this->_setprev($i, $newkey);
95         $this->_setnext($old_prev, $newkey);
96         $this->_dbh->insert($newkey, "$old_prev:$i:$data");
97         return $newkey;
98     }
99
100     function _insert_after_nc($i, &$data) {
101         $newkey = $this->_new_key();
102         $old_next = $this->_setnext($i, $newkey);
103         $this->_setprev($old_next, $newkey);
104         $this->_dbh->insert($newkey, "$i:$old_next:$data");
105         return $newkey;
106     }
107
108     function delete($i) {
109         $dbh = &$this->_dbh;
110         list($prev, $next) = explode(':', $dbh->fetch(intval($i)), 3);
111         $this->_setnext($prev, $next);
112         $this->_setprev($next, $prev);
113         $dbh->delete(intval($i));
114     }
115
116     function _new_key() {
117         $dbh = &$this->_dbh;
118         $new_key = $dbh->fetch('max_key') + 1;
119         $dbh->replace('max_key', $new_key);
120         return $new_key;
121     }
122
123     function _setprev($i, $new_prev) {
124         $dbh = &$this->_dbh;
125         list($old_prev, $next, $data) = explode(':', $dbh->fetch($i), 3);
126         $dbh->replace($i, "$new_prev:$next:$data");
127         return $old_prev;
128     }
129
130     function _setnext($i, $new_next) {
131         $dbh = &$this->_dbh;
132         list($prev, $old_next, $data) = explode(':', $dbh->fetch($i), 3);
133         $dbh->replace($i, "$prev:$new_next:$data");
134         return $old_next;
135     }
136 }
137
138 // Local Variables:
139 // mode: php
140 // tab-width: 8
141 // c-basic-offset: 4
142 // c-hanging-comment-ender-p: nil
143 // indent-tabs-mode: nil
144 // End:
145 ?>