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 () { #{{{
23 recentchangespage => {
25 example => "recentchanges",
26 description => "name of the recentchanges page",
33 description => "number of changes to track",
39 sub checkconfig () { #{{{
40 $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
41 $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
44 sub refresh ($) { #{{{
48 foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
49 $seen{store($change, $config{recentchangespage})}=1;
52 # delete old and excess changes
53 foreach my $page (keys %pagesources) {
54 if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
55 unlink($config{srcdir}.'/'.$pagesources{$page});
60 # Enable the recentchanges link on wiki pages.
61 sub pagetemplate (@) { #{{{
63 my $template=$params{template};
64 my $page=$params{page};
66 if (defined $config{recentchangespage} && $config{rcs} &&
67 $page ne $config{recentchangespage} &&
68 $template->query(name => "recentchangesurl")) {
69 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
70 $template->param(have_actions => 1);
74 # Pages with extension _change have plain html markup, pass through.
75 sub htmlize (@) { #{{{
77 return $params{content};
82 if (defined $cgi->param('do') && $cgi->param('do') eq "recentchanges_link") {
83 # This is a link from a change page to some
84 # other page. Since the change pages are only generated
85 # once, statically, links on them won't be updated if the
86 # page they link to is deleted, or newly created, or
87 # changes for whatever reason. So this CGI handles that
88 # dynamic linking stuff.
89 my $page=$cgi->param("page");
91 error("missing page parameter");
96 my $link=bestlink("", $page);
98 print "Content-type: text/html\n\n";
99 print IkiWiki::misctemplate(gettext(gettext("missing page")),
101 sprintf(gettext("The page %s does not exist."),
102 htmllink("", "", $page)).
106 IkiWiki::redirect($cgi, $config{url}.IkiWiki::beautify_urlpath("/".htmlpage($link)));
113 sub store ($$$) { #{{{
116 my $page="$config{recentchangespage}/change_".IkiWiki::titlepage($change->{rev});
118 # Optimisation to avoid re-writing pages. Assumes commits never
119 # change (or that any changes are not important).
120 return $page if exists $pagesources{$page} && ! $config{rebuild};
122 # Limit pages to first 10, and add links to the changed pages.
123 my $is_excess = exists $change->{pages}[10];
124 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
127 if (length $config{cgiurl}) {
128 $_->{link} = "<a href=\"".
130 do => "recentchanges_link",
134 IkiWiki::pagetitle($_->{page}).
138 $_->{link} = IkiWiki::pagetitle($_->{page});
140 $_->{baseurl}="$config{url}/" if length $config{url};
143 } @{$change->{pages}}
145 push @{$change->{pages}}, { link => '...' } if $is_excess;
147 # See if the committer is an openid.
148 $change->{author}=$change->{user};
149 my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
150 if (defined $oiduser) {
151 $change->{authorurl}=$change->{user};
152 $change->{user}=$oiduser;
154 elsif (length $config{cgiurl}) {
155 $change->{authorurl} = IkiWiki::cgiurl(
156 do => "recentchanges_link",
157 page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
161 # escape wikilinks and preprocessor stuff in commit messages
162 if (ref $change->{message}) {
163 foreach my $field (@{$change->{message}}) {
164 if (exists $field->{line}) {
165 $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
170 # Fill out a template with the change info.
171 my $template=template("change.tmpl", blind_cache => 1);
174 commitdate => displaytime($change->{when}, "%X %x"),
175 wikiname => $config{wikiname},
177 IkiWiki::run_hooks(pagetemplate => sub {
178 shift->(page => $page, destpage => $page,
179 template => $template, rev => $change->{rev});
182 my $file=$page."._change";
183 writefile($file, $config{srcdir}, $template->output);
184 utime $change->{when}, $change->{when}, "$config{srcdir}/$file";