summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/brokenlinks.pm
blob: 1c52099bf9be94360e7f6fde85befdd6e22ca4b4 (plain)
  1. #!/usr/bin/perl
  2. # Provides a list of broken links.
  3. package IkiWiki::Plugin::brokenlinks;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 2.00;
  7. sub import {
  8. hook(type => "getsetup", id => "brokenlinks", call => \&getsetup);
  9. hook(type => "preprocess", id => "brokenlinks", call => \&preprocess);
  10. }
  11. sub getsetup {
  12. return
  13. plugin => {
  14. safe => 1,
  15. rebuild => undef,
  16. },
  17. }
  18. sub preprocess (@) {
  19. my %params=@_;
  20. $params{pages}="*" unless defined $params{pages};
  21. # Needs to update whenever a page is added or removed, so
  22. # register a dependency.
  23. add_depends($params{page}, $params{pages});
  24. my %broken;
  25. foreach my $page (keys %links) {
  26. if (pagespec_match($page, $params{pages}, location => $params{page})) {
  27. my $discussion=gettext("discussion");
  28. my %seen;
  29. foreach my $link (@{$links{$page}}) {
  30. next if $seen{$link};
  31. $seen{$link}=1;
  32. next if $link =~ /.*\/\Q$discussion\E/i && $config{discussion};
  33. my $bestlink=bestlink($page, $link);
  34. next if length $bestlink;
  35. push @{$broken{$link}}, $page;
  36. }
  37. }
  38. }
  39. my @broken;
  40. foreach my $link (keys %broken) {
  41. my $page=$broken{$link}->[0];
  42. push @broken, sprintf(gettext("%s from %s"),
  43. htmllink($page, $params{destpage}, $link, noimageinline => 1),
  44. join(", ", map {
  45. htmllink($params{page}, $params{destpage}, $_, noimageinline => 1)
  46. } @{$broken{$link}}));
  47. }
  48. return gettext("There are no broken links!") unless %broken;
  49. return "<ul>\n"
  50. .join("\n",
  51. map {
  52. "<li>$_</li>"
  53. }
  54. sort @broken)
  55. ."</ul>\n";
  56. }
  57. 1