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 # XXX this is here because I've been seeing a strange uninitialized
45 if (! defined $config{recentchangespage}) {
47 Carp::cluck("undefined recentchangespage; please report this to Joey");
50 if ($config{rcs} && $page ne $config{recentchangespage} &&
51 $template->query(name => "recentchangesurl")) {
52 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
53 $template->param(have_actions => 1);
57 # Pages with extension _change have plain html markup, pass through.
58 sub htmlize (@) { #{{{
60 return $params{content};
65 if (defined $cgi->param('do') && $cgi->param('do') eq "recentchanges_link") {
66 # This is a link from a change page to some
67 # other page. Since the change pages are only generated
68 # once, statically, links on them won't be updated if the
69 # page they link to is deleted, or newly created, or
70 # changes for whatever reason. So this CGI handles that
71 # dynamic linking stuff.
72 my $page=$cgi->param("page");
74 error("missing page parameter");
79 my $link=bestlink("", $page);
81 print "Content-type: text/html\n\n";
82 print IkiWiki::misctemplate(gettext(gettext("missing page")),
84 sprintf(gettext("The page %s does not exist."),
85 htmllink("", "", $page)).
89 IkiWiki::redirect($cgi, $config{url}."/".htmlpage($link));
96 sub store ($$$) { #{{{
99 my $page="$config{recentchangespage}/change_".IkiWiki::titlepage($change->{rev});
101 # Optimisation to avoid re-writing pages. Assumes commits never
102 # change (or that any changes are not important).
103 return $page if exists $pagesources{$page} && ! $config{rebuild};
105 # Limit pages to first 10, and add links to the changed pages.
106 my $is_excess = exists $change->{pages}[10];
107 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
110 if (length $config{cgiurl}) {
111 $_->{link} = "<a href=\"".
113 do => "recentchanges_link",
117 IkiWiki::pagetitle($_->{page}).
121 $_->{link} = IkiWiki::pagetitle($_->{page});
123 $_->{baseurl}="$config{url}/" if length $config{url};
126 } @{$change->{pages}}
128 push @{$change->{pages}}, { link => '...' } if $is_excess;
130 # See if the committer is an openid.
131 $change->{author}=$change->{user};
132 my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
133 if (defined $oiduser) {
134 $change->{authorurl}=$change->{user};
135 $change->{user}=$oiduser;
137 elsif (length $config{cgiurl}) {
138 $change->{authorurl} = IkiWiki::cgiurl(
139 do => "recentchanges_link",
140 page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
144 # escape wikilinks and preprocessor stuff in commit messages
145 if (ref $change->{message}) {
146 foreach my $field (@{$change->{message}}) {
147 if (exists $field->{line}) {
148 $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
153 # Fill out a template with the change info.
154 my $template=template("change.tmpl", blind_cache => 1);
157 commitdate => displaytime($change->{when}, "%X %x"),
158 wikiname => $config{wikiname},
160 IkiWiki::run_hooks(pagetemplate => sub {
161 shift->(page => $page, destpage => $page,
162 template => $template, rev => $change->{rev});
165 my $file=$page."._change";
166 writefile($file, $config{srcdir}, $template->output);
167 utime $change->{when}, $change->{when}, "$config{srcdir}/$file";