2 # Blog aggregation plugin.
3 package IkiWiki::Plugin::aggregate;
17 hook(type => "getopt", id => "aggregate", call => \&getopt);
18 hook(type => "checkconfig", id => "aggregate", call => \&checkconfig);
19 hook(type => "filter", id => "aggregate", call => \&filter);
20 hook(type => "preprocess", id => "aggregate", call => \&preprocess);
21 hook(type => "delete", id => "aggregate", call => \&delete);
22 hook(type => "savestate", id => "aggregate", call => \&savestate);
26 eval q{use Getopt::Long};
27 Getopt::Long::Configure('pass_through');
28 GetOptions("aggregate" => \$config{aggregate});
31 sub checkconfig () { #{{{
34 if ($config{aggregate}) {
39 IkiWiki::unlockwiki();
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} : $params{page}."/".IkiWiki::titlepage($params{name});
75 ($dir)=$dir=~/$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}." posts".
97 ($feed->{newposts} ? "; ".$feed->{newposts}." new" : "").
101 sub delete (@) { #{{{
104 # Remove feed data for removed pages.
105 foreach my $file (@files) {
106 my $page=pagename($file);
111 sub loadstate () { #{{{
112 if (-e "$config{wikistatedir}/aggregate") {
113 open (IN, "$config{wikistatedir}/aggregate" ||
114 die "$config{wikistatedir}/aggregate: $!");
116 $_=IkiWiki::possibly_foolish_untaint($_);
119 foreach my $i (split(/ /, $_)) {
120 my ($field, $val)=split(/=/, $i, 2);
121 if ($field eq "name" || $field eq "feed" ||
122 $field eq "guid" || $field eq "message") {
123 $data->{$field}=decode_entities($val, " \t\n");
125 elsif ($field eq "tag") {
126 push @{$data->{tags}}, $val;
129 $data->{$field}=$val;
133 if (exists $data->{name}) {
134 $feeds{$data->{name}}=$data;
136 elsif (exists $data->{guid}) {
137 $guids{$data->{guid}}=$data;
145 sub savestate () { #{{{
146 eval q{use HTML::Entities};
148 open (OUT, ">$config{wikistatedir}/aggregate" ||
149 die "$config{wikistatedir}/aggregate: $!");
150 foreach my $data (values %feeds, values %guids) {
151 if ($data->{remove}) {
153 foreach my $guid (values %guids) {
154 if ($guid->{feed} eq $data->{name}) {
160 unlink pagefile($data->{page});
166 foreach my $field (keys %$data) {
167 if ($field eq "name" || $field eq "feed" ||
168 $field eq "guid" || $field eq "message") {
169 push @line, "$field=".encode_entities($data->{$field}, " \t\n");
171 elsif ($field eq "tags") {
172 push @line, "tag=$_" foreach @{$data->{tags}};
175 push @line, "$field=".$data->{$field};
178 print OUT join(" ", @line)."\n";
183 sub aggregate () { #{{{
184 eval q{use XML::Feed};
186 eval q{use HTML::Entities};
189 foreach my $feed (values %feeds) {
190 next unless $config{rebuild} ||
191 time - $feed->{lastupdate} >= $feed->{updateinterval};
192 $feed->{lastupdate}=time;
194 $IkiWiki::forcerebuild{$feed->{sourcepage}}=1;
196 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 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 debug($feed->{message});
214 $feed->{message}=XML::Feed->errstr;
215 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 at ".
231 displaytime($feed->{lastupdate});
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;
261 ($page)=$page=~/$config{wiki_file_regexp}/;
262 if (! defined $page || ! length $page) {
263 $page=$feed->{dir}."/item";
266 while (exists $IkiWiki::pagecase{lc $page.$c} ||
267 -e pagefile($page.$c)) {
271 debug("creating new page $page");
273 $guid->{feed}=$feed->{name};
275 # To write or not to write? Need to avoid writing unchanged pages
276 # to avoid unneccessary rebuilding. The mtime from rss cannot be
277 # trusted; let's use a digest.
278 eval q{use Digest::MD5 'md5_hex'};
280 my $digest=md5_hex(Encode::encode_utf8($params{content}));
281 return unless ! exists $guid->{md5} || $guid->{md5} ne $digest || $config{rebuild};
282 $guid->{md5}=$digest;
285 my $template=template("aggregatepost.tmpl", blind_cache => 1);
286 $template->param(title => $params{title})
287 if defined $params{title} && length($params{title});
288 $template->param(content => htmlescape(htmlabs($params{content}, $feed->{feedurl})));
289 $template->param(name => $feed->{name});
290 $template->param(url => $feed->{url});
291 $template->param(permalink => urlabs($params{link}, $feed->{feedurl}))
292 if defined $params{link};
293 if (ref $feed->{tags}) {
294 $template->param(tags => [map { tag => $_ }, @{$feed->{tags}}]);
296 writefile(htmlpage($guid->{page}), $config{srcdir},
299 # Set the mtime, this lets the build process get the right creation
300 # time on record for the new page.
301 utime $mtime, $mtime, pagefile($guid->{page}) if defined $mtime;
304 sub htmlescape ($) { #{{{
305 # escape accidental wikilinks and preprocessor stuff
307 $html=~s/(?<!\\)\[\[/\\\[\[/g;
311 sub urlabs ($$) { #{{{
315 URI->new_abs($url, $urlbase)->as_string;
318 sub htmlabs ($$) { #{{{
319 # Convert links in html from relative to absolute.
320 # Note that this is a heuristic, which is not specified by the rss
321 # spec and may not be right for all feeds. Also, see Debian
327 my $p = HTML::Parser->new(api_version => 3);
328 $p->handler(default => sub { $ret.=join("", @_) }, "text");
329 $p->handler(start => sub {
330 my ($tagname, $pos, $text) = @_;
331 if (ref $HTML::Tagset::linkElements{$tagname}) {
333 # use attribute sets from right to left
334 # to avoid invalidating the offsets
335 # when replacing the values
336 my($k_offset, $k_len, $v_offset, $v_len) =
338 my $attrname = lc(substr($text, $k_offset, $k_len));
339 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
340 next unless $v_offset; # 0 v_offset means no value
341 my $v = substr($text, $v_offset, $v_len);
342 $v =~ s/^([\'\"])(.*)\1$/$2/;
343 my $new_v=urlabs($v, $urlbase);
344 $new_v =~ s/\"/"/g; # since we quote with ""
345 substr($text, $v_offset, $v_len) = qq("$new_v");
349 }, "tagname, tokenpos, text");
356 sub remove_feeds () { #{{{
360 foreach my $id (keys %feeds) {
361 if ($feeds{$id}->{sourcepage} eq $page) {
362 $feeds{$id}->{remove}=1;
368 sub pagefile ($) { #{{{
371 return "$config{srcdir}/".htmlpage($page);