Changeset 329


Ignore:
Timestamp:
Jun 15, 2006, 5:17:46 PM (13 years ago)
Author:
nick
Message:

Refactor, and add support for a minimal kind of feed (eg as would come from a search). Also include more geo data in the feed

Location:
wiki-toolkit/trunk/lib/Wiki/Toolkit/Feed
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • wiki-toolkit/trunk/lib/Wiki/Toolkit/Feed/Atom.pm

    r320 r329  
    5050}
    5151
    52 =item <generate_node_list_feed>
    53  
    54 Generate and return an Atom feed for a list of nodes
    55  
    56 =cut
    57 sub generate_node_list_feed {
    58   my ($self,$atom_timestamp,@nodes) = @_;
     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) = @_;
    5960
    6061  my $generator = '';
     
    7576  my $atom = qq{<?xml version="1.0" encoding="UTF-8"?>
    7677
    77 <feed xmlns="http://www.w3.org/2005/Atom">
     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>
    7883
    7984  <link href="}            . $self->{site_url}     . qq{" />
     
    8388  <id>}                    . $self->{site_url}     . qq{</id>
    8489  $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);
    85114
    86115  my (@urls, @items);
     
    132161    }
    133162
     163    # Include geospacial data, if we have it
     164    my $geo_atom = "";
     165    if($node->{metadata}->{latitude}) {
     166        $geo_atom .= "  <geo:lat>".$node->{metadata}->{latitude}."</geo:lat>\n";
     167    }
     168    if($node->{metadata}->{longitude}) {
     169        $geo_atom .= "  <geo:long>".$node->{metadata}->{longitude}."</geo:long>\n";
     170    }
     171    if($node->{metadata}->{os_x}) {
     172        $geo_atom .= "  <space:os_x>".$node->{metadata}->{os_x}."</space:os_x>\n";
     173    }
     174    if($node->{metadata}->{os_y}) {
     175        $geo_atom .= "  <space:os_y>".$node->{metadata}->{os_y}."</space:os_y>\n";
     176    }
     177    if($node->{metadata}->{distance}) {
     178        $geo_atom .= "  <space:distance>".$node->{metadata}->{distance}."</space:distance>\n";
     179    }
     180
    134181    # TODO: Find an Atom equivalent of ModWiki, so we can include more info
     182
    135183   
    136184    push @items, qq{
     
    143191    <author><name>$author</name></author>
    144192$category_atom
     193$geo_atom
    145194  </entry>
    146195};
     
    148197  }
    149198 
    150   $atom .= join('', @items) . "\n</feed>\n";
     199  $atom .= join('', @items) . "\n";
     200  $atom .= $self->build_feed_end($atom_timestamp);
     201
     202  return $atom;   
     203}
     204
     205=item <generate_node_name_distance_feed>
     206 
     207Generate a very cut down atom feed, based just on the nodes, their locations
     208(if given), and their distance from a reference location (if given).
     209
     210Typically used on search feeds.
     211 
     212=cut
     213sub generate_node_name_distance_feed {
     214  my ($self,$atom_timestamp,@nodes) = @_;
     215
     216  my $atom = $self->build_feed_start($atom_timestamp);
     217
     218  my (@urls, @items);
     219
     220  foreach my $node (@nodes)
     221  {
     222    my $node_name = $node->{name};
     223
     224    my $url = $self->{make_node_url}->($node_name);
     225
     226    # make XML-clean
     227    my $title =  $node_name;
     228       $title =~ s/&/&amp;/g;
     229       $title =~ s/</&lt;/g;
     230       $title =~ s/>/&gt;/g;
     231
     232    push @items, qq{
     233  <entry>
     234    <title>$title</title>
     235    <link href="$url" />
     236    <id>$url</id>
     237  </entry>
     238};
     239
     240  }
     241 
     242  $atom .= join('', @items) . "\n";
     243  $atom .= $self->build_feed_end($atom_timestamp);
    151244
    152245  return $atom;   
  • wiki-toolkit/trunk/lib/Wiki/Toolkit/Feed/Listing.pm

    r315 r329  
    167167=cut
    168168sub generate_node_list_feed { die("Not implemented by feed renderer!"); }
     169=item B<generate_node_name_distance_feed>
     170All implementing feed renderers must implement a method to produce a
     171stripped down feed from the supplied list of node names, and optionally
     172locations and distance from a reference point.
     173=cut
    169174
    1701751;
  • wiki-toolkit/trunk/lib/Wiki/Toolkit/Feed/RSS.pm

    r320 r329  
    5151}
    5252
    53 
    54 =item <generate_node_list_feed>
    55 
    56 Generate and return an RSS feed for a list of nodes
    57 
    58 =cut
    59 sub generate_node_list_feed {
    60   my ($self,$feed_timestamp,@nodes) = @_;
     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) = @_;
    6161
    6262  #"http://purl.org/rss/1.0/modules/wiki/"
    63   my $rss = qq{<?xml version="1.0" encoding="UTF-8"?>
     63  return qq{<?xml version="1.0" encoding="UTF-8"?>
    6464
    6565<rdf:RDF
     
    7171 xmlns:rdfs    = "http://www.w3.org/2000/01/rdf-schema#"
    7272 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/"
    7375>
    74 
    75 <channel rdf:about="">
    76 
    77 <dc:publisher>}       . $self->{site_url}   . qq{</dc:publisher>\n};
     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};
    7888
    7989if ($self->{software_name})
     
    110120<modwiki:interwiki>}     . $self->{interwiki_identifier} . qq{</modwiki:interwiki>};
    111121
     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
    112155  my (@urls, @items);
    113 
    114156  foreach my $node (@nodes)
    115157  {
     
    177219        }
    178220    }
    179    
     221
     222    # Include geospacial data, if we have it
     223    my $geo_rss = "";
     224    if($node->{metadata}->{latitude}) {
     225        $geo_rss .= "  <geo:lat>".$node->{metadata}->{latitude}."</geo:lat>\n";
     226    }
     227    if($node->{metadata}->{longitude}) {
     228        $geo_rss .= "  <geo:long>".$node->{metadata}->{longitude}."</geo:long>\n";
     229    }
     230    if($node->{metadata}->{os_x}) {
     231        $geo_rss .= "  <space:os_x>".$node->{metadata}->{os_x}."</space:os_x>\n";
     232    }
     233    if($node->{metadata}->{os_y}) {
     234        $geo_rss .= "  <space:os_y>".$node->{metadata}->{os_y}."</space:os_y>\n";
     235    }
     236    if($node->{metadata}->{distance}) {
     237        $geo_rss .= "  <space:distance>".$node->{metadata}->{distance}."</space:distance>\n";
     238    }
     239
    180240    push @items, qq{
    181241<item rdf:about="$url">
     
    192252  <rdfs:seeAlso rdf:resource="$rdf_url" />
    193253$category_rss
     254$geo_rss
    194255</item>
    195256};
    196257  }
    197258 
     259  # Output the items list
    198260  $rss .= qq{
    199261
     
    204266
    205267</channel>
    206 } . join('', @items) . "\n</rdf:RDF>\n";
     268};
     269
     270  # Output the individual item entries
     271  $rss .= join('', @items) . "\n";
     272
     273  # Finish up
     274  $rss .= $self->build_feed_end($feed_timestamp);
     275 
     276  return $rss;   
     277}
     278
     279
     280=item B<generate_node_name_distance_feed>
     281
     282Generate a very cut down rss feed, based just on the nodes, their locations
     283(if given), and their distance from a reference location (if given).
     284
     285Typically used on search feeds.
     286
     287=cut
     288sub generate_node_name_distance_feed {
     289  my ($self,$feed_timestamp,@nodes) = @_;
     290
     291  # Start our feed
     292  my $rss = $self->build_feed_start($feed_timestamp);
     293  $rss .= qq{
     294
     295<channel rdf:about="">
     296
     297};
     298  $rss .= $self->build_feed_mid($feed_timestamp);
     299
     300  # Generate the items list, and the individiual item entries
     301  my (@urls, @items);
     302  foreach my $node (@nodes)
     303  {
     304    my $node_name = $node->{name};
     305
     306    my $url = $self->{make_node_url}->($node_name);
     307
     308    push @urls, qq{    <rdf:li rdf:resource="$url" />\n};
     309
     310    my $rdf_url =  $url;
     311       $rdf_url =~ s/\?/\?id=/;
     312       $rdf_url .= ';format=rdf';
     313
     314    # make XML-clean
     315    my $title =  $node_name;
     316       $title =~ s/&/&amp;/g;
     317       $title =~ s/</&lt;/g;
     318       $title =~ s/>/&gt;/g;
     319
     320    # What location stuff do we have?
     321    my $location = undef;
     322    my $distance = undef;
     323
     324    push @items, qq{
     325<item rdf:about="$url">
     326  <title>$title</title>
     327  <link>$url</link>
     328  <rdfs:seeAlso rdf:resource="$rdf_url" />
     329</item>
     330};
     331  }
     332 
     333  # Output the items list
     334  $rss .= qq{
     335
     336<items>
     337  <rdf:Seq>
     338} . join('', @urls) . qq{  </rdf:Seq>
     339</items>
     340
     341</channel>
     342};
     343
     344  # Output the individual item entries
     345  $rss .= join('', @items) . "\n";
     346
     347  # Finish up
     348  $rss .= $self->build_feed_end($feed_timestamp);
    207349 
    208350  return $rss;   
Note: See TracChangeset for help on using the changeset viewer.