| 1 | package SilkiX::Converter::Kwiki; | ||
| 2 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | $SilkiX::Converter::Kwiki::VERSION = '0.03'; | |
| 4 | } | ||
| 5 | |||
| 6 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use Cwd qw( abs_path ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use Encode qw( decode ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use File::Basename qw( basename ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use File::chdir; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use File::MimeInfo qw( mimetype ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 1 | ▶ | use File::Slurp qw( read_file ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 0 | ▶ | use HTML::Tidy; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 17 | use JSON qw( from_json ); | ||
| 18 | use Kwiki (); | ||
| 19 | use Kwiki::Attachments (); | ||
| 20 | use Path::Class qw( dir file ); | ||
| 21 | use Scalar::Util qw( blessed ); | ||
| 22 | use SilkiX::Converter::Kwiki::HTMLToWiki; | ||
| 23 | use Silki::Schema::File; | ||
| 24 | use Silki::Schema::Page; | ||
| 25 | use Silki::Schema::User; | ||
| 26 | use Silki::Schema::Wiki; | ||
| 27 | use URI::Escape qw( uri_unescape ); | ||
| 28 | |||
| 29 | use Moose; | ||
| 30 | use MooseX::SemiAffordanceAccessor; | ||
| 31 | use MooseX::StrictConstructor; | ||
| 32 | use Moose::Util::TypeConstraints; | ||
| 33 | |||
| 34 | use MooseX::Getopt::OptionTypeMap; | ||
| 35 | |||
| 36 | with 'MooseX::Getopt::Dashes'; | ||
| 37 | |||
| 38 | my $kwiki_dir = subtype as 'Str' => where { -d && -f "$_/plugins" }; | ||
| 39 | |||
| 40 | has kwiki_root => ( | ||
| 41 | is => 'ro', | ||
| 42 | isa => $kwiki_dir, | ||
| 43 | required => 1, | ||
| 44 | ); | ||
| 45 | |||
| 46 | has default_user => ( | ||
| 47 | is => 'ro', | ||
| 48 | isa => 'Str', | ||
| 49 | required => 1, | ||
| 50 | ); | ||
| 51 | |||
| 52 | has wiki_name => ( | ||
| 53 | is => 'rw', | ||
| 54 | isa => 'Str', | ||
| 55 | required => 1, | ||
| 56 | ); | ||
| 57 | |||
| 58 | has fast => ( | ||
| 59 | is => 'ro', | ||
| 60 | isa => 'Bool', | ||
| 61 | default => 0, | ||
| 62 | ); | ||
| 63 | |||
| 64 | has _wiki => ( | ||
| 65 | is => 'rw', | ||
| 66 | isa => 'Silki::Schema::Wiki', | ||
| 67 | ); | ||
| 68 | |||
| 69 | has _kwiki => ( | ||
| 70 | is => 'ro', | ||
| 71 | isa => 'Kwiki', | ||
| 72 | lazy => 1, | ||
| 73 | builder => '_build_kwiki', | ||
| 74 | init_arg => undef, | ||
| 75 | ); | ||
| 76 | |||
| 77 | my $file = subtype as 'Str' => where {-f}; | ||
| 78 | |||
| 79 | has user_map_file => ( | ||
| 80 | is => 'rw', | ||
| 81 | writer => '_set_user_map_file', | ||
| 82 | isa => $file, | ||
| 83 | predicate => 'has_user_map_file', | ||
| 84 | ); | ||
| 85 | |||
| 86 | has _user_map => ( | ||
| 87 | is => 'ro', | ||
| 88 | isa => 'HashRef', | ||
| 89 | lazy => 1, | ||
| 90 | builder => '_build_user_map', | ||
| 91 | init_arg => undef, | ||
| 92 | ); | ||
| 93 | |||
| 94 | has _formatter => ( | ||
| 95 | is => 'ro', | ||
| 96 | isa => 'Silki::Formatter::HTMLToWiki', | ||
| 97 | lazy => 1, | ||
| 98 | builder => '_build_formatter', | ||
| 99 | init_arg => undef, | ||
| 100 | ); | ||
| 101 | |||
| 102 | has debug => ( | ||
| 103 | is => 'ro', | ||
| 104 | isa => 'Bool', | ||
| 105 | default => 0, | ||
| 106 | ); | ||
| 107 | |||
| 108 | has dump_page_titles => ( | ||
| 109 | is => 'ro', | ||
| 110 | isa => 'Bool', | ||
| 111 | default => 0, | ||
| 112 | ); | ||
| 113 | |||
| 114 | has dump_usernames => ( | ||
| 115 | is => 'ro', | ||
| 116 | isa => 'Bool', | ||
| 117 | default => 0, | ||
| 118 | ); | ||
| 119 | |||
| 120 | sub BUILD { | ||
| 121 | my $self = shift; | ||
| 122 | my $p = shift; | ||
| 123 | |||
| 124 | unless ( $self->_wiki() ) { | ||
| 125 | my $wiki = Silki::Schema::Wiki->new( title => $p->{wiki_name} ); | ||
| 126 | |||
| 127 | die "No such wiki $p->{wiki_name}" unless $wiki; | ||
| 128 | |||
| 129 | $self->_set_wiki($wiki); | ||
| 130 | } | ||
| 131 | |||
| 132 | $self->_set_user_map_file( abs_path( $self->user_map_file ) ) | ||
| 133 | if $self->has_user_map_file; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub run { | ||
| 137 | my $self = shift; | ||
| 138 | |||
| 139 | # Kwiki just assumes it is running from its root directory. | ||
| 140 | local $CWD = $self->kwiki_root(); | ||
| 141 | |||
| 142 | if ( $self->dump_page_titles() ) { | ||
| 143 | print "\n"; | ||
| 144 | print "All page titles in the Kwiki wiki ...\n"; | ||
| 145 | print $_->title, "\n" for $self->_kwiki()->hub()->pages()->all(); | ||
| 146 | } | ||
| 147 | |||
| 148 | if ( $self->dump_usernames() ) { | ||
| 149 | print "\n"; | ||
| 150 | print "All usernames in the Kwiki wiki ...\n"; | ||
| 151 | |||
| 152 | my %names; | ||
| 153 | for my $page ( $self->_kwiki()->hub()->pages()->all() ) { | ||
| 154 | for my $metadata ( reverse @{ $page->history() } ) { | ||
| 155 | |||
| 156 | my $username = $metadata->{edit_by}; | ||
| 157 | my $date = $metadata->{edit_unixtime}; | ||
| 158 | |||
| 159 | $names{$username} ||= { | ||
| 160 | first_seen => $date, | ||
| 161 | last_seen => $date, | ||
| 162 | }; | ||
| 163 | |||
| 164 | if ( $date < $names{$username}{first_seen} ) { | ||
| 165 | $names{$username}{first_seen} = $date; | ||
| 166 | } | ||
| 167 | |||
| 168 | if ( $date > $names{$username}{last_seen} ) { | ||
| 169 | $names{$username}{last_seen} = $date; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | for my $username ( sort { lc $a cmp lc $b } keys %names ) { | ||
| 175 | print "$username - " | ||
| 176 | . DateTime->from_epoch( | ||
| 177 | epoch => $names{$username}{first_seen} ) | ||
| 178 | . ' to ' | ||
| 179 | . DateTime->from_epoch( | ||
| 180 | epoch => $names{$username}{last_seen} ) | ||
| 181 | . "\n"; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | exit if $self->dump_page_titles() || $self->dump_usernames(); | ||
| 186 | |||
| 187 | $self->_disable_pg_triggers() if $self->fast(); | ||
| 188 | |||
| 189 | eval { | ||
| 190 | for my $page ( | ||
| 191 | map { $_->[1] } | ||
| 192 | sort { $a->[0] <=> $b->[0] } | ||
| 193 | map { | ||
| 194 | my $order = $_->title() eq 'HomePage' ? 0 : 1; | ||
| 195 | [ $order, $_ ] | ||
| 196 | } $self->_kwiki()->hub()->pages()->all() | ||
| 197 | ) { | ||
| 198 | |||
| 199 | $self->_convert_page($page); | ||
| 200 | } | ||
| 201 | |||
| 202 | $self->_enable_pg_triggers() if $self->fast(); | ||
| 203 | |||
| 204 | $self->_rebuild_searchable_text() if $self->fast(); | ||
| 205 | |||
| 206 | for my $user ( | ||
| 207 | grep { !$_->is_disabled() } | ||
| 208 | grep { blessed $_} values %{ $self->_user_map() } | ||
| 209 | ) { | ||
| 210 | |||
| 211 | $self->_wiki->add_user( | ||
| 212 | user => $user, | ||
| 213 | role => Silki::Schema::Role->Member(), | ||
| 214 | ); | ||
| 215 | } | ||
| 216 | }; | ||
| 217 | |||
| 218 | if ( my $e = $@ ) { | ||
| 219 | $self->_enable_pg_triggers(); | ||
| 220 | die $e; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | sub _build_kwiki { | ||
| 225 | my $self = shift; | ||
| 226 | |||
| 227 | # Magic voodoo to make Kwiki work. I don't really care to dig too | ||
| 228 | # deep into this. | ||
| 229 | my $kwiki = Kwiki->new(); | ||
| 230 | my $hub = $kwiki->load_hub( 'config*.*', -plugins => 'plugins' ); | ||
| 231 | $hub->registry()->load(); | ||
| 232 | $hub->add_hooks(); | ||
| 233 | $hub->pre_process(); | ||
| 234 | $hub->preload(); | ||
| 235 | |||
| 236 | return $kwiki; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub _convert_page { | ||
| 240 | my $self = shift; | ||
| 241 | my $page = shift; | ||
| 242 | |||
| 243 | if ( $self->_skip_page($page) ) { | ||
| 244 | $self->_debug(q{}); | ||
| 245 | $self->_debug( "Skipping page " . $page->title() ); | ||
| 246 | return; | ||
| 247 | } | ||
| 248 | |||
| 249 | return if $page->title() eq 'Help'; | ||
| 250 | |||
| 251 | $page->title( $self->_proper_kwiki_title( $page->id ) ); | ||
| 252 | |||
| 253 | my $s_title = $self->_convert_title( $page->title ); | ||
| 254 | |||
| 255 | $self->_debug(q{}); | ||
| 256 | $self->_debug( "Converting page " . $page->title() . " to $s_title" ); | ||
| 257 | |||
| 258 | my @history = reverse @{ $page->history() }; | ||
| 259 | |||
| 260 | my $creator = $self->_convert_user( $history[0]->{edit_by} ); | ||
| 261 | |||
| 262 | my $s_page = Silki::Schema::Page->insert( | ||
| 263 | title => $s_title, | ||
| 264 | user_id => $creator->user_id(), | ||
| 265 | wiki_id => $self->_wiki()->wiki_id(), | ||
| 266 | ); | ||
| 267 | |||
| 268 | my $attachment_map = $self->_convert_attachments( $page, $s_page ); | ||
| 269 | |||
| 270 | for my $revision (@history) { | ||
| 271 | $self->_debug(" ... revision $revision->{revision_id}"); | ||
| 272 | |||
| 273 | my $user = $self->_convert_user( $revision->{edit_by} ); | ||
| 274 | |||
| 275 | my $content = $self->_convert_content( | ||
| 276 | $attachment_map, | ||
| 277 | scalar $self->_kwiki->hub->archive->fetch( | ||
| 278 | $page, $revision->{revision_id} | ||
| 279 | ) | ||
| 280 | ); | ||
| 281 | |||
| 282 | local $Silki::Schema::PageRevision::SkipPostChangeHack | ||
| 283 | = $revision == $history[-1] ? 0 : 1; | ||
| 284 | |||
| 285 | $s_page->add_revision( | ||
| 286 | content => $content, | ||
| 287 | user_id => $user->user_id(), | ||
| 288 | creation_datetime => | ||
| 289 | DateTime->from_epoch( epoch => $revision->{edit_unixtime} ), | ||
| 290 | ); | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | sub _skip_page { | ||
| 295 | return 0; | ||
| 296 | } | ||
| 297 | |||
| 298 | # Kwiki completely breaks utf8 in page titles with its conversion | ||
| 299 | # routines. This redoes the conversion and unbreaks utf8. | ||
| 300 | sub _proper_kwiki_title { | ||
| 301 | my $self = shift; | ||
| 302 | my $id = shift; | ||
| 303 | |||
| 304 | my $title = uri_unescape($id); | ||
| 305 | |||
| 306 | return decode( 'utf-8', $title ); | ||
| 307 | } | ||
| 308 | |||
| 309 | sub _convert_title { | ||
| 310 | my $self = shift; | ||
| 311 | my $title = shift; | ||
| 312 | |||
| 313 | return 'Front Page' if $title eq 'HomePage'; | ||
| 314 | |||
| 315 | return $self->_de_studly($title); | ||
| 316 | } | ||
| 317 | |||
| 318 | sub _de_studly { | ||
| 319 | my $eslf = shift; | ||
| 320 | my $title = shift; | ||
| 321 | |||
| 322 | $title =~ s/([^A-Z])([A-Z])/$1 $2/g; | ||
| 323 | |||
| 324 | return $title; | ||
| 325 | } | ||
| 326 | |||
| 327 | sub _convert_user { | ||
| 328 | my $self = shift; | ||
| 329 | my $kwiki_user = shift; | ||
| 330 | |||
| 331 | $kwiki_user = $self->default_user() | ||
| 332 | unless defined $kwiki_user && length $kwiki_user; | ||
| 333 | |||
| 334 | return Silki::Schema::User->GuestUser() | ||
| 335 | if $kwiki_user eq 'AnonymousGnome'; | ||
| 336 | |||
| 337 | return Silki::Schema::User->SystemUser() | ||
| 338 | if $kwiki_user eq 'kwiki-install'; | ||
| 339 | |||
| 340 | my $user; | ||
| 341 | |||
| 342 | return $self->_user_map()->{$kwiki_user} | ||
| 343 | if blessed $self->_user_map()->{$kwiki_user}; | ||
| 344 | |||
| 345 | $self->_debug("Looking for user mapping from $kwiki_user"); | ||
| 346 | |||
| 347 | $user = $self->_resolve_from_map($kwiki_user); | ||
| 348 | |||
| 349 | if ( $user && !ref $user ) { | ||
| 350 | $kwiki_user = $user; | ||
| 351 | undef $user; | ||
| 352 | } | ||
| 353 | |||
| 354 | if ($user) { | ||
| 355 | my $email | ||
| 356 | = blessed $user ? $user->email_address() : $user->{email_address}; | ||
| 357 | $self->_debug(" ... found an explicit mapping to $email"); | ||
| 358 | } | ||
| 359 | else { | ||
| 360 | $self->_debug(' ... using implicit mapping'); | ||
| 361 | } | ||
| 362 | |||
| 363 | $user ||= {}; | ||
| 364 | for my $key ( | ||
| 365 | qw( email_address password display_name time_zone is_disabled )) { | ||
| 366 | next if exists $user->{$key}; | ||
| 367 | |||
| 368 | my $meth = '_default_' . $key . '_for_user'; | ||
| 369 | $user->{$key} = $self->$meth( $kwiki_user, $user ); | ||
| 370 | } | ||
| 371 | |||
| 372 | my $s_user | ||
| 373 | = blessed $user | ||
| 374 | ? $user | ||
| 375 | : Silki::Schema::User->new( email_address => $user->{email_address} ); | ||
| 376 | |||
| 377 | if ($s_user) { | ||
| 378 | $self->_debug(' ... found a user in the database'); | ||
| 379 | } | ||
| 380 | else { | ||
| 381 | my $status = $user->{is_disabled} ? 'disabled' : 'active'; | ||
| 382 | my $msg = qq{ ... creating a new $status user}; | ||
| 383 | $msg .= qq{, password is "$user->{password}"} | ||
| 384 | unless $user->{is_disabled}; | ||
| 385 | |||
| 386 | $self->_debug($msg); | ||
| 387 | |||
| 388 | $s_user = Silki::Schema::User->insert( | ||
| 389 | %{$user}, | ||
| 390 | user => Silki::Schema::User->SystemUser(), | ||
| 391 | ); | ||
| 392 | } | ||
| 393 | |||
| 394 | $self->_user_map->{$kwiki_user} = $s_user; | ||
| 395 | } | ||
| 396 | |||
| 397 | sub _resolve_from_map { | ||
| 398 | my $self = shift; | ||
| 399 | my $key = shift; | ||
| 400 | |||
| 401 | my $start = $key; | ||
| 402 | my $x = 0; | ||
| 403 | |||
| 404 | while (1) { | ||
| 405 | my $value = $self->_user_map()->{$key}; | ||
| 406 | |||
| 407 | return $value if ref $value; | ||
| 408 | |||
| 409 | return $key unless defined $value; | ||
| 410 | |||
| 411 | $self->_debug(" ... following map from $key to $value" ); | ||
| 412 | |||
| 413 | $key = $value; | ||
| 414 | |||
| 415 | if ($x++ > 10 ) { | ||
| 416 | die "Could not resolve mapping for $start after 10 iterations"; | ||
| 417 | } | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 421 | sub _default_password_for_user { | ||
| 422 | return 'change me'; | ||
| 423 | } | ||
| 424 | |||
| 425 | sub _default_display_name_for_user { | ||
| 426 | my $self = shift; | ||
| 427 | my $kwiki_user = shift; | ||
| 428 | |||
| 429 | return $kwiki_user; | ||
| 430 | } | ||
| 431 | |||
| 432 | sub _default_email_address_for_user { | ||
| 433 | my $self = shift; | ||
| 434 | my $kwiki_user = shift; | ||
| 435 | |||
| 436 | return $kwiki_user . '@localhost.localdomain'; | ||
| 437 | } | ||
| 438 | |||
| 439 | sub _default_time_zone_for_user { | ||
| 440 | my $self = shift; | ||
| 441 | my $kwiki_user = shift; | ||
| 442 | |||
| 443 | return 'America/New_York'; | ||
| 444 | } | ||
| 445 | |||
| 446 | sub _default_is_disabled_for_user { | ||
| 447 | return 0; | ||
| 448 | } | ||
| 449 | |||
| 450 | sub _build_user_map { | ||
| 451 | my $self = shift; | ||
| 452 | |||
| 453 | my $map; | ||
| 454 | $map = from_json( read_file( $self->user_map_file() ) ) | ||
| 455 | if $self->has_user_map_file; | ||
| 456 | $map ||= {}; | ||
| 457 | |||
| 458 | return $map; | ||
| 459 | } | ||
| 460 | |||
| 461 | sub _convert_attachments { | ||
| 462 | my $self = shift; | ||
| 463 | my $page = shift; | ||
| 464 | my $s_page = shift; | ||
| 465 | |||
| 466 | return unless $self->_kwiki()->hub()->can('attachments'); | ||
| 467 | |||
| 468 | return | ||
| 469 | unless $self->_kwiki()->hub()->attachments() | ||
| 470 | ->get_attachments( $page->id() ); | ||
| 471 | |||
| 472 | my $dir = dir( | ||
| 473 | $self->_kwiki()->hub()->attachments()->plugin_directory(), | ||
| 474 | $page->id() | ||
| 475 | ); | ||
| 476 | |||
| 477 | my %map; | ||
| 478 | for my $kwiki_file ( @{ $self->_kwiki()->hub()->attachments()->files() } ) | ||
| 479 | { | ||
| 480 | $self->_debug( ' ... attachment ' . $kwiki_file->name() ); | ||
| 481 | |||
| 482 | my $file_path = $dir->file( $kwiki_file->name() ); | ||
| 483 | |||
| 484 | next unless -s $file_path; | ||
| 485 | |||
| 486 | my $file = Silki::Schema::File->insert( | ||
| 487 | filename => $kwiki_file->name(), | ||
| 488 | mime_type => mimetype( $file_path->stringify() ), | ||
| 489 | file_size => -s $file_path, | ||
| 490 | contents => scalar read_file( $file_path->stringify() ), | ||
| 491 | user_id => Silki::Schema::User->SystemUser()->user_id(), | ||
| 492 | page_id => $s_page->page_id(), | ||
| 493 | ); | ||
| 494 | |||
| 495 | $map{ $kwiki_file->name() } = $file; | ||
| 496 | } | ||
| 497 | |||
| 498 | return \%map; | ||
| 499 | } | ||
| 500 | |||
| 501 | # gibberish that will not be present in any real page. We can use this | ||
| 502 | # to delimit things that need to be dealt with _after_ the kwiki -> | ||
| 503 | # html -> markdown conversion. | ||
| 504 | my $Marker = 'asfkjsdkglsjdglkjsga09dsug0329jt3poi3p41o6j24963109ytu0cgsv'; | ||
| 505 | |||
| 506 | sub _convert_content { | ||
| 507 | my $self = shift; | ||
| 508 | my $attachment_map = shift; | ||
| 509 | my $content = shift; | ||
| 510 | |||
| 511 | return q{} unless defined $content && length $content; | ||
| 512 | |||
| 513 | my $counter = 1; | ||
| 514 | my %post_convert; | ||
| 515 | $content =~ s/\{file:?\s*([^}]+)}/ | ||
| 516 | $post_convert{$counter++} = [ 'attachment', $1 ]; | ||
| 517 | $Marker . ':' . ( $counter - 1 )/eg; | ||
| 518 | |||
| 519 | my $kwiki_html = $self->_kwiki->hub->formatter->text_to_html($content); | ||
| 520 | |||
| 521 | my $markdown = $self->_formatter()->html_to_wikitext($kwiki_html); | ||
| 522 | |||
| 523 | $markdown =~ s/$Marker:(\d+)/ | ||
| 524 | $self->_post_convert( $post_convert{$1}, $attachment_map )/eg; | ||
| 525 | |||
| 526 | return $markdown; | ||
| 527 | } | ||
| 528 | |||
| 529 | sub _post_convert { | ||
| 530 | my $self = shift; | ||
| 531 | my $action = shift; | ||
| 532 | my $attachment_map = shift; | ||
| 533 | |||
| 534 | if ( $action->[0] eq 'attachment' ) { | ||
| 535 | $self->_attachment_link( $action->[1], $attachment_map ); | ||
| 536 | } | ||
| 537 | else { | ||
| 538 | die "Unknown post-convert action: $action->[0]"; | ||
| 539 | } | ||
| 540 | } | ||
| 541 | |||
| 542 | sub _attachment_link { | ||
| 543 | my $self = shift; | ||
| 544 | my $filename = shift; | ||
| 545 | my $attachment_map = shift; | ||
| 546 | |||
| 547 | my $file = $attachment_map->{$filename} | ||
| 548 | or return q{}; | ||
| 549 | |||
| 550 | return '{{file:' . $file->filename() . '}}'; | ||
| 551 | } | ||
| 552 | |||
| 553 | sub _build_formatter { | ||
| 554 | my $self = shift; | ||
| 555 | |||
| 556 | my $wiki_link_fixer = sub { | ||
| 557 | return $self->_convert_wiki_link( $_[0] ); | ||
| 558 | }; | ||
| 559 | |||
| 560 | return SilkiX::Converter::Kwiki::HTMLToWiki->new( | ||
| 561 | wiki => $self->_wiki(), | ||
| 562 | wiki_link_fixer => $wiki_link_fixer, | ||
| 563 | ); | ||
| 564 | } | ||
| 565 | |||
| 566 | sub _convert_wiki_link { | ||
| 567 | my $self = shift; | ||
| 568 | my $kwiki_title = shift; | ||
| 569 | |||
| 570 | return $self->_convert_title($kwiki_title); | ||
| 571 | } | ||
| 572 | |||
| 573 | sub _disable_pg_triggers { | ||
| 574 | my $self = shift; | ||
| 575 | |||
| 576 | my $dbh = Silki::Schema->DBIManager()->default_source()->dbh(); | ||
| 577 | |||
| 578 | $dbh->do( q{ALTER TABLE "Page" DISABLE TRIGGER USER} ); | ||
| 579 | $dbh->do( q{ALTER TABLE "PageRevision" DISABLE TRIGGER USER} ); | ||
| 580 | } | ||
| 581 | |||
| 582 | sub _enable_pg_triggers { | ||
| 583 | my $self = shift; | ||
| 584 | |||
| 585 | my $dbh = Silki::Schema->DBIManager()->default_source()->dbh(); | ||
| 586 | |||
| 587 | $dbh->do( q{ALTER TABLE "Page" ENABLE TRIGGER USER} ); | ||
| 588 | $dbh->do( q{ALTER TABLE "PageRevision" ENABLE TRIGGER USER} ); | ||
| 589 | } | ||
| 590 | |||
| 591 | sub _rebuild_searchable_text { | ||
| 592 | my $self = shift; | ||
| 593 | |||
| 594 | my $sql = <<'EOF'; | ||
| 595 | INSERT INTO "PageSearchableText" | ||
| 596 | (page_id, ts_text) | ||
| 597 | SELECT pages.page_id, | ||
| 598 | setweight(to_tsvector('pg_catalog.english', pages.title), 'A') || | ||
| 599 | setweight(to_tsvector('pg_catalog.english', pages.content), 'B') | ||
| 600 | FROM ( SELECT p.page_id, p.title, pr.content | ||
| 601 | FROM "Page" AS p, "PageRevision" AS pr | ||
| 602 | WHERE revision_number = | ||
| 603 | ( SELECT MAX(revision_number) | ||
| 604 | FROM "PageRevision" | ||
| 605 | WHERE page_id = p.page_id ) | ||
| 606 | AND p.page_id = pr.page_id | ||
| 607 | AND p.wiki_id = ? | ||
| 608 | ) AS pages | ||
| 609 | EOF | ||
| 610 | |||
| 611 | my $dbh = Silki::Schema->DBIManager()->default_source()->dbh(); | ||
| 612 | |||
| 613 | $dbh->do( $sql, {}, $self->_wiki()->wiki_id() ); | ||
| 614 | } | ||
| 615 | |||
| 616 | sub _debug { | ||
| 617 | my $self = shift; | ||
| 618 | |||
| 619 | return unless $self->debug(); | ||
| 620 | |||
| 621 | my $msg = shift; | ||
| 622 | |||
| 623 | print STDERR $msg, "\n"; | ||
| 624 | } | ||
| 625 | |||
| 626 | { | ||
| 627 | use Spoon::Hub; | ||
| 628 | |||
| 629 | package | ||
| 630 | Spoon::Hub; | ||
| 631 | |||
| 632 | no warnings 'redefine'; | ||
| 633 | |||
| 634 | # shuts up a warning during global destruction | ||
| 635 | sub remove_hooks { | ||
| 636 | my $self = shift; | ||
| 637 | my $hooks = $self->all_hooks; | ||
| 638 | while (@$hooks) { | ||
| 639 | my $hook = pop(@$hooks) | ||
| 640 | or next; | ||
| 641 | $hook->unhook; | ||
| 642 | } | ||
| 643 | } | ||
| 644 | } | ||
| 645 | |||
| 646 | 1; | ||
| 647 | |||
| 648 | # ABSTRACT: Convert a Kwiki wiki to a Silki wiki | ||
| 649 | |||
| 650 | |||
| 651 | |||
| 652 | =pod | ||
| 653 | |||
| 654 | =head1 NAME | ||
| 655 | |||
| 656 | SilkiX::Converter::Kwiki - Convert a Kwiki wiki to a Silki wiki | ||
| 657 | |||
| 658 | =head1 VERSION | ||
| 659 | |||
| 660 | version 0.03 | ||
| 661 | |||
| 662 | =head1 SYNOPSIS | ||
| 663 | |||
| 664 | kwiki2silki --wiki 'Silki Wiki Title' \ | ||
| 665 | --kwiki-root /path/to/kwiki \ | ||
| 666 | --user-map-file /path/to/map.json \ | ||
| 667 | --default-user DefaultNameFromKwiki \ | ||
| 668 | --fast | ||
| 669 | |||
| 670 | SILKIX_CONVERTER=MyCustomConversionSubclass \ | ||
| 671 | kwiki2silki --wiki 'Silki Wiki Title' \ | ||
| 672 | --kwiki-root /path/to/kwiki \ | ||
| 673 | --user-map-file /path/to/map.json \ | ||
| 674 | --default-user DefaultNameFromKwiki | ||
| 675 | |||
| 676 | =head1 DESCRIPTION | ||
| 677 | |||
| 678 | This module lets you convert a Kwiki wiki into a Silki wiki. The primary | ||
| 679 | interface is via the F<kwiki2silki> script that ships with this distribution. | ||
| 680 | |||
| 681 | If you define a C<SILKIX_CONVERTER> environment variable, then this class will | ||
| 682 | be used to do the conversion. This lets you create a custom subclass, which | ||
| 683 | can be useful, especially when it comes to default values for users and for | ||
| 684 | mapping page titles from Kwiki to Silki. | ||
| 685 | |||
| 686 | =head1 WARNINGS | ||
| 687 | |||
| 688 | If you pass the C<--fast> option, the converter disables some database | ||
| 689 | triggers in order to speed things up, and restores them at the end of the | ||
| 690 | conversion. It attempts to restore them if the process fails mid-stream, but | ||
| 691 | this is software, and software has bugs. | ||
| 692 | |||
| 693 | If you have other wikis which are in use, make sure to backup your database | ||
| 694 | with F<pg_dump>. Also, shut down the web UI during the conversion, or else | ||
| 695 | other wikis could end up corrupted because of the missing triggers. | ||
| 696 | |||
| 697 | The converter assumes that the wiki you're writing to is effectively | ||
| 698 | empty. Don't try to convert into an already-in-use wiki! | ||
| 699 | |||
| 700 | Basically, this software is rough, and could mess you up. Be careful. | ||
| 701 | |||
| 702 | =head1 KWIKI PLUGINS | ||
| 703 | |||
| 704 | This module assumes that your Kwiki install has certain plugins. It assumes | ||
| 705 | that your Kwiki install has one of the archive plugins installed, though it | ||
| 706 | doesn't matter which one. | ||
| 707 | |||
| 708 | You also will need the L<Kwiki::Attachments> module installed. This may fail | ||
| 709 | tests, but just force it (sigh). However, if your kwiki install does not load | ||
| 710 | the attachments plugin, that's ok. If it does, then this converter will | ||
| 711 | convert attachments too. | ||
| 712 | |||
| 713 | =head1 USER MAP FILE | ||
| 714 | |||
| 715 | By default, the converter does a dumb mapping of kwiki usernames to Silki | ||
| 716 | users. The email address is C<$kwiki_user@localhost.localdomain>. | ||
| 717 | |||
| 718 | If you want to make useful accounts for users, you can create a user map JSON | ||
| 719 | file. | ||
| 720 | |||
| 721 | This should be a JSON object (aka hash) where the keys are Kwiki | ||
| 722 | usernames. The values can either be an object describing the user for Silki, | ||
| 723 | or another key name. If it is another key name, then the converter follows the | ||
| 724 | references until it finds an object or ten links have been followed, at which | ||
| 725 | point it blows up. | ||
| 726 | |||
| 727 | The Silki user object in the user map can have the following keys: | ||
| 728 | |||
| 729 | =over 4 | ||
| 730 | |||
| 731 | =item * email_address | ||
| 732 | |||
| 733 | =item * password | ||
| 734 | |||
| 735 | Defaults to "change me". | ||
| 736 | |||
| 737 | =item * display_name | ||
| 738 | |||
| 739 | The name used by Silki to display the user. This can be empty. | ||
| 740 | |||
| 741 | =item * time_zone | ||
| 742 | |||
| 743 | Must be an Olson time zone. Defaults to "America/New_York". | ||
| 744 | |||
| 745 | =item * is_disabled | ||
| 746 | |||
| 747 | A boolean which defaults to false. | ||
| 748 | |||
| 749 | =back | ||
| 750 | |||
| 751 | =head1 CONVERTING TITLES | ||
| 752 | |||
| 753 | By default, titles are converted by splitting StudlyCaps words apart, so | ||
| 754 | "RandomKwikiPage" becomes "Random Kwiki Page". | ||
| 755 | |||
| 756 | The one exception is that the Kwiki "HomePage" becomes the Silki "Front Page". | ||
| 757 | |||
| 758 | You can use a converter subclass to add more intelligence to this process. | ||
| 759 | |||
| 760 | Also, you'll need to manually delete your wiki's front page (for now). | ||
| 761 | |||
| 762 | =head1 DUMPING TITLES AND USERS | ||
| 763 | |||
| 764 | The F<kwiki2silki> script can dump all titles and users from your kwiki | ||
| 765 | install: | ||
| 766 | |||
| 767 | kwiki2silki --wiki 'Silki Wiki Title' \ | ||
| 768 | --kwiki-root /path/to/kwiki \ | ||
| 769 | --dump-page-titles \ | ||
| 770 | --dump-usernames | ||
| 771 | |||
| 772 | This is very handy in helping you come up with a user map. | ||
| 773 | |||
| 774 | =head1 AUTHOR | ||
| 775 | |||
| 776 | Dave Rolsky <autarch@urth.org> | ||
| 777 | |||
| 778 | =head1 COPYRIGHT AND LICENSE | ||
| 779 | |||
| 780 | This software is Copyright (c) 2010 by Dave Rolsky. | ||
| 781 | |||
| 782 | This is free software, licensed under: | ||
| 783 | |||
| 784 | The Artistic License 2.0 | ||
| 785 | |||
| 786 | =cut | ||
| 787 | |||
| 788 | |||
| 789 | __END__ | ||