Bug 18837: (follow-up) Add additional tests
[koha.git] / C4 / HTML5Media.pm
blobc77e8a40bc628c44da2f2e4721b015fda148229e
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::UploadedFiles;
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 $HTML5MediaYouTube = C4::Context->preference("HTML5MediaYouTube");
52 my $marcflavour = C4::Context->preference("marcflavour");
53 foreach my $HTML5Media_field (@HTML5Media_fields) {
54 my $isyoutube = 0;
55 my %HTML5Media;
56 # protocol
57 if ( $HTML5Media_field->indicator(1) eq '1' ) {
58 $HTML5Media{protocol} = 'ftp';
60 elsif ( $HTML5Media_field->indicator(1) eq '4' ) {
61 $HTML5Media{protocol} = 'http';
63 elsif ( $HTML5Media_field->indicator(1) eq '7' ) {
64 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC') {
65 $HTML5Media{protocol} = $HTML5Media_field->subfield('2');
67 elsif ($marcflavour eq 'UNIMARC') {
68 $HTML5Media{protocol} = $HTML5Media_field->subfield('y');
71 else {
72 $HTML5Media{protocol} = 'http';
74 # user
75 if ( $HTML5Media_field->subfield('l') ) {
76 $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.
78 elsif ( $HTML5Media_field->subfield('h') ) {
79 $HTML5Media{username} = $HTML5Media_field->subfield('h');
81 # user/pass
82 if ( $HTML5Media{username} && $HTML5Media_field->subfield('k') ) {
83 $HTML5Media{loginblock} = $HTML5Media{username} . ':' . $HTML5Media_field->subfield('k') . '@';
85 elsif ( $HTML5Media{username} ) {
86 $HTML5Media{loginblock} = $HTML5Media{username} . '@';
88 else {
89 $HTML5Media{loginblock} = '';
91 # port
92 if ( $HTML5Media_field->subfield('p') ) {
93 $HTML5Media{portblock} = ':' . $HTML5Media_field->subfield('k');
95 else {
96 $HTML5Media{portblock} = '';
98 # src
99 if ( $HTML5Media_field->subfield('u') ) {
100 $HTML5Media{srcblock} = $HTML5Media_field->subfield('u');
101 if (grep /youtu\.?be/, $HTML5Media_field->subfield('u') ) {
102 if ($HTML5MediaYouTube == 1) {
103 require WWW::YouTube::Download;
104 import WWW::YouTube::Download qw(playback_url);
105 my $youtube = WWW::YouTube::Download->new;
106 eval {
107 $HTML5Media{srcblock} = $youtube->playback_url(
108 $HTML5Media_field->subfield('u'), {
109 'fmt' => '43' #webm is the only format compatible to all modern browsers. maybe check for available qualities
113 if ($@) { warn $@; }
114 else { $isyoutube = 1;}
116 else {
117 next; # do not embed youtube videos
121 elsif ( $HTML5Media_field->subfield('a') && $HTML5Media_field->subfield('d') && $HTML5Media_field->subfield('f') ) {
122 $HTML5Media{host} = $HTML5Media_field->subfield('a');
123 $HTML5Media{host} =~ s/(^\/|\/$)//g;
124 $HTML5Media{path} = $HTML5Media_field->subfield('d');
125 $HTML5Media{path} =~ s/(^\/|\/$)//g; # TODO we could check for youtube here too, but nobody uses these fields anyway…
126 $HTML5Media{file} = $HTML5Media_field->subfield('f');
127 $HTML5Media{srcblock} = $HTML5Media{protocol} . '://' . $HTML5Media{loginblock} . $HTML5Media{host} . $HTML5Media{portblock} . '/' . $HTML5Media{path} . '/' . $HTML5Media{file};
129 else {
130 next; # no file to play
132 # extension
133 # check uploaded files
134 if ( $HTML5Media{srcblock} =~ /\Qopac-retrieve-file.pl\E/ ) {
135 my ( undef, $id ) = split /id=/, $HTML5Media{srcblock};
136 next if !$id;
137 my %public = ( ( caller )[1] =~ /opac/ ) ? ( public => 1 ): ();
138 my $upload = Koha::UploadedFiles->search({
139 hashvalue => $id, %public,
140 })->next;
141 next if !$upload || $upload->filename !~ /\./;
142 $HTML5Media{extension} = ( $upload->filename =~ m/([^.]+)$/ )[0];
144 # check remote files
145 else {
146 $HTML5Media{extension} = ($HTML5Media{srcblock} =~ m/([^.]+)$/)[0];
148 if ( ( !grep /\Q$HTML5Media{extension}\E/, @HTML5MediaExtensions ) && ( $isyoutube != 1) ) {
149 next; # not a specified media file
151 # youtube
152 if ($isyoutube == 1) {
153 $HTML5Media{mime} = 'video/webm';
155 # mime
156 if ( $HTML5Media_field->subfield('c') ) {
157 $HTML5Media{codecs} = $HTML5Media_field->subfield('c');
159 ### from subfield q…
160 if ( $HTML5Media_field->subfield('q') ) {
161 $HTML5Media{mime} = $HTML5Media_field->subfield('q');
163 ### …or from file extension and codecs…
164 elsif ( $HTML5Media{codecs} ) {
165 if ( $HTML5Media{codecs} =~ /theora.*vorbis/ ) {
166 $HTML5Media{mime} = 'video/ogg';
168 elsif ( $HTML5Media{codecs} =~ /vp8.*vorbis/ ) {
169 $HTML5Media{mime} = 'video/webm';
171 elsif ( ($HTML5Media{codecs} =~ /^vorbis$/) && ($HTML5Media{extension} eq 'ogg') ) {
172 $HTML5Media{mime} = 'audio/ogg';
174 elsif ( ($HTML5Media{codecs} =~ /^vorbis$/) && ($HTML5Media{extension} eq 'webm') ) {
175 $HTML5Media{mime} = 'audio/webm';
178 ### …or just from file extension
179 else {
180 if ( $HTML5Media{extension} eq 'ogv' ) {
181 $HTML5Media{mime} = 'video/ogg';
182 $HTML5Media{codecs} = 'theora,vorbis';
184 if ( $HTML5Media{extension} eq 'oga' ) {
185 $HTML5Media{mime} = 'audio/ogg';
186 $HTML5Media{codecs} = 'vorbis';
188 elsif ( $HTML5Media{extension} eq 'spx' ) {
189 $HTML5Media{mime} = 'audio/ogg';
190 $HTML5Media{codecs} = 'speex';
192 elsif ( $HTML5Media{extension} eq 'opus' ) {
193 $HTML5Media{mime} = 'audio/ogg';
194 $HTML5Media{codecs} = 'opus';
196 elsif ( $HTML5Media{extension} eq 'vtt' ) {
197 $HTML5Media{mime} = 'text/vtt';
200 # codecs
201 if ( $HTML5Media{codecs} ) {
202 $HTML5Media{codecblock} = '; codecs="' . $HTML5Media{codecs} . '"';
204 else {
205 $HTML5Media{codecblock} = '';
207 # type
208 if ( $HTML5Media{mime} ) {
209 $HTML5Media{typeblock} = ' type=\'' . $HTML5Media{mime} . $HTML5Media{codecblock} . '\'';
211 else {
212 $HTML5Media{typeblock} = '';
214 # element
215 if ( $HTML5Media{mime} =~ /audio/ ) {
216 $HTML5Media{type} = 'audio';
218 elsif ( $HTML5Media{mime} =~ /video/ ) {
219 $HTML5Media{type} = 'video';
221 elsif ( $HTML5Media{mime} =~ /text/ ) {
222 $HTML5Media{type} = 'track';
224 # push
225 if ( $HTML5Media{srcblock} && $HTML5Media{type} ) {
226 push (@HTML5Media_sets, \%HTML5Media);
229 # parent element
230 for my $media ( @HTML5Media_sets ) {
231 if ( ($media->{mime}) && ($media->{mime} =~ /audio/) ) {
232 if ( $HTML5MediaParent ne 'video' ) {
233 $HTML5MediaParent = 'audio';
234 $HTML5MediaWidth = '';
237 elsif ( ($media->{mime}) && ($media->{mime} =~ /video/) ) {
238 $HTML5MediaParent = 'video';
239 $HTML5MediaWidth = ' width="480"';
242 # child element
243 for my $media ( @HTML5Media_sets ) {
244 if ( ($media->{type}) && ( ($media->{type} eq 'video') || ($media->{type} eq 'audio') ) ) {
245 if ( $media->{type} eq $HTML5MediaParent ) {
246 $media->{child} = 'source';
249 else {
250 $media->{child} = $media->{type};
254 return (
255 HTML5MediaEnabled => ( (scalar(@HTML5Media_sets) > 0) && ($HTML5MediaParent) ),
256 HTML5MediaSets => \@HTML5Media_sets,
257 HTML5MediaParent => $HTML5MediaParent,
258 HTML5MediaWidth => $HTML5MediaWidth,