2006-07-29 19:44:41 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# Produce page statistics in various forms.
|
|
|
|
#
|
|
|
|
# Currently supported:
|
|
|
|
# cloud: produces statistics in the form of a del.icio.us-style tag cloud
|
|
|
|
# (default)
|
|
|
|
# table: produces a table with the number of backlinks for each page
|
|
|
|
#
|
2006-08-19 07:05:02 +02:00
|
|
|
# by Enrico Zini
|
2006-07-29 19:44:41 +02:00
|
|
|
package IkiWiki::Plugin::pagestats;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2006-07-29 19:44:41 +02:00
|
|
|
|
|
|
|
# Names of the HTML classes to use for the tag cloud
|
|
|
|
our @classes = ('smallestPC', 'smallPC', 'normalPC', 'bigPC', 'biggestPC' );
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-08-03 23:20:21 +02:00
|
|
|
hook(type => "getsetup", id => "pagestats", call => \&getsetup);
|
2006-09-10 00:50:27 +02:00
|
|
|
hook(type => "preprocess", id => "pagestats", call => \&preprocess);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-07-29 19:44:41 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-08-03 23:20:21 +02:00
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => undef,
|
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-08-03 23:20:21 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub preprocess (@) {
|
2006-07-29 19:44:41 +02:00
|
|
|
my %params=@_;
|
|
|
|
$params{pages}="*" unless defined $params{pages};
|
|
|
|
my $style = ($params{style} or 'cloud');
|
|
|
|
|
|
|
|
# Needs to update whenever a page is added or removed, so
|
|
|
|
# register a dependency.
|
2006-09-10 00:50:27 +02:00
|
|
|
add_depends($params{page}, $params{pages});
|
2009-06-18 19:33:49 +02:00
|
|
|
add_depends($params{page}, $params{among}) if exists $params{among};
|
2006-07-29 19:44:41 +02:00
|
|
|
|
|
|
|
my %counts;
|
|
|
|
my $max = 0;
|
2009-04-23 21:45:30 +02:00
|
|
|
foreach my $page (pagespec_match_list([keys %links],
|
|
|
|
$params{pages}, location => $params{page})) {
|
|
|
|
use IkiWiki::Render;
|
2009-06-18 19:33:49 +02:00
|
|
|
|
|
|
|
my @backlinks = IkiWiki::backlink_pages($page);
|
|
|
|
|
|
|
|
if (exists $params{among}) {
|
|
|
|
@backlinks = pagespec_match_list(\@backlinks,
|
|
|
|
$params{among}, location => $params{page});
|
|
|
|
}
|
|
|
|
|
|
|
|
$counts{$page} = scalar(@backlinks);
|
2009-04-23 21:45:30 +02:00
|
|
|
$max = $counts{$page} if $counts{$page} > $max;
|
2006-07-29 19:44:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($style eq 'table') {
|
2006-07-29 22:14:07 +02:00
|
|
|
return "<table class='pageStats'>\n".
|
|
|
|
join("\n", map {
|
|
|
|
"<tr><td>".
|
2007-02-20 04:05:47 +01:00
|
|
|
htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
|
2006-07-29 22:14:07 +02:00
|
|
|
"</td><td>".$counts{$_}."</td></tr>"
|
|
|
|
}
|
|
|
|
sort { $counts{$b} <=> $counts{$a} } keys %counts).
|
|
|
|
"\n</table>\n" ;
|
2007-05-12 21:28:03 +02:00
|
|
|
}
|
|
|
|
else {
|
2006-07-29 19:44:41 +02:00
|
|
|
# In case of misspelling, default to a page cloud
|
|
|
|
|
|
|
|
my $res = "<div class='pagecloud'>\n";
|
|
|
|
foreach my $page (sort keys %counts) {
|
2009-06-18 19:25:46 +02:00
|
|
|
next unless $counts{$page} > 0;
|
|
|
|
|
2006-07-29 19:44:41 +02:00
|
|
|
my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
|
2006-07-29 22:07:48 +02:00
|
|
|
$res .= "<span class=\"$class\">".
|
2006-09-10 00:50:27 +02:00
|
|
|
htmllink($params{page}, $params{destpage}, $page).
|
2006-07-29 22:07:48 +02:00
|
|
|
"</span>\n";
|
2006-07-29 19:44:41 +02:00
|
|
|
}
|
|
|
|
$res .= "</div>\n";
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-07-29 19:44:41 +02:00
|
|
|
|
|
|
|
1
|