answer about the `%pagestate` of autopages.
[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                         "set-yaml=s" => sub {
100                                 my ($var, $val)=split('=', $_[1], 2);
101                                 if (! defined $var || ! defined $val) {
102                                         die gettext("usage: --set-yaml var=value"), "\n";
103                                 }
104                                 eval q{use YAML::Any};
105                                 eval q{use YAML} if $@;
106                                 die $@ if $@;
107                                 eval q{$YAML::Syck::ImplicitUnicode=1};
108                                 $config{$var}=Load($val."\n");
109                         },
110                         "version" => sub {
111                                 print "ikiwiki version $IkiWiki::version\n";
112                                 exit;
113                         },
114                         "help|h" => sub { $SIG{__WARN__}=sub {}; die },
115                 ) || usage();
116
117                 if (! $config{setup}) {
118                         loadplugins();
119                         if (@ARGV == 2) {
120                                 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
121                                 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
122                                 checkconfig();
123                         }
124                         else {
125                                 usage() unless $config{dumpsetup};
126                         }
127                 }
128         }
129         else {
130                 # wrapper passes a full config structure in the environment
131                 # variable
132                 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
133                 if ($@) {
134                         error("WRAPPED_OPTIONS: $@");
135                 }
136                 delete $ENV{WRAPPED_OPTIONS};
137
138                 loadplugins();
139                 checkconfig();
140         }
141 }
142
143 sub main () {
144         getconfig();
145         
146         if ($config{setup}) {
147                 delete $config{setup};
148                 loadplugins();
149                 checkconfig();
150
151                 if (@{$config{wrappers}} && 
152                     ! $config{render} && ! $config{dumpsetup} &&
153                     ! $config{clean} &&
154                     ((! $config{refresh} && ! $config{post_commit})
155                      || $config{genwrappers})) {
156                         debug(gettext("generating wrappers.."));
157                         require IkiWiki::Wrapper;
158                         my %origconfig=(%config);
159                         foreach my $wrapper (@{$config{wrappers}}) {
160                                 %config=(%origconfig, %{$wrapper});
161                                 $config{verbose}=$config{setupverbose}
162                                         if exists $config{setupverbose};
163                                 $config{syslog}=$config{setupsyslog}
164                                         if exists $config{setupsyslog};
165                                 delete @config{qw(setupsyslog setupverbose wrappers genwrappers rebuild)};
166                                 checkconfig();
167                                 if (! $config{cgi} && ! $config{post_commit} &&
168                                     ! $config{test_receive}) {
169                                         $config{post_commit}=1;
170                                 }
171                                 gen_wrapper();
172                         }
173                         %config=(%origconfig);
174                 }
175                 
176                 # setup implies a wiki rebuild by default
177                 if (! $config{refresh} && ! $config{render} &&
178                     ! $config{post_commit} && ! $config{clean}) {
179                         $config{rebuild}=1;
180                 }
181         }
182
183         if ($config{dumpsetup}) {
184                 $config{srcdir}="" if ! defined $config{srcdir};
185                 $config{destdir}="" if ! defined $config{destdir};
186                 $config{syslog}=1 if $config{setupsyslog};
187                 require IkiWiki::Setup;
188                 IkiWiki::Setup::dump($config{dumpsetup});
189         }
190         elsif ($config{wrapper}) {
191                 lockwiki();
192                 require IkiWiki::Wrapper;
193                 gen_wrapper();
194         }
195         elsif ($config{cgi}) {
196                 require IkiWiki::CGI;
197                 eval {cgi()};
198                 if ($@) {
199                         cgierror($@);
200                 }
201         }
202         elsif ($config{render}) {
203                 require IkiWiki::Render;
204                 commandline_render();
205         }
206         elsif ($config{post_commit} && ! commit_hook_enabled()) {
207                 # do nothing
208         }
209         elsif ($config{clean}) {
210                 require IkiWiki::Render;
211                 foreach my $wrapper (@{$config{wrappers}}) {
212                         prune($wrapper->{wrapper});
213                 }
214                 clean_rendered();
215                 system("rm", "-rf", $config{wikistatedir});
216         }
217         else {
218                 if ($config{rebuild}) {
219                         debug(gettext("rebuilding wiki.."));
220                 }
221                 else {
222                         debug(gettext("refreshing wiki.."));
223                 }
224                 lockwiki();
225                 loadindex();
226                 require IkiWiki::Render;
227                 rcs_update();
228                 refresh();
229                 saveindex();
230                 debug(gettext("done"));
231         }
232 }
233
234 main;