Bug 16198: Fix regression in OPAC suggestions
[koha.git] / C4 / HTML5Media.pm
blobb4389332a75aa6c0962c41f22bdf42979554226d
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 $HTML5MediaYouTube = C4::Context->preference("HTML5MediaYouTube");
52 my $marcflavour = C4::Context->preference("marcflavour");
53 my $isyoutube = 0;
54 foreach my $HTML5Media_field (@HTML5Media_fields) {
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 /youtube/, $HTML5Media_field->subfield('u') ) { # TODO is there an official YT URL shortener? Can we use that too?
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 $HTML5Media{srcblock} = $youtube->playback_url(
107 $HTML5Media_field->subfield('u'), {
108 'fmt' => '43' #webm is the only format compatible to all modern browsers. maybe check for available qualities
111 # TODO handle error if format not available. Does that ever occur?
112 $isyoutube = 1;
114 else {
115 next; # do not embed youtube videos
119 elsif ( $HTML5Media_field->subfield('a') && $HTML5Media_field->subfield('d') && $HTML5Media_field->subfield('f') ) {
120 $HTML5Media{host} = $HTML5Media_field->subfield('a');
121 $HTML5Media{host} =~ s/(^\/|\/$)//g;
122 $HTML5Media{path} = $HTML5Media_field->subfield('d');
123 $HTML5Media{path} =~ s/(^\/|\/$)//g; # TODO we could check for youtube here too, but nobody uses these fields anyway…
124 $HTML5Media{file} = $HTML5Media_field->subfield('f');
125 $HTML5Media{srcblock} = $HTML5Media{protocol} . '://' . $HTML5Media{loginblock} . $HTML5Media{host} . $HTML5Media{portblock} . '/' . $HTML5Media{path} . '/' . $HTML5Media{file};
127 else {
128 next; # no file to play
130 # extension
131 # check uploaded files
132 if ( $HTML5Media{srcblock} =~ /\Qopac-retrieve-file.pl\E/ ) {
133 my ( undef, $id ) = split /id=/, $HTML5Media{srcblock};
134 next if !$id;
135 my $public = ( ( caller )[1] =~ /opac/ ) ? { public => 1 }: {};
136 my $upl = Koha::Upload->new( $public )->get({ hashvalue => $id });
137 next if !$upl || $upl->{name} !~ /\./;
138 $HTML5Media{extension} = ( $upl->{name} =~ m/([^.]+)$/ )[0];
140 # check remote files
141 else {
142 $HTML5Media{extension} = ($HTML5Media{srcblock} =~ m/([^.]+)$/)[0];
144 if ( ( !grep /\Q$HTML5Media{extension}\E/, @HTML5MediaExtensions ) && ( $isyoutube != 1) ) {
145 next; # not a specified media file
147 # youtube
148 if ($isyoutube == 1) {
149 $HTML5Media{mime} = 'video/webm';
151 # mime
152 if ( $HTML5Media_field->subfield('c') ) {
153 $HTML5Media{codecs} = $HTML5Media_field->subfield('c');
155 ### from subfield q…
156 if ( $HTML5Media_field->subfield('q') ) {
157 $HTML5Media{mime} = $HTML5Media_field->subfield('q');
159 ### …or from file extension and codecs…
160 elsif ( $HTML5Media{codecs} ) {
161 if ( $HTML5Media{codecs} =~ /theora.*vorbis/ ) {
162 $HTML5Media{mime} = 'video/ogg';
164 elsif ( $HTML5Media{codecs} =~ /vp8.*vorbis/ ) {
165 $HTML5Media{mime} = 'video/webm';
167 elsif ( ($HTML5Media{codecs} =~ /^vorbis$/) && ($HTML5Media{extension} eq 'ogg') ) {
168 $HTML5Media{mime} = 'audio/ogg';
170 elsif ( ($HTML5Media{codecs} =~ /^vorbis$/) && ($HTML5Media{extension} eq 'webm') ) {
171 $HTML5Media{mime} = 'audio/webm';
174 ### …or just from file extension
175 else {
176 if ( $HTML5Media{extension} eq 'ogv' ) {
177 $HTML5Media{mime} = 'video/ogg';
178 $HTML5Media{codecs} = 'theora,vorbis';
180 if ( $HTML5Media{extension} eq 'oga' ) {
181 $HTML5Media{mime} = 'audio/ogg';
182 $HTML5Media{codecs} = 'vorbis';
184 elsif ( $HTML5Media{extension} eq 'spx' ) {
185 $HTML5Media{mime} = 'audio/ogg';
186 $HTML5Media{codecs} = 'speex';
188 elsif ( $HTML5Media{extension} eq 'opus' ) {
189 $HTML5Media{mime} = 'audio/ogg';
190 $HTML5Media{codecs} = 'opus';
192 elsif ( $HTML5Media{extension} eq 'vtt' ) {
193 $HTML5Media{mime} = 'text/vtt';
196 # codecs
197 if ( $HTML5Media{codecs} ) {
198 $HTML5Media{codecblock} = '; codecs="' . $HTML5Media{codecs} . '"';
200 else {
201 $HTML5Media{codecblock} = '';
203 # type
204 if ( $HTML5Media{mime} ) {
205 $HTML5Media{typeblock} = ' type=\'' . $HTML5Media{mime} . $HTML5Media{codecblock} . '\'';
207 else {
208 $HTML5Media{typeblock} = '';
210 # element
211 if ( $HTML5Media{mime} =~ /audio/ ) {
212 $HTML5Media{type} = 'audio';
214 elsif ( $HTML5Media{mime} =~ /video/ ) {
215 $HTML5Media{type} = 'video';
217 elsif ( $HTML5Media{mime} =~ /text/ ) {
218 $HTML5Media{type} = 'track';
220 # push
221 if ( $HTML5Media{srcblock} && $HTML5Media{type} ) {
222 push (@HTML5Media_sets, \%HTML5Media);
225 # parent element
226 for my $media ( @HTML5Media_sets ) {
227 if ( ($media->{mime}) && ($media->{mime} =~ /audio/) ) {
228 if ( $HTML5MediaParent ne 'video' ) {
229 $HTML5MediaParent = 'audio';
230 $HTML5MediaWidth = '';
233 elsif ( ($media->{mime}) && ($media->{mime} =~ /video/) ) {
234 $HTML5MediaParent = 'video';
235 $HTML5MediaWidth = ' width="480"';
238 # child element
239 for my $media ( @HTML5Media_sets ) {
240 if ( ($media->{type}) && ( ($media->{type} eq 'video') || ($media->{type} eq 'audio') ) ) {
241 if ( $media->{type} eq $HTML5MediaParent ) {
242 $media->{child} = 'source';
245 else {
246 $media->{child} = $media->{type};
250 return (
251 HTML5MediaEnabled => ( (scalar(@HTML5Media_sets) > 0) && ($HTML5MediaParent) ),
252 HTML5MediaSets => \@HTML5Media_sets,
253 HTML5MediaParent => $HTML5MediaParent,
254 HTML5MediaWidth => $HTML5MediaWidth,