source: wiki-toolkit/trunk/lib/Wiki/Toolkit/Feed/RSS.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: 15.0 KB
Line 
1package Wiki::Toolkit::Feed::RSS;
2
3use strict;
4
5use vars qw( @ISA $VERSION );
6$VERSION = '0.10';
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/)
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 interwiki_identifier make_diff_url make_history_url
41                        software_name software_version software_homepage/)
42    {
43        $self->{$arg} = $args{$arg} || '';
44    }
45
46    $self->{timestamp_fmt} = $Wiki::Toolkit::Store::Database::timestamp_fmt;
47    $self->{utc_offset} = strftime "%z", localtime;
48    $self->{utc_offset} =~ s/(..)(..)$/$1:$2/;
49
50    $self;
51}
52
53=item <build_feed_start>
54
55Internal method, to build all the stuff that will go at the start of a feed.
56Generally will output namespaces, headers and so on.
57
58=cut
59sub build_feed_start {
60  my ($self,$feed_timestamp) = @_;
61
62  #"http://purl.org/rss/1.0/modules/wiki/"
63  return qq{<?xml version="1.0" encoding="UTF-8"?>
64
65<rdf:RDF
66 xmlns         = "http://purl.org/rss/1.0/"
67 xmlns:dc      = "http://purl.org/dc/elements/1.1/"
68 xmlns:doap    = "http://usefulinc.com/ns/doap#"
69 xmlns:foaf    = "http://xmlns.com/foaf/0.1/"
70 xmlns:rdf     = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
71 xmlns:rdfs    = "http://www.w3.org/2000/01/rdf-schema#"
72 xmlns:modwiki = "http://www.usemod.com/cgi-bin/mb.pl?ModWiki"
73 xmlns:geo     = "http://www.w3.org/2003/01/geo/wgs84_pos#"
74 xmlns:space   = "http://frot.org/space/0.1/"
75>
76};
77}
78
79=item <build_feed_mid>
80
81Internal method, to build all the stuff (except items) to go inside the channel
82
83=cut
84sub build_feed_mid {
85    my ($self,$feed_timestamp) = @_;
86
87    my $rss .= qq{<dc:publisher>} . $self->{site_url} . qq{</dc:publisher>\n};
88
89if ($self->{software_name})
90{
91  $rss .= qq{<foaf:maker>
92  <doap:Project>
93    <doap:name>} . $self->{software_name} . qq{</doap:name>\n};
94}
95
96if ($self->{software_name} && $self->{software_homepage})
97{
98  $rss .= qq{    <doap:homepage rdf:resource="} . $self->{software_homepage} . qq{" />\n};
99}
100
101if ($self->{software_name} && $self->{software_version})
102{
103  $rss .= qq{    <doap:release>
104      <doap:Version>
105        <doap:revision>} . $self->{software_version} . qq{</doap:revision>
106      </doap:Version>
107    </doap:release>\n};
108}
109
110if ($self->{software_name})
111{
112  $rss .= qq{  </doap:Project>
113</foaf:maker>\n};
114}
115
116$rss .= qq{<title>}   . $self->{site_name}            . qq{</title>
117<link>}               . $self->{recent_changes_link}  . qq{</link>
118<description>}        . $self->{site_description}     . qq{</description>
119<dc:date>}            . $feed_timestamp                . qq{</dc:date>
120<modwiki:interwiki>}     . $self->{interwiki_identifier} . qq{</modwiki:interwiki>};
121
122   return $rss;
123}
124
125=item <build_feed_end>
126
127Internal method, to build all the stuff that will go at the end of a feed
128
129=cut
130sub build_feed_end {
131    my ($self,$feed_timestamp) = @_;
132
133    return "</rdf:RDF>\n";
134}
135
136
137=item <generate_node_list_feed>
138
139Generate and return an RSS feed for a list of nodes
140
141=cut
142sub generate_node_list_feed {
143  my ($self,$feed_timestamp,@nodes) = @_;
144
145  # Start our feed
146  my $rss = $self->build_feed_start($feed_timestamp);
147  $rss .= qq{
148
149<channel rdf:about="">
150
151};
152  $rss .= $self->build_feed_mid($feed_timestamp);
153
154  # Generate the items list, and the individiual item entries
155  my (@urls, @items);
156  foreach my $node (@nodes)
157  {
158    my $node_name = $node->{name};
159
160    my $timestamp = $node->{last_modified};
161   
162    # Make a Time::Piece object.
163    my $time = Time::Piece->strptime($timestamp, $self->{timestamp_fmt});
164
165    my $utc_offset = $self->{utc_offset};
166   
167    $timestamp = $time->strftime( "%Y-%m-%dT%H:%M:%S$utc_offset" );
168
169    my $author      = $node->{metadata}{username}[0] || $node->{metadata}{host}[0] || '';
170    my $description = $node->{metadata}{comment}[0]  || '';
171
172    $description .= " [$author]" if $author;
173
174    my $version = $node->{version};
175    my $status  = (1 == $version) ? 'new' : 'updated';
176
177    my $major_change = $node->{metadata}{major_change}[0];
178       $major_change = 1 unless defined $major_change;
179    my $importance = $major_change ? 'major' : 'minor';
180
181    my $url = $self->{make_node_url}->($node_name, $version);
182
183    push @urls, qq{    <rdf:li rdf:resource="$url" />\n};
184
185    my $diff_url = '';
186   
187    if ($self->{make_diff_url})
188    {
189            $diff_url = $self->{make_diff_url}->($node_name);
190    }
191
192    my $history_url = '';
193   
194    if ($self->{make_history_url})
195    {
196      $history_url = $self->{make_history_url}->($node_name);
197    }
198
199    my $node_url = $self->{make_node_url}->($node_name);
200
201    my $rdf_url =  $node_url;
202       $rdf_url =~ s/\?/\?id=/;
203       $rdf_url .= ';format=rdf';
204
205    # make XML-clean
206    my $title =  $node_name;
207       $title =~ s/&/&amp;/g;
208       $title =~ s/</&lt;/g;
209       $title =~ s/>/&gt;/g;
210
211    # Pop the categories into dublin core subject elements
212    #  (http://dublincore.org/usage/terms/history/#subject-004)
213    # TODO: Decide if we should include the "all categories listing" url
214    #        as the scheme (URI) attribute?
215    my $category_rss = "";
216    if($node->{metadata}->{category}) {
217        foreach my $cat (@{ $node->{metadata}->{category} }) {
218            $category_rss .= "  <dc:subject>$cat</dc:subject>\n";
219        }
220    }
221
222    # Include geospacial data, if we have it
223    my $geo_rss = $self->format_geo($node->{metadata});
224
225    push @items, qq{
226<item rdf:about="$url">
227  <title>$title</title>
228  <link>$url</link>
229  <description>$description</description>
230  <dc:date>$timestamp</dc:date>
231  <dc:contributor>$author</dc:contributor>
232  <modwiki:status>$status</modwiki:status>
233  <modwiki:importance>$importance</modwiki:importance>
234  <modwiki:diff>$diff_url</modwiki:diff>
235  <modwiki:version>$version</modwiki:version>
236  <modwiki:history>$history_url</modwiki:history>
237  <rdfs:seeAlso rdf:resource="$rdf_url" />
238$category_rss
239$geo_rss
240</item>
241};
242  }
243 
244  # Output the items list
245  $rss .= qq{
246
247<items>
248  <rdf:Seq>
249} . join('', @urls) . qq{  </rdf:Seq>
250</items>
251
252</channel>
253};
254
255  # Output the individual item entries
256  $rss .= join('', @items) . "\n";
257
258  # Finish up
259  $rss .= $self->build_feed_end($feed_timestamp);
260 
261  return $rss;   
262}
263
264
265=item B<generate_node_name_distance_feed>
266
267Generate a very cut down rss feed, based just on the nodes, their locations
268(if given), and their distance from a reference location (if given).
269
270Typically used on search feeds.
271
272=cut
273sub generate_node_name_distance_feed {
274  my ($self,$feed_timestamp,@nodes) = @_;
275
276  # Start our feed
277  my $rss = $self->build_feed_start($feed_timestamp);
278  $rss .= qq{
279
280<channel rdf:about="">
281
282};
283  $rss .= $self->build_feed_mid($feed_timestamp);
284
285  # Generate the items list, and the individiual item entries
286  my (@urls, @items);
287  foreach my $node (@nodes)
288  {
289    my $node_name = $node->{name};
290
291    my $url = $self->{make_node_url}->($node_name);
292
293    push @urls, qq{    <rdf:li rdf:resource="$url" />\n};
294
295    my $rdf_url =  $url;
296       $rdf_url =~ s/\?/\?id=/;
297       $rdf_url .= ';format=rdf';
298
299    # make XML-clean
300    my $title =  $node_name;
301       $title =~ s/&/&amp;/g;
302       $title =~ s/</&lt;/g;
303       $title =~ s/>/&gt;/g;
304
305    # What location stuff do we have?
306    my $location = undef;
307    my $distance = undef;
308
309    push @items, qq{
310<item rdf:about="$url">
311  <title>$title</title>
312  <link>$url</link>
313  <rdfs:seeAlso rdf:resource="$rdf_url" />
314</item>
315};
316  }
317 
318  # Output the items list
319  $rss .= qq{
320
321<items>
322  <rdf:Seq>
323} . join('', @urls) . qq{  </rdf:Seq>
324</items>
325
326</channel>
327};
328
329  # Output the individual item entries
330  $rss .= join('', @items) . "\n";
331
332  # Finish up
333  $rss .= $self->build_feed_end($feed_timestamp);
334 
335  return $rss;   
336}
337
338=item B<feed_timestamp>
339
340Generate the timestamp for the RSS, based on the newest node (if available)
341
342=cut
343sub feed_timestamp
344{
345    my ($self, $newest_node) = @_;
346
347    if ($newest_node->{last_modified})
348    {
349        my $time = Time::Piece->strptime( $newest_node->{last_modified}, $self->{timestamp_fmt} );
350
351        my $utc_offset = $self->{utc_offset};
352
353        return $time->strftime( "%Y-%m-%dT%H:%M:%S$utc_offset" );
354    }
355    else
356    {
357        return '1970-01-01T00:00:00+0000';
358    }
359}
360
361# Compatibility method - use feed_timestamp with a node instead
362sub rss_timestamp {
363    my ($self, %args) = @_;
364
365    warn("Old style method used - please convert to calling feed_timestamp with a node!");
366    my $feed_timestamp = $self->feed_timestamp(
367                              $self->fetch_newest_for_recently_changed(%args)
368    );
369    return $feed_timestamp;
370}
371
3721;
373
374__END__
375
376=head1 NAME
377
378  Wiki::Toolkit::Feed::RSS - Output RecentChanges RSS for Wiki::Toolkit.
379
380=head1 DESCRIPTION
381
382This is an alternative access to the recent changes of a Wiki::Toolkit
383wiki. It outputs RSS as described by the ModWiki proposal at
384L<http://www.usemod.com/cgi-bin/mb.pl?ModWiki>
385
386=head1 SYNOPSIS
387
388  use Wiki::Toolkit;
389  use Wiki::Toolkit::Feed::RSS;
390
391  my $wiki = CGI::Wiki->new( ... );  # See perldoc Wiki::Toolkit
392
393  # Set up the RSS feeder with the mandatory arguments - see
394  # C<new()> below for more, optional, arguments.
395  my $rss = Wiki::Toolkit::Feed::RSS->new(
396    wiki                => $wiki,
397    site_name           => 'My Wiki',
398    site_url            => 'http://example.com/',
399    make_node_url       => sub
400                           {
401                             my ($node_name, $version) = @_;
402                             return 'http://example.com/?id=' . uri_escape($node_name) . ';version=' . uri_escape($version);
403                           },
404    recent_changes_link => 'http://example.com/?RecentChanges',
405  );
406
407  print "Content-type: application/xml\n\n";
408  print $rss->recent_changes;
409
410=head1 METHODS
411
412=head2 C<new()>
413
414  my $rss = Wiki::Toolkit::Feed::RSS->new(
415    # Mandatory arguments:
416    wiki                 => $wiki,
417    site_name            => 'My Wiki',
418    site_url             => 'http://example.com/',
419    make_node_url        => sub
420                            {
421                              my ($node_name, $version) = @_;
422                              return 'http://example.com/?id=' . uri_escape($node_name) . ';version=' . uri_escape($version);
423                            },
424    recent_changes_link  => 'http://example.com/?RecentChanges',
425
426    # Optional arguments:
427    site_description     => 'My wiki about my stuff',
428    interwiki_identifier => 'MyWiki',
429    make_diff_url        => sub
430                            {
431                              my $node_name = shift;
432                              return 'http://example.com/?diff=' . uri_escape($node_name)
433                            },
434    make_history_url     => sub
435                            {
436                              my $node_name = shift;
437                              return 'http://example.com/?hist=' . uri_escape($node_name)
438                            },
439    software_name        => $your_software_name,     # e.g. "CGI::Wiki"
440    software_version     => $your_software_version,  # e.g. "0.73"
441    software_homepage    => $your_software_homepage, # e.g. "http://search.cpan.org/dist/Wiki-Toolkit/"
442  );
443
444C<wiki> must be a L<Wiki::Toolkit> object. C<make_node_url>, and
445C<make_diff_url> and C<make_history_url>, if supplied, must be coderefs.
446
447The mandatory arguments are:
448
449=over 4
450
451=item * wiki
452
453=item * site_name
454
455=item * site_url
456
457=item * make_node_url
458
459=item * recent_changes_link
460
461=back
462
463The three optional arguments
464
465=over 4
466
467=item * software_name
468
469=item * software_version
470
471=item * software_homepage
472
473=back
474
475are used to generate DOAP (Description Of A Project - see L<http://usefulinc.com/doap>) metadata
476for the feed to show what generated it.
477
478=head2 C<recent_changes()>
479
480  $wiki->write_node(
481                     'About This Wiki',
482                     'blah blah blah',
483                                 $checksum,
484                           {
485                       comment  => 'Stub page, please update!',
486                                   username => 'Fred',
487                     }
488  );
489
490  print "Content-type: application/xml\n\n";
491  print $rss->recent_changes;
492
493  # Or get something other than the default of the latest 15 changes.
494  print $rss->recent_changes( items => 50 );
495  print $rss->recent_changes( days => 7 );
496
497  # Or ignore minor edits.
498  print $rss->recent_changes( ignore_minor_edits => 1 );
499
500  # Personalise your feed further - consider only changes
501  # made by Fred to pages about bookshops.
502  print $rss->recent_changes(
503             filter_on_metadata => {
504                         username => 'Fred',
505                         category => 'Bookshops',
506                       },
507              );
508
509If using C<filter_on_metadata>, note that only changes satisfying
510I<all> criteria will be returned.
511
512B<Note:> Many of the fields emitted by the RSS generator are taken
513from the node metadata. The form of this metadata is I<not> mandated
514by L<Wiki::Toolkit>. Your wiki application should make sure to store some or
515all of the following metadata when calling C<write_node>:
516
517=over 4
518
519=item B<comment> - a brief comment summarising the edit that has just been made; will be used in the RDF description for this item.  Defaults to the empty string.
520
521=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 the empty string.
522
523=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 Dublin Core contributor for this item.  Defaults to the empty string.
524
525=item B<major_change> - true if the edit was a major edit and false if it was a minor edit; used for the importance of the item.  Defaults to true (ie if C<major_change> was not defined or was explicitly stored as C<undef>).
526
527=back
528
529=head2 C<feed_timestamp()>
530
531  print $rss->feed_timestamp();
532
533Returns the timestamp of the feed in POSIX::strftime style ("Tue, 29 Feb 2000
53412:34:56 GMT"), which is equivalent to the timestamp of the most recent item
535in the feed. Takes the same arguments as recent_changes(). You will most likely
536need this to print a Last-Modified HTTP header so user-agents can determine
537whether they need to reload the feed or not.
538 
539=head1 SEE ALSO
540
541=over 4
542
543=item * L<Wiki::Toolkit>
544
545=item * L<http://web.resource.org/rss/1.0/spec>
546
547=item * L<http://www.usemod.com/cgi-bin/mb.pl?ModWiki>
548
549=back
550
551=head1 MAINTAINER
552
553The Wiki::Toolkit project. Originally by Kake Pugh <kake@earth.li>.
554
555=head1 COPYRIGHT AND LICENSE
556
557Copyright 2003-4 Kake Pugh.
558Copyright 2005 Earle Martin.
559Copyright 2006 the Wiki::Toolkit team
560
561This module is free software; you can redistribute it and/or modify it
562under the same terms as Perl itself.
563
564=head1 THANKS
565
566The members of the Semantic Web Interest Group channel on irc.freenode.net,
567#swig, were very useful in the development of this module.
568
569=cut
Note: See TracBrowser for help on using the repository browser.