idea
[ikiwiki] / ikiwiki.in
1 #!/usr/bin/perl -T
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                         "post-commit" => \$config{post_commit},
41                         "render=s" => \$config{render},
42                         "wrappers!" => \$config{genwrappers},
43                         "usedirs!" => \$config{usedirs},
44                         "prefix-directives!" => \$config{prefix_directives},
45                         "getctime" => \$config{getctime},
46                         "numbacklinks=i" => \$config{numbacklinks},
47                         "rcs=s" => \$config{rcs},
48                         "no-rcs" => sub { $config{rcs}="" },
49                         "cgi!" => \$config{cgi},
50                         "discussion!" => \$config{discussion},
51                         "w3mmode!" => \$config{w3mmode},
52                         "url=s" => \$config{url},
53                         "cgiurl=s" => \$config{cgiurl},
54                         "historyurl=s" => \$config{historyurl},
55                         "diffurl=s" => \$config{diffurl},
56                         "svnpath" => \$config{svnpath},
57                         "adminemail=s" => \$config{adminemail},
58                         "timeformat=s" => \$config{timeformat},
59                         "sslcookie!" => \$config{sslcookie},
60                         "userdir=s" => \$config{userdir},
61                         "htmlext=s" => \$config{htmlext},
62                         "libdir=s" => \$config{libdir},
63                         "exclude=s@" => sub {
64                                 push @{$config{wiki_file_prune_regexps}}, $_[1];
65                         },
66                         "adminuser=s@" => sub {
67                                 push @{$config{adminuser}}, $_[1]
68                         },
69                         "templatedir=s" => sub {
70                                 $config{templatedir}=possibly_foolish_untaint($_[1])
71                         },
72                         "underlaydir=s" => sub {
73                                 $config{underlaydir}=possibly_foolish_untaint($_[1])
74                         },
75                         "wrapper:s" => sub {
76                                 $config{wrapper}=$_[1] ? possibly_foolish_untaint($_[1]) : "ikiwiki-wrap"
77                         },
78                         "wrappermode=i" => sub {
79                                 $config{wrappermode}=possibly_foolish_untaint($_[1])
80                         },
81                         "plugin=s@" => sub {
82                                 push @{$config{add_plugins}}, $_[1];
83                         },
84                         "disable-plugin=s@" => sub {
85                                 push @{$config{disable_plugins}}, $_[1];
86                         },
87                         "set=s" => sub {
88                                 my ($var, $val)=split('=', $_[1], 2);
89                                 if (! defined $var || ! defined $val) {
90                                         die gettext("usage: --set var=value"), "\n";
91                                 }
92                                 $config{$var}=$val;
93                         },
94                         "version" => sub {
95                                 print "ikiwiki version $IkiWiki::version\n";
96                                 exit;
97                         },
98                         "help|h" => sub { $SIG{__WARN__}=sub {}; die },
99                 ) || usage();
100
101                 if (! $config{setup} && ! $config{render}) {
102                         loadplugins();
103                         if (@ARGV == 2) {
104                                 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
105                                 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
106                                 checkconfig();
107                         }
108                         else {
109                                 usage() unless $config{dumpsetup};
110                         }
111                 }
112         }
113         else {
114                 # wrapper passes a full config structure in the environment
115                 # variable
116                 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
117                 if ($@) {
118                         error("WRAPPED_OPTIONS: $@");
119                 }
120                 delete $ENV{WRAPPED_OPTIONS};
121
122                 if ($config{post_commit} && ! commit_hook_enabled()) {
123                         # optimisation for no-op post_commit 
124                         exit 0;
125                 }
126                 elsif ($config{test_receive}) {
127                         # quick success if the user is trusted
128                         require IkiWiki::Receive;
129                         exit 0 if IkiWiki::Receive::trusted();
130                 }
131
132                 loadplugins();
133                 checkconfig();
134         }
135 } #}}}
136
137 sub main () { #{{{
138         getconfig();
139         
140         if ($config{setup}) {
141                 delete $config{setup};
142                 loadplugins();
143                 checkconfig();
144
145                 if (@{$config{wrappers}} && 
146                     ! $config{render} && ! $config{dumpsetup} &&
147                     (! $config{refresh} || $config{genwrappers})) {
148                         debug(gettext("generating wrappers.."));
149                         require IkiWiki::Wrapper;
150                         my %origconfig=(%config);
151                         foreach my $wrapper (@{$config{wrappers}}) {
152                                 %config=(%origconfig, %{$wrapper});
153                                 $config{verbose}=$config{setupverbose}
154                                         if exists $config{setupverbose};
155                                 $config{syslog}=$config{setupsyslog}
156                                         if exists $config{setupsyslog};
157                                 delete @config{qw(setupsyslog setupverbose wrappers genwrappers rebuild)};
158                                 checkconfig();
159                                 if (! $config{cgi} && ! $config{post_commit} &&
160                                     ! $config{test_receive}) {
161                                         $config{post_commit}=1;
162                                 }
163                                 gen_wrapper();
164                         }
165                         %config=(%origconfig);
166                 }
167                 
168                 # setup implies a wiki rebuild by default
169                 if (! $config{refresh}) {
170                         $config{rebuild}=1;
171                 }
172         }
173
174         if ($config{dumpsetup}) {
175                 $config{srdir}=$config{destdir}="";
176                 require IkiWiki::Setup;
177                 IkiWiki::Setup::dump($config{dumpsetup});
178         }
179         elsif ($config{wrapper}) {
180                 lockwiki();
181                 require IkiWiki::Wrapper;
182                 gen_wrapper();
183         }
184         elsif ($config{cgi}) {
185                 require IkiWiki::CGI;
186                 eval {cgi()};
187                 if ($@) {
188                         cgierror($@);
189                 }
190         }
191         elsif ($config{render}) {
192                 require IkiWiki::Render;
193                 commandline_render();
194         }
195         elsif ($config{post_commit} && ! commit_hook_enabled()) {
196                 # do nothing
197         }
198         elsif ($config{test_receive}) {
199                 require IkiWiki::Receive;
200                 IkiWiki::Receive::test();
201         }
202         else {
203                 if ($config{rebuild}) {
204                         debug(gettext("rebuilding wiki.."));
205                 }
206                 else {
207                         debug(gettext("refreshing wiki.."));
208                 }
209                 lockwiki();
210                 loadindex();
211                 require IkiWiki::Render;
212                 rcs_update();
213                 refresh();
214                 saveindex();
215                 debug(gettext("done"));
216         }
217 } #}}}
218
219 main;