2 # Blog aggregation plugin.
3 package IkiWiki::Plugin::aggregate;
13 IkiWiki::hook(type => "getopt", id => "aggregate",
15 IkiWiki::hook(type => "checkconfig", id => "aggregate",
16 call => \&checkconfig);
17 IkiWiki::hook(type => "filter", id => "aggregate",
19 IkiWiki::hook(type => "preprocess", id => "aggregate",
20 call => \&preprocess);
21 IkiWiki::hook(type => "delete", id => "aggregate",
23 IkiWiki::hook(type => "savestate", id => "aggregate",
28 eval q{use Getopt::Long};
29 Getopt::Long::Configure('pass_through');
30 GetOptions("aggregate" => \$IkiWiki::config{aggregate});
33 sub checkconfig () { #{{{
35 if ($IkiWiki::config{aggregate}) {
44 my $page=$params{page};
46 # Mark all feeds originating on this page as removable;
47 # preprocess will unmark those that still exist.
50 return $params{content};
53 sub preprocess (@) { #{{{
56 foreach my $required (qw{name url}) {
57 if (! exists $params{$required}) {
58 return "[[aggregate plugin missing $required parameter]]";
63 my $name=$params{name};
64 if (exists $feeds{$name}) {
71 $feed->{sourcepage}=$params{page};
72 $feed->{url}=$params{url};
73 my $dir=exists $params{dir} ? $params{dir} : IkiWiki::titlepage($params{name});
75 ($dir)=$dir=~/$IkiWiki::config{wiki_file_regexp}/;
77 $feed->{feedurl}=defined $params{feedurl} ? $params{feedurl} : "";
78 $feed->{updateinterval}=defined $params{updateinterval} ? $params{updateinterval} * 60 : 15 * 60;
79 $feed->{expireage}=defined $params{expireage} ? $params{expireage} : 0;
80 $feed->{expirecount}=defined $params{expirecount} ? $params{expirecount} : 0;
81 delete $feed->{remove};
82 $feed->{lastupdate}=0 unless defined $feed->{lastupdate};
83 $feed->{numposts}=0 unless defined $feed->{numposts};
84 $feed->{newposts}=0 unless defined $feed->{newposts};
85 $feed->{message}="new feed" unless defined $feed->{message};
91 push @{$feed->{tags}}, $value;
95 return "<a href=\"".$feed->{url}."\">".$feed->{name}."</a>: ".
96 "<i>".$feed->{message}."</i> (".$feed->{numposts}.
97 " stored posts; ".$feed->{newposts}." new)<br />";
100 sub delete (@) { #{{{
103 # Remove feed data for removed pages.
104 foreach my $file (@files) {
105 my $page=IkiWiki::pagename($file);
110 sub loadstate () { #{{{
111 eval q{use HTML::Entities};
113 if (-e "$IkiWiki::config{wikistatedir}/aggregate") {
114 open (IN, "$IkiWiki::config{wikistatedir}/aggregate" ||
115 die "$IkiWiki::config{wikistatedir}/aggregate: $!");
117 $_=IkiWiki::possibly_foolish_untaint($_);
120 foreach my $i (split(/ /, $_)) {
121 my ($field, $val)=split(/=/, $i, 2);
122 if ($field eq "name" || $field eq "feed" ||
123 $field eq "guid" || $field eq "message") {
124 $data->{$field}=decode_entities($val, " \t\n");
126 elsif ($field eq "tag") {
127 push @{$data->{tags}}, $val;
130 $data->{$field}=$val;
134 if (exists $data->{name}) {
135 $feeds{$data->{name}}=$data;
137 elsif (exists $data->{guid}) {
138 $guids{$data->{guid}}=$data;
146 sub savestate () { #{{{
147 eval q{use HTML::Entities};
149 open (OUT, ">$IkiWiki::config{wikistatedir}/aggregate" ||
150 die "$IkiWiki::config{wikistatedir}/aggregate: $!");
151 foreach my $data (values %feeds, values %guids) {
152 if ($data->{remove}) {
154 foreach my $guid (values %guids) {
155 if ($guid->{feed} eq $data->{name}) {
161 unlink pagefile($data->{page});
167 foreach my $field (keys %$data) {
168 if ($field eq "name" || $field eq "feed" ||
169 $field eq "guid" || $field eq "message") {
170 push @line, "$field=".encode_entities($data->{$field}, " \t\n");
172 elsif ($field eq "tags") {
173 push @line, "tag=$_" foreach @{$data->{tags}};
176 push @line, "$field=".$data->{$field};
179 print OUT join(" ", @line)."\n";
184 sub aggregate () { #{{{
185 eval q{use XML::Feed};
187 eval q{use HTML::Entities};
190 foreach my $feed (values %feeds) {
191 next unless time - $feed->{lastupdate} >= $feed->{updateinterval};
192 $feed->{lastupdate}=time;
194 $IkiWiki::forcerebuild{$feed->{sourcepage}}=1;
196 IkiWiki::debug("checking feed ".$feed->{name}." ...");
198 if (! length $feed->{feedurl}) {
199 my @urls=XML::Feed->find_feeds($feed->{url});
201 $feed->{message}="could not find feed at ".$feed->{feedurl};
202 IkiWiki::debug($feed->{message});
205 $feed->{feedurl}=pop @urls;
207 my $f=eval{XML::Feed->parse(URI->new($feed->{feedurl}))};
209 $feed->{message}="feed crashed XML::Feed! $@";
210 IkiWiki::debug($feed->{message});
214 $feed->{message}=XML::Feed->errstr;
215 IkiWiki::debug($feed->{message});
219 foreach my $entry ($f->entries) {
222 title => defined $entry->title ? decode_entities($entry->title) : "untitled",
223 link => $entry->link,
224 content => $entry->content->body,
225 guid => defined $entry->id ? $entry->id : time."_".$feed->name,
226 ctime => $entry->issued ? ($entry->issued->epoch || time) : time,
230 $feed->{message}="processed ok";
236 sub add_page (@) { #{{{
239 my $feed=$params{feed};
242 if (exists $guids{$params{guid}}) {
243 # updating an existing post
244 $guid=$guids{$params{guid}};
248 $guid->{guid}=$params{guid};
249 $guids{$params{guid}}=$guid;
250 $mtime=$params{ctime};
254 # assign it an unused page
255 my $page=$feed->{dir}."/".IkiWiki::titlepage($params{title});
257 ($page)=$page=~/$IkiWiki::config{wiki_file_regexp}/;
258 if (! defined $page || ! length $page) {
259 $page=$feed->{dir}."/item";
261 $page=~s/\.\.//g; # avoid ".." directory tricks
263 while (exists $IkiWiki::pagesources{$page.$c} ||
264 -e pagefile($page.$c)) {
268 IkiWiki::debug("creating new page $page");
270 $guid->{feed}=$feed->{name};
272 # To write or not to write? Need to avoid writing unchanged pages
273 # to avoid unneccessary rebuilding. The mtime from rss cannot be
274 # trusted; let's use a digest.
275 eval q{use Digest::MD5 'md5_hex'};
277 my $digest=md5_hex(Encode::encode_utf8($params{content}));
278 return unless ! exists $guid->{md5} || $guid->{md5} ne $digest;
279 $guid->{md5}=$digest;
282 my $template=IkiWiki::template("aggregatepost.tmpl", blind_cache => 1);
283 my $content=$params{content};
284 $params{content}=~s/(?<!\\)\[\[/\\\[\[/g; # escape accidental wikilinks
285 # and preprocessor stuff
286 $template->param(content => $params{content});
287 $template->param(url => $feed->{url});
288 $template->param(name => $feed->{name});
289 $template->param(link => $params{link}) if defined $params{link};
290 if (ref $feed->{tags}) {
291 $template->param(tags => [map { tag => $_ }, @{$feed->{tags}}]);
293 IkiWiki::writefile($guid->{page}.".html", $IkiWiki::config{srcdir},
296 # Set the mtime, this lets the build process get the right creation
297 # time on record for the new page.
298 utime $mtime, $mtime, pagefile($guid->{page}) if defined $mtime;
301 sub remove_feeds () { #{{{
305 foreach my $id (keys %feeds) {
306 if ($feeds{$id}->{sourcepage} eq $page) {
307 $feeds{$id}->{remove}=1;
313 sub pagefile ($) { #{{{
316 return "$IkiWiki::config{srcdir}/$page.html";