]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/pagestats.pm
otl: Avoid globally filtering non-otl pages
[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         # Needs to update whenever a page is added or removed, so
39         # register a dependency.
40         add_depends($params{page}, $params{pages});
41         add_depends($params{page}, $params{among}) if exists $params{among};
42         
43         my %counts;
44         my $max = 0;
45         foreach my $page (pagespec_match_list([keys %links],
46                         $params{pages}, location => $params{page})) {
47                 use IkiWiki::Render;
48
49                 my @backlinks = IkiWiki::backlink_pages($page);
50
51                 if (exists $params{among}) {
52                         @backlinks = pagespec_match_list(\@backlinks,
53                                 $params{among}, location => $params{page});
54                 }
55
56                 $counts{$page} = scalar(@backlinks);
57                 $max = $counts{$page} if $counts{$page} > $max;
58         }
59
60         if ($style eq 'table') {
61                 return "<table class='pageStats'>\n".
62                         join("\n", map {
63                                 "<tr><td>".
64                                 htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
65                                 "</td><td>".$counts{$_}."</td></tr>"
66                         }
67                         sort { $counts{$b} <=> $counts{$a} } keys %counts).
68                         "\n</table>\n" ;
69         }
70         else {
71                 # In case of misspelling, default to a page cloud
72
73                 my $res = "<div class='pagecloud'>\n";
74                 foreach my $page (sort keys %counts) {
75                         next unless $counts{$page} > 0;
76
77                         my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
78                         $res .= "<span class=\"$class\">".
79                                 htmllink($params{page}, $params{destpage}, $page).
80                                 "</span>\n";
81                 }
82                 $res .= "</div>\n";
83
84                 return $res;
85         }
86 }
87
88 1