use new refresh hook
[ikiwiki] / 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 => "refresh", id => "recentchanges",
10                 call => \&refresh);
11         hook(type => "preprocess", id => "recentchanges",
12                 call => \&preprocess);
13         hook(type => "htmlize", id => "_change",
14                 call => \&htmlize);
15 } #}}}
16
17 sub refresh ($) { #{{{
18         my @changes=IkiWiki::rcs_recentchanges(100);
19         updatechanges("*", "recentchanges", \@changes);
20 } #}}}
21
22 sub preprocess (@) { #{{{
23         my %params=@_;
24
25         # TODO
26
27         return "";
28 } #}}}
29
30 # Pages with extension _change have plain html markup, pass through.
31 sub htmlize (@) { #{{{
32         my %params=@_;
33         return $params{content};
34 } #}}}
35
36 sub store ($$) { #{{{
37         my $change=shift;
38         my $subdir=shift;
39         
40         my $page="$subdir/change_".IkiWiki::titlepage($change->{rev});
41
42         # Optimisation to avoid re-writing pages. Assumes commits never
43         # change (or that any changes are not important).
44         return if exists $pagesources{$page} && ! $config{rebuild};
45
46         # Limit pages to first 10, and add links to the changed pages.
47         my $is_excess = exists $change->{pages}[10];
48         delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
49         $change->{pages} = [
50                 map {
51                         if (length $config{url}) {
52                                 $_->{link} = "<a href=\"$config{url}/".
53                                         urlto($_->{page},"")."\">".
54                                         IkiWiki::pagetitle($_->{page})."</a>";
55                         }
56                         else {
57                                 $_->{link} = IkiWiki::pagetitle($_->{page});
58                         }
59                         $_;
60                 } @{$change->{pages}}
61         ];
62         push @{$change->{pages}}, { link => '...' } if $is_excess;
63
64         # See if the committer is an openid.
65         my $oiduser=IkiWiki::openiduser($change->{user});
66         if (defined $oiduser) {
67                 $change->{authorurl}=$change->{user};
68                 $change->{user}=$oiduser;
69         }
70         elsif (length $config{url}) {
71                 $change->{authorurl}="$config{url}/".
72                         (length $config{userdir} ? "$config{userdir}/" : "").
73                         $change->{user};
74         }
75
76         # escape  wikilinks and preprocessor stuff in commit messages
77         if (ref $change->{message}) {
78                 foreach my $field (@{$change->{message}}) {
79                         if (exists $field->{line}) {
80                                 $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
81                         }
82                 }
83         }
84
85         # Fill out a template with the change info.
86         my $template=template("change.tmpl", blind_cache => 1);
87         $template->param(
88                 %$change,
89                 commitdate => displaytime($change->{when}, "%X %x"),
90                 wikiname => $config{wikiname},
91         );
92         $template->param(baseurl => "$config{url}/") if length $config{url};
93         IkiWiki::run_hooks(pagetemplate => sub {
94                 shift->(page => $page, destpage => $page, template => $template);
95         });
96
97         my $file=$page."._change";
98         writefile($file, $config{srcdir}, $template->output);
99         utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
100 } #}}}
101
102 sub updatechanges ($$) { #{{{
103         my $pagespec=shift;
104         my $subdir=shift;
105         my @changes=@{shift()};
106
107         foreach my $change (@changes) {
108                 store($change, $subdir);
109         }
110         
111         # TODO: delete old
112 } #}}}
113
114 1