]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/aggregate.pm
fix encoding of some stuff
[ikiwiki.git] / IkiWiki / Plugin / aggregate.pm
1 #!/usr/bin/perl
2 # Blog aggregation plugin.
3 package IkiWiki::Plugin::aggregate;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8
9 my %feeds;
10 my %guids;
11
12 sub import { #{{{
13         IkiWiki::hook(type => "getopt", id => "aggregate", 
14                 call => \&getopt);
15         IkiWiki::hook(type => "checkconfig", id => "aggregate",
16                 call => \&checkconfig);
17         IkiWiki::hook(type => "filter", id => "aggregate", 
18                 call => \&filter);
19         IkiWiki::hook(type => "preprocess", id => "aggregate",
20                 call => \&preprocess);
21         IkiWiki::hook(type => "delete", id => "aggregate",
22                 call => \&delete);
23         IkiWiki::hook(type => "savestate", id => "aggregate",
24                 call => \&savestate);
25 } # }}}
26
27 sub getopt () { #{{{
28         eval q{use Getopt::Long};
29         Getopt::Long::Configure('pass_through');
30         GetOptions("aggregate" => \$IkiWiki::config{aggregate});
31 } #}}}
32
33 sub checkconfig () { #{{{
34         loadstate();
35         if ($IkiWiki::config{aggregate}) {
36                 IkiWiki::loadindex();
37                 aggregate();
38                 savestate();
39         }
40 } #}}}
41
42 sub filter (@) { #{{{
43         my %params=@_;
44         my $page=$params{page};
45
46         # Mark all feeds originating on this page as removable;
47         # preprocess will unmark those that still exist.
48         remove_feeds($page);
49
50         return $params{content};
51 } # }}}
52
53 sub preprocess (@) { #{{{
54         my %params=@_;
55
56         foreach my $required (qw{name url}) {
57                 if (! exists $params{$required}) {
58                         return "[[aggregate plugin missing $required parameter]]";
59                 }
60         }
61
62         my $feed={};
63         my $name=$params{name};
64         if (exists $feeds{$name}) {
65                 $feed=$feeds{$name};
66         }
67         else {
68                 $feeds{$name}=$feed;
69         }
70         $feed->{name}=$name;
71         $feed->{sourcepage}=$params{page};
72         $feed->{url}=$params{url};
73         my $dir=exists $params{dir} ? $params{dir} : IkiWiki::titlepage($params{name});
74         $dir=~s/^\/+//;
75         ($dir)=$dir=~/$IkiWiki::config{wiki_file_regexp}/;
76         $feed->{dir}=$dir;
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};
86         while (@_) {
87                 my $key=shift;
88                 my $value=shift;
89                 if ($key eq 'tag') {
90                         push @{$feed->{tags}}, $value;
91                 }
92         }
93
94         return "<a href=\"".$feed->{url}."\">".$feed->{name}."</a>: ".
95                "<i>".$feed->{message}."</i> (".$feed->{numposts}.
96                " stored posts; ".$feed->{newposts}." new)<br />";
97 } # }}}
98
99 sub delete (@) { #{{{
100         my @files=@_;
101
102         # Remove feed data for removed pages.
103         foreach my $file (@files) {
104                 my $page=IkiWiki::pagename($file);
105                 remove_feeds($page);
106         }
107 } #}}}
108
109 sub loadstate () { #{{{
110         if (-e "$IkiWiki::config{wikistatedir}/aggregate") {
111                 open (IN, "$IkiWiki::config{wikistatedir}/aggregate" ||
112                         die "$IkiWiki::config{wikistatedir}/aggregate: $!");
113                 while (<IN>) {
114                         $_=IkiWiki::possibly_foolish_untaint($_);
115                         chomp;
116                         my $data={};
117                         foreach my $i (split(/ /, $_)) {
118                                 my ($field, $val)=split(/=/, $i, 2);
119                                 if ($field eq "name" || $field eq "feed" ||
120                                     $field eq "guid" || $field eq "message") {
121                                         $data->{$field}=IkiWiki::pagetitle($val);
122                                 }
123                                 elsif ($field eq "tag") {
124                                         push @{$data->{tags}}, $val;
125                                 }
126                                 else {
127                                         $data->{$field}=$val;
128                                 }
129                         }
130                         
131                         if (exists $data->{name}) {
132                                 $feeds{$data->{name}}=$data;
133                         }
134                         elsif (exists $data->{guid}) {
135                                 $guids{$data->{guid}}=$data;
136                         }
137                 }
138
139                 close IN;
140         }
141 } #}}}
142
143 sub savestate () { #{{{
144         open (OUT, ">$IkiWiki::config{wikistatedir}/aggregate" ||
145                 die "$IkiWiki::config{wikistatedir}/aggregate: $!");
146         foreach my $data (values %feeds, values %guids) {
147                 if ($data->{remove}) {
148                         if ($data->{name}) {
149                                 foreach my $guid (values %guids) {
150                                         if ($guid->{feed} eq $data->{name}) {
151                                                 $guid->{remove}=1;
152                                         }
153                                 }
154                         }
155                         else {
156                                 unlink pagefile($data->{page});
157                         }
158                         next;
159                 }
160
161                 my @line;
162                 foreach my $field (keys %$data) {
163                         if ($field eq "name" || $field eq "feed" ||
164                             $field eq "guid" || $field eq "message") {
165                                 push @line, "$field=".IkiWiki::titlepage($data->{$field});
166                         }
167                         elsif ($field eq "tags") {
168                                 push @line, "tag=$_" foreach @{$data->{tags}};
169                         }
170                         else {
171                                 push @line, "$field=".$data->{$field};
172                         }
173                 }
174                 print OUT join(" ", @line)."\n";
175         }
176         close OUT;
177 } #}}}
178
179 sub aggregate () { #{{{
180         eval q{use XML::Feed};
181         die $@ if $@;
182         eval q{use HTML::Entities};
183         die $@ if $@;
184
185         foreach my $feed (values %feeds) {
186                 next unless time - $feed->{lastupdate} >= $feed->{updateinterval};
187                 $feed->{lastupdate}=time;
188                 $feed->{newposts}=0;
189                 $IkiWiki::forcerebuild{$feed->{sourcepage}}=1;
190
191                 IkiWiki::debug("checking feed ".$feed->{name}." ...");
192
193                 if (! length $feed->{feedurl}) {
194                         my @urls=XML::Feed->find_feeds($feed->{url});
195                         if (! @urls) {
196                                 $feed->{message}="could not find feed at ".$feed->{feedurl};
197                                 IkiWiki::debug($feed->{message});
198                                 next;
199                         }
200                         $feed->{feedurl}=pop @urls;
201                 }
202                 my $f=eval{XML::Feed->parse(URI->new($feed->{feedurl}))};
203                 if ($@) {
204                         $feed->{message}="feed crashed XML::Feed! $@";
205                         IkiWiki::debug($feed->{message});
206                         next;
207                 }
208                 if (! $f) {
209                         $feed->{message}=XML::Feed->errstr;
210                         IkiWiki::debug($feed->{message});
211                         next;
212                 }
213
214                 foreach my $entry ($f->entries) {
215                         add_page(
216                                 feed => $feed,
217                                 title => defined $entry->title ? decode_entities($entry->title) : "untitled",
218                                 link => $entry->link,
219                                 content => $entry->content->body,
220                                 guid => defined $entry->id ? $entry->id : time."_".$feed->name,
221                                 ctime => $entry->issued ? ($entry->issued->epoch || time) : time,
222                         );
223                 }
224
225                 $feed->{message}="processed ok";
226         }
227
228         # TODO: expiry
229 } #}}}
230
231 sub add_page (@) { #{{{
232         my %params=@_;
233         
234         my $feed=$params{feed};
235         my $guid={};
236         my $mtime;
237         if (exists $guids{$params{guid}}) {
238                 # updating an existing post
239                 $guid=$guids{$params{guid}};
240         }
241         else {
242                 # new post
243                 $guid->{guid}=$params{guid};
244                 $guids{$params{guid}}=$guid;
245                 $mtime=$params{ctime};
246                 $feed->{numposts}++;
247                 $feed->{newposts}++;
248
249                 # assign it an unused page
250                 my $page=$feed->{dir}."/".IkiWiki::titlepage($params{title});
251                 ($page)=$page=~/$IkiWiki::config{wiki_file_regexp}/;
252                 if (! defined $page || ! length $page) {
253                         $page=$feed->{dir}."/item";
254                 }
255                 $page=~s/\.\.//g; # avoid ".." directory tricks
256                 my $c="";
257                 while (exists $IkiWiki::pagesources{$page.$c} ||
258                        -e pagefile($page.$c)) {
259                         $c++
260                 }
261                 $guid->{page}=$page;
262                 IkiWiki::debug("creating new page $page");
263         }
264         $guid->{feed}=$feed->{name};
265         
266         # To write or not to write? Need to avoid writing unchanged pages
267         # to avoid unneccessary rebuilding. The mtime from rss cannot be
268         # trusted; let's use a digest.
269         eval q{use Digest::MD5 'md5_hex'};
270         require Encode;
271         my $digest=md5_hex(Encode::encode_utf8($params{content}));
272         return unless ! exists $guid->{md5} || $guid->{md5} ne $digest;
273         $guid->{md5}=$digest;
274
275         # Create the page.
276         my $template=IkiWiki::template("aggregatepost.tmpl", blind_cache => 1);
277         my $content=$params{content};
278         $params{content}=~s/(?<!\\)\[\[/\\\[\[/g; # escape accidental wikilinks
279                                                   # and preprocessor stuff
280         $template->param(content => $params{content});
281         $template->param(url => $feed->{url});
282         $template->param(name => $feed->{name});
283         $template->param(link => $params{link}) if defined $params{link};
284         if (ref $feed->{tags}) {
285                 $template->param(tags => [map { tag => $_ }, @{$feed->{tags}}]);
286         }
287         IkiWiki::writefile($guid->{page}.".html", $IkiWiki::config{srcdir},
288                 $template->output);
289
290         # Set the mtime, this lets the build process get the right creation
291         # time on record for the new page.
292         utime $mtime, $mtime, pagefile($guid->{page}) if defined $mtime;
293 } #}}}
294
295 sub remove_feeds () { #{{{
296         my $page=shift;
297
298         my %removed;
299         foreach my $id (keys %feeds) {
300                 if ($feeds{$id}->{sourcepage} eq $page) {
301                         $feeds{$id}->{remove}=1;
302                         $removed{$id}=1;
303                 }
304         }
305 } #}}}
306
307 sub pagefile ($) { #{{{
308         my $page=shift;
309
310         return "$IkiWiki::config{srcdir}/$page.html";
311 } #}}}
312
313 1