2 package IkiWiki::Plugin::recentchanges;
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);
18 sub getsetup () { #{{{
24 recentchangespage => {
26 example => "recentchanges",
27 description => "name of the recentchanges page",
34 description => "number of changes to track",
40 sub checkconfig () { #{{{
41 $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
42 $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
45 sub refresh ($) { #{{{
49 foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
50 $seen{store($change, $config{recentchangespage})}=1;
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});
61 # Enable the recentchanges link on wiki pages.
62 sub pagetemplate (@) { #{{{
64 my $template=$params{template};
65 my $page=$params{page};
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);
75 # Pages with extension _change have plain html markup, pass through.
76 sub htmlize (@) { #{{{
78 return $params{content};
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"));
92 error("missing page parameter");
97 my $link=bestlink("", $page);
99 print "Content-type: text/html\n\n";
100 print IkiWiki::misctemplate(gettext(gettext("missing page")),
102 sprintf(gettext("The page %s does not exist."),
103 htmllink("", "", $page)).
107 IkiWiki::redirect($cgi, urlto($link, undef, 1));
114 sub store ($$$) { #{{{
117 my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
119 # Optimisation to avoid re-writing pages. Assumes commits never
120 # change (or that any changes are not important).
121 return $page if exists $pagesources{$page} && ! $config{rebuild};
123 # Limit pages to first 10, and add links to the changed pages.
124 my $is_excess = exists $change->{pages}[10];
125 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
128 if (length $config{cgiurl}) {
129 $_->{link} = "<a href=\"".
131 do => "recentchanges_link",
135 pagetitle($_->{page}).
139 $_->{link} = pagetitle($_->{page});
141 $_->{baseurl}="$config{url}/" if length $config{url};
144 } @{$change->{pages}}
146 push @{$change->{pages}}, { link => '...' } if $is_excess;
148 # See if the committer is an openid.
149 $change->{author}=$change->{user};
150 my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
151 if (defined $oiduser) {
152 $change->{authorurl}=$change->{user};
153 $change->{user}=$oiduser;
155 elsif (length $config{cgiurl}) {
156 $change->{authorurl} = IkiWiki::cgiurl(
157 do => "recentchanges_link",
158 page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
162 # escape wikilinks and preprocessor stuff in commit messages
163 if (ref $change->{message}) {
164 foreach my $field (@{$change->{message}}) {
165 if (exists $field->{line}) {
166 $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
171 # Fill out a template with the change info.
172 my $template=template("change.tmpl", blind_cache => 1);
175 commitdate => displaytime($change->{when}, "%X %x"),
176 wikiname => $config{wikiname},
179 $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
180 if exists $config{url};
182 IkiWiki::run_hooks(pagetemplate => sub {
183 shift->(page => $page, destpage => $page,
184 template => $template, rev => $change->{rev});
187 my $file=$page."._change";
188 writefile($file, $config{srcdir}, $template->output);
189 utime $change->{when}, $change->{when}, "$config{srcdir}/$file";