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 # Return the text of the link to a tag, depending on option linktext.
36 my ($page, %params) = @_;
37 if (exists $params{show} &&
38 exists $pagestate{$page} &&
39 exists $pagestate{$page}{meta}{$params{show}}) {
40 return $pagestate{$page}{meta}{$params{show}};
49 $params{pages}="*" unless defined $params{pages};
50 my $style = ($params{style} or 'cloud');
52 # Backwards compatibility
53 if (defined $params{show} && $params{show} =~ m/^\d+$/) {
54 $params{limit} = $params{show};
60 foreach my $page (pagespec_match_list($params{page}, $params{pages},
61 # update when a displayed page is added/removed
62 deptype => deptype("presence"))) {
65 my @backlinks = IkiWiki::backlink_pages($page);
67 if (exists $params{among}) {
68 # only consider backlinks from the amoung pages
69 @backlinks = pagespec_match_list(
70 $params{page}, $params{among},
71 # update whenever links on those pages change
72 deptype => deptype("links"),
77 # update when any page with links changes,
78 # in case the links point to our displayed pages
79 add_depends($params{page}, "*", deptype("links"));
82 $counts{$page} = scalar(@backlinks);
83 $max = $counts{$page} if $counts{$page} > $max;
86 if (exists $params{limit}) {
89 foreach my $key (sort { $counts{$b} <=> $counts{$a} } keys %counts) {
90 last if ++$i > $params{limit};
91 $show{$key}=$counts{$key};
96 if ($style eq 'table') {
97 return "<table class='".(exists $params{class} ? $params{class} : "pageStats")."'>\n".
100 htmllink($params{page}, $params{destpage}, $_, noimageinline => 1, linktext => linktext($_, %params)).
101 "</td><td>".$counts{$_}."</td></tr>"
103 sort { $counts{$b} <=> $counts{$a} } keys %counts).
107 # In case of misspelling, default to a page cloud
110 if ($style eq 'list') {
111 $res = "<ul class='".(exists $params{class} ? $params{class} : "list")."'>\n";
114 $res = "<div class='".(exists $params{class} ? $params{class} : "pagecloud")."'>\n";
116 foreach my $page (sort keys %counts) {
117 next unless $counts{$page} > 0;
119 my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
121 $res.="<li>" if $style eq 'list';
122 $res .= "<span class=\"$class\">".
123 htmllink($params{page}, $params{destpage}, $page, linktext => linktext($page, %params)).
125 $res.="</li>" if $style eq 'list';
128 if ($style eq 'list') {