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