File Coverage

blib/lib/E2/Writeup.pm
Criterion Covered Total %
statement 18 161 11.1
branch 0 104 0.0
condition 0 14 0.0
subroutine 6 28 21.4
pod 14 17 82.3
total 38 324 11.7


line stmt bran cond sub pod time code
1             # E2::Writeup
2             # Jose M. Weeks
3             # 23 June 2003
4             #
5             # See bottom for pod documentation.
6              
7             package E2::Writeup;
8              
9 2     2   51 use 5.006;
  2         5  
  2         65  
10 2     2   9 use strict;
  2         4  
  2         43  
11 2     2   9 use warnings;
  2         2  
  2         43  
12 2     2   9 use Carp;
  2         4  
  2         135  
13 2     2   1599 use HTML::Entities;
  2         20375  
  2         224  
14              
15 2     2   19 use E2::Node;
  2         5  
  2         11508  
16              
17             our @ISA = "E2::Node";
18             our $VERSION = "0.33";
19             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             sub new {
49 0     0 1   my $arg = shift;
50 0   0       my $class = ref( $arg ) || $arg;
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             sub clear {
60 0 0   0 1   my $self = shift or croak "Usage: clear E2WRITEUP";
61              
62 0 0         warn "E2::Writeup::clear\n" if $DEBUG > 1;
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
  0            
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             sub type_as_string {
90 0     0 0   return 'writeup';
91             }
92              
93             sub parse {
94 0 0   0 0   my $self = shift or croak "Usage: parse E2WRITEUP, TWIG";
95 0 0         my $b = shift or croak "Usage: parse E2WRITEUP, TWIG";
96              
97 0 0         warn "E2::Writeup::parse\n" if $DEBUG > 1;
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 0         if( $c ) {
122 0           $self->{text} = $self->decode_xml($c->text);
123             }
124              
125 0           $c = $b->first_child('reputation');
126 0 0         if( $c ) {
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} } = ();
  0            
134 0           $self->{cool_count} = 0;
135            
136 0 0         if( my $cools = $b->first_child('cools') ) {
137 0           foreach my $d ( $cools->children('e2link') ) {
138 0           push @{ $self->{cools} }, {
  0            
139             name => $self->decode_xml( $d->text ),
140             id => $d->{att}->{node_id}
141             };
142 0           $self->{cool_count}++;
143             }
144             }
145              
146 0           return 1;
147             }
148              
149             sub twig_handlers {
150 0 0   0 0   my $self = shift or croak "Usage: twig_handlers E2WRITEUP";
151              
152             return (
153             'writeup' => sub {
154 0     0     (my $a, my $b) = @_;
155 0           $self->parse( $b );
156             }
157 0           );
158             }
159              
160             sub cool {
161 0 0   0 1   my $self = shift or croak "Usage: cool E2WRITEUP [, NODE_ID ]";
162 0   0       my $node_id = shift || $self->node_id;
163              
164 0 0         warn "E2::Writeup::cool\n" if $DEBUG > 1;
165              
166 0 0         if( !$self->logged_in ) {
167 0 0         warn "Unable to cool: not logged in" if $DEBUG;
168 0           return undef;
169             }
170 0 0         if( !$node_id ) {
171 0 0         warn "Unable to cool: no node specified" if $DEBUG;
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             sub {
184             # FIXME: add check
185 0     0     return 1;
186 0           });
187             }
188              
189             sub vote {
190 0     0 1   my ($self, $vote) = @_;
191            
192 0 0 0       if( $vote != -1 && $vote != 1 ) {
193 0           croak "Usage: vote E2WRITEUP, -1 | 1";
194             }
195              
196 0 0         warn "E2::Writeup::vote\n" if $DEBUG > 1;
197            
198 0 0         if( !$self->logged_in ) {
199 0 0         warn "Unable to vote: not logged in" if $DEBUG;
200 0           return undef;
201             }
202              
203 0 0         if( $self->this_user_id == $self->author_id ) {
204 0 0         warn "Unable to vote on your own writeup" if $DEBUG;
205 0           return undef;
206             }
207              
208 0 0         if( $self->rep->{cast} ) {
209 0 0         warn "Unable to vote on a writeup more than once" if $DEBUG;
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             sub {
227 0     0     my $r = shift;
228              
229             # if( !($r =~ /
230             # return undef;
231             # }
232              
233             # Parse, and if it parses, return rep->{cast}.
234              
235 0 0         return undef if ! $self->load_from_xml( $r );
236 0   0       return $self->rep->{cast} || 0;
237 0           });
238             }
239              
240             sub reply {
241 0 0   0 1   my $self = shift or croak "Usage: reply E2WRITEUP, TEXT [, CC ]";
242 0 0         my $text = shift or croak "Usage: reply E2WRITEUP, TEXT [, CC ]";
243 0           my $cc = shift;
244            
245 0 0         warn "E2::Writeup::reply\n" if $DEBUG > 1;
246              
247 0 0         if( !$self->logged_in ) {
248 0 0         warn "Unable to reply: not logged in" if $DEBUG;
249 0           return undef;
250             }
251              
252 0 0         if( !$self->exists ) {
253 0 0         warn "Unable to reply: no writeup loaded" if $DEBUG;
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 0         $req{"ccmsgwuauthor_$id"} = 1 if $cc;
265            
266             $self->thread_then(
267             [
268             \&E2::Interface::process_request,
269             $self,
270             %req
271             ],
272             sub {
273 0     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 0 0       if( ($r =~ /\(sent writeup message/s) &&
281             ($r =~ /you said "re/s) ) {
282 0           return 1;
283             }
284              
285 0           return 0;
286 0           });
287             }
288            
289             sub update {
290 0 0   0 1   my $self = shift or croak "Usage: update_writeup E2WRITEUP, TEXT [ , TYPE ]";
291 0 0         my $text = shift or croak "Usage: update_writeup E2WRITEUP, TEXT [ , TYPE ]";
292 0           my $type_s = shift;
293 0           my $type;
294              
295 0 0         warn "E2::Writeup::update\n" if $DEBUG > 1;
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 0         if( !$self->logged_in ) {
307 0 0         warn "Unable to update: not logged in" if $DEBUG;
308 0           return undef;
309             }
310              
311 0 0         if( lc($self->this_username) ne lc($self->author) ) {
312 0 0         warn "Unable to update: not your writeup" if $DEBUG;
313 0           return undef;
314             }
315              
316 0 0         if( !$type_s ) {
317 0           $type_s = $self->wrtype;
318             }
319            
320 0           $type = $h{ lc( $type_s ) };
321 0 0         if( !$type ) {
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             sub {
338 0     0     my $r = shift;
339              
340 0 0         if( !($r =~ /
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             sub wrtype {
353 0 0   0 1   my $self = shift or croak "Usage: wrtype E2WRITEUP";
354 0           return $self->{wrtype};
355             }
356              
357             sub parent {
358 0 0   0 1   my $self = shift or croak "Usage: parent E2WRITEUP";
359 0           return $self->{parent};
360             }
361              
362             sub parent_id {
363 0 0   0 1   my $self = shift or croak "Usage: parent_id E2WRITEUP";
364 0           return $self->{parent_id};
365             }
366              
367             sub marked {
368 0 0   0 1   my $self = shift or croak "Usage: marked E2WRITEUP";
369 0           return $self->{marked};
370             }
371              
372             sub rep {
373 0 0   0 1   my $self = shift or croak "Usage: rep E2WRITEUP";
374              
375 0           return $self->{rep};
376             }
377              
378             sub text {
379 0 0   0 1   my $self = shift or croak "Usage: text E2WRITEUP";
380 0           return $self->{text};
381             }
382              
383             sub cool_count {
384 0 0   0 1   my $self = shift or croak "Usage: cool_count E2WRITEUP";
385 0           return $self->{cool_count};
386             }
387              
388             sub cools {
389 0 0   0 1   my $self = shift or croak "Usage: cools E2WRITEUP";
390 0 0         return () if ! defined $self->{cools};
391              
392 0           return @{ $self->{cools} };
  0            
393             }
394              
395             1;
396             __END__