No more Discussion Pages of Discussion Pages of...
[ikiwiki.git] / 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{getctime},
48                         "numbacklinks=i" => \$config{numbacklinks},
49                         "rcs=s" => \$config{rcs},
50                         "no-rcs" => sub { $config{rcs}="" },
51                         "cgi!" => \$config{cgi},
52                         "discussion!" => \$config{discussion},
53                         "w3mmode!" => \$config{w3mmode},
54                         "url=s" => \$config{url},
55                         "cgiurl=s" => \$config{cgiurl},
56                         "historyurl=s" => \$config{historyurl},
57                         "diffurl=s" => \$config{diffurl},
58                         "svnpath" => \$config{svnpath},
59                         "adminemail=s" => \$config{adminemail},
60                         "timeformat=s" => \$config{timeformat},
61                         "sslcookie!" => \$config{sslcookie},
62                         "userdir=s" => \$config{userdir},
63                         "htmlext=s" => \$config{htmlext},
64                         "libdir=s" => \$config{libdir},
65                         "exclude=s@" => sub {
66                                 push @{$config{wiki_file_prune_regexps}}, $_[1];
67                         },
68                         "include=s@" => sub {
69                                 $config{include}=defined $config{include} && length $config{include} ? "$config{include}|$_[1]" : $_[1];
70                         },
71                         "adminuser=s@" => sub {
72                                 push @{$config{adminuser}}, $_[1]
73                         },
74                         "templatedir=s" => sub {
75                                 $config{templatedir}=possibly_foolish_untaint($_[1])
76                         },
77                         "underlaydir=s" => sub {
78                                 $config{underlaydir}=possibly_foolish_untaint($_[1])
79                         },
80                         "wrapper:s" => sub {
81                                 $config{wrapper}=$_[1] ? possibly_foolish_untaint($_[1]) : "ikiwiki-wrap"
82                         },
83                         "wrappermode=i" => sub {
84                                 $config{wrappermode}=possibly_foolish_untaint($_[1])
85                         },
86                         "plugin=s@" => sub {
87                                 push @{$config{add_plugins}}, $_[1];
88                         },
89                         "disable-plugin=s@" => sub {
90                                 push @{$config{disable_plugins}}, $_[1];
91                         },
92                         "set=s" => sub {
93                                 my ($var, $val)=split('=', $_[1], 2);
94                                 if (! defined $var || ! defined $val) {
95                                         die gettext("usage: --set var=value"), "\n";
96                                 }
97                                 $config{$var}=$val;
98                         },
99                         "version" => sub {
100                                 print "ikiwiki version $IkiWiki::version\n";
101                                 exit;
102                         },
103                         "help|h" => sub { $SIG{__WARN__}=sub {}; die },
104                 ) || usage();
105
106                 if (! $config{setup}) {
107                         loadplugins();
108                         if (@ARGV == 2) {
109                                 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
110                                 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
111                                 checkconfig();
112                         }
113                         else {
114                                 usage() unless $config{dumpsetup};
115                         }
116                 }
117         }
118         else {
119                 # wrapper passes a full config structure in the environment
120                 # variable
121                 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
122                 if ($@) {
123                         error("WRAPPED_OPTIONS: $@");
124                 }
125                 delete $ENV{WRAPPED_OPTIONS};
126
127                 loadplugins();
128                 checkconfig();
129         }
130 }
131
132 sub main () {
133         getconfig();
134         
135         if ($config{setup}) {
136                 delete $config{setup};
137                 loadplugins();
138                 checkconfig();
139
140                 if (@{$config{wrappers}} && 
141                     ! $config{render} && ! $config{dumpsetup} &&
142                     ! $config{clean} &&
143                     ((! $config{refresh} && ! $config{post_commit})
144                      || $config{genwrappers})) {
145                         debug(gettext("generating wrappers.."));
146                         require IkiWiki::Wrapper;
147                         my %origconfig=(%config);
148                         foreach my $wrapper (@{$config{wrappers}}) {
149                                 %config=(%origconfig, %{$wrapper});
150                                 $config{verbose}=$config{setupverbose}
151                                         if exists $config{setupverbose};
152                                 $config{syslog}=$config{setupsyslog}
153                                         if exists $config{setupsyslog};
154                                 delete @config{qw(setupsyslog setupverbose wrappers genwrappers rebuild)};
155                                 checkconfig();
156                                 if (! $config{cgi} && ! $config{post_commit} &&
157                                     ! $config{test_receive}) {
158                                         $config{post_commit}=1;
159                                 }
160                                 gen_wrapper();
161                         }
162                         %config=(%origconfig);
163                 }
164                 
165                 # setup implies a wiki rebuild by default
166                 if (! $config{refresh} && ! $config{render} &&
167                     ! $config{post_commit} && ! $config{clean}) {
168                         $config{rebuild}=1;
169                 }
170         }
171
172         if ($config{dumpsetup}) {
173                 $config{srcdir}="" if ! defined $config{srcdir};
174                 $config{destdir}="" if ! defined $config{destdir};
175                 $config{syslog}=1 if $config{setupsyslog};
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{clean}) {
199                 require IkiWiki::Render;
200                 foreach my $wrapper (@{$config{wrappers}}) {
201                         prune($wrapper->{wrapper});
202                 }
203                 clean_rendered();
204                 system("rm", "-rf", $config{wikistatedir});
205         }
206         else {
207                 if ($config{rebuild}) {
208                         debug(gettext("rebuilding wiki.."));
209                 }
210                 else {
211                         debug(gettext("refreshing wiki.."));
212                 }
213                 lockwiki();
214                 loadindex();
215                 require IkiWiki::Render;
216                 rcs_update();
217                 refresh();
218                 saveindex();
219                 debug(gettext("done"));
220         }
221 }
222
223 main;