2 # Blog aggregation plugin.
3 package IkiWiki::Plugin::aggregate;
12 use open qw{:utf8 :std};
18 hook(type => "getopt", id => "aggregate", call => \&getopt);
19 hook(type => "checkconfig", id => "aggregate", call => \&checkconfig);
20 hook(type => "filter", id => "aggregate", call => \&filter);
21 hook(type => "preprocess", id => "aggregate", call => \&preprocess);
22 hook(type => "delete", id => "aggregate", call => \&delete);
23 hook(type => "savestate", id => "aggregate", call => \&savestate);
27 eval q{use Getopt::Long};
28 Getopt::Long::Configure('pass_through');
29 GetOptions("aggregate" => \$config{aggregate});
32 sub checkconfig () { #{{{
35 if ($config{aggregate}) {
41 IkiWiki::unlockwiki();
46 my $page=$params{page};
48 # Mark all feeds originating on this page as removable;
49 # preprocess will unmark those that still exist.
52 return $params{content};
55 sub preprocess (@) { #{{{
58 foreach my $required (qw{name url}) {
59 if (! exists $params{$required}) {
60 return "[[aggregate plugin missing $required parameter]]";
65 my $name=$params{name};
66 if (exists $feeds{$name}) {
73 $feed->{sourcepage}=$params{page};
74 $feed->{url}=$params{url};
75 my $dir=exists $params{dir} ? $params{dir} : $params{page}."/".IkiWiki::titlepage($params{name});
77 ($dir)=$dir=~/$config{wiki_file_regexp}/;
79 $feed->{feedurl}=defined $params{feedurl} ? $params{feedurl} : "";
80 $feed->{updateinterval}=defined $params{updateinterval} ? $params{updateinterval} * 60 : 15 * 60;
81 $feed->{expireage}=defined $params{expireage} ? $params{expireage} : 0;
82 $feed->{expirecount}=defined $params{expirecount} ? $params{expirecount} : 0;
83 delete $feed->{remove};
84 delete $feed->{expired};
85 $feed->{lastupdate}=0 unless defined $feed->{lastupdate};
86 $feed->{numposts}=0 unless defined $feed->{numposts};
87 $feed->{newposts}=0 unless defined $feed->{newposts};
88 $feed->{message}="new feed" unless defined $feed->{message};
89 $feed->{error}=0 unless defined $feed->{error};
95 push @{$feed->{tags}}, $value;
99 return "<a href=\"".$feed->{url}."\">".$feed->{name}."</a>: ".
100 ($feed->{error} ? "<em>" : "").$feed->{message}.
101 ($feed->{error} ? "</em>" : "").
102 " (".$feed->{numposts}." posts".
103 ($feed->{newposts} ? "; ".$feed->{newposts}." new" : "").
107 sub delete (@) { #{{{
110 # Remove feed data for removed pages.
111 foreach my $file (@files) {
112 my $page=pagename($file);
117 sub loadstate () { #{{{
118 if (-e "$config{wikistatedir}/aggregate") {
119 open (IN, "$config{wikistatedir}/aggregate" ||
120 die "$config{wikistatedir}/aggregate: $!");
122 $_=IkiWiki::possibly_foolish_untaint($_);
125 foreach my $i (split(/ /, $_)) {
126 my ($field, $val)=split(/=/, $i, 2);
127 if ($field eq "name" || $field eq "feed" ||
128 $field eq "guid" || $field eq "message") {
129 $data->{$field}=decode_entities($val, " \t\n");
131 elsif ($field eq "tag") {
132 push @{$data->{tags}}, $val;
135 $data->{$field}=$val;
139 if (exists $data->{name}) {
140 $feeds{$data->{name}}=$data;
142 elsif (exists $data->{guid}) {
143 $guids{$data->{guid}}=$data;
151 sub savestate () { #{{{
152 eval q{use HTML::Entities};
154 open (OUT, ">$config{wikistatedir}/aggregate" ||
155 die "$config{wikistatedir}/aggregate: $!");
156 foreach my $data (values %feeds, values %guids) {
157 if ($data->{remove}) {
159 foreach my $guid (values %guids) {
160 if ($guid->{feed} eq $data->{name}) {
166 unlink pagefile($data->{page});
170 elsif ($data->{expired} && exists $data->{page}) {
171 unlink pagefile($data->{page});
172 delete $data->{page};
177 foreach my $field (keys %$data) {
178 if ($field eq "name" || $field eq "feed" ||
179 $field eq "guid" || $field eq "message") {
180 push @line, "$field=".encode_entities($data->{$field}, " \t\n");
182 elsif ($field eq "tags") {
183 push @line, "tag=$_" foreach @{$data->{tags}};
186 push @line, "$field=".$data->{$field};
189 print OUT join(" ", @line)."\n";
195 foreach my $feed (values %feeds) {
196 next unless $feed->{expireage} || $feed->{expirecount};
198 foreach my $item (sort { $IkiWiki::pagectime{$b->{page}} <=> $IkiWiki::pagectime{$a->{page}} }
199 grep { exists $_->{page} && $_->{feed} eq $feed->{name} && $IkiWiki::pagectime{$_->{page}} }
201 if ($feed->{expireage}) {
202 my $days_old = (time - $IkiWiki::pagectime{$item->{page}}) / 60 / 60 / 24;
203 if ($days_old > $feed->{expireage}) {
204 debug("expiring ".$item->{page}." ($days_old days old)");
208 elsif ($feed->{expirecount} &&
209 $count >= $feed->{expirecount}) {
210 debug("expiring ".$item->{page});
220 sub aggregate () { #{{{
221 eval q{use XML::Feed};
223 eval q{use HTML::Entities};
226 foreach my $feed (values %feeds) {
227 next unless $config{rebuild} ||
228 time - $feed->{lastupdate} >= $feed->{updateinterval};
229 $feed->{lastupdate}=time;
231 $IkiWiki::forcerebuild{$feed->{sourcepage}}=1;
233 debug("checking feed ".$feed->{name}." ...");
235 if (! length $feed->{feedurl}) {
236 my @urls=XML::Feed->find_feeds($feed->{url});
238 $feed->{message}="could not find feed at ".$feed->{feedurl};
240 debug($feed->{message});
243 $feed->{feedurl}=pop @urls;
245 my $f=eval{XML::Feed->parse(URI->new($feed->{feedurl}))};
247 $feed->{message}="feed crashed XML::Feed! $@";
249 debug($feed->{message});
253 $feed->{message}=XML::Feed->errstr;
255 debug($feed->{message});
259 foreach my $entry ($f->entries) {
262 title => defined $entry->title ? decode_entities($entry->title) : "untitled",
263 link => $entry->link,
264 content => $entry->content->body,
265 guid => defined $entry->id ? $entry->id : time."_".$feed->name,
266 ctime => $entry->issued ? ($entry->issued->epoch || time) : time,
270 $feed->{message}="processed ok at ".
271 displaytime($feed->{lastupdate});
276 sub add_page (@) { #{{{
279 my $feed=$params{feed};
282 if (exists $guids{$params{guid}}) {
283 # updating an existing post
284 $guid=$guids{$params{guid}};
285 return if $guid->{expired};
289 $guid->{guid}=$params{guid};
290 $guids{$params{guid}}=$guid;
291 $mtime=$params{ctime};
295 # assign it an unused page
296 my $page=IkiWiki::titlepage($params{title});
297 # escape slashes and periods in title so it doesn't specify
298 # directory name or trigger ".." disallowing code.
299 $page=~s!([/.])!"__".ord($1)."__"!eg;
300 $page=$feed->{dir}."/".$page;
301 ($page)=$page=~/$config{wiki_file_regexp}/;
302 if (! defined $page || ! length $page) {
303 $page=$feed->{dir}."/item";
306 while (exists $IkiWiki::pagecase{lc $page.$c} ||
307 -e pagefile($page.$c)) {
311 debug("creating new page $page");
313 $guid->{feed}=$feed->{name};
315 # To write or not to write? Need to avoid writing unchanged pages
316 # to avoid unneccessary rebuilding. The mtime from rss cannot be
317 # trusted; let's use a digest.
318 eval q{use Digest::MD5 'md5_hex'};
320 my $digest=md5_hex(Encode::encode_utf8($params{content}));
321 return unless ! exists $guid->{md5} || $guid->{md5} ne $digest || $config{rebuild};
322 $guid->{md5}=$digest;
325 my $template=template("aggregatepost.tmpl", blind_cache => 1);
326 $template->param(title => $params{title})
327 if defined $params{title} && length($params{title});
328 $template->param(content => htmlescape(htmlabs($params{content}, $feed->{feedurl})));
329 $template->param(name => $feed->{name});
330 $template->param(url => $feed->{url});
331 $template->param(permalink => urlabs($params{link}, $feed->{feedurl}))
332 if defined $params{link};
333 if (ref $feed->{tags}) {
334 $template->param(tags => [map { tag => $_ }, @{$feed->{tags}}]);
336 writefile(htmlpage($guid->{page}), $config{srcdir},
339 # Set the mtime, this lets the build process get the right creation
340 # time on record for the new page.
341 utime $mtime, $mtime, pagefile($guid->{page}) if defined $mtime;
344 sub htmlescape ($) { #{{{
345 # escape accidental wikilinks and preprocessor stuff
347 $html=~s/(?<!\\)\[\[/\\\[\[/g;
351 sub urlabs ($$) { #{{{
355 URI->new_abs($url, $urlbase)->as_string;
358 sub htmlabs ($$) { #{{{
359 # Convert links in html from relative to absolute.
360 # Note that this is a heuristic, which is not specified by the rss
361 # spec and may not be right for all feeds. Also, see Debian
367 my $p = HTML::Parser->new(api_version => 3);
368 $p->handler(default => sub { $ret.=join("", @_) }, "text");
369 $p->handler(start => sub {
370 my ($tagname, $pos, $text) = @_;
371 if (ref $HTML::Tagset::linkElements{$tagname}) {
373 # use attribute sets from right to left
374 # to avoid invalidating the offsets
375 # when replacing the values
376 my($k_offset, $k_len, $v_offset, $v_len) =
378 my $attrname = lc(substr($text, $k_offset, $k_len));
379 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
380 next unless $v_offset; # 0 v_offset means no value
381 my $v = substr($text, $v_offset, $v_len);
382 $v =~ s/^([\'\"])(.*)\1$/$2/;
383 my $new_v=urlabs($v, $urlbase);
384 $new_v =~ s/\"/"/g; # since we quote with ""
385 substr($text, $v_offset, $v_len) = qq("$new_v");
389 }, "tagname, tokenpos, text");
396 sub remove_feeds () { #{{{
400 foreach my $id (keys %feeds) {
401 if ($feeds{$id}->{sourcepage} eq $page) {
402 $feeds{$id}->{remove}=1;
408 sub pagefile ($) { #{{{
411 return "$config{srcdir}/".htmlpage($page);