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

Last change on this file since 784 was 784, checked in by Dominic Hargreaves, 15 years ago

Revert changeset 769 to reintroduce Atom support.

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