]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/recentchanges.pm
it's not $title, try the other variable
[ikiwiki.git] / IkiWiki / Plugin / recentchanges.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::recentchanges;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 sub import { #{{{
9         hook(type => "checkconfig", id => "recentchanges", call => \&checkconfig);
10         hook(type => "refresh", id => "recentchanges", call => \&refresh);
11         hook(type => "pagetemplate", id => "recentchanges", call => \&pagetemplate);
12         hook(type => "htmlize", id => "_change", call => \&htmlize);
13         hook(type => "cgi", id => "recentchanges", call => \&cgi);
14 } #}}}
15
16 sub checkconfig () { #{{{
17         $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
18         $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
19 } #}}}
20
21 sub refresh ($) { #{{{
22         my %seen;
23
24         # add new changes
25         foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
26                 $seen{store($change, $config{recentchangespage})}=1;
27         }
28         
29         # delete old and excess changes
30         foreach my $page (keys %pagesources) {
31                 if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
32                         unlink($config{srcdir}.'/'.$pagesources{$page});
33                 }
34         }
35 } #}}}
36
37 # Enable the recentchanges link on wiki pages.
38 sub pagetemplate (@) { #{{{
39         my %params=@_;
40         my $template=$params{template};
41         my $page=$params{page};
42
43         # XXX this is here because I've been seeing a strange uninitialized
44         # value in this sub.
45         if (! defined $config{recentchangespage}) {
46                 eval q{use Carp};
47                 Carp::cluck("undefined recentchangespage; please report this to Joey");
48         }
49
50         if ($config{rcs} && $page ne $config{recentchangespage} &&
51             $template->query(name => "recentchangesurl")) {
52                 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
53                 $template->param(have_actions => 1);
54         }
55 } #}}}
56
57 # Pages with extension _change have plain html markup, pass through.
58 sub htmlize (@) { #{{{
59         my %params=@_;
60         return $params{content};
61 } #}}}
62
63 sub cgi ($) { #{{{
64         my $cgi=shift;
65         if (defined $cgi->param('do') && $cgi->param('do') eq "recentchanges_link") {
66                 # This is a link from a change page to some
67                 # other page. Since the change pages are only generated
68                 # once, statically, links on them won't be updated if the
69                 # page they link to is deleted, or newly created, or
70                 # changes for whatever reason. So this CGI handles that
71                 # dynamic linking stuff.
72                 my $page=$cgi->param("page");
73                 if (!defined $page) {
74                         error("missing page parameter");
75                 }
76
77                 IkiWiki::loadindex();
78
79                 my $link=bestlink("", $page);
80                 if (! length $link) {
81                         print "Content-type: text/html\n\n";
82                         print IkiWiki::misctemplate(gettext(gettext("missing page")),
83                                 "<p>".
84                                 sprintf(gettext("The page %s does not exist."),
85                                         htmllink("", "", $page)).
86                                 "</p>");
87                 }
88                 else {
89                         IkiWiki::redirect($cgi, $config{url}."/".htmlpage($link));
90                 }
91
92                 exit;
93         }
94 }
95
96 sub store ($$$) { #{{{
97         my $change=shift;
98
99         my $page="$config{recentchangespage}/change_".IkiWiki::titlepage($change->{rev});
100
101         # Optimisation to avoid re-writing pages. Assumes commits never
102         # change (or that any changes are not important).
103         return $page if exists $pagesources{$page} && ! $config{rebuild};
104
105         # Limit pages to first 10, and add links to the changed pages.
106         my $is_excess = exists $change->{pages}[10];
107         delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
108         $change->{pages} = [
109                 map {
110                         if (length $config{cgiurl}) {
111                                 $_->{link} = "<a href=\"".
112                                         IkiWiki::cgiurl(
113                                                 do => "recentchanges_link",
114                                                 page => $_->{page}
115                                         ).
116                                         "\">".
117                                         IkiWiki::pagetitle($_->{page}).
118                                         "</a>"
119                         }
120                         else {
121                                 $_->{link} = IkiWiki::pagetitle($_->{page});
122                         }
123                         $_->{baseurl}="$config{url}/" if length $config{url};
124
125                         $_;
126                 } @{$change->{pages}}
127         ];
128         push @{$change->{pages}}, { link => '...' } if $is_excess;
129
130         # See if the committer is an openid.
131         $change->{author}=$change->{user};
132         my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
133         if (defined $oiduser) {
134                 $change->{authorurl}=$change->{user};
135                 $change->{user}=$oiduser;
136         }
137         elsif (length $config{cgiurl}) {
138                 $change->{authorurl} = IkiWiki::cgiurl(
139                         do => "recentchanges_link",
140                         page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
141                 );
142         }
143
144         # escape wikilinks and preprocessor stuff in commit messages
145         if (ref $change->{message}) {
146                 foreach my $field (@{$change->{message}}) {
147                         if (exists $field->{line}) {
148                                 $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
149                         }
150                 }
151         }
152
153         # Fill out a template with the change info.
154         my $template=template("change.tmpl", blind_cache => 1);
155         $template->param(
156                 %$change,
157                 commitdate => displaytime($change->{when}, "%X %x"),
158                 wikiname => $config{wikiname},
159         );
160         IkiWiki::run_hooks(pagetemplate => sub {
161                 shift->(page => $page, destpage => $page,
162                         template => $template, rev => $change->{rev});
163         });
164
165         my $file=$page."._change";
166         writefile($file, $config{srcdir}, $template->output);
167         utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
168
169         return $page;
170 } #}}}
171
172 1