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

Last change on this file since 425 was 425, checked in by Dominic Hargreaves, 14 years ago

fix uninitialized warnings (fixes #28)

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