1 # @tag: add_file_version
2 # @description: Versionen für files in extra Tabelle erzeugen
3 # @depends: release_3_8_0 file_version files_add_uid
4 package SL::DBUpgrade2::add_file_version;
10 use SL::File::Backend::Webdav;
12 use SL::Locale::String qw(t8);
13 use SL::System::Process;
15 use UUID::Tiny ':std';
17 use parent qw(SL::DBUpgrade2::Base);
19 sub get_all_versions {
23 my $fileobj = SL::File::Object->new(
29 my $maxversion = $fileobj->backend eq 'Filesystem' ? $fileobj->db_file->backend_data : 1;
30 $fileobj->version($maxversion);
31 push @versionobjs, $fileobj;
32 if ($maxversion > 1) {
33 for my $version (2..$maxversion) {
34 my $clone = $fileobj->clone;
35 $clone->version($maxversion-$version+1);
37 push @versionobjs, $clone;
47 my $file_id = $file_obj->id;
48 my $backend = $file_obj->backend
49 or die "File with ID '$file_id' has no backend specified.";
50 my $db_file = $file_obj->db_file;
51 my $file_name = $file_obj->file_name;
52 my $version = $file_obj->version;
55 if ($backend eq 'Webdav') {
56 ($path) = $file_obj->backend_class->webdav_path($db_file);
57 } elsif ($backend eq 'Filesystem') {
58 # use filesystem with depth 3
59 my $iddir = sprintf("%04d", $db_file->id % 1000);
60 my $base_path = File::Spec->catdir($::lx_office_conf{paths}->{document_path}, $::auth->client->{id}, $iddir, $db_file->id);
61 $path = File::Spec->catdir($base_path, $db_file->id . '_' . $version);
63 die "Unknown backend '$backend' for file with ID '$file_id'.";
66 die "No file found at $path. Expected: $file_name, file.id: $file_id" if !-f $path;
70 sub test_all_files_exists {
73 my $all_dbfiles = SL::DB::Manager::File->get_all;
75 foreach my $dbfile (@{$all_dbfiles}) {
76 my @versions = get_all_versions($dbfile);
77 foreach my $version (@versions) {
79 get_filepath($version);
90 my ($self, $errors) = @_;
92 print $::form->parse_html_template("dbupgrade/add_file_version_form", {
103 FULL OUTER JOIN file_versions fv ON (f.id = fv.file_id)
105 HAVING count(fv.file_id) = 0
107 my @file_ids_without_version = map {$_->[0]} @{$self->dbh->selectall_arrayref($query)};
109 unless ($::form->{delete_file_entries_of_missing_files}) {
110 my @errors = $self->test_all_files_exists();
111 if (scalar @errors) {
112 $self->print_errors(\@errors);
117 SL::DB->client->with_transaction(sub {
119 my $all_dbfiles = SL::DB::Manager::File->get_all;
120 foreach my $dbfile (@$all_dbfiles) {
121 my $file_id = $dbfile->id;
122 my $backend = $dbfile->backend
123 or die "File with ID '$file_id' has no backend specified.";
126 if ($backend eq 'Webdav') {
127 $doc_path = SL::File::Backend::Webdav::get_rootdir();
128 } elsif ($backend eq 'Filesystem') {
129 $doc_path = $::lx_office_conf{paths}->{document_path};
131 die "Unknown backend '$backend' for file with ID '$file_id'.";
134 my @versions = get_all_versions($dbfile);
135 foreach my $version (@versions) {
138 $tofile = get_filepath($version);
142 my $rel_file = $tofile;
143 $rel_file =~ s/$doc_path//;
145 my $fv = SL::DB::FileVersion->new(
146 file_id => $dbfile->id,
147 version => $version->version || 1,
148 file_location => $rel_file,
149 doc_path => $doc_path,
150 backend => $dbfile->backend,
151 guid => create_uuid_as_string(UUID_V4),
159 FULL OUTER JOIN file_versions fv ON (f.id = fv.file_id)
161 HAVING count(fv.file_id) = 0
163 my @file_ids_without_version =
165 @{SL::DB::->client->dbh->selectall_arrayref($query)};
166 if (scalar @file_ids_without_version) {
167 if ($::form->{delete_file_entries_of_missing_files}) {
168 SL::DB::Manager::File->delete_all(where => [id => \@file_ids_without_version]);
170 die "Files without versions: " . join(', ', @file_ids_without_version);
175 }) or do { die SL::DB->client->error };