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