]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/recentchanges.pm
Patch for anchor-based change permalinks in recent changes feed
[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 use Encode;
8
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 } #}}}
17
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 } #}}}
39
40 sub checkconfig () { #{{{
41         $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
42         $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
43 } #}}}
44
45 sub refresh ($) { #{{{
46         my %seen;
47
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 } #}}}
60
61 # Enable the recentchanges link on wiki pages.
62 sub pagetemplate (@) { #{{{
63         my %params=@_;
64         my $template=$params{template};
65         my $page=$params{page};
66
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 } #}}}
74
75 # Pages with extension _change have plain html markup, pass through.
76 sub htmlize (@) { #{{{
77         my %params=@_;
78         return $params{content};
79 } #}}}
80
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                 }
94
95                 IkiWiki::loadindex();
96
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                 }
109
110                 exit;
111         }
112 }
113
114 sub store ($$$) { #{{{
115         my $change=shift;
116
117         my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
118         my $baseurl = IkiWiki::baseurl();
119         my $permalink="$baseurl$config{recentchangespage}/#change-".titlepage($change->{rev});
120
121         # Optimisation to avoid re-writing pages. Assumes commits never
122         # change (or that any changes are not important).
123         return $page if exists $pagesources{$page} && ! $config{rebuild};
124
125         # Limit pages to first 10, and add links to the changed pages.
126         my $is_excess = exists $change->{pages}[10];
127         delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
128         $change->{pages} = [
129                 map {
130                         if (length $config{cgiurl}) {
131                                 $_->{link} = "<a href=\"".
132                                         IkiWiki::cgiurl(
133                                                 do => "recentchanges_link",
134                                                 page => $_->{page}
135                                         ).
136                                         "\">".
137                                         pagetitle($_->{page}).
138                                         "</a>"
139                         }
140                         else {
141                                 $_->{link} = pagetitle($_->{page});
142                         }
143                         $_->{baseurl}="$config{url}/" if length $config{url};
144
145                         $_;
146                 } @{$change->{pages}}
147         ];
148         push @{$change->{pages}}, { link => '...' } if $is_excess;
149
150         # See if the committer is an openid.
151         $change->{author}=$change->{user};
152         my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
153         if (defined $oiduser) {
154                 $change->{authorurl}=$change->{user};
155                 $change->{user}=$oiduser;
156         }
157         elsif (length $config{cgiurl}) {
158                 $change->{authorurl} = IkiWiki::cgiurl(
159                         do => "recentchanges_link",
160                         page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
161                 );
162         }
163
164         # escape wikilinks and preprocessor stuff in commit messages
165         if (ref $change->{message}) {
166                 foreach my $field (@{$change->{message}}) {
167                         if (exists $field->{line}) {
168                                 $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
169                         }
170                 }
171         }
172
173         # Fill out a template with the change info.
174         my $template=template("change.tmpl", blind_cache => 1);
175         $template->param(
176                 %$change,
177                 commitdate => displaytime($change->{when}, "%X %x"),
178                 commitdate_raw => scalar localtime($change->{when}),
179                 wikiname => $config{wikiname},
180                 permalink => $permalink,
181         );
182         IkiWiki::run_hooks(pagetemplate => sub {
183                 shift->(page => $page, destpage => $page,
184                         template => $template, rev => $change->{rev});
185         });
186
187         my $file=$page."._change";
188         writefile($file, $config{srcdir}, $template->output);
189         utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
190
191         return $page;
192 } #}}}
193
194 1