]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/aggregate.pm
md5 can't handle wide chars
[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} : $params{url};
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)";
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 "message") {
120                                         $data->{$field}=IkiWiki::pagetitle($val);
121                                 }
122                                 elsif ($field eq "tag") {
123                                         push @{$data->{tags}}, $val;
124                                 }
125                                 else {
126                                         $data->{$field}=$val;
127                                 }
128                         }
129                         
130                         if (exists $data->{name}) {
131                                 $feeds{$data->{name}}=$data;
132                         }
133                         elsif (exists $data->{guid}) {
134                                 $guids{$data->{guid}}=$data;
135                         }
136                 }
137
138                 close IN;
139         }
140 } #}}}
141
142 sub savestate () { #{{{
143         open (OUT, ">$IkiWiki::config{wikistatedir}/aggregate" ||
144                 die "$IkiWiki::config{wikistatedir}/aggregate: $!");
145         foreach my $data (values %feeds, values %guids) {
146                 if ($data->{remove}) {
147                         if ($data->{name}) {
148                                 foreach my $guid (values %guids) {
149                                         if ($guid->{feed} eq $data->{name}) {
150                                                 $guid->{remove}=1;
151                                         }
152                                 }
153                         }
154                         else {
155                                 unlink pagefile($data->{page});
156                         }
157                         next;
158                 }
159
160                 my @line;
161                 foreach my $field (keys %$data) {
162                         if ($field eq "name" || $field eq "message") {
163                                 push @line, "$field=".IkiWiki::titlepage($data->{$field});
164                         }
165                         elsif ($field eq "tags") {
166                                 push @line, "tag=$_" foreach @{$data->{tags}};
167                         }
168                         else {
169                                 push @line, "$field=".$data->{$field};
170                         }
171                 }
172                 print OUT join(" ", @line)."\n";
173         }
174         close OUT;
175 } #}}}
176
177 sub aggregate () { #{{{
178         eval q{use XML::Feed};
179         die $@ if $@;
180         eval q{use HTML::Entities};
181         die $@ if $@;
182
183 FEED:   foreach my $feed (values %feeds) {
184                 next unless time - $feed->{lastupdate} >= $feed->{updateinterval};
185                 $feed->{lastupdate}=time;
186                 $feed->{newposts}=0;
187                 $IkiWiki::forcerebuild{$feed->{sourcepage}}=1;
188
189                 IkiWiki::debug("checking feed ".$feed->{name}." ...");
190
191                 my @urls=XML::Feed->find_feeds($feed->{feedurl});
192                 if (! @urls) {
193                         $feed->{message}="could not find feed at ".$feed->{feedurl};
194                         IkiWiki::debug($feed->{message});
195                 }
196                 foreach my $url (@urls) {
197                         my $f=XML::Feed->parse(URI->new($url));
198                         if (! $f) {
199                                 $feed->{message}=XML::Feed->errstr;
200                                 IkiWiki::debug($feed->{message});
201                                 next FEED;
202                         }
203
204                         foreach my $entry ($f->entries) {
205                                 add_page(
206                                         feed => $feed,
207                                         title => decode_entities($entry->title),
208                                         link => $entry->link,
209                                         content => $entry->content->body,
210                                         guid => defined $entry->id ? $entry->id : time."_".$feed->name,
211                                         ctime => $entry->issued ? ($entry->issued->epoch || time) : time,
212                                 );
213                         }
214                 }
215                 $feed->{message}="processed ok";
216         }
217
218         # TODO: expiry
219 } #}}}
220
221 sub add_page (@) { #{{{
222         my %params=@_;
223
224         my $feed=$params{feed};
225         my $guid={};
226         my $mtime;
227         if (exists $guids{$params{guid}}) {
228                 # updating an existing post
229                 $guid=$guids{$params{guid}};
230         }
231         else {
232                 # new post
233                 $guid->{guid}=$params{guid};
234                 $guids{$params{guid}}=$guid;
235                 $mtime=$params{ctime};
236                 $feed->{numposts}++;
237                 $feed->{newposts}++;
238
239                 # assign it an unused page
240                 my $page=$feed->{dir}."/".IkiWiki::titlepage($params{title});
241                 ($page)=$page=~/$IkiWiki::config{wiki_file_regexp}/;
242                 if (! defined $page || ! length $page) {
243                         $page=$feed->{dir}."/item";
244                 }
245                 $page=~s/\.\.//g; # avoid ".." directory tricks
246                 my $c="";
247                 while (exists $IkiWiki::pagesources{$page.$c} ||
248                        -e pagefile($page.$c)) {
249                         $c++
250                 }
251                 $guid->{page}=$page;
252                 IkiWiki::debug("creating new page $page");
253         }
254         $guid->{feed}=$feed->{name};
255         
256         # To write or not to write? Need to avoid writing unchanged pages
257         # to avoid unneccessary rebuilding. The mtime from rss cannot be
258         # trusted; let's use a digest.
259         eval q{use Digest::MD5 'md5_hex'};
260         require Encode;
261         my $digest=md5_hex(Encode::encode_utf8($params{content}));
262         return unless ! exists $guid->{md5} || $guid->{md5} ne $digest;
263         $guid->{md5}=$digest;
264
265         # Create the page.
266         my $template=IkiWiki::template("aggregatepost.tmpl", blind_cache => 1);
267         my $content=$params{content};
268         $params{content}=~s/(?<!\\)\[\[/\\\[\[/g; # escape accidental wikilinks
269                                                   # and preprocessor stuff
270         $template->param(content => $params{content});
271         $template->param(url => $feed->{url});
272         $template->param(name => $feed->{name});
273         $template->param(link => $params{link}) if defined $params{link};
274         if (ref $feed->{tags}) {
275                 $template->param(tags => [map { tag => $_ }, @{$feed->{tags}}]);
276         }
277         IkiWiki::writefile($guid->{page}.".html", $IkiWiki::config{srcdir},
278                 $template->output);
279
280         # Set the mtime, this lets the build process get the right creation
281         # time on record for the new page.
282         utime $mtime, $mtime, pagefile($guid->{page}) if defined $mtime;
283 } #}}}
284
285 sub remove_feeds () { #{{{
286         my $page=shift;
287
288         my %removed;
289         foreach my $id (keys %feeds) {
290                 if ($feeds{$id}->{sourcepage} eq $page) {
291                         $feeds{$id}->{remove}=1;
292                         $removed{$id}=1;
293                 }
294         }
295 } #}}}
296
297 sub pagefile ($) { #{{{
298         my $page=shift;
299
300         return "$IkiWiki::config{srcdir}/$page.html";
301 } #}}}
302
303 1