]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/linkmap.pm
img: Fix a bug that could taint @links with undef values.
[ikiwiki.git] / IkiWiki / Plugin / linkmap.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::linkmap;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use IPC::Open2;
8
9 sub import {
10         hook(type => "getsetup", id => "linkmap", call => \&getsetup);
11         hook(type => "preprocess", id => "linkmap", call => \&preprocess);
12 }
13
14 sub getsetup () {
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                 },
20 }
21
22 my $mapnum=0;
23
24 sub preprocess (@) {
25         my %params=@_;
26
27         $params{pages}="*" unless defined $params{pages};
28         
29         $mapnum++;
30         my $connected=IkiWiki::yesno($params{connected});
31
32         # Get all the items to map.
33         my %mapitems = map { $_ => urlto($_, $params{destpage}) }
34                 pagespec_match_list($params{page}, $params{pages},
35                         # update when a page is added or removed, or its
36                         # links change
37                         deptype => deptype("presence", "links"));
38
39         my $dest=$params{page}."/linkmap.png";
40
41         # Use ikiwiki's function to create the file, this makes sure needed
42         # subdirs are there and does some sanity checking.
43         will_render($params{page}, $dest);
44         writefile($dest, $config{destdir}, "");
45
46         # Run dot to create the graphic and get the map data.
47         my $pid;
48         my $sigpipe=0;
49         $SIG{PIPE}=sub { $sigpipe=1 };
50         $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
51         
52         # open2 doesn't respect "use open ':utf8'"
53         binmode (IN, ':utf8'); 
54         binmode (OUT, ':utf8'); 
55
56         print OUT "digraph linkmap$mapnum {\n";
57         print OUT "concentrate=true;\n";
58         print OUT "charset=\"utf-8\";\n";
59         print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
60                 if defined $params{width} and defined $params{height};
61         my %shown;
62         my $show=sub {
63                 my $item=shift;
64                 if (! $shown{$item}) {
65                         print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
66                         $shown{$item}=1;
67                 }
68         };
69         foreach my $item (keys %mapitems) {
70                 $show->($item) unless $connected;
71                 foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
72                         next unless length $link and $mapitems{$link};
73                         foreach my $endpoint ($item, $link) {
74                                 $show->($endpoint);
75                         }
76                         print OUT "\"$item\" -> \"$link\";\n";
77                 }
78         }
79         print OUT "}\n";
80         close OUT || error gettext("failed to run dot");
81
82         local $/=undef;
83         my $ret="<img src=\"".urlto($dest, $params{destpage}).
84                "\" alt=\"".gettext("linkmap").
85                "\" usemap=\"#linkmap$mapnum\" />\n".
86                 <IN>;
87         close IN || error gettext("failed to run dot");
88         
89         waitpid $pid, 0;
90         if ($?) {
91                 error gettext("failed to run dot");
92         }
93         $SIG{PIPE}="DEFAULT";
94         error gettext("failed to run dot") if $sigpipe;
95
96         return $ret;
97 }
98
99 1