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