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 if (defined pagetype($link)) {
108 IkiWiki::redirect($cgi, $config{url}.IkiWiki::beautify_urlpath("/".htmlpage($link)));
111 IkiWiki::redirect($cgi, $config{url}.IkiWiki::beautify_urlpath("/".$link));
119 sub store ($$$) { #{{{
122 my $page="$config{recentchangespage}/change_".IkiWiki::titlepage($change->{rev});
124 # Optimisation to avoid re-writing pages. Assumes commits never
125 # change (or that any changes are not important).
126 return $page if exists $pagesources{$page} && ! $config{rebuild};
128 # Limit pages to first 10, and add links to the changed pages.
129 my $is_excess = exists $change->{pages}[10];
130 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
133 if (length $config{cgiurl}) {
134 $_->{link} = "<a href=\"".
136 do => "recentchanges_link",
140 IkiWiki::pagetitle($_->{page}).
144 $_->{link} = IkiWiki::pagetitle($_->{page});
146 $_->{baseurl}="$config{url}/" if length $config{url};
149 } @{$change->{pages}}
151 push @{$change->{pages}}, { link => '...' } if $is_excess;
153 # See if the committer is an openid.
154 $change->{author}=$change->{user};
155 my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
156 if (defined $oiduser) {
157 $change->{authorurl}=$change->{user};
158 $change->{user}=$oiduser;
160 elsif (length $config{cgiurl}) {
161 $change->{authorurl} = IkiWiki::cgiurl(
162 do => "recentchanges_link",
163 page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
167 # escape wikilinks and preprocessor stuff in commit messages
168 if (ref $change->{message}) {
169 foreach my $field (@{$change->{message}}) {
170 if (exists $field->{line}) {
171 $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
176 # Fill out a template with the change info.
177 my $template=template("change.tmpl", blind_cache => 1);
180 commitdate => displaytime($change->{when}, "%X %x"),
181 wikiname => $config{wikiname},
183 IkiWiki::run_hooks(pagetemplate => sub {
184 shift->(page => $page, destpage => $page,
185 template => $template, rev => $change->{rev});
188 my $file=$page."._change";
189 writefile($file, $config{srcdir}, $template->output);
190 utime $change->{when}, $change->{when}, "$config{srcdir}/$file";