source: trunk/lib/OpenGuides/Feed.pm @ 790

Last change on this file since 790 was 790, checked in by nick, 15 years ago

Pass the requested feed listing through all the feeds code, rather than just assuming it's recent_changes

File size: 7.4 KB
Line 
1package OpenGuides::Feed;
2
3use strict;
4
5use vars qw( $VERSION );
6$VERSION = '0.01';
7
8use Wiki::Toolkit::Feed::Atom;
9use Wiki::Toolkit::Feed::RSS;
10use Time::Piece;
11use URI::Escape;
12use Carp 'croak';
13
14sub new {
15    my ($class, @args) = @_;
16    my $self = {};
17    bless $self, $class;
18    $self->_init(@args);
19}
20
21sub _init {
22    my ($self, %args) = @_;
23
24    my $wiki = $args{wiki};
25   
26    unless ( $wiki && UNIVERSAL::isa( $wiki, "Wiki::Toolkit" ) ) {
27       croak "No Wiki::Toolkit object supplied.";
28    }
29    $self->{wiki} = $wiki;
30
31    my $config = $args{config};
32
33    unless ( $config && UNIVERSAL::isa( $config, "OpenGuides::Config" ) ) {
34        croak "No OpenGuides::Config object supplied.";
35    }
36    $self->{config} = $config;
37
38    $self->{make_node_url} = sub {
39        my ($node_name, $version) = @_;
40
41        my $config = $self->{config};
42   
43        my $node_url = $config->script_url . uri_escape($config->script_name) . '?';
44        $node_url .= 'id=' if defined $version;
45        $node_url .= uri_escape($self->{wiki}->formatter->node_name_to_node_param($node_name));
46        $node_url .= ';version=' . uri_escape($version) if defined $version;
47
48        $node_url;
49      }; 
50    $self->{site_name}        = $config->site_name;
51    $self->{default_city}     = $config->default_city     || "";
52    $self->{default_country}  = $config->default_country  || "";
53    $self->{site_description} = $config->site_desc        || "";
54    $self->{og_version}       = $args{og_version};
55
56    $self;
57}
58
59sub make_feed {
60    my ($self, %args) = @_;
61   
62    my $feed_type = $args{feed_type};
63    my $feed_listing = $args{feed_listing};
64   
65    my %known_types = (
66                          'rss'  => 1,
67                          'atom' => 1,
68                      );
69    my %known_listings = (
70                          'recent_changes' => 1,
71                         );
72                     
73    croak "No feed type specified" unless $feed_type;
74    croak "Unknown feed type: $feed_type" unless $known_types{$feed_type};
75
76    croak "No feed listing specified" unless $feed_listing;
77    croak "Unknown feed listing: $feed_listing" unless $known_listings{$feed_listing};
78
79    if ($feed_type eq 'rss') {
80        if ($feed_listing eq 'recent_changes') {
81            return $self->rss_maker->recent_changes(%args);
82        }
83    }
84    elsif ($feed_type eq 'atom') {
85        if ($feed_listing eq 'recent_changes') {
86            return $self->atom_maker->recent_changes(%args);
87        }
88    }
89}
90
91sub atom_maker {
92    my $self = shift;
93 
94    unless ($self->{atom_maker}) {
95        $self->{atom_maker} = Wiki::Toolkit::Feed::Atom->new(
96            wiki                => $self->{wiki},
97            site_name           => $self->{site_name},
98            site_url            => $self->{config}->script_url,
99            site_description    => $self->{site_description},
100            make_node_url       => $self->{make_node_url},
101            recent_changes_link => $self->{config}->script_url . '?action=rc',
102            atom_link           => $self->{config}->script_url . '?action=rc&format=atom',
103            software_name       => 'OpenGuides',
104            software_homepage   => 'http://openguides.org/',
105            software_version    => $self->{og_version},
106        );
107    }
108   
109    $self->{atom_maker};
110}
111
112sub rss_maker {
113    my $self = shift;
114
115    unless ($self->{rss_maker}) {
116        $self->{rss_maker} = Wiki::Toolkit::Feed::RSS->new(
117            wiki                => $self->{wiki},
118            site_name           => $self->{site_name},
119            site_url            => $self->{config}->script_url,
120            site_description    => $self->{site_description},
121            make_node_url       => $self->{make_node_url},
122            recent_changes_link => $self->{config}->script_url . '?action=rc',
123            software_name       => 'OpenGuides',
124            software_homepage   => 'http://openguides.org/',
125            software_version    => $self->{og_version},
126        );
127    }
128   
129    $self->{rss_maker};
130}
131
132sub feed_timestamp {
133    my ($self, %args) = @_;
134
135    # The timestamp methods in our feeds are equivalent, we might as well
136    # use the RSS one.
137    $self->rss_maker->rss_timestamp(%args);
138}
139
140=head1 NAME
141
142OpenGuides::Feed - generate data feeds for OpenGuides in various formats.
143
144=head1 DESCRIPTION
145
146Produces RSS 1.0 and Atom 1.0 feeds for OpenGuides.  Distributed and
147installed as part of the OpenGuides project, not intended for independent
148installation.  This documentation is probably only useful to OpenGuides
149developers.
150
151=head1 SYNOPSIS
152
153    use Wiki::Toolkit;
154    use OpenGuides::Config;
155    use OpenGuides::Feed;
156
157    my $wiki = Wiki::Toolkit->new( ... );
158    my $config = OpenGuides::Config->new( file => "wiki.conf" );
159    my $feed = OpenGuides::Feed->new( wiki       => $wiki,
160                                      config     => $config,
161                                      og_version => '1.0', );
162
163    # Ten most recent changes in RSS format.
164    print "Content-Type: application/rdf+xml\n";
165    print "Last-Modified: " . $self->feed_timestamp( items => 10 ) . "\n\n";
166    my %args = ( items     => 10,
167                 feed_type => 'rss', );
168    print $feed->make_feed( %args );
169
170=head1 METHODS
171
172=over 4
173
174=item B<new>
175
176    my $feed = OpenGuides::Feed->new( wiki       => $wiki,
177                                      config     => $config,
178                                      og_version => '1.0', );
179
180C<wiki> must be a L<Wiki::Toolkit> object and C<config> must be an
181L<OpenGuides::Config> object.  Both of these arguments are mandatory.
182C<og_version> is an optional argument specifying the version of
183OpenGuides for inclusion in the feed.
184
185=item B<rss_maker>
186
187Returns a raw L<Wiki::Toolkit::Feed::RSS> object created with the values you
188invoked this module with.
189
190=item B<atom_maker>
191
192Returns a raw L<Wiki::Toolkit::Feed::Atom> object created with the values you
193invoked this module with.
194
195=item B<make_feed>
196
197    # Ten most recent changes in RSS format.
198    print "Content-Type: application/rdf+xml\n";
199    print "Last-Modified: " . $feed->feed_timestamp( items => 10 ) . "\n\n";
200    my %args = ( items     => 10,
201                 feed_type => 'rss', );
202    print $rdf_writer->make_feed( %args );
203
204    # All the changes made by bob in the past week, ignoring minor edits, in Atom.
205    $args{days}               = 7;
206    $args{ignore_minor_edits  = 1;
207    $args{filter_on_metadata} => { username => "bob" };
208
209    print "Content-Type: application/atom+xml\n";
210    print "Last-Modified: " . $feed->feed_timestamp( %args ) . "\n\n";
211    print $feed->make_feed( %args );
212
213=item B<feed_timestamp>
214
215    print "Last-Modified: " . $feed->feed_timestamp( %args ) . "\n\n";
216
217Returns the timestamp of the data feed in POSIX::strftime style ("Tue, 29 Feb 2000
21812:34:56 GMT"), which is equivalent to the timestamp of the most recent item
219in the feed. Takes the same arguments as make_recentchanges_rss(). You will most
220likely need this to print a Last-Modified HTTP header so user-agents can determine
221whether they need to reload the feed or not.
222
223=back
224
225=head1 SEE ALSO
226
227=over 4
228
229=item * L<Wiki::Toolkit>, L<Wiki::Toolkit::Feed::RSS> and L<Wiki::Toolkit::Feed::Atom>
230
231=item * L<http://openguides.org/>
232
233=back
234
235=head1 AUTHOR
236
237The OpenGuides Project (openguides-dev@openguides.org)
238
239=head1 COPYRIGHT
240
241Copyright (C) 2003-2006 The OpenGuides Project.  All Rights Reserved.
242
243This module is free software; you can redistribute it and/or modify it
244under the same terms as Perl itself.
245
246=head1 CREDITS
247
248Written by Earle Martin, based on the original OpenGuides::RDF by Kake Pugh.
249
250=cut
251
2521;
Note: See TracBrowser for help on using the repository browser.