enable action bar on misctemplates
[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 => "pageactions", id => "recentchanges", call => \&pageactions);
15         hook(type => "pagetemplate", id => "recentchanges", call => \&pagetemplate);
16         hook(type => "htmlize", id => "_change", call => \&htmlize);
17         # Load goto to fix up links from recentchanges
18         IkiWiki::loadplugin("goto");
19 }
20
21 sub getsetup () {
22         return
23                 plugin => {
24                         safe => 1,
25                         rebuild => 1,
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 pageactions (@) {
66         my %params=@_;
67         my $page=$params{page};
68
69         if (defined $config{recentchangespage} && $config{rcs} &&
70             $page ne $config{recentchangespage}) {
71                 return htmllink($page, $page, $config{recentchangespage},
72                         gettext("RecentChanges"));
73         }
74 }
75
76 # Backwards compatability for templates still using
77 # RECENTCHANGESURL.
78 sub pagetemplate (@) {
79         my %params=@_;
80         my $template=$params{template};
81         my $page=$params{page};
82
83         if (defined $config{recentchangespage} && $config{rcs} &&
84             $template->query(name => "recentchangesurl") &&
85             $page ne $config{recentchangespage}) {
86                 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
87                 $template->param(have_actions => 1);
88         }
89 }
90
91 # Pages with extension _change have plain html markup, pass through.
92 sub htmlize (@) {
93         my %params=@_;
94         return $params{content};
95 }
96
97 sub store ($$$) {
98         my $change=shift;
99
100         my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
101
102         # Optimisation to avoid re-writing pages. Assumes commits never
103         # change (or that any changes are not important).
104         return $page if exists $pagesources{$page} && ! $config{rebuild};
105
106         # Limit pages to first 10, and add links to the changed pages.
107         my $is_excess = exists $change->{pages}[10];
108         delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
109         $change->{pages} = [
110                 map {
111                         if (length $config{cgiurl}) {
112                                 $_->{link} = "<a href=\"".
113                                         IkiWiki::cgiurl(
114                                                 do => "goto",
115                                                 page => $_->{page}
116                                         ).
117                                         "\" rel=\"nofollow\">".
118                                         pagetitle($_->{page}).
119                                         "</a>"
120                         }
121                         else {
122                                 $_->{link} = pagetitle($_->{page});
123                         }
124                         $_->{baseurl}="$config{url}/" if length $config{url};
125
126                         $_;
127                 } @{$change->{pages}}
128         ];
129         push @{$change->{pages}}, { link => '...' } if $is_excess;
130
131         # See if the committer is an openid.
132         $change->{author}=$change->{user};
133         my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
134         if (defined $oiduser) {
135                 $change->{authorurl}=$change->{user};
136                 $change->{user}=$oiduser;
137         }
138         elsif (length $config{cgiurl}) {
139                 $change->{authorurl} = IkiWiki::cgiurl(
140                         do => "goto",
141                         page => IkiWiki::userpage($change->{author}),
142                 );
143         }
144
145         if (ref $change->{message}) {
146                 foreach my $field (@{$change->{message}}) {
147                         if (exists $field->{line}) {
148                                 # escape html
149                                 $field->{line} = encode_entities($field->{line});
150                                 # escape links and preprocessor stuff
151                                 $field->{line} = encode_entities($field->{line}, '\[\]');
152                         }
153                 }
154         }
155
156         # Fill out a template with the change info.
157         my $template=template("change.tmpl", blind_cache => 1);
158         $template->param(
159                 %$change,
160                 commitdate => displaytime($change->{when}, "%X %x"),
161                 wikiname => $config{wikiname},
162         );
163         
164         $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
165                 if exists $config{url};
166         
167         IkiWiki::run_hooks(pagetemplate => sub {
168                 shift->(page => $page, destpage => $page,
169                         template => $template, rev => $change->{rev});
170         });
171
172         my $file=$page."._change";
173         writefile($file, $config{srcdir}, $template->output);
174         utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
175
176         return $page;
177 }
178
179 1