]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/pagestats.pm
fix data:image handling
[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 2.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 => "preprocess", id => "pagestats", call => \&preprocess);
22 } # }}}
23
24 sub preprocess (@) { #{{{
25         my %params=@_;
26         $params{pages}="*" unless defined $params{pages};
27         my $style = ($params{style} or 'cloud');
28         
29         # Needs to update whenever a page is added or removed, so
30         # register a dependency.
31         add_depends($params{page}, $params{pages});
32         
33         my %counts;
34         my $max = 0;
35         foreach my $page (keys %links) {
36                 if (pagespec_match($page, $params{pages}, location => $params{page})) {
37                         use IkiWiki::Render;
38                         $counts{$page} = scalar(IkiWiki::backlinks($page));
39                         $max = $counts{$page} if $counts{$page} > $max;
40                 }
41         }
42
43         if ($style eq 'table') {
44                 return "<table class='pageStats'>\n".
45                         join("\n", map {
46                                 "<tr><td>".
47                                 htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
48                                 "</td><td>".$counts{$_}."</td></tr>"
49                         }
50                         sort { $counts{$b} <=> $counts{$a} } keys %counts).
51                         "\n</table>\n" ;
52         }
53         else {
54                 # In case of misspelling, default to a page cloud
55
56                 my $res = "<div class='pagecloud'>\n";
57                 foreach my $page (sort keys %counts) {
58                         my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
59                         $res .= "<span class=\"$class\">".
60                                 htmllink($params{page}, $params{destpage}, $page).
61                                 "</span>\n";
62                 }
63                 $res .= "</div>\n";
64
65                 return $res;
66         }
67 } # }}}
68
69 1