proof of concept implementation of static recentchanges
[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",
10                 call => \&checkconfig);
11         hook(type => "needsbuild", id => "recentchanges",
12                 call => \&needsbuild);
13         hook(type => "preprocess", id => "recentchanges",
14                 call => \&preprocess);
15         hook(type => "htmlize", id => "_change",
16                 call => \&htmlize);
17 } #}}}
18
19 sub checkconfig () { #{{{
20         updatechanges();
21 } #}}}
22
23 sub needsbuild () { #{{{
24         # TODO
25 } #}}}
26
27 sub preprocess (@) { #{{{
28         my %params=@_;
29
30         # TODO
31
32         return "";
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         my $subdir=shift;
44         
45         my $page="$subdir/change_".IkiWiki::titlepage($change->{rev});
46
47         # Optimisation to avoid re-writing pages. Assumes commits never
48         # change, or that any changes are not important.
49         return if exists $pagesources{$page} && ! $config{rebuild};
50
51         # Limit pages to first 10, and add links to the changed pages.
52         my $is_excess = exists $change->{pages}[10];
53         delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
54         $change->{pages} = [
55                 map {
56                         if (length $config{url}) {
57                                 $_->{link} = "<a href=\"$config{url}/".
58                                         urlto($_->{page},"")."\">".
59                                         IkiWiki::pagetitle($_->{page})."</a>";
60                         }
61                         else {
62                                 $_->{link} = IkiWiki::pagetitle($_->{page});
63                         }
64                         $_;
65                 } @{$change->{pages}}
66         ];
67         push @{$change->{pages}}, { link => '...' } if $is_excess;
68
69         # Fill out a template with the change info.
70         $change->{user} = IkiWiki::userlink($change->{user});
71         my $ctime=$change->{when};
72         $change->{when} = IkiWiki::displaytime($change->{when}, "%X %x");
73         my $template=template("change.tmpl", blind_cache => 1);
74         $template->param(%$change);
75         $template->param(baseurl => "$config{url}/") if length $config{url};
76         IkiWiki::run_hooks(pagetemplate => sub {
77                 shift->(page => $page, destpage => $page, template => $template);
78         });
79
80         writefile($page."._change", $config{srcdir}, $template->output);
81         utime $ctime, $ctime, "$config{srcdir}/$page._change";
82 } #}}}
83
84 sub updatechanges () { #{{{
85         my @changelog=IkiWiki::rcs_recentchanges(100);
86         foreach my $change (@changelog) {
87                 store($change, "recentchanges");
88         }
89         # TODO: delete old
90 } #}}}
91
92 1