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

Last change on this file since 5866 was 5765, checked in by mdewsnip, 21 years ago

Commented out check for spaces in filenames - why is it there?? Instead the spaces are converted to "%20"s.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.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;
59use parsargv;
60
61
62sub BEGIN {
63 @ISA = ('BasPlug');
64}
65
66my $arguments =
67 [ { 'name' => "assoc_field",
68 'desc' => "{UnknownPlug.assoc_field}",
69 'type' => "string",
70 'deft' => "",
71 'reqd' => "no" } ,
72 { 'name' => "file_type",
73 'desc' => "{UnknownPlug.file_type}",
74 'type' => "string",
75 'deft' => "",
76 'reqd' => "no" } ];
77
78my $options = { 'name' => "UnknownPlug",
79 'desc' => "{UnknownPlug.desc}",
80 'inherits' => "yes",
81 'args' => $arguments };
82
83# sub print_usage {
84# print STDERR "\n usage: plugin UnknownPlug [options]\n\n";
85# print STDERR " options:\n";
86# print STDERR " -assoc_field\t Name of the metadata field that will hold\n";
87# print STDERR "\t\t the associated file's name.\n";
88# print STDERR "\n";
89# print STDERR " -file_type\t Mime type of the file (e.g. image/gif)\n";
90# print STDERR "\n";
91# }
92
93sub new {
94 my ($class) = @_;
95 my $self = new BasPlug ($class, @_);
96
97 # 14-05-02 To allow for proper inheritance of arguments - John Thompson
98 my $option_list = $self->{'option_list'};
99 push( @{$option_list}, $options );
100
101 if (!parsargv::parse(\@_,
102 q^assoc_field/.*/^, \$self->{'assoc_field'},
103 q^file_type/.*/^, \$self->{'file_type'},
104 "allow_extra_options")) {
105 print STDERR "\nIncorrect options passed to UnknownPlug, check your collect.cfg configuration file\n";
106 $self->print_txt_usage(""); # Use default resource bundle
107 die "\n";
108 }
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 # if ($filename =~ m/ /) {
134 # print $outhandle "UnknownPlug: \"$filename\" contains a space. choking.\n";
135 # return undef;
136 # }
137
138 # Escape spaces in the filename
139 $filename =~ s/ /%20/g;
140 $file =~ s/ /%20/g;
141
142 # Add the file as an associated file ...
143 my $section = $doc_obj->get_top_section();
144 my $file_type = $self->{'file_type'} || "unknown/unknown";
145 my $assoc_field = $self->{'assoc_field'} || "unknown_file";
146
147 $doc_obj->associate_file($filename, $file, $mime_type, $section);
148 $doc_obj->add_metadata ($section, $assoc_field, $file);
149
150 return 1;
151}
152
153
154
155# The UnknownPlug read() function. This function does all the right
156# things to make general options work for a given plugin. UnknownPlug
157# overrides read() because there is no need to read the actual text of
158# the file in, because the contents of the file is not text...
159#
160#
161# Return number of files processed, undef if can't process
162#
163# Note that $base_dir might be "" and that $file might include directories
164
165sub read {
166 my $self = shift (@_);
167 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
168
169 my $outhandle = $self->{'outhandle'};
170
171 # Make sure we're processing the correct file
172 my $filename = &util::filename_cat($base_dir, $file);
173 return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
174 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
175 return undef;
176 }
177 print $outhandle "UnknownPlug processing \"$filename\"\n"
178 if $self->{'verbosity'} > 1;
179
180 #if there's a leading directory name, eat it...
181 $file =~ s/^.*[\/\\]//;
182
183 # create a new document
184 my $doc_obj = new doc ($filename, "indexed_doc");
185 $doc_obj->set_OIDtype ($processor->{'OIDtype'});
186
187 # associate the file with the document
188 if (associate_unknown_file($self, $filename, $file, $doc_obj) != 1)
189 {
190 print "UnknownPlug: couldn't process \"$filename\"\n";
191 return 0;
192 }
193
194 #create an empty text string so we don't break downstream plugins
195 my $text = "Dummy text inserted by UnknownPlug...";
196
197 # include any metadata passed in from previous plugins
198 my $section = $doc_obj->get_top_section();
199 $self->extra_metadata ($doc_obj, $section, $metadata);
200
201 # do plugin specific processing of doc_obj
202 return undef unless defined ($self->process (\$text, $pluginfo, $base_dir,
203 $file, $metadata, $doc_obj));
204
205 # do any automatic metadata extraction
206 $self->auto_extract_metadata ($doc_obj);
207
208 # add an OID
209 $doc_obj->set_OID();
210 $doc_obj->add_text($section, $text);
211
212 # process the document
213 $processor->process($doc_obj);
214
215 return 1;
216}
217
218
219# UnknownPlug processing of doc_obj. In practice we don't need to do
220# anything here because the read function takes care of everything.
221
222sub process {
223 my $self = shift (@_);
224 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
225 my $outhandle = $self->{'outhandle'};
226
227 return 1;
228}
229
230
2311;
232
233
234
235
236
237
238
239
240
241
242
Note: See TracBrowser for help on using the repository browser.