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