]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/DbaListSet.php
Allow bold, italics or underlined for numbers
[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     function next($i)
51     {
52         list(, $next,) = explode(':', $this->_dbh->fetch(intval($i)), 3);
53         return intval($next);
54     }
55
56     function prev(&$i)
57     {
58         list($prev, ,) = explode(':', $this->_dbh->fetch(intval($i)), 3);
59         return intval($prev);
60     }
61
62     function exists($i)
63     {
64         $i = intval($i);
65         return $i && $this->_dbh->exists($i);
66     }
67
68     function fetch($i)
69     {
70         list(, , $data) = explode(':', $this->_dbh->fetch(intval($i)), 3);
71         return $data;
72     }
73
74     function replace($i, $data)
75     {
76         $dbh = &$this->_dbh;
77         list($prev, $next,) = explode(':', $dbh->fetch(intval($i)), 3);
78         $dbh->replace($i, "$prev:$next:$data");
79     }
80
81     function insert_before($i, $data)
82     {
83         assert(intval($i));
84         return $this->_insert_before_nc($i, $data);
85     }
86
87     function insert_after($i, $data)
88     {
89         assert(intval($i));
90         return $this->_insert_after_nc($i, $data);
91     }
92
93     function append($seq, $data)
94     {
95         $key = "s" . urlencode($seq);
96         $this->_insert_before_nc($key, $data);
97     }
98
99     function prepend($seq, $data)
100     {
101         $key = "s" . urlencode($seq);
102         $this->_insert_after_nc($key, $data);
103     }
104
105     function _insert_before_nc($i, &$data)
106     {
107         $newkey = $this->_new_key();
108         $old_prev = $this->_setprev($i, $newkey);
109         $this->_setnext($old_prev, $newkey);
110         $this->_dbh->insert($newkey, "$old_prev:$i:$data");
111         return $newkey;
112     }
113
114     function _insert_after_nc($i, &$data)
115     {
116         $newkey = $this->_new_key();
117         $old_next = $this->_setnext($i, $newkey);
118         $this->_setprev($old_next, $newkey);
119         $this->_dbh->insert($newkey, "$i:$old_next:$data");
120         return $newkey;
121     }
122
123     function delete($i)
124     {
125         $dbh = &$this->_dbh;
126         list($prev, $next) = explode(':', $dbh->fetch(intval($i)), 3);
127         $this->_setnext($prev, $next);
128         $this->_setprev($next, $prev);
129         $dbh->delete(intval($i));
130     }
131
132     function _new_key()
133     {
134         $dbh = &$this->_dbh;
135         $new_key = $dbh->fetch('max_key') + 1;
136         $dbh->replace('max_key', $new_key);
137         return $new_key;
138     }
139
140     function _setprev($i, $new_prev)
141     {
142         $dbh = &$this->_dbh;
143         list($old_prev, $next, $data) = explode(':', $dbh->fetch($i), 3);
144         $dbh->replace($i, "$new_prev:$next:$data");
145         return $old_prev;
146     }
147
148     function _setnext($i, $new_next)
149     {
150         $dbh = &$this->_dbh;
151         list($prev, $old_next, $data) = explode(':', $dbh->fetch($i), 3);
152         $dbh->replace($i, "$prev:$new_next:$data");
153         return $old_next;
154     }
155 }
156
157 // Local Variables:
158 // mode: php
159 // tab-width: 8
160 // c-basic-offset: 4
161 // c-hanging-comment-ender-p: nil
162 // indent-tabs-mode: nil
163 // End: