summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/recentchanges.pm
blob: 781e297230460bcae757f68ec79a593a4fb876c1 (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. IkiWiki::redirect($cgi, urlto($link, undef, 1));
  95. }
  96. exit;
  97. }
  98. }
  99. sub store ($$$) { #{{{
  100. my $change=shift;
  101. my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
  102. # Optimisation to avoid re-writing pages. Assumes commits never
  103. # change (or that any changes are not important).
  104. return $page if exists $pagesources{$page} && ! $config{rebuild};
  105. # Limit pages to first 10, and add links to the changed pages.
  106. my $is_excess = exists $change->{pages}[10];
  107. delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
  108. $change->{pages} = [
  109. map {
  110. if (length $config{cgiurl}) {
  111. $_->{link} = "<a href=\"".
  112. IkiWiki::cgiurl(
  113. do => "recentchanges_link",
  114. page => $_->{page}
  115. ).
  116. "\">".
  117. pagetitle($_->{page}).
  118. "</a>"
  119. }
  120. else {
  121. $_->{link} = pagetitle($_->{page});
  122. }
  123. $_->{baseurl}="$config{url}/" if length $config{url};
  124. $_;
  125. } @{$change->{pages}}
  126. ];
  127. push @{$change->{pages}}, { link => '...' } if $is_excess;
  128. # See if the committer is an openid.
  129. $change->{author}=$change->{user};
  130. my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
  131. if (defined $oiduser) {
  132. $change->{authorurl}=$change->{user};
  133. $change->{user}=$oiduser;
  134. }
  135. elsif (length $config{cgiurl}) {
  136. $change->{authorurl} = IkiWiki::cgiurl(
  137. do => "recentchanges_link",
  138. page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
  139. );
  140. }
  141. # escape wikilinks and preprocessor stuff in commit messages
  142. if (ref $change->{message}) {
  143. foreach my $field (@{$change->{message}}) {
  144. if (exists $field->{line}) {
  145. $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
  146. }
  147. }
  148. }
  149. # Fill out a template with the change info.
  150. my $template=template("change.tmpl", blind_cache => 1);
  151. $template->param(
  152. %$change,
  153. commitdate => displaytime($change->{when}, "%X %x"),
  154. commitdate_raw => scalar localtime($change->{when}),
  155. wikiname => $config{wikiname},
  156. );
  157. $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
  158. if exists $config{url};
  159. IkiWiki::run_hooks(pagetemplate => sub {
  160. shift->(page => $page, destpage => $page,
  161. template => $template, rev => $change->{rev});
  162. });
  163. my $file=$page."._change";
  164. writefile($file, $config{srcdir}, $template->output);
  165. utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
  166. return $page;
  167. } #}}}
  168. 1