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