summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/recentchanges.pm
blob: 8383fb72a12e19c8d92627956efc154855ff57e5 (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 => "pagetemplate", id => "recentchanges", call => \&pagetemplate);
  10. hook(type => "htmlize", id => "_change", call => \&htmlize);
  11. hook(type => "cgi", id => "recentchanges", call => \&cgi);
  12. } #}}}
  13. sub checkconfig () { #{{{
  14. $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
  15. $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
  16. } #}}}
  17. sub refresh ($) { #{{{
  18. my %seen;
  19. # add new changes
  20. foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
  21. $seen{store($change, $config{recentchangespage})}=1;
  22. }
  23. # delete old and excess changes
  24. foreach my $page (keys %pagesources) {
  25. if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
  26. unlink($config{srcdir}.'/'.$pagesources{$page});
  27. }
  28. }
  29. } #}}}
  30. # Enable the recentchanges link on wiki pages.
  31. sub pagetemplate (@) { #{{{
  32. my %params=@_;
  33. my $template=$params{template};
  34. my $page=$params{page};
  35. if (defined $config{recentchangespage} && $config{rcs} &&
  36. $page ne $config{recentchangespage} &&
  37. $template->query(name => "recentchangesurl")) {
  38. $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
  39. $template->param(have_actions => 1);
  40. }
  41. } #}}}
  42. # Pages with extension _change have plain html markup, pass through.
  43. sub htmlize (@) { #{{{
  44. my %params=@_;
  45. return $params{content};
  46. } #}}}
  47. sub cgi ($) { #{{{
  48. my $cgi=shift;
  49. if (defined $cgi->param('do') && $cgi->param('do') eq "recentchanges_link") {
  50. # This is a link from a change page to some
  51. # other page. Since the change pages are only generated
  52. # once, statically, links on them won't be updated if the
  53. # page they link to is deleted, or newly created, or
  54. # changes for whatever reason. So this CGI handles that
  55. # dynamic linking stuff.
  56. my $page=$cgi->param("page");
  57. if (!defined $page) {
  58. error("missing page parameter");
  59. }
  60. IkiWiki::loadindex();
  61. my $link=bestlink("", $page);
  62. if (! length $link) {
  63. print "Content-type: text/html\n\n";
  64. print IkiWiki::misctemplate(gettext(gettext("missing page")),
  65. "<p>".
  66. sprintf(gettext("The page %s does not exist."),
  67. htmllink("", "", $page)).
  68. "</p>");
  69. }
  70. else {
  71. IkiWiki::redirect($cgi, $config{url}.IkiWiki::beautify_urlpath("/".htmlpage($link)));
  72. }
  73. exit;
  74. }
  75. }
  76. sub store ($$$) { #{{{
  77. my $change=shift;
  78. my $page="$config{recentchangespage}/change_".IkiWiki::titlepage($change->{rev});
  79. # Optimisation to avoid re-writing pages. Assumes commits never
  80. # change (or that any changes are not important).
  81. return $page if exists $pagesources{$page} && ! $config{rebuild};
  82. # Limit pages to first 10, and add links to the changed pages.
  83. my $is_excess = exists $change->{pages}[10];
  84. delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
  85. $change->{pages} = [
  86. map {
  87. if (length $config{cgiurl}) {
  88. $_->{link} = "<a href=\"".
  89. IkiWiki::cgiurl(
  90. do => "recentchanges_link",
  91. page => $_->{page}
  92. ).
  93. "\">".
  94. IkiWiki::pagetitle($_->{page}).
  95. "</a>"
  96. }
  97. else {
  98. $_->{link} = IkiWiki::pagetitle($_->{page});
  99. }
  100. $_->{baseurl}="$config{url}/" if length $config{url};
  101. $_;
  102. } @{$change->{pages}}
  103. ];
  104. push @{$change->{pages}}, { link => '...' } if $is_excess;
  105. # See if the committer is an openid.
  106. $change->{author}=$change->{user};
  107. my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
  108. if (defined $oiduser) {
  109. $change->{authorurl}=$change->{user};
  110. $change->{user}=$oiduser;
  111. }
  112. elsif (length $config{cgiurl}) {
  113. $change->{authorurl} = IkiWiki::cgiurl(
  114. do => "recentchanges_link",
  115. page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
  116. );
  117. }
  118. # escape wikilinks and preprocessor stuff in commit messages
  119. if (ref $change->{message}) {
  120. foreach my $field (@{$change->{message}}) {
  121. if (exists $field->{line}) {
  122. $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
  123. }
  124. }
  125. }
  126. # Fill out a template with the change info.
  127. my $template=template("change.tmpl", blind_cache => 1);
  128. $template->param(
  129. %$change,
  130. commitdate => displaytime($change->{when}, "%X %x"),
  131. wikiname => $config{wikiname},
  132. );
  133. IkiWiki::run_hooks(pagetemplate => sub {
  134. shift->(page => $page, destpage => $page,
  135. template => $template, rev => $change->{rev});
  136. });
  137. my $file=$page."._change";
  138. writefile($file, $config{srcdir}, $template->output);
  139. utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
  140. return $page;
  141. } #}}}
  142. 1