Merge branch 'master' into tova
[ikiwiki] / IkiWiki / Plugin / brokenlinks.pm
1 #!/usr/bin/perl
2 # Provides a list of broken links.
3 package IkiWiki::Plugin::brokenlinks;
4
5 use warnings;
6 use strict;
7 use IkiWiki 2.00;
8
9 sub import { #{{{
10         hook(type => "preprocess", id => "brokenlinks", call => \&preprocess);
11 } # }}}
12
13 sub preprocess (@) { #{{{
14         my %params=@_;
15         $params{pages}="*" unless defined $params{pages};
16         
17         # Needs to update whenever a page is added or removed, so
18         # register a dependency.
19         add_depends($params{page}, $params{pages});
20         
21         my %broken;
22         foreach my $page (keys %links) {
23                 if (pagespec_match($page, $params{pages}, location => $params{page})) {
24                         my $discussion=gettext("discussion");
25                         my %seen;
26                         foreach my $link (@{$links{$page}}) {
27                                 next if $seen{$link};
28                                 $seen{$link}=1;
29                                 next if $link =~ /.*\/\Q$discussion\E/i && $config{discussion};
30                                 my $bestlink=bestlink($page, $link);
31                                 next if length $bestlink;
32                                 push @{$broken{$link}}, $page;
33                         }
34                 }
35         }
36
37         my @broken;
38         foreach my $link (keys %broken) {
39                 my $page=$broken{$link}->[0];
40                 push @broken, sprintf(gettext("%s from %s"),
41                         htmllink($page, $params{destpage}, $link, noimageinline => 1),
42                         join(", ", map {
43                                 htmllink($params{page}, $params{destpage}, $_,  noimageinline => 1)
44                         } @{$broken{$link}}));
45         }
46         
47         return gettext("There are no broken links!") unless %broken;
48         return "<ul>\n"
49                 .join("\n",
50                         map {
51                                 "<li>$_</li>"
52                         }
53                         sort @broken)
54                 ."</ul>\n";
55 } # }}}
56
57 1