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

Last change on this file since 769 was 769, checked in by Dominic Hargreaves, 16 years ago

Revert changes using CGI::Wiki::Plugin::Atom pending CGI::Wiki/Wiki::Toolkit refactoring.

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