]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/pagestats.pm
linkmap: Add option to omit disconnected pages from the map.
[ikiwiki.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;
12
13 use warnings;
14 use strict;
15 use IkiWiki 3.00;
16
17 # Names of the HTML classes to use for the tag cloud
18 our @classes = ('smallestPC', 'smallPC', 'normalPC', 'bigPC', 'biggestPC' );
19
20 sub import {
21         hook(type => "getsetup", id => "pagestats", call => \&getsetup);
22         hook(type => "preprocess", id => "pagestats", call => \&preprocess);
23 }
24
25 sub getsetup () {
26         return 
27                 plugin => {
28                         safe => 1,
29                         rebuild => undef,
30                 },
31 }
32
33 sub preprocess (@) {
34         my %params=@_;
35         $params{pages}="*" unless defined $params{pages};
36         my $style = ($params{style} or 'cloud');
37         
38         my %counts;
39         my $max = 0;
40         foreach my $page (pagespec_match_list($params{page}, $params{pages},
41                                   # update when a displayed page is added/removed
42                                   deptype => deptype("presence"))) {
43                 use IkiWiki::Render;
44
45                 my @backlinks = IkiWiki::backlink_pages($page);
46
47                 if (exists $params{among}) {
48                         # only consider backlinks from the amoung pages
49                         @backlinks = pagespec_match_list(
50                                 $params{page}, $params{among},
51                                 # update whenever links on those pages change
52                                 deptype => deptype("links"),
53                                 list => \@backlinks
54                         );
55                 }
56                 else {
57                         # update when any page with links changes,
58                         # in case the links point to our displayed pages
59                         add_depends($params{page}, "*", deptype("links"));
60                 }
61
62                 $counts{$page} = scalar(@backlinks);
63                 $max = $counts{$page} if $counts{$page} > $max;
64         }
65
66         if (exists $params{show}) {
67                 my $i=0;
68                 my %show;
69                 foreach my $key (sort { $counts{$b} <=> $counts{$a} } keys %counts) {
70                         last if ++$i > $params{show};
71                         $show{$key}=$counts{$key};
72                 }
73                 %counts=%show;
74         }
75
76         if ($style eq 'table') {
77                 return "<table class='pageStats'>\n".
78                         join("\n", map {
79                                 "<tr><td>".
80                                 htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
81                                 "</td><td>".$counts{$_}."</td></tr>"
82                         }
83                         sort { $counts{$b} <=> $counts{$a} } keys %counts).
84                         "\n</table>\n" ;
85         }
86         else {
87                 # In case of misspelling, default to a page cloud
88
89                 my $res = "<div class='pagecloud'>\n";
90                 foreach my $page (sort keys %counts) {
91                         next unless $counts{$page} > 0;
92
93                         my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
94                         $res .= "<span class=\"$class\">".
95                                 htmllink($params{page}, $params{destpage}, $page).
96                                 "</span>\n";
97                 }
98                 $res .= "</div>\n";
99
100                 return $res;
101         }
102 }
103
104 1