Merge branch 'master' into transitive-dependencies
[ikiwiki.git] / 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 3.00;
8
9 sub import {
10         hook(type => "getsetup", id => "brokenlinks", call => \&getsetup);
11         hook(type => "preprocess", id => "brokenlinks", call => \&preprocess);
12 }
13
14 sub getsetup {
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                 },
20 }
21
22 sub preprocess (@) {
23         my %params=@_;
24         $params{pages}="*" unless defined $params{pages};
25         
26         # Needs to update whenever a page is added or removed, so
27         # register a dependency.
28         add_depends($params{page}, $params{pages});
29         
30         my @broken;
31         foreach my $link (keys %IkiWiki::brokenlinks) {
32                 next if $link =~ /.*\/\Q$config{discussionpage}\E/i && $config{discussion};
33
34                 my @pages;
35                 foreach my $page (@{$IkiWiki::brokenlinks{$link}}) {
36                         push @pages, $page
37                                 if pagespec_match($page, $params{pages}, location => $params{page});
38                 }
39                 next unless @pages;
40
41                 my $page=$IkiWiki::brokenlinks{$link}->[0];
42                 push @broken, sprintf(gettext("%s from %s"),
43                         htmllink($page, $params{destpage}, $link, noimageinline => 1),
44                         join(", ", map {
45                                 htmllink($params{page}, $params{destpage}, $_,  noimageinline => 1)
46                         } @pages)
47                 );
48         }
49         
50         return gettext("There are no broken links!") unless @broken;
51         return "<ul>\n"
52                 .join("\n",
53                         map {
54                                 "<li>$_</li>"
55                         }
56                         sort @broken)
57                 ."</ul>\n";
58 }
59
60 1