summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/brokenlinks.pm
blob: fa34a70089d2fe2ffa651a5a6b001f22cf5141cd (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,
  25. htmllink($page, $params{destpage}, $link, noimageinline => 1).
  26. " from ".
  27. htmllink($params{page}, $params{destpage}, $page, noimageinline => 1);
  28. }
  29. }
  30. }
  31. return gettext("There are no broken links!") unless @broken;
  32. my %seen;
  33. return "<ul>\n".join("\n", map { "<li>$_</li>" } grep { ! $seen{$_}++ } sort @broken)."</ul>\n";
  34. } # }}}
  35. 1