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 ($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};
42 if ($config{rcs} && $page ne $config{recentchangespage} &&
43 $template->query(name => "recentchangesurl")) {
44 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
45 $template->param(have_actions => 1);
49 # Pages with extension _change have plain html markup, pass through.
50 sub htmlize (@) { #{{{
52 return $params{content};
57 if (defined $cgi->param('do') && $cgi->param('do') eq "recentchanges_link") {
58 # This is a link from a change page to some
59 # other page. Since the change pages are only generated
60 # once, statically, links on them won't be updated if the
61 # page they link to is deleted, or newly created, or
62 # changes for whatever reason. So this CGI handles that
63 # dynamic linking stuff.
64 my $page=$cgi->param("page");
66 error("missing page parameter");
71 my $link=bestlink("", $page);
73 print "Content-type: text/html\n\n";
74 print IkiWiki::misctemplate(gettext(gettext("missing page")),
76 sprintf(gettext("The page %s does not exist."),
77 htmllink("", "", $page)).
81 IkiWiki::redirect($cgi, $config{url}."/".htmlpage($link));
88 sub store ($$$) { #{{{
91 my $page="$config{recentchangespage}/change_".IkiWiki::titlepage($change->{rev});
93 # Optimisation to avoid re-writing pages. Assumes commits never
94 # change (or that any changes are not important).
95 return $page if exists $pagesources{$page} && ! $config{rebuild};
97 # Limit pages to first 10, and add links to the changed pages.
98 my $is_excess = exists $change->{pages}[10];
99 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
102 if (length $config{cgiurl}) {
103 $_->{link} = "<a href=\"".
105 do => "recentchanges_link",
109 IkiWiki::pagetitle($_->{page}).
113 $_->{link} = IkiWiki::pagetitle($_->{page});
115 $_->{baseurl}="$config{url}/" if length $config{url};
118 } @{$change->{pages}}
120 push @{$change->{pages}}, { link => '...' } if $is_excess;
122 # See if the committer is an openid.
123 $change->{author}=$change->{user};
124 my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
125 if (defined $oiduser) {
126 $change->{authorurl}=$change->{user};
127 $change->{user}=$oiduser;
129 elsif (length $config{cgiurl}) {
130 $change->{authorurl} = IkiWiki::cgiurl(
131 do => "recentchanges_link",
132 page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
136 # escape wikilinks and preprocessor stuff in commit messages
137 if (ref $change->{message}) {
138 foreach my $field (@{$change->{message}}) {
139 if (exists $field->{line}) {
140 $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
145 # Fill out a template with the change info.
146 my $template=template("change.tmpl", blind_cache => 1);
149 commitdate => displaytime($change->{when}, "%X %x"),
150 wikiname => $config{wikiname},
152 IkiWiki::run_hooks(pagetemplate => sub {
153 shift->(page => $page, destpage => $page, template => $template);
156 my $file=$page."._change";
157 writefile($file, $config{srcdir}, $template->output);
158 utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
163 sub updatechanges ($$) { #{{{
165 my @changes=@{shift()};