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} : "feed/".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 $IkiWiki::config{rebuild} ||
192 time - $feed->{lastupdate} >= $feed->{updateinterval};
193 $feed->{lastupdate}=time;
195 $IkiWiki::forcerebuild{$feed->{sourcepage}}=1;
197 IkiWiki::debug("checking feed ".$feed->{name}." ...");
199 if (! length $feed->{feedurl}) {
200 my @urls=XML::Feed->find_feeds($feed->{url});
202 $feed->{message}="could not find feed at ".$feed->{feedurl};
203 IkiWiki::debug($feed->{message});
206 $feed->{feedurl}=pop @urls;
208 my $f=eval{XML::Feed->parse(URI->new($feed->{feedurl}))};
210 $feed->{message}="feed crashed XML::Feed! $@";
211 IkiWiki::debug($feed->{message});
215 $feed->{message}=XML::Feed->errstr;
216 IkiWiki::debug($feed->{message});
220 foreach my $entry ($f->entries) {
223 title => defined $entry->title ? decode_entities($entry->title) : "untitled",
224 link => $entry->link,
225 content => $entry->content->body,
226 guid => defined $entry->id ? $entry->id : time."_".$feed->name,
227 ctime => $entry->issued ? ($entry->issued->epoch || time) : time,
231 $feed->{message}="processed ok";
237 sub add_page (@) { #{{{
240 my $feed=$params{feed};
243 if (exists $guids{$params{guid}}) {
244 # updating an existing post
245 $guid=$guids{$params{guid}};
249 $guid->{guid}=$params{guid};
250 $guids{$params{guid}}=$guid;
251 $mtime=$params{ctime};
255 # assign it an unused page
256 my $page=IkiWiki::titlepage($params{title});
257 # escape slashes and periods in title so it doesn't specify
258 # directory name or trigger ".." disallowing code.
259 $page=~s!([/.])!"__".ord($1)."__"!eg;
260 $page=$feed->{dir}."/".$page;
262 ($page)=$page=~/$IkiWiki::config{wiki_file_regexp}/;
263 if (! defined $page || ! length $page) {
264 $page=$feed->{dir}."/item";
267 while (exists $IkiWiki::pagesources{$page.$c} ||
268 -e pagefile($page.$c)) {
272 IkiWiki::debug("creating new page $page");
274 $guid->{feed}=$feed->{name};
276 # To write or not to write? Need to avoid writing unchanged pages
277 # to avoid unneccessary rebuilding. The mtime from rss cannot be
278 # trusted; let's use a digest.
279 eval q{use Digest::MD5 'md5_hex'};
281 my $digest=md5_hex(Encode::encode_utf8($params{content}));
282 return unless ! exists $guid->{md5} || $guid->{md5} ne $digest || $IkiWiki::config{rebuild};
283 $guid->{md5}=$digest;
286 my $template=IkiWiki::template("aggregatepost.tmpl", blind_cache => 1);
287 my $content=$params{content};
288 $params{content}=~s/(?<!\\)\[\[/\\\[\[/g; # escape accidental wikilinks
289 # and preprocessor stuff
290 $template->param(title => $params{title})
291 if defined $params{title} && length($params{title});
292 $template->param(content => $params{content});
293 $template->param(url => $feed->{url});
294 $template->param(name => $feed->{name});
295 $template->param(link => $params{link}) if defined $params{link};
296 if (ref $feed->{tags}) {
297 $template->param(tags => [map { tag => $_ }, @{$feed->{tags}}]);
299 IkiWiki::writefile($guid->{page}.".html", $IkiWiki::config{srcdir},
302 # Set the mtime, this lets the build process get the right creation
303 # time on record for the new page.
304 utime $mtime, $mtime, pagefile($guid->{page}) if defined $mtime;
307 sub remove_feeds () { #{{{
311 foreach my $id (keys %feeds) {
312 if ($feeds{$id}->{sourcepage} eq $page) {
313 $feeds{$id}->{remove}=1;
319 sub pagefile ($) { #{{{
322 return "$IkiWiki::config{srcdir}/$page.html";