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