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);
25 recentchangespage => {
27 example => "recentchanges",
28 description => "name of the recentchanges page",
35 description => "number of changes to track",
42 $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
43 $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
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.
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 # If the page is internal (like a comment), see if it has a
99 # permalink. Comments do.
100 if (IkiWiki::isinternal($page) &&
101 defined $pagestate{$page}{meta}{permalink}) {
102 IkiWiki::redirect($cgi,
103 $pagestate{$page}{meta}{permalink});
107 my $link=bestlink("", $page);
108 if (! length $link) {
109 print "Content-type: text/html\n\n";
110 print IkiWiki::misctemplate(gettext(gettext("missing page")),
112 sprintf(gettext("The page %s does not exist."),
113 htmllink("", "", $page)).
117 IkiWiki::redirect($cgi, urlto($link, undef, 1));
127 my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
129 # Optimisation to avoid re-writing pages. Assumes commits never
130 # change (or that any changes are not important).
131 return $page if exists $pagesources{$page} && ! $config{rebuild};
133 # Limit pages to first 10, and add links to the changed pages.
134 my $is_excess = exists $change->{pages}[10];
135 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
138 if (length $config{cgiurl}) {
139 $_->{link} = "<a href=\"".
141 do => "recentchanges_link",
144 "\" rel=\"nofollow\">".
145 pagetitle($_->{page}).
149 $_->{link} = pagetitle($_->{page});
151 $_->{baseurl}="$config{url}/" if length $config{url};
154 } @{$change->{pages}}
156 push @{$change->{pages}}, { link => '...' } if $is_excess;
158 # See if the committer is an openid.
159 $change->{author}=$change->{user};
160 my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
161 if (defined $oiduser) {
162 $change->{authorurl}=$change->{user};
163 $change->{user}=$oiduser;
165 elsif (length $config{cgiurl}) {
166 $change->{authorurl} = IkiWiki::cgiurl(
167 do => "recentchanges_link",
168 page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
172 if (ref $change->{message}) {
173 foreach my $field (@{$change->{message}}) {
174 if (exists $field->{line}) {
176 $field->{line} = encode_entities($field->{line});
177 # escape links and preprocessor stuff
178 $field->{line} = encode_entities($field->{line}, '\[\]');
183 # Fill out a template with the change info.
184 my $template=template("change.tmpl", blind_cache => 1);
187 commitdate => displaytime($change->{when}, "%X %x"),
188 wikiname => $config{wikiname},
191 $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
192 if exists $config{url};
194 IkiWiki::run_hooks(pagetemplate => sub {
195 shift->(page => $page, destpage => $page,
196 template => $template, rev => $change->{rev});
199 my $file=$page."._change";
200 writefile($file, $config{srcdir}, $template->output);
201 utime $change->{when}, $change->{when}, "$config{srcdir}/$file";