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

Last change on this file since 8121 was 7508, checked in by kjdon, 20 years ago

changed the plugin metadata - instead of having eg HTMLPlug metadata set to 1, now we have Plugin metadata set to HTMLPlug

  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 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_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
83my $options = { 'name' => "UnknownPlug",
84 'desc' => "{UnknownPlug.desc}",
85 'abstract' => "no",
86 'inherits' => "yes",
87 'args' => $arguments };
88
89
90sub new {
91 my ($class) = @_;
92 my $self = new BasPlug ($class, @_);
93 $self->{'plugin_type'} = "UnknownPlug";
94 # 14-05-02 To allow for proper inheritance of arguments - John Thompson
95 my $option_list = $self->{'option_list'};
96 push( @{$option_list}, $options );
97
98 if (!parsargv::parse(\@_,
99 q^assoc_field/.*/^, \$self->{'assoc_field'},
100 q^file_format/.*/^, \$self->{'file_format'},
101 q^mime_type/.*/^, \$self->{'mime_type'},
102 "allow_extra_options")) {
103 print STDERR "\nIncorrect options passed to UnknownPlug, check your collect.cfg configuration file\n";
104 $self->print_txt_usage(""); # Use default resource bundle
105 die "\n";
106 }
107
108
109 return bless $self, $class;
110}
111
112sub get_default_process_exp {
113 return '';
114}
115
116
117# Associate the unknown file with the new document
118
119sub associate_unknown_file {
120 my $self = shift (@_);
121 my $filename = shift (@_); # filename with full path
122 my $file = shift (@_); # filename without path
123 my $doc_obj = shift (@_);
124
125 my $verbosity = $self->{'verbosity'};
126 my $outhandle = $self->{'outhandle'};
127
128 # check the filename is okay
129 return 0 if ($file eq "" || $filename eq "");
130
131 # Add the image metadata
132 my $url = $file;
133 $url =~ s/ /%20/g;
134
135 # Add the file as an associated file ...
136 my $section = $doc_obj->get_top_section();
137 my $file_format = $self->{'file_format'} || "unknown";
138 my $mime_type = $self->{'mime_type'} || "unknown/unknown";
139 my $assoc_field = $self->{'assoc_field'} || "unknown_file";
140
141 $doc_obj->associate_file($filename, $file, $mime_type, $section);
142 $doc_obj->add_metadata ($section, "FileFormat", $file_format);
143 $doc_obj->add_metadata ($section, $assoc_field, $file);
144 $doc_obj->add_metadata ($section, "Source", $file);
145
146 $self->title_fallback($doc_obj,$section,$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, $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
193 # associate the file with the document
194 if (associate_unknown_file($self, $filename, $file, $doc_obj) != 1)
195 {
196 print "UnknownPlug: couldn't process \"$filename\"\n";
197 return -1; # error during processing
198 }
199
200 #create an empty text string so we don't break downstream plugins
201 my $text = &gsprintf::lookup_string("{BasPlug.dummy_text}");
202
203 # include any metadata passed in from previous plugins
204 my $section = $doc_obj->get_top_section();
205 $self->extra_metadata ($doc_obj, $section, $metadata);
206
207 # do plugin specific processing of doc_obj
208 return -1 unless defined ($self->process (\$text, $pluginfo, $base_dir,
209 $file, $metadata, $doc_obj));
210
211 # do any automatic metadata extraction
212 $self->auto_extract_metadata ($doc_obj);
213
214 # add an OID
215 $doc_obj->set_OID();
216 $doc_obj->add_text($section, $text);
217
218 # process the document
219 $processor->process($doc_obj);
220
221 $self->{'num_processed'} ++;
222 return 1;
223}
224
225
226# UnknownPlug processing of doc_obj. In practice we don't need to do
227# anything here because the read function takes care of everything.
228
229sub process {
230 my $self = shift (@_);
231 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
232 my $outhandle = $self->{'outhandle'};
233
234 return 1;
235}
236
237
2381;
239
240
241
242
243
244
245
246
247
248
249
Note: See TracBrowser for help on using the repository browser.