| 1 | package Silki::CLI::Import; | ||
| 2 | { | ||
| 3 | $Silki::CLI::Import::VERSION = '0.29'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use namespace::autoclean; | ||
| 9 | use autodie; | ||
| 10 | |||
| 11 | use Silki::Schema::Domain; | ||
| 12 | use Silki::Types qw( Bool Str ); | ||
| 13 | use Silki::Wiki::Importer; | ||
| 14 | |||
| 15 | use Moose; | ||
| 16 | |||
| 17 | with qw( MooseX::Getopt::Dashes Silki::Role::CLI::HasOptionalProcess ); | ||
| 18 | |||
| 19 | has tarball => ( | ||
| 20 | is => 'ro', | ||
| 21 | isa => Str, | ||
| 22 | required => 1, | ||
| 23 | documentation => 'The tarball containing the wiki to import. Required.', | ||
| 24 | ); | ||
| 25 | |||
| 26 | has domain => ( | ||
| 27 | is => 'ro', | ||
| 28 | isa => Str, | ||
| 29 | documentation => | ||
| 30 | 'The domain for the new wiki. Defaults to the default domain for the site.', | ||
| 31 | ); | ||
| 32 | |||
| 33 | has fast => ( | ||
| 34 | is => 'ro', | ||
| 35 | isa => Bool, | ||
| 36 | documentation => | ||
| 37 | 'If this is true, the importer will disable triggers during the import.' | ||
| 38 | . ' This is not safe if users are using the web interface at the same time,' | ||
| 39 | . ' so it defaults to false.' | ||
| 40 | ); | ||
| 41 | |||
| 42 | sub _run { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | $self->_replace_dbi_manager() if $self->process(); | ||
| 46 | |||
| 47 | my $wiki; | ||
| 48 | |||
| 49 | my %p = ( | ||
| 50 | tarball => $self->tarball(), | ||
| 51 | log => $self->_log_coderef(), | ||
| 52 | fast => $self->fast(), | ||
| 53 | ); | ||
| 54 | |||
| 55 | $p{domain} = Silki::Schema::Domain->new( web_hostname => $self->domain() ) | ||
| 56 | if $self->domain(); | ||
| 57 | |||
| 58 | return Silki::Wiki::Importer->new(%p)->imported_wiki(); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub _final_result_string { | ||
| 62 | my $self = shift; | ||
| 63 | my $wiki = shift; | ||
| 64 | |||
| 65 | return $wiki->uri(); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _print_success_message { | ||
| 69 | my $self = shift; | ||
| 70 | my $wiki = shift; | ||
| 71 | |||
| 72 | print "\n"; | ||
| 73 | print ' The ' . $wiki->short_name() . ' wiki has been imported.'; | ||
| 74 | print "\n"; | ||
| 75 | print ' You can visit it at ' . $wiki->uri( with_host => 1 ); | ||
| 76 | print "\n\n"; | ||
| 77 | } | ||
| 78 | |||
| 79 | # This is a hack to make sure that updatess to the process table are not done | ||
| 80 | # inside the import transaction. Otherwise the status cannot actually be seen | ||
| 81 | # until the transaction finishes. | ||
| 82 | sub _replace_dbi_manager { | ||
| 83 | my $self = shift; | ||
| 84 | |||
| 85 | my $new_source = Silki::Schema->DBIManager()->default_source() | ||
| 86 | ->clone( name => 'for Process updates' ); | ||
| 87 | |||
| 88 | my $man = _DBIManager->new(); | ||
| 89 | $man->add_source( Silki::Schema->DBIManager()->default_source() ); | ||
| 90 | $man->add_source($new_source); | ||
| 91 | |||
| 92 | Silki::Schema->SetDBIManager($man); | ||
| 93 | |||
| 94 | return; | ||
| 95 | } | ||
| 96 | |||
| 97 | { | ||
| 98 | |||
| 99 | package _DBIManager; | ||
| 100 | { | ||
| 101 | $_DBIManager::VERSION = '0.29'; | ||
| 102 | } | ||
| 103 | |||
| 104 | use Moose; | ||
| 105 | |||
| 106 | extends 'Fey::DBIManager'; | ||
| 107 | |||
| 108 | override source_for_sql => sub { | ||
| 109 | my $self = shift; | ||
| 110 | my $sql = shift; | ||
| 111 | |||
| 112 | return $sql->isa('Fey::SQL::Update') | ||
| 113 | && $sql->sql('Fey::FakeDBI') =~ /UPDATE "Process"/ | ||
| 114 | ? $self->get_source('for Process updates') | ||
| 115 | : $self->get_source('default'); | ||
| 116 | }; | ||
| 117 | } | ||
| 118 | |||
| 119 | # Intentionally not made immutable, since we only ever make one of these | ||
| 120 | # objects in a process. | ||
| 121 | |||
| 122 | 1; |