make the author metadata for changes pages be the un-munged openid
[ikiwiki] / 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 => "htmlize", id => "_change", call => \&htmlize);
12 } #}}}
13
14 sub checkconfig () { #{{{
15         $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
16         $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
17 } #}}}
18
19 sub refresh ($) { #{{{
20         my %seen;
21
22         # add new changes
23         foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
24                 $seen{store($change, $config{recentchangespage})}=1;
25         }
26         
27         # delete old and excess changes
28         foreach my $page (keys %pagesources) {
29                 if ($page=~/^\Q$config{recentchangespage}\E\/change_/ && ! $seen{$page}) {
30                         unlink($config{srcdir}.'/'.$pagesources{$page});
31                 }
32         }
33 } #}}}
34
35 # Pages with extension _change have plain html markup, pass through.
36 sub htmlize (@) { #{{{
37         my %params=@_;
38         return $params{content};
39 } #}}}
40
41 sub store ($$$) { #{{{
42         my $change=shift;
43
44         my $page="$config{recentchangespage}/change_".IkiWiki::titlepage($change->{rev});
45
46         # Optimisation to avoid re-writing pages. Assumes commits never
47         # change (or that any changes are not important).
48         return $page if exists $pagesources{$page} && ! $config{rebuild};
49
50         # Limit pages to first 10, and add links to the changed pages.
51         my $is_excess = exists $change->{pages}[10];
52         delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
53         $change->{pages} = [
54                 map {
55                         if (length $config{url}) {
56                                 $_->{link} = "<a href=\"$config{url}/".
57                                         urlto($_->{page},"")."\">".
58                                         IkiWiki::pagetitle($_->{page})."</a>";
59                         }
60                         else {
61                                 $_->{link} = IkiWiki::pagetitle($_->{page});
62                         }
63                         $_;
64                 } @{$change->{pages}}
65         ];
66         push @{$change->{pages}}, { link => '...' } if $is_excess;
67
68         # See if the committer is an openid.
69         $change->{author}=$change->{user};
70         my $oiduser=IkiWiki::openiduser($change->{user});
71         if (defined $oiduser) {
72                 $change->{authorurl}=$change->{user};
73                 $change->{user}=$oiduser;
74         }
75         elsif (length $config{url}) {
76                 $change->{authorurl}="$config{url}/".
77                         (length $config{userdir} ? "$config{userdir}/" : "").
78                         $change->{user};
79         }
80
81         # escape  wikilinks and preprocessor stuff in commit messages
82         if (ref $change->{message}) {
83                 foreach my $field (@{$change->{message}}) {
84                         if (exists $field->{line}) {
85                                 $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
86                         }
87                 }
88         }
89
90         # Fill out a template with the change info.
91         my $template=template("change.tmpl", blind_cache => 1);
92         $template->param(
93                 %$change,
94                 commitdate => displaytime($change->{when}, "%X %x"),
95                 wikiname => $config{wikiname},
96         );
97         $template->param(baseurl => "$config{url}/") if length $config{url};
98         IkiWiki::run_hooks(pagetemplate => sub {
99                 shift->(page => $page, destpage => $page, template => $template);
100         });
101
102         my $file=$page."._change";
103         writefile($file, $config{srcdir}, $template->output);
104         utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
105
106         return $page;
107 } #}}}
108
109 sub updatechanges ($$) { #{{{
110         my $subdir=shift;
111         my @changes=@{shift()};
112         
113 } #}}}
114
115 1