summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/pagestats.pm
blob: 874ead7e6de669c1d33c66b74984ef90504f9b8e (plain)
  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. use warnings;
  13. use strict;
  14. use IkiWiki 3.00;
  15. # Names of the HTML classes to use for the tag cloud
  16. our @classes = ('smallestPC', 'smallPC', 'normalPC', 'bigPC', 'biggestPC' );
  17. sub import {
  18. hook(type => "getsetup", id => "pagestats", call => \&getsetup);
  19. hook(type => "preprocess", id => "pagestats", call => \&preprocess);
  20. }
  21. sub getsetup () {
  22. return
  23. plugin => {
  24. safe => 1,
  25. rebuild => undef,
  26. },
  27. }
  28. sub preprocess (@) {
  29. my %params=@_;
  30. $params{pages}="*" unless defined $params{pages};
  31. my $style = ($params{style} or 'cloud');
  32. # Needs to update whenever a page is added or removed, so
  33. # register a dependency.
  34. add_depends($params{page}, $params{pages});
  35. add_depends($params{page}, $params{among}) if exists $params{among};
  36. my %counts;
  37. my $max = 0;
  38. foreach my $page (pagespec_match_list([keys %links],
  39. $params{pages}, location => $params{page})) {
  40. use IkiWiki::Render;
  41. my @backlinks = IkiWiki::backlink_pages($page);
  42. if (exists $params{among}) {
  43. @backlinks = pagespec_match_list(\@backlinks,
  44. $params{among}, location => $params{page});
  45. }
  46. $counts{$page} = scalar(@backlinks);
  47. $max = $counts{$page} if $counts{$page} > $max;
  48. }
  49. if ($style eq 'table') {
  50. return "<table class='pageStats'>\n".
  51. join("\n", map {
  52. "<tr><td>".
  53. htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
  54. "</td><td>".$counts{$_}."</td></tr>"
  55. }
  56. sort { $counts{$b} <=> $counts{$a} } keys %counts).
  57. "\n</table>\n" ;
  58. }
  59. else {
  60. # In case of misspelling, default to a page cloud
  61. my $res = "<div class='pagecloud'>\n";
  62. foreach my $page (sort keys %counts) {
  63. next unless $counts{$page} > 0;
  64. my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
  65. $res .= "<span class=\"$class\">".
  66. htmllink($params{page}, $params{destpage}, $page).
  67. "</span>\n";
  68. }
  69. $res .= "</div>\n";
  70. return $res;
  71. }
  72. }
  73. 1