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 () { #{{{
39 if ($IkiWiki::config{aggregate}) {
48 my $page=$params{page};
50 # Mark all feeds originating on this page as removable;
51 # preprocess will unmark those that still exist.
54 return $params{content};
57 sub preprocess (@) { #{{{
60 foreach my $required (qw{name url}) {
61 if (! exists $params{$required}) {
62 return "[[aggregate plugin missing $required parameter]]";
67 my $name=$params{name};
68 if (exists $feeds{$name}) {
75 $feed->{sourcepage}=$params{page};
76 $feed->{url}=$params{url};
77 my $dir=exists $params{dir} ? $params{dir} : $params{page}."/".IkiWiki::titlepage($params{name});
79 ($dir)=$dir=~/$IkiWiki::config{wiki_file_regexp}/;
81 $feed->{feedurl}=defined $params{feedurl} ? $params{feedurl} : "";
82 $feed->{updateinterval}=defined $params{updateinterval} ? $params{updateinterval} * 60 : 15 * 60;
83 $feed->{expireage}=defined $params{expireage} ? $params{expireage} : 0;
84 $feed->{expirecount}=defined $params{expirecount} ? $params{expirecount} : 0;
85 delete $feed->{remove};
86 $feed->{lastupdate}=0 unless defined $feed->{lastupdate};
87 $feed->{numposts}=0 unless defined $feed->{numposts};
88 $feed->{newposts}=0 unless defined $feed->{newposts};
89 $feed->{message}="new feed" unless defined $feed->{message};
95 push @{$feed->{tags}}, $value;
99 return "<a href=\"".$feed->{url}."\">".$feed->{name}."</a>: ".
100 "<i>".$feed->{message}."</i> (".$feed->{numposts}.
101 " stored posts; ".$feed->{newposts}." new)<br />";
104 sub delete (@) { #{{{
107 # Remove feed data for removed pages.
108 foreach my $file (@files) {
109 my $page=IkiWiki::pagename($file);
114 sub loadstate () { #{{{
115 if (-e "$IkiWiki::config{wikistatedir}/aggregate") {
116 open (IN, "$IkiWiki::config{wikistatedir}/aggregate" ||
117 die "$IkiWiki::config{wikistatedir}/aggregate: $!");
119 $_=IkiWiki::possibly_foolish_untaint($_);
122 foreach my $i (split(/ /, $_)) {
123 my ($field, $val)=split(/=/, $i, 2);
124 if ($field eq "name" || $field eq "feed" ||
125 $field eq "guid" || $field eq "message") {
126 $data->{$field}=decode_entities($val, " \t\n");
128 elsif ($field eq "tag") {
129 push @{$data->{tags}}, $val;
132 $data->{$field}=$val;
136 if (exists $data->{name}) {
137 $feeds{$data->{name}}=$data;
139 elsif (exists $data->{guid}) {
140 $guids{$data->{guid}}=$data;
148 sub savestate () { #{{{
149 eval q{use HTML::Entities};
151 open (OUT, ">$IkiWiki::config{wikistatedir}/aggregate" ||
152 die "$IkiWiki::config{wikistatedir}/aggregate: $!");
153 foreach my $data (values %feeds, values %guids) {
154 if ($data->{remove}) {
156 foreach my $guid (values %guids) {
157 if ($guid->{feed} eq $data->{name}) {
163 unlink pagefile($data->{page});
169 foreach my $field (keys %$data) {
170 if ($field eq "name" || $field eq "feed" ||
171 $field eq "guid" || $field eq "message") {
172 push @line, "$field=".encode_entities($data->{$field}, " \t\n");
174 elsif ($field eq "tags") {
175 push @line, "tag=$_" foreach @{$data->{tags}};
178 push @line, "$field=".$data->{$field};
181 print OUT join(" ", @line)."\n";
186 sub aggregate () { #{{{
187 eval q{use XML::Feed};
189 eval q{use HTML::Entities};
192 foreach my $feed (values %feeds) {
193 next unless $IkiWiki::config{rebuild} ||
194 time - $feed->{lastupdate} >= $feed->{updateinterval};
195 $feed->{lastupdate}=time;
197 $IkiWiki::forcerebuild{$feed->{sourcepage}}=1;
199 IkiWiki::debug("checking feed ".$feed->{name}." ...");
201 if (! length $feed->{feedurl}) {
202 my @urls=XML::Feed->find_feeds($feed->{url});
204 $feed->{message}="could not find feed at ".$feed->{feedurl};
205 IkiWiki::debug($feed->{message});
208 $feed->{feedurl}=pop @urls;
210 my $f=eval{XML::Feed->parse(URI->new($feed->{feedurl}))};
212 $feed->{message}="feed crashed XML::Feed! $@";
213 IkiWiki::debug($feed->{message});
217 $feed->{message}=XML::Feed->errstr;
218 IkiWiki::debug($feed->{message});
222 foreach my $entry ($f->entries) {
225 title => defined $entry->title ? decode_entities($entry->title) : "untitled",
226 link => $entry->link,
227 content => $entry->content->body,
228 guid => defined $entry->id ? $entry->id : time."_".$feed->name,
229 ctime => $entry->issued ? ($entry->issued->epoch || time) : time,
233 $feed->{message}="processed ok";
239 sub add_page (@) { #{{{
242 my $feed=$params{feed};
245 if (exists $guids{$params{guid}}) {
246 # updating an existing post
247 $guid=$guids{$params{guid}};
251 $guid->{guid}=$params{guid};
252 $guids{$params{guid}}=$guid;
253 $mtime=$params{ctime};
257 # assign it an unused page
258 my $page=IkiWiki::titlepage($params{title});
259 # escape slashes and periods in title so it doesn't specify
260 # directory name or trigger ".." disallowing code.
261 $page=~s!([/.])!"__".ord($1)."__"!eg;
262 $page=$feed->{dir}."/".$page;
264 ($page)=$page=~/$IkiWiki::config{wiki_file_regexp}/;
265 if (! defined $page || ! length $page) {
266 $page=$feed->{dir}."/item";
269 while (exists $IkiWiki::pagesources{$page.$c} ||
270 -e pagefile($page.$c)) {
274 IkiWiki::debug("creating new page $page");
276 $guid->{feed}=$feed->{name};
278 # To write or not to write? Need to avoid writing unchanged pages
279 # to avoid unneccessary rebuilding. The mtime from rss cannot be
280 # trusted; let's use a digest.
281 eval q{use Digest::MD5 'md5_hex'};
283 my $digest=md5_hex(Encode::encode_utf8($params{content}));
284 return unless ! exists $guid->{md5} || $guid->{md5} ne $digest || $IkiWiki::config{rebuild};
285 $guid->{md5}=$digest;
288 my $template=IkiWiki::template("aggregatepost.tmpl", blind_cache => 1);
289 $template->param(title => $params{title})
290 if defined $params{title} && length($params{title});
291 $template->param(content => htmlescape(htmlabs($params{content}, $feed->{feedurl})));
292 $template->param(url => $feed->{url});
293 $template->param(name => $feed->{name});
294 $template->param(link => urlabs($params{link}, $feed->{feedurl}))
295 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 htmlescape ($) { #{{{
308 # escape accidental wikilinks and preprocessor stuff
310 $html=~s/(?<!\\)\[\[/\\\[\[/g;
314 sub urlabs ($$) { #{{{
318 URI->new_abs($url, $urlbase)->as_string;
321 sub htmlabs ($$) { #{{{
322 # Convert links in html from relative to absolute.
323 # Note that this is a heuristic, which is not specified by the rss
324 # spec and may not be right for all feeds. Also, see Debian
325 # bug #XXXX TODO: get bug.
330 my $p = HTML::Parser->new(api_version => 3);
331 $p->handler(default => sub { $ret.=join("", @_) }, "text");
332 $p->handler(start => sub {
333 my ($tagname, $pos, $text) = @_;
334 if (ref $HTML::Tagset::linkElements{$tagname}) {
336 # use attribute sets from right to left
337 # to avoid invalidating the offsets
338 # when replacing the values
339 my($k_offset, $k_len, $v_offset, $v_len) =
341 my $attrname = lc(substr($text, $k_offset, $k_len));
342 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
343 next unless $v_offset; # 0 v_offset means no value
344 my $v = substr($text, $v_offset, $v_len);
345 $v =~ s/^([\'\"])(.*)\1$/$2/;
346 my $new_v=urlabs($v, $urlbase);
347 $new_v =~ s/\"/"/g; # since we quote with ""
348 substr($text, $v_offset, $v_len) = qq("$new_v");
352 }, "tagname, tokenpos, text");
359 sub remove_feeds () { #{{{
363 foreach my $id (keys %feeds) {
364 if ($feeds{$id}->{sourcepage} eq $page) {
365 $feeds{$id}->{remove}=1;
371 sub pagefile ($) { #{{{
374 return "$IkiWiki::config{srcdir}/$page.html";