]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/postsparkline.pm
Move about: fix to version 2.31.3 in the changelog
[git.ikiwiki.info.git] / IkiWiki / Plugin / postsparkline.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::postsparkline;
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
8 sub import { #{{{
9         IkiWiki::loadplugin('sparkline');
10         hook(type => "preprocess", id => "postsparkline", call => \&preprocess);
11 } # }}}
13 sub preprocess (@) { #{{{
14         my %params=@_;
16         if (! exists $params{max}) {
17                 $params{max}=100;
18         }
20         if (! exists $params{pages}) {
21                 return "";
22         }
24         if (! exists $params{time} || $params{time} ne 'mtime') {
25                 $params{timehash} = \%IkiWiki::pagectime;
26         }
27         else {
28                 $params{timehash} = \%IkiWiki::pagemtime;
29         }
31         if (! exists $params{formula}) {
32                 return "[[postsparkline ".gettext("missing formula")."]]";
33         }
34         my $formula=$params{formula};
35         $formula=~s/[^a-zA-Z0-9]*//g;
36         $formula=IkiWiki::possibly_foolish_untaint($formula);
37         if (! length $formula ||
38             ! IkiWiki::Plugin::postsparkline::formula->can($formula)) {
39                 return "[[postsparkline ".gettext("unknown formula")."]]";
40         }
42         add_depends($params{page}, $params{pages});
44         my @list;
45         foreach my $page (keys %pagesources) {
46                 next if $page eq $params{page};
47                 if (pagespec_match($page, $params{pages}, location => $params{page})) {
48                         push @list, $page;
49                 }
50         }
51         
52         @list = sort { $params{timehash}->{$b} <=> $params{timehash}->{$a} } @list;
54         my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
55         if ($@) {
56                 return "[[postsparkline error $@]]";
57         }
59         if (! @data) {
60                 # generate an empty graph
61                 push @data, 0 foreach 1..($params{max} / 2);
62         }
64         my $color=exists $params{color} ? "($params{color})" : "";
66         delete $params{pages};
67         delete $params{formula};
68         delete $params{ftime};
69         delete $params{color};
70         return IkiWiki::Plugin::sparkline::preprocess(%params, 
71                 map { $_.$color => "" } reverse @data);
72 } # }}}
74 sub perfoo ($@) {
75         my $sub=shift;
76         my $params=shift;
77         
78         my $max=$params->{max};
79         my ($first, $prev, $cur);
80         my $count=0;
81         my @data;
82         foreach (@_) {
83                 $cur=$sub->($params->{timehash}->{$_});
84                 if (defined $prev) {
85                         if ($prev != $cur) {
86                                 push @data, "$prev,$count";
87                                 $count=0;
88                                 last if --$max <= 0;
90                                 for ($cur+1 .. $prev-1) {
91                                         push @data, "$_,0";
92                                         last if --$max == 0;
93                                 }
94                         }
95                 }
96                 else {
97                         $first=$cur;
98                 }
99                 $count++;
100                 $prev=$cur;
101         }
103         return @data;
106 package IkiWiki::Plugin::postsparkline::formula;
108 sub peryear (@) {
109         return IkiWiki::Plugin::postsparkline::perfoo(sub {
110                 return (localtime $_[0])[5];
111         }, @_);
114 sub permonth (@) {
115         return IkiWiki::Plugin::postsparkline::perfoo(sub {
116                 my ($month, $year)=(localtime $_[0])[4,5];
117                 return $year*12+$month;
118         }, @_);
121 sub perday (@) {
122         return IkiWiki::Plugin::postsparkline::perfoo(sub {
123                 my ($year, $yday)=(localtime $_[0])[5,7];
124                 return $year*365+$yday;
125         }, @_);
128 sub interval ($@) {
129         my $params=shift;
131         my $max=$params->{max};
132         my @data;
133         for (my $i=1; $i < @_; $i++) {
134                 push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};
135                 last if --$max <= 0;
136         }
137         return @data;