]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/recentchanges.pm
really add
[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 => "checkconfig", id => "recentchanges", call => \&checkconfig);
10         hook(type => "refresh", id => "recentchanges", call => \&refresh);
11         hook(type => "pagetemplate", id => "recentchanges", call => \&pagetemplate);
12         hook(type => "htmlize", id => "_change", call => \&htmlize);
13         hook(type => "cgi", id => "recentchanges", call => \&cgi);
14 } #}}}
16 sub checkconfig () { #{{{
17         $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
18         $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
19 } #}}}
21 sub refresh ($) { #{{{
22         my %seen;
24         # add new changes
25         foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
26                 $seen{store($change, $config{recentchangespage})}=1;
27         }
28         
29         # delete old and excess changes
30         foreach my $page (keys %pagesources) {
31                 if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
32                         unlink($config{srcdir}.'/'.$pagesources{$page});
33                 }
34         }
35 } #}}}
37 # Enable the recentchanges link on wiki pages.
38 sub pagetemplate (@) { #{{{
39         my %params=@_;
40         my $template=$params{template};
41         my $page=$params{page};
43         if (defined $config{recentchangespage} && $config{rcs} &&
44             $page ne $config{recentchangespage} &&
45             $template->query(name => "recentchangesurl")) {
46                 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
47                 $template->param(have_actions => 1);
48         }
49 } #}}}
51 # Pages with extension _change have plain html markup, pass through.
52 sub htmlize (@) { #{{{
53         my %params=@_;
54         return $params{content};
55 } #}}}
57 sub cgi ($) { #{{{
58         my $cgi=shift;
59         if (defined $cgi->param('do') && $cgi->param('do') eq "recentchanges_link") {
60                 # This is a link from a change page to some
61                 # other page. Since the change pages are only generated
62                 # once, statically, links on them won't be updated if the
63                 # page they link to is deleted, or newly created, or
64                 # changes for whatever reason. So this CGI handles that
65                 # dynamic linking stuff.
66                 my $page=$cgi->param("page");
67                 if (!defined $page) {
68                         error("missing page parameter");
69                 }
71                 IkiWiki::loadindex();
73                 my $link=bestlink("", $page);
74                 if (! length $link) {
75                         print "Content-type: text/html\n\n";
76                         print IkiWiki::misctemplate(gettext(gettext("missing page")),
77                                 "<p>".
78                                 sprintf(gettext("The page %s does not exist."),
79                                         htmllink("", "", $page)).
80                                 "</p>");
81                 }
82                 else {
83                         IkiWiki::redirect($cgi, $config{url}.IkiWiki::beautify_urlpath("/".htmlpage($link)));
84                 }
86                 exit;
87         }
88 }
90 sub store ($$$) { #{{{
91         my $change=shift;
93         my $page="$config{recentchangespage}/change_".IkiWiki::titlepage($change->{rev});
95         # Optimisation to avoid re-writing pages. Assumes commits never
96         # change (or that any changes are not important).
97         return $page if exists $pagesources{$page} && ! $config{rebuild};
99         # Limit pages to first 10, and add links to the changed pages.
100         my $is_excess = exists $change->{pages}[10];
101         delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
102         $change->{pages} = [
103                 map {
104                         if (length $config{cgiurl}) {
105                                 $_->{link} = "<a href=\"".
106                                         IkiWiki::cgiurl(
107                                                 do => "recentchanges_link",
108                                                 page => $_->{page}
109                                         ).
110                                         "\">".
111                                         IkiWiki::pagetitle($_->{page}).
112                                         "</a>"
113                         }
114                         else {
115                                 $_->{link} = IkiWiki::pagetitle($_->{page});
116                         }
117                         $_->{baseurl}="$config{url}/" if length $config{url};
119                         $_;
120                 } @{$change->{pages}}
121         ];
122         push @{$change->{pages}}, { link => '...' } if $is_excess;
124         # See if the committer is an openid.
125         $change->{author}=$change->{user};
126         my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
127         if (defined $oiduser) {
128                 $change->{authorurl}=$change->{user};
129                 $change->{user}=$oiduser;
130         }
131         elsif (length $config{cgiurl}) {
132                 $change->{authorurl} = IkiWiki::cgiurl(
133                         do => "recentchanges_link",
134                         page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
135                 );
136         }
138         # escape wikilinks and preprocessor stuff in commit messages
139         if (ref $change->{message}) {
140                 foreach my $field (@{$change->{message}}) {
141                         if (exists $field->{line}) {
142                                 $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
143                         }
144                 }
145         }
147         # Fill out a template with the change info.
148         my $template=template("change.tmpl", blind_cache => 1);
149         $template->param(
150                 %$change,
151                 commitdate => displaytime($change->{when}, "%X %x"),
152                 wikiname => $config{wikiname},
153         );
154         IkiWiki::run_hooks(pagetemplate => sub {
155                 shift->(page => $page, destpage => $page,
156                         template => $template, rev => $change->{rev});
157         });
159         my $file=$page."._change";
160         writefile($file, $config{srcdir}, $template->output);
161         utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
163         return $page;
164 } #}}}