summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/recentchanges.pm
blob: 2525785e7dea0d1d219c29ede9ed99f2550012b1 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::recentchanges;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. sub import { #{{{
  7. hook(type => "refresh", id => "recentchanges",
  8. call => \&refresh);
  9. hook(type => "preprocess", id => "recentchanges",
  10. call => \&preprocess);
  11. hook(type => "htmlize", id => "_change",
  12. call => \&htmlize);
  13. } #}}}
  14. sub refresh ($) { #{{{
  15. my @changes=IkiWiki::rcs_recentchanges(100);
  16. updatechanges("*", "recentchanges", \@changes);
  17. } #}}}
  18. sub preprocess (@) { #{{{
  19. my %params=@_;
  20. # TODO
  21. return "";
  22. } #}}}
  23. # Pages with extension _change have plain html markup, pass through.
  24. sub htmlize (@) { #{{{
  25. my %params=@_;
  26. return $params{content};
  27. } #}}}
  28. sub store ($$) { #{{{
  29. my $change=shift;
  30. my $subdir=shift;
  31. my $page="$subdir/change_".IkiWiki::titlepage($change->{rev});
  32. # Optimisation to avoid re-writing pages. Assumes commits never
  33. # change (or that any changes are not important).
  34. return if exists $pagesources{$page} && ! $config{rebuild};
  35. # Limit pages to first 10, and add links to the changed pages.
  36. my $is_excess = exists $change->{pages}[10];
  37. delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
  38. $change->{pages} = [
  39. map {
  40. if (length $config{url}) {
  41. $_->{link} = "<a href=\"$config{url}/".
  42. urlto($_->{page},"")."\">".
  43. IkiWiki::pagetitle($_->{page})."</a>";
  44. }
  45. else {
  46. $_->{link} = IkiWiki::pagetitle($_->{page});
  47. }
  48. $_;
  49. } @{$change->{pages}}
  50. ];
  51. push @{$change->{pages}}, { link => '...' } if $is_excess;
  52. # See if the committer is an openid.
  53. my $oiduser=IkiWiki::openiduser($change->{user});
  54. if (defined $oiduser) {
  55. $change->{authorurl}=$change->{user};
  56. $change->{user}=$oiduser;
  57. }
  58. elsif (length $config{url}) {
  59. $change->{authorurl}="$config{url}/".
  60. (length $config{userdir} ? "$config{userdir}/" : "").
  61. $change->{user};
  62. }
  63. # escape wikilinks and preprocessor stuff in commit messages
  64. if (ref $change->{message}) {
  65. foreach my $field (@{$change->{message}}) {
  66. if (exists $field->{line}) {
  67. $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
  68. }
  69. }
  70. }
  71. # Fill out a template with the change info.
  72. my $template=template("change.tmpl", blind_cache => 1);
  73. $template->param(
  74. %$change,
  75. commitdate => displaytime($change->{when}, "%X %x"),
  76. wikiname => $config{wikiname},
  77. );
  78. $template->param(baseurl => "$config{url}/") if length $config{url};
  79. IkiWiki::run_hooks(pagetemplate => sub {
  80. shift->(page => $page, destpage => $page, template => $template);
  81. });
  82. my $file=$page."._change";
  83. writefile($file, $config{srcdir}, $template->output);
  84. utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
  85. } #}}}
  86. sub updatechanges ($$) { #{{{
  87. my $pagespec=shift;
  88. my $subdir=shift;
  89. my @changes=@{shift()};
  90. foreach my $change (@changes) {
  91. store($change, $subdir);
  92. }
  93. # TODO: delete old
  94. } #}}}
  95. 1