summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/brokenlinks.pm
blob: 3406f991978d1e707c8f33c46cdd20db419447ab (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;
  7. sub import { #{{{
  8. IkiWiki::hook(type => "preprocess", id => "brokenlinks",
  9. call => \&preprocess);
  10. } # }}}
  11. sub preprocess (@) { #{{{
  12. my %params=@_;
  13. $params{pages}="*" unless defined $params{pages};
  14. # Needs to update whenever a page is added or removed, so
  15. # register a dependency.
  16. IkiWiki::add_depends($params{page}, $params{pages});
  17. my @broken;
  18. foreach my $page (%IkiWiki::links) {
  19. if (IkiWiki::globlist_match($page, $params{pages})) {
  20. foreach my $link (@{$IkiWiki::links{$page}}) {
  21. next if $link =~ /.*\/discussion/i && $IkiWiki::config{discussion};
  22. my $bestlink=IkiWiki::bestlink($page, $link);
  23. next if length $bestlink;
  24. push @broken,
  25. IkiWiki::htmllink($page, $params{destpage}, $link, 1).
  26. " in ".
  27. IkiWiki::htmllink($params{page}, $params{destpage}, $page, 1);
  28. }
  29. }
  30. }
  31. return "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