]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/recentchanges.pm
Forgotten notice about /ostatnie_zmiany.html page
[git.ikiwiki.info.git] / IkiWiki / Plugin / recentchanges.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::recentchanges;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use Encode;
8 use HTML::Entities;
10 sub import {
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         # Load goto to fix up links from recentchanges
17         IkiWiki::loadplugin("goto");
18 }
20 sub getsetup () {
21         return
22                 plugin => {
23                         safe => 1,
24                         rebuild => 1,
25                 },
26                 recentchangespage => {
27                         type => "string",
28                         example => "recentchanges",
29                         description => "name of the recentchanges page",
30                         safe => 1,
31                         rebuild => 1,
32                 },
33                 recentchangesnum => {
34                         type => "integer",
35                         example => 100,
36                         description => "number of changes to track",
37                         safe => 1,
38                         rebuild => 0,
39                 },
40 }
42 sub checkconfig () {
43         $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
44         $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
45 }
47 sub refresh ($) {
48         my %seen;
50         # add new changes
51         foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
52                 $seen{store($change, $config{recentchangespage})}=1;
53         }
54         
55         # delete old and excess changes
56         foreach my $page (keys %pagesources) {
57                 if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
58                         unlink($config{srcdir}.'/'.$pagesources{$page});
59                 }
60         }
61 }
63 # Enable the recentchanges link.
64 sub pagetemplate (@) {
65         my %params=@_;
66         my $template=$params{template};
67         my $page=$params{page};
69         if (defined $config{recentchangespage} && $config{rcs} &&
70             $template->query(name => "recentchangesurl") &&
71             $page ne $config{recentchangespage}) {
72                 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
73                 $template->param(have_actions => 1);
74         }
75 }
77 # Pages with extension _change have plain html markup, pass through.
78 sub htmlize (@) {
79         my %params=@_;
80         return $params{content};
81 }
83 sub store ($$$) {
84         my $change=shift;
86         my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
88         # Optimisation to avoid re-writing pages. Assumes commits never
89         # change (or that any changes are not important).
90         return $page if exists $pagesources{$page} && ! $config{rebuild};
92         # Limit pages to first 10, and add links to the changed pages.
93         my $is_excess = exists $change->{pages}[10];
94         delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
95         $change->{pages} = [
96                 map {
97                         if (length $config{cgiurl}) {
98                                 $_->{link} = "<a href=\"".
99                                         IkiWiki::cgiurl(
100                                                 do => "goto",
101                                                 page => $_->{page}
102                                         ).
103                                         "\" rel=\"nofollow\">".
104                                         pagetitle($_->{page}).
105                                         "</a>"
106                         }
107                         else {
108                                 $_->{link} = pagetitle($_->{page});
109                         }
110                         $_->{baseurl}="$config{url}/" if length $config{url};
112                         $_;
113                 } @{$change->{pages}}
114         ];
115         push @{$change->{pages}}, { link => '...' } if $is_excess;
117         $change->{author}=$change->{user};
118         my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
119         if (defined $oiduser) {
120                 $change->{authorurl}=$change->{user};
121                 $change->{user}=defined $change->{nickname} ? $change->{nickname} : $oiduser;
122         }
123         elsif (length $config{cgiurl}) {
124                 $change->{authorurl} = IkiWiki::cgiurl(
125                         do => "goto",
126                         page => IkiWiki::userpage($change->{author}),
127                 );
128         }
130         if (ref $change->{message}) {
131                 foreach my $field (@{$change->{message}}) {
132                         if (exists $field->{line}) {
133                                 # escape html
134                                 $field->{line} = encode_entities($field->{line});
135                                 # escape links and preprocessor stuff
136                                 $field->{line} = encode_entities($field->{line}, '\[\]');
137                         }
138                 }
139         }
141         # Fill out a template with the change info.
142         my $template=template("change.tmpl", blind_cache => 1);
143         $template->param(
144                 %$change,
145                 commitdate => displaytime($change->{when}, "%X %x"),
146                 wikiname => $config{wikiname},
147         );
148         
149         $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
150                 if exists $config{url};
151         
152         IkiWiki::run_hooks(pagetemplate => sub {
153                 shift->(page => $page, destpage => $page,
154                         template => $template, rev => $change->{rev});
155         });
157         my $file=$page."._change";
158         writefile($file, $config{srcdir}, $template->output);
159         utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
161         return $page;