summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/brokenlinks.pm
blob: 8ee734bf947b6137a2925bac3eeb69ebc965a9bd (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. my @broken;
  22. foreach my $link (keys %IkiWiki::brokenlinks) {
  23. next if $link =~ /.*\/\Q$config{discussionpage}\E/i && $config{discussion};
  24. my @pages=pagespec_match_list($params{page}, $params{pages},
  25. list => $IkiWiki::brokenlinks{$link},
  26. # needs to update when links on a page change
  27. deptype => deptype("links")
  28. );
  29. next unless @pages;
  30. my $page=$IkiWiki::brokenlinks{$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. } @pages)
  36. );
  37. }
  38. return gettext("There are no broken links!") unless @broken;
  39. return "<ul>\n"
  40. .join("\n",
  41. map {
  42. "<li>$_</li>"
  43. }
  44. sort @broken)
  45. ."</ul>\n";
  46. }
  47. 1