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