Writeup.pm

filter: Statementstmt 13.3% 22 / 165 Branchbran 0.0% 0 / 104 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 21.4% 6 / 28 Podpod 82.3% 14 / 17 totaltotal 12.4% 42 / 337 CC 59 SCAR 39.4
CC
59
Cov
8%
vote
110.0
update
110.0
reply
90.0
CRAP 51.6
« UserSearch.pm ↑ Summary Node.pm »
1 # E2::Writeup
2 # Jose M. Weeks <jose@joseweeks.com>
3 # 23 June 2003
4 #
5 # See bottom for pod documentation.
6  
7 package E2::Writeup;
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 2 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use E2::Node;
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  
25 sub clear;
26  
27 sub wrtype;
28 sub parent;
29 sub parent_id;
30 sub marked;
31 sub rep;
32 sub text;
33 sub cools;
34 sub cool_count;
35  
36 sub cool;
37  
38 sub update;
39  
40 # Private
41  
42 sub type_as_string;
43 sub twig_handlers;
44 sub parse;
45  
46 # Object Methods
47  
48 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
49 0 my $arg = shift;
50 0 ▶ my $class = ref( $arg ) || $arg;
Condition0%0/3
ref $arg || $arg
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $arg || $arg
ref $arg $arg
Truth table0%0/3
A || B
A= ref $argB= $arg
ABresult
1X1
011
000
51 0 my $self = $class->SUPER::new();
52  
53 # See clear for the other members of $self
54  
55 0 $self->clear;
56 0 return $self;
57 }
58  
59 0 ▶sub clear {
Subroutine clear: not called CC=3 SCAR=24.8
Pod documented
60 0 ▶ my $self = shift or croak "Usage: clear E2WRITEUP";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: clear E2WRITEUP") 0 0 0
61  
62 0 ▶ warn "E2::Writeup::clear\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Writeup::DEBUG > 1 0 0 0
63
64 0 $self->{author} = undef;
65 0 $self->{author_id} = undef;
66 0 $self->{wrtype} = undef;
67 0 $self->{parent} = undef;
68 0 $self->{parent_id} = undef;
69 0 $self->{marked} = undef; # Marked for destruction
70 0 $self->{text} = undef;
71 0 $self->{cool_count} = 0;
72  
73 0 $self->{rep} = {}; # Hash with the following keys:
74 # o up
75 # o down
76 # o total
77 # o cast
78  
79 0 @{$self->{cools} } = (); # List of cools, each a hashref
80 # with the following keys:
81 # o name # username of C!er
82 # o id # user_id of C!er
83  
84 # Now clear parent
85  
86 0 return $self->SUPER::clear;
87 }
88  
89 0 ▶sub type_as_string {
Subroutine type_as_string: not called CC=1 SCAR=6.9
Pod undocumented
90 0 return 'writeup';
91 }
92  
93 0 ▶sub parse {
Subroutine parse: not called CC=8 SCAR=42.8
Pod undocumented
94 0 ▶ my $self = shift or croak "Usage: parse E2WRITEUP, TWIG";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: parse E2WRITEUP, TWIG") 0 0 0
95 0 ▶ my $b = shift or croak "Usage: parse E2WRITEUP, TWIG";
Branch: 1 branch
BranchTrueFalseTotal
my $b = shift() or croak("Usage: parse E2WRITEUP, TWIG") 0 0 0
96  
97 0 ▶ warn "E2::Writeup::parse\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Writeup::DEBUG > 1 0 0 0
98  
99 # $b is an XML::Twig
100  
101 0 $self->{node_id} = $b->{att}->{node_id};
102 0 $self->{createtime} = $self->decode_xml( $b->{att}->{createtime} );
103 0 $self->{marked} = $b->{att}->{marked};
104 0 $self->{wrtype} = $b->first_child('writeuptype')->text;
105
106 0 my $c = $b->first_child('parent')->
107 first_child('e2link');
108  
109 0 $self->{parent} = $self->decode_xml( $c->text );
110 0 $self->{parent_id} = $c->{att}->{node_id};
111  
112 0 $self->{title} = $self->decode_xml(
113 $b->first_child('title')->text
114 );
115  
116 0 $c = $b->first_child('author');
117 0 $self->{author} = $self->decode_xml( $c->text );
118 0 $self->{author_id} = $c->{att}->{user_id};
119  
120 0 $c = $b->first_child('doctext');
121 0 ▶ if( $c ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c) 0 0 0
122 0 $self->{text} = $self->decode_xml($c->text);
123 }
124  
125 0 $c = $b->first_child('reputation');
126 0 ▶ if( $c ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c) 0 0 0
127 0 $self->{rep}->{up} = $c->{att}->{up};
128 0 $self->{rep}->{down} = $c->{att}->{down};
129 0 $self->{rep}->{cast} = $c->{att}->{cast};
130 0 $self->{rep}->{total} = $c->text;
131 }
132
133 0 @{ $self->{cools} } = ();
134 0 $self->{cool_count} = 0;
135
136 0 ▶ if( my $cools = $b->first_child('cools') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cools = $b->first_child("cools")) 0 0 0
137 0 foreach my $d ( $cools->children('e2link') ) {
138 0 push @{ $self->{cools} }, {
139 name => $self->decode_xml( $d->text ),
140 id => $d->{att}->{node_id}
141 0 };
142 0 $self->{cool_count}++;
143 }
144 }
145  
146 0 return 1;
147 }
148  
149 0 ▶sub twig_handlers {
Subroutine twig_handlers: not called CC=2 SCAR=17.9
Pod undocumented
150 0 ▶ my $self = shift or croak "Usage: twig_handlers E2WRITEUP";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: twig_handlers E2WRITEUP") 0 0 0
151  
152 return (
153 0 ▶ 'writeup' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
154 0 (my $a, my $b) = @_;
155 0 $self->parse( $b );
156 }
157 0 );
158 }
159  
160 0 ▶sub cool {
Subroutine cool: not called CC=8 SCAR=42.8
Pod documented
161 0 ▶ my $self = shift or croak "Usage: cool E2WRITEUP [, NODE_ID ]";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: cool E2WRITEUP [, NODE_ID ]") 0 0 0
162 0 ▶ my $node_id = shift || $self->node_id;
Condition0%0/3
shift() || $self->node_id
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->node_id
shift() $self->node_id
Truth table0%0/3
A || B
A= shift()B= $self->node_id
ABresult
1X1
011
000
163  
164 0 ▶ warn "E2::Writeup::cool\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Writeup::DEBUG > 1 0 0 0
165  
166 0 ▶ if( !$self->logged_in ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->logged_in) 0 0 0
167 0 ▶ warn "Unable to cool: not logged in" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Writeup::DEBUG 0 0 0
168 0 return undef;
169 }
170 0 ▶ if( !$node_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node_id) 0 0 0
171 0 ▶ warn "Unable to cool: no node specified" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Writeup::DEBUG 0 0 0
172 0 return undef;
173 }
174  
175 return $self->thread_then(
176 [
177 \&E2::Interface::process_request,
178 $self,
179 node_id => $node_id,
180 op => "cool",
181 displaytype => "xmltrue"
182 ],
183 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
184 # FIXME: add check
185 0 return 1;
186 0 });
187 }
188  
189 0 ▶sub vote {
Subroutine vote: not called CC=10 SCAR=47.0
Pod documented
190 0 my ($self, $vote) = @_;
191
192 0 ▶ if( $vote != -1 && $vote != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vote != -1 and $vote != 1) 0 0 0
Condition0%0/3
$vote != -1 and $vote != 1
!ll&&!rl&&r
000
MC/DC0%0/2
$vote != -1 and $vote != 1
$vote != -1 $vote != 1
Truth table0%0/3
A and B
A= $vote != -1B= $vote != 1
ABresult
0X0
100
111
193 0 croak "Usage: vote E2WRITEUP, -1 | 1";
194 }
195  
196 0 ▶ warn "E2::Writeup::vote\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Writeup::DEBUG > 1 0 0 0
197
198 0 ▶ if( !$self->logged_in ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->logged_in) 0 0 0
199 0 ▶ warn "Unable to vote: not logged in" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Writeup::DEBUG 0 0 0
200 0 return undef;
201 }
202  
203 0 ▶ if( $self->this_user_id == $self->author_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->this_user_id == $self->author_id) 0 0 0
204 0 ▶ warn "Unable to vote on your own writeup" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Writeup::DEBUG 0 0 0
205 0 return undef;
206 }
207  
208 0 ▶ if( $self->rep->{cast} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rep->{"cast"}) 0 0 0
209 0 ▶ warn "Unable to vote on a writeup more than once" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Writeup::DEBUG 0 0 0
210 0 return undef;
211 }
212  
213 0 my %req = (
214 node_id => $self->node_id,
215 op => 'vote',
216 displaytype => 'xmltrue',
217 'vote__' . $self->node_id => $vote
218 );
219  
220 return $self->thread_then(
221 [
222 \&E2::Interface::process_request,
223 $self,
224 %req
225 ],
226 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
227 0 my $r = shift;
228  
229 # if( !($r =~ /<node /s ) ) {
230 # return undef;
231 # }
232  
233 # Parse, and if it parses, return rep->{cast}.
234  
235 0 ▶ return undef if ! $self->load_from_xml( $r );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->load_from_xml($r) 0 0 0
236 0 ▶ return $self->rep->{cast} || 0;
Condition0%0/2
$self->rep->{'cast'} || 0
l!l
00
MC/DC0%0/1
$self->rep->{'cast'} || 0
$self->rep->{'cast'}
Truth table0%0/2
A
A= $self->rep->{'cast'}
Aresult
11
00
237 0 });
238 }
239  
240 0 ▶sub reply {
Subroutine reply: not called CC=9 SCAR=45.0
Pod documented
241 0 ▶ my $self = shift or croak "Usage: reply E2WRITEUP, TEXT [, CC ]";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: reply E2WRITEUP, TEXT [, CC ]") 0 0 0
242 0 ▶ my $text = shift or croak "Usage: reply E2WRITEUP, TEXT [, CC ]";
Branch: 1 branch
BranchTrueFalseTotal
my $text = shift() or croak("Usage: reply E2WRITEUP, TEXT [, CC ]") 0 0 0
243 0 my $cc = shift;
244
245 0 ▶ warn "E2::Writeup::reply\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Writeup::DEBUG > 1 0 0 0
246  
247 0 ▶ if( !$self->logged_in ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->logged_in) 0 0 0
248 0 ▶ warn "Unable to reply: not logged in" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Writeup::DEBUG 0 0 0
249 0 return undef;
250 }
251  
252 0 ▶ if( !$self->exists ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->exists) 0 0 0
253 0 ▶ warn "Unable to reply: no writeup loaded" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Writeup::DEBUG 0 0 0
254 0 return undef;
255 }
256  
257 0 my $id = $self->node_id;
258 0 my %req = (
259 node_id => $id,
260 op => 'vote',
261 "msgwuauthor_$id" => $text
262 );
263  
264 0 ▶ $req{"ccmsgwuauthor_$id"} = 1 if $cc;
Branch: 1 branch
BranchTrueFalseTotal
if $cc 0 0 0
265
266 $self->thread_then(
267 [
268 \&E2::Interface::process_request,
269 $self,
270 %req
271 ],
272 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
273 0 my $r = shift;
274  
275 # Simple test. We can't send messages if we specify
276 # displaytype=xmltrue, so we're stuck with the HTML
277 # page. Hopefully any page formatting/theme issues
278 # won't break this if we keep it small.
279  
280 0 ▶ if( ($r =~ /\(sent writeup message/s) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($r =~ /\(sent writeup message/s and $r =~ /you said "re/s) 0 0 0
Condition0%0/3
$r =~ /\(sent writeup message/s and $r =~ /you said "re/s
!ll&&!rl&&r
000
MC/DC0%0/2
$r =~ /\(sent writeup message/s and $r =~ /you said "re/s
$r =~ /\(sent writeup message/s $r =~ /you said "re/s
Truth table0%0/3
A and B
A= $r =~ /\(sent writeup message/sB= $r =~ /you said "re/s
ABresult
0X0
100
111
281 ($r =~ /you said "re/s) ) {
282 0 return 1;
283 }
284  
285 0 return 0;
286 0 });
287 }
288
289 0 ▶sub update {
Subroutine update: not called CC=10 SCAR=47.0
Pod documented
290 0 ▶ my $self = shift or croak "Usage: update_writeup E2WRITEUP, TEXT [ , TYPE ]";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: update_writeup E2WRITEUP, TEXT [ , TYPE ]") 0 0 0
291 0 ▶ my $text = shift or croak "Usage: update_writeup E2WRITEUP, TEXT [ , TYPE ]";
Branch: 1 branch
BranchTrueFalseTotal
my $text = shift() or croak("Usage: update_writeup E2WRITEUP, TEXT [ , TYPE ]") 0 0 0
292 0 my $type_s = shift;
293 0 my $type;
294  
295 0 ▶ warn "E2::Writeup::update\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Writeup::DEBUG > 1 0 0 0
296  
297 # Translate type to code
298  
299 0 my %h = ( person => 249,
300 thing => 250,
301 idea => 251,
302 place => 252 );
303  
304 # Make sure we are logged-in and this is our writeup
305  
306 0 ▶ if( !$self->logged_in ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->logged_in) 0 0 0
307 0 ▶ warn "Unable to update: not logged in" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Writeup::DEBUG 0 0 0
308 0 return undef;
309 }
310  
311 0 ▶ if( lc($self->this_username) ne lc($self->author) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $self->this_username ne lc $self->author) 0 0 0
312 0 ▶ warn "Unable to update: not your writeup" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Writeup::DEBUG 0 0 0
313 0 return undef;
314 }
315  
316 0 ▶ if( !$type_s ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type_s) 0 0 0
317 0 $type_s = $self->wrtype;
318 }
319
320 0 $type = $h{ lc( $type_s ) };
321 0 ▶ if( !$type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 0 0
322 0 croak "Invalid type: $type_s";
323 }
324  
325 # Request
326  
327 $self->thread_then(
328 [
329 \&E2::Interface::process_request,
330 $self,
331 node_id => $self->node_id,
332 writeup_wrtype_writeuptype => $type,
333 displaytype => "xmltrue",
334 sexisgood => "submit",
335 writeup_doctext => $text
336 ],
337 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
338 0 my $r = shift;
339  
340 0 ▶ if( !($r =~ /<node /s ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r =~ /<node /s) 0 0 0
341 0 return undef;
342 }
343  
344 0 return $self->load_from_xml( $r );
345 0 });
346 }
347  
348 #---------------
349 # Access Methods
350 #---------------
351  
352 0 ▶sub wrtype {
Subroutine wrtype: not called CC=2 SCAR=17.9
Pod documented
353 0 ▶ my $self = shift or croak "Usage: wrtype E2WRITEUP";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: wrtype E2WRITEUP") 0 0 0
354 0 return $self->{wrtype};
355 }
356  
357 0 ▶sub parent {
Subroutine parent: not called CC=2 SCAR=17.9
Pod documented
358 0 ▶ my $self = shift or croak "Usage: parent E2WRITEUP";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: parent E2WRITEUP") 0 0 0
359 0 return $self->{parent};
360 }
361  
362 0 ▶sub parent_id {
Subroutine parent_id: not called CC=2 SCAR=17.9
Pod documented
363 0 ▶ my $self = shift or croak "Usage: parent_id E2WRITEUP";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: parent_id E2WRITEUP") 0 0 0
364 0 return $self->{parent_id};
365 }
366  
367 0 ▶sub marked {
Subroutine marked: not called CC=2 SCAR=17.9
Pod documented
368 0 ▶ my $self = shift or croak "Usage: marked E2WRITEUP";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: marked E2WRITEUP") 0 0 0
369 0 return $self->{marked};
370 }
371  
372 0 ▶sub rep {
Subroutine rep: not called CC=2 SCAR=17.9
Pod documented
373 0 ▶ my $self = shift or croak "Usage: rep E2WRITEUP";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: rep E2WRITEUP") 0 0 0
374  
375 0 return $self->{rep};
376 }
377  
378 0 ▶sub text {
Subroutine text: not called CC=2 SCAR=17.9
Pod documented
379 0 ▶ my $self = shift or croak "Usage: text E2WRITEUP";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: text E2WRITEUP") 0 0 0
380 0 return $self->{text};
381 }
382  
383 0 ▶sub cool_count {
Subroutine cool_count: not called CC=2 SCAR=17.9
Pod documented
384 0 ▶ my $self = shift or croak "Usage: cool_count E2WRITEUP";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: cool_count E2WRITEUP") 0 0 0
385 0 return $self->{cool_count};
386 }
387  
388 0 ▶sub cools {
Subroutine cools: not called CC=3 SCAR=24.8
Pod documented
389 0 ▶ my $self = shift or croak "Usage: cools E2WRITEUP";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: cools E2WRITEUP") 0 0 0
390 0 ▶ return () if ! defined $self->{cools};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"cools"} 0 0 0
391  
392 0 return @{ $self->{cools} };
393 }
394  
395 2 1;
396 __END__
« UserSearch.pm ↑ Summary Node.pm »