]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Setup/Automator.pm
inline: Avoid showing edit links if page editing is disabled. (Sjoerd)
[ikiwiki.git] / 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                 elsif ($config{rcs} eq 'cvs') {
76                         $config{cvs_wrapper}=$config{repository}."/CVSROOT/post-commit";
77                 }
78                 else {
79                         error sprintf(gettext("unsupported revision control system %s"),
80                                 $config{rcs});
81                 }
82         }
83
84         IkiWiki::checkconfig();
85
86         print "\n\nSetting up $config{wikiname} ...\n";
87
88         # Set up the srcdir.
89         mkpath($config{srcdir}) || die "mkdir $config{srcdir}: $!";
90         # Copy in example wiki.
91         if (exists $config{example}) {
92                 # cp -R is POSIX
93                 # Another reason not to use -a is so that pages such as blog
94                 # posts will not have old creation dates on this new wiki.
95                 system("cp -R $IkiWiki::installdir/share/ikiwiki/examples/$config{example}/* $config{srcdir}");
96                 delete $config{example};
97         }
98
99         # Set up the repository.
100         delete $config{repository} if ! $config{rcs} || $config{rcs}=~/bzr|mercurial/;
101         if ($config{rcs}) {
102                 my @params=($config{rcs}, $config{srcdir});
103                 push @params, $config{repository} if exists $config{repository};
104                 if (system("ikiwiki-makerepo", @params) != 0) {
105                         error gettext("failed to set up the repository with ikiwiki-makerepo");
106                 }
107         }
108
109         # Make sure that all the listed plugins can load
110         # and checkconfig is ok. If a plugin fails to work,
111         # remove it from the configuration and keep on truckin'.
112         my %bakconfig=%config; # checkconfig can modify %config so back up
113         if (! eval { IkiWiki::loadplugins(); IkiWiki::checkconfig() }) {
114                 foreach my $plugin (@{$config{default_plugins}}, @{$bakconfig{add_plugins}}) {
115                         eval {
116                                 # delete all hooks so that only this plugins's
117                                 # checkconfig will be run
118                                 %IkiWiki::hooks=();
119                                 IkiWiki::loadplugin($plugin);
120                                 IkiWiki::run_hooks(checkconfig => sub { shift->() });
121                         };
122                         if ($@) {
123                                 print STDERR sprintf(gettext("** Disabling plugin %s, since it is failing with this message:"),
124                                         $plugin)."\n";
125                                 print STDERR "$@\n";
126                                 push @{$bakconfig{disable_plugins}}, $plugin;
127                         }
128                 }
129         }
130         %config=%bakconfig;
131
132         # Generate setup file.
133         require IkiWiki::Setup;
134         IkiWiki::Setup::dump($config{dumpsetup});
135
136         # Build the wiki, but w/o wrappers, so it's not live yet.
137         mkpath($config{destdir}) || die "mkdir $config{destdir}: $!";
138         if (system("ikiwiki", "--refresh", "--setup", $config{dumpsetup}) != 0) {
139                 die "ikiwiki --refresh --setup $config{dumpsetup} failed";
140         }
141
142         # Create admin user(s).
143         foreach my $admin (@{$config{adminuser}}) {
144                 next if $admin=~/^http\?:\/\//; # openid
145                 
146                 # Prompt for password w/o echo.
147                 my ($password, $password2);
148                 system('stty -echo 2>/dev/null');
149                 local $|=1;
150                 print "\n\nCreating wiki admin $admin ...\n";
151                 for (;;) {
152                         print "Choose a password: ";
153                         chomp($password=<STDIN>);
154                         print "\n";
155                         print "Confirm password: ";
156                         chomp($password2=<STDIN>);
157
158                         last if $password2 eq $password;
159
160                         print "Password mismatch.\n\n";
161                 }
162                 print "\n\n\n";
163                 system('stty sane 2>/dev/null');
164
165                 if (IkiWiki::userinfo_setall($admin, { regdate => time }) &&
166                     IkiWiki::Plugin::passwordauth::setpassword($admin, $password)) {
167                         IkiWiki::userinfo_set($admin, "email", $config{adminemail}) if defined $config{adminemail};
168                 }
169                 else {
170                         error("problem setting up $admin user");
171                 }
172         }
173         
174         # Add wrappers, make live.
175         if (system("ikiwiki", "--wrappers", "--setup", $config{dumpsetup}) != 0) {
176                 die "ikiwiki --wrappers --setup $config{dumpsetup} failed";
177         }
178
179         # Add it to the wikilist.
180         mkpath("$ENV{HOME}/.ikiwiki");
181         open (WIKILIST, ">>$ENV{HOME}/.ikiwiki/wikilist") || die "$ENV{HOME}/.ikiwiki/wikilist: $!";
182         print WIKILIST "$ENV{USER} $config{dumpsetup}\n";
183         close WIKILIST;
184         if (system("ikiwiki-update-wikilist") != 0) {
185                 print STDERR "** Failed to add you to the system wikilist file.\n";
186                 print STDERR "** (Probably ikiwiki-update-wikilist is not SUID root.)\n";
187                 print STDERR "** Your wiki will not be automatically updated when ikiwiki is upgraded.\n";
188         }
189         
190         # Done!
191         print "\n\nSuccessfully set up $config{wikiname}:\n";
192         foreach my $key (qw{url srcdir destdir repository}) {
193                 next unless exists $config{$key};
194                 print "\t$key: ".(" " x (10 - length($key)))." ".
195                         prettydir($config{$key})."\n";
196         }
197         print "To modify settings, edit ".prettydir($config{dumpsetup})." and then run:\n";
198         print " ikiwiki -setup ".prettydir($config{dumpsetup})."\n";
199         exit 0;
200 }
201
202 1