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