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,
|
2010-02-12 12:35:52 +01:00
|
|
|
section => "widget",
|
2008-08-03 23:20:21 +02:00
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-08-03 23:20:21 +02:00
|
|
|
|
2014-02-22 23:30:09 +01:00
|
|
|
sub linktext ($%) {
|
|
|
|
# Return the text of the link to a tag, depending on option linktext.
|
|
|
|
my ($page, %params) = @_;
|
2014-09-14 16:57:09 +02:00
|
|
|
if (exists $params{show} &&
|
2014-09-14 16:51:22 +02:00
|
|
|
exists $pagestate{$page} &&
|
2014-09-14 16:57:09 +02:00
|
|
|
exists $pagestate{$page}{meta}{$params{show}}) {
|
|
|
|
return $pagestate{$page}{meta}{$params{show}};
|
2014-09-14 16:51:22 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return undef;
|
|
|
|
}
|
2014-02-22 23:30:09 +01: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');
|
2014-09-14 16:15:27 +02:00
|
|
|
|
|
|
|
# Backwards compatibility
|
|
|
|
if (defined $params{show} && $params{show} =~ m/^\d+$/) {
|
|
|
|
$params{limit} = $params{show};
|
|
|
|
delete $params{show};
|
|
|
|
}
|
|
|
|
|
2006-07-29 19:44:41 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-09-14 16:15:27 +02:00
|
|
|
if (exists $params{limit}) {
|
2009-12-25 20:31:51 +01:00
|
|
|
my $i=0;
|
|
|
|
my %show;
|
|
|
|
foreach my $key (sort { $counts{$b} <=> $counts{$a} } keys %counts) {
|
2014-09-14 16:15:27 +02:00
|
|
|
last if ++$i > $params{limit};
|
2009-12-25 20:31:51 +01:00
|
|
|
$show{$key}=$counts{$key};
|
|
|
|
}
|
|
|
|
%counts=%show;
|
|
|
|
}
|
|
|
|
|
2006-07-29 19:44:41 +02:00
|
|
|
if ($style eq 'table') {
|
2010-04-15 20:29:14 +02:00
|
|
|
return "<table class='".(exists $params{class} ? $params{class} : "pageStats")."'>\n".
|
2006-07-29 22:14:07 +02:00
|
|
|
join("\n", map {
|
|
|
|
"<tr><td>".
|
2014-02-22 23:30:09 +01:00
|
|
|
htmllink($params{page}, $params{destpage}, $_, noimageinline => 1, linktext => linktext($_, %params)).
|
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
|
|
|
|
|
2010-04-15 20:29:14 +02:00
|
|
|
my $res;
|
|
|
|
if ($style eq 'list') {
|
|
|
|
$res = "<ul class='".(exists $params{class} ? $params{class} : "list")."'>\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$res = "<div class='".(exists $params{class} ? $params{class} : "pagecloud")."'>\n";
|
|
|
|
}
|
2006-07-29 19:44:41 +02:00
|
|
|
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)];
|
2010-04-15 20:29:14 +02:00
|
|
|
|
|
|
|
$res.="<li>" if $style eq 'list';
|
2006-07-29 22:07:48 +02:00
|
|
|
$res .= "<span class=\"$class\">".
|
2014-02-22 23:30:09 +01:00
|
|
|
htmllink($params{page}, $params{destpage}, $page, linktext => linktext($page, %params)).
|
2006-07-29 22:07:48 +02:00
|
|
|
"</span>\n";
|
2010-04-15 20:29:14 +02:00
|
|
|
$res.="</li>" if $style eq 'list';
|
|
|
|
|
|
|
|
}
|
|
|
|
if ($style eq 'list') {
|
2010-04-15 20:45:03 +02:00
|
|
|
$res .= "</ul>\n";
|
2010-04-15 20:29:14 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$res .= "</div>\n";
|
2006-07-29 19:44:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-07-29 19:44:41 +02:00
|
|
|
|
|
|
|
1
|