2 package IkiWiki::Plugin::recentchanges;
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);
16 sub checkconfig () { #{{{
17 $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
18 $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
21 sub refresh ($) { #{{{
25 foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
26 $seen{store($change, $config{recentchangespage})}=1;
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});
37 # Enable the recentchanges link on wiki pages.
38 sub pagetemplate (@) { #{{{
40 my $template=$params{template};
41 my $page=$params{page};
43 if (defined $config{recentchangespage} && $config{rcs} &&
44 $page ne $config{recentchangespage} &&
45 $template->query(name => "recentchangesurl")) {
46 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
47 $template->param(have_actions => 1);
51 # Pages with extension _change have plain html markup, pass through.
52 sub htmlize (@) { #{{{
54 return $params{content};
59 if (defined $cgi->param('do') && $cgi->param('do') eq "recentchanges_link") {
60 # This is a link from a change page to some
61 # other page. Since the change pages are only generated
62 # once, statically, links on them won't be updated if the
63 # page they link to is deleted, or newly created, or
64 # changes for whatever reason. So this CGI handles that
65 # dynamic linking stuff.
66 my $page=$cgi->param("page");
68 error("missing page parameter");
73 my $link=bestlink("", $page);
75 print "Content-type: text/html\n\n";
76 print IkiWiki::misctemplate(gettext(gettext("missing page")),
78 sprintf(gettext("The page %s does not exist."),
79 htmllink("", "", $page)).
83 IkiWiki::redirect($cgi, $config{url}."/".htmlpage($link));
90 sub store ($$$) { #{{{
93 my $page="$config{recentchangespage}/change_".IkiWiki::titlepage($change->{rev});
95 # Optimisation to avoid re-writing pages. Assumes commits never
96 # change (or that any changes are not important).
97 return $page if exists $pagesources{$page} && ! $config{rebuild};
99 # Limit pages to first 10, and add links to the changed pages.
100 my $is_excess = exists $change->{pages}[10];
101 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
104 if (length $config{cgiurl}) {
105 $_->{link} = "<a href=\"".
107 do => "recentchanges_link",
111 IkiWiki::pagetitle($_->{page}).
115 $_->{link} = IkiWiki::pagetitle($_->{page});
117 $_->{baseurl}="$config{url}/" if length $config{url};
120 } @{$change->{pages}}
122 push @{$change->{pages}}, { link => '...' } if $is_excess;
124 # See if the committer is an openid.
125 $change->{author}=$change->{user};
126 my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
127 if (defined $oiduser) {
128 $change->{authorurl}=$change->{user};
129 $change->{user}=$oiduser;
131 elsif (length $config{cgiurl}) {
132 $change->{authorurl} = IkiWiki::cgiurl(
133 do => "recentchanges_link",
134 page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
138 # escape wikilinks and preprocessor stuff in commit messages
139 if (ref $change->{message}) {
140 foreach my $field (@{$change->{message}}) {
141 if (exists $field->{line}) {
142 $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
147 # Fill out a template with the change info.
148 my $template=template("change.tmpl", blind_cache => 1);
151 commitdate => displaytime($change->{when}, "%X %x"),
152 wikiname => $config{wikiname},
154 IkiWiki::run_hooks(pagetemplate => sub {
155 shift->(page => $page, destpage => $page,
156 template => $template, rev => $change->{rev});
159 my $file=$page."._change";
160 writefile($file, $config{srcdir}, $template->output);
161 utime $change->{when}, $change->{when}, "$config{srcdir}/$file";