Noss/FeedReader.pm

filter: Statementstmt 79.1% 76 / 96 Branchbran 57.5% 23 / 40 Conditioncond 50.0% 9 / 18 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 90.9% 10 / 11 Podpod 50.0% 1 / 2 totaltotal 67.4% 120 / 178 CC 31 SCAR 28.3
CC
31
Cov
70%
read_feed
25.8
discover_feeds
14.5
_title_from_desc
12.0
CRAP 16.9
« Noss/FeedReader/RSS.pm ↑ Summary Noss/FeedReader/MediaRSS.pm »
1 package WWW::Noss::FeedReader;
2 5 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 our $VERSION = '2.05';
6  
7 5 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 our @EXPORT_OK = qw(read_feed discover_feeds);
9  
10 5 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use WWW::Noss::FeedReader::Atom;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use WWW::Noss::FeedReader::RSS;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use WWW::Noss::TextToHtml qw(strip_tags unescape_html);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # What is with difference between 'title' and 'displaytitle'?
17 # Prior to 1.09, there was only title, which served as both the title to use
18 # for internally identifying a post by using it in a post's nossuid, and
19 # also the title to show the user when querying posts. The issue is that if I
20 # ever wanted to change the way noss processed certain titles, this would
21 # interfere with generating posts' nossuids and cause them to be considered
22 # new posts. That is why I introduced displaytitle, so that it could serve
23 # as the human-readable version of a post's title which could be safely
24 # changed without causing issues for existing databases.
25 #
26 # So basically:
27 # title - Internal title used by noss for generating nossuids; should not
28 # change.
29 # displaytitle - Title that will be shown to users; can be changed.
30  
31 0 ▶sub _title_from_desc {
Subroutine _title_from_desc: not called CC=3 SCAR=24.8
32  
33 0 my ($desc) = @_;
34  
35 0 ▶ return '' if not defined $desc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $desc 0 0 0
36  
37 0 $desc = unescape_html(strip_tags($desc));
38 0 $desc =~ s/\s+/ /g;
39 0 $desc =~ s/^\s+|\s+$//g;
40 0 my $long = length $desc > 40;
41 0 $desc = substr $desc, 0, 40;
42 0 $desc =~ s/ $//;
43 0 ▶ $desc .= '...' if $long;
Branch: 1 branch
BranchTrueFalseTotal
if $long 0 0 0
44  
45 0 return $desc;
46  
47 }
48  
49 16 ▶sub read_feed {
Subroutine read_feed: called CC=19 SCAR=32.5
Pod documented
50  
51 16 my ($feed) = @_;
52  
53 16 my $channel;
54 my $entries;
55  
56 16 my $dom = eval { XML::LibXML->load_xml(location => $feed->path) };
57  
58 16 ▶ if (not defined $dom) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dom) 0 16 16
59 0 die sprintf
60 "Failed to parse %s as an XML document, %s might not be an RSS or Atom feed\n",
61 $feed->path,
62 $feed->name;
63 }
64  
65 16 ▶ if ($dom->documentElement->nodeName eq 'rss') {
Branch: 2 branches
BranchTrueFalseTotal
if ($dom->documentElement->nodeName eq 'rss') { } 3 13 16
elsif ($dom->documentElement->nodeName eq 'feed' and $dom->documentElement->getAttribute('xmlns') eq $WWW::Noss::FeedReader::Atom::NS) { } 13 0 13
66 3 ($channel, $entries) = WWW::Noss::FeedReader::RSS->read_feed(
67 $feed,
68 $dom
69 );
70 ▶ } elsif (
Condition33%1/3
$dom->documentElement->nodeName eq "feed" and $dom->documentElement->getAttribute("xmlns") eq $WWW::Noss::FeedReader::Atom::NS
!ll&&!rl&&r
0013
MC/DC0%0/2
$dom->documentElement->nodeName eq "feed" and $dom->documentElement->getAttribute("xmlns") eq $WWW::Noss::FeedReader::Atom::NS
$dom->documentElement->nodeName eq "feed" $dom->documentElement->getAttribute("xmlns") eq $WWW::Noss::FeedReader::Atom::NS
Truth table33%1/3
A and B
A= $dom->documentElement->nodeName eq "feed"B= $dom->documentElement->getAttribute("xmlns") eq $WWW::Noss::FeedReader::Atom::NS
ABresult
0X0
100
111
71 $dom->documentElement->nodeName eq 'feed' and
72 $dom->documentElement->getAttribute('xmlns') eq $WWW::Noss::FeedReader::Atom::NS
73 ) {
74 13 ($channel, $entries) = WWW::Noss::FeedReader::Atom->read_feed(
75 $feed,
76 $dom
77 );
78 } else {
79 0 die sprintf "%s is not an RSS or Atom feed\n", $feed->name;
80 }
81  
82 16 ▶ if (defined $channel->{ description }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $channel->{"description"}) 15 1 16
83 15 $channel->{ description } =~ s/\s+/ /g;
84 15 $channel->{ description } =~ s/^ | $//g;
85 }
86  
87 16 for my $i (0 .. $#$entries) {
88 72 ▶ if (not defined $entries->[$i]{ displaytitle }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $entries->[$i]{"displaytitle"}) 0 72 72
89 0 ▶ if (defined $entries->[$i]{ summary }) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $entries->[$i]{'summary'}) { } 0 0 0
elsif (defined $entries->[$i]{'link'}) { } 0 0 0
90 0 $entries->[$i]{ displaytitle } = _title_from_desc($entries->[$i]{ summary });
91 } elsif (defined $entries->[$i]{ link }) {
92 0 $entries->[$i]{ displaytitle } = $entries->[$i]{ link };
93 }
94 }
95 72 ▶ unless ($feed->title_ok($entries->[$i]{ displaytitle })) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($feed->title_ok($entries->[$i]{"displaytitle"})) 9 63 72
96 9 $entries->[$i] = undef;
97 9 next;
98 }
99 63 ▶ unless ($feed->content_ok($entries->[$i]{ summary })) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($feed->content_ok($entries->[$i]{"summary"})) 10 53 63
100 10 $entries->[$i] = undef;
101 10 next;
102 }
103 53 ▶ unless ($feed->tags_ok($entries->[$i]{ category })) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($feed->tags_ok($entries->[$i]{"category"})) 10 43 53
104 10 $entries->[$i] = undef;
105 10 next;
106 }
107 }
108  
109 16 @$entries = grep { defined } @$entries;
110  
111 16 ▶ unless (@$entries) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$entries) 0 16 16
112 0 die sprintf "%s does contain any posts\n", $feed->name;
113 }
114  
115 16 ▶ if (defined $feed->limit and $feed->limit < @$entries) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $feed->limit and $feed->limit < @$entries) 1 15 16
Condition66%2/3
defined $feed->limit and $feed->limit < @$entries
!ll&&!rl&&r
1501
MC/DC50%1/2
defined $feed->limit and $feed->limit < @$entries
defined $feed->limit $feed->limit < @$entries
Truth table66%2/3
A and B
A= defined $feed->limitB= $feed->limit < @$entries
ABresult
0X0
100
111
116 1 @$entries = @$entries[-$feed->limit .. -1];
117 }
118  
119 16 for my $i (0 .. $#$entries) {
120 40 $entries->[$i]{ nossid } = $i + 1;
121 40 ▶ $entries->[$i]{ author } //= $channel->{ author };
Condition33%1/3
$entries->[$i]{"author"} //= $channel->{"author"}
l!l&&r!l&&!r
4000
MC/DC0%0/2
$entries->[$i]{"author"} //= $channel->{"author"}
$entries->[$i]{"author"} $channel->{"author"}
Truth table33%1/3
A //= B
A= $entries->[$i]{"author"}B= $channel->{"author"}
ABresult
1X1
011
000
122 $entries->[$i]{ nossuid } =
123 join ";",
124 200 ▶ map { $_ // '' }
Condition50%1/2
$_ // ""
l!l
2000
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
125 40 @{ $entries->[$i] }{ qw(uid feed title link published) };
126 }
127  
128 16 return ($channel, $entries);
129  
130 }
131  
132 1 ▶sub discover_feeds {
Subroutine discover_feeds: called CC=11 SCAR=26.8
Pod undocumented
133  
134 1 my ($html_file) = @_;
135  
136 1 my $dom = XML::LibXML->load_html(
137 location => $html_file,
138 recover => 2,
139 suppress_errors => 1,
140 );
141  
142 1 my ($head) = $dom->findnodes('/html/head');
143 1 ▶ if (not defined $head) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $head) 0 1 1
144 0 return ();
145 }
146  
147 1 my @discovered;
148 1 for my $c ($head->childNodes) {
149 5 ▶ next unless $c->isa('XML::LibXML::Element');
Branch: 1 branch
BranchTrueFalseTotal
unless $c->isa("XML::LibXML::Element") 3 2 5
150 2 ▶ next unless $c->nodeName eq 'link';
Branch: 1 branch
BranchTrueFalseTotal
unless $c->nodeName eq "link" 0 2 2
151 2 ▶ my $rel = $c->getAttribute('rel') // '';
Condition50%1/2
$c->getAttribute("rel") // ""
l!l
20
MC/DC0%0/1
$c->getAttribute("rel") // ""
$c->getAttribute("rel")
Truth table50%1/2
A
A= $c->getAttribute("rel")
Aresult
11
00
152 2 ▶ if ($rel ne 'alternate') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel ne "alternate") 0 2 2
153 0 next;
154 }
155 2 ▶ my $type = $c->getAttribute('type') // '';
Condition50%1/2
$c->getAttribute("type") // ""
l!l
20
MC/DC0%0/1
$c->getAttribute("type") // ""
$c->getAttribute("type")
Truth table50%1/2
A
A= $c->getAttribute("type")
Aresult
11
00
156 2 ▶ if ($type ne 'application/atom+xml' and $type ne 'application/rss+xml') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "application/atom+xml" and $type ne "application/rss+xml") 0 2 2
Condition66%2/3
$type ne "application/atom+xml" and $type ne "application/rss+xml"
!ll&&!rl&&r
110
MC/DC0%0/2
$type ne "application/atom+xml" and $type ne "application/rss+xml"
$type ne "application/atom+xml" $type ne "application/rss+xml"
Truth table66%2/3
A and B
A= $type ne "application/atom+xml"B= $type ne "application/rss+xml"
ABresult
0X0
100
111
157 0 next;
158 }
159 2 my $href = $c->getAttribute('href');
160 2 ▶ if (not defined $href) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $href) 0 2 2
161 0 next;
162 }
163 2 push @discovered, $href;
164 }
165  
166 1 return @discovered;
167  
168 }
169  
170 5 1;
171  
172 =head1 NAME
173  
174 WWW::Noss::FeedReader - RSS/Atom feed reader
175  
176 =head1 USAGE
177  
178 use WWW::Noss::FeedReader qw(read_feed);
179  
180 my ($channel, $entries) = read_feed($feed);
181  
182 =head1 DESCRIPTION
183  
184 B<WWW::Noss::FeedReader> is a module that provides the C<read_feed()>
185 subroutine for reading RSS and Atom feeds. This is a private module, please
186 consult the L<noss> manual for user documentation.
187  
188 =head1 SUBROUTINES
189  
190 Subroutines are not exported automatically.
191  
192 =over 4
193  
194 =item (\%channel, \@entries) = read_feed($feed)
195  
196 Reads the given L<WWW::Noss::FeedConfig> object and returns the channel and
197 entry data. Returns both as C<undef> on failure.
198  
199 C<\%channel> should look something like this:
200  
201 {
202 nossname => ...,
203 nosslink => ...,
204 title => ...,
205 link => ...,
206 description => ...,
207 updated => ...,
208 author => ...,
209 category => [ ... ],
210 generator => ...,
211 image => ...,
212 rights => ...,
213 skiphours => [ ... ],
214 skipdays => [ ... ],
215 }
216  
217 C<\@entries> will be a list of hash refs that look something like this:
218  
219 {
220 nossid => ...,
221 status => ...,
222 feed => ...,
223 title => ...,
224 link => ...,
225 author => ...,
226 category => [ ... ],
227 summary => ...,
228 published => ...,
229 updated => ...,
230 uid => ...,
231 displaytitle => ...,
232 }
233  
234 =back
235  
236 =head1 AUTHOR
237  
238 Written by Samuel Young, E<lt>samyoung@posteo.comE<gt>.
239  
240 This project's source can be found on its
241 L<Codeberg page|https://codeberg.org/1-1sam/noss.git>. Comments and pull
242 requests are welcome!
243  
244 =head1 COPYRIGHT
245  
246 Copyright (C) 2025-2026 Samuel Young
247  
248 This program is free software: you can redistribute it and/or modify
249 it under the terms of the GNU General Public License as published by
250 the Free Software Foundation, either version 3 of the License, or
251 (at your option) any later version.
252  
253 =head1 SEE ALSO
254  
255 L<WWW::Noss::FeedConfig>, L<noss>
256  
257 =cut
258  
259 # vim: expandtab shiftwidth=4
« Noss/FeedReader/RSS.pm ↑ Summary Noss/FeedReader/MediaRSS.pm »