]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/brokenlinks.pm
fix example
[git.ikiwiki.info.git] / IkiWiki / Plugin / brokenlinks.pm
1 #!/usr/bin/perl
2 # Provides a list of broken links.
3 package IkiWiki::Plugin::brokenlinks;
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
9 sub import {
10         hook(type => "getsetup", id => "brokenlinks", call => \&getsetup);
11         hook(type => "preprocess", id => "brokenlinks", call => \&preprocess);
12 }
14 sub getsetup {
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                 },
20 }
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 $page (pagespec_match_list([keys %links],
32                         $params{pages}, location => $params{page})) {
33                 my $discussion=gettext("Discussion");
34                 my %seen;
35                 foreach my $link (@{$links{$page}}) {
36                         next if $seen{$link};
37                         $seen{$link}=1;
38                         next if $link =~ /.*\/\Q$discussion\E/i && $config{discussion};
39                         my $bestlink=bestlink($page, $link);
40                         next if length $bestlink;
41                         push @{$broken{$link}}, $page;
42                 }
43         }
45         my @broken;
46         foreach my $link (keys %broken) {
47                 my $page=$broken{$link}->[0];
48                 push @broken, sprintf(gettext("%s from %s"),
49                         htmllink($page, $params{destpage}, $link, noimageinline => 1),
50                         join(", ", map {
51                                 htmllink($params{page}, $params{destpage}, $_,  noimageinline => 1)
52                         } @{$broken{$link}}));
53         }
54         
55         return gettext("There are no broken links!") unless %broken;
56         return "<ul>\n"
57                 .join("\n",
58                         map {
59                                 "<li>$_</li>"
60                         }
61                         sort @broken)
62                 ."</ul>\n";
63 }
65 1