E2Node.pm

filter: Statementstmt 26.9% 44 / 163 Branchbran 2.8% 3 / 104 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 31.2% 10 / 32 Podpod 88.8% 16 / 18 totaltotal 22.2% 74 / 332 CC 57 SCAR 32.7
CC
57
Cov
17%
vote
90.0
add_writeup
56.0
get_writeup_by_author
42.0
CRAP 26.2
« Scratchpad.pm ↑ Summary Ticker.pm »
1 # E2::E2Node
2 # Jose M. Weeks <jose@joseweeks.com>
3 # 03 July 2003
4 #
5 # See bottom for pod documentation.
6  
7 package E2::E2Node;
8  
9 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use E2::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use E2::Writeup;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 our @ISA = "E2::Node";
18 2 our $VERSION = "0.33";
19 2 our $DEBUG; *DEBUG = *E2::Interface::DEBUG;
20  
21 # Prototypes
22  
23 sub new;
24 sub clear;
25  
26 sub has_mine;
27 sub is_locked;
28  
29 sub list_writeups;
30 sub list_softlinks;
31 sub list_firmlinks;
32 sub list_sametitles;
33  
34 sub get_writeup;
35 sub get_writeup_by_author;
36 sub get_my_writeup;
37 sub get_writeup_number;
38 sub get_writeup_count;
39  
40 sub vote;
41  
42 sub add_writeup;
43 sub create;
44  
45 # Private
46  
47 sub type_as_string;
48 sub twig_handlers;
49  
50 # Object Methods
51  
52 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
53 1 my $arg = shift;
54 1 ▶ my $class = ref( $arg ) || $arg;
Condition33%1/3
ref $arg || $arg
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $arg || $arg
ref $arg $arg
Truth table33%1/3
A || B
A= ref $argB= $arg
ABresult
1X1
011
000
55 1 my $self = $class->SUPER::new();
56  
57 # See clear for the other members of $self
58  
59 1 $self->clear;
60
61 1 return $self;
62 }
63  
64 3 ▶sub clear {
Subroutine clear: called CC=3 SCAR=11.0
Pod documented
65 3 ▶ my $self = shift or croak "Usage: clear E2E2NODE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: clear E2E2NODE") 3 0 3
66  
67 3 ▶ warn "E2::E2Node::clear\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::E2Node::DEBUG > 1 0 3 3
68  
69 3 @{ $self->{writeups} } = (); # Array to hold writeups in currently
70 # loaded node. See E2::Writeup.
71
72 3 $self->{locked} = undef; # soft locked
73  
74 3 $self->{next} = 0; # Next writeup to return
75 3 $self->{mine} = undef; # My writeup in this node.
76  
77 3 @{ $self->{firmlinks} } = (); # List of firmlinks
78 3 @{ $self->{softlinks} } = (); # List of softlinks
79 3 @{ $self->{sametitles}} = (); # List of sametitles
80 # The preceding three return hashrefs
81 # with the following keys:
82 # o title
83 # o id
84 # sametitles has this as well:
85 # o type
86  
87 # Now clear parent
88  
89 3 return $self->SUPER::clear;
90 }
91  
92 0 ▶sub has_mine {
Subroutine has_mine: not called CC=4 SCAR=30.0
Pod documented
93 0 ▶ my $self = shift or croak "Usage: has_mine E2E2NODE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: has_mine E2E2NODE") 0 0 0
94  
95 0 ▶ if(!$self->node_id) { return undef; } # No node loaded
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->node_id) 0 0 0
96  
97 0 ▶ if( defined( $self->{mine} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"mine"}) 0 0 0
98 0 return 1;
99 }
100  
101 0 return 0;
102 }
103  
104 0 ▶sub is_locked {
Subroutine is_locked: not called CC=4 SCAR=30.0
Pod documented
105 0 ▶ my $self = shift or croak "Usage: is_locked E2E2NODE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: is_locked E2E2NODE") 0 0 0
106  
107 0 ▶ if( !$self->node_id ) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->node_id) 0 0 0
108  
109 0 ▶ if( $self->{locked} ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'locked'}) { } 0 0 0
110 0 else { return 0; }
111 }
112  
113 0 ▶sub list_softlinks {
Subroutine list_softlinks: not called CC=3 SCAR=24.8
Pod documented
114 0 ▶ my $self = shift or croak "Usage: list_softlinks E2NODE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: list_softlinks E2NODE") 0 0 0
115  
116 0 ▶ return undef if !$self->node_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->node_id 0 0 0
117
118 0 return @{ $self->{softlinks} };
119 }
120  
121 0 ▶sub list_firmlinks {
Subroutine list_firmlinks: not called CC=3 SCAR=24.8
Pod documented
122 0 ▶ my $self = shift or croak "Usage: list_firmlinks E2NODE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: list_firmlinks E2NODE") 0 0 0
123  
124 0 ▶ return undef if !$self->node_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->node_id 0 0 0
125
126 0 return @{ $self->{firmlinks} };
127 }
128  
129 0 ▶sub list_sametitles {
Subroutine list_sametitles: not called CC=3 SCAR=24.8
Pod documented
130 0 ▶ my $self = shift or croak "Usage: list_sametitles E2NODE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: list_sametitles E2NODE") 0 0 0
131  
132 0 ▶ return undef if !$self->node_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->node_id 0 0 0
133  
134 0 return @{ $self->{sametitles} };
135 }
136  
137 1 ▶sub twig_handlers {
Subroutine twig_handlers: called CC=2 SCAR=16.3
Pod undocumented
138 1 ▶ my $self = shift or croak "Usage: twig_handlers E2E2NODE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: twig_handlers E2E2NODE") 1 0 1
139  
140 return (
141  
142 0 ▶ 'node/nodelock' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
143 0 (my $a, my $b) = @_;
144 0 $self->{locked} = $b->text;
145 0 ▶ if( $self->{locked} eq "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"locked"} eq "") 0 0 0
146 0 $self->{locked} = undef;
147 }
148 },
149 0 ▶ 'node/writeup' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
150 0 (my $a, my $b) = @_;
151  
152 0 my $wu = new E2::Writeup;
153 0 $wu->clone( $self );
154 0 $wu->parse( $b );
155  
156 0 my $name = $self->this_username;
157 0 my $uid = $self->this_user_id;
158
159 0 ▶ if( $self->logged_in &&
Branch: 2 branches
BranchTrueFalseTotal
if ($self->logged_in and $uid ? $uid == $wu->author_id : lc $name eq lc $wu->author) 0 0 0
$uid ? : 0 0 0
Condition0%0/3
$self->logged_in and $uid ? $uid == $wu->author_id : lc $name eq lc $wu->author
!ll&&!rl&&r
000
MC/DC0%0/2
$self->logged_in and $uid ? $uid == $wu->author_id : lc $name eq lc $wu->author
$self->logged_in $uid ? $uid == $wu->author_id : lc $name eq lc $wu->author
Truth table0%0/3
A and B
A= $self->logged_inB= $uid ? $uid == $wu->author_id : lc $name eq lc $wu->author
ABresult
0X0
100
111
160 ($uid ? $uid == $wu->author_id :
161 lc($name) eq lc($wu->author)) ) {
162 0 $self->{mine} = @{ $self->{writeups} };
163 }
164  
165 0 push @{ $self->{writeups} }, $wu;
166 },
167 0 ▶ 'node/softlinks/e2link' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
168 0 (my $a, my $b) = @_;
169 0 push @{ $self->{softlinks} }, {
170 title => $b->text,
171 id => $b->{att}->{node_id}
172 0 };
173 },
174 0 ▶ 'node/firmlinks/e2link' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
175 0 (my $a, my $b) = @_;
176 0 push @{ $self->{firmlinks} }, {
177 title => $b->text,
178 id => $b->{att}->{node_id}
179 0 };
180 },
181 0 ▶ 'node/sametitles/nodesuggest' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
182 0 (my $a, my $b) = @_;
183 0 my $c = $b->first_child( 'e2link' );
184 0 push @{ $self->{sametitles} }, {
185 title => $c->text,
186 type => $b->{att}->{type},
187 id => $c->{att}->{node_id}
188 0 };
189 }
190 1 );
191 }
192  
193 1 ▶sub type_as_string {
Subroutine type_as_string: called CC=1 SCAR=0.0
Pod undocumented
194 1 return 'e2node';
195 }
196  
197 0 ▶sub list_writeups {
Subroutine list_writeups: not called CC=3 SCAR=24.8
Pod documented
198 0 ▶ my $self = shift or croak "Usage: list_writeups E2E2NODE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: list_writeups E2E2NODE") 0 0 0
199  
200 0 ▶ return undef if !$self->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists 0 0 0
201 0 return @{ $self->{writeups} };
202 }
203  
204 0 ▶sub get_writeup {
Subroutine get_writeup: not called CC=3 SCAR=24.8
Pod documented
205 0 ▶ my $self = shift or croak "Usage: get_writeup E2E2NODE [ , NUM ]";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: get_writeup E2E2NODE [ , NUM ]") 0 0 0
206 0 my $num = shift;
207  
208 0 ▶ if( $num ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num) 0 0 0
209 0 $self->{next} = $num;
210 }
211  
212 0 return $self->{writeups}[ $self->{next}++ ];
213 }
214  
215 0 ▶sub get_writeup_by_author {
Subroutine get_writeup_by_author: not called CC=6 SCAR=37.4
Pod documented
216 0 ▶ my $self = shift
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: get_writeup_by_author E2E2NODE, AUTHOR") 0 0 0
217 or croak "Usage: get_writeup_by_author E2E2NODE, AUTHOR";
218 0 ▶ my $author = shift
Branch: 1 branch
BranchTrueFalseTotal
my $author = shift() or croak("Usage: get_writeup_by_author E2E2NODE, AUTHOR") 0 0 0
219 or croak "Usage: get_writeup_by_author E2E2NODE, AUTHOR";
220  
221 0 ▶ if( !$self->node_id ) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->node_id) 0 0 0
222  
223 0 ▶ for( my $i = 0; $i < @{ $self->{writeups} }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"writeups"};}) 0 0 0
224 0 ▶ if( lc($author) eq
Branch: 1 branch
BranchTrueFalseTotal
if (lc $author eq lc $self->{"writeups"}[$i]->author) 0 0 0
225 lc($self->{writeups}[$i]->author) ) {
226 0 return $self->{writeups}[$i];
227 }
228 }
229  
230 0 return 0;
231 }
232  
233 0 ▶sub get_my_writeup {
Subroutine get_my_writeup: not called CC=3 SCAR=24.8
Pod documented
234 0 ▶ my $self = shift or croak "Usage: get_my_writeup E2E2NODE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: get_my_writeup E2E2NODE") 0 0 0
235 0 my $i = $self->{mine};
236  
237 0 ▶ if( ! defined $i ) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $i) 0 0 0
238  
239 0 return $self->{writeups}[$i];
240 }
241  
242 0 ▶sub get_writeup_number {
Subroutine get_writeup_number: not called CC=3 SCAR=24.8
Pod documented
243 0 ▶ my $self = shift or croak "Usage: get_writeup_number E2E2NODE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: get_writeup_number E2E2NODE") 0 0 0
244  
245 0 ▶ if( !$self->node_id ) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->node_id) 0 0 0
246 0 return $self->{next};
247 }
248  
249 0 ▶sub get_writeup_count {
Subroutine get_writeup_count: not called CC=3 SCAR=24.8
Pod documented
250 0 ▶ my $self = shift or croak "Usage: get_writeup_count E2E2NODE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: get_writeup_count E2E2NODE") 0 0 0
251  
252 0 ▶ if( !$self->node_id ) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->node_id) 0 0 0
253  
254 0 return $$self->{writeups};
255 }
256  
257 0 ▶sub create {
Subroutine create: not called CC=6 SCAR=37.4
Pod documented
258 0 ▶ my $self = shift or croak "Usage: create E2E2NODE, TITLE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: create E2E2NODE, TITLE") 0 0 0
259 0 ▶ my $title = shift or croak "Usage: create E2E2NODE, TITLE";
Branch: 1 branch
BranchTrueFalseTotal
my $title = shift() or croak("Usage: create E2E2NODE, TITLE") 0 0 0
260  
261 0 ▶ warn "E2::E2Node::create\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::E2Node::DEBUG > 1 0 0 0
262  
263 # Make sure we have username & user_id
264  
265 0 ▶ if( !$self->logged_in ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->logged_in) 0 0 0
266 0 ▶ warn "Unable to create node: not logged in" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::E2Node::DEBUG 0 0 0
267 0 return undef;
268 }
269  
270 return $self->thread_then(
271 [
272 \&E2::Interface::process_request,
273 $self,
274 node => $title,
275 op => "new",
276 type => "e2node",
277 displaytype => "xmltrue",
278 e2node_createdby_user => $self->{user_id}
279 ],
280 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
281  
282 0 my $r = shift;
283 0 ▶ if( !$r =~ /<author .*?user_id="(.*?)"/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (!$r =~ /<author .*?user_id="(.*?)"/s) 0 0 0
284 0 croak "Invalid document";
285 }
286  
287 0 $self->load_from_xml( $r );
288  
289 0 return $self->exists;
290 0 });
291 }
292  
293 # FIXME: Allow multiple votes/replies/etc. in one request.
294  
295 0 ▶sub vote {
Subroutine vote: not called CC=9 SCAR=45.0
Pod documented
296 0 ▶ my $self = shift or croak "Usage: vote E2E2NODE, NODE_ID => VOTE [ , NODE_ID2 = VOTE2 [ , ... ] ]";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: vote E2E2NODE, NODE_ID => VOTE [ , NODE_ID2 = VOTE2 [ , ... ] ]") 0 0 0
297 0 ▶ my %list = @_ or croak "Usage: vote E2E2NODE, NODE_ID => VOTE [ , NODE_ID2 = VOTE2 [ , ... ] ]";
Branch: 1 branch
BranchTrueFalseTotal
my(%list) = or croak("Usage: vote E2E2NODE, NODE_ID => VOTE [ , NODE_ID2 = VOTE2 [ , ... ] ]") 0 0 0
298  
299 0 ▶ warn "E2::E2Node::vote\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::E2Node::DEBUG > 1 0 0 0
300  
301 0 ▶ if( !$self->logged_in ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->logged_in) 0 0 0
302 0 ▶ warn "Unable to vote: not logged in" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::E2Node::DEBUG 0 0 0
303 0 return undef;
304 }
305  
306 my %params = ( node_id => $self->{node_id},
307 0 op => "vote",
308 displaytype => "xmltrue");
309  
310 0 foreach( keys %list ) {
311 0 my $v = $list{$_};
312  
313 0 ▶ if( $v != 1 && $v != -1 ) { next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($v != 1 and $v != -1) 0 0 0
Condition0%0/3
$v != 1 and $v != -1
!ll&&!rl&&r
000
MC/DC0%0/2
$v != 1 and $v != -1
$v != 1 $v != -1
Truth table0%0/3
A and B
A= $v != 1B= $v != -1
ABresult
0X0
100
111
314  
315 0 $params{ "vote__$_" } = $v;
316 }
317  
318 return $self->thread_then(
319 [
320 \&E2::Interface::process_request,
321 $self,
322 %params
323 ],
324 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
325 0 my $r = shift;
326
327 0 ▶ if( !($r =~ /<node /s ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r =~ /<node /s) 0 0 0
328 0 croak 'Invalid document';
329 }
330  
331 0 return $self->load_from_xml( $r );
332 0 });
333 }
334  
335 0 ▶sub add_writeup {
Subroutine add_writeup: not called CC=7 SCAR=40.3
Pod documented
336 0 ▶ my $self = shift
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: add_writeup E2E2NODE, TEXT, TYPE [ , NODISPLAY ]") 0 0 0
337 or croak "Usage: add_writeup E2E2NODE, TEXT, TYPE [ , NODISPLAY ]";
338 0 ▶ my $text = shift
Branch: 1 branch
BranchTrueFalseTotal
my $text = shift() or croak("Usage: add_writeup E2E2NODE, TEXT, TYPE [ , NODISPLAY ]") 0 0 0
339 or croak "Usage: add_writeup E2E2NODE, TEXT, TYPE [ , NODISPLAY ]";
340 0 ▶ my $type = shift
Branch: 1 branch
BranchTrueFalseTotal
my $type = shift() or croak("Usage: add_writeup E2E2NODE, TEXT, TYPE [ , NODISPLAY ]") 0 0 0
341 or croak "Usage: add_writeup E2E2NODE, TEXT, TYPE [ , NODISPLAY ]";
342 0 my $nodisplay = shift;
343  
344 0 ▶ warn "E2::E2Node::add_writeup\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::E2Node::DEBUG > 1 0 0 0
345  
346 0 ▶ if( !$self->logged_in ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->logged_in) 0 0 0
347 0 ▶ warn "Unable to add writeup: not logged in" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::E2Node::DEBUG 0 0 0
348 0 return undef;
349 }
350  
351 return $self->thread_then(
352 [
353 \&E2::Interface::process_request,
354 $self,
355 node => "new writeup",
356 op => "new",
357 type => "writeup",
358 node => $self->{node_id}, # Why two "node" params?
359 writeup_notnew => $nodisplay, # dunno....
360 writeup_doctext => $text,
361 writeup_parent_e2node => $self->{node_id},
362 writeuptype => $type
363 ],
364 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
365
366 # FIXME - Add code to test for success.
367  
368 0 return 1;
369 0 });
370 }
371  
372 2 1;
373 __END__
« Scratchpad.pm ↑ Summary Ticker.pm »