Merge branch 'master' into next
[ikiwiki] / IkiWiki / Plugin / postsparkline.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::postsparkline;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7
8 sub import {
9         IkiWiki::loadplugin('sparkline');
10         hook(type => "getsetup", id => "postsparkline", call => \&getsetup);
11         hook(type => "preprocess", id => "postsparkline", call => \&preprocess);
12 }
13
14 sub getsetup () {
15         return 
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                 },
20 }
21
22 sub preprocess (@) {
23         my %params=@_;
24
25         if (! exists $params{max}) {
26                 $params{max}=100;
27         }
28
29         if (! exists $params{pages}) {
30                 return "";
31         }
32
33         if (! exists $params{time} || $params{time} ne 'mtime') {
34                 $params{timehash} = \%IkiWiki::pagectime;
35         }
36         else {
37                 $params{timehash} = \%IkiWiki::pagemtime;
38         }
39
40         if (! exists $params{formula}) {
41                 error gettext("missing formula")
42         }
43         my $formula=$params{formula};
44         $formula=~s/[^a-zA-Z0-9]*//g;
45         $formula=IkiWiki::possibly_foolish_untaint($formula);
46         if (! length $formula ||
47             ! IkiWiki::Plugin::postsparkline::formula->can($formula)) {
48                 error gettext("unknown formula");
49         }
50
51         add_depends($params{page}, $params{pages});
52
53         my @list;
54         foreach my $page (keys %pagesources) {
55                 next if $page eq $params{page};
56                 if (pagespec_match($page, $params{pages}, location => $params{page})) {
57                         push @list, $page;
58                 }
59         }
60         
61         @list = sort { $params{timehash}->{$b} <=> $params{timehash}->{$a} } @list;
62
63         my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
64         if ($@) {
65                 error $@;
66         }
67
68         if (! @data) {
69                 # generate an empty graph
70                 push @data, 0 foreach 1..($params{max} / 2);
71         }
72
73         my $color=exists $params{color} ? "($params{color})" : "";
74
75         delete $params{pages};
76         delete $params{formula};
77         delete $params{ftime};
78         delete $params{color};
79         return IkiWiki::Plugin::sparkline::preprocess(%params, 
80                 map { $_.$color => "" } reverse @data);
81 }
82
83 sub perfoo ($@) {
84         my $sub=shift;
85         my $params=shift;
86         
87         my $max=$params->{max};
88         my ($first, $prev, $cur);
89         my $count=0;
90         my @data;
91         foreach (@_) {
92                 $cur=$sub->($params->{timehash}->{$_});
93                 if (defined $prev) {
94                         if ($prev != $cur) {
95                                 push @data, "$prev,$count";
96                                 $count=0;
97                                 last if --$max <= 0;
98
99                                 for ($cur+1 .. $prev-1) {
100                                         push @data, "$_,0";
101                                         last if --$max == 0;
102                                 }
103                         }
104                 }
105                 else {
106                         $first=$cur;
107                 }
108                 $count++;
109                 $prev=$cur;
110         }
111
112         return @data;
113 }
114
115 package IkiWiki::Plugin::postsparkline::formula;
116
117 sub peryear (@) {
118         return IkiWiki::Plugin::postsparkline::perfoo(sub {
119                 return (localtime $_[0])[5];
120         }, @_);
121 }
122
123 sub permonth (@) {
124         return IkiWiki::Plugin::postsparkline::perfoo(sub {
125                 my ($month, $year)=(localtime $_[0])[4,5];
126                 return $year*12+$month;
127         }, @_);
128 }
129
130 sub perday (@) {
131         return IkiWiki::Plugin::postsparkline::perfoo(sub {
132                 my ($year, $yday)=(localtime $_[0])[5,7];
133                 return $year*365+$yday;
134         }, @_);
135 }
136
137 sub interval ($@) {
138         my $params=shift;
139
140         my $max=$params->{max};
141         my @data;
142         for (my $i=1; $i < @_; $i++) {
143                 push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};
144                 last if --$max <= 0;
145         }
146         return @data;
147 }
148
149 1