summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/brokenlinks.pm
blob: ef5837a5876c44b1082a3556d5cabe75cd24c7ef (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. foreach my $link (@{$links{$page}}) {
  21. next if $link =~ /.*\/\Q$discussion\E/i && $config{discussion};
  22. my $bestlink=bestlink($page, $link);
  23. next if length $bestlink;
  24. push @{$broken{$link}}, $page;
  25. }
  26. }
  27. }
  28. my @broken;
  29. foreach my $link (keys %broken) {
  30. my $page=$broken{$link}->[0];
  31. push @broken, sprintf(gettext("%s from %s"),
  32. htmllink($page, $params{destpage}, $link, noimageinline => 1),
  33. join(", ", map {
  34. htmllink($params{page}, $params{destpage}, $_, noimageinline => 1)
  35. } @{$broken{$link}}));
  36. }
  37. return gettext("There are no broken links!") unless %broken;
  38. return "<ul>\n"
  39. .join("\n",
  40. map {
  41. "<li>$_</li>"
  42. }
  43. sort @broken)
  44. ."</ul>\n";
  45. } # }}}
  46. 1