source: wiki-toolkit/trunk/lib/Wiki/Toolkit/Feed/Listing.pm @ 329

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

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

File size: 4.8 KB
Line 
1package Wiki::Toolkit::Feed::Listing;
2
3use strict;
4
5=head1 NAME
6
7Wiki::Toolkit::Feed::Listing - parent class for Feeds from Wiki::Toolkit.
8
9Handles common data fetching tasks, so that child classes need only
10worry about formatting the feeds.
11
12Also enforces some common methods that must be implemented.
13
14=cut
15
16
17=item B<fetch_recently_changed_nodes>
18
19Based on the supplied criteria, fetch a list of the recently changed nodes
20
21=cut
22sub fetch_recently_changed_nodes {
23    my ($self, %args) = @_;
24
25    my $wiki = $self->{wiki};
26
27    my %criteria = (
28                   ignore_case => 1,
29                   );
30
31    # If we're not passed any parameters to limit the items returned,
32    #  default to 15.
33    $args{days} ? $criteria{days}           = $args{days}
34                : $criteria{last_n_changes} = $args{items} || 15;
35 
36    $criteria{metadata_wasnt} = { major_change => 0 }     if $args{ignore_minor_edits};
37    $criteria{metadata_was}   = $args{filter_on_metadata} if $args{filter_on_metadata};
38
39    my @changes = $wiki->list_recent_changes(%criteria);
40
41    return @changes;
42}
43
44=item B<fetch_oldest_for_recently_changed>
45
46Based on the supplied criteria (but not using all of those used by
47B<fetch_recently_changed_nodes>), find the newest node from the recently
48changed nodes set. Normally used for dating the whole of a Feed.
49
50=cut
51sub fetch_newest_for_recently_changed {
52    my ($self, %args) = @_;
53
54    my %criteria = (ignore_case => 1);
55
56    $args{days} ? $criteria{days}           = $args{days}
57                : $criteria{last_n_changes} = $args{items} || 15;
58
59    $criteria{metadata_wasnt} = { major_change => 0 }     if $args{ignore_minor_edits};
60    $criteria{metadata_was}   = $args{filter_on_metadata} if $args{filter_on_metadata};
61
62    my @changes = $self->{wiki}->list_recent_changes(%criteria);
63
64    return $changes[0];
65}
66
67
68=item B<fetch_node_all_versions>
69
70For a given node (name or ID), return all the versions there have been,
71including all metadata required for it to go into a "recent changes"
72style listing.
73
74=cut
75sub fetch_node_all_versions {
76    my ($self, %args) = @_;
77
78    # Check we got the right options
79    unless($args{'name'}) {
80        return ();
81    }
82
83    # Do the fetch
84    my @nodes = $self->{wiki}->list_node_all_versions(
85                        name => $args{'name'},
86                        with_content => 0,
87                        with_metadata => 1,
88    );
89
90    # Ensure that all the metadata fields are arrays and not strings
91    foreach my $node (@nodes) {
92        foreach my $mdk (keys %{$node->{'metadata'}}) {
93            unless(ref($node->{'metadata'}->{$mdk}) eq "ARRAY") {
94                $node->{'metadata'}->{$mdk} = [ $node->{'metadata'}->{$mdk} ];
95            }
96        }
97    }
98
99    return @nodes;
100}
101
102
103=item B<recent_changes>
104
105Build an Atom Feed of the recent changes to the Wiki::Toolkit instance,
106using any supplied parameters to narrow the results.
107
108If the argument "also_return_timestamp" is supplied, it will return an
109array of the feed, and the feed timestamp. Otherwise it just returns the feed.
110
111=cut
112sub recent_changes
113{
114    my ($self, %args) = @_;
115
116    my @changes = $self->fetch_recently_changed_nodes(%args);
117    my $feed_timestamp = $self->feed_timestamp(
118                              $self->fetch_newest_for_recently_changed(%args)
119    );
120
121    my $feed = $self->generate_node_list_feed($feed_timestamp, @changes);
122
123    if($args{'also_return_timestamp'}) {
124        return ($feed,$feed_timestamp);
125    } else {
126        return $feed;
127    }
128}
129
130
131=item B<node_all_versions>
132
133Build an Atom Feed of all the different versions of a given node.
134
135If the argument "also_return_timestamp" is supplied, it will return an
136array of the feed, and the feed timestamp. Otherwise it just returns the feed.
137
138=cut
139sub node_all_versions
140{
141    my ($self, %args) = @_;
142
143    my @all_versions = $self->fetch_node_all_versions(%args);
144    my $feed_timestamp = $self->feed_timestamp( $all_versions[0] );
145
146    my $feed = $self->generate_node_list_feed($feed_timestamp, @all_versions);
147
148    if($args{'also_return_timestamp'}) {
149        return ($feed,$feed_timestamp);
150    } else {
151        return $feed;
152    }
153} 
154
155
156
157# The following are methods that any feed renderer must provide
158
159=item B<feed_timestamp>
160All implementing feed renderers must implement a method to produce a
161feed specific timestamp, based on the supplied node
162=cut
163sub feed_timestamp          { die("Not implemented by feed renderer!"); }
164=item B<generate_node_list_feed>
165All implementing feed renderers must implement a method to produce a
166feed from the supplied list of nodes
167=cut
168sub 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
174
1751;
Note: See TracBrowser for help on using the repository browser.