1package CGI::Kwiki::New;
2$VERSION = '0.18';
3use strict;
4use Config;
5use File::Path;
6use CGI::Kwiki qw(attribute);
7use base 'CGI::Kwiki::Privacy';
8
9attribute 'driver';
10attribute 'options';
11
12sub new {
13    my ($class, @args) = @_;
14    my $self;
15    my ($subclass) = map { s/.*=// } grep { /^--type=/ } @args;
16    if (defined $subclass) {
17        my $full_subclass = "CGI::Kwiki::New::$subclass";
18        eval qq{ require $full_subclass }; die $@ if $@;
19        $self = $full_subclass->new(@args);
20    }
21    else {
22        $self = bless {options => {}}, $class;
23        for (@args) {
24            next unless s/^--//;
25            if (/^(.*?)=(.*)$/) {
26                $self->options->{$1} = $2;
27            }
28            else {
29                $self->options->{$_} = 1;
30            }
31        }
32    }
33    return $self;
34}
35
36sub help {
37    print <<END;
38usage: kwiki-install [options]
39
40feature options:
41  --privacy    - Turn on Public/Protected/Private page security.
42
43upgrade options:
44  --upgrade      - Upgrade everything. Merge config.
45  --reinstall    - Upgrade everything including config file.
46  --config       - Upgrade config file. You will lose local settings!
47  --config_merge - Merge in new config file settings.
48  --scripts      - Upgrade cgi scripts.
49  --pages        - Upgrade default kwiki pages unless changed by a user.
50  --i18n         - Add internationalized page sets.
51  --template     - Upgrade templates.
52  --javascript   - Upgrade javascript.
53  --style        - Upgrade css stylesheets.
54
55END
56}
57
58sub apply_options {
59    my ($self) = @_;
60    for my $option (sort keys %{$self->options}) {
61        if ($self->can($option) and
62            $option !~ /^(apply_options|install|create)$/
63           ) {
64            print "Applying $option\n";
65            $self->$option;
66        }
67        else {
68            warn "Invalid option '--$option' specified\n";
69        }
70    }
71}
72
73sub install {
74    my ($self) = @_;
75    $CGI::Kwiki::ADMIN = 1;
76    $self->driver(CGI::Kwiki::load_driver());
77    $CGI::Kwiki::user_name = 'kwiki-install';
78    if ($self->options->{help}) {
79        $self->help;
80    }
81    elsif (glob '*') {
82        if (keys %{$self->options}) {
83            $self->apply_options;
84        }
85        else {
86            $self->install_error;
87        }
88    }
89    else {
90        $self->create;
91    }
92}
93
94sub create {
95    my ($self) = @_;
96    $self->config;
97    $self->basics;
98    print "Kwiki software installed! Point your browser at this location.\n";
99}
100
101sub upgrade {
102    my ($self) = @_;
103    $self->config_merge && do {
104        $self = __PACKAGE__->new(@ARGV);
105        local $^W;
106        $self->driver(CGI::Kwiki::load_driver());
107    };
108    $self->basics;
109    print "Kwiki software upgraded!\n";
110}
111
112sub basics {
113    my ($self) = @_;
114    $self->locks;
115    $self->scripts;
116    $self->template;
117    $self->javascript;
118    $self->style;
119    $self->pages;
120}
121
122sub reinstall {
123    my ($self) = @_;
124    $self->create;
125}
126
127sub locks {
128    my ($self) = @_;
129    $self->mkdir('metabase');
130    $self->mkdir('metabase/lock');
131}
132
133sub scripts {
134    my ($self) = @_;
135    $self->driver->load_class('scripts');
136    $self->driver->scripts->create_files;
137}
138
139sub config {
140    my ($self) = @_;
141    my $driver = $self->driver;
142    $driver->load_class('config_yaml');
143    $driver->load_class('template');
144    $driver->config_yaml->driver($driver);
145    $driver->config_yaml->create_files;
146}
147
148sub config_merge {
149    my ($self) = @_;
150    my $driver = $self->driver;
151    $driver->load_class('config_yaml');
152    $driver->load_class('template');
153    $driver->config_yaml->driver($driver);
154    $driver->config_yaml->merge_config;
155}
156
157sub pages {
158    my ($self) = @_;
159    $self->driver->load_class('metadata');
160    $self->mkdir('metabase');
161    $self->mkdir('metabase/metadata');
162    $self->driver->load_class('pages');
163    $self->driver->pages->create_files;
164}
165
166sub i18n {
167    my ($self) = @_;
168    $self->driver->load_class('metadata');
169    $self->mkdir('metabase');
170    $self->mkdir('metabase/metadata');
171    $self->driver->load_class('pages');
172    foreach my $class ($self->driver->pages->subclasses) {
173	my $page = $class->new($self->driver);
174	$page->create_files;
175    }
176}
177
178sub template {
179    my ($self) = @_;
180    $self->driver->load_class('template');
181    $self->driver->template->create_files;
182}
183
184sub javascript {
185    my ($self) = @_;
186    $self->driver->load_class('javascript');
187    $self->driver->javascript->create_files;
188}
189
190sub style {
191    my ($self) = @_;
192    $self->driver->load_class('style');
193    $self->driver->style->create_files;
194}
195
196sub privacy {
197    my ($self) = @_;
198    $self->mkdir('metabase/public');
199    $self->mkdir('metabase/private');
200    $self->mkdir('metabase/protected');
201    $self->mkdir('metabase/blog');
202    opendir DATABASE, "database" or die $!;
203    while (my $page_id = $self->unescape(scalar readdir(DATABASE))) {
204        next if $page_id =~ /^\./;
205        if ((not $self->is_public($page_id)) &&
206            (not $self->is_protected($page_id)) &&
207            (not $self->is_private($page_id))
208           ) {
209            $self->set_privacy('public', $page_id);
210        }
211    }
212}
213
214sub mkdir {
215    my ($self, $dir) = @_;
216    unless (-d $dir) {
217        umask 0000;
218        mkdir($dir, 0777);
219    }
220}
221
222sub install_error {
223    print <<END;
224Can't install new kwiki in non-empty directory. If you are upgrading, try:
225
226    kwiki-install --upgrade
227
228For more options try:
229
230    kwiki-install --help
231
232END
233}
234
2351;
236
237__END__
238
239=head1 NAME
240
241CGI::Kwiki::New - Default New Wiki Generator for CGI::Kwiki
242
243=head1 DESCRIPTION
244
245See installed kwiki pages for more information.
246
247=head1 AUTHOR
248
249Brian Ingerson <INGY@cpan.org>
250
251=head1 COPYRIGHT
252
253Copyright (c) 2003. Brian Ingerson. All rights reserved.
254
255This program is free software; you can redistribute it and/or modify it
256under the same terms as Perl itself.
257
258See http://www.perl.com/perl/misc/Artistic.html
259
260=cut
261