Thread.pm

filter: Statementstmt 36.9% 31 / 84 Branchbran 12.5% 3 / 24 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 56.2% 9 / 16 Podpod 100% 4 / 4 totaltotal 31.7% 47 / 148 CC 19 SCAR 37.2
CC
19
Cov
28%
_commenthash
110.0
commentids
42.0
thread
6.0
CRAP 41.2
« Comment.pm ↑ Summary
1 package WWW::UsePerl::Journal::Thread;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 $VERSION = '0.15';
8  
9 #----------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 WWW::UsePerl::Journal::Thread - Handles the retrieval of UsePerl journal comment threads
14  
15 =head1 SYNOPSIS
16  
17 use WWW::UsePerl::Journal;
18 use WWW::UsePerl::Journal::Thread;
19  
20 my $journal = WWW::UsePerl::Journal->new('barbie');
21 my @entries = $journal->entryids();
22  
23 my $thread = WWW::UsePerl::Journal::Thread->new(
24 j => $journal,
25 thread => $entries[0]
26 );
27  
28 my @comments = $thread->commentids();
29 for my $id (@comments) {
30 printf "\n----\n%s [%d %s %d] %s",
31 $thread->comment($id)->subject(),
32 $thread->comment($id)->score(),
33 $thread->comment($id)->user(),
34 $thread->comment($id)->uid(),
35 $thread->comment($id)->date(),
36 $thread->comment($id)->content();
37 }
38  
39 my $threadid = $thread->thread();
40  
41 =head1 DESCRIPTION
42  
43 A collection of routines to handle the retrieval of threads from a
44 UsePerl (L<http://use.perl.org/>) journal entry.
45  
46 Using WWW::UsePerl::Journal, journal entry ids can be obtain. Each entry id
47 can be used to obtain a comment thread. Each comment property is accessed
48 via a comment object from within the thread.
49  
50 Note that as on late 2010 use.perl was decommissioned. A read-only version of
51 the site now exists on the perl.org servers, and a full database backup is
52 also available if you wish to host your own use.perl archive.
53  
54 A future edition of this distribution will allow a DBI interface to a local
55 database to retrieve journal entries.
56  
57 =cut
58  
59 # -------------------------------------
60 # Library Modules
61  
62 6 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 6 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 6 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 6 ▶use WWW::UsePerl::Journal::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 # -------------------------------------
68 # Variables
69  
70 6 ▶use constant USEPERL => 'http://use.perl.org/use.perl.org';
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 6 my %months = (
73 'January' => 1,
74 'February' => 2,
75 'March' => 3,
76 'April' => 4,
77 'May' => 5,
78 'June' => 6,
79 'July' => 7,
80 'August' => 8,
81 'September' => 9,
82 'October' => 10,
83 'November' => 11,
84 'December' => 12,
85 );
86  
87 # -------------------------------------
88 # Public Interface
89  
90 =head1 PUBLIC INTERFACE
91  
92 =head2 The Constructor
93  
94 =over 4
95  
96 =item new
97  
98 use WWW::UsePerl::Journal;
99 my $journal = WWW::UsePerl::Journal->new('barbie');
100  
101 use WWW::UsePerl::Journal::Thread;
102 my $j = WWW::UsePerl::Journal::Thread->new(
103 j => $journal,
104 eid => $entryid,
105 );
106  
107 Creates an thread instance for the specified journal entry. An entry ID
108 returned from $journal->entryids() must use the entry => $entryid form to
109 obtain the correct thread.
110  
111 =back
112  
113 =cut
114  
115 sub new {
116 2 ▶ my ($class,%opts) = @_;
Subroutine new: called CC=4 SCAR=16.4
Pod documented
117  
118 2 for(qw/j eid/) {
119 3 ▶ return unless(exists $opts{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{$_} 1 2 3
120 }
121  
122 die "No parent object"
123 1 ▶ unless $opts{j}->isa('WWW::UsePerl::Journal');
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"j"}->isa("WWW::UsePerl::Journal") 1 0 1
124  
125 0 my %atts = map {$_ => $opts{$_}} qw(j eid);
126 0 my $self = bless \%atts, $class;
127  
128 0 return $self;
129 }
130  
131 =head2 Methods
132  
133 =over 4
134  
135 =item thread()
136  
137 Returns the current thread id.
138  
139 =cut
140  
141 sub thread {
142 0 ▶ my $self = shift;
Subroutine thread: not called CC=2 SCAR=17.9
Pod documented
143 0 ▶ $self->_commenthash unless($self->{thread});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"thread"} 0 0 0
144 0 return $self->{thread};
145 }
146  
147 =item comment($commentid)
148  
149 Returns a comment object of the given comment ID
150  
151 =cut
152  
153 sub comment {
154 0 ▶ my $self = shift;
Subroutine comment: not called CC=1 SCAR=6.9
Pod documented
155 0 my $cid = shift;
156 0 my %entries = $self->_commenthash;
157 0 return $entries{$cid};
158 }
159  
160 =item commentids()
161  
162 Returns an ascending array of the comment IDs.
163  
164 Can take an optional hash containing; {descending=>1} to return a descending
165 list of comment IDs, {ascending=>1} to return an ascending list or
166 {threaded=>1} to return a thread ordered list. 'ascending' being the default.
167  
168 =back
169  
170 =cut
171  
172 sub commentids {
173 0 ▶ my ($self,%hash) = @_;
Subroutine commentids: not called CC=6 SCAR=37.4
Pod documented
174  
175 0 my ($key,$sorter) = ('_commentids_asc',\&_ascender);
176 0 ▶ ($key,$sorter) = ('_commentids_dsc',\&_descender) if(%hash && $hash{descending});
Branch: 1 branch
BranchTrueFalseTotal
if %hash and $hash{"descending"} 0 0 0
Condition0%0/3
%hash and $hash{"descending"}
!ll&&!rl&&r
000
MC/DC0%0/2
%hash and $hash{"descending"}
%hash $hash{"descending"}
Truth table0%0/3
A and B
A= %hashB= $hash{"descending"}
ABresult
0X0
100
111
177 0 ▶ ($key,$sorter) = ('_commentids_thd',sub{-1}) if(%hash && $hash{threaded});
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if %hash and $hash{"threaded"} 0 0 0
Condition0%0/3
%hash and $hash{"threaded"}
!ll&&!rl&&r
000
MC/DC0%0/2
%hash and $hash{"threaded"}
%hash $hash{"threaded"}
Truth table0%0/3
A and B
A= %hashB= $hash{"threaded"}
ABresult
0X0
100
111
178  
179 0 ▶ $self->{$key} ||= do {
Condition0%0/3
$self->{$key} ||= do { my(%entries) = $self->_commenthash; my(@ids) = (sort $sorter keys %entries); \@ids }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{$key} ||= do { my(%entries) = $self->_commenthash; my(@ids) = (sort $sorter keys %entries); \@ids }
$self->{$key} do { my(%entries) = $self->_commenthash; my(@ids) = (sort $sorter keys %entries); \@ids }
Truth table0%0/3
A ||= B
A= $self->{$key}B= do { my(%entries) = $self->_commenthash; my(@ids) = (sort $sorter keys %entries); \@ids }
ABresult
1X1
011
000
180 0 my %entries = $self->_commenthash;
181 0 my @ids = sort $sorter keys %entries;
182 0 \@ids;
183 };
184  
185 0 return @{$self->{$key}};
186 }
187  
188 # -------------------------------------
189 # The Private Methods
190  
191 # name: commenthash
192 # desc: Returns a hash of WWW::UsePerl::Journal::Comment objects
193  
194 sub _commenthash {
195 0 ▶ my $self = shift;
Subroutine _commenthash: not called CC=10 SCAR=47.0
196  
197 0 ▶ return %{ $self->{_commenthash} } if($self->{_commenthash});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_commenthash"} 0 0 0
198  
199 # URL depends upon which id we've been given, as thread and entry
200 # are different, but both can still return the thread list, just in
201 # different formats
202  
203 0 my $user = $self->{j}->user;
204 0 my $url = USEPERL . "/_$user/journal/$self->{eid}.html";
205  
206 0 my $content;
207 0 eval { $content = $self->{j}->{ua}->request(GET $url)->content; };
208 0 ▶ return $self->{j}->error("could not create comment list") if($@ || !$content);
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not $content 0 0 0
Condition0%0/3
$@ or not $content
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $content
$@ not $content
Truth table0%0/3
A or not(B)
A= $@B= not $content
ABresult
1X1
011
000
209  
210 0 ▶ if($self->{j}->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"j"}->debug) 0 0 0
211 0 $self->{j}->log('mess' => "#_commenthash: url=[$url]\n");
212 0 $self->{j}->log('mess' => "#_commenthash: content=[$content]\n");
213 }
214  
215 # main comment thread
216 0 my %comments;
217 0 my @cids = $content =~ m! <div\s+id="comment_top_(\d+)" !sixg;
218 0 ▶ if($self->{j}->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"j"}->debug) 0 0 0
219 0 $self->{j}->log('mess' => "#cids: @cids\n");
220 }
221  
222 0 ($self->{thread}) = $content =~ m!sid=(\d+)!;
223  
224 0 for my $cid (@cids) {
225  
226 0 ▶ if($self->{j}->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"j"}->debug) 0 0 0
227 0 $self->{j}->log('mess' => "\n#_commenthash: cid=[$cid]\n");
228 }
229  
230 0 ▶ next if($comments{$cid});
Branch: 1 branch
BranchTrueFalseTotal
if $comments{$cid} 0 0 0
231  
232 0 my ($extract) = $content =~ m! (<ul\s+id="[^"]+"[^>]*>\s*<li\s+id="tree_$cid"\s+class="[^"]+">.*?<div\s+id="replyto_$cid"></div>) !six;
233 0 ▶ if($self->{j}->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"j"}->debug) 0 0 0
234 0 $self->{j}->log('mess' => "\n#extract: [$extract]\n");
235 }
236  
237 $comments{$cid} = WWW::UsePerl::Journal::Comment->new(
238 j => $self->{j},
239 cid => $cid,
240 eid => $self->{eid},
241 0 extract => $extract
242 );
243 }
244  
245 0 %{ $self->{_commenthash} } = %comments;
246 0 return %{ $self->{_commenthash} };
247 }
248  
249 # sort methods
250  
251 0 ▶sub _ascender { $a <=> $b }
Subroutine _ascender: not called CC=1 SCAR=6.9
252 0 ▶sub _descender { $b <=> $a }
Subroutine _descender: not called CC=1 SCAR=6.9
253  
254 6 1;
255  
256 __END__
« Comment.pm ↑ Summary