summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/pagestats.pm
blob: 47638210aeddeff49866ac1e5a6459828556ddd2 (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. my %counts;
  33. my $max = 0;
  34. foreach my $page (pagespec_match_list($params{page}, $params{pages},
  35. # update when a displayed page is added/removed
  36. deptype => deptype("presence"))) {
  37. use IkiWiki::Render;
  38. my @backlinks = IkiWiki::backlink_pages($page);
  39. if (exists $params{among}) {
  40. # only consider backlinks from the amoung pages
  41. @backlinks = pagespec_match_list(
  42. $params{page}, $params{among},
  43. # update whenever links on those pages change
  44. deptype => deptype("links"),
  45. list => \@backlinks
  46. );
  47. }
  48. else {
  49. # update when any page with links changes,
  50. # in case the links point to our displayed pages
  51. add_depends($params{page}, "*", deptype("links"));
  52. }
  53. $counts{$page} = scalar(@backlinks);
  54. $max = $counts{$page} if $counts{$page} > $max;
  55. }
  56. if ($style eq 'table') {
  57. return "<table class='pageStats'>\n".
  58. join("\n", map {
  59. "<tr><td>".
  60. htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
  61. "</td><td>".$counts{$_}."</td></tr>"
  62. }
  63. sort { $counts{$b} <=> $counts{$a} } keys %counts).
  64. "\n</table>\n" ;
  65. }
  66. else {
  67. # In case of misspelling, default to a page cloud
  68. my $res = "<div class='pagecloud'>\n";
  69. foreach my $page (sort keys %counts) {
  70. next unless $counts{$page} > 0;
  71. my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
  72. $res .= "<span class=\"$class\">".
  73. htmllink($params{page}, $params{destpage}, $page).
  74. "</span>\n";
  75. }
  76. $res .= "</div>\n";
  77. return $res;
  78. }
  79. }
  80. 1