source: main/tags/2.71/gsdl/perllib/mgppbuildproc.pm@ 25382

Last change on this file since 25382 was 12951, checked in by kjdon, 18 years ago

changed \! to == 0 cos someone complained that it didn't work

  • Property svn:keywords set to Author Date Id Revision
File size: 11.5 KB
Line 
1###########################################################################
2#
3# mgppbuildproc.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26# This document processor outputs a document
27# for mgpp to process
28
29
30package mgppbuildproc;
31
32use basebuildproc;
33use strict;
34no strict 'refs'; # allow filehandles to be variables and viceversa
35
36
37BEGIN {
38 @mgppbuildproc::ISA = ('basebuildproc');
39}
40
41#this must be the same as in mgppbuilder
42our %level_map = ('document'=>'Doc',
43 'section'=>'Sec',
44 'paragraph'=>'Para');
45
46sub new {
47 my $class = shift @_;
48 my $self = new basebuildproc (@_);
49
50 # use a different index specification to the default
51 $self->{'index'} = "text";
52
53 $self->{'dontindex'} = {};
54 $self->{'indexfieldmap'} = {};
55 $self->{'indexfields'} = {}; # only put in the ones that are not specified directly in the index
56 $self->{'strip_html'}=1;
57
58 return bless $self, $class;
59}
60
61
62sub set_indexfieldmap {
63 my $self = shift (@_);
64 my ($indexmap) = @_;
65
66 $self->{'indexfieldmap'} = $indexmap;
67}
68
69sub get_indexfieldmap {
70 my $self = shift (@_);
71
72 return $self->{'indexfieldmap'};
73}
74
75sub set_levels {
76 my $self = shift (@_);
77 my ($levels) = @_;
78
79 $self->{'levels'} = $levels;
80}
81
82sub set_strip_html {
83 my $self = shift (@_);
84 my ($strip) = @_;
85 $self->{'strip_html'}=$strip;
86}
87
88
89sub get_gdbm_level {
90 my $self = shift (@_);
91
92 #if a Section level index is not built, the gdbm file should be at doc
93 #level not Section
94 if ($self->{'levels'}->{'section'}) {
95 return "section";
96 }
97 return "document";
98}
99
100
101#sub find_paragraphs {
102# $_[1] =~ s/(<p\b)/<Paragraph>$1/gi;
103#}
104
105sub remove_gtlt {
106 my $self =shift(@_);
107 my ($text, $para) = @_;
108 $text =~s/[<>]//g;
109 return "$para$text$para";
110}
111
112sub process_tags {
113 my $self = shift(@_);
114 my ($text, $para) = @_;
115 if ($text =~ /^p\b/i) {
116 return $para;
117 }
118 return "";
119}
120
121sub preprocess_text {
122 my $self = shift (@_);
123 my ($text, $strip_html, $para) = @_;
124 # at this stage, we do not do paragraph tags unless have strip_html -
125 # it will result in a huge mess of non-xml
126 return unless $strip_html;
127
128 my $new_text = $text;
129
130 # if we have <pre> tags, we can have < > inside them, need to delete
131 # the <> before stripping tags
132 $new_text =~ s/<pre>(.*?)<\/pre>/$self->remove_gtlt($1,$para)/gse;
133
134 if ($para eq "") {
135 # just remove all tags
136 $new_text =~ s/<[^>]*>//gs;
137 } else {
138 # strip all tags except <p> tags which get turned into $para
139 $new_text =~ s/<([^>]*)>/$self->process_tags($1, $para)/gse;
140
141 }
142 return $new_text;
143}
144#this function strips the html tags from the doc if ($strip_html) and
145# if ($para) replaces <p> with <Paragraph> tags.
146# if both are false, the original text is returned
147#assumes that <pre> and </pre> have no spaces, and removes all < and > inside
148#these tags
149sub preprocess_text_old_and_slow {
150 my $self = shift (@_);
151 my ($text, $strip_html, $para) = @_;
152 my ($outtext) = "";
153 if ($strip_html) {
154 while ($text =~ /<([^>]*)>/ && $text ne "") {
155
156 my $tag = $1;
157 $outtext .= $`." "; #add everything before the matched tag
158 $text = $'; #'everything after the matched tag
159 if ($para && $tag =~ /^\s*p\s/i) {
160 $outtext .= $para;
161 }
162 elsif ($tag =~ /^pre$/) { # a pre tag
163 $text =~ /<\/pre>/; # find the closing pre tag
164 my $tmp_text = $`; #everything before the closing pre tag
165 $text = $'; #'everything after the </pre>
166 $tmp_text =~ s/[<>]//g; # remove all < and >
167 $outtext.= $tmp_text . " ";
168 }
169 }
170
171 $outtext .= $text; # add any remaining text
172 return $outtext;
173 } #if strip_html
174
175 #if ($para) {
176 #$text =~ s/(<p\b)/$para$1/gi;
177 #return $text;
178 # }
179 return $text;
180}
181
182
183
184sub filter_text {
185 # $self->filter_text ($field, $new_text);
186 # don't want to do anything for this version, however,
187 # in a particular collection you might want to override
188 # this method to post-process certain fields depending on
189 # the field, or whether we are outputting it for indexing
190}
191
192sub text {
193 my $self = shift (@_);
194 my ($doc_obj) = @_;
195 my $handle = $self->{'output_handle'};
196 my $outhandle = $self->{'outhandle'};
197
198 # only output this document if it is one to be indexed
199 return if ($doc_obj->get_doc_type() ne "indexed_doc");
200
201 my $indexed_doc = $self->is_subcollection_doc($doc_obj);
202
203 # this is another document
204 $self->{'num_docs'} += 1;
205
206 # get the parameters for the output
207 # split on : just in case there is subcoll and lang stuff
208 my ($fields) = split (/:/, $self->{'index'});
209
210 my ($documenttag) = "";
211 my($documentendtag) = "";
212 if ($self->{'levels'}->{'document'}) {
213 $documenttag = "\n<". $level_map{'document'} . ">\n";
214 $documentendtag = "\n</". $level_map{'document'} . ">\n";
215 }
216 my ($sectiontag) = "";
217 if ($self->{'levels'}->{'section'}) {
218 $sectiontag = "\n<". $level_map{'section'} . ">\n";
219 }
220 my ($paratag) = "";
221
222 # paragraph tags will only be used for indexing (can't retrieve
223 # paragraphs), and can ony be used if we are stripping HTML tags
224 if ($self->{'indexing_text'} && $self->{'levels'}->{'paragraph'}) {
225 if ($self->{'strip_html'}) {
226 $paratag = "<". $level_map{'paragraph'} . ">";
227 } else {
228 print $outhandle "Paragraph level can not be used with no_strip_html!. Not indexing Paragraphs.\n";
229 }
230 }
231
232 my $doc_section = 0; # just for this document
233
234 my $text = $documenttag;
235
236 # get the text for this document
237 my $section = $doc_obj->get_top_section();
238
239 while (defined $section) {
240 # update a few statistics
241 $doc_section++;
242 $self->{'num_sections'} += 1;
243 $text .= "$sectiontag";
244
245 my $indexed_section = $doc_obj->get_metadata_element($section, "gsdldoctype") || "indexed_section";
246 if (($indexed_doc == 0) || ($indexed_section ne "indexed_section" && $indexed_section ne "indexed_doc")) {
247 # we are not actually indexing anything for this document,
248 # but we want to keep the section numbers the same, so we just
249 # output section tags for each section (which is done above)
250 $section = $doc_obj->get_next_section($section);
251 next;
252 }
253
254 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
255 foreach my $field (split (/;/, $fields)) {
256 # only deal with this field if it doesn't start with top or
257 # this is the first section
258 my $real_field = $field;
259 next if (($real_field =~ s/^top//) && ($doc_section != 1));
260
261 my $new_text = "";
262
263 # we get allfields by default - do nothing
264 if ($real_field eq "allfields") {
265
266 }
267
268 # metadata - output all metadata we know about except gsdl stuff
269 elsif ($real_field eq "metadata") {
270 my $shortname = "";
271 my $metadata = $doc_obj->get_all_metadata ($section);
272 foreach my $pair (@$metadata) {
273 my ($mfield, $mvalue) = (@$pair);
274 # check fields here, maybe others dont want - change to use dontindex!!
275 if ($mfield ne "Identifier"
276 && $mfield !~ /^gsdl/
277 && $mfield ne "classifytype"
278 && $mfield ne "assocfilepath"
279 && defined $mvalue && $mvalue ne "") {
280
281 if (defined $self->{'indexfieldmap'}->{$mfield}) {
282 $shortname = $self->{'indexfieldmap'}->{$mfield};
283 }
284 else {
285 $shortname = $self->create_shortname($mfield);
286 $self->{'indexfieldmap'}->{$mfield} = $shortname;
287 $self->{'indexfieldmap'}->{$shortname} = 1;
288 }
289 $new_text .= "$paratag<$shortname>$mvalue</$shortname>\n";
290 if (!defined $self->{'indexfields'}->{$mfield}) {
291 $self->{'indexfields'}->{$mfield} = 1;
292 }
293 }
294 }
295 }
296 else {
297 #individual metadata and or text specified - could be
298 # a comma separated list
299 my $shortname="";
300 if (defined $self->{'indexfieldmap'}->{$real_field}) {
301 $shortname = $self->{'indexfieldmap'}->{$real_field};
302 }
303 else {
304 $shortname = $self->create_shortname($real_field);
305 $self->{'indexfieldmap'}->{$real_field} = $shortname;
306 $self->{'indexfieldmap'}->{$shortname} = 1;
307 }
308 my @metadata_list = ();
309 foreach my $submeta (split /,/, $real_field) {
310 if ($submeta eq "text") {
311 my $section_text = $doc_obj->get_text($section);
312 if ($self->{'indexing_text'}) {
313 # tag the text with <Text>...</Text>, add the <Paragraph> tags and strip out html if needed
314 $new_text .= "$paratag<$shortname>\n";
315 if ($paratag ne "") {
316 $section_text = $self->preprocess_text($section_text, $self->{'strip_html'}, "</$shortname>$paratag<$shortname>");
317 }
318 else {
319 $section_text = $self->preprocess_text($section_text, $self->{'strip_html'}, "");
320 }
321 $new_text .= "$section_text</$shortname>\n";
322 }
323 else {
324 # leave html stuff in, and don't add Paragraph tags - never retrieve paras at the moment
325 $new_text .= $section_text;
326 }
327 }
328 else {
329 my @section_metadata = @{$doc_obj->get_metadata ($section, $submeta)};
330 if ($section ne $doc_obj->get_top_section() && $self->{'indexing_text'} && defined ($self->{'sections_index_document_metadata'})) {
331 if ($self->{'sections_index_document_metadata'} eq "always" || ( scalar(@section_metadata) == 0 && $self->{'sections_index_document_metadata'} eq "unless_section_metadata_exists")) {
332 push (@section_metadata, @{$doc_obj->get_metadata ($doc_obj->get_top_section(), $submeta)});
333 }
334 }
335 push (@metadata_list, @section_metadata);
336 }
337 }
338 foreach my $item (@metadata_list) {
339 $new_text .= "$paratag<$shortname>$item</$shortname>\n";
340 }
341 }
342
343 # filter the text
344 $self->filter_text ($field, $new_text);
345
346 $self->{'num_processed_bytes'} += length ($new_text);
347 $text .= "$new_text";
348 } # foreach field
349
350 $section = $doc_obj->get_next_section($section);
351 } # while defined section
352 print $handle "$text\n$documentendtag";
353
354}
355
356#chooses the first two letters or digits for the shortname
357#now ignores non-letdig characters
358sub create_shortname {
359 my $self = shift(@_);
360
361 my ($realname) = @_;
362 #take the first two chars
363 my $shortname;
364 if ($realname =~ /^[^\w]*(\w)[^\w]*(\w)/) {
365 $shortname = "$1$2";
366 } else {
367 # there aren't two letdig's in the field - try arbitrary combinations
368 $realname = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
369 $shortname = "AB";
370 }
371 $shortname =~ tr/a-z/A-Z/;
372
373 #if already used, take the first and third letdigs and so on
374 my $count = 1;
375 while (defined $self->{'indexfieldmap'}->{$shortname}) {
376 if ($realname =~ /^[^\w]*(\w)([^\w]*\w){$count}[^\w]*(\w)/) {
377 $shortname = "$1$3";
378 $count++;
379 $shortname =~ tr/a-z/A-Z/;
380
381 }
382 else {
383 #remove up to and incl the first letdig
384 $realname =~ s/^[^\w]*\w//;
385 $count = 0;
386 }
387 }
388
389 return $shortname;
390}
391
3921;
393
Note: See TracBrowser for help on using the repository browser.