summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/pagestats.pm
blob: 17b26f7baa75ed3b3e68c95f310f6821227d0c89 (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='".(exists $params{class} ? $params{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;
  79. if ($style eq 'list') {
  80. $res = "<ul class='".(exists $params{class} ? $params{class} : "list")."'>\n";
  81. }
  82. else {
  83. $res = "<div class='".(exists $params{class} ? $params{class} : "pagecloud")."'>\n";
  84. }
  85. foreach my $page (sort keys %counts) {
  86. next unless $counts{$page} > 0;
  87. my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
  88. $res.="<li>" if $style eq 'list';
  89. $res .= "<span class=\"$class\">".
  90. htmllink($params{page}, $params{destpage}, $page).
  91. "</span>\n";
  92. $res.="</li>" if $style eq 'list';
  93. }
  94. if ($style eq 'list') {
  95. $res .= "</ul>\n";
  96. }
  97. else {
  98. $res .= "</div>\n";
  99. }
  100. return $res;
  101. }
  102. }
  103. 1