File Coverage

blib/lib/News/Overview/Entry.pm
Criterion Covered Total %
statement 6 43 13.9
branch 0 18 0.0
condition 0 7 0.0
subroutine 2 12 16.6
pod 10 10 100.0
total 18 90 20.0


line stmt bran cond sub pod time code
1             $VERSION = "0.02";
2             package News::Overview::Entry;
3             our $VERSION = "0.02";
4              
5             # -*- Perl -*- # Fri Oct 10 11:29:51 CDT 2003
6             #############################################################################
7             # Written by Tim Skirvin . Copyright 2003, Tim
8             # Skirvin. Redistribution terms are below.
9             #############################################################################
10              
11             =head1 NAME
12              
13             News::Overview::Entry - an object for storing specific overview information
14              
15             =head1 SYNOPSIS
16              
17             use News::Overview::Entry;
18             use News::Overview;
19            
20             my $article = new News::Article;
21             # Populate News::Article somehow;
22            
23             # Create a new News::Overview::Entry object
24             my $msgid = $article->header('message-id');
25             my @refs = split(/\s+/, $article->header('references') || "");
26             my %other;
27             foreach ( News::Overview->defaults ) {
28             $other{$_} = $article->header($_) || "";
29             }
30             my $entry = News::Overview::Entry->new($msgid, \@refs, %other);
31              
32             See below for more specific information about related functions.
33              
34             =head1 DESCRIPTION
35              
36             News::Overview::Entry is an object meant to hold overview information
37             about a single news article - that is, the minimal information necessary
38             to sort the article with other articles, as used by XOVER in the NNTP
39             specification. It is primarily a helper class for use with
40             News::Overview; see that manual page for more information.
41              
42            
43             =head1 USAGE
44              
45             All of this object's usage is contained within its functions.
46              
47             =cut
48              
49             ###############################################################################
50             ### main() ####################################################################
51             ###############################################################################
52              
53 1     1   10 use strict;
  1         1  
  1         37  
54 1     1   834 use Date::Parse;
  1         10085  
  1         725  
55              
56             =head2 Basic functions
57              
58             =over 4
59              
60             =item new ( MSGID [, REFERENCES, OTHER] )
61              
62             Creates a new News::Overview::Entry object. C is the Message-ID of
63             the object, and is required (the function will return undef if it's not
64             present).
65              
66             C is an arrayref that contains the message-IDs of the entry's
67             parents. C is a hash of other information about the article, which
68             are stored in the entry.
69              
70             =cut
71              
72             sub new {
73 0     0 1   my ($proto, $msgid, $refs, %other) = @_;
74 0 0         return undef unless $msgid;
75 0   0       $refs ||= [];
76 0   0       my $class = ref($proto) || $proto;
77 0           my $self = {
78             ID => $msgid,
79             Values => {
80             'Message-ID' => $msgid,
81             'References' => join(" ", reverse @{$refs}),
82             %other
83             },
84 0   0       Parent => (@{$refs})[0] || undef,
85             Children => {},
86             References => $refs,
87             };
88 0           bless $self, $class;
89 0           $$self{Values}->{Newsgroups} = join(',', $self->newsgroups);
90 0           $self;
91             }
92              
93             =item id ()
94              
95             Returns the associated message-ID of the Entry.
96              
97             =cut
98              
99 0 0   0 1   sub id { shift->{ID} || "" }
100              
101             =item parent ()
102              
103             Returns the Entry's parent's message-ID, or undef if there isn't one.
104             This is currently defined as "the last item in References". This isn't
105             currently being used, and may change to something else.
106              
107             =cut
108              
109 0 0   0 1   sub parent { shift->{Parent} || undef }
110              
111             =item children ()
112              
113             Returns all of the children of the current Entry; each returned item is
114             another News::Article::Entry object. Note that no children exist until
115             they are added with add_child().
116              
117             =cut
118              
119             sub children {
120 0     0 1   my @array = values %{shift->{Children}};
  0            
121 0 0         wantarray ? @array : \@array;
122             }
123              
124             =item value ( FIELD )
125              
126             Returns the value of the information stired in C within the Entry.
127              
128             =cut
129              
130 0 0   0 1   sub value { my ($self, $field) = @_; $self->values->{$field} || ""; }
  0            
131              
132             =item values ( )
133              
134             Returns a hashref containing all of the keys and values of the information
135             associated with the Entry.
136              
137             =cut
138              
139 0     0 1   sub values { shift->{Values} }
140              
141             =back
142              
143             =head2 Additional Information
144              
145             These functions offer additional information about the Entry, generally by
146             parsing the above information.
147              
148             =over 4
149              
150             =item depth ()
151              
152             Returns the "depth" of the Entry, ie the number references above the
153             article. A parent article (one with no References) is depth 0.
154            
155             =cut
156              
157 0 0   0 1   sub depth { ( scalar @{shift->{References}} ) || 0 }
  0            
158              
159             =item time ( )
160              
161             Returns the time, in seconds since the epoch, that the Entry says it was
162             posted at (generated by parsing the Date header; will default to 0).
163              
164             This information is cached for later use within the item.
165              
166             =cut
167              
168             sub time {
169 0     0 1   my $self = shift;
170 0 0         unless ($$self{Time}) { $$self{Time} = str2time($self->values->{Date}) }
  0            
171 0           $$self{Time};
172             }
173              
174             =item newsgroups ()
175              
176             Returns the newsgroups associated with the entry, either by parsing the
177             Newsgroups field or Xref. Returns an array of associated newsgroups.
178              
179             =cut
180              
181             sub newsgroups {
182 0     0 1   my ($self) = @_;
183 0 0         if (my $groups = $self->value('Newsgroups') ) {
    0          
184 0           my (@groups) = split(/\s+/, $groups);
185 0           return @groups;
186             } elsif ( my $xref = $self->value('Xref') ) {
187 0           my ($server, @groups) = split(/\s+/, $xref);
188 0           map { s/:.*//g } @groups;
  0            
189 0           return @groups;
190             } else {
191 0           return ();
192             }
193             }
194              
195             =back
196              
197             =head2 Manipulation Functions
198              
199             Besides new(), these functions are the only ones that actually modify the
200             Entry in a notable way.
201              
202             =over 4
203              
204             =item add_child ( CHILDREN )
205              
206             Add children to the given item, each of which is a News::Overview::Entry
207             object. These are later accessed with children(). This is useful for
208             threading.
209              
210             Returns the number of children that were successfully added.
211              
212             =cut
213              
214             sub add_child {
215 0     0 1   my ($self, @children) = @_;
216 0           my $start = scalar $self->children;
217 0           foreach (@children) { $$self{Children}->{$_->id} = $_; }
  0            
218 0           ( scalar $self->children ) - $start;
219             }
220              
221             =head1 REQUIREMENTS
222              
223             News::Overview, Date::Parse
224              
225             =head1 SEE ALSO
226              
227             B
228              
229             =head1 TODO
230              
231             Use the parent() bit effectively. Make "pseudo-Entries", to make entries
232             for articles that don't exist, to make threading more accurate.
233              
234             =head1 AUTHOR
235              
236             Tim Skirvin
237              
238             =head1 COPYRIGHT
239              
240             Copyright 2003 by Tim Skirvin . This code may be
241             distributed under the same terms as Perl itself.
242              
243             =cut
244              
245             1;
246              
247             ###############################################################################
248             ### Version History ###########################################################
249             ###############################################################################
250             # v0.01b Fri Oct 10 11:29:51 CDT 2003
251             ### First commented version (above date indicates the start of the comments).
252             # v0.12 Thu Apr 22 13:19:25 CDT 2004
253             ### No real changes; internal code layout.