source: wiki-toolkit/trunk/lib/Wiki/Toolkit/Feed/Atom.pm @ 330

Last change on this file since 330 was 330, checked in by nick, 14 years ago

Move the common geo formatting into Listing.pm

  • Property svn:executable set to *
File size: 11.6 KB
Line 
1package Wiki::Toolkit::Feed::Atom;
2
3use strict;
4
5use vars qw( @ISA $VERSION );
6$VERSION = '0.01';
7
8use POSIX 'strftime';
9use Time::Piece;
10use URI::Escape;
11use Carp qw( croak );
12
13use Wiki::Toolkit::Feed::Listing;
14@ISA = qw( Wiki::Toolkit::Feed::Listing );
15
16sub new
17{
18  my $class = shift;
19  my $self  = {};
20  bless $self, $class;
21
22  my %args = @_;
23  my $wiki = $args{wiki};
24
25  unless ($wiki && UNIVERSAL::isa($wiki, 'Wiki::Toolkit'))
26  {
27    croak 'No Wiki::Toolkit object supplied';
28  }
29 
30  $self->{wiki} = $wiki;
31 
32  # Mandatory arguments.
33  foreach my $arg (qw/site_name site_url make_node_url recent_changes_link atom_link/)
34  {
35    croak "No $arg supplied" unless $args{$arg};
36    $self->{$arg} = $args{$arg};
37  }
38 
39  # Optional arguments.
40  foreach my $arg (qw/site_description software_name software_version software_homepage/)
41  {
42    $self->{$arg} = $args{$arg} || '';
43  }
44
45  $self->{timestamp_fmt} = $Wiki::Toolkit::Store::Database::timestamp_fmt;
46  $self->{utc_offset} = strftime "%z", localtime;
47  $self->{utc_offset} =~ s/(..)(..)$/$1:$2/;
48 
49  $self;
50}
51
52=item <build_feed_start>
53
54Internal method, to build all the stuff that will go at the start of a feed.
55Outputs the feed header, and initial feed info.
56
57=cut
58sub build_feed_start {
59  my ($self,$atom_timestamp) = @_;
60
61  my $generator = '';
62 
63  if ($self->{software_name})
64  {
65    $generator  = '  <generator';
66    $generator .= ' uri="' . $self->{software_homepage} . '"'   if $self->{software_homepage};
67    $generator .= ' version=' . $self->{software_version} . '"' if $self->{software_version};
68    $generator .= ">\n";
69    $generator .= $self->{software_name} . "</generator>\n";
70  }                         
71
72  my $subtitle = $self->{site_description}
73                 ? '<subtitle>' . $self->{site_description} . "</subtitle>\n"
74                 : '';
75                 
76  my $atom = qq{<?xml version="1.0" encoding="UTF-8"?>
77
78<feed
79 xmlns         = "http://www.w3.org/2005/Atom"
80 xmlns:geo     = "http://www.w3.org/2003/01/geo/wgs84_pos#"
81 xmlns:space   = "http://frot.org/space/0.1/"
82>
83
84  <link href="}            . $self->{site_url}     . qq{" />
85  <title>}                 . $self->{site_name}    . qq{</title>
86  <link rel="self" href="} . $self->{atom_link}    . qq{" />
87  <updated>}               . $atom_timestamp       . qq{</updated>
88  <id>}                    . $self->{site_url}     . qq{</id>
89  $subtitle};
90 
91  return $atom;
92}
93
94=item <build_feed_end>
95
96Internal method, to build all the stuff that will go at the end of a feed.
97
98=cut
99sub build_feed_end {
100    my ($self,$feed_timestamp) = @_;
101
102    return "</feed>\n";
103}
104
105=item <generate_node_list_feed>
106 
107Generate and return an Atom feed for a list of nodes
108 
109=cut
110sub generate_node_list_feed {
111  my ($self,$atom_timestamp,@nodes) = @_;
112
113  my $atom = $self->build_feed_start($atom_timestamp);
114
115  my (@urls, @items);
116
117  foreach my $node (@nodes)
118  {
119    my $node_name = $node->{name};
120
121    my $item_timestamp = $node->{last_modified};
122   
123    # Make a Time::Piece object.
124    my $time = Time::Piece->strptime($item_timestamp, $self->{timestamp_fmt});
125
126    my $utc_offset = $self->{utc_offset};
127   
128    $item_timestamp = $time->strftime( "%Y-%m-%dT%H:%M:%S$utc_offset" );
129
130    my $author      = $node->{metadata}{username}[0] || $node->{metadata}{host}[0] || 'Anonymous';
131    my $description = $node->{metadata}{comment}[0]  || 'No description given for node';
132
133    $description .= " [$author]" if $author;
134
135    my $version = $node->{version};
136    my $status  = (1 == $version) ? 'new' : 'updated';
137
138    my $major_change = $node->{metadata}{major_change}[0];
139       $major_change = 1 unless defined $major_change;
140    my $importance = $major_change ? 'major' : 'minor';
141
142    my $url = $self->{make_node_url}->($node_name, $version);
143
144    # make XML-clean
145    my $title =  $node_name;
146       $title =~ s/&/&amp;/g;
147       $title =~ s/</&lt;/g;
148       $title =~ s/>/&gt;/g;
149
150    # Pop the categories into atom:category elements (4.2.2)
151    # We can do this because the spec says:
152    #   "This specification assigns no meaning to the content (if any)
153    #    of this element."
154    # TODO: Decide if we should include the "all categories listing" url
155    #        as the scheme (URI) attribute?
156    my $category_atom = "";
157    if($node->{metadata}->{category}) {
158        foreach my $cat (@{ $node->{metadata}->{category} }) {
159            $category_atom .= "    <category term=\"$cat\" />\n";
160        }
161    }
162
163    # Include geospacial data, if we have it
164    my $geo_atom = $self->format_geo($node->{metadata});
165
166    # TODO: Find an Atom equivalent of ModWiki, so we can include more info
167
168   
169    push @items, qq{
170  <entry>
171    <title>$title</title>
172    <link href="$url" />
173    <id>$url</id>
174    <summary>$description</summary>
175    <updated>$item_timestamp</updated>
176    <author><name>$author</name></author>
177$category_atom
178$geo_atom
179  </entry>
180};
181
182  }
183 
184  $atom .= join('', @items) . "\n";
185  $atom .= $self->build_feed_end($atom_timestamp);
186
187  return $atom;   
188}
189
190=item <generate_node_name_distance_feed>
191 
192Generate a very cut down atom feed, based just on the nodes, their locations
193(if given), and their distance from a reference location (if given).
194
195Typically used on search feeds.
196 
197=cut
198sub generate_node_name_distance_feed {
199  my ($self,$atom_timestamp,@nodes) = @_;
200
201  my $atom = $self->build_feed_start($atom_timestamp);
202
203  my (@urls, @items);
204
205  foreach my $node (@nodes)
206  {
207    my $node_name = $node->{name};
208
209    my $url = $self->{make_node_url}->($node_name);
210
211    # make XML-clean
212    my $title =  $node_name;
213       $title =~ s/&/&amp;/g;
214       $title =~ s/</&lt;/g;
215       $title =~ s/>/&gt;/g;
216
217    push @items, qq{
218  <entry>
219    <title>$title</title>
220    <link href="$url" />
221    <id>$url</id>
222  </entry>
223};
224
225  }
226 
227  $atom .= join('', @items) . "\n";
228  $atom .= $self->build_feed_end($atom_timestamp);
229
230  return $atom;   
231}
232
233=item B<feed_timestamp>
234
235Generate the timestamp for the Atom, based on the newest node (if available)
236
237=cut
238sub feed_timestamp
239{
240  my ($self, $newest_node) = @_;
241 
242  if ($newest_node->{last_modified})
243  {
244    my $time = Time::Piece->strptime( $newest_node->{last_modified}, $self->{timestamp_fmt} );
245
246    my $utc_offset = $self->{utc_offset};
247   
248    return $time->strftime( "%Y-%m-%dT%H:%M:%S$utc_offset" );
249  }
250  else
251  {
252    return '1970-01-01T00:00:00+0000';
253  }
254}
255
2561;
257
258__END__
259
260=head1 NAME
261
262  Wiki::Toolkit::Feed::Atom - A Wiki::Toolkit plugin to output RecentChanges Atom.
263
264=head1 DESCRIPTION
265
266This is an alternative access to the recent changes of a Wiki::Toolkit
267wiki. It outputs the Atom Syndication Format as described at
268L<http://www.atomenabled.org/developers/syndication/>.
269
270This module is a straight port of L<Wiki::Toolkit::Feed::RSS>.
271
272=head1 SYNOPSIS
273
274  use Wiki::Toolkit;
275  use Wiki::Toolkit::Feed::Atom;
276
277  my $wiki = Wiki::Toolkit->new( ... );  # See perldoc Wiki::Toolkit
278
279  # Set up the RSS feeder with the mandatory arguments - see
280  # C<new()> below for more, optional, arguments.
281  my $atom = Wiki::Toolkit::Feed::Atom->new(
282    wiki                => $wiki,
283    site_name           => 'My Wiki',
284    site_url            => 'http://example.com/',
285    make_node_url       => sub
286                           {
287                             my ($node_name, $version) = @_;
288                             return 'http://example.com/?id=' . uri_escape($node_name) . ';version=' . uri_escape($version);
289                           },
290    recent_changes_link => 'http://example.com/?RecentChanges',
291    atom_link => 'http://example.com/?action=rc;format=atom',
292  );
293
294  print "Content-type: application/atom+xml\n\n";
295  print $atom->recent_changes;
296
297=head1 METHODS
298
299=head2 C<new()>
300
301  my $atom = Wiki::Toolkit::Feed::Atom->new(
302    # Mandatory arguments:
303    wiki                 => $wiki,
304    site_name            => 'My Wiki',
305    site_url             => 'http://example.com/',
306    make_node_url        => sub
307                            {
308                              my ($node_name, $version) = @_;
309                              return 'http://example.com/?id=' . uri_escape($node_name) . ';version=' . uri_escape($version);
310                            },
311    recent_changes_link  => 'http://example.com/?RecentChanges',,
312    atom_link => 'http://example.com/?action=rc;format=atom',
313
314    # Optional arguments:
315    site_description     => 'My wiki about my stuff',
316    software_name        => $your_software_name,     # e.g. "Wiki::Toolkit"
317    software_version     => $your_software_version,  # e.g. "0.73"
318    software_homepage    => $your_software_homepage, # e.g. "http://search.cpan.org/dist/CGI-Wiki/"
319  );
320
321C<wiki> must be a L<Wiki::Toolkit> object. C<make_node_url>, if supplied, must
322be a coderef.
323
324The mandatory arguments are:
325
326=over 4
327
328=item * wiki
329
330=item * site_name
331
332=item * site_url
333
334=item * make_node_url
335
336=item * recent_changes_link
337
338=item * atom_link
339
340=back
341
342The three optional arguments
343
344=over 4
345
346=item * software_name
347
348=item * software_version
349
350=item * software_homepage
351
352=back
353
354are used to generate the C<generator> part of the feed.
355
356=head2 C<recent_changes()>
357
358  $wiki->write_node(
359                     'About This Wiki',
360                     'blah blah blah',
361                                 $checksum,
362                           {
363                       comment  => 'Stub page, please update!',
364                                   username => 'Fred',
365                     }
366  );
367
368  print "Content-type: application/atom+xml\n\n";
369  print $atom->recent_changes;
370
371  # Or get something other than the default of the latest 15 changes.
372  print $atom->recent_changes( items => 50 );
373  print $atom->recent_changes( days => 7 );
374
375  # Or ignore minor edits.
376  print $atom->recent_changes( ignore_minor_edits => 1 );
377
378  # Personalise your feed further - consider only changes
379  # made by Fred to pages about bookshops.
380  print $atom->recent_changes(
381             filter_on_metadata => {
382                         username => 'Fred',
383                         category => 'Bookshops',
384                       },
385              );
386
387If using C<filter_on_metadata>, note that only changes satisfying
388I<all> criteria will be returned.
389
390B<Note:> Many of the fields emitted by the Atom generator are taken
391from the node metadata. The form of this metadata is I<not> mandated
392by L<Wiki::Toolkit>. Your wiki application should make sure to store some or
393all of the following metadata when calling C<write_node>:
394
395=over 4
396
397=item B<comment> - a brief comment summarising the edit that has just been made; will be used in the summary for this item.  Defaults to the empty string.
398
399=item B<username> - an identifier for the person who made the edit; will be used as the Dublin Core contributor for this item, and also in the RDF description.  Defaults to 'No description given for change'.
400
401=item B<host> - the hostname or IP address of the computer used to make the edit; if no username is supplied then this will be used as the author for this item.  Defaults to 'Anonymous'.
402
403=back
404
405=head2 C<feed_timestamp()>
406
407  print $atom->feed_timestamp();
408
409Returns the timestamp of the feed in POSIX::strftime style ("Tue, 29 Feb 2000
41012:34:56 GMT"), which is equivalent to the timestamp of the most recent item
411in the feed. Takes the same arguments as recent_changes(). You will most likely
412need this to print a Last-Modified HTTP header so user-agents can determine
413whether they need to reload the feed or not.
414 
415=head1 SEE ALSO
416
417=over 4
418
419=item * L<Wiki::Toolkit>
420
421=item * L<http://www.atomenabled.org/developers/syndication/>
422
423=back
424
425=head1 MAINTAINER
426
427The Wiki::Toolkit team, http://www.wiki-toolkit.org/.
428
429=head1 COPYRIGHT AND LICENSE
430
431Copyright 2006 Earle Martin and the Wiki::Toolkit team.
432
433This module is free software; you can redistribute it and/or modify it
434under the same terms as Perl itself.
435
436=head1 THANKS
437
438Kake Pugh for originally writing Wiki::Toolkit::Feed::RSS and indeed
439Wiki::Toolkit itself.
440
441=cut
Note: See TracBrowser for help on using the repository browser.