summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/brokenlinks.pm
blob: 0295a8fe6b570d7b49503ba6587ae41a28f0cfa7 (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 => "preprocess", id => "brokenlinks", call => \&preprocess);
  9. } # }}}
  10. sub preprocess (@) { #{{{
  11. my %params=@_;
  12. $params{pages}="*" unless defined $params{pages};
  13. # Needs to update whenever a page is added or removed, so
  14. # register a dependency.
  15. add_depends($params{page}, $params{pages});
  16. my %broken;
  17. foreach my $page (keys %links) {
  18. if (pagespec_match($page, $params{pages}, location => $params{page})) {
  19. my $discussion=gettext("discussion");
  20. my %seen;
  21. foreach my $link (@{$links{$page}}) {
  22. next if $seen{$link};
  23. $seen{$link}=1;
  24. next if $link =~ /.*\/\Q$discussion\E/i && $config{discussion};
  25. my $bestlink=bestlink($page, $link);
  26. next if length $bestlink;
  27. push @{$broken{$link}}, $page;
  28. }
  29. }
  30. }
  31. my @broken;
  32. foreach my $link (keys %broken) {
  33. my $page=$broken{$link}->[0];
  34. push @broken, sprintf(gettext("%s from %s"),
  35. htmllink($page, $params{destpage}, $link, noimageinline => 1),
  36. join(", ", map {
  37. htmllink($params{page}, $params{destpage}, $_, noimageinline => 1)
  38. } @{$broken{$link}}));
  39. }
  40. return gettext("There are no broken links!") unless %broken;
  41. return "<ul>\n"
  42. .join("\n",
  43. map {
  44. "<li>$_</li>"
  45. }
  46. sort @broken)
  47. ."</ul>\n";
  48. } # }}}
  49. 1