2 package IkiWiki::Plugin::recentchanges;
11 hook(type => "getsetup", id => "recentchanges", call => \&getsetup);
12 hook(type => "checkconfig", id => "recentchanges", call => \&checkconfig);
13 hook(type => "refresh", id => "recentchanges", call => \&refresh);
14 hook(type => "pagetemplate", id => "recentchanges", call => \&pagetemplate);
15 hook(type => "htmlize", id => "_change", call => \&htmlize);
16 hook(type => "cgi", id => "recentchanges", call => \&cgi);
19 sub getsetup () { #{{{
25 recentchangespage => {
27 example => "recentchanges",
28 description => "name of the recentchanges page",
35 description => "number of changes to track",
41 sub checkconfig () { #{{{
42 $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
43 $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
46 sub refresh ($) { #{{{
50 foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
51 $seen{store($change, $config{recentchangespage})}=1;
54 # delete old and excess changes
55 foreach my $page (keys %pagesources) {
56 if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
57 unlink($config{srcdir}.'/'.$pagesources{$page});
62 # Enable the recentchanges link on wiki pages.
63 sub pagetemplate (@) { #{{{
65 my $template=$params{template};
66 my $page=$params{page};
68 if (defined $config{recentchangespage} && $config{rcs} &&
69 $page ne $config{recentchangespage} &&
70 $template->query(name => "recentchangesurl")) {
71 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
72 $template->param(have_actions => 1);
76 # Pages with extension _change have plain html markup, pass through.
77 sub htmlize (@) { #{{{
79 return $params{content};
84 if (defined $cgi->param('do') && $cgi->param('do') eq "recentchanges_link") {
85 # This is a link from a change page to some
86 # other page. Since the change pages are only generated
87 # once, statically, links on them won't be updated if the
88 # page they link to is deleted, or newly created, or
89 # changes for whatever reason. So this CGI handles that
90 # dynamic linking stuff.
91 my $page=decode_utf8($cgi->param("page"));
93 error("missing page parameter");
98 my $link=bestlink("", $page);
100 print "Content-type: text/html\n\n";
101 print IkiWiki::misctemplate(gettext(gettext("missing page")),
103 sprintf(gettext("The page %s does not exist."),
104 htmllink("", "", $page)).
108 IkiWiki::redirect($cgi, urlto($link, undef, 1));
115 sub store ($$$) { #{{{
118 my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
120 # Optimisation to avoid re-writing pages. Assumes commits never
121 # change (or that any changes are not important).
122 return $page if exists $pagesources{$page} && ! $config{rebuild};
124 # Limit pages to first 10, and add links to the changed pages.
125 my $is_excess = exists $change->{pages}[10];
126 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
129 if (length $config{cgiurl}) {
130 $_->{link} = "<a href=\"".
132 do => "recentchanges_link",
135 "\" rel=\"nofollow\">".
136 pagetitle($_->{page}).
140 $_->{link} = pagetitle($_->{page});
142 $_->{baseurl}="$config{url}/" if length $config{url};
145 } @{$change->{pages}}
147 push @{$change->{pages}}, { link => '...' } if $is_excess;
149 # See if the committer is an openid.
150 $change->{author}=$change->{user};
151 my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
152 if (defined $oiduser) {
153 $change->{authorurl}=$change->{user};
154 $change->{user}=$oiduser;
156 elsif (length $config{cgiurl}) {
157 $change->{authorurl} = IkiWiki::cgiurl(
158 do => "recentchanges_link",
159 page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
163 if (ref $change->{message}) {
164 foreach my $field (@{$change->{message}}) {
165 if (exists $field->{line}) {
167 $field->{line} = encode_entities($field->{line});
168 # escape links and preprocessor stuff
169 $field->{line} = encode_entities($field->{line}, '\[\]');
174 # Fill out a template with the change info.
175 my $template=template("change.tmpl", blind_cache => 1);
178 commitdate => displaytime($change->{when}, "%X %x"),
179 wikiname => $config{wikiname},
182 $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
183 if exists $config{url};
185 IkiWiki::run_hooks(pagetemplate => sub {
186 shift->(page => $page, destpage => $page,
187 template => $template, rev => $change->{rev});
190 my $file=$page."._change";
191 writefile($file, $config{srcdir}, $template->output);
192 utime $change->{when}, $change->{when}, "$config{srcdir}/$file";