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 IkiWiki::redirect($cgi, urlto($link, undef, 1));
114 sub store ($$$) { #{{{
117 my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
118 my $baseurl = IkiWiki::baseurl();
119 my $permalink="$baseurl$config{recentchangespage}/#change-".titlepage($change->{rev});
121 # Optimisation to avoid re-writing pages. Assumes commits never
122 # change (or that any changes are not important).
123 return $page if exists $pagesources{$page} && ! $config{rebuild};
125 # Limit pages to first 10, and add links to the changed pages.
126 my $is_excess = exists $change->{pages}[10];
127 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
130 if (length $config{cgiurl}) {
131 $_->{link} = "<a href=\"".
133 do => "recentchanges_link",
137 pagetitle($_->{page}).
141 $_->{link} = pagetitle($_->{page});
143 $_->{baseurl}="$config{url}/" if length $config{url};
146 } @{$change->{pages}}
148 push @{$change->{pages}}, { link => '...' } if $is_excess;
150 # See if the committer is an openid.
151 $change->{author}=$change->{user};
152 my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
153 if (defined $oiduser) {
154 $change->{authorurl}=$change->{user};
155 $change->{user}=$oiduser;
157 elsif (length $config{cgiurl}) {
158 $change->{authorurl} = IkiWiki::cgiurl(
159 do => "recentchanges_link",
160 page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
164 # escape wikilinks and preprocessor stuff in commit messages
165 if (ref $change->{message}) {
166 foreach my $field (@{$change->{message}}) {
167 if (exists $field->{line}) {
168 $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
173 # Fill out a template with the change info.
174 my $template=template("change.tmpl", blind_cache => 1);
177 commitdate => displaytime($change->{when}, "%X %x"),
178 commitdate_raw => scalar localtime($change->{when}),
179 wikiname => $config{wikiname},
180 permalink => $permalink,
182 IkiWiki::run_hooks(pagetemplate => sub {
183 shift->(page => $page, destpage => $page,
184 template => $template, rev => $change->{rev});
187 my $file=$page."._change";
188 writefile($file, $config{srcdir}, $template->output);
189 utime $change->{when}, $change->{when}, "$config{srcdir}/$file";