summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/recentchanges.pm
blob: f5982604bd7c091a8604700663062b36b3d5973c (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. my $oiduser=IkiWiki::openiduser($change->{user});
  58. if (defined $oiduser) {
  59. $change->{authorurl}=$change->{user};
  60. $change->{user}=$oiduser;
  61. }
  62. elsif (length $config{url}) {
  63. $change->{authorurl}="$config{url}/".
  64. (length $config{userdir} ? "$config{userdir}/" : "").
  65. $change->{user};
  66. }
  67. # escape wikilinks and preprocessor stuff in commit messages
  68. if (ref $change->{message}) {
  69. foreach my $field (@{$change->{message}}) {
  70. if (exists $field->{line}) {
  71. $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
  72. }
  73. }
  74. }
  75. # Fill out a template with the change info.
  76. my $template=template("change.tmpl", blind_cache => 1);
  77. $template->param(
  78. %$change,
  79. commitdate => displaytime($change->{when}, "%X %x"),
  80. wikiname => $config{wikiname},
  81. );
  82. $template->param(baseurl => "$config{url}/") if length $config{url};
  83. IkiWiki::run_hooks(pagetemplate => sub {
  84. shift->(page => $page, destpage => $page, template => $template);
  85. });
  86. my $file=$page."._change";
  87. writefile($file, $config{srcdir}, $template->output);
  88. utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
  89. return $page;
  90. } #}}}
  91. sub updatechanges ($$) { #{{{
  92. my $subdir=shift;
  93. my @changes=@{shift()};
  94. } #}}}
  95. 1