Group related plugins into sections in the setup file, and drop unused rcs plugins...
[ikiwiki] / IkiWiki / Plugin / recentchanges.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::recentchanges;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use Encode;
8 use HTML::Entities;
9
10 sub import {
11         hook(type => "getsetup", id => "recentchanges", call => \&getsetup);
12         hook(type => "checkconfig", id => "recentchanges", call => \&checkconfig);
13         hook(type => "refresh", id => "recentchanges", call => \&refresh);
14         hook(type => "pagetemplate", id => "recentchanges", call => \&pagetemplate);
15         hook(type => "htmlize", id => "_change", call => \&htmlize);
16         # Load goto to fix up links from recentchanges
17         IkiWiki::loadplugin("goto");
18 }
19
20 sub getsetup () {
21         return
22                 plugin => {
23                         safe => 1,
24                         rebuild => 1,
25                         section => "core",
26                 },
27                 recentchangespage => {
28                         type => "string",
29                         example => "recentchanges",
30                         description => "name of the recentchanges page",
31                         safe => 1,
32                         rebuild => 1,
33                 },
34                 recentchangesnum => {
35                         type => "integer",
36                         example => 100,
37                         description => "number of changes to track",
38                         safe => 1,
39                         rebuild => 0,
40                 },
41 }
42
43 sub checkconfig () {
44         $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
45         $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
46 }
47
48 sub refresh ($) {
49         my %seen;
50
51         # add new changes
52         foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
53                 $seen{store($change, $config{recentchangespage})}=1;
54         }
55         
56         # delete old and excess changes
57         foreach my $page (keys %pagesources) {
58                 if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
59                         unlink($config{srcdir}.'/'.$pagesources{$page});
60                 }
61         }
62 }
63
64 # Enable the recentchanges link on wiki pages.
65 sub pagetemplate (@) {
66         my %params=@_;
67         my $template=$params{template};
68         my $page=$params{page};
69
70         if (defined $config{recentchangespage} && $config{rcs} &&
71             $page ne $config{recentchangespage} &&
72             $template->query(name => "recentchangesurl")) {
73                 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
74                 $template->param(have_actions => 1);
75         }
76 }
77
78 # Pages with extension _change have plain html markup, pass through.
79 sub htmlize (@) {
80         my %params=@_;
81         return $params{content};
82 }
83
84 sub store ($$$) {
85         my $change=shift;
86
87         my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
88
89         # Optimisation to avoid re-writing pages. Assumes commits never
90         # change (or that any changes are not important).
91         return $page if exists $pagesources{$page} && ! $config{rebuild};
92
93         # Limit pages to first 10, and add links to the changed pages.
94         my $is_excess = exists $change->{pages}[10];
95         delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
96         $change->{pages} = [
97                 map {
98                         if (length $config{cgiurl}) {
99                                 $_->{link} = "<a href=\"".
100                                         IkiWiki::cgiurl(
101                                                 do => "goto",
102                                                 page => $_->{page}
103                                         ).
104                                         "\" rel=\"nofollow\">".
105                                         pagetitle($_->{page}).
106                                         "</a>"
107                         }
108                         else {
109                                 $_->{link} = pagetitle($_->{page});
110                         }
111                         $_->{baseurl}="$config{url}/" if length $config{url};
112
113                         $_;
114                 } @{$change->{pages}}
115         ];
116         push @{$change->{pages}}, { link => '...' } if $is_excess;
117
118         # See if the committer is an openid.
119         $change->{author}=$change->{user};
120         my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
121         if (defined $oiduser) {
122                 $change->{authorurl}=$change->{user};
123                 $change->{user}=$oiduser;
124         }
125         elsif (length $config{cgiurl}) {
126                 $change->{authorurl} = IkiWiki::cgiurl(
127                         do => "goto",
128                         page => IkiWiki::userpage($change->{author}),
129                 );
130         }
131
132         if (ref $change->{message}) {
133                 foreach my $field (@{$change->{message}}) {
134                         if (exists $field->{line}) {
135                                 # escape html
136                                 $field->{line} = encode_entities($field->{line});
137                                 # escape links and preprocessor stuff
138                                 $field->{line} = encode_entities($field->{line}, '\[\]');
139                         }
140                 }
141         }
142
143         # Fill out a template with the change info.
144         my $template=template("change.tmpl", blind_cache => 1);
145         $template->param(
146                 %$change,
147                 commitdate => displaytime($change->{when}, "%X %x"),
148                 wikiname => $config{wikiname},
149         );
150         
151         $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
152                 if exists $config{url};
153         
154         IkiWiki::run_hooks(pagetemplate => sub {
155                 shift->(page => $page, destpage => $page,
156                         template => $template, rev => $change->{rev});
157         });
158
159         my $file=$page."._change";
160         writefile($file, $config{srcdir}, $template->output);
161         utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
162
163         return $page;
164 }
165
166 1