summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/aggregate.pm
blob: 16db42a8fbd4f917d3cb4ac75313988295dbc558 (plain)
  1. #!/usr/bin/perl
  2. # Blog aggregation plugin.
  3. package IkiWiki::Plugin::aggregate;
  4. use warnings;
  5. use strict;
  6. use IkiWiki;
  7. use HTML::Entities;
  8. my %feeds;
  9. my %guids;
  10. sub import { #{{{
  11. IkiWiki::hook(type => "getopt", id => "aggregate",
  12. call => \&getopt);
  13. IkiWiki::hook(type => "checkconfig", id => "aggregate",
  14. call => \&checkconfig);
  15. IkiWiki::hook(type => "filter", id => "aggregate",
  16. call => \&filter);
  17. IkiWiki::hook(type => "preprocess", id => "aggregate",
  18. call => \&preprocess);
  19. IkiWiki::hook(type => "delete", id => "aggregate",
  20. call => \&delete);
  21. IkiWiki::hook(type => "savestate", id => "aggregate",
  22. call => \&savestate);
  23. } # }}}
  24. sub getopt () { #{{{
  25. eval q{use Getopt::Long};
  26. Getopt::Long::Configure('pass_through');
  27. GetOptions("aggregate" => \$IkiWiki::config{aggregate});
  28. } #}}}
  29. sub checkconfig () { #{{{
  30. loadstate();
  31. if ($IkiWiki::config{aggregate}) {
  32. IkiWiki::loadindex();
  33. aggregate();
  34. savestate();
  35. }
  36. } #}}}
  37. sub filter (@) { #{{{
  38. my %params=@_;
  39. my $page=$params{page};
  40. # Mark all feeds originating on this page as removable;
  41. # preprocess will unmark those that still exist.
  42. remove_feeds($page);
  43. return $params{content};
  44. } # }}}
  45. sub preprocess (@) { #{{{
  46. my %params=@_;
  47. foreach my $required (qw{name url}) {
  48. if (! exists $params{$required}) {
  49. return "[[aggregate plugin missing $required parameter]]";
  50. }
  51. }
  52. my $feed={};
  53. my $name=$params{name};
  54. if (exists $feeds{$name}) {
  55. $feed=$feeds{$name};
  56. }
  57. else {
  58. $feeds{$name}=$feed;
  59. }
  60. $feed->{name}=$name;
  61. $feed->{sourcepage}=$params{page};
  62. $feed->{url}=$params{url};
  63. my $dir=exists $params{dir} ? $params{dir} : $params{page}."/".IkiWiki::titlepage($params{name});
  64. $dir=~s/^\/+//;
  65. ($dir)=$dir=~/$IkiWiki::config{wiki_file_regexp}/;
  66. $feed->{dir}=$dir;
  67. $feed->{feedurl}=defined $params{feedurl} ? $params{feedurl} : "";
  68. $feed->{updateinterval}=defined $params{updateinterval} ? $params{updateinterval} * 60 : 15 * 60;
  69. $feed->{expireage}=defined $params{expireage} ? $params{expireage} : 0;
  70. $feed->{expirecount}=defined $params{expirecount} ? $params{expirecount} : 0;
  71. delete $feed->{remove};
  72. $feed->{lastupdate}=0 unless defined $feed->{lastupdate};
  73. $feed->{numposts}=0 unless defined $feed->{numposts};
  74. $feed->{newposts}=0 unless defined $feed->{newposts};
  75. $feed->{message}="new feed" unless defined $feed->{message};
  76. $feed->{tags}=[];
  77. while (@_) {
  78. my $key=shift;
  79. my $value=shift;
  80. if ($key eq 'tag') {
  81. push @{$feed->{tags}}, $value;
  82. }
  83. }
  84. return "<a href=\"".$feed->{url}."\">".$feed->{name}."</a>: ".
  85. "<i>".$feed->{message}."</i> (".$feed->{numposts}.
  86. " stored posts; ".$feed->{newposts}." new)<br />";
  87. } # }}}
  88. sub delete (@) { #{{{
  89. my @files=@_;
  90. # Remove feed data for removed pages.
  91. foreach my $file (@files) {
  92. my $page=IkiWiki::pagename($file);
  93. remove_feeds($page);
  94. }
  95. } #}}}
  96. sub loadstate () { #{{{
  97. if (-e "$IkiWiki::config{wikistatedir}/aggregate") {
  98. open (IN, "$IkiWiki::config{wikistatedir}/aggregate" ||
  99. die "$IkiWiki::config{wikistatedir}/aggregate: $!");
  100. while (<IN>) {
  101. $_=IkiWiki::possibly_foolish_untaint($_);
  102. chomp;
  103. my $data={};
  104. foreach my $i (split(/ /, $_)) {
  105. my ($field, $val)=split(/=/, $i, 2);
  106. if ($field eq "name" || $field eq "feed" ||
  107. $field eq "guid" || $field eq "message") {
  108. $data->{$field}=decode_entities($val, " \t\n");
  109. }
  110. elsif ($field eq "tag") {
  111. push @{$data->{tags}}, $val;
  112. }
  113. else {
  114. $data->{$field}=$val;
  115. }
  116. }
  117. if (exists $data->{name}) {
  118. $feeds{$data->{name}}=$data;
  119. }
  120. elsif (exists $data->{guid}) {
  121. $guids{$data->{guid}}=$data;
  122. }
  123. }
  124. close IN;
  125. }
  126. } #}}}
  127. sub savestate () { #{{{
  128. eval q{use HTML::Entities};
  129. die $@ if $@;
  130. open (OUT, ">$IkiWiki::config{wikistatedir}/aggregate" ||
  131. die "$IkiWiki::config{wikistatedir}/aggregate: $!");
  132. foreach my $data (values %feeds, values %guids) {
  133. if ($data->{remove}) {
  134. if ($data->{name}) {
  135. foreach my $guid (values %guids) {
  136. if ($guid->{feed} eq $data->{name}) {
  137. $guid->{remove}=1;
  138. }
  139. }
  140. }
  141. else {
  142. unlink pagefile($data->{page});
  143. }
  144. next;
  145. }
  146. my @line;
  147. foreach my $field (keys %$data) {
  148. if ($field eq "name" || $field eq "feed" ||
  149. $field eq "guid" || $field eq "message") {
  150. push @line, "$field=".encode_entities($data->{$field}, " \t\n");
  151. }
  152. elsif ($field eq "tags") {
  153. push @line, "tag=$_" foreach @{$data->{tags}};
  154. }
  155. else {
  156. push @line, "$field=".$data->{$field};
  157. }
  158. }
  159. print OUT join(" ", @line)."\n";
  160. }
  161. close OUT;
  162. } #}}}
  163. sub aggregate () { #{{{
  164. eval q{use XML::Feed};
  165. die $@ if $@;
  166. eval q{use HTML::Entities};
  167. die $@ if $@;
  168. foreach my $feed (values %feeds) {
  169. next unless $IkiWiki::config{rebuild} ||
  170. time - $feed->{lastupdate} >= $feed->{updateinterval};
  171. $feed->{lastupdate}=time;
  172. $feed->{newposts}=0;
  173. $IkiWiki::forcerebuild{$feed->{sourcepage}}=1;
  174. IkiWiki::debug("checking feed ".$feed->{name}." ...");
  175. if (! length $feed->{feedurl}) {
  176. my @urls=XML::Feed->find_feeds($feed->{url});
  177. if (! @urls) {
  178. $feed->{message}="could not find feed at ".$feed->{feedurl};
  179. IkiWiki::debug($feed->{message});
  180. next;
  181. }
  182. $feed->{feedurl}=pop @urls;
  183. }
  184. my $f=eval{XML::Feed->parse(URI->new($feed->{feedurl}))};
  185. if ($@) {
  186. $feed->{message}="feed crashed XML::Feed! $@";
  187. IkiWiki::debug($feed->{message});
  188. next;
  189. }
  190. if (! $f) {
  191. $feed->{message}=XML::Feed->errstr;
  192. IkiWiki::debug($feed->{message});
  193. next;
  194. }
  195. foreach my $entry ($f->entries) {
  196. add_page(
  197. feed => $feed,
  198. title => defined $entry->title ? decode_entities($entry->title) : "untitled",
  199. link => $entry->link,
  200. content => $entry->content->body,
  201. guid => defined $entry->id ? $entry->id : time."_".$feed->name,
  202. ctime => $entry->issued ? ($entry->issued->epoch || time) : time,
  203. );
  204. }
  205. $feed->{message}="processed ok";
  206. }
  207. # TODO: expiry
  208. } #}}}
  209. sub add_page (@) { #{{{
  210. my %params=@_;
  211. my $feed=$params{feed};
  212. my $guid={};
  213. my $mtime;
  214. if (exists $guids{$params{guid}}) {
  215. # updating an existing post
  216. $guid=$guids{$params{guid}};
  217. }
  218. else {
  219. # new post
  220. $guid->{guid}=$params{guid};
  221. $guids{$params{guid}}=$guid;
  222. $mtime=$params{ctime};
  223. $feed->{numposts}++;
  224. $feed->{newposts}++;
  225. # assign it an unused page
  226. my $page=IkiWiki::titlepage($params{title});
  227. # escape slashes and periods in title so it doesn't specify
  228. # directory name or trigger ".." disallowing code.
  229. $page=~s!([/.])!"__".ord($1)."__"!eg;
  230. $page=$feed->{dir}."/".$page;
  231. $page=lc($page);
  232. ($page)=$page=~/$IkiWiki::config{wiki_file_regexp}/;
  233. if (! defined $page || ! length $page) {
  234. $page=$feed->{dir}."/item";
  235. }
  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