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);
36 $params{pages}="*" unless defined $params{pages};
37 my $style = ($params{style} or 'cloud');
41 foreach my $page (pagespec_match_list($params{page}, $params{pages},
42 # update when a displayed page is added/removed
43 deptype => deptype("presence"))) {
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"),
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"));
63 $counts{$page} = scalar(@backlinks);
64 $max = $counts{$page} if $counts{$page} > $max;
67 if (exists $params{show}) {
70 foreach my $key (sort { $counts{$b} <=> $counts{$a} } keys %counts) {
71 last if ++$i > $params{show};
72 $show{$key}=$counts{$key};
77 if ($style eq 'table') {
78 return "<table class='".(exists $params{class} ? $params{class} : "pageStats")."'>\n".
81 htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
82 "</td><td>".$counts{$_}."</td></tr>"
84 sort { $counts{$b} <=> $counts{$a} } keys %counts).
88 # In case of misspelling, default to a page cloud
91 if ($style eq 'list') {
92 $res = "<ul class='".(exists $params{class} ? $params{class} : "list")."'>\n";
95 $res = "<div class='".(exists $params{class} ? $params{class} : "pagecloud")."'>\n";
97 foreach my $page (sort keys %counts) {
98 next unless $counts{$page} > 0;
100 my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
102 $res.="<li>" if $style eq 'list';
103 $res .= "<span class=\"$class\">".
104 htmllink($params{page}, $params{destpage}, $page).
106 $res.="</li>" if $style eq 'list';
109 if ($style eq 'list') {