Silki/Wiki/Importer.pm untested

filter: Statementstmt 0.0% 0 / 193 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 279 CC 19 SCAR 25.5
CC
19
Cov
0%
_import
30.0
_import_user
30.0
_import_pages
20.0
CRAP 12.8
1 package Silki::Wiki::Importer;
2 {
3 $Silki::Wiki::Importer::VERSION = '0.29';
4 }
5  
6 use strict;
7 use warnings;
8 use namespace::autoclean;
9  
10 use Archive::Tar::Wrapper;
11 use File::Slurp qw( read_file );
12 use Path::Class qw( dir );
13 use Silki::I18N qw( loc );
14 use Silki::JSON;
15 use Silki::Schema::Domain;
16 use Silki::Schema::User;
17 use Silki::Schema::Wiki;
18 use Silki::Types qw( ArrayRef Bool Dir HashRef Tarball );
19  
20 use Moose;
21 use MooseX::SemiAffordanceAccessor;
22  
23 with 'Silki::Role::OptionalLog';
24  
25 has user => (
26 is => 'ro',
27 isa => 'Silki::Schema::User',
28 lazy => 1,
29 default => sub { Silki::Schema::User->SystemUser() },
30 );
31  
32 has domain => (
33 is => 'ro',
34 isa => 'Silki::Schema::Domain',
35 lazy => 1,
36 default => sub { Silki::Schema::Domain->DefaultDomain() },
37 );
38  
39 has fast => (
40 is => 'ro',
41 isa => Bool,
42 default => 0,
43 );
44  
45 has _tarball => (
46 is => 'ro',
47 isa => Tarball,
48 init_arg => 'tarball',
49 required => 1,
50 coerce => 1,
51 );
52  
53 has _archive => (
54 is => 'ro',
55 isa => 'Archive::Tar::Wrapper',
56 init_arg => undef,
57 lazy => 1,
58 builder => '_build_archive',
59 );
60  
61 has _export_root_dir => (
62 is => 'ro',
63 isa => Dir,
64 init_arg => undef,
65 lazy => 1,
66 builder => '_build_export_root_dir',
67 );
68  
69 has _wiki => (
70 is => 'rw',
71 isa => 'Silki::Schema::Wiki',
72 );
73  
74 has _user_id_map => (
75 traits => ['Hash'],
76 is => 'bare',
77 isa => HashRef,
78 init_arg => undef,
79 default => sub { {} },
80 handles => {
81 _new_user_id_for => 'get',
82 _set_user_id_mapping => 'set',
83 },
84 );
85  
86 has _page_id_map => (
87 traits => ['Hash'],
88 is => 'bare',
89 isa => HashRef,
90 init_arg => undef,
91 default => sub { {} },
92 handles => {
93 _new_page_id_for => 'get',
94 _set_page_id_mapping => 'set',
95 },
96 );
97  
98 has _pending_invitations => (
99 traits => ['Array'],
100 is => 'bare',
101 isa => ArrayRef,
102 init_arg => undef,
103 default => sub { [] },
104 handles => {
105 _users_to_invite => 'elements',
106 _add_pending_invitation => 'push',
107 },
108 );
109  
110 sub imported_wiki {
111 my $self = shift;
112  
113 $self->_import();
114  
115 return $self->_wiki();
116 }
117  
118 sub _import {
119 my $self = shift;
120  
121 $self->_disable_pg_triggers()
122 if $self->fast();
123  
124 eval {
125 Silki::Schema->RunInTransaction(
126 sub {
127 $self->_create_wiki();
128 $self->_import_users();
129 $self->_import_pages();
130 $self->_import_files();
131 }
132 );
133 };
134  
135 my $e = $@;
136  
137 $self->_enable_pg_triggers() if $self->fast();
138  
139 die $e if $e;
140  
141 $self->_rebuild_searchable_text() if $self->fast();
142  
143 # invite users
144 }
145  
146 sub _build_archive {
147 my $self = shift;
148  
149 $self->_maybe_log( loc('Reading the tarball (this may take a while)') );
150  
151 my $arch = Archive::Tar::Wrapper->new();
152 $arch->read( $self->_tarball() );
153  
154 return $arch;
155 }
156  
157 sub _create_wiki {
158 my $self = shift;
159  
160 my $wiki_data = Silki::JSON->Decode(
161 scalar read_file(
162 $self->_find_file_in_archive('wiki.json')->stringify()
163 )
164 );
165  
166 if ( Silki::Schema::Wiki->new( title => $wiki_data->{title} ) ) {
167 die loc(
168 'There is already a wiki with this title (%1)',
169 $wiki_data->{title}
170 );
171 }
172  
173 if ( Silki::Schema::Wiki->new( short_name => $wiki_data->{short_name} ) )
174 {
175 die loc(
176 'There is already a wiki with this short_name (%1)',
177 $wiki_data->{short_name}
178 );
179 }
180  
181 $self->_maybe_log(
182 loc( 'Creating a new wiki (%1)', $wiki_data->{title} ) );
183  
184 my $wiki = Silki::Schema::Wiki->insert(
185 title => $wiki_data->{title},
186 short_name => $wiki_data->{short_name},
187 user => $self->user(),
188 domain_id => $self->domain()->domain_id(),
189 skip_default_pages => 1,
190 );
191  
192 my $perm_data = Silki::JSON->Decode(
193 scalar read_file(
194 $self->_find_file_in_archive('permissions.json')->stringify()
195 )
196 );
197  
198 my %set = map { $_ => [ keys %{ $perm_data->{$_} } ] } keys %{$perm_data};
199  
200 $wiki->_set_permissions_from_set( \%set );
201  
202 $self->_set_wiki($wiki);
203  
204 return;
205 }
206  
207 sub _import_users {
208 my $self = shift;
209  
210 my $user_count = 0;
211  
212 for my $data (
213 map { Silki::JSON->Decode( scalar read_file( $_->stringify() ) ) }
214 grep { !$_->is_dir() }
215 $self->_export_root_dir()->subdir('users')->children()
216 ) {
217  
218 $self->_import_user($data);
219  
220 $user_count++;
221  
222 if ( $user_count % 10 == 0 ) {
223 $self->_maybe_log(
224 loc( 'Imported %quant( %1, user, users )', $user_count ) );
225 }
226 }
227  
228 if ( $user_count % 10 != 0 ) {
229 $self->_maybe_log(
230 loc( 'Imported %quant( %1, user, users )', $user_count ) );
231 }
232 }
233  
234 sub _import_user {
235 my $self = shift;
236 my $data = shift;
237  
238 my $user
239 = Silki::Schema::User->new( email_address => $data->{email_address} )
240 || Silki::Schema::User->new( username => $data->{username} );
241  
242 my $role = delete $data->{role_in_wiki};
243  
244 my $orig_user_id = delete $data->{user_id};
245  
246 if ($user) {
247 $self->_maybe_log(
248 loc(
249 'Skipping user already in system (%1)', $user->email_address()
250 )
251 );
252 }
253 elsif ( $data->{is_system_user} ) {
254 die loc(
255 'Found a system user that does not exist in this system (%1)!',
256 $data->{username}
257 );
258 }
259 else {
260 delete @{$data}{
261 qw(
262 is_system_user
263 creation_datetime
264 last_modified_datetime
265 )
266 };
267  
268 $user = Silki::Schema::User->insert(
269 %{$data},
270 requires_activation => 1,
271 user => $self->user(),
272 );
273  
274 if ( $data->{openid_uri} ) {
275 $user->update(
276 openid_uri => $data->{openid_uri},
277 user => $self->user(),
278 );
279 }
280 }
281  
282 if ($role) {
283 $self->_wiki()->add_user(
284 user => $user,
285 role => Silki::Schema::Role->$role(),
286 );
287  
288 $self->_add_pending_invitation($user);
289 }
290  
291 $self->_set_user_id_mapping( $orig_user_id => $user->user_id() );
292  
293 return;
294 }
295  
296 sub _import_pages {
297 my $self = shift;
298  
299 my $page_count = 0;
300 my $revision_count = 0;
301  
302 for my $page_dir ( grep { $_->is_dir() }
303 $self->_export_root_dir()->subdir('pages')->children() ) {
304  
305 my $page_data = Silki::JSON->Decode(
306 scalar read_file( $page_dir->file('page.json')->stringify() ) );
307  
308 my $old_page_id = delete $page_data->{page_id};
309  
310 my $page = Silki::Schema::Page->insert(
311 %{$page_data},
312 wiki_id => $self->_wiki()->wiki_id(),
313 user_id => $self->_new_user_id_for( $page_data->{user_id} ),
314 );
315  
316 $self->_set_page_id_mapping( $old_page_id => $page->page_id() );
317  
318 $page_count++;
319  
320 my @revision_files = sort {
321 my ($arev) = $a->basename() =~ /revision-(\d+)/;
322 my ($brev) = $b->basename() =~ /revision-(\d+)/;
323 $arev <=> $brev
324 }
325 grep { !$_->is_dir() && $_->basename() =~ /revision-\d+/ }
326 $page_dir->children();
327  
328 for my $revision_file (@revision_files) {
329 my $rev_data = Silki::JSON->Decode(
330 scalar read_file( $revision_file->stringify() ) );
331  
332 delete @{$rev_data}{qw( page_id revision_number )};
333  
334 local $Silki::Schema::PageRevision::SkipPostChangeHack
335 = $revision_file eq $revision_files[-1] ? 0 : 1;
336  
337 $page->add_revision(
338 %{$rev_data},
339 user_id => $self->_new_user_id_for( $rev_data->{user_id} ),
340 );
341  
342 $revision_count++;
343  
344 if ( $revision_count % 20 == 0 ) {
345 $self->_maybe_log(
346 loc(
347 'Imported %quant( %1, revision, revisions ) (%quant( %2, page, pages ))',
348 $revision_count,
349 $page_count
350 )
351 );
352 }
353 }
354 }
355  
356 if ( $revision_count % 20 != 0 ) {
357 $self->_maybe_log(
358 loc(
359 'Imported %quant( %1, revision, revisions ) (%quant( %2, page, pages ))',
360 $revision_count,
361 $page_count
362 )
363 );
364 }
365 }
366  
367 sub _import_files {
368 my $self = shift;
369  
370 my $file_count = 0;
371  
372 return unless -d $self->_export_root_dir()->subdir('files');
373  
374 for my $file_dir ( grep { $_->is_dir() }
375 $self->_export_root_dir()->subdir('files')->children() ) {
376  
377 my $data = Silki::JSON->Decode(
378 scalar read_file( $file_dir->file('file.json')->stringify() ) );
379  
380 delete $data->{file_id};
381  
382 my $data_file = $file_dir->file( $data->{filename} );
383  
384 my $file = Silki::Schema::File->insert(
385 %{$data},
386 file_size => -s $data_file,
387 contents => scalar read_file( $data_file->stringify() ),
388 page_id => $self->_new_page_id_for( $data->{page_id} ),
389 );
390  
391 $file_count++;
392  
393 if ( $file_count % 10 == 0 ) {
394 $self->_maybe_log(
395 loc( 'Imported %quant( %1, file, files )', $file_count ) );
396 }
397 }
398  
399 if ( $file_count % 10 != 0 ) {
400 $self->_maybe_log(
401 loc( 'Imported %quant( %1, file, files )', $file_count ) );
402 }
403 }
404  
405 sub _find_file_in_archive {
406 my $self = shift;
407  
408 $self->_export_root_dir()->file(@_);
409 }
410  
411 sub _build_export_root_dir {
412 my $self = shift;
413  
414 my $tardir = dir( $self->_archive()->tardir() );
415  
416 my ($dir) = ( glob( $tardir->subdir('export-of-*') ) )[0];
417  
418 return dir($dir);
419 }
420  
421 sub _disable_pg_triggers {
422 my $self = shift;
423  
424 my $dbh = Silki::Schema->DBIManager()->default_source()->dbh();
425  
426 $dbh->do(q{ALTER TABLE "Page" DISABLE TRIGGER USER});
427 $dbh->do(q{ALTER TABLE "PageRevision" DISABLE TRIGGER USER});
428 }
429  
430 sub _enable_pg_triggers {
431 my $self = shift;
432  
433 my $dbh = Silki::Schema->DBIManager()->default_source()->dbh();
434  
435 $dbh->do(q{ALTER TABLE "Page" ENABLE TRIGGER USER});
436 $dbh->do(q{ALTER TABLE "PageRevision" ENABLE TRIGGER USER});
437 }
438  
439 sub _rebuild_searchable_text {
440 my $self = shift;
441  
442 my $sql = <<'EOF';
443 INSERT INTO "PageSearchableText"
444 (page_id, ts_text)
445 SELECT pages.page_id,
446 setweight(to_tsvector('pg_catalog.english', pages.title), 'A') ||
447 setweight(to_tsvector('pg_catalog.english', pages.content), 'B')
448 FROM ( SELECT p.page_id, p.title, pr.content
449 FROM "Page" AS p, "PageRevision" AS pr
450 WHERE revision_number =
451 ( SELECT MAX(revision_number)
452 FROM "PageRevision"
453 WHERE page_id = p.page_id )
454 AND p.page_id = pr.page_id
455 AND p.wiki_id = ?
456 ) AS pages
457 EOF
458  
459 my $dbh = Silki::Schema->DBIManager()->default_source()->dbh();
460  
461 $dbh->do( $sql, {}, $self->_wiki()->wiki_id() );
462 }
463  
464 __PACKAGE__->meta()->make_immutable();
465  
466 1;