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

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

Jeffrey's new parsing modifications, committed approx 6 July, 15.16

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