3 # Produce page statistics in various forms.
6 # cloud: produces statistics in the form of a del.icio.us-style tag cloud
8 # table: produces a table with the number of backlinks for each page
11 package IkiWiki::Plugin::pagestats;
17 # Names of the HTML classes to use for the tag cloud
18 our @classes = ('smallestPC', 'smallPC', 'normalPC', 'bigPC', 'biggestPC' );
21 hook(type => "getsetup", id => "pagestats", call => \&getsetup);
22 hook(type => "preprocess", id => "pagestats", call => \&preprocess);
35 $params{pages}="*" unless defined $params{pages};
36 my $style = ($params{style} or 'cloud');
38 # Needs to update whenever a page is added or removed.
39 add_depends($params{page}, $params{pages}, exists => 1);
40 # Also needs to update when any page with links changes,
41 # in case the links point to our displayed pages.
42 # (Among limits this further.)
43 add_depends($params{page}, exists $params{among} ? $params{among} : "*",
48 foreach my $page (pagespec_match_list([keys %pagesources],
49 $params{pages}, location => $params{page})) {
52 my @backlinks = IkiWiki::backlink_pages($page);
54 if (exists $params{among}) {
55 @backlinks = pagespec_match_list(\@backlinks,
56 $params{among}, location => $params{page});
59 $counts{$page} = scalar(@backlinks);
60 $max = $counts{$page} if $counts{$page} > $max;
63 if ($style eq 'table') {
64 return "<table class='pageStats'>\n".
67 htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
68 "</td><td>".$counts{$_}."</td></tr>"
70 sort { $counts{$b} <=> $counts{$a} } keys %counts).
74 # In case of misspelling, default to a page cloud
76 my $res = "<div class='pagecloud'>\n";
77 foreach my $page (sort keys %counts) {
78 next unless $counts{$page} > 0;
80 my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
81 $res .= "<span class=\"$class\">".
82 htmllink($params{page}, $params{destpage}, $page).