response
[ikiwiki] / ikiwiki.in
1 #!/usr/bin/perl
2 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
3 delete @ENV{qw{IFS CDPATH ENV BASH_ENV}};
4
5 package IkiWiki;
6
7 use warnings;
8 use strict;
9 use lib '.'; # For use in nonstandard directory, munged by Makefile.
10 use IkiWiki;
11
12 sub usage () {
13         die gettext("usage: ikiwiki [options] source dest"), "\n",
14             gettext("       ikiwiki --setup configfile"), "\n";
15 }
16
17 sub getconfig () {
18         if (! exists $ENV{WRAPPED_OPTIONS}) {
19                 %config=defaultconfig();
20                 eval q{use Getopt::Long};
21                 Getopt::Long::Configure('pass_through');
22                 GetOptions(
23                         "setup|s=s" => sub {
24                                 require IkiWiki::Setup;
25                                 my $verbose=$config{verbose};
26                                 my $syslog=$config{syslog};
27                                 IkiWiki::Setup::load($_[1]);
28                                 $config{setupverbose}=$config{verbose};
29                                 $config{setupsyslog}=$config{syslog};
30                                 $config{verbose}=$verbose || $config{setupverbose};
31                                 $config{syslog}=$syslog;
32                                 $config{setup}=1;
33                         },
34                         "dumpsetup|s=s" => \$config{dumpsetup},
35                         "wikiname=s" => \$config{wikiname},
36                         "verbose|v!" => \$config{verbose},
37                         "syslog!" => \$config{syslog},
38                         "rebuild!" => \$config{rebuild},
39                         "refresh!" => \$config{refresh},
40                         "clean!" => \$config{clean},
41                         "post-commit" => \$config{post_commit},
42                         "render=s" => \$config{render},
43                         "wrappers!" => \$config{genwrappers},
44                         "wrappergroup=s" => \$config{wrappergroup},
45                         "usedirs!" => \$config{usedirs},
46                         "prefix-directives!" => \$config{prefix_directives},
47                         "getctime" => \$config{gettime},
48                         "gettime!" => \$config{gettime},
49                         "numbacklinks=i" => \$config{numbacklinks},
50                         "rcs=s" => \$config{rcs},
51                         "no-rcs" => sub { $config{rcs}="" },
52                         "cgi!" => \$config{cgi},
53                         "discussion!" => \$config{discussion},
54                         "w3mmode!" => \$config{w3mmode},
55                         "url=s" => \$config{url},
56                         "cgiurl=s" => \$config{cgiurl},
57                         "historyurl=s" => \$config{historyurl},
58                         "diffurl=s" => \$config{diffurl},
59                         "svnpath" => \$config{svnpath},
60                         "adminemail=s" => \$config{adminemail},
61                         "timeformat=s" => \$config{timeformat},
62                         "sslcookie!" => \$config{sslcookie},
63                         "userdir=s" => \$config{userdir},
64                         "htmlext=s" => \$config{htmlext},
65                         "libdir=s" => \$config{libdir},
66                         "exclude=s@" => sub {
67                                 push @{$config{wiki_file_prune_regexps}}, $_[1];
68                         },
69                         "include=s@" => sub {
70                                 $config{include}=defined $config{include} && length $config{include} ? "$config{include}|$_[1]" : $_[1];
71                         },
72                         "adminuser=s@" => sub {
73                                 push @{$config{adminuser}}, $_[1]
74                         },
75                         "templatedir=s" => sub {
76                                 $config{templatedir}=possibly_foolish_untaint($_[1])
77                         },
78                         "underlaydir=s" => sub {
79                                 $config{underlaydir}=possibly_foolish_untaint($_[1])
80                         },
81                         "wrapper:s" => sub {
82                                 $config{wrapper}=$_[1] ? possibly_foolish_untaint($_[1]) : "ikiwiki-wrap"
83                         },
84                         "wrappermode=i" => sub {
85                                 $config{wrappermode}=possibly_foolish_untaint($_[1])
86                         },
87                         "plugin=s@" => sub {
88                                 push @{$config{add_plugins}}, $_[1];
89                         },
90                         "disable-plugin=s@" => sub {
91                                 push @{$config{disable_plugins}}, $_[1];
92                         },
93                         "set=s" => sub {
94                                 my ($var, $val)=split('=', $_[1], 2);
95                                 if (! defined $var || ! defined $val) {
96                                         die gettext("usage: --set var=value"), "\n";
97                                 }
98                                 $config{$var}=$val;
99                         },
100                         "set-yaml=s" => sub {
101                                 my ($var, $val)=split('=', $_[1], 2);
102                                 if (! defined $var || ! defined $val) {
103                                         die gettext("usage: --set-yaml var=value"), "\n";
104                                 }
105                                 eval q{use YAML::Any};
106                                 eval q{use YAML} if $@;
107                                 die $@ if $@;
108                                 eval q{$YAML::Syck::ImplicitUnicode=1};
109                                 $config{$var}=Load($val."\n");
110                         },
111                         "version" => sub {
112                                 print "ikiwiki version $IkiWiki::version\n";
113                                 exit;
114                         },
115                         "help|h" => sub { $SIG{__WARN__}=sub {}; die },
116                 ) || usage();
117
118                 if (! $config{setup}) {
119                         loadplugins();
120                         if (@ARGV == 2) {
121                                 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
122                                 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
123                                 checkconfig();
124                         }
125                         else {
126                                 usage() unless $config{dumpsetup};
127                         }
128                 }
129         }
130         else {
131                 # wrapper passes a full config structure in the environment
132                 # variable
133                 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
134                 if ($@) {
135                         error("WRAPPED_OPTIONS: $@");
136                 }
137                 delete $ENV{WRAPPED_OPTIONS};
138
139                 loadplugins();
140                 checkconfig();
141         }
142 }
143
144 sub main () {
145         getconfig();
146         
147         if ($config{setup}) {
148                 delete $config{setup};
149                 loadplugins();
150                 checkconfig();
151
152                 if (@{$config{wrappers}} && 
153                     ! $config{render} && ! $config{dumpsetup} &&
154                     ! $config{clean} &&
155                     ((! $config{refresh} && ! $config{post_commit})
156                      || $config{genwrappers})) {
157                         debug(gettext("generating wrappers.."));
158                         require IkiWiki::Wrapper;
159                         my %origconfig=(%config);
160                         foreach my $wrapper (@{$config{wrappers}}) {
161                                 %config=(%origconfig, %{$wrapper});
162                                 $config{verbose}=$config{setupverbose}
163                                         if exists $config{setupverbose};
164                                 $config{syslog}=$config{setupsyslog}
165                                         if exists $config{setupsyslog};
166                                 delete @config{qw(setupsyslog setupverbose wrappers genwrappers rebuild)};
167                                 checkconfig();
168                                 if (! $config{cgi} && ! $config{post_commit} &&
169                                     ! $config{test_receive}) {
170                                         $config{post_commit}=1;
171                                 }
172                                 gen_wrapper();
173                         }
174                         %config=(%origconfig);
175                 }
176                 
177                 # setup implies a wiki rebuild by default
178                 if (! $config{refresh} && ! $config{render} &&
179                     ! $config{post_commit} && ! $config{clean}) {
180                         $config{rebuild}=1;
181                 }
182         }
183
184         if ($config{dumpsetup}) {
185                 $config{srcdir}="" if ! defined $config{srcdir};
186                 $config{destdir}="" if ! defined $config{destdir};
187                 $config{syslog}=1 if $config{setupsyslog};
188                 require IkiWiki::Setup;
189                 IkiWiki::Setup::dump($config{dumpsetup});
190         }
191         elsif ($config{wrapper}) {
192                 lockwiki();
193                 require IkiWiki::Wrapper;
194                 gen_wrapper();
195         }
196         elsif ($config{cgi}) {
197                 require IkiWiki::CGI;
198                 eval {cgi()};
199                 if ($@) {
200                         cgierror($@);
201                 }
202         }
203         elsif ($config{render}) {
204                 require IkiWiki::Render;
205                 commandline_render();
206         }
207         elsif ($config{post_commit} && ! commit_hook_enabled()) {
208                 # do nothing
209         }
210         elsif ($config{clean}) {
211                 require IkiWiki::Render;
212                 foreach my $wrapper (@{$config{wrappers}}) {
213                         prune($wrapper->{wrapper});
214                 }
215                 clean_rendered();
216                 system("rm", "-rf", $config{wikistatedir});
217         }
218         else {
219                 if ($config{rebuild}) {
220                         debug(gettext("rebuilding wiki.."));
221                 }
222                 else {
223                         debug(gettext("refreshing wiki.."));
224                 }
225                 lockwiki();
226                 loadindex();
227                 require IkiWiki::Render;
228                 rcs_update();
229                 refresh();
230                 saveindex();
231                 debug(gettext("done"));
232         }
233 }
234
235 main;