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