corrected the update-the-laptop-from-the-server section
[ikiwiki.git] / IkiWiki / Setup.pm
bloba3fd5ce66466f45e1480776e31be39ece90f592b
1 #!/usr/bin/perl
2 # Ikiwiki setup files are perl files that 'use IkiWiki::Setup::foo',
3 # passing it some sort of configuration data.
5 package IkiWiki::Setup;
7 use warnings;
8 use strict;
9 use IkiWiki;
10 use open qw{:utf8 :std};
11 use File::Spec;
13 sub load ($) {
14 my $setup=IkiWiki::possibly_foolish_untaint(shift);
15 $config{setupfile}=File::Spec->rel2abs($setup);
17 #translators: The first parameter is a filename, and the second
18 #translators: is a (probably not translated) error message.
19 open (IN, $setup) || error(sprintf(gettext("cannot read %s: %s"), $setup, $!));
20 my $code;
22 local $/=undef;
23 $code=<IN> || error("$setup: $!");
26 ($code)=$code=~/(.*)/s;
27 close IN;
29 eval $code;
30 error("$setup: ".$@) if $@;
33 sub merge ($) {
34 # Merge setup into existing config and untaint.
35 my %setup=%{shift()};
37 if (exists $setup{add_plugins} && exists $config{add_plugins}) {
38 push @{$setup{add_plugins}}, @{$config{add_plugins}};
40 if (exists $setup{exclude}) {
41 push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
43 foreach my $c (keys %setup) {
44 if (defined $setup{$c}) {
45 if (! ref $setup{$c} || ref $setup{$c} eq 'Regexp') {
46 $config{$c}=IkiWiki::possibly_foolish_untaint($setup{$c});
48 elsif (ref $setup{$c} eq 'ARRAY') {
49 if ($c eq 'wrappers') {
50 # backwards compatability code
51 $config{$c}=$setup{$c};
53 else {
54 $config{$c}=[map { IkiWiki::possibly_foolish_untaint($_) } @{$setup{$c}}]
57 elsif (ref $setup{$c} eq 'HASH') {
58 foreach my $key (keys %{$setup{$c}}) {
59 $config{$c}{$key}=IkiWiki::possibly_foolish_untaint($setup{$c}{$key});
63 else {
64 $config{$c}=undef;
68 if (length $config{cgi_wrapper}) {
69 push @{$config{wrappers}}, {
70 cgi => 1,
71 wrapper => $config{cgi_wrapper},
72 wrappermode => (defined $config{cgi_wrappermode} ? $config{cgi_wrappermode} : "06755"),
77 sub getsetup () {
78 # Gets all available setup data from all plugins. Returns an
79 # ordered list of [plugin, setup] pairs.
81 # disable logging to syslog while dumping, broken plugins may
82 # whine when loaded
83 my $syslog=$config{syslog};
84 $config{syslog}=undef;
86 # Load all plugins, so that all setup options are available.
87 my @plugins=IkiWiki::listplugins();
88 foreach my $plugin (@plugins) {
89 eval { IkiWiki::loadplugin($plugin) };
90 if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) {
91 my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
95 my %sections;
96 foreach my $plugin (@plugins) {
97 if (exists $IkiWiki::hooks{getsetup}{$plugin}{call}) {
98 # use an array rather than a hash, to preserve order
99 my @s=eval { $IkiWiki::hooks{getsetup}{$plugin}{call}->() };
100 next unless @s;
102 # set default section value (note use of shared
103 # hashref between array and hash)
104 my %s=@s;
105 if (! exists $s{plugin} || ! $s{plugin}->{section}) {
106 $s{plugin}->{section}="other";
109 # only the selected rcs plugin is included
110 if ($config{rcs} && $plugin eq $config{rcs}) {
111 $s{plugin}->{section}="core";
113 elsif ($s{plugin}->{section} eq "rcs") {
114 next;
117 push @{$sections{$s{plugin}->{section}}}, [ $plugin, \@s ];
121 $config{syslog}=$syslog;
123 return map { sort { $a->[0] cmp $b->[0] } @{$sections{$_}} }
124 sort { # core first, other last, otherwise alphabetical
125 ($b eq "core") <=> ($a eq "core")
127 ($a eq "other") <=> ($b eq "other")
129 $a cmp $b
130 } keys %sections;
133 sub dump ($) {
134 my $file=IkiWiki::possibly_foolish_untaint(shift);
136 require IkiWiki::Setup::Standard;
137 my @dump=IkiWiki::Setup::Standard::gendump("Setup file for ikiwiki.");
139 open (OUT, ">", $file) || die "$file: $!";
140 print OUT "$_\n" foreach @dump;
141 close OUT;