root/branches/feature-server/plagger/lib/Plagger/Feed.pm

Revision 856 (checked in by miyagawa, 2 years ago)

merge from trunk to plagger-server for Enclosures support and such. Sorry for the big commit

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Feed;
2 use strict;
3
4 use base qw( Plagger::Thing );
5 __PACKAGE__->mk_accessors(qw( link url image description language author updated tags meta type source_xml aggregator ));
6
7 use Digest::MD5 qw(md5_hex);
8 use Plagger::Util;
9
10 sub new {
11     my $class = shift;
12     bless {
13         meta  => {},
14         tags  => [],
15         entries => [],
16         type  => 'feed',
17     }, $class;
18 }
19
20 sub add_entry {
21     my($self, $entry) = @_;
22     push @{ $self->{entries} }, $entry;
23 }
24
25 sub delete_entry {
26     my($self, $entry) = @_;
27     my @entries = grep { $_ ne $entry } $self->entries;
28     $self->{entries} = \@entries;
29 }
30
31 sub entries {
32     my $self = shift;
33     wantarray ? @{ $self->{entries} } : $self->{entries};
34 }
35
36 sub count {
37     my $self = shift;
38     scalar @{ $self->{entries} };
39 }
40
41 sub title {
42     my $self = shift;
43     if (@_) {
44         my $title = shift;
45         utf8::decode($title) unless utf8::is_utf8($title);
46         $self->{title} = $title;
47     }
48     $self->{title};
49 }
50
51 sub id {
52     my $self = shift;
53     $self->{id} = shift if @_;
54     $self->{id} || Digest::MD5::md5_hex($self->url || $self->link);
55 }
56
57 sub id_safe {
58     my $self = shift;
59     my $id = $self->id;
60     $id =~ s![^\w\s]+!_!g;
61     $id =~ s!\s+!_!g;
62     $id;
63 }
64
65 sub title_text {
66     my $self = shift;
67     Plagger::Util::strip_html($self->title);
68 }
69
70 sub sort_entries {
71     my $self = shift;
72
73     # xxx reverse chron only, using Schwartzian transform
74     my @entries = map { $_->[1] }
75         sort { $b->[0] <=> $a->[0] }
76         map { [ $_->date || DateTime->from_epoch(epoch => 0), $_ ] } $self->entries;
77
78     $self->{entries} = \@entries;
79 }
80
81 sub clear_entries {
82     my $self = shift;
83     $self->{entries} = [];
84 }
85
86 sub dedupe_entries {
87     my $self = shift;
88     my %seen;
89     my @entries;
90     for my $entry ($self->entries) {
91         push @entries, $entry if !$seen{$entry->permalink}++;
92     }
93     $self->{entries} = \@entries;
94 }
95
96 1;
Note: See TracBrowser for help on using the browser.