pagespec_match_list added and used in most appropriate places
[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=pagespec_match_list(
54                 [ grep { $_ ne $params{page} } keys %pagesources],
55                 $params{pages}, location => $params{page});
56         
57         @list = sort { $params{timehash}->{$b} <=> $params{timehash}->{$a} } @list;
58
59         my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
60         if ($@) {
61                 error $@;
62         }
63
64         if (! @data) {
65                 # generate an empty graph
66                 push @data, 0 foreach 1..($params{max} / 2);
67         }
68
69         my $color=exists $params{color} ? "($params{color})" : "";
70
71         delete $params{pages};
72         delete $params{formula};
73         delete $params{ftime};
74         delete $params{color};
75         return IkiWiki::Plugin::sparkline::preprocess(%params, 
76                 map { $_.$color => "" } reverse @data);
77 }
78
79 sub perfoo ($@) {
80         my $sub=shift;
81         my $params=shift;
82         
83         my $max=$params->{max};
84         my ($first, $prev, $cur);
85         my $count=0;
86         my @data;
87         foreach (@_) {
88                 $cur=$sub->($params->{timehash}->{$_});
89                 if (defined $prev) {
90                         if ($prev != $cur) {
91                                 push @data, "$prev,$count";
92                                 $count=0;
93                                 last if --$max <= 0;
94
95                                 for ($cur+1 .. $prev-1) {
96                                         push @data, "$_,0";
97                                         last if --$max == 0;
98                                 }
99                         }
100                 }
101                 else {
102                         $first=$cur;
103                 }
104                 $count++;
105                 $prev=$cur;
106         }
107
108         return @data;
109 }
110
111 package IkiWiki::Plugin::postsparkline::formula;
112
113 sub peryear (@) {
114         return IkiWiki::Plugin::postsparkline::perfoo(sub {
115                 return (localtime $_[0])[5];
116         }, @_);
117 }
118
119 sub permonth (@) {
120         return IkiWiki::Plugin::postsparkline::perfoo(sub {
121                 my ($month, $year)=(localtime $_[0])[4,5];
122                 return $year*12+$month;
123         }, @_);
124 }
125
126 sub perday (@) {
127         return IkiWiki::Plugin::postsparkline::perfoo(sub {
128                 my ($year, $yday)=(localtime $_[0])[5,7];
129                 return $year*365+$yday;
130         }, @_);
131 }
132
133 sub interval ($@) {
134         my $params=shift;
135
136         my $max=$params->{max};
137         my @data;
138         for (my $i=1; $i < @_; $i++) {
139                 push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};
140                 last if --$max <= 0;
141         }
142         return @data;
143 }
144
145 1