summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/brokenlinks.pm
blob: eb698b0bef9f7b2c9df90b342dcfac56c79a1795 (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 a page is added or removed, so
  22. # register a dependency.
  23. add_depends($params{page}, $params{pages});
  24. my @broken;
  25. foreach my $link (keys %IkiWiki::brokenlinks) {
  26. next if $link =~ /.*\/\Q$config{discussionpage}\E/i && $config{discussion};
  27. my @pages;
  28. foreach my $page (@{$IkiWiki::brokenlinks{$link}}) {
  29. push @pages, $page
  30. if pagespec_match($page, $params{pages}, location => $params{page});
  31. }
  32. next unless @pages;
  33. my $page=$IkiWiki::brokenlinks{$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. } @pages)
  39. );
  40. }
  41. return gettext("There are no broken links!") unless @broken;
  42. return "<ul>\n"
  43. .join("\n",
  44. map {
  45. "<li>$_</li>"
  46. }
  47. sort @broken)
  48. ."</ul>\n";
  49. }
  50. 1