2 package IkiWiki::Plugin::recentchanges;
9 hook(type => "checkconfig", id => "recentchanges", call => \&checkconfig);
10 hook(type => "refresh", id => "recentchanges", call => \&refresh);
11 hook(type => "htmlize", id => "_change", call => \&htmlize);
12 hook(type => "pagetemplate", id => "recentchanges", call => \&pagetemplate);
15 sub checkconfig () { #{{{
16 $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
17 $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
20 sub refresh ($) { #{{{
24 foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
25 $seen{store($change, $config{recentchangespage})}=1;
28 # delete old and excess changes
29 foreach my $page (keys %pagesources) {
30 if ($page=~/^\Q$config{recentchangespage}\E\/change_/ && ! $seen{$page}) {
31 unlink($config{srcdir}.'/'.$pagesources{$page});
36 # Enable the recentchanges link on wiki pages.
37 sub pagetemplate (@) { #{{{
39 my $template=$params{template};
40 my $page=$params{page};
41 if ($config{rcs} && $page ne $config{recentchangespage} &&
42 $template->query(name => "recentchangesurl")) {
43 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
44 $template->param(have_actions => 1);
48 # Pages with extension _change have plain html markup, pass through.
49 sub htmlize (@) { #{{{
51 return $params{content};
54 sub store ($$$) { #{{{
57 my $page="$config{recentchangespage}/change_".IkiWiki::titlepage($change->{rev});
59 # Optimisation to avoid re-writing pages. Assumes commits never
60 # change (or that any changes are not important).
61 return $page if exists $pagesources{$page} && ! $config{rebuild};
63 # Limit pages to first 10, and add links to the changed pages.
64 my $is_excess = exists $change->{pages}[10];
65 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
68 if (length $config{url}) {
69 $_->{link} = "<a href=\"$config{url}/".
70 urlto($_->{page},"")."\">".
71 IkiWiki::pagetitle($_->{page})."</a>";
74 $_->{link} = IkiWiki::pagetitle($_->{page});
76 $_->{baseurl}="$config{url}/" if length $config{url};
81 push @{$change->{pages}}, { link => '...' } if $is_excess;
83 # See if the committer is an openid.
84 $change->{author}=$change->{user};
85 my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
86 if (defined $oiduser) {
87 $change->{authorurl}=$change->{user};
88 $change->{user}=$oiduser;
90 elsif (length $config{url}) {
91 $change->{authorurl}="$config{url}/".
92 (length $config{userdir} ? "$config{userdir}/" : "").
96 # escape wikilinks and preprocessor stuff in commit messages
97 if (ref $change->{message}) {
98 foreach my $field (@{$change->{message}}) {
99 if (exists $field->{line}) {
100 $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
105 # Fill out a template with the change info.
106 my $template=template("change.tmpl", blind_cache => 1);
109 commitdate => displaytime($change->{when}, "%X %x"),
110 wikiname => $config{wikiname},
112 IkiWiki::run_hooks(pagetemplate => sub {
113 shift->(page => $page, destpage => $page, template => $template);
116 my $file=$page."._change";
117 writefile($file, $config{srcdir}, $template->output);
118 utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
123 sub updatechanges ($$) { #{{{
125 my @changes=@{shift()};