]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/pagestats.pm
Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[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');
38         
39         my %counts;
40         my $max = 0;
41         foreach my $page (pagespec_match_list($params{page}, $params{pages},
42                                   # update when a displayed page is added/removed
43                                   deptype => deptype("presence"))) {
44                 use IkiWiki::Render;
46                 my @backlinks = IkiWiki::backlink_pages($page);
48                 if (exists $params{among}) {
49                         # only consider backlinks from the amoung pages
50                         @backlinks = pagespec_match_list(
51                                 $params{page}, $params{among},
52                                 # update whenever links on those pages change
53                                 deptype => deptype("links"),
54                                 list => \@backlinks
55                         );
56                 }
57                 else {
58                         # update when any page with links changes,
59                         # in case the links point to our displayed pages
60                         add_depends($params{page}, "*", deptype("links"));
61                 }
63                 $counts{$page} = scalar(@backlinks);
64                 $max = $counts{$page} if $counts{$page} > $max;
65         }
67         if (exists $params{show}) {
68                 my $i=0;
69                 my %show;
70                 foreach my $key (sort { $counts{$b} <=> $counts{$a} } keys %counts) {
71                         last if ++$i > $params{show};
72                         $show{$key}=$counts{$key};
73                 }
74                 %counts=%show;
75         }
77         if ($style eq 'table') {
78                 return "<table class='".(exists $params{class} ? $params{class} : "pageStats")."'>\n".
79                         join("\n", map {
80                                 "<tr><td>".
81                                 htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
82                                 "</td><td>".$counts{$_}."</td></tr>"
83                         }
84                         sort { $counts{$b} <=> $counts{$a} } keys %counts).
85                         "\n</table>\n" ;
86         }
87         else {
88                 # In case of misspelling, default to a page cloud
90                 my $res;
91                 if ($style eq 'list') {
92                         $res = "<ul class='".(exists $params{class} ? $params{class} : "list")."'>\n";
93                 }
94                 else {
95                         $res = "<div class='".(exists $params{class} ? $params{class} : "pagecloud")."'>\n";
96                 }
97                 foreach my $page (sort keys %counts) {
98                         next unless $counts{$page} > 0;
100                         my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
101                         
102                         $res.="<li>" if $style eq 'list';
103                         $res .= "<span class=\"$class\">".
104                                 htmllink($params{page}, $params{destpage}, $page).
105                                 "</span>\n";
106                         $res.="</li>" if $style eq 'list';
108                 }
109                 if ($style eq 'list') {
110                         $res .= "</ul>\n";
111                 }
112                 else {
113                         $res .= "</div>\n";
114                 }
116                 return $res;
117         }