lib/PurpleWiki/Sequence.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/PurpleWiki/Database/User/UseMod.pm ↑ Summary lib/PurpleWiki/Parser/WikiText.pm »
1 # PurpleWiki::Sequence.pm
2 # vi:sw=4:ts=4:ai:sm:et:tw=0
3 #
4 # $Id: Sequence.pm 352 2004-05-08 22:00:33Z cdent $
5 #
6 # Copyright (c) Blue Oxen Associates 2002-2003. All rights reserved.
7 #
8 # This file is part of PurpleWiki. PurpleWiki is derived from:
9 #
10 # UseModWiki v0.92 (c) Clifford A. Adams 2000-2001
11 # AtisWiki v0.3 (c) Markus Denker 1998
12 # CVWiki CVS-patches (c) Peter Merel 1997
13 # The Original WikiWikiWeb (c) Ward Cunningham
14 #
15 # PurpleWiki is free software; you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation; either version 2 of the License, or
18 # (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the
27 # Free Software Foundation, Inc.
28 # 59 Temple Place, Suite 330
29 # Boston, MA 02111-1307 USA
30  
31 package PurpleWiki::Sequence;
32  
33 # Tool for generating PurpleWiki::Sequence numbers for use
34 # in Nids
35  
36 # $Id: Sequence.pm 352 2004-05-08 22:00:33Z cdent $
37  
38 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 17 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 0 ▶use DB_File;
Subroutine BEGIN: called CC=1 SCAR=2.6
41  
42 our $VERSION;
43 $VERSION = sprintf("%d", q$Id: Sequence.pm 352 2004-05-08 22:00:33Z cdent $ =~ /\s(\d+)\s/);
44  
45 my $ORIGIN = '0';
46 my $LOCK_WAIT = 1;
47 my $LOCK_TRIES = 5;
48  
49 sub new {
50 my $proto = shift;
51 my $datadir = shift;
52 my $remote = shift;
53 my $class = ref($proto) || $proto;
54 my $self = {};
55  
56 $self->{remote} = $remote;
57 $self->{datafile} = $datadir . '/sequence';
58 $self->{indexfile} = $datadir . '/sequence.index';
59 bless ($self, $class);
60 return $self;
61 }
62  
63 # Returns the URL associated with a nid
64 sub getURL {
65 my $self = shift;
66 my $nid = shift;
67 my $url;
68  
69 if ($self->{remote}) {
70 $url = $self->_getURLByRemote($nid);
71 } else {
72 $url = $self->_getURLByLocal($nid);
73 }
74  
75 return $url;
76 }
77  
78 # Returns the next ID in the sequence
79 sub getNext {
80 my $self = shift;
81 my $url = shift;
82 my $nid;
83  
84 if ($self->{remote}) {
85 $nid = $self->_getNidByRemote($url);
86 } else {
87 $nid = $self->_getNidByLocal($url);
88 }
89  
90 return $nid;
91 }
92  
93 sub _getNidByLocal {
94 my $self = shift;
95 my $url = shift;
96 $self->_lockFile();
97 my $value = $self->_retrieveNextValue();
98 $self->_unlockFile();
99 # update the NID to URL index
100 if ($url) {
101 $self->_updateIndex($value, $url);
102 }
103 return $value;
104 }
105  
106 sub _getNidByRemote {
107 my $self = shift;
108 my $url = shift;
109 # FIXME: think about count?
110 my $urlRequest = $self->{remote} . "/1/$url";
111 my $nid;
112  
113 use LWP::UserAgent;
114 use HTTP::Request;
115  
116 my $ua = new LWP::UserAgent(agent => ref($self));
117 my $request = new HTTP::Request('GET', $urlRequest);
118 my $result = $ua->request($request);
119  
120 if ($result->is_success()) {
121 $nid = $result->content();
122 $nid =~ s/\s+//g;
123 } else {
124 # FIXME: need something better than a die here?
125 die "unable to retrieve nid for $url ", $result->code;
126 }
127  
128 return $nid;
129 }
130  
131  
132 sub _tieIndex {
133 my $self = shift;
134 my $index = shift;
135  
136 tie %$index, 'DB_File', $self->{indexfile},
137 O_RDWR|O_CREAT, 0666, $DB_HASH or
138 die "unable to tie " . $self->{indexfile} . ' ' . $!;
139 }
140  
141 # I suspect this is expensive
142 sub _updateIndex {
143 my $self = shift;
144 my $value = shift;
145 my $url = shift;
146 my %index;
147  
148 $self->_tieIndex(\%index);
149 $index{$value} = $url;
150 untie %index;
151 }
152  
153 # look in the index for the url associated with a nid
154 sub _getURLByLocal {
155 my $self = shift;
156 my $nid = shift;
157 my %index;
158 my $url;
159  
160 $self->_tieIndex(\%index);
161 $url = $index{$nid};
162  
163 untie %index;
164  
165 return $url;
166 }
167  
168 # use LWP to search a remote index
169 sub _getURLByRemote {
170 my $self = shift;
171 my $nid = shift;
172 my $urlRequest = $self->{remote} . "/$nid";
173 my $url;
174  
175 use LWP::UserAgent;
176 use HTTP::Request;
177  
178 my $ua = new LWP::UserAgent(agent => ref($self));
179 my $request = new HTTP::Request('GET', $urlRequest);
180 my $result = $ua->request($request);
181  
182 if ($result->is_success()) {
183 $url = $result->content();
184 $url =~ s/\s+//g;
185 } else {
186 # FIXME: need something better than a die here?
187 die "unable to retrieve url for $nid: ", $result->code;
188 }
189  
190 return $url;
191 }
192  
193 sub _retrieveNextValue {
194 my $self = shift;
195  
196 my $newValue = $self->_incrementValue($self->_getCurrentValue());
197 $self->_setValue($newValue);
198 return $newValue;
199 }
200  
201 sub _setValue {
202 my $self = shift;
203 my $value = shift;
204  
205 my $fh = new IO::File;
206 if ($fh->open($self->{datafile}, 'w')) {
207 print $fh $value;
208 $fh->close();
209 } else {
210 die "unable to write value to " . $self->{datafile} . ": $!";
211 }
212 }
213  
214 sub _incrementValue {
215 my $self = shift;
216 my $oldValue = shift;
217  
218 my @oldValues = split('', $oldValue);
219 my @newValues;
220 my $carryBit = 1;
221  
222 foreach my $char (reverse(@oldValues)) {
223 if ($carryBit) {
224 my $newChar;
225 ($newChar, $carryBit) = $self->_incChar($char);
226 push(@newValues, $newChar);
227 } else {
228 push(@newValues, $char);
229 }
230 }
231 push(@newValues, '1') if ($carryBit);
232 return join('', (reverse(@newValues)));
233 }
234  
235 # FIXME: ASCII/Unicode dependent
236 sub _incChar {
237 my $self = shift;
238 my $char = shift;
239  
240 if ($char eq 'Z') {
241 return '0', 1;
242 }
243 if ($char eq '9') {
244 return 'A', 0;
245 }
246 if ($char =~ /[A-Z0-9]/) {
247 return chr(ord($char) + 1), 0;
248 }
249 }
250  
251 sub _getCurrentValue {
252 my $self = shift;
253 my $file = $self->{datafile};
254 my $value;
255  
256 if (-f $file) {
257 my $fh = new IO::File;
258 $fh->open($file) || die "Unable to open $file: $!";
259 $value = $fh->getline();
260 $fh->close();
261 } else {
262 $value = $ORIGIN;
263 }
264  
265 return $value;
266 }
267  
268 # FIXME: this should not die
269 sub _lockFile {
270 my $self = shift;
271 # use simple directory locks for ease
272 my $dir = $self->{datafile} . '.lck';
273 my $tries = 0;
274  
275 # FIXME: copied from UseMod, relies on errno
276 while (mkdir($dir, 0555) == 0) {
277 if ($! != 17) {
278 die "Unable to create locking directory $dir";
279 }
280 $tries++;
281 if ($tries > $LOCK_TRIES) {
282 die "Timeout creating locking directory $dir";
283 }
284 sleep($LOCK_WAIT);
285 }
286 }
287
288 sub _unlockFile {
289 my $self = shift;
290 my $dir = $self->{datafile} . '.lck';
291 rmdir($dir) or die "Unable to remove locking directory $dir: $!";
292 }
293  
294 1;
295 __END__
« lib/PurpleWiki/Database/User/UseMod.pm ↑ Summary lib/PurpleWiki/Parser/WikiText.pm »