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 if (defined pagetype($link)) {
107 IkiWiki::redirect($cgi, $config{url}.IkiWiki::beautify_urlpath("/".htmlpage($link)));
110 IkiWiki::redirect($cgi, $config{url}.IkiWiki::beautify_urlpath("/".$link));
118 sub store ($$$) { #{{{
121 my $page="$config{recentchangespage}/change_".IkiWiki::titlepage($change->{rev});
123 # Optimisation to avoid re-writing pages. Assumes commits never
124 # change (or that any changes are not important).
125 return $page if exists $pagesources{$page} && ! $config{rebuild};
127 # Limit pages to first 10, and add links to the changed pages.
128 my $is_excess = exists $change->{pages}[10];
129 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
132 if (length $config{cgiurl}) {
133 $_->{link} = "<a href=\"".
135 do => "recentchanges_link",
139 IkiWiki::pagetitle($_->{page}).
143 $_->{link} = IkiWiki::pagetitle($_->{page});
145 $_->{baseurl}="$config{url}/" if length $config{url};
148 } @{$change->{pages}}
150 push @{$change->{pages}}, { link => '...' } if $is_excess;
152 # See if the committer is an openid.
153 $change->{author}=$change->{user};
154 my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
155 if (defined $oiduser) {
156 $change->{authorurl}=$change->{user};
157 $change->{user}=$oiduser;
159 elsif (length $config{cgiurl}) {
160 $change->{authorurl} = IkiWiki::cgiurl(
161 do => "recentchanges_link",
162 page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
166 # escape wikilinks and preprocessor stuff in commit messages
167 if (ref $change->{message}) {
168 foreach my $field (@{$change->{message}}) {
169 if (exists $field->{line}) {
170 $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
175 # Fill out a template with the change info.
176 my $template=template("change.tmpl", blind_cache => 1);
179 commitdate => displaytime($change->{when}, "%X %x"),
180 wikiname => $config{wikiname},
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";