]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/postsparkline.pm
signinedit: Auto-disable the plugin when all authentication methods are disabled.
[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 3.00;
8 sub import {
9         IkiWiki::loadplugin('sparkline');
10         hook(type => "getsetup", id => "postsparkline", call => \&getsetup);
11         hook(type => "preprocess", id => "postsparkline", call => \&preprocess);
12 }
14 sub getsetup () {
15         return 
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                 },
20 }
22 sub preprocess (@) {
23         my %params=@_;
25         if (! exists $params{max}) {
26                 $params{max}=100;
27         }
29         if (! exists $params{pages}) {
30                 return "";
31         }
33         my $deptype;
34         if (! exists $params{time} || $params{time} ne 'mtime') {
35                 $params{timehash} = \%IkiWiki::pagectime;
36                 # need to update when pages are added or removed
37                 $deptype = deptype("presence");
38         }
39         else {
40                 $params{timehash} = \%IkiWiki::pagemtime;
41                 # need to update when pages are changed
42                 $deptype = deptype("content");
43         }
45         if (! exists $params{formula}) {
46                 error gettext("missing formula")
47         }
48         my $formula=$params{formula};
49         $formula=~s/[^a-zA-Z0-9]*//g;
50         $formula=IkiWiki::possibly_foolish_untaint($formula);
51         if (! length $formula ||
52             ! IkiWiki::Plugin::postsparkline::formula->can($formula)) {
53                 error gettext("unknown formula");
54         }
56         my @list=sort { $params{timehash}->{$b} <=> $params{timehash}->{$a} } 
57                 pagespec_match_list($params{page}, $params{pages},
58                         deptype => $deptype,
59                         filter => sub { $_[0] eq $params{page} },
60                 );
62         my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
63         if ($@) {
64                 error $@;
65         }
67         if (! @data) {
68                 # generate an empty graph
69                 push @data, 0 foreach 1..($params{max} / 2);
70         }
72         my $color=exists $params{color} ? "($params{color})" : "";
74         delete $params{pages};
75         delete $params{formula};
76         delete $params{ftime};
77         delete $params{color};
78         return IkiWiki::Plugin::sparkline::preprocess(%params, 
79                 map { $_.$color => "" } reverse @data);
80 }
82 sub perfoo ($@) {
83         my $sub=shift;
84         my $params=shift;
85         
86         my $max=$params->{max};
87         my ($first, $prev, $cur);
88         my $count=0;
89         my @data;
90         foreach (@_) {
91                 $cur=$sub->($params->{timehash}->{$_});
92                 if (defined $prev) {
93                         if ($prev != $cur) {
94                                 push @data, "$prev,$count";
95                                 $count=0;
96                                 last if --$max <= 0;
98                                 for ($cur+1 .. $prev-1) {
99                                         push @data, "$_,0";
100                                         last if --$max == 0;
101                                 }
102                         }
103                 }
104                 else {
105                         $first=$cur;
106                 }
107                 $count++;
108                 $prev=$cur;
109         }
111         return @data;
114 package IkiWiki::Plugin::postsparkline::formula;
116 sub peryear (@) {
117         return IkiWiki::Plugin::postsparkline::perfoo(sub {
118                 return (localtime $_[0])[5];
119         }, @_);
122 sub permonth (@) {
123         return IkiWiki::Plugin::postsparkline::perfoo(sub {
124                 my ($month, $year)=(localtime $_[0])[4,5];
125                 return $year*12+$month;
126         }, @_);
129 sub perday (@) {
130         return IkiWiki::Plugin::postsparkline::perfoo(sub {
131                 my ($year, $yday)=(localtime $_[0])[5,7];
132                 return $year*365+$yday;
133         }, @_);
136 sub interval ($@) {
137         my $params=shift;
139         my $max=$params->{max};
140         my @data;
141         for (my $i=1; $i < @_; $i++) {
142                 push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};
143                 last if --$max <= 0;
144         }
145         return @data;