Fix bug in wikiname sanitisation in the setup automator.
[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
13 sub ask ($$) { #{{{
14         my ($question, $default)=@_;
15
16         my $r=Term::ReadLine->new("ikiwiki");
17         $r->readline($question." ", $default);
18 } #}}}
19
20 sub prettydir ($) { #{{{
21         my $dir=shift;
22         $dir=~s/^\Q$ENV{HOME}\E\//~\//;
23         return $dir;
24 } #}}}
25
26 sub import (@) { #{{{
27         my $this=shift;
28         IkiWiki::Setup::merge({@_});
29
30         # Sanitize this to avoid problimatic directory names.
31         $config{wikiname}=~s/[^-A-Za-z0-9_]//g;
32         if (! length $config{wikiname}) {
33                 error gettext("you must enter a wikiname (that contains alphanumerics)");
34         }
35
36         # Avoid overwriting any existing files.
37         foreach my $key (qw{srcdir destdir repository dumpsetup}) {
38                 next unless exists $config{$key};
39                 my $add="";
40                 while (-e $add.$config{$key}) {
41                         $add=1 if ! $add;
42                         $add++;
43                 }
44                 $config{$key}=$add.$config{$key};
45         }
46         
47         # Set up wrapper
48         if ($config{rcs}) {
49                 if ($config{rcs} eq 'git') {
50                         $config{git_wrapper}=$config{repository}."/hooks/post-update";
51                 }
52                 elsif ($config{rcs} eq 'svn') {
53                         $config{svn_wrapper}=$config{repository}."/hooks/post-commit";
54                 }
55                 elsif ($config{rcs} eq 'bzr') {
56                         # TODO
57                 }
58                 elsif ($config{rcs} eq 'mercurial') {
59                         # TODO
60                 }
61                 else {
62                         error sprintf(gettext("unsupported revision control system %s"),
63                                 $config{rcs});
64                 }
65         }
66
67         IkiWiki::checkconfig();
68
69         print "\n\nSetting up $config{wikiname} ...\n";
70
71         # Set up the repository.
72         mkpath($config{srcdir}) || die "mkdir $config{srcdir}: $!";
73         delete $config{repository} if ! $config{rcs} || $config{rcs}=~/bzr|mercurial/;
74         if ($config{rcs}) {
75                 my @params=($config{rcs}, $config{srcdir});
76                 push @params, $config{repository} if exists $config{repository};
77                 if (system("ikiwiki-makerepo", @params) != 0) {
78                         error gettext("failed to set up the repository with ikiwiki-makerepo");
79                 }
80         }
81
82         # Generate setup file.
83         require IkiWiki::Setup;
84         IkiWiki::Setup::dump($config{dumpsetup});
85
86         # Build the wiki, but w/o wrappers, so it's not live yet.
87         mkpath($config{destdir}) || die "mkdir $config{destdir}: $!";
88         if (system("ikiwiki", "--refresh", "--setup", $config{dumpsetup}) != 0) {
89                 die "ikiwiki --refresh --setup $config{dumpsetup} failed";
90         }
91
92         # Create admin user(s).
93         foreach my $admin (@{$config{adminuser}}) {
94                 next if $admin=~/^http\?:\/\//; # openid
95                 
96                 # Prompt for password w/o echo.
97                 system('stty -echo 2>/dev/null');
98                 local $|=1;
99                 print "\n\nCreating wiki admin $admin ...\n";
100                 print "Choose a password: ";
101                 chomp(my $password=<STDIN>);
102                 print "\n\n\n";
103                 system('stty sane 2>/dev/null');
104
105                 if (IkiWiki::userinfo_setall($admin, { regdate => time }) &&
106                     IkiWiki::Plugin::passwordauth::setpassword($admin, $password)) {
107                         IkiWiki::userinfo_set($admin, "email", $config{adminemail}) if defined $config{adminemail};
108                 }
109                 else {
110                         error("problem setting up $admin user");
111                 }
112         }
113         
114         # Add wrappers, make live.
115         if (system("ikiwiki", "--wrappers", "--setup", $config{dumpsetup}) != 0) {
116                 die "ikiwiki --wrappers --setup $config{dumpsetup} failed";
117         }
118
119         # Add it to the wikilist.
120         mkpath("$ENV{HOME}/.ikiwiki");
121         open (WIKILIST, ">>$ENV{HOME}/.ikiwiki/wikilist") || die "$ENV{HOME}/.ikiwiki/wikilist: $!";
122         print WIKILIST "$ENV{USER} $config{dumpsetup}\n";
123         close WIKILIST;
124         if (system("ikiwiki-update-wikilist") != 0) {
125                 print STDERR "** Failed to add you to the system wikilist file.\n";
126                 print STDERR "** (Probably ikiwiki-update-wikilist is not SUID root.)\n";
127                 print STDERR "** Your wiki will not be automatically updated when ikiwiki is upgraded.\n";
128         }
129         
130         # Done!
131         print "\n\nSuccessfully set up $config{wikiname}:\n";
132         foreach my $key (qw{url srcdir destdir repository}) {
133                 next unless exists $config{$key};
134                 print "\t$key: ".(" " x (10 - length($key)))." ".
135                         prettydir($config{$key})."\n";
136         }
137         print "To modify settings, edit ".prettydir($config{dumpsetup})." and then run:\n";
138         print " ikiwiki -setup ".prettydir($config{dumpsetup})."\n";
139         exit 0;
140 } #}}}
141
142 1