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