summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/brokenlinks.pm
blob: 62a0a42f4547c865f000d02d2e188987951037ec (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 3.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 the links on a page change.
  22. add_depends($params{page}, $params{pages}, deptype("links"));
  23. my @broken;
  24. foreach my $link (keys %IkiWiki::brokenlinks) {
  25. next if $link =~ /.*\/\Q$config{discussionpage}\E/i && $config{discussion};
  26. my @pages;
  27. foreach my $page (@{$IkiWiki::brokenlinks{$link}}) {
  28. push @pages, $page
  29. if pagespec_match($page, $params{pages}, location => $params{page});
  30. }
  31. next unless @pages;
  32. my $page=$IkiWiki::brokenlinks{$link}->[0];
  33. push @broken, sprintf(gettext("%s from %s"),
  34. htmllink($page, $params{destpage}, $link, noimageinline => 1),
  35. join(", ", map {
  36. htmllink($params{page}, $params{destpage}, $_, noimageinline => 1)
  37. } @pages)
  38. );
  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