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