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');
|
|
|
|
|
|
|
|
my %counts;
|
|
|
|
my $max = 0;
|
2009-10-09 05:51:06 +02:00
|
|
|
foreach my $page (pagespec_match_list($params{page}, $params{pages},
|
|
|
|
# update when a displayed page is added/removed
|
|
|
|
deptype => deptype("presence"))) {
|
2009-04-23 21:45:30 +02:00
|
|
|
use IkiWiki::Render;
|
2009-06-18 19:33:49 +02:00
|
|
|
|
|
|
|
my @backlinks = IkiWiki::backlink_pages($page);
|
|
|
|
|
|
|
|
if (exists $params{among}) {
|
2009-10-09 01:27:25 +02:00
|
|
|
# only consider backlinks from the amoung pages
|
2009-10-09 05:51:06 +02:00
|
|
|
@backlinks = pagespec_match_list(
|
|
|
|
$params{page}, $params{among},
|
2009-10-09 01:27:25 +02:00
|
|
|
# update whenever links on those pages change
|
|
|
|
deptype => deptype("links"),
|
|
|
|
list => \@backlinks
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# update when any page with links changes,
|
|
|
|
# in case the links point to our displayed pages
|
|
|
|
add_depends($params{page}, "*", deptype("links"));
|
2009-06-18 19:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2009-12-25 20:31:51 +01:00
|
|
|
if (exists $params{show}) {
|
|
|
|
my $i=0;
|
|
|
|
my %show;
|
|
|
|
foreach my $key (sort { $counts{$b} <=> $counts{$a} } keys %counts) {
|
|
|
|
last if ++$i > $params{show};
|
|
|
|
$show{$key}=$counts{$key};
|
|
|
|
}
|
|
|
|
%counts=%show;
|
|
|
|
}
|
|
|
|
|
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
|