* Split off an IkiWiki.pm out of ikiwiki and have all the other modules use
[ikiwiki.git] / ikiwiki
1 #!/usr/bin/perl -T
2 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
3
4 package IkiWiki;
5 use warnings;
6 use strict;
7 use lib '.'; # For use without installation, removed by Makefile.
8 use IkiWiki;
9
10 sub usage () { #{{{
11         die "usage: ikiwiki [options] source dest\n";
12 } #}}}
13
14 sub getconfig () { #{{{
15         if (! exists $ENV{WRAPPED_OPTIONS}) {
16                 %config=(
17                         wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$|\.rss$)},
18                         wiki_link_regexp => qr/\[\[(?:([^\s\]\|]+)\|)?([^\s\]]+)\]\]/,
19                         wiki_processor_regexp => qr/\[\[(\w+)\s+([^\]]*)\]\]/,
20                         wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
21                         verbose => 0,
22                         wikiname => "wiki",
23                         default_pageext => ".mdwn",
24                         cgi => 0,
25                         svn => 1,
26                         notify => 0,
27                         url => '',
28                         cgiurl => '',
29                         historyurl => '',
30                         diffurl => '',
31                         anonok => 0,
32                         rss => 0,
33                         sanitize => 1,
34                         rebuild => 0,
35                         refresh => 0,
36                         getctime => 0,
37                         hyperestraier => 0,
38                         wrapper => undef,
39                         wrappermode => undef,
40                         svnrepo => undef,
41                         svnpath => "trunk",
42                         srcdir => undef,
43                         destdir => undef,
44                         templatedir => "/usr/share/ikiwiki/templates",
45                         underlaydir => "/usr/share/ikiwiki/basewiki",
46                         setup => undef,
47                         adminuser => undef,
48                         adminemail => undef,
49                         plugin => [qw{inline}],
50                 );
51
52                 eval q{use Getopt::Long};
53                 GetOptions(
54                         "setup|s=s" => \$config{setup},
55                         "wikiname=s" => \$config{wikiname},
56                         "verbose|v!" => \$config{verbose},
57                         "rebuild!" => \$config{rebuild},
58                         "refresh!" => \$config{refresh},
59                         "getctime" => \$config{getctime},
60                         "wrappermode=i" => \$config{wrappermode},
61                         "svn!" => \$config{svn},
62                         "anonok!" => \$config{anonok},
63                         "hyperestraier" => \$config{hyperestraier},
64                         "rss!" => \$config{rss},
65                         "cgi!" => \$config{cgi},
66                         "notify!" => \$config{notify},
67                         "sanitize!" => \$config{sanitize},
68                         "url=s" => \$config{url},
69                         "cgiurl=s" => \$config{cgiurl},
70                         "historyurl=s" => \$config{historyurl},
71                         "diffurl=s" => \$config{diffurl},
72                         "svnrepo" => \$config{svnrepo},
73                         "svnpath" => \$config{svnpath},
74                         "adminemail=s" => \$config{adminemail},
75                         "exclude=s@" => sub {
76                                 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
77                         },
78                         "adminuser=s@" => sub {
79                                 push @{$config{adminuser}}, $_[1]
80                         },
81                         "templatedir=s" => sub {
82                                 $config{templatedir}=possibly_foolish_untaint($_[1])
83                         },
84                         "underlaydir=s" => sub {
85                                 $config{underlaydir}=possibly_foolish_untaint($_[1])
86                         },
87                         "wrapper:s" => sub {
88                                 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
89                         },
90                         "plugin=s@" => sub {
91                                 push @{$config{plugin}}, $_[1];
92                         }
93                 ) || usage();
94
95                 if (! $config{setup}) {
96                         usage() unless @ARGV == 2;
97                         $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
98                         $config{destdir} = possibly_foolish_untaint(shift @ARGV);
99                         checkconfig();
100                 }
101         }
102         else {
103                 # wrapper passes a full config structure in the environment
104                 # variable
105                 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
106                 checkconfig();
107         }
108 } #}}}
109
110 sub main () { #{{{
111         getconfig();
112         
113         if ($config{cgi}) {
114                 lockwiki();
115                 loadindex();
116                 require IkiWiki::CGI;
117                 cgi();
118         }
119         elsif ($config{setup}) {
120                 require IkiWiki::Setup;
121                 setup();
122         }
123         elsif ($config{wrapper}) {
124                 lockwiki();
125                 require IkiWiki::Wrapper;
126                 gen_wrapper();
127         }
128         else {
129                 lockwiki();
130                 loadindex();
131                 require IkiWiki::Render;
132                 rcs_update();
133                 rcs_getctime() if $config{getctime};
134                 refresh();
135                 rcs_notify() if $config{notify};
136                 saveindex();
137         }
138 } #}}}
139
140 main;