| 1 | package CGI::Wiki::Simple::Setup; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use CGI::Wiki; | ||
| 6 | use CGI::Wiki::Simple; | ||
| 7 | use Carp qw(croak); | ||
| 8 | use Digest::MD5 qw( md5_hex ); | ||
| 9 | |||
| 10 | use vars qw($VERSION); | ||
| 11 | |||
| 12 | $VERSION = '0.11'; | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | CGI::Wiki::Simple::Setup - Set up the wiki and fill content into some basic pages. | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | This is a simple utility module that given a database sets up a complete wiki within it. | ||
| 21 | |||
| 22 | =head1 SYNOPSIS | ||
| 23 | |||
| 24 | =for example begin | ||
| 25 | |||
| 26 | setup( dbtype => 'sqlite', dbname => "mywiki.db" ); | ||
| 27 | # This sets up a SQLite wiki within the file mywiki.db | ||
| 28 | |||
| 29 | setup( dbtype => 'mysql', dbname => "wiki", dbuser => "wiki", dbpass => "secret", file => 'nodeball.txt' ); | ||
| 30 | # This sets up a MySQL wiki and loads the nodes from the file nodeball.txt | ||
| 31 | |||
| 32 | =for example end | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | my %stores = ( | ||
| 37 | sqlite => 'SQLite', | ||
| 38 | mysql => 'MySQL', | ||
| 39 | pg => 'Pg', | ||
| 40 | ); | ||
| 41 | |||
| 42 | =head2 C<get_store %ARGS> | ||
| 43 | |||
| 44 | C<get_store> creates a store from a hash of parameters. There | ||
| 45 | are two mandatory parameters : | ||
| 46 | |||
| 47 | dbtype => 'mysql' | ||
| 48 | |||
| 49 | This is the type of the database. Recognized values are C<mysql>, | ||
| 50 | C<sqlite> and C<pg>. | ||
| 51 | |||
| 52 | dbname => 'wiki' | ||
| 53 | |||
| 54 | This is the name of the database. | ||
| 55 | |||
| 56 | The remaining parameters are optional : | ||
| 57 | |||
| 58 | dbuser => 'wikiuser' | ||
| 59 | |||
| 60 | The database user | ||
| 61 | |||
| 62 | dbpass => 'secret' | ||
| 63 | |||
| 64 | The password for the database | ||
| 65 | |||
| 66 | setup => 1 | ||
| 67 | |||
| 68 | Create the database unless it exists already | ||
| 69 | |||
| 70 | clear => 1 | ||
| 71 | |||
| 72 | Wipe all nodes from the database before reinitializing | ||
| 73 | it. Only valid if C<setup> is also true. | ||
| 74 | |||
| 75 | check => 1 | ||
| 76 | |||
| 77 | Check that a node called C<index> exists. This raises | ||
| 78 | an error if the database exists but is empty. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | sub get_store { | ||
| 83 | my %args = @_; | ||
| 84 | |||
| 85 | my @setup_args; | ||
| 86 | my $dbtype; | ||
| 87 | |||
| 88 | push @setup_args, $args{dbname}; | ||
| 89 | for (qw(dbuser dbpass)) { | ||
| 90 | push @setup_args, $args{$_} | ||
| 91 | if exists $args{$_}; | ||
| 92 | }; | ||
| 93 | |||
| 94 | $dbtype = $args{dbtype}; | ||
| 95 | |||
| 96 | croak "Unknown database type $dbtype" | ||
| 97 | unless exists $stores{lc($dbtype)}; | ||
| 98 | $dbtype = $stores{lc($dbtype)}; | ||
| 99 | |||
| 100 | eval "use CGI::Wiki::Store::$dbtype; use CGI::Wiki::Setup::$dbtype"; | ||
| 101 | |||
| 102 | if ($args{setup}) { | ||
| 103 | no strict 'refs'; | ||
| 104 | &{"CGI::Wiki::Setup::${dbtype}::cleardb"}(@setup_args) | ||
| 105 | if $args{clear}; | ||
| 106 | &{"CGI::Wiki::Setup::${dbtype}::setup"}(@setup_args); | ||
| 107 | }; | ||
| 108 | |||
| 109 | # get the wiki store : | ||
| 110 | my $store = "CGI::Wiki::Store::$dbtype"->new( %args ); | ||
| 111 | warn "Couldn't get store for $args{dbname}" unless $store; | ||
| 112 | |||
| 113 | $store->retrieve_node("index") | ||
| 114 | if ($args{check}); | ||
| 115 | |||
| 116 | return $store; | ||
| 117 | }; | ||
| 118 | |||
| 119 | =head2 C<setup %ARGS> | ||
| 120 | |||
| 121 | Creates a new database and initializes it. Takes the | ||
| 122 | same parameters as C<get_store> and two additional | ||
| 123 | optional parameters : | ||
| 124 | |||
| 125 | nocontent => 1 | ||
| 126 | |||
| 127 | Prevents loading the three default nodes from the module | ||
| 128 | into the wiki. | ||
| 129 | |||
| 130 | force => 1 | ||
| 131 | |||
| 132 | Overwrites nodes with the loaded content even if they | ||
| 133 | already exists. | ||
| 134 | |||
| 135 | =cut | ||
| 136 | |||
| 137 | sub setup { | ||
| 138 | my %args = @_; | ||
| 139 | |||
| 140 | croak "No dbtype given" | ||
| 141 | unless $args{dbtype}; | ||
| 142 | |||
| 143 | my $store = get_store( %args, setup => 1 ); | ||
| 144 | unless ($args{nocontent}) { | ||
| 145 | print "Loading content\n" | ||
| 146 | unless $args{silent}; | ||
| 147 | load_nodeball( store => $store, ) | ||
| 148 | }; | ||
| 149 | |||
| 150 | $store->dbh->disconnect; | ||
| 151 | }; | ||
| 152 | |||
| 153 | =head2 C<setup_if_needed %ARGS> | ||
| 154 | |||
| 155 | Creates a new database and initializes it if no | ||
| 156 | current database is found. Takes the same arguments | ||
| 157 | as C<setup> | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub setup_if_needed { | ||
| 162 | my %args = @_; | ||
| 163 | |||
| 164 | my $store; | ||
| 165 | eval { $store = get_store( %args, check => 1 ); }; | ||
| 166 | setup(%args) if $@ or ! $store; | ||
| 167 | }; | ||
| 168 | |||
| 169 | =head2 C<load_nodeball> | ||
| 170 | |||
| 171 | Loads a nodeball into the wiki. A nodeball is a set of nodes | ||
| 172 | in a text file like this : | ||
| 173 | |||
| 174 | __NODE__ | ||
| 175 | Title: TestNode | ||
| 176 | |||
| 177 | This is a test node. It | ||
| 178 | consists of content that will be formatted through the | ||
| 179 | wiki formatter. | ||
| 180 | |||
| 181 | __NODE__ | ||
| 182 | Title: AnotherTestNode | ||
| 183 | |||
| 184 | You know it. | ||
| 185 | |||
| 186 | The routine takes the following parameters additional | ||
| 187 | to the usual database parameters : | ||
| 188 | |||
| 189 | fh => \*FILE | ||
| 190 | |||
| 191 | Loads the nodeball from the filehandle FILE. | ||
| 192 | |||
| 193 | file => 'nodeball.txt' | ||
| 194 | |||
| 195 | Loads the nodeball from the specified file. | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | sub load_nodeball { | ||
| 200 | my %args = @_; | ||
| 201 | |||
| 202 | if ($args{file}) { | ||
| 203 | open F, "<$args{file}" | ||
| 204 | or die "Couldn't read nodeball from '$args{file}' : $!\n"; | ||
| 205 | } elsif ($args{fh}) { | ||
| 206 | *F = *{$args{fh}} | ||
| 207 | } else { | ||
| 208 | *F = *DATA; | ||
| 209 | }; | ||
| 210 | |||
| 211 | my $store = $args{store} || get_store( %args ); | ||
| 212 | |||
| 213 | unless ($args{nocontent}) { | ||
| 214 | my $wiki = CGI::Wiki->new( | ||
| 215 | store => $store, | ||
| 216 | search => undef ); | ||
| 217 | |||
| 218 | my $offset = tell F; | ||
| 219 | my @NODES = map { | ||
| 220 | /^Title:\s+(.*?)\r?\n(.*)/ms | ||
| 221 | ? { title => $1, content => $2 } | ||
| 222 | : () | ||
| 223 | } do { undef $/; split /__NODE__/, <F> }; | ||
| 224 | seek F, $offset, 0; | ||
| 225 | |||
| 226 | commit_content( %args, wiki => $wiki, nodes => \@NODES, ); | ||
| 227 | |||
| 228 | undef $wiki; | ||
| 229 | }; | ||
| 230 | }; | ||
| 231 | |||
| 232 | =head2 C<commit_content %ARGS> | ||
| 233 | |||
| 234 | Loads a set of nodes into the wiki database. Takes the following | ||
| 235 | parameters : | ||
| 236 | |||
| 237 | wiki => $wiki | ||
| 238 | |||
| 239 | An initialized CGI::Wiki. | ||
| 240 | |||
| 241 | force => 1 | ||
| 242 | |||
| 243 | Force overwriting existing nodes | ||
| 244 | |||
| 245 | silent => 1 | ||
| 246 | |||
| 247 | Do not print out normal messages. Warnings still get raised. | ||
| 248 | |||
| 249 | nodes => \@NODES | ||
| 250 | |||
| 251 | A reference to an array of hash references. Each element should | ||
| 252 | have the following structure : | ||
| 253 | |||
| 254 | { title => 'A node title', content => 'Some content' } | ||
| 255 | |||
| 256 | |||
| 257 | =cut | ||
| 258 | |||
| 259 | sub commit_content { | ||
| 260 | my %args = @_; | ||
| 261 | my $wiki = $args{wiki}; | ||
| 262 | |||
| 263 | croak "No wiki passed in the 'wiki' parameter" | ||
| 264 | unless $wiki; | ||
| 265 | |||
| 266 | my @nodes = @{$args{nodes}}; | ||
| 267 | foreach my $node (@nodes) { | ||
| 268 | my $title = $node->{title}; | ||
| 269 | |||
| 270 | my %old_node = $wiki->retrieve_node($title); | ||
| 271 | if (not $old_node{content} or $args{force}) { | ||
| 272 | my $content = $node->{content}; | ||
| 273 | $content =~ s/\r\n/\n/g; | ||
| 274 | my $cksum = $old_node{checksum}; | ||
| 275 | my $written = $wiki->write_node($title, $content, $cksum); | ||
| 276 | if ($written) { | ||
| 277 | print "(Re)initialized node '$title'\n" | ||
| 278 | unless $args{silent}; | ||
| 279 | } else { | ||
| 280 | warn "Node '$title' not written\n"; | ||
| 281 | }; | ||
| 282 | } else { | ||
| 283 | warn "Node '$title' already contains data. Not overwritten."; | ||
| 284 | }; | ||
| 285 | }; | ||
| 286 | }; | ||
| 287 | |||
| 288 | =head1 COPYRIGHT | ||
| 289 | |||
| 290 | Copyright (C) 2003 Max Maischein. All Rights Reserved. | ||
| 291 | |||
| 292 | This code is free software; you can redistribute it and/or modify it | ||
| 293 | under the same terms as Perl itself. | ||
| 294 | |||
| 295 | =head1 SEE ALSO | ||
| 296 | |||
| 297 | L<CGI::Wiki>, L<CGI::Wiki::Simple> | ||
| 298 | |||
| 299 | =cut | ||
| 300 | |||
| 301 | __DATA__ |