munge to avoid markdown eating email addresses
[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                         "adminuser=s@" => sub {
69                                 push @{$config{adminuser}}, $_[1]
70                         },
71                         "templatedir=s" => sub {
72                                 $config{templatedir}=possibly_foolish_untaint($_[1])
73                         },
74                         "underlaydir=s" => sub {
75                                 $config{underlaydir}=possibly_foolish_untaint($_[1])
76                         },
77                         "wrapper:s" => sub {
78                                 $config{wrapper}=$_[1] ? possibly_foolish_untaint($_[1]) : "ikiwiki-wrap"
79                         },
80                         "wrappermode=i" => sub {
81                                 $config{wrappermode}=possibly_foolish_untaint($_[1])
82                         },
83                         "plugin=s@" => sub {
84                                 push @{$config{add_plugins}}, $_[1];
85                         },
86                         "disable-plugin=s@" => sub {
87                                 push @{$config{disable_plugins}}, $_[1];
88                         },
89                         "set=s" => sub {
90                                 my ($var, $val)=split('=', $_[1], 2);
91                                 if (! defined $var || ! defined $val) {
92                                         die gettext("usage: --set var=value"), "\n";
93                                 }
94                                 $config{$var}=$val;
95                         },
96                         "version" => sub {
97                                 print "ikiwiki version $IkiWiki::version\n";
98                                 exit;
99                         },
100                         "help|h" => sub { $SIG{__WARN__}=sub {}; die },
101                 ) || usage();
102
103                 if (! $config{setup}) {
104                         loadplugins();
105                         if (@ARGV == 2) {
106                                 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
107                                 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
108                                 checkconfig();
109                         }
110                         else {
111                                 usage() unless $config{dumpsetup};
112                         }
113                 }
114         }
115         else {
116                 # wrapper passes a full config structure in the environment
117                 # variable
118                 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
119                 if ($@) {
120                         error("WRAPPED_OPTIONS: $@");
121                 }
122                 delete $ENV{WRAPPED_OPTIONS};
123
124                 loadplugins();
125                 checkconfig();
126         }
127 }
128
129 sub main () {
130         getconfig();
131         
132         if ($config{setup}) {
133                 delete $config{setup};
134                 loadplugins();
135                 checkconfig();
136
137                 if (@{$config{wrappers}} && 
138                     ! $config{render} && ! $config{dumpsetup} &&
139                     ! $config{clean} &&
140                     ((! $config{refresh} && ! $config{post_commit})
141                      || $config{genwrappers})) {
142                         debug(gettext("generating wrappers.."));
143                         require IkiWiki::Wrapper;
144                         my %origconfig=(%config);
145                         foreach my $wrapper (@{$config{wrappers}}) {
146                                 %config=(%origconfig, %{$wrapper});
147                                 $config{verbose}=$config{setupverbose}
148                                         if exists $config{setupverbose};
149                                 $config{syslog}=$config{setupsyslog}
150                                         if exists $config{setupsyslog};
151                                 delete @config{qw(setupsyslog setupverbose wrappers genwrappers rebuild)};
152                                 checkconfig();
153                                 if (! $config{cgi} && ! $config{post_commit} &&
154                                     ! $config{test_receive}) {
155                                         $config{post_commit}=1;
156                                 }
157                                 gen_wrapper();
158                         }
159                         %config=(%origconfig);
160                 }
161                 
162                 # setup implies a wiki rebuild by default
163                 if (! $config{refresh} && ! $config{render} &&
164                     ! $config{post_commit} && ! $config{clean}) {
165                         $config{rebuild}=1;
166                 }
167         }
168
169         if ($config{dumpsetup}) {
170                 $config{srcdir}="" if ! defined $config{srcdir};
171                 $config{destdir}="" if ! defined $config{destdir};
172                 $config{syslog}=1 if $config{setupsyslog};
173                 require IkiWiki::Setup;
174                 IkiWiki::Setup::dump($config{dumpsetup});
175         }
176         elsif ($config{wrapper}) {
177                 lockwiki();
178                 require IkiWiki::Wrapper;
179                 gen_wrapper();
180         }
181         elsif ($config{cgi}) {
182                 require IkiWiki::CGI;
183                 eval {cgi()};
184                 if ($@) {
185                         cgierror($@);
186                 }
187         }
188         elsif ($config{render}) {
189                 require IkiWiki::Render;
190                 commandline_render();
191         }
192         elsif ($config{post_commit} && ! commit_hook_enabled()) {
193                 # do nothing
194         }
195         elsif ($config{clean}) {
196                 require IkiWiki::Render;
197                 foreach my $wrapper (@{$config{wrappers}}) {
198                         prune($wrapper->{wrapper});
199                 }
200                 clean_rendered();
201                 system("rm", "-rf", $config{wikistatedir});
202         }
203         else {
204                 if ($config{rebuild}) {
205                         debug(gettext("rebuilding wiki.."));
206                 }
207                 else {
208                         debug(gettext("refreshing wiki.."));
209                 }
210                 lockwiki();
211                 loadindex();
212                 require IkiWiki::Render;
213                 rcs_update();
214                 refresh();
215                 saveindex();
216                 debug(gettext("done"));
217         }
218 }
219
220 main;