]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Setup.pm
po: added an optional target percentage to needstranslation
[git.ikiwiki.info.git] / IkiWiki / Setup.pm
1 #!/usr/bin/perl
2 # Ikiwiki setup files can be perl files that 'use IkiWiki::Setup::foo',
3 # passing it some sort of configuration data. Or, they can contain
4 # the module name at the top, without the 'use', and the whole file is
5 # then fed into that module.
7 package IkiWiki::Setup;
9 use warnings;
10 use strict;
11 use IkiWiki;
12 use open qw{:utf8 :std};
13 use File::Spec;
15 sub load ($;$) {
16         my $file=IkiWiki::possibly_foolish_untaint(shift);
17         my $safemode=shift;
19         $config{setupfile}=File::Spec->rel2abs($file);
21         #translators: The first parameter is a filename, and the second
22         #translators: is a (probably not translated) error message.
23         open (IN, $file) || error(sprintf(gettext("cannot read %s: %s"), $file, $!));
24         my $content;
25         {
26                 local $/=undef;
27                 $content=<IN> || error("$file: $!");
28         }
29         close IN;
31         if ($content=~/((?:use|require)\s+)?IkiWiki::Setup::(\w+)/) {
32                 $config{setuptype}=$2;
33                 if ($1) {
34                         error sprintf(gettext("cannot load %s in safe mode"), $file)
35                                 if $safemode;
36                         no warnings;
37                         eval IkiWiki::possibly_foolish_untaint($content);
38                         error("$file: ".$@) if $@;
39                 }
40                 else {
41                         eval qq{require IkiWiki::Setup::$config{setuptype}};
42                         error $@ if $@;
43                         "IkiWiki::Setup::$config{setuptype}"->loaddump(IkiWiki::possibly_foolish_untaint($content));
44                 }
45         }
46         else {
47                 error sprintf(gettext("failed to parse %s"), $file);
48         }
49 }
51 sub dump ($) {
52         my $file=IkiWiki::possibly_foolish_untaint(shift);
54         my @header=(
55                 "Setup file for ikiwiki.",
56                 "",
57                 "Passing this to ikiwiki --setup will make ikiwiki generate",
58                 "wrappers and build the wiki.",
59                 "",
60                 "Remember to re-run ikiwiki --setup any time you edit this file.",
61         );
63         # Fork because dumping setup requires loading all plugins.
64         my $pid=fork();
65         if ($pid == 0) {
66                 eval qq{require IkiWiki::Setup::$config{setuptype}};
67                 error $@ if $@;
68                 my @dump="IkiWiki::Setup::$config{setuptype}"->gendump(@header);
69         
70                 open (OUT, ">", $file) || die "$file: $!";
71                 print OUT "$_\n" foreach @dump;
72                 close OUT;
74                 exit 0;
75         }
76         else {
77                 waitpid $pid, 0;
78                 exit($? >> 8) if $? >> 8;
79                 exit(1) if $?;
80         }
81 }
83 sub merge ($) {
84         # Merge setup into existing config and untaint.
85         my %setup=%{shift()};
87         if (exists $setup{add_plugins} && exists $config{add_plugins}) {
88                 push @{$setup{add_plugins}}, @{$config{add_plugins}};
89         }
90         if (exists $setup{exclude}) {
91                 push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
92         }
93         foreach my $c (keys %setup) {
94                 if (defined $setup{$c}) {
95                         if (! ref $setup{$c} || ref $setup{$c} eq 'Regexp') {
96                                 $config{$c}=IkiWiki::possibly_foolish_untaint($setup{$c});
97                         }
98                         elsif (ref $setup{$c} eq 'ARRAY') {
99                                 if ($c eq 'wrappers') {
100                                         # backwards compatability code
101                                         $config{$c}=$setup{$c};
102                                 }
103                                 else {
104                                         $config{$c}=[map { IkiWiki::possibly_foolish_untaint($_) } @{$setup{$c}}]
105                                 }
106                         }
107                         elsif (ref $setup{$c} eq 'HASH') {
108                                 foreach my $key (keys %{$setup{$c}}) {
109                                         $config{$c}{$key}=IkiWiki::possibly_foolish_untaint($setup{$c}{$key});
110                                 }
111                         }
112                 }
113                 else {
114                         $config{$c}=undef;
115                 }
116         }
117         
118         if (length $config{cgi_wrapper}) {
119                 push @{$config{wrappers}}, {
120                         cgi => 1,
121                         wrapper => $config{cgi_wrapper},
122                         wrappermode => (defined $config{cgi_wrappermode} ? $config{cgi_wrappermode} : "06755"),
123                 };
124         }
127 sub getsetup () {
128         # Gets all available setup data from all plugins. Returns an
129         # ordered list of [plugin, setup] pairs.
131         # disable logging to syslog while dumping, broken plugins may
132         # whine when loaded
133         my $syslog=$config{syslog};
134         $config{syslog}=undef;
136         # Load all plugins, so that all setup options are available.
137         my @plugins=IkiWiki::listplugins();
138         foreach my $plugin (@plugins) {
139                 eval { IkiWiki::loadplugin($plugin, 1) };
140                 if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) {
141                         my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
142                 }
143         }
144         
145         my %sections;
146         foreach my $plugin (@plugins) {
147                 if (exists $IkiWiki::hooks{getsetup}{$plugin}{call}) {
148                         # use an array rather than a hash, to preserve order
149                         my @s=eval { $IkiWiki::hooks{getsetup}{$plugin}{call}->() };
150                         next unless @s;
152                         # set default section value (note use of shared
153                         # hashref between array and hash)
154                         my %s=@s;
155                         if (! exists $s{plugin} || ! $s{plugin}->{section}) {
156                                 $s{plugin}->{section}="other";
157                         }
159                         # only the selected rcs plugin is included
160                         if ($config{rcs} && $plugin eq $config{rcs}) {
161                                 $s{plugin}->{section}="core";
162                         }
163                         elsif ($s{plugin}->{section} eq "rcs") {
164                                 next;
165                         }
167                         push @{$sections{$s{plugin}->{section}}}, [ $plugin, \@s ];
168                 }
169         }
170         
171         $config{syslog}=$syslog;
173         return map { sort { $a->[0] cmp $b->[0] } @{$sections{$_}} }
174                 sort { # core first, other last, otherwise alphabetical
175                         ($b eq "core") <=> ($a eq "core")
176                            ||
177                         ($a eq "other") <=> ($b eq "other")
178                            ||
179                         $a cmp $b
180                 } keys %sections;
183 sub commented_dump ($$) {
184         my $dumpline=shift;
185         my $indent=shift;
187         my %setup=(%config);
188         my @ret;
189         
190         # disable logging to syslog while dumping
191         $config{syslog}=undef;
193         eval q{use Text::Wrap};
194         die $@ if $@;
196         my %section_plugins;
197         push @ret, commented_dumpvalues($dumpline, $indent, \%setup, IkiWiki::getsetup());
198         foreach my $pair (IkiWiki::Setup::getsetup()) {
199                 my $plugin=$pair->[0];
200                 my $setup=$pair->[1];
201                 my %s=@{$setup};
202                 my $section=$s{plugin}->{section};
203                 push @{$section_plugins{$section}}, $plugin;
204                 if (@{$section_plugins{$section}} == 1) {
205                         push @ret, "", $indent.("#" x 70), "$indent# $section plugins",
206                                 sub {
207                                         wrap("$indent#   (", "$indent#    ",
208                                                 join(", ", @{$section_plugins{$section}})).")"
209                                 },
210                                 $indent.("#" x 70);
211                 }
213                 my @values=commented_dumpvalues($dumpline, $indent, \%setup, @{$setup});
214                 if (@values) {
215                         push @ret, "", "$indent# $plugin plugin", @values;
216                 }
217         }
219         return map { ref $_ ? $_->() : $_ } @ret;
222 sub commented_dumpvalues ($$$@) {
223         my $dumpline=shift;
224         my $indent=shift;
225         my $setup=shift;
226         my @ret;
227         while (@_) {
228                 my $key=shift;
229                 my %info=%{shift()};
231                 next if $key eq "plugin" || $info{type} eq "internal";
232                 
233                 push @ret, "$indent# ".$info{description} if exists $info{description};
234                 
235                 if (exists $setup->{$key} && defined $setup->{$key}) {
236                         push @ret, $dumpline->($key, $setup->{$key}, $info{type}, "");
237                         delete $setup->{$key};
238                 }
239                 elsif (exists $info{example}) {
240                         push @ret, $dumpline->($key, $info{example}, $info{type}, "#");
241                 }
242                 else {
243                         push @ret, $dumpline->($key, "", $info{type}, "#");
244                 }
245         }
246         return @ret;