source: trunk/gsdl/perllib/plugins/UnknownPlug.pm@ 10594

Last change on this file since 10594 was 10594, checked in by kjdon, 19 years ago

mime type added as MimeType metadata

  • Property svn:keywords set to Author Date Id Revision
File size: 8.2 KB
Line 
1###########################################################################
2#
3# UnknownPlug.pm -- Plugin for files you know about but Greenstone doesn't
4#
5# A component of the Greenstone digital library software from the New
6# Zealand Digital Library Project at the University of Waikato, New
7# Zealand.
8#
9# Copyright (C) 2001 Gordon W. Paynter
10# Copyright (C) 2001 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful, but
18# WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20# General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
28# UnknownPlug - a plugin for unknown files
29
30# This is a simple Plugin for importing files in formats that
31# Greenstone doesn't know anything about. A fictional document will
32# be created for every such file, and the file itself will be passed
33# to Greenstone as the "associated file" of the document.
34
35# Here's an example where it is useful: I have a collection of
36# pictures that include a couple of quicktime movie files with names
37# like DCP_0163.MOV. Rather than write a new plugin for quicktime
38# movies, I add this line to the collection configuration file:
39
40# plugin UnknownPlug -process_exp "*.MOV" -assoc_field "movie"
41
42# A document is created for each movie, with the associated movie
43# file's name in the "movie" metadata field. In the collection's
44# format strings, I use the {If} macro to output different text for
45# each type of file, like this:
46
47# {If}{[movie],<HTML for displaying movie>}{If}{[Image],<HTML for displaying image>}
48
49# You can also add extra metadata, such as the Title, Subject, and
50# Duration, with metadata.xml files and RecPlug. (If you want to use
51# UnknownPlug with more than one type of file, you will have to add
52# some sort of distinguishing metadata in this way.)
53
54
55
56package UnknownPlug;
57
58use BasPlug;
59
60use strict;
61no strict 'refs'; # allow filehandles to be variables and viceversa
62
63sub BEGIN {
64 @UnknownPlug::ISA = ('BasPlug');
65}
66
67my $arguments =
68 [ { 'name' => "assoc_field",
69 'desc' => "{UnknownPlug.assoc_field}",
70 'type' => "string",
71 'deft' => "",
72 'reqd' => "no" },
73 { 'name' => "file_format",
74 'desc' => "{UnknownPlug.file_format}",
75 'type' => "string",
76 'deft' => "",
77 'reqd' => "no" },
78 { 'name' => "mime_type",
79 'desc' => "{UnknownPlug.mime_type}",
80 'type' => "string",
81 'deft' => "",
82 'reqd' => "no" },
83 { 'name' => "process_extension",
84 'desc' => "{UnknownPlug.process_extension}",
85 'type' => "string",
86 'deft' => "",
87 'reqd' => "no" } ];
88
89my $options = { 'name' => "UnknownPlug",
90 'desc' => "{UnknownPlug.desc}",
91 'abstract' => "no",
92 'inherits' => "yes",
93 'args' => $arguments };
94
95
96sub new {
97 my ($class) = shift (@_);
98 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
99 push(@$pluginlist, $class);
100
101 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
102 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
103
104 my $self = (defined $hashArgOptLists)? new BasPlug($pluginlist,$inputargs,$hashArgOptLists): new BasPlug($pluginlist,$inputargs);
105
106 # "-process_extension" is a simpler alternative to -process_exp for non-regexp people
107 if (!$self->{'process_exp'} && $self->{'process_extension'}) {
108 $self->{'process_exp'} = "\\." . $self->{'process_extension'} . "\$";
109 }
110
111 return bless $self, $class;
112}
113
114sub get_default_process_exp {
115 return '';
116}
117
118
119# Associate the unknown file with the new document
120
121sub associate_unknown_file {
122 my $self = shift (@_);
123 my $filename = shift (@_); # filename with full path
124 my $file = shift (@_); # filename without path
125 my $doc_obj = shift (@_);
126
127 my $verbosity = $self->{'verbosity'};
128 my $outhandle = $self->{'outhandle'};
129
130 # check the filename is okay
131 return 0 if ($file eq "" || $filename eq "");
132
133 # Add the image metadata
134 my $url = $file;
135 $url =~ s/ /%20/g;
136
137 # Add the file as an associated file ...
138 my $section = $doc_obj->get_top_section();
139 my $file_format = $self->{'file_format'} || "unknown";
140 my $mime_type = $self->{'mime_type'} || "unknown/unknown";
141 my $assoc_field = $self->{'assoc_field'} || "unknown_file";
142
143 $doc_obj->associate_file($filename, $file, $mime_type, $section);
144 $doc_obj->add_metadata ($section, "FileFormat", $file_format);
145 $doc_obj->add_metadata ($section, "MimeType", $mime_type);
146 $doc_obj->add_metadata ($section, $assoc_field, $file);
147
148 $doc_obj->add_metadata ($section, "srclink",
149 "<a href=\"_httpcollection_/index/assoc/[assocfilepath]/[$assoc_field]\">");
150 $doc_obj->add_metadata ($section, "srcicon", "_iconunknown_");
151 $doc_obj->add_metadata ($section, "/srclink", "</a>");
152
153 return 1;
154}
155
156
157
158# The UnknownPlug read() function. This function does all the right
159# things to make general options work for a given plugin. UnknownPlug
160# overrides read() because there is no need to read the actual text of
161# the file in, because the contents of the file is not text...
162#
163#
164# Return number of files processed, undef if can't process
165#
166# Note that $base_dir might be "" and that $file might include directories
167
168sub read {
169 my $self = shift (@_);
170 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
171
172 my $outhandle = $self->{'outhandle'};
173
174 # Make sure we're processing the correct file
175 my $filename = &util::filename_cat($base_dir, $file);
176 return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
177 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
178 return undef;
179 }
180 print STDERR "<Processing n='$file' p='UnknownPlug'>\n" if ($gli);
181 print $outhandle "UnknownPlug processing \"$filename\"\n"
182 if $self->{'verbosity'} > 1;
183
184 #if there's a leading directory name, eat it...
185 $file =~ s/^.*[\/\\]//;
186
187 # create a new document
188 my $doc_obj = new doc ($filename, "indexed_doc");
189 $doc_obj->set_OIDtype ($processor->{'OIDtype'});
190 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
191 $doc_obj->add_metadata($doc_obj->get_top_section(), "Source", &ghtml::dmsafe($file)); # set the filename as Source metadata to be consistent with other plugins
192 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "FileSize", (-s $filename));
193
194 # associate the file with the document
195 if (associate_unknown_file($self, $filename, $file, $doc_obj) != 1)
196 {
197 if ($gli) {
198 print STDERR "<ProcessingError n='$file'>\n";
199 }
200 print $outhandle "UnknownPlug: couldn't process \"$filename\"\n";
201 return -1; # error during processing
202 }
203
204 #create an empty text string so we don't break downstream plugins
205 my $text = &gsprintf::lookup_string("{BasPlug.dummy_text}");
206
207 # include any metadata passed in from previous plugins
208 my $section = $doc_obj->get_top_section();
209 $self->extra_metadata ($doc_obj, $section, $metadata);
210
211 $self->title_fallback($doc_obj,$section,$file);
212
213 # do plugin specific processing of doc_obj
214 unless (defined ($self->process(\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj))) {
215 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
216 return -1;
217 }
218
219 # do any automatic metadata extraction
220 $self->auto_extract_metadata ($doc_obj);
221
222 # add an OID
223 $doc_obj->set_OID();
224 $doc_obj->add_text($section, $text);
225
226 # process the document
227 $processor->process($doc_obj);
228
229 $self->{'num_processed'} ++;
230 return 1;
231}
232
233
234# UnknownPlug processing of doc_obj. In practice we don't need to do
235# anything here because the read function takes care of everything.
236
237sub process {
238 my $self = shift (@_);
239 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
240 my $outhandle = $self->{'outhandle'};
241
242 return 1;
243}
244
245
2461;
247
248
249
250
251
252
253
254
255
256
257
Note: See TracBrowser for help on using the repository browser.