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