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