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