Bug 14106: (RM followup) sick of failing tests in Jessie
[koha.git] / C4 / HTML5Media.pm
blobc8c8a77ca8cd66c7dea662dd5a8bcebca30b8464
1 package C4::HTML5Media;
3 # Copyright 2012 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;
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 $HTML5Media{extension} = ($HTML5Media{srcblock} =~ m/([^.]+)$/)[0];
113 if ( !grep /\Q$HTML5Media{extension}\E/, @HTML5MediaExtensions ) {
114 next; # not a specified media file
116 # mime
117 if ( $HTML5Media_field->subfield('c') ) {
118 $HTML5Media{codecs} = $HTML5Media_field->subfield('c');
120 ### from subfield q…
121 if ( $HTML5Media_field->subfield('q') ) {
122 $HTML5Media{mime} = $HTML5Media_field->subfield('q');
124 ### …or from file extension and codecs…
125 elsif ( $HTML5Media{codecs} ) {
126 if ( $HTML5Media{codecs} =~ /theora.*vorbis/ ) {
127 $HTML5Media{mime} = 'video/ogg';
129 elsif ( $HTML5Media{codecs} =~ /vp8.*vorbis/ ) {
130 $HTML5Media{mime} = 'video/webm';
132 elsif ( ($HTML5Media{codecs} =~ /^vorbis$/) && ($HTML5Media{extension} eq 'ogg') ) {
133 $HTML5Media{mime} = 'audio/ogg';
135 elsif ( ($HTML5Media{codecs} =~ /^vorbis$/) && ($HTML5Media{extension} eq 'webm') ) {
136 $HTML5Media{mime} = 'audio/webm';
139 ### …or just from file extension
140 else {
141 if ( $HTML5Media{extension} eq 'ogv' ) {
142 $HTML5Media{mime} = 'video/ogg';
143 $HTML5Media{codecs} = 'theora,vorbis';
145 if ( $HTML5Media{extension} eq 'oga' ) {
146 $HTML5Media{mime} = 'audio/ogg';
147 $HTML5Media{codecs} = 'vorbis';
149 elsif ( $HTML5Media{extension} eq 'spx' ) {
150 $HTML5Media{mime} = 'audio/ogg';
151 $HTML5Media{codecs} = 'speex';
153 elsif ( $HTML5Media{extension} eq 'opus' ) {
154 $HTML5Media{mime} = 'audio/ogg';
155 $HTML5Media{codecs} = 'opus';
157 elsif ( $HTML5Media{extension} eq 'vtt' ) {
158 $HTML5Media{mime} = 'text/vtt';
161 # codecs
162 if ( $HTML5Media{codecs} ) {
163 $HTML5Media{codecblock} = '; codecs="' . $HTML5Media{codecs} . '"';
165 else {
166 $HTML5Media{codecblock} = '';
168 # type
169 if ( $HTML5Media{mime} ) {
170 $HTML5Media{typeblock} = ' type=\'' . $HTML5Media{mime} . $HTML5Media{codecblock} . '\'';
172 else {
173 $HTML5Media{typeblock} = '';
175 # element
176 if ( $HTML5Media{mime} =~ /audio/ ) {
177 $HTML5Media{type} = 'audio';
179 elsif ( $HTML5Media{mime} =~ /video/ ) {
180 $HTML5Media{type} = 'video';
182 elsif ( $HTML5Media{mime} =~ /text/ ) {
183 $HTML5Media{type} = 'track';
185 # push
186 if ( $HTML5Media{srcblock} && $HTML5Media{type} ) {
187 push (@HTML5Media_sets, \%HTML5Media);
190 # parent element
191 for my $media ( @HTML5Media_sets ) {
192 if ( ($media->{mime}) && ($media->{mime} =~ /audio/) ) {
193 if ( $HTML5MediaParent ne 'video' ) {
194 $HTML5MediaParent = 'audio';
195 $HTML5MediaWidth = '';
198 elsif ( ($media->{mime}) && ($media->{mime} =~ /video/) ) {
199 $HTML5MediaParent = 'video';
200 $HTML5MediaWidth = ' width="480"';
203 # child element
204 for my $media ( @HTML5Media_sets ) {
205 if ( ($media->{type}) && ( ($media->{type} eq 'video') || ($media->{type} eq 'audio') ) ) {
206 if ( $media->{type} eq $HTML5MediaParent ) {
207 $media->{child} = 'source';
210 else {
211 $media->{child} = $media->{type};
215 return (
216 HTML5MediaEnabled => ( (scalar(@HTML5Media_sets) > 0) && ($HTML5MediaParent) ),
217 HTML5MediaSets => \@HTML5Media_sets,
218 HTML5MediaParent => $HTML5MediaParent,
219 HTML5MediaWidth => $HTML5MediaWidth,