2 # Blog aggregation plugin.
3 package IkiWiki::Plugin::aggregate;
17 IkiWiki::hook(type => "getopt", id => "aggregate",
19 IkiWiki::hook(type => "checkconfig", id => "aggregate",
20 call => \&checkconfig);
21 IkiWiki::hook(type => "filter", id => "aggregate",
23 IkiWiki::hook(type => "preprocess", id => "aggregate",
24 call => \&preprocess);
25 IkiWiki::hook(type => "delete", id => "aggregate",
27 IkiWiki::hook(type => "savestate", id => "aggregate",
32 eval q{use Getopt::Long};
33 Getopt::Long::Configure('pass_through');
34 GetOptions("aggregate" => \$IkiWiki::config{aggregate});
37 sub checkconfig () { #{{{
40 if ($IkiWiki::config{aggregate}) {
45 IkiWiki::unlockwiki();
50 my $page=$params{page};
52 # Mark all feeds originating on this page as removable;
53 # preprocess will unmark those that still exist.
56 return $params{content};
59 sub preprocess (@) { #{{{
62 foreach my $required (qw{name url}) {
63 if (! exists $params{$required}) {
64 return "[[aggregate plugin missing $required parameter]]";
69 my $name=$params{name};
70 if (exists $feeds{$name}) {
77 $feed->{sourcepage}=$params{page};
78 $feed->{url}=$params{url};
79 my $dir=exists $params{dir} ? $params{dir} : $params{page}."/".IkiWiki::titlepage($params{name});
81 ($dir)=$dir=~/$IkiWiki::config{wiki_file_regexp}/;
83 $feed->{feedurl}=defined $params{feedurl} ? $params{feedurl} : "";
84 $feed->{updateinterval}=defined $params{updateinterval} ? $params{updateinterval} * 60 : 15 * 60;
85 $feed->{expireage}=defined $params{expireage} ? $params{expireage} : 0;
86 $feed->{expirecount}=defined $params{expirecount} ? $params{expirecount} : 0;
87 delete $feed->{remove};
88 $feed->{lastupdate}=0 unless defined $feed->{lastupdate};
89 $feed->{numposts}=0 unless defined $feed->{numposts};
90 $feed->{newposts}=0 unless defined $feed->{newposts};
91 $feed->{message}="new feed" unless defined $feed->{message};
97 push @{$feed->{tags}}, $value;
101 return "<a href=\"".$feed->{url}."\">".$feed->{name}."</a>: ".
102 "<i>".$feed->{message}."</i> (".$feed->{numposts}.
103 " stored posts; ".$feed->{newposts}." new)";
106 sub delete (@) { #{{{
109 # Remove feed data for removed pages.
110 foreach my $file (@files) {
111 my $page=IkiWiki::pagename($file);
116 sub loadstate () { #{{{
117 if (-e "$IkiWiki::config{wikistatedir}/aggregate") {
118 open (IN, "$IkiWiki::config{wikistatedir}/aggregate" ||
119 die "$IkiWiki::config{wikistatedir}/aggregate: $!");
121 $_=IkiWiki::possibly_foolish_untaint($_);
124 foreach my $i (split(/ /, $_)) {
125 my ($field, $val)=split(/=/, $i, 2);
126 if ($field eq "name" || $field eq "feed" ||
127 $field eq "guid" || $field eq "message") {
128 $data->{$field}=decode_entities($val, " \t\n");
130 elsif ($field eq "tag") {
131 push @{$data->{tags}}, $val;
134 $data->{$field}=$val;
138 if (exists $data->{name}) {
139 $feeds{$data->{name}}=$data;
141 elsif (exists $data->{guid}) {
142 $guids{$data->{guid}}=$data;
150 sub savestate () { #{{{
151 eval q{use HTML::Entities};
153 open (OUT, ">$IkiWiki::config{wikistatedir}/aggregate" ||
154 die "$IkiWiki::config{wikistatedir}/aggregate: $!");
155 foreach my $data (values %feeds, values %guids) {
156 if ($data->{remove}) {
158 foreach my $guid (values %guids) {
159 if ($guid->{feed} eq $data->{name}) {
165 unlink pagefile($data->{page});
171 foreach my $field (keys %$data) {
172 if ($field eq "name" || $field eq "feed" ||
173 $field eq "guid" || $field eq "message") {
174 push @line, "$field=".encode_entities($data->{$field}, " \t\n");
176 elsif ($field eq "tags") {
177 push @line, "tag=$_" foreach @{$data->{tags}};
180 push @line, "$field=".$data->{$field};
183 print OUT join(" ", @line)."\n";
188 sub aggregate () { #{{{
189 eval q{use XML::Feed};
191 eval q{use HTML::Entities};
194 foreach my $feed (values %feeds) {
195 next unless $IkiWiki::config{rebuild} ||
196 time - $feed->{lastupdate} >= $feed->{updateinterval};
197 $feed->{lastupdate}=time;
199 $IkiWiki::forcerebuild{$feed->{sourcepage}}=1;
201 IkiWiki::debug("checking feed ".$feed->{name}." ...");
203 if (! length $feed->{feedurl}) {
204 my @urls=XML::Feed->find_feeds($feed->{url});
206 $feed->{message}="could not find feed at ".$feed->{feedurl};
207 IkiWiki::debug($feed->{message});
210 $feed->{feedurl}=pop @urls;
212 my $f=eval{XML::Feed->parse(URI->new($feed->{feedurl}))};
214 $feed->{message}="feed crashed XML::Feed! $@";
215 IkiWiki::debug($feed->{message});
219 $feed->{message}=XML::Feed->errstr;
220 IkiWiki::debug($feed->{message});
224 foreach my $entry ($f->entries) {
227 title => defined $entry->title ? decode_entities($entry->title) : "untitled",
228 link => $entry->link,
229 content => $entry->content->body,
230 guid => defined $entry->id ? $entry->id : time."_".$feed->name,
231 ctime => $entry->issued ? ($entry->issued->epoch || time) : time,
235 $feed->{message}="processed ok";
241 sub add_page (@) { #{{{
244 my $feed=$params{feed};
247 if (exists $guids{$params{guid}}) {
248 # updating an existing post
249 $guid=$guids{$params{guid}};
253 $guid->{guid}=$params{guid};
254 $guids{$params{guid}}=$guid;
255 $mtime=$params{ctime};
259 # assign it an unused page
260 my $page=IkiWiki::titlepage($params{title});
261 # escape slashes and periods in title so it doesn't specify
262 # directory name or trigger ".." disallowing code.
263 $page=~s!([/.])!"__".ord($1)."__"!eg;
264 $page=$feed->{dir}."/".$page;
265 ($page)=$page=~/$IkiWiki::config{wiki_file_regexp}/;
266 if (! defined $page || ! length $page) {
267 $page=$feed->{dir}."/item";
270 while (exists $IkiWiki::pagecase{lc $page.$c} ||
271 -e pagefile($page.$c)) {
275 IkiWiki::debug("creating new page $page");
277 $guid->{feed}=$feed->{name};
279 # To write or not to write? Need to avoid writing unchanged pages
280 # to avoid unneccessary rebuilding. The mtime from rss cannot be
281 # trusted; let's use a digest.
282 eval q{use Digest::MD5 'md5_hex'};
284 my $digest=md5_hex(Encode::encode_utf8($params{content}));
285 return unless ! exists $guid->{md5} || $guid->{md5} ne $digest || $IkiWiki::config{rebuild};
286 $guid->{md5}=$digest;
289 my $template=IkiWiki::template("aggregatepost.tmpl", blind_cache => 1);
290 $template->param(title => $params{title})
291 if defined $params{title} && length($params{title});
292 $template->param(content => htmlescape(htmlabs($params{content}, $feed->{feedurl})));
293 $template->param(name => $feed->{name});
294 $template->param(url => $feed->{url});
295 $template->param(permalink => urlabs($params{link}, $feed->{feedurl}))
296 if defined $params{link};
297 if (ref $feed->{tags}) {
298 $template->param(tags => [map { tag => $_ }, @{$feed->{tags}}]);
300 IkiWiki::writefile(IkiWiki::htmlpage($guid->{page}), $IkiWiki::config{srcdir},
303 # Set the mtime, this lets the build process get the right creation
304 # time on record for the new page.
305 utime $mtime, $mtime, pagefile($guid->{page}) if defined $mtime;
308 sub htmlescape ($) { #{{{
309 # escape accidental wikilinks and preprocessor stuff
311 $html=~s/(?<!\\)\[\[/\\\[\[/g;
315 sub urlabs ($$) { #{{{
319 URI->new_abs($url, $urlbase)->as_string;
322 sub htmlabs ($$) { #{{{
323 # Convert links in html from relative to absolute.
324 # Note that this is a heuristic, which is not specified by the rss
325 # spec and may not be right for all feeds. Also, see Debian
331 my $p = HTML::Parser->new(api_version => 3);
332 $p->handler(default => sub { $ret.=join("", @_) }, "text");
333 $p->handler(start => sub {
334 my ($tagname, $pos, $text) = @_;
335 if (ref $HTML::Tagset::linkElements{$tagname}) {
337 # use attribute sets from right to left
338 # to avoid invalidating the offsets
339 # when replacing the values
340 my($k_offset, $k_len, $v_offset, $v_len) =
342 my $attrname = lc(substr($text, $k_offset, $k_len));
343 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
344 next unless $v_offset; # 0 v_offset means no value
345 my $v = substr($text, $v_offset, $v_len);
346 $v =~ s/^([\'\"])(.*)\1$/$2/;
347 my $new_v=urlabs($v, $urlbase);
348 $new_v =~ s/\"/"/g; # since we quote with ""
349 substr($text, $v_offset, $v_len) = qq("$new_v");
353 }, "tagname, tokenpos, text");
360 sub remove_feeds () { #{{{
364 foreach my $id (keys %feeds) {
365 if ($feeds{$id}->{sourcepage} eq $page) {
366 $feeds{$id}->{remove}=1;
372 sub pagefile ($) { #{{{
375 return "$IkiWiki::config{srcdir}/".IkiWiki::htmlpage($page);