]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - ikiwiki.in
1758399ea83e0380a4cc90337c5a3edfe0135327
[git.ikiwiki.info.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}};
5 package IkiWiki;
7 use warnings;
8 use strict;
9 use lib '.'; # For use in nonstandard directory, munged by Makefile.
10 use IkiWiki;
12 sub usage () {
13         die gettext("usage: ikiwiki [options] source dest"), "\n",
14             gettext("       ikiwiki --setup configfile"), "\n";
15 }
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                                 $config{$var}=Load($val."\n");
108                         },
109                         "version" => sub {
110                                 print "ikiwiki version $IkiWiki::version\n";
111                                 exit;
112                         },
113                         "help|h" => sub { $SIG{__WARN__}=sub {}; die },
114                 ) || usage();
116                 if (! $config{setup}) {
117                         loadplugins();
118                         if (@ARGV == 2) {
119                                 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
120                                 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
121                                 checkconfig();
122                         }
123                         else {
124                                 usage() unless $config{dumpsetup};
125                         }
126                 }
127         }
128         else {
129                 # wrapper passes a full config structure in the environment
130                 # variable
131                 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
132                 if ($@) {
133                         error("WRAPPED_OPTIONS: $@");
134                 }
135                 delete $ENV{WRAPPED_OPTIONS};
137                 loadplugins();
138                 checkconfig();
139         }
142 sub main () {
143         getconfig();
144         
145         if ($config{setup}) {
146                 delete $config{setup};
147                 loadplugins();
148                 checkconfig();
150                 if (@{$config{wrappers}} && 
151                     ! $config{render} && ! $config{dumpsetup} &&
152                     ! $config{clean} &&
153                     ((! $config{refresh} && ! $config{post_commit})
154                      || $config{genwrappers})) {
155                         debug(gettext("generating wrappers.."));
156                         require IkiWiki::Wrapper;
157                         my %origconfig=(%config);
158                         foreach my $wrapper (@{$config{wrappers}}) {
159                                 %config=(%origconfig, %{$wrapper});
160                                 $config{verbose}=$config{setupverbose}
161                                         if exists $config{setupverbose};
162                                 $config{syslog}=$config{setupsyslog}
163                                         if exists $config{setupsyslog};
164                                 delete @config{qw(setupsyslog setupverbose wrappers genwrappers rebuild)};
165                                 checkconfig();
166                                 if (! $config{cgi} && ! $config{post_commit} &&
167                                     ! $config{test_receive}) {
168                                         $config{post_commit}=1;
169                                 }
170                                 gen_wrapper();
171                         }
172                         %config=(%origconfig);
173                 }
174                 
175                 # setup implies a wiki rebuild by default
176                 if (! $config{refresh} && ! $config{render} &&
177                     ! $config{post_commit} && ! $config{clean}) {
178                         $config{rebuild}=1;
179                 }
180         }
182         if ($config{dumpsetup}) {
183                 $config{srcdir}="" if ! defined $config{srcdir};
184                 $config{destdir}="" if ! defined $config{destdir};
185                 $config{syslog}=1 if $config{setupsyslog};
186                 require IkiWiki::Setup;
187                 IkiWiki::Setup::dump($config{dumpsetup});
188         }
189         elsif ($config{wrapper}) {
190                 lockwiki();
191                 require IkiWiki::Wrapper;
192                 gen_wrapper();
193         }
194         elsif ($config{cgi}) {
195                 require IkiWiki::CGI;
196                 eval {cgi()};
197                 if ($@) {
198                         cgierror($@);
199                 }
200         }
201         elsif ($config{render}) {
202                 require IkiWiki::Render;
203                 commandline_render();
204         }
205         elsif ($config{post_commit} && ! commit_hook_enabled()) {
206                 # do nothing
207         }
208         elsif ($config{clean}) {
209                 require IkiWiki::Render;
210                 foreach my $wrapper (@{$config{wrappers}}) {
211                         prune($wrapper->{wrapper});
212                 }
213                 clean_rendered();
214                 system("rm", "-rf", $config{wikistatedir});
215         }
216         else {
217                 if ($config{rebuild}) {
218                         debug(gettext("rebuilding wiki.."));
219                 }
220                 else {
221                         debug(gettext("refreshing wiki.."));
222                 }
223                 lockwiki();
224                 loadindex();
225                 require IkiWiki::Render;
226                 rcs_update();
227                 refresh();
228                 saveindex();
229                 debug(gettext("done"));
230         }
233 main;