move recentchanges link enabling into a pagetemplate hook
[ikiwiki.git] / IkiWiki / Plugin / recentchanges.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::recentchanges;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 sub import { #{{{
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);
13 } #}}}
14
15 sub checkconfig () { #{{{
16         $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
17         $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
18 } #}}}
19
20 sub refresh ($) { #{{{
21         my %seen;
22
23         # add new changes
24         foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
25                 $seen{store($change, $config{recentchangespage})}=1;
26         }
27         
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});
32                 }
33         }
34 } #}}}
35
36 # Enable the recentchanges link on wiki pages.
37 sub pagetemplate (@) { #{{{
38         my %params=@_;
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);
45         }
46 } #}}}
47
48 # Pages with extension _change have plain html markup, pass through.
49 sub htmlize (@) { #{{{
50         my %params=@_;
51         return $params{content};
52 } #}}}
53
54 sub store ($$$) { #{{{
55         my $change=shift;
56
57         my $page="$config{recentchangespage}/change_".IkiWiki::titlepage($change->{rev});
58
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};
62
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;
66         $change->{pages} = [
67                 map {
68                         if (length $config{url}) {
69                                 $_->{link} = "<a href=\"$config{url}/".
70                                         urlto($_->{page},"")."\">".
71                                         IkiWiki::pagetitle($_->{page})."</a>";
72                         }
73                         else {
74                                 $_->{link} = IkiWiki::pagetitle($_->{page});
75                         }
76                         $_->{baseurl}="$config{url}/" if length $config{url};
77
78                         $_;
79                 } @{$change->{pages}}
80         ];
81         push @{$change->{pages}}, { link => '...' } if $is_excess;
82
83         # See if the committer is an openid.
84         $change->{author}=$change->{user};
85         my $oiduser=IkiWiki::openiduser($change->{user});
86         if (defined $oiduser) {
87                 $change->{authorurl}=$change->{user};
88                 $change->{user}=$oiduser;
89         }
90         elsif (length $config{url}) {
91                 $change->{authorurl}="$config{url}/".
92                         (length $config{userdir} ? "$config{userdir}/" : "").
93                         $change->{user};
94         }
95
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;
101                         }
102                 }
103         }
104
105         # Fill out a template with the change info.
106         my $template=template("change.tmpl", blind_cache => 1);
107         $template->param(
108                 %$change,
109                 commitdate => displaytime($change->{when}, "%X %x"),
110                 wikiname => $config{wikiname},
111         );
112         IkiWiki::run_hooks(pagetemplate => sub {
113                 shift->(page => $page, destpage => $page, template => $template);
114         });
115
116         my $file=$page."._change";
117         writefile($file, $config{srcdir}, $template->output);
118         utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
119
120         return $page;
121 } #}}}
122
123 sub updatechanges ($$) { #{{{
124         my $subdir=shift;
125         my @changes=@{shift()};
126         
127 } #}}}
128
129 1