close
[ikiwiki] / ikiwiki.in
1 #!/usr/bin/perl
2 package IkiWiki;
3
4 use warnings;
5 use strict;
6 use lib '.'; # For use in nonstandard directory, munged by Makefile.
7 use IkiWiki 3.00;
8
9 sub usage () {
10         die gettext("usage: ikiwiki [options] source dest"), "\n",
11             gettext("       ikiwiki --setup configfile"), "\n";
12 }
13
14 sub setup (@) {
15         require IkiWiki::Setup;
16         my $verbose=$config{verbose};
17         my $syslog=$config{syslog};
18         IkiWiki::Setup::load($_[1]);
19         $config{setupverbose}=$config{verbose};
20         $config{setupsyslog}=$config{syslog};
21         $config{verbose}=$verbose || $config{setupverbose};
22         $config{syslog}=$syslog;
23         $config{setup}=1;
24 }
25
26 sub getconfig () {
27         if (! exists $ENV{WRAPPED_OPTIONS}) {
28                 %config=defaultconfig();
29                 eval q{use Getopt::Long};
30                 Getopt::Long::Configure('pass_through');
31                 GetOptions(
32                         "setup|s=s" => \&setup,
33                         "dumpsetup|s=s" => \$config{dumpsetup},
34                         "changesetup|s=s" => sub {
35                                 $config{changesetup}=$_[1];
36                                 $config{genwrappers}=1;
37                                 $config{refresh}=1;
38                                 setup(@_);
39                         },
40                         "wikiname=s" => \$config{wikiname},
41                         "verbose|v!" => \$config{verbose},
42                         "syslog!" => \$config{syslog},
43                         "rebuild!" => \$config{rebuild},
44                         "refresh!" => \$config{refresh},
45                         "clean!" => \$config{clean},
46                         "post-commit" => \$config{post_commit},
47                         "render=s" => \$config{render},
48                         "wrappers!" => \$config{genwrappers},
49                         "wrappergroup=s" => \$config{wrappergroup},
50                         "usedirs!" => \$config{usedirs},
51                         "prefix-directives!" => \$config{prefix_directives},
52                         "getctime" => \$config{gettime},
53                         "gettime!" => \$config{gettime},
54                         "numbacklinks=i" => \$config{numbacklinks},
55                         "rcs=s" => \$config{rcs},
56                         "no-rcs" => sub { $config{rcs}="" },
57                         "cgi!" => \$config{cgi},
58                         "discussion!" => \$config{discussion},
59                         "w3mmode!" => \$config{w3mmode},
60                         "url=s" => \$config{url},
61                         "cgiurl=s" => \$config{cgiurl},
62                         "historyurl=s" => \$config{historyurl},
63                         "diffurl=s" => \$config{diffurl},
64                         "svnpath" => \$config{svnpath},
65                         "adminemail=s" => \$config{adminemail},
66                         "timeformat=s" => \$config{timeformat},
67                         "sslcookie!" => \$config{sslcookie},
68                         "userdir=s" => \$config{userdir},
69                         "htmlext=s" => \$config{htmlext},
70                         "libdir=s" => \$config{libdir},
71                         "exclude=s@" => sub {
72                                 push @{$config{wiki_file_prune_regexps}}, $_[1];
73                         },
74                         "include=s@" => sub {
75                                 $config{include}=defined $config{include} && length $config{include} ? "$config{include}|$_[1]" : $_[1];
76                         },
77                         "adminuser=s@" => sub {
78                                 push @{$config{adminuser}}, $_[1]
79                         },
80                         "templatedir=s" => sub {
81                                 $config{templatedir}=possibly_foolish_untaint($_[1])
82                         },
83                         "underlaydir=s" => sub {
84                                 $config{underlaydir}=possibly_foolish_untaint($_[1])
85                         },
86                         "wrapper:s" => sub {
87                                 $config{wrapper}=$_[1] ? possibly_foolish_untaint($_[1]) : "ikiwiki-wrap"
88                         },
89                         "wrappermode=i" => sub {
90                                 $config{wrappermode}=possibly_foolish_untaint($_[1])
91                         },
92                         "plugin=s@" => sub {
93                                 push @{$config{add_plugins}}, $_[1]
94                                         unless grep { $_ eq $_[1] } @{$config{add_plugins}};
95                         },
96                         "disable-plugin=s@" => sub {
97                                 push @{$config{disable_plugins}}, $_[1];
98                         },
99                         "set=s" => sub {
100                                 my ($var, $val)=split('=', $_[1], 2);
101                                 if (! defined $var || ! defined $val) {
102                                         die gettext("usage: --set var=value"), "\n";
103                                 }
104                                 $config{$var}=$val;
105                         },
106                         "set-yaml=s" => sub {
107                                 my ($var, $val)=split('=', $_[1], 2);
108                                 if (! defined $var || ! defined $val) {
109                                         die gettext("usage: --set-yaml var=value"), "\n";
110                                 }
111                                 eval q{use YAML::Any};
112                                 eval q{use YAML} if $@;
113                                 die $@ if $@;
114                                 eval q{$YAML::Syck::ImplicitUnicode=1};
115                                 $config{$var}=Load($val."\n");
116                         },
117                         "version" => sub {
118                                 print "ikiwiki version $IkiWiki::version\n";
119                                 exit;
120                         },
121                         "help|h" => sub { $SIG{__WARN__}=sub {}; die },
122                 ) || usage();
123
124                 if (! $config{setup}) {
125                         loadplugins();
126                         if (@ARGV == 2) {
127                                 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
128                                 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
129                                 checkconfig();
130                         }
131                         else {
132                                 usage() unless $config{dumpsetup};
133                         }
134                 }
135         }
136         else {
137                 # wrapper passes a full config structure in the environment
138                 # variable
139                 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
140                 if ($@) {
141                         error("WRAPPED_OPTIONS: $@");
142                 }
143                 delete $ENV{WRAPPED_OPTIONS};
144
145                 loadplugins();
146                 checkconfig();
147         }
148 }
149
150 sub main () {
151         getconfig();
152         
153         if ($config{setup}) {
154                 delete $config{setup};
155                 loadplugins();
156                 checkconfig();
157
158                 if (@{$config{wrappers}} && 
159                     ! $config{render} && ! $config{dumpsetup} &&
160                     ! $config{clean} &&
161                     ((! $config{refresh} && ! $config{post_commit})
162                      || $config{genwrappers})) {
163                         require IkiWiki::Wrapper;
164                         gen_wrappers();
165                 }
166                 
167                 # setup implies a wiki rebuild by default
168                 if (! $config{refresh} && ! $config{render} &&
169                     ! $config{post_commit} && ! $config{clean}) {
170                         $config{rebuild}=1;
171                 }
172         }
173
174         if ($config{changesetup}) {
175                 require IkiWiki::Setup;
176                 IkiWiki::Setup::dump($config{changesetup});
177         }
178
179         if ($config{dumpsetup}) {
180                 $config{srcdir}="" if ! defined $config{srcdir};
181                 $config{destdir}="" if ! defined $config{destdir};
182                 $config{syslog}=1 if $config{setupsyslog};
183                 require IkiWiki::Setup;
184                 IkiWiki::Setup::dump($config{dumpsetup});
185         }
186         elsif ($config{wrapper}) {
187                 lockwiki();
188                 require IkiWiki::Wrapper;
189                 gen_wrapper();
190         }
191         elsif ($config{cgi}) {
192                 require IkiWiki::CGI;
193                 eval {cgi()};
194                 if ($@) {
195                         cgierror($@);
196                 }
197         }
198         elsif ($config{render}) {
199                 require IkiWiki::Render;
200                 commandline_render();
201         }
202         elsif ($config{post_commit} && ! commit_hook_enabled()) {
203                 # do nothing
204         }
205         elsif ($config{clean}) {
206                 require IkiWiki::Render;
207                 foreach my $wrapper (@{$config{wrappers}}) {
208                         prune($wrapper->{wrapper});
209                 }
210                 clean_rendered();
211                 system("rm", "-rf", $config{wikistatedir});
212         }
213         else {
214                 if ($config{rebuild}) {
215                         debug(gettext("rebuilding wiki.."));
216                 }
217                 else {
218                         debug(gettext("refreshing wiki.."));
219                 }
220                 lockwiki();
221                 loadindex();
222                 require IkiWiki::Render;
223                 rcs_update();
224                 refresh();
225                 saveindex();
226                 debug(gettext("done"));
227         }
228 }
229
230 main;