Bug 17193: Remove C4::Search::SearchAcquisitions
[koha.git] / C4 / HTML5Media.pm
1 package C4::HTML5Media;
2
3 # Copyright 2012/2015 Mirko Tietgen
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use strict;
21 use warnings;
22
23 use C4::Context;
24 use MARC::Field;
25 use Koha::Upload;
26
27 =head1 HTML5Media
28
29 C4::HTML5Media
30
31 =head1 Description
32
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.
34
35 =cut
36
37 =head2 gethtml5media
38
39 Get all relevant data from field 856. Takes a $record in the subroutine call, sets appropriate params.
40
41 =cut
42
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';
59         }
60         elsif ( $HTML5Media_field->indicator(1) eq '4' ) {
61             $HTML5Media{protocol} = 'http';
62         }
63         elsif ( $HTML5Media_field->indicator(1) eq '7' ) {
64             if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC') {
65                 $HTML5Media{protocol} = $HTML5Media_field->subfield('2');
66             }
67             elsif ($marcflavour eq 'UNIMARC') {
68                 $HTML5Media{protocol} = $HTML5Media_field->subfield('y');
69             }
70         }
71         else {
72             $HTML5Media{protocol} = 'http';
73         }
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.
77         }
78         elsif ( $HTML5Media_field->subfield('h') ) {
79             $HTML5Media{username} = $HTML5Media_field->subfield('h');
80         }
81         # user/pass
82         if ( $HTML5Media{username} && $HTML5Media_field->subfield('k') ) {
83             $HTML5Media{loginblock} = $HTML5Media{username} . ':' . $HTML5Media_field->subfield('k') . '@';
84         }
85         elsif ( $HTML5Media{username} ) {
86             $HTML5Media{loginblock} = $HTML5Media{username} . '@';
87         }
88         else {
89             $HTML5Media{loginblock} = '';
90         }
91         # port
92         if ( $HTML5Media_field->subfield('p') ) {
93             $HTML5Media{portblock} = ':' . $HTML5Media_field->subfield('k');
94         }
95         else {
96             $HTML5Media{portblock} = '';
97         }
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
110                             }
111                         );
112                     };
113                     if ($@) { warn $@; }
114                     else  { $isyoutube = 1;}
115                 }
116                else {
117                    next; # do not embed youtube videos
118                }
119             }
120         }
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};
128         }
129         else {
130             next; # no file to play
131         }
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 $upl = Koha::Upload->new( $public )->get({ hashvalue => $id });
139             next if !$upl || $upl->{name} !~ /\./;
140             $HTML5Media{extension} = ( $upl->{name} =~ m/([^.]+)$/ )[0];
141         }
142         # check remote files
143         else {
144             $HTML5Media{extension} = ($HTML5Media{srcblock} =~ m/([^.]+)$/)[0];
145         }
146         if ( ( !grep /\Q$HTML5Media{extension}\E/, @HTML5MediaExtensions ) && ( $isyoutube != 1) ) {
147             next; # not a specified media file
148         }
149         # youtube
150         if ($isyoutube == 1) {
151                 $HTML5Media{mime} = 'video/webm';
152         }
153         # mime
154         if ( $HTML5Media_field->subfield('c') ) {
155             $HTML5Media{codecs} = $HTML5Media_field->subfield('c');
156         }
157         ### from subfield q…
158         if ( $HTML5Media_field->subfield('q') ) {
159             $HTML5Media{mime} = $HTML5Media_field->subfield('q');
160         }
161         ### …or from file extension and codecs…
162         elsif ( $HTML5Media{codecs} ) {
163             if ( $HTML5Media{codecs} =~ /theora.*vorbis/ ) {
164                 $HTML5Media{mime} = 'video/ogg';
165             }
166             elsif ( $HTML5Media{codecs} =~ /vp8.*vorbis/ ) {
167                 $HTML5Media{mime} = 'video/webm';
168             }
169             elsif ( ($HTML5Media{codecs} =~ /^vorbis$/) && ($HTML5Media{extension} eq 'ogg') ) {
170                 $HTML5Media{mime} = 'audio/ogg';
171             }
172             elsif ( ($HTML5Media{codecs} =~ /^vorbis$/) && ($HTML5Media{extension} eq 'webm') ) {
173                 $HTML5Media{mime} = 'audio/webm';
174             }
175         }
176         ### …or just from file extension
177         else {
178             if ( $HTML5Media{extension} eq 'ogv' ) {
179                 $HTML5Media{mime} = 'video/ogg';
180                 $HTML5Media{codecs} = 'theora,vorbis';
181             }
182             if ( $HTML5Media{extension} eq 'oga' ) {
183                 $HTML5Media{mime} = 'audio/ogg';
184                 $HTML5Media{codecs} = 'vorbis';
185             }
186             elsif ( $HTML5Media{extension} eq 'spx' ) {
187                 $HTML5Media{mime} = 'audio/ogg';
188                 $HTML5Media{codecs} = 'speex';
189             }
190             elsif ( $HTML5Media{extension} eq 'opus' ) {
191                 $HTML5Media{mime} = 'audio/ogg';
192                 $HTML5Media{codecs} = 'opus';
193             }
194             elsif ( $HTML5Media{extension} eq 'vtt' ) {
195                 $HTML5Media{mime} = 'text/vtt';
196             }
197         }
198         # codecs
199         if ( $HTML5Media{codecs} ) {
200             $HTML5Media{codecblock} = '; codecs="' . $HTML5Media{codecs} . '"';
201         }
202         else {
203             $HTML5Media{codecblock} = '';
204         }
205         # type
206         if ( $HTML5Media{mime} ) {
207             $HTML5Media{typeblock} = ' type=\'' . $HTML5Media{mime} . $HTML5Media{codecblock} . '\'';
208         }
209         else {
210           $HTML5Media{typeblock} = '';
211         }
212         # element
213         if ( $HTML5Media{mime} =~ /audio/ ) {
214             $HTML5Media{type} = 'audio';
215         }
216         elsif ( $HTML5Media{mime} =~ /video/ ) {
217             $HTML5Media{type} = 'video';
218         }
219         elsif ( $HTML5Media{mime} =~ /text/ ) {
220             $HTML5Media{type} = 'track';
221         }
222         # push
223         if ( $HTML5Media{srcblock} && $HTML5Media{type} ) {
224             push (@HTML5Media_sets, \%HTML5Media);
225         }
226     }
227     # parent element
228     for my $media ( @HTML5Media_sets ) {
229         if ( ($media->{mime}) && ($media->{mime} =~ /audio/) ) {
230             if ( $HTML5MediaParent ne 'video' ) {
231                 $HTML5MediaParent = 'audio';
232                 $HTML5MediaWidth = '';
233             }
234         }
235         elsif ( ($media->{mime}) && ($media->{mime} =~ /video/) ) {
236             $HTML5MediaParent = 'video';
237             $HTML5MediaWidth = ' width="480"';
238         }
239     }
240     # child element
241     for my $media ( @HTML5Media_sets ) {
242         if ( ($media->{type}) && ( ($media->{type} eq 'video') || ($media->{type} eq 'audio') ) ) {
243             if ( $media->{type} eq $HTML5MediaParent ) {
244                 $media->{child} = 'source';
245             }
246         }
247         else {
248             $media->{child} = $media->{type};
249         }
250     }
251
252     return (
253         HTML5MediaEnabled  => ( (scalar(@HTML5Media_sets) > 0) && ($HTML5MediaParent) ),
254         HTML5MediaSets     => \@HTML5Media_sets,
255         HTML5MediaParent   => $HTML5MediaParent,
256         HTML5MediaWidth    => $HTML5MediaWidth,
257     );
258 }
259
260 1;