2 package IkiWiki::Plugin::recentchanges;
11 hook(type => "getsetup", id => "recentchanges", call => \&getsetup);
12 hook(type => "checkconfig", id => "recentchanges", call => \&checkconfig);
13 hook(type => "refresh", id => "recentchanges", call => \&refresh);
14 hook(type => "pagetemplate", id => "recentchanges", call => \&pagetemplate);
15 hook(type => "htmlize", id => "_change", call => \&htmlize);
16 hook(type => "sessioncgi", id => "recentchanges", call => \&sessioncgi);
17 # Load goto to fix up links from recentchanges
18 IkiWiki::loadplugin("goto");
27 recentchangespage => {
29 example => "recentchanges",
30 description => "name of the recentchanges page",
37 description => "number of changes to track",
44 $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
45 $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
52 foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
53 $seen{store($change, $config{recentchangespage})}=1;
56 # delete old and excess changes
57 foreach my $page (keys %pagesources) {
58 if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
59 unlink($config{srcdir}.'/'.$pagesources{$page});
64 sub confirmation_form {
65 my ($q, $session, $rev) = @_;
67 eval q{use CGI::FormBuilder};
69 my $f = CGI::FormBuilder->new(
76 action => $config{cgiurl},
78 template => { template('revert.tmpl') },
81 $f->field(name => "sid", type => "hidden", value => $session->id,
83 $f->field(name => "do", type => "hidden", value => "revert", force => 1);
85 return $f, ["Revert", "Cancel"];
89 my ($q, $session) = @_;
90 my $do = $q->param('do');
91 my $rev = $q->param('rev');
93 return unless $do eq 'revert' && $rev;
95 IkiWiki::rcs_preprevert(cgi => $q, session => $session, rev => $rev);
97 my ($form, $buttons) = confirmation_form($q, $session);
98 IkiWiki::decode_form_utf8($form);
100 if($form->submitted eq 'Revert' && $form->validate) {
101 IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
103 IkiWiki::disable_commit_hook();
104 my $r = IkiWiki::rcs_revert(
107 IkiWiki::enable_commit_hook();
110 die "Revert '$rev' failed.";
112 require IkiWiki::Render;
114 IkiWiki::saveindex();
117 $form->title(sprintf(gettext("confirm reversion of %s"), $rev));
118 my $patch_contents = IkiWiki::rcs_showpatch(rev => $rev);
119 $form->tmpl_param(patch_contents => encode_entities($patch_contents));
120 $form->field(name => "rev", type => "hidden", value => $rev, force => 1);
121 IkiWiki::showform($form, $buttons, $session, $q);
125 IkiWiki::redirect($q, urlto($config{recentchangespage}, ''));
129 # Enable the recentchanges link.
130 sub pagetemplate (@) {
132 my $template=$params{template};
133 my $page=$params{page};
135 if (defined $config{recentchangespage} && $config{rcs} &&
136 $template->query(name => "recentchangesurl") &&
137 $page ne $config{recentchangespage}) {
138 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
139 $template->param(have_actions => 1);
143 # Pages with extension _change have plain html markup, pass through.
146 return $params{content};
152 my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
154 # Optimisation to avoid re-writing pages. Assumes commits never
155 # change (or that any changes are not important).
156 return $page if exists $pagesources{$page} && ! $config{rebuild};
158 # Limit pages to first 10, and add links to the changed pages.
159 my $is_excess = exists $change->{pages}[10];
160 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
163 if (length $config{cgiurl}) {
164 $_->{link} = "<a href=\"".
169 "\" rel=\"nofollow\">".
170 pagetitle($_->{page}).
174 $_->{link} = pagetitle($_->{page});
176 $_->{baseurl}="$config{url}/" if length $config{url};
179 } @{$change->{pages}}
181 push @{$change->{pages}}, { link => '...' } if $is_excess;
183 if (length $config{cgiurl}) {
184 $change->{reverturl} = IkiWiki::cgiurl(
186 rev => $change->{rev}
190 $change->{author}=$change->{user};
191 my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
192 if (defined $oiduser) {
193 $change->{authorurl}=$change->{user};
194 $change->{user}=defined $change->{nickname} ? $change->{nickname} : $oiduser;
196 elsif (length $config{cgiurl}) {
197 $change->{authorurl} = IkiWiki::cgiurl(
199 page => IkiWiki::userpage($change->{author}),
203 if (ref $change->{message}) {
204 foreach my $field (@{$change->{message}}) {
205 if (exists $field->{line}) {
207 $field->{line} = encode_entities($field->{line});
208 # escape links and preprocessor stuff
209 $field->{line} = encode_entities($field->{line}, '\[\]');
214 # Fill out a template with the change info.
215 my $template=template("change.tmpl", blind_cache => 1);
218 commitdate => displaytime($change->{when}, "%X %x"),
219 wikiname => $config{wikiname},
222 $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
223 if exists $config{url};
225 IkiWiki::run_hooks(pagetemplate => sub {
226 shift->(page => $page, destpage => $page,
227 template => $template, rev => $change->{rev});
230 my $file=$page."._change";
231 writefile($file, $config{srcdir}, $template->output);
232 utime $change->{when}, $change->{when}, "$config{srcdir}/$file";