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