]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/brokenlinks.pm
linkmap, pagestats: avoid using %links to get a list of pages
[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 the links on a page change.
27         add_depends($params{page}, $params{pages}, links => 1);
28         
29         my @broken;
30         foreach my $link (keys %IkiWiki::brokenlinks) {
31                 next if $link =~ /.*\/\Q$config{discussionpage}\E/i && $config{discussion};
33                 my @pages;
34                 foreach my $page (@{$IkiWiki::brokenlinks{$link}}) {
35                         push @pages, $page
36                                 if pagespec_match($page, $params{pages}, location => $params{page});
37                 }
38                 next unless @pages;
40                 my $page=$IkiWiki::brokenlinks{$link}->[0];
41                 push @broken, sprintf(gettext("%s from %s"),
42                         htmllink($page, $params{destpage}, $link, noimageinline => 1),
43                         join(", ", map {
44                                 htmllink($params{page}, $params{destpage}, $_,  noimageinline => 1)
45                         } @pages)
46                 );
47         }
48         
49         return gettext("There are no broken links!") unless @broken;
50         return "<ul>\n"
51                 .join("\n",
52                         map {
53                                 "<li>$_</li>"
54                         }
55                         sort @broken)
56                 ."</ul>\n";
57 }
59 1