]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/recentchanges.pm
improve fix to DTRT for a param of "0"
[git.ikiwiki.info.git] / IkiWiki / Plugin / recentchanges.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::recentchanges;
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
8 sub import { #{{{
9         hook(type => "getsetup", id => "recentchanges", call => \&getsetup);
10         hook(type => "checkconfig", id => "recentchanges", call => \&checkconfig);
11         hook(type => "refresh", id => "recentchanges", call => \&refresh);
12         hook(type => "pagetemplate", id => "recentchanges", call => \&pagetemplate);
13         hook(type => "htmlize", id => "_change", call => \&htmlize);
14         hook(type => "cgi", id => "recentchanges", call => \&cgi);
15 } #}}}
17 sub getsetup () { #{{{
18         return
19                 plugin => {
20                         safe => 1,
21                         rebuild => 1,
22                 },
23                 recentchangespage => {
24                         type => "string",
25                         example => "recentchanges",
26                         description => "name of the recentchanges page",
27                         safe => 1,
28                         rebuild => 1,
29                 },
30                 recentchangesnum => {
31                         type => "integer",
32                         example => 100,
33                         description => "number of changes to track",
34                         safe => 1,
35                         rebuild => 0,
36                 },
37 } #}}}
39 sub checkconfig () { #{{{
40         $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
41         $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
42 } #}}}
44 sub refresh ($) { #{{{
45         my %seen;
47         # add new changes
48         foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
49                 $seen{store($change, $config{recentchangespage})}=1;
50         }
51         
52         # delete old and excess changes
53         foreach my $page (keys %pagesources) {
54                 if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
55                         unlink($config{srcdir}.'/'.$pagesources{$page});
56                 }
57         }
58 } #}}}
60 # Enable the recentchanges link on wiki pages.
61 sub pagetemplate (@) { #{{{
62         my %params=@_;
63         my $template=$params{template};
64         my $page=$params{page};
66         if (defined $config{recentchangespage} && $config{rcs} &&
67             $page ne $config{recentchangespage} &&
68             $template->query(name => "recentchangesurl")) {
69                 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
70                 $template->param(have_actions => 1);
71         }
72 } #}}}
74 # Pages with extension _change have plain html markup, pass through.
75 sub htmlize (@) { #{{{
76         my %params=@_;
77         return $params{content};
78 } #}}}
80 sub cgi ($) { #{{{
81         my $cgi=shift;
82         if (defined $cgi->param('do') && $cgi->param('do') eq "recentchanges_link") {
83                 # This is a link from a change page to some
84                 # other page. Since the change pages are only generated
85                 # once, statically, links on them won't be updated if the
86                 # page they link to is deleted, or newly created, or
87                 # changes for whatever reason. So this CGI handles that
88                 # dynamic linking stuff.
89                 my $page=$cgi->param("page");
90                 if (!defined $page) {
91                         error("missing page parameter");
92                 }
94                 IkiWiki::loadindex();
96                 my $link=bestlink("", $page);
97                 if (! length $link) {
98                         print "Content-type: text/html\n\n";
99                         print IkiWiki::misctemplate(gettext(gettext("missing page")),
100                                 "<p>".
101                                 sprintf(gettext("The page %s does not exist."),
102                                         htmllink("", "", $page)).
103                                 "</p>");
104                 }
105                 else {
106                         IkiWiki::redirect($cgi, $config{url}.IkiWiki::beautify_urlpath("/".htmlpage($link)));
107                 }
109                 exit;
110         }
113 sub store ($$$) { #{{{
114         my $change=shift;
116         my $page="$config{recentchangespage}/change_".IkiWiki::titlepage($change->{rev});
118         # Optimisation to avoid re-writing pages. Assumes commits never
119         # change (or that any changes are not important).
120         return $page if exists $pagesources{$page} && ! $config{rebuild};
122         # Limit pages to first 10, and add links to the changed pages.
123         my $is_excess = exists $change->{pages}[10];
124         delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
125         $change->{pages} = [
126                 map {
127                         if (length $config{cgiurl}) {
128                                 $_->{link} = "<a href=\"".
129                                         IkiWiki::cgiurl(
130                                                 do => "recentchanges_link",
131                                                 page => $_->{page}
132                                         ).
133                                         "\">".
134                                         IkiWiki::pagetitle($_->{page}).
135                                         "</a>"
136                         }
137                         else {
138                                 $_->{link} = IkiWiki::pagetitle($_->{page});
139                         }
140                         $_->{baseurl}="$config{url}/" if length $config{url};
142                         $_;
143                 } @{$change->{pages}}
144         ];
145         push @{$change->{pages}}, { link => '...' } if $is_excess;
147         # See if the committer is an openid.
148         $change->{author}=$change->{user};
149         my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
150         if (defined $oiduser) {
151                 $change->{authorurl}=$change->{user};
152                 $change->{user}=$oiduser;
153         }
154         elsif (length $config{cgiurl}) {
155                 $change->{authorurl} = IkiWiki::cgiurl(
156                         do => "recentchanges_link",
157                         page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
158                 );
159         }
161         # escape wikilinks and preprocessor stuff in commit messages
162         if (ref $change->{message}) {
163                 foreach my $field (@{$change->{message}}) {
164                         if (exists $field->{line}) {
165                                 $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
166                         }
167                 }
168         }
170         # Fill out a template with the change info.
171         my $template=template("change.tmpl", blind_cache => 1);
172         $template->param(
173                 %$change,
174                 commitdate => displaytime($change->{when}, "%X %x"),
175                 wikiname => $config{wikiname},
176         );
177         IkiWiki::run_hooks(pagetemplate => sub {
178                 shift->(page => $page, destpage => $page,
179                         template => $template, rev => $change->{rev});
180         });
182         my $file=$page."._change";
183         writefile($file, $config{srcdir}, $template->output);
184         utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
186         return $page;
187 } #}}}