poll vote (Accept both)
[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::XS; use Encode};
112                                 die $@ if $@;
113                                 $config{$var}=Load(encode_utf8($val)."\n");
114                         },
115                         "version" => sub {
116                                 print "ikiwiki version $IkiWiki::version\n";
117                                 exit;
118                         },
119                         "help|h" => sub { $SIG{__WARN__}=sub {}; die },
120                 ) || usage();
121
122                 if (! $config{setup}) {
123                         loadplugins();
124                         if (@ARGV == 2) {
125                                 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
126                                 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
127                                 checkconfig();
128                         }
129                         else {
130                                 usage() unless $config{dumpsetup};
131                         }
132                 }
133         }
134         else {
135                 # wrapper passes a full config structure in the environment
136                 # variable
137                 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
138                 if ($@) {
139                         error("WRAPPED_OPTIONS: $@");
140                 }
141                 delete $ENV{WRAPPED_OPTIONS};
142
143                 loadplugins();
144                 checkconfig();
145         }
146 }
147
148 sub main () {
149         getconfig();
150         
151         if ($config{setup}) {
152                 delete $config{setup};
153                 loadplugins();
154                 checkconfig();
155
156                 if (@{$config{wrappers}} && 
157                     ! $config{render} && ! $config{dumpsetup} &&
158                     ! $config{clean} &&
159                     ((! $config{refresh} && ! $config{post_commit})
160                      || $config{genwrappers})) {
161                         require IkiWiki::Wrapper;
162                         gen_wrappers();
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{changesetup}) {
173                 require IkiWiki::Setup;
174                 IkiWiki::Setup::dump($config{changesetup});
175         }
176
177         if ($config{dumpsetup}) {
178                 $config{srcdir}="" if ! defined $config{srcdir};
179                 $config{destdir}="" if ! defined $config{destdir};
180                 $config{syslog}=1 if $config{setupsyslog};
181                 checkconfig();
182                 require IkiWiki::Setup;
183                 IkiWiki::Setup::dump($config{dumpsetup});
184         }
185         elsif ($config{wrapper}) {
186                 lockwiki();
187                 require IkiWiki::Wrapper;
188                 gen_wrapper();
189         }
190         elsif ($config{cgi}) {
191                 require IkiWiki::CGI;
192                 eval {cgi()};
193                 if ($@) {
194                         cgierror($@);
195                 }
196         }
197         elsif ($config{render}) {
198                 require IkiWiki::Render;
199                 commandline_render();
200         }
201         elsif ($config{post_commit} && ! commit_hook_enabled()) {
202                 # do nothing
203         }
204         elsif ($config{clean}) {
205                 require IkiWiki::Render;
206                 foreach my $wrapper (@{$config{wrappers}}) {
207                         prune($wrapper->{wrapper});
208                 }
209                 clean_rendered();
210                 system("rm", "-rf", $config{wikistatedir});
211         }
212         else {
213                 if ($config{rebuild}) {
214                         debug(gettext("rebuilding wiki.."));
215                 }
216                 else {
217                         debug(gettext("refreshing wiki.."));
218                 }
219                 lockwiki();
220                 loadindex();
221                 require IkiWiki::Render;
222                 rcs_update();
223                 refresh();
224                 saveindex();
225                 debug(gettext("done"));
226         }
227 }
228
229 main;