Bug 13642 - Adding new features for Dublin Core metadata
[koha.git] / C4 / HTML5Media.pm
bloba53922ef81f9731c6661bc3450599fea8706d23d
1 package C4::HTML5Media;
3 # Copyright 2012/2015 Mirko Tietgen
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use strict;
21 use warnings;
23 use C4::Context;
24 use MARC::Field;
25 use Koha::Upload;
27 =head1 HTML5Media
29 C4::HTML5Media
31 =head1 Description
33 This module gets the relevant data from field 856 (MARC21/UNIMARC) to create a HTML5 audio or video element containing the file(s) catalogued in 856.
35 =cut
37 =head2 gethtml5media
39 Get all relevant data from field 856. Takes a $record in the subroutine call, sets appropriate params.
41 =cut
43 sub gethtml5media {
44 my $self = shift;
45 my $record = shift;
46 my @HTML5Media_sets = ();
47 my @HTML5Media_fields = $record->field(856);
48 my $HTML5MediaParent;
49 my $HTML5MediaWidth;
50 my @HTML5MediaExtensions = split( /\|/, C4::Context->preference("HTML5MediaExtensions") );
51 my $marcflavour = C4::Context->preference("marcflavour");
52 foreach my $HTML5Media_field (@HTML5Media_fields) {
53 my %HTML5Media;
54 # protocol
55 if ( $HTML5Media_field->indicator(1) eq '1' ) {
56 $HTML5Media{protocol} = 'ftp';
58 elsif ( $HTML5Media_field->indicator(1) eq '4' ) {
59 $HTML5Media{protocol} = 'http';
61 elsif ( $HTML5Media_field->indicator(1) eq '7' ) {
62 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC') {
63 $HTML5Media{protocol} = $HTML5Media_field->subfield('2');
65 elsif ($marcflavour eq 'UNIMARC') {
66 $HTML5Media{protocol} = $HTML5Media_field->subfield('y');
69 else {
70 $HTML5Media{protocol} = 'http';
72 # user
73 if ( $HTML5Media_field->subfield('l') ) {
74 $HTML5Media{username} = $HTML5Media_field->subfield('l'); # yes, that is arbitrary if h and l are not the same. originally i flipped a coin in that case.
76 elsif ( $HTML5Media_field->subfield('h') ) {
77 $HTML5Media{username} = $HTML5Media_field->subfield('h');
79 # user/pass
80 if ( $HTML5Media{username} && $HTML5Media_field->subfield('k') ) {
81 $HTML5Media{loginblock} = $HTML5Media{username} . ':' . $HTML5Media_field->subfield('k') . '@';
83 elsif ( $HTML5Media{username} ) {
84 $HTML5Media{loginblock} = $HTML5Media{username} . '@';
86 else {
87 $HTML5Media{loginblock} = '';
89 # port
90 if ( $HTML5Media_field->subfield('p') ) {
91 $HTML5Media{portblock} = ':' . $HTML5Media_field->subfield('k');
93 else {
94 $HTML5Media{portblock} = '';
96 # src
97 if ( $HTML5Media_field->subfield('u') ) {
98 $HTML5Media{srcblock} = $HTML5Media_field->subfield('u');
100 elsif ( $HTML5Media_field->subfield('a') && $HTML5Media_field->subfield('d') && $HTML5Media_field->subfield('f') ) {
101 $HTML5Media{host} = $HTML5Media_field->subfield('a');
102 $HTML5Media{host} =~ s/(^\/|\/$)//g;
103 $HTML5Media{path} = $HTML5Media_field->subfield('d');
104 $HTML5Media{path} =~ s/(^\/|\/$)//g;
105 $HTML5Media{file} = $HTML5Media_field->subfield('f');
106 $HTML5Media{srcblock} = $HTML5Media{protocol} . '://' . $HTML5Media{loginblock} . $HTML5Media{host} . $HTML5Media{portblock} . '/' . $HTML5Media{path} . '/' . $HTML5Media{file};
108 else {
109 next; # no file to play
111 # extension
112 # check uploaded files
113 if ( $HTML5Media{srcblock} =~ /\Qopac-retrieve-file.pl\E/ ) {
114 my ( undef, $id ) = split /id=/, $HTML5Media{srcblock};
115 next if !$id;
116 my $public = ( ( caller )[1] =~ /opac/ ) ? { public => 1 }: {};
117 my $upl = Koha::Upload->new( $public )->get({ hashvalue => $id });
118 next if !$upl || $upl->{name} !~ /\./;
119 $HTML5Media{extension} = ( $upl->{name} =~ m/([^.]+)$/ )[0];
121 # check remote files
122 else {
123 $HTML5Media{extension} = ($HTML5Media{srcblock} =~ m/([^.]+)$/)[0];
125 if ( !grep /\Q$HTML5Media{extension}\E/, @HTML5MediaExtensions ) {
126 next; # not a specified media file
128 # mime
129 if ( $HTML5Media_field->subfield('c') ) {
130 $HTML5Media{codecs} = $HTML5Media_field->subfield('c');
132 ### from subfield q…
133 if ( $HTML5Media_field->subfield('q') ) {
134 $HTML5Media{mime} = $HTML5Media_field->subfield('q');
136 ### …or from file extension and codecs…
137 elsif ( $HTML5Media{codecs} ) {
138 if ( $HTML5Media{codecs} =~ /theora.*vorbis/ ) {
139 $HTML5Media{mime} = 'video/ogg';
141 elsif ( $HTML5Media{codecs} =~ /vp8.*vorbis/ ) {
142 $HTML5Media{mime} = 'video/webm';
144 elsif ( ($HTML5Media{codecs} =~ /^vorbis$/) && ($HTML5Media{extension} eq 'ogg') ) {
145 $HTML5Media{mime} = 'audio/ogg';
147 elsif ( ($HTML5Media{codecs} =~ /^vorbis$/) && ($HTML5Media{extension} eq 'webm') ) {
148 $HTML5Media{mime} = 'audio/webm';
151 ### …or just from file extension
152 else {
153 if ( $HTML5Media{extension} eq 'ogv' ) {
154 $HTML5Media{mime} = 'video/ogg';
155 $HTML5Media{codecs} = 'theora,vorbis';
157 if ( $HTML5Media{extension} eq 'oga' ) {
158 $HTML5Media{mime} = 'audio/ogg';
159 $HTML5Media{codecs} = 'vorbis';
161 elsif ( $HTML5Media{extension} eq 'spx' ) {
162 $HTML5Media{mime} = 'audio/ogg';
163 $HTML5Media{codecs} = 'speex';
165 elsif ( $HTML5Media{extension} eq 'opus' ) {
166 $HTML5Media{mime} = 'audio/ogg';
167 $HTML5Media{codecs} = 'opus';
169 elsif ( $HTML5Media{extension} eq 'vtt' ) {
170 $HTML5Media{mime} = 'text/vtt';
173 # codecs
174 if ( $HTML5Media{codecs} ) {
175 $HTML5Media{codecblock} = '; codecs="' . $HTML5Media{codecs} . '"';
177 else {
178 $HTML5Media{codecblock} = '';
180 # type
181 if ( $HTML5Media{mime} ) {
182 $HTML5Media{typeblock} = ' type=\'' . $HTML5Media{mime} . $HTML5Media{codecblock} . '\'';
184 else {
185 $HTML5Media{typeblock} = '';
187 # element
188 if ( $HTML5Media{mime} =~ /audio/ ) {
189 $HTML5Media{type} = 'audio';
191 elsif ( $HTML5Media{mime} =~ /video/ ) {
192 $HTML5Media{type} = 'video';
194 elsif ( $HTML5Media{mime} =~ /text/ ) {
195 $HTML5Media{type} = 'track';
197 # push
198 if ( $HTML5Media{srcblock} && $HTML5Media{type} ) {
199 push (@HTML5Media_sets, \%HTML5Media);
202 # parent element
203 for my $media ( @HTML5Media_sets ) {
204 if ( ($media->{mime}) && ($media->{mime} =~ /audio/) ) {
205 if ( $HTML5MediaParent ne 'video' ) {
206 $HTML5MediaParent = 'audio';
207 $HTML5MediaWidth = '';
210 elsif ( ($media->{mime}) && ($media->{mime} =~ /video/) ) {
211 $HTML5MediaParent = 'video';
212 $HTML5MediaWidth = ' width="480"';
215 # child element
216 for my $media ( @HTML5Media_sets ) {
217 if ( ($media->{type}) && ( ($media->{type} eq 'video') || ($media->{type} eq 'audio') ) ) {
218 if ( $media->{type} eq $HTML5MediaParent ) {
219 $media->{child} = 'source';
222 else {
223 $media->{child} = $media->{type};
227 return (
228 HTML5MediaEnabled => ( (scalar(@HTML5Media_sets) > 0) && ($HTML5MediaParent) ),
229 HTML5MediaSets => \@HTML5Media_sets,
230 HTML5MediaParent => $HTML5MediaParent,
231 HTML5MediaWidth => $HTML5MediaWidth,