2 package IkiWiki::Plugin::recentchanges;
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);
17 sub getsetup () { #{{{
19 recentchangespage => {
21 example => "recentchanges",
22 description => "name of the recentchanges page",
29 description => "number of changes to track",
35 sub checkconfig () { #{{{
36 $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
37 $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
40 sub refresh ($) { #{{{
44 foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
45 $seen{store($change, $config{recentchangespage})}=1;
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});
56 # Enable the recentchanges link on wiki pages.
57 sub pagetemplate (@) { #{{{
59 my $template=$params{template};
60 my $page=$params{page};
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);
70 # Pages with extension _change have plain html markup, pass through.
71 sub htmlize (@) { #{{{
73 return $params{content};
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");
87 error("missing page parameter");
92 my $link=bestlink("", $page);
94 print "Content-type: text/html\n\n";
95 print IkiWiki::misctemplate(gettext(gettext("missing page")),
97 sprintf(gettext("The page %s does not exist."),
98 htmllink("", "", $page)).
102 IkiWiki::redirect($cgi, $config{url}.IkiWiki::beautify_urlpath("/".htmlpage($link)));
109 sub store ($$$) { #{{{
112 my $page="$config{recentchangespage}/change_".IkiWiki::titlepage($change->{rev});
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};
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;
123 if (length $config{cgiurl}) {
124 $_->{link} = "<a href=\"".
126 do => "recentchanges_link",
130 IkiWiki::pagetitle($_->{page}).
134 $_->{link} = IkiWiki::pagetitle($_->{page});
136 $_->{baseurl}="$config{url}/" if length $config{url};
139 } @{$change->{pages}}
141 push @{$change->{pages}}, { link => '...' } if $is_excess;
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;
150 elsif (length $config{cgiurl}) {
151 $change->{authorurl} = IkiWiki::cgiurl(
152 do => "recentchanges_link",
153 page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
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;
166 # Fill out a template with the change info.
167 my $template=template("change.tmpl", blind_cache => 1);
170 commitdate => displaytime($change->{when}, "%X %x"),
171 wikiname => $config{wikiname},
173 IkiWiki::run_hooks(pagetemplate => sub {
174 shift->(page => $page, destpage => $page,
175 template => $template, rev => $change->{rev});
178 my $file=$page."._change";
179 writefile($file, $config{srcdir}, $template->output);
180 utime $change->{when}, $change->{when}, "$config{srcdir}/$file";