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

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

fixed up maxdocs - now pass an extra parameter to the read function

  • Property svn:keywords set to Author Date Id Revision
File size: 8.4 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 @UnknownPlug::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_format",
73 'desc' => "{UnknownPlug.file_format}",
74 'type' => "string",
75 'deft' => "",
76 'reqd' => "no" },
77 { 'name' => "mime_type",
78 'desc' => "{UnknownPlug.mime_type}",
79 'type' => "string",
80 'deft' => "",
81 'reqd' => "no" },
82 { 'name' => "process_extension",
83 'desc' => "{UnknownPlug.process_extension}",
84 'type' => "string",
85 'deft' => "",
86 'reqd' => "no" } ];
87
88my $options = { 'name' => "UnknownPlug",
89 'desc' => "{UnknownPlug.desc}",
90 'abstract' => "no",
91 'inherits' => "yes",
92 'args' => $arguments };
93
94
95sub new {
96 my ($class) = @_;
97 my $self = new BasPlug ($class, @_);
98 $self->{'plugin_type'} = "UnknownPlug";
99 # 14-05-02 To allow for proper inheritance of arguments - John Thompson
100 my $option_list = $self->{'option_list'};
101 push( @{$option_list}, $options );
102
103 if (!parsargv::parse(\@_,
104 q^assoc_field/.*/^, \$self->{'assoc_field'},
105 q^file_format/.*/^, \$self->{'file_format'},
106 q^mime_type/.*/^, \$self->{'mime_type'},
107 q^process_extension/.*/^, \$self->{'process_extension'},
108 "allow_extra_options")) {
109 print STDERR "\nIncorrect options passed to UnknownPlug, check your collect.cfg configuration file\n";
110 $self->print_txt_usage(""); # Use default resource bundle
111 die "\n";
112 }
113
114 # "-process_extension" is a simpler alternative to -process_exp for non-regexp people
115 if (!$self->{'process_exp'} && $self->{'process_extension'}) {
116 $self->{'process_exp'} = "\\." . $self->{'process_extension'} . "\$";
117 }
118
119 return bless $self, $class;
120}
121
122sub get_default_process_exp {
123 return '';
124}
125
126
127# Associate the unknown file with the new document
128
129sub associate_unknown_file {
130 my $self = shift (@_);
131 my $filename = shift (@_); # filename with full path
132 my $file = shift (@_); # filename without path
133 my $doc_obj = shift (@_);
134
135 my $verbosity = $self->{'verbosity'};
136 my $outhandle = $self->{'outhandle'};
137
138 # check the filename is okay
139 return 0 if ($file eq "" || $filename eq "");
140
141 # Add the image metadata
142 my $url = $file;
143 $url =~ s/ /%20/g;
144
145 # Add the file as an associated file ...
146 my $section = $doc_obj->get_top_section();
147 my $file_format = $self->{'file_format'} || "unknown";
148 my $mime_type = $self->{'mime_type'} || "unknown/unknown";
149 my $assoc_field = $self->{'assoc_field'} || "unknown_file";
150
151 $doc_obj->associate_file($filename, $file, $mime_type, $section);
152 $doc_obj->add_metadata ($section, "FileFormat", $file_format);
153 $doc_obj->add_metadata ($section, $assoc_field, $file);
154
155 $doc_obj->add_metadata ($section, "srclink",
156 "<a href=\"_httpcollection_/index/assoc/[assocfilepath]/[$assoc_field]\">");
157 $doc_obj->add_metadata ($section, "srcicon", "_iconunknown_");
158 $doc_obj->add_metadata ($section, "/srclink", "</a>");
159
160 return 1;
161}
162
163
164
165# The UnknownPlug read() function. This function does all the right
166# things to make general options work for a given plugin. UnknownPlug
167# overrides read() because there is no need to read the actual text of
168# the file in, because the contents of the file is not text...
169#
170#
171# Return number of files processed, undef if can't process
172#
173# Note that $base_dir might be "" and that $file might include directories
174
175sub read {
176 my $self = shift (@_);
177 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
178
179 my $outhandle = $self->{'outhandle'};
180
181 # Make sure we're processing the correct file
182 my $filename = &util::filename_cat($base_dir, $file);
183 return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
184 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
185 return undef;
186 }
187 print STDERR "<Processing n='$file' p='UnknownPlug'>\n" if ($gli);
188 print $outhandle "UnknownPlug processing \"$filename\"\n"
189 if $self->{'verbosity'} > 1;
190
191 #if there's a leading directory name, eat it...
192 $file =~ s/^.*[\/\\]//;
193
194 # create a new document
195 my $doc_obj = new doc ($filename, "indexed_doc");
196 $doc_obj->set_OIDtype ($processor->{'OIDtype'});
197 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
198 $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
199 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "FileSize", (-s $filename));
200
201 # associate the file with the document
202 if (associate_unknown_file($self, $filename, $file, $doc_obj) != 1)
203 {
204 if ($gli) {
205 print STDERR "<ProcessingError n='$file'>\n";
206 }
207 print $outhandle "UnknownPlug: couldn't process \"$filename\"\n";
208 return -1; # error during processing
209 }
210
211 #create an empty text string so we don't break downstream plugins
212 my $text = &gsprintf::lookup_string("{BasPlug.dummy_text}");
213
214 # include any metadata passed in from previous plugins
215 my $section = $doc_obj->get_top_section();
216 $self->extra_metadata ($doc_obj, $section, $metadata);
217
218 $self->title_fallback($doc_obj,$section,$file);
219
220 # do plugin specific processing of doc_obj
221 unless (defined ($self->process(\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj))) {
222 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
223 return -1;
224 }
225
226 # do any automatic metadata extraction
227 $self->auto_extract_metadata ($doc_obj);
228
229 # add an OID
230 $doc_obj->set_OID();
231 $doc_obj->add_text($section, $text);
232
233 # process the document
234 $processor->process($doc_obj);
235
236 $self->{'num_processed'} ++;
237 return 1;
238}
239
240
241# UnknownPlug processing of doc_obj. In practice we don't need to do
242# anything here because the read function takes care of everything.
243
244sub process {
245 my $self = shift (@_);
246 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
247 my $outhandle = $self->{'outhandle'};
248
249 return 1;
250}
251
252
2531;
254
255
256
257
258
259
260
261
262
263
264
Note: See TracBrowser for help on using the repository browser.