]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/pagestats.pm
Merge remote-tracking branch 'smcv/ready/limit'
[git.ikiwiki.info.git] / IkiWiki / Plugin / pagestats.pm
1 #!/usr/bin/perl
2 #
3 # Produce page statistics in various forms.
4 #
5 # Currently supported:
6 #   cloud: produces statistics in the form of a del.icio.us-style tag cloud
7 #          (default)
8 #   table: produces a table with the number of backlinks for each page
9 #
10 # by Enrico Zini
11 package IkiWiki::Plugin::pagestats;
13 use warnings;
14 use strict;
15 use IkiWiki 3.00;
17 # Names of the HTML classes to use for the tag cloud
18 our @classes = ('smallestPC', 'smallPC', 'normalPC', 'bigPC', 'biggestPC' );
20 sub import {
21         hook(type => "getsetup", id => "pagestats", call => \&getsetup);
22         hook(type => "preprocess", id => "pagestats", call => \&preprocess);
23 }
25 sub getsetup () {
26         return 
27                 plugin => {
28                         safe => 1,
29                         rebuild => undef,
30                         section => "widget",
31                 },
32 }
34 sub preprocess (@) {
35         my %params=@_;
36         $params{pages}="*" unless defined $params{pages};
37         my $style = ($params{style} or 'cloud');
39         # Backwards compatibility
40         if (defined $params{show} && $params{show} =~ m/^\d+$/) {
41                 $params{limit} = $params{show};
42                 delete $params{show};
43         }
45         my %counts;
46         my $max = 0;
47         foreach my $page (pagespec_match_list($params{page}, $params{pages},
48                                   # update when a displayed page is added/removed
49                                   deptype => deptype("presence"))) {
50                 use IkiWiki::Render;
52                 my @backlinks = IkiWiki::backlink_pages($page);
54                 if (exists $params{among}) {
55                         # only consider backlinks from the amoung pages
56                         @backlinks = pagespec_match_list(
57                                 $params{page}, $params{among},
58                                 # update whenever links on those pages change
59                                 deptype => deptype("links"),
60                                 list => \@backlinks
61                         );
62                 }
63                 else {
64                         # update when any page with links changes,
65                         # in case the links point to our displayed pages
66                         add_depends($params{page}, "*", deptype("links"));
67                 }
69                 $counts{$page} = scalar(@backlinks);
70                 $max = $counts{$page} if $counts{$page} > $max;
71         }
73         if (exists $params{limit}) {
74                 my $i=0;
75                 my %show;
76                 foreach my $key (sort { $counts{$b} <=> $counts{$a} } keys %counts) {
77                         last if ++$i > $params{limit};
78                         $show{$key}=$counts{$key};
79                 }
80                 %counts=%show;
81         }
83         if ($style eq 'table') {
84                 return "<table class='".(exists $params{class} ? $params{class} : "pageStats")."'>\n".
85                         join("\n", map {
86                                 "<tr><td>".
87                                 htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
88                                 "</td><td>".$counts{$_}."</td></tr>"
89                         }
90                         sort { $counts{$b} <=> $counts{$a} } keys %counts).
91                         "\n</table>\n" ;
92         }
93         else {
94                 # In case of misspelling, default to a page cloud
96                 my $res;
97                 if ($style eq 'list') {
98                         $res = "<ul class='".(exists $params{class} ? $params{class} : "list")."'>\n";
99                 }
100                 else {
101                         $res = "<div class='".(exists $params{class} ? $params{class} : "pagecloud")."'>\n";
102                 }
103                 foreach my $page (sort keys %counts) {
104                         next unless $counts{$page} > 0;
106                         my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
107                         
108                         $res.="<li>" if $style eq 'list';
109                         $res .= "<span class=\"$class\">".
110                                 htmllink($params{page}, $params{destpage}, $page).
111                                 "</span>\n";
112                         $res.="</li>" if $style eq 'list';
114                 }
115                 if ($style eq 'list') {
116                         $res .= "</ul>\n";
117                 }
118                 else {
119                         $res .= "</div>\n";
120                 }
122                 return $res;
123         }