Silki/Wiki/Exporter.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 162 CC 7 SCAR 18.0
CC
7
Cov
0%
_add_users_to_archive
20.0
_add_pages_to_archive
12.0
_add_files_to_archive
6.0
CRAP 6.1
1 package Silki::Wiki::Exporter;
2 {
3 $Silki::Wiki::Exporter::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( write_file );
12 use File::Spec;
13 use File::Temp qw( tempdir );
14 use Path::Class qw( file );
15 use Silki::I18N qw( loc );
16 use Silki::JSON;
17 use Silki::Schema::Wiki;
18 use Silki::Types qw( Dir HashRef Tarball );
19  
20 use Moose;
21 use MooseX::SemiAffordanceAccessor;
22  
23 with 'Silki::Role::OptionalLog';
24  
25 has _wiki => (
26 is => 'ro',
27 isa => 'Silki::Schema::Wiki',
28 init_arg => 'wiki',
29 required => 1,
30 );
31  
32 has tarball => (
33 is => 'ro',
34 isa => Tarball,
35 init_arg => undef,
36 lazy => 1,
37 builder => '_build_tarball',
38 );
39  
40 has _dir => (
41 is => 'ro',
42 isa => Dir,
43 init_arg => undef,
44 lazy => 1,
45 default => sub { 'export-of-' . $_[0]->_wiki()->short_name() },
46 coerce => 1,
47 );
48  
49 has _archive => (
50 is => 'ro',
51 isa => 'Archive::Tar::Wrapper',
52 init_arg => undef,
53 lazy => 1,
54 default => sub { Archive::Tar::Wrapper->new() },
55 );
56  
57 has _user_ids_from_pages => (
58 traits => ['Hash'],
59 is => 'bare',
60 isa => HashRef,
61 init_arg => undef,
62 default => sub { {} },
63 handles => {
64 _user_ids_from_pages => 'keys',
65 _add_user_id_from_page => 'set',
66 _delete_user_id_from_page => 'delete',
67 },
68 );
69  
70 use constant EXPORT_FORMAT_VERSION => 1;
71  
72 # Exists for the benefit of the test code
73 sub _export_format_version {
74 return EXPORT_FORMAT_VERSION;
75 }
76  
77 sub _build_tarball {
78 my $self = shift;
79  
80 $self->_add_to_archive(
81 $self->_dir()->file('export-metadata.json'), {
82 silki_version => Silki->VERSION(),
83 export_format_version => EXPORT_FORMAT_VERSION,
84 },
85 );
86  
87 $self->_add_to_archive(
88 $self->_dir()->file('wiki.json'),
89 $self->_wiki()->serialize(),
90 );
91  
92 $self->_add_to_archive(
93 $self->_dir()->file('permissions.json'),
94 $self->_wiki()->permissions(),
95 );
96  
97 $self->_add_pages_to_archive();
98 $self->_add_users_to_archive();
99 $self->_add_files_to_archive();
100  
101 my $tarball = file(
102 tempdir( CLEANUP => 1 ),
103 'export-of-' . $self->_wiki()->short_name() . '.tar.gz'
104 );
105  
106 $self->_maybe_log( loc('Creating the tarball (this may take a while).') );
107  
108 $self->_archive()->write( $tarball, 'compress' );
109  
110 return $tarball;
111 }
112  
113 sub _add_pages_to_archive {
114 my $self = shift;
115  
116 my $revision_count = 0;
117  
118 my $pages = $self->_wiki()->pages();
119  
120 my %users;
121  
122 while ( my $page = $pages->next() ) {
123  
124 my $page_dir = $self->_dir()->subdir( 'pages', $page->uri_path() );
125  
126 $self->_add_to_archive(
127 $page_dir->file('page.json'),
128 $page->serialize,
129 );
130  
131 my $revisions = $page->revisions();
132 while ( my $revision = $revisions->next() ) {
133  
134 my $revision_file = $page_dir->file(
135 'revision-' . $revision->revision_number() . '.json' );
136  
137 $self->_add_to_archive(
138 $page_dir->file(
139 'revision-' . $revision->revision_number() . '.json'
140 ),
141 $revision->serialize,
142 );
143  
144 $revision_count++;
145  
146 if ( $revision_count % 50 == 0 ) {
147 $self->_maybe_log(
148 loc(
149 'Exported %1 revisions (of %quant( %2, page, pages ))',
150 $revision_count,
151 $pages->index(),
152 )
153 );
154 }
155  
156 $self->_add_user_id_from_page( $revision->user_id(), 1 );
157 }
158 }
159  
160 if ( $revision_count % 50 != 0 ) {
161 $self->_maybe_log(
162 loc(
163 'Exported %1 revisions (of %quant( %2, page, pages ))',
164 $revision_count,
165 $pages->index(),
166 )
167 );
168 }
169 }
170  
171 sub _add_users_to_archive {
172 my $self = shift;
173  
174 my $user_count = 0;
175  
176 my $members = $self->_wiki()->members();
177 while ( my ( $user, $role ) = $members->next() ) {
178  
179 $self->_delete_user_id_from_page( $user->user_id() );
180  
181 my $ser = $user->serialize();
182 $ser->{role_in_wiki} = $role->name();
183  
184 $self->_add_to_archive(
185 $self->_dir()
186 ->file( 'users', 'user-' . $user->user_id() . '.json' ),
187 $ser,
188 );
189  
190 $user_count++;
191  
192 if ( $user_count % 20 == 0 ) {
193 $self->_maybe_log(
194 loc(
195 'Exported %quant( %1, user, users )',
196 $user_count,
197 )
198 );
199 }
200 }
201  
202 for my $user ( map { Silki::Schema::User->new( user_id => $_ ) }
203 $self->_user_ids_from_pages() ) {
204  
205 $self->_add_to_archive(
206 $self->_dir()
207 ->file( 'users', 'user-' . $user->user_id() . '.json' ),
208 $user->serialize(),
209 );
210  
211 $user_count++;
212  
213 if ( $user_count % 20 == 0 ) {
214 $self->_maybe_log(
215 loc(
216 'Exported %quant( %1, user, users )',
217 $user_count,
218 )
219 );
220 }
221 }
222  
223 if ( $user_count % 20 != 0 ) {
224 $self->_maybe_log(
225 loc(
226 'Exported %quant( %1, user, users )',
227 $user_count,
228 )
229 );
230 }
231 }
232  
233 sub _add_files_to_archive {
234 my $self = shift;
235  
236 my $files = $self->_wiki()->files();
237  
238 while ( my $file = $files->next() ) {
239 my $dir
240 = $self->_dir()->subdir( 'files', 'file-' . $file->file_id() );
241  
242 $self->_add_to_archive(
243 $dir->file('file.json'),
244 $file->serialize(),
245 );
246  
247 $self->_archive->add(
248 $dir->file( $file->filename() ),
249 \( $file->contents() ),
250 );
251  
252 if ( $files->index() % 20 == 0 ) {
253 $self->_maybe_log(
254 loc(
255 'Exported %quant( %1, file, files )',
256 $files->index(),
257 )
258 );
259 }
260 }
261 }
262  
263 sub _add_to_archive {
264 my $self = shift;
265 my $path = shift;
266 my $data = shift;
267  
268 $self->_archive()->add(
269 $path,
270 \( Silki::JSON->Encode($data) ),
271 { binmode => ':utf8' }
272 );
273  
274 return;
275 }
276  
277 __PACKAGE__->meta()->make_immutable();
278  
279 1;