auto.setup, auto-blog.setup: Fix sanitization of entered wikiname. Closes: #547378
[ikiwiki] / IkiWiki / Setup / Automator.pm
1 #!/usr/bin/perl
2 # Ikiwiki setup automator.
3
4 package IkiWiki::Setup::Automator;
5
6 use warnings;
7 use strict;
8 use IkiWiki;
9 use IkiWiki::UserInfo;
10 use Term::ReadLine;
11 use File::Path;
12 use Encode;
13
14 sub ask ($$) {
15         my ($question, $default)=@_;
16
17         my $r=Term::ReadLine->new("ikiwiki");
18         $r->readline(encode_utf8($question)." ", $default);
19 }
20
21 sub prettydir ($) {
22         my $dir=shift;
23         $dir=~s/^\Q$ENV{HOME}\E\//~\//;
24         return $dir;
25 }
26
27 sub sanitize_wikiname ($) {
28         my $wikiname=shift;
29
30         # Sanitize this to avoid problimatic directory names.
31         $wikiname=~s/[^-A-Za-z0-9_]//g;
32         if (! length $wikiname) {
33                 error gettext("you must enter a wikiname (that contains alphanumerics)");
34         }
35         return $wikiname;
36 }
37
38 sub import (@) {
39         my $this=shift;
40         IkiWiki::Setup::merge({@_});
41
42         # Avoid overwriting any existing files.
43         foreach my $key (qw{srcdir destdir repository dumpsetup}) {
44                 next unless exists $config{$key};
45                 my $add="";
46                 my $dir=IkiWiki::dirname($config{$key})."/";
47                 my $base=IkiWiki::basename($config{$key});
48                 while (-e $dir.$add.$base) {
49                         $add=1 if ! $add;
50                         $add++;
51                 }
52                 $config{$key}=$dir.$add.$base;
53         }
54         
55         # Set up wrapper
56         if ($config{rcs}) {
57                 if ($config{rcs} eq 'git') {
58                         $config{git_wrapper}=$config{repository}."/hooks/post-update";
59                 }
60                 elsif ($config{rcs} eq 'svn') {
61                         $config{svn_wrapper}=$config{repository}."/hooks/post-commit";
62                 }
63                 elsif ($config{rcs} eq 'monotone') {
64                         $config{mtn_wrapper}=$config{srcdir}."_MTN/ikiwiki-netsync-hook";
65                 }
66                 elsif ($config{rcs} eq 'darcs') {
67                         $config{darcs_wrapper}=$config{repository}."/_darcs/ikiwiki-wrapper";
68                 }
69                 elsif ($config{rcs} eq 'bzr') {
70                         # TODO
71                 }
72                 elsif ($config{rcs} eq 'mercurial') {
73                         # TODO
74                 }
75                 else {
76                         error sprintf(gettext("unsupported revision control system %s"),
77                                 $config{rcs});
78                 }
79         }
80
81         IkiWiki::checkconfig();
82
83         print "\n\nSetting up $config{wikiname} ...\n";
84
85         # Set up the srcdir.
86         mkpath($config{srcdir}) || die "mkdir $config{srcdir}: $!";
87         # Copy in example wiki.
88         if (exists $config{example}) {
89                 # cp -R is POSIX
90                 # Another reason not to use -a is so that pages such as blog
91                 # posts will not have old creation dates on this new wiki.
92                 system("cp -R $IkiWiki::installdir/share/ikiwiki/examples/$config{example}/* $config{srcdir}");
93                 delete $config{example};
94         }
95
96         # Set up the repository.
97         delete $config{repository} if ! $config{rcs} || $config{rcs}=~/bzr|mercurial/;
98         if ($config{rcs}) {
99                 my @params=($config{rcs}, $config{srcdir});
100                 push @params, $config{repository} if exists $config{repository};
101                 if (system("ikiwiki-makerepo", @params) != 0) {
102                         error gettext("failed to set up the repository with ikiwiki-makerepo");
103                 }
104         }
105
106         # Make sure that all the listed plugins can load
107         # and checkconfig is ok. If a plugin fails to work,
108         # remove it from the configuration and keep on truckin'.
109         my %bakconfig=%config; # checkconfig can modify %config so back up
110         if (! eval { IkiWiki::loadplugins(); IkiWiki::checkconfig() }) {
111                 foreach my $plugin (@{$config{default_plugins}}, @{$bakconfig{add_plugins}}) {
112                         eval {
113                                 # delete all hooks so that only this plugins's
114                                 # checkconfig will be run
115                                 %IkiWiki::hooks=();
116                                 IkiWiki::loadplugin($plugin);
117                                 IkiWiki::run_hooks(checkconfig => sub { shift->() });
118                         };
119                         if ($@) {
120                                 print STDERR sprintf(gettext("** Disabling plugin %s, since it is failing with this message:"),
121                                         $plugin)."\n";
122                                 print STDERR "$@\n";
123                                 push @{$bakconfig{disable_plugins}}, $plugin;
124                         }
125                 }
126         }
127         %config=%bakconfig;
128
129         # Generate setup file.
130         require IkiWiki::Setup;
131         IkiWiki::Setup::dump($config{dumpsetup});
132
133         # Build the wiki, but w/o wrappers, so it's not live yet.
134         mkpath($config{destdir}) || die "mkdir $config{destdir}: $!";
135         if (system("ikiwiki", "--refresh", "--setup", $config{dumpsetup}) != 0) {
136                 die "ikiwiki --refresh --setup $config{dumpsetup} failed";
137         }
138
139         # Create admin user(s).
140         foreach my $admin (@{$config{adminuser}}) {
141                 next if $admin=~/^http\?:\/\//; # openid
142                 
143                 # Prompt for password w/o echo.
144                 my ($password, $password2);
145                 system('stty -echo 2>/dev/null');
146                 local $|=1;
147                 print "\n\nCreating wiki admin $admin ...\n";
148                 for (;;) {
149                         print "Choose a password: ";
150                         chomp($password=<STDIN>);
151                         print "\n";
152                         print "Confirm password: ";
153                         chomp($password2=<STDIN>);
154
155                         last if $password2 eq $password;
156
157                         print "Password mismatch.\n\n";
158                 }
159                 print "\n\n\n";
160                 system('stty sane 2>/dev/null');
161
162                 if (IkiWiki::userinfo_setall($admin, { regdate => time }) &&
163                     IkiWiki::Plugin::passwordauth::setpassword($admin, $password)) {
164                         IkiWiki::userinfo_set($admin, "email", $config{adminemail}) if defined $config{adminemail};
165                 }
166                 else {
167                         error("problem setting up $admin user");
168                 }
169         }
170         
171         # Add wrappers, make live.
172         if (system("ikiwiki", "--wrappers", "--setup", $config{dumpsetup}) != 0) {
173                 die "ikiwiki --wrappers --setup $config{dumpsetup} failed";
174         }
175
176         # Add it to the wikilist.
177         mkpath("$ENV{HOME}/.ikiwiki");
178         open (WIKILIST, ">>$ENV{HOME}/.ikiwiki/wikilist") || die "$ENV{HOME}/.ikiwiki/wikilist: $!";
179         print WIKILIST "$ENV{USER} $config{dumpsetup}\n";
180         close WIKILIST;
181         if (system("ikiwiki-update-wikilist") != 0) {
182                 print STDERR "** Failed to add you to the system wikilist file.\n";
183                 print STDERR "** (Probably ikiwiki-update-wikilist is not SUID root.)\n";
184                 print STDERR "** Your wiki will not be automatically updated when ikiwiki is upgraded.\n";
185         }
186         
187         # Done!
188         print "\n\nSuccessfully set up $config{wikiname}:\n";
189         foreach my $key (qw{url srcdir destdir repository}) {
190                 next unless exists $config{$key};
191                 print "\t$key: ".(" " x (10 - length($key)))." ".
192                         prettydir($config{$key})."\n";
193         }
194         print "To modify settings, edit ".prettydir($config{dumpsetup})." and then run:\n";
195         print " ikiwiki -setup ".prettydir($config{dumpsetup})."\n";
196         exit 0;
197 }
198
199 1