source: main/trunk/greenstone2/perllib/plugins/PagedImagePlugin.pm

Last change on this file was 38749, checked in by davidb, 2 months ago

Code introduced to set SourceDirectory as a piece of metadata for all plugins. Done in read_into_doc_obj(), and so with the inheritance we have across plugins, this needed in be added into 4-5 of our existing plugins. In doing so, it was noticed that not all of them called the post_process_doc_obj() and/or the newer apply_metadata_mapping() subroutine. These were fixed up as part of this coding change, along with improved consistency of declaring the top_section local variable

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 30.9 KB
Line 
1###########################################################################
2#
3# PagedImagePlugin.pm -- plugin for sets of images and OCR text that
4# make up a document
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 1999 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26
27# PagedImagePlugin
28# processes sequences of images, with optional OCR text
29#
30# This plugin takes *.item files, which contain metadata and lists of image
31# files, and produces a document containing sections, one for each page.
32# The files should be named something.item, then you can have more than one
33# book in a directory. You will need to create these files, one for each
34# document/book.
35#
36#There are two formats for the item files: a plain text format, and an xml
37#format. You can use either format, and can have both formats in the same
38#collection if you like. If you use the plain format, you must not start the
39#file off with <PagedDocument>
40
41#### PLAIN FORMAT
42# The format of the xxx.item file is as follows:
43# The first lines contain any metadata for the whole document
44# <metadata-name>metadata-value
45# eg.
46# <Title>Snail farming
47# <Date>19230102
48# Then comes a list of pages, one page per line, each line has the format
49#
50# pagenum:imagefile:textfile:r
51#
52# page num and imagefile are required. pagenum is used for the Title
53# of the section, and in the display is shown as page <pagenum>.
54# imagefile is the image for the page. textfile is an optional text
55# file containing the OCR (or any) text for the page - this gets added
56# as the text for the section. r is optional, and signals that the image
57# should be rotated 180deg. Eg use this if the image has been made upside down.
58# So an example item file looks like:
59# <Title>Snail farming
60# <Date>19960403
61# 1:p1.gif:p1.txt:
62# 2:p2.gif::
63# 3:p3.gif:p3.txt:
64# 3b:p3b.gif:p3b.txt:r
65# The second page has no text, the fourth page is a back page, and
66# should be rotated.
67#
68
69#### XML FORMAT
70# The xml format looks like the following
71#<PagedDocument>
72#<Metadata name="Title">The Title of the entire document</Metadata>
73#<Page pagenum="1" imgfile="xxx.jpg" txtfile="yyy.txt">
74#<Metadata name="Title">The Title of this page</Metadata>
75#</Page>
76#... more pages
77#</PagedDocument>
78#PagedDocument contains a list of Pages, Metadata and PageGroups. Any metadata
79#that is not inside another tag will belong to the document.
80#Each Page has a pagenum (not used at the moment), an imgfile and/or a txtfile.
81#These are both optional - if neither is used, the section will have no content.
82#Pages can also have metadata associated with them.
83#PageGroups can be introduced at any point - they can contain Metadata and Pages and other PageGroups. They are used to introduce hierarchical structure into the document.
84#For example
85#<PagedDocument>
86#<PageGroup>
87#<Page>
88#<Page>
89#</PageGroup>
90#<Page>
91#</PagedDocument>
92#would generate a structure like
93#X
94#--X
95# --X
96# --X
97#--X
98#PageGroup tags can also have imgfile/textfile metadata if you like - this way they get some content themselves.
99
100#Currently the XML structure doesn't work very well with the paged document type, unless you use numerical Titles for each section.
101#There is still a bit of work to do on this format:
102#* enable other text file types, eg html, pdf etc
103#* make the document paging work properly
104#* add pagenum as Title unless a Title is present?
105
106# All the supplemetary image amd text files should be in the same folder as
107# the .item file.
108#
109# To display the images instead of the document text, you can use [srcicon]
110# in the DocumentText format statement.
111# For example,
112#
113# format DocumentText "<center><table width=_pagewidth_><tr><td>[srcicon]</td></tr></table></center>"
114#
115# To have it create thumbnail size images, use the '-create_thumbnail' option.
116# To have it create medium size images for display, use the '-create_screenview'
117# option. As usual, running
118# 'perl -S pluginfo.pl PagedImagePlugin' will list all the options.
119
120# If you want the resulting documents to be presented with a table of
121# contents, use '-documenttype hierarchy', otherwise they will have
122# next and previous arrows, and a goto page X box.
123
124# If you have used -create_screenview, you can also use [screenicon] in the format
125# statement to display the smaller image. Here is an example that switches
126# between the two:
127#
128# format DocumentText "<center><table width=_pagewidth_><tr><td>{If}{_cgiargp_ eq full,<a href='_httpdocument_&d=_cgiargd_&p=small'>Switch to small version.</a>,<a href='_httpdocument_&d=_cgiargd_&p=full'>Switch to fullsize version</a>}</td></tr><tr><td>{If}{_cgiargp_ eq full,<a href='_httpdocument_&d=_cgiargd_&p=small' title='Switch to small version'>[srcicon]</a>,<a href='_httpdocument_&d=_cgiargd_&p=full' title='Switch to fullsize version'>[screenicon]</a>}</td></tr></table></center>"
129#
130# Additional metadata can be added into the .item files, alternatively you can
131# use normal metadata.xml files, with the name of the xxx.item file as the
132# FileName (only for document level metadata).
133
134package PagedImagePlugin;
135
136use Encode;
137use ReadXMLFile;
138use ReadTextFile;
139use ImageConverter;
140use MetadataRead;
141
142use strict;
143no strict 'refs'; # allow filehandles to be variables and viceversa
144
145sub BEGIN {
146 @PagedImagePlugin::ISA = ('MetadataRead', 'ReadXMLFile', 'ReadTextFile', 'ImageConverter');
147}
148
149my $gs2_type_list =
150 [ { 'name' => "auto",
151 'desc' => "{PagedImagePlugin.documenttype.auto2}" },
152 { 'name' => "paged",
153 'desc' => "{PagedImagePlugin.documenttype.paged2}" },
154 { 'name' => "hierarchy",
155 'desc' => "{PagedImagePlugin.documenttype.hierarchy}" }
156 ];
157
158my $gs3_type_list =
159 [ { 'name' => "auto",
160 'desc' => "{PagedImagePlugin.documenttype.auto3}" },
161 { 'name' => "paged",
162 'desc' => "{PagedImagePlugin.documenttype.paged3}" },
163 { 'name' => "hierarchy",
164 'desc' => "{PagedImagePlugin.documenttype.hierarchy}" },
165 { 'name' => "pagedhierarchy",
166 'desc' => "{PagedImagePlugin.documenttype.pagedhierarchy}" }
167 ];
168
169my $arguments =
170 [ { 'name' => "process_exp",
171 'desc' => "{BaseImporter.process_exp}",
172 'type' => "string",
173 'deft' => &get_default_process_exp(),
174 'reqd' => "no" },
175 { 'name' => "title_sub",
176 'desc' => "{HTMLPlugin.title_sub}",
177 'type' => "string",
178 'deft' => "" },
179 { 'name' => "headerpage",
180 'desc' => "{PagedImagePlugin.headerpage}",
181 'type' => "flag",
182 'reqd' => "no" },
183# { 'name' => "documenttype",
184# 'desc' => "{PagedImagePlugin.documenttype}",
185# 'type' => "enum",
186# 'list' => $type_list,
187# 'deft' => "auto",
188# 'reqd' => "no" },
189 {'name' => "processing_tmp_files",
190 'desc' => "{BaseImporter.processing_tmp_files}",
191 'type' => "flag",
192 'hiddengli' => "yes"}
193 ];
194
195my $doc_type_opt = { 'name' => "documenttype",
196 'desc' => "{PagedImagePlugin.documenttype}",
197 'type' => "enum",
198 'deft' => "auto",
199 'reqd' => "no" };
200
201my $options = { 'name' => "PagedImagePlugin",
202 'desc' => "{PagedImagePlugin.desc}",
203 'abstract' => "no",
204 'inherits' => "yes",
205 'args' => $arguments };
206
207sub new {
208 my ($class) = shift (@_);
209 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
210 push(@$pluginlist, $class);
211
212 push(@{$hashArgOptLists->{"OptList"}},$options);
213
214 my $imc_self = new ImageConverter($pluginlist, $inputargs, $hashArgOptLists);
215 # we can use this plugin to check gs3 version
216 if ($imc_self->{'gs_version'} eq "3") {
217 $doc_type_opt->{'list'} = $gs3_type_list;
218 }
219 else {
220 $doc_type_opt->{'list'} = $gs2_type_list;
221 }
222 push(@$arguments,$doc_type_opt);
223 # now we add the args to the list for parsing
224 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
225
226 my $rtf_self = new ReadTextFile($pluginlist, $inputargs, $hashArgOptLists, 1);
227 my $rxf_self = new ReadXMLFile($pluginlist, $inputargs, $hashArgOptLists);
228
229 my $self = BaseImporter::merge_inheritance($rxf_self, $imc_self,$rtf_self);
230
231 # Update $self used by XML::Parser so it finds callback functions
232 # such as start_document here and not in ReadXMLFile (which is what
233 # $self was when new XML::Parser was done)
234 #
235 # If the $self returned by this constructor is the same as the one
236 # used in ReadXMLFile (e.g. in the GreenstoneXMLPlugin) then this step isn't necessary
237 #
238 # Consider embedding this type of assignment into merge_inheritance
239 # to help catch all cases?
240
241 $rxf_self->{'parser'}->{'PluginObj'} = $self;
242
243 return bless $self, $class;
244}
245
246
247sub init {
248 my $self = shift (@_);
249 my ($verbosity, $outhandle, $failhandle) = @_;
250
251 $self->SUPER::init(@_);
252 $self->ImageConverter::init();
253}
254
255sub begin {
256 my $self = shift (@_);
257 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
258
259 $self->SUPER::begin(@_);
260 $self->ImageConverter::begin(@_);
261}
262
263sub get_default_process_exp {
264 my $self = shift (@_);
265
266 return q^\.item$^;
267}
268
269sub get_doctype {
270 my $self = shift(@_);
271
272 return "PagedDocument";
273}
274
275
276# want to use BaseImporter's version of this, not ReadXMLFile's
277sub can_process_this_file {
278 my $self = shift(@_);
279 return $self->BaseImporter::can_process_this_file(@_);
280}
281
282# instead of a block exp, now we scan the file and record all text and img files mentioned there for blocking.
283sub store_block_files
284{
285 my $self = shift (@_);
286 my ($filename_full_path, $block_hash) = @_;
287
288 my $xml_version = $self->is_xml_item_file($filename_full_path);
289
290 # do we need to do this?
291 # does BOM interfere just with XML parsing? In that case don't need it here
292 # if we do it here, we are modifying the file before we have worked out if
293 # its new or not, so it will always be reimported.
294 #$self->tidy_item_file($filename_full_path);
295
296 my ($dir, $file) = $filename_full_path =~ /^(.*?)([^\/\\]*)$/;
297 if ($xml_version) {
298
299 # do something
300 $self->scan_xml_for_files_to_block($filename_full_path, $dir, $block_hash);
301 } else {
302
303 $self->scan_item_for_files_to_block($filename_full_path, $dir, $block_hash);
304 }
305
306}
307
308# we want to use BaseImporter's read, not ReadXMLFile's
309sub read
310{
311 my $self = shift (@_);
312
313 $self->BaseImporter::read(@_);
314}
315
316
317
318sub read_into_doc_obj {
319 my $self = shift (@_);
320 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
321 my $outhandle = $self->{'outhandle'};
322 my $verbosity = $self->{'verbosity'};
323
324 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
325
326 $self->{'orig_filename_full_path'} = $filename_full_path;
327 #$self->{'orig_filename_no_path'} = $filename_no_path;
328
329 my $toplevel_plugin_classname = ref($self);
330 print $outhandle "$toplevel_plugin_classname processing \"$filename_full_path\"\n"
331 if $verbosity > 1;
332 print STDERR "<Processing n='$file' p='$toplevel_plugin_classname'>\n" if ($gli);
333
334 $self->{'MaxImageWidth'} = 0;
335 $self->{'MaxImageHeight'} = 0;
336
337 # here we need to decide if we have an old text .item file, or a new xml
338 # .item file
339 my $xml_version = $self->is_xml_item_file($filename_full_path);
340
341 $self->tidy_item_file($filename_full_path);
342
343 my $doc_obj;
344 if ($xml_version) {
345 # careful checking needed here!! are we using local xml handlers or super ones
346 $self->ReadXMLFile::read($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli);
347 $doc_obj = $self->{'doc_obj'};
348 } else {
349 my ($dir, $item_file);
350 ($dir, $item_file) = $filename_full_path =~ /^(.*?)([^\/\\]*)$/;
351
352 #process the .item file
353 $doc_obj = $self->process_item($filename_full_path, $dir, $item_file, $processor, $metadata);
354
355 }
356
357 my $top_section = $doc_obj->get_top_section();
358
359 $doc_obj->add_utf8_metadata($top_section, "Plugin", "$self->{'plugin_type'}");
360 $doc_obj->add_metadata($top_section, "FileFormat", "PagedImage");
361
362 my $dirname = &File::Basename::dirname($file);
363 $doc_obj->add_utf8_metadata($top_section, "SourceDirectory", $dirname);
364
365 # include any metadata passed in from previous plugins
366 # note that this metadata is associated with the top level section
367 $self->add_associated_files($doc_obj, $filename_full_path);
368 $self->extra_metadata ($doc_obj, $top_section, $metadata);
369 $self->auto_extract_metadata ($doc_obj);
370 $self->plugin_specific_process($base_dir, $file, $doc_obj, $gli);
371 # if we haven't found any Title so far, assign one
372 $self->title_fallback($doc_obj,$top_section,$filename_no_path);
373
374 if ($self->{'metadata_mapping_rules'}) {
375 $self->apply_metadata_mapping_file($doc_obj);
376 }
377
378 # force a new OID - this will use OIDtype option set for this plugin.
379 $self->add_OID($doc_obj);
380
381 $self->post_process_doc_obj($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli);
382 return (1,$doc_obj);
383}
384# override this for an inheriting plugin to add extra metadata etc
385sub plugin_specific_process {
386 my $self = shift(@_);
387 my ($base_dir, $file, $doc_obj, $gli) = @_;
388
389}
390
391# for now, the test is if the first non-empty line is <PagedDocument>, then its xml
392sub is_xml_item_file {
393 my $self = shift(@_);
394 my ($filename) = @_;
395
396 my $xml_version = 0;
397 open (ITEMFILE, $filename) || die "couldn't open $filename\n";
398
399 my $line = "";
400 my $num = 0;
401
402 $line = <ITEMFILE>;
403 while (defined ($line) && ($line !~ /\w/)) {
404 $line = <ITEMFILE>;
405 }
406
407 if (defined $line) {
408 chomp $line;
409 if ($line =~ /<PagedDocument/) {
410 $xml_version = 1;
411 }
412 }
413
414 close ITEMFILE;
415 return $xml_version;
416}
417
418sub tidy_item_file {
419 my $self = shift(@_);
420 my ($filename) = @_;
421
422 open (ITEMFILE, "<:encoding(UTF-8)", $filename) || die "couldn't open $filename\n";
423 my $backup_filename = "backup.item";
424 open (BACKUP,">$backup_filename")|| die "couldn't write to $backup_filename\n";
425 binmode(BACKUP, ":utf8");
426 my $line = "";
427 $line = <ITEMFILE>;
428 #$line =~ s/^\xEF\xBB\xBF//; # strip BOM in text file read in as a sequence of bytes (not unicode aware strings)
429 $line =~ s/^\x{FEFF}//; # strip BOM in file opened *as UTF-8*. Strings in the file just read in are now unicode-aware,
430 # this means the BOM is now a unicode codepoint instead of a byte sequence
431 # See http://en.wikipedia.org/wiki/Byte_order_mark and http://perldoc.perl.org/5.14.0/perlunicode.html
432 $line =~ s/\x{0B}+//ig; # removing \vt-vertical tabs using the unicode codepoint for \vt
433 $line =~ s/&/&amp;/g;
434 print BACKUP ($line);
435 #Tidy up the item file some metadata title contains \vt-vertical tab
436 while ($line = <ITEMFILE>) {
437 $line =~ s/\x{0B}+//ig; # removing \vt-vertical tabs using the unicode codepoint for \vt
438 $line =~ s/&/&amp;/g;
439 print BACKUP ($line);
440 }
441 close ITEMFILE;
442 close BACKUP;
443 &File::Copy::copy ($backup_filename, $filename);
444 &FileUtils::removeFiles($backup_filename);
445
446}
447
448sub rotate_image {
449 my $self = shift (@_);
450 my ($filename_full_path) = @_;
451
452 my ($this_filetype) = $filename_full_path =~ /\.([^\.]*)$/;
453 my ($result, $result_str, $new_filename) = $self->convert($filename_full_path, $this_filetype, "-rotate 180", "ROTATE");
454
455 if (-e "$new_filename") {
456 return $new_filename;
457 }
458 # somethings gone wrong
459 return $filename_full_path;
460
461}
462
463sub process_image {
464 my $self = shift(@_);
465 my ($filename_full_path, $filename_no_path, $doc_obj, $section, $rotate) = @_;
466 # check the filenames
467 return 0 if ($filename_no_path eq "" || !-f $filename_full_path);
468
469 # remember that this image file was one of our source files, but only
470 # if we are not processing a tmp file
471 if (!$self->{'processing_tmp_files'} ) {
472 $doc_obj->associate_source_file($filename_full_path);
473 }
474 # do rotation
475 if ((defined $rotate) && ($rotate eq "r")) {
476 # we get a new temporary file which is rotated
477 $filename_full_path = $self->rotate_image($filename_full_path);
478 }
479
480 # do generate images
481 my $result = 0;
482 if ($self->{'image_conversion_available'} == 1) {
483 # do we need to convert $filename_no_path to utf8/url encoded?
484 # We are already reading in from a file, what encoding is it in???
485 my $url_encoded_full_filename
486 = &unicode::raw_filename_to_url_encoded($filename_full_path);
487 #print STDERR "#### process_image filename_full_path: $filename_full_path vs $self->{'orig_filename_full_path'}\n";
488 $result = $self->generate_images($filename_full_path, $url_encoded_full_filename, $doc_obj, $section);
489 }
490 #overwrite one set in ImageConverter
491 $doc_obj->set_metadata_element ($section, "FileFormat", "PagedImage");
492 return $result;
493}
494
495
496sub xml_start_tag {
497 my $self = shift(@_);
498 my ($expat, $element) = @_;
499 $self->{'element'} = $element;
500
501 my $doc_obj = $self->{'doc_obj'};
502 if ($element eq "PagedDocument") {
503 $self->{'current_section'} = $doc_obj->get_top_section();
504 } elsif ($element eq "PageGroup" || $element eq "Page") {
505 if ($element eq "PageGroup") {
506 $self->{'has_internal_structure'} = 1;
507 }
508 # create a new section as a child
509 $self->{'current_section'} = $doc_obj->insert_section($doc_obj->get_end_child($self->{'current_section'}));
510 $self->{'num_pages'}++;
511 # assign pagenum as what??
512 my $pagenum = $_{'pagenum'}; #TODO!!
513 if (defined $pagenum) {
514 $doc_obj->set_utf8_metadata_element($self->{'current_section'}, 'PageNum', $pagenum);
515 }
516 my ($imgfile) = $_{'imgfile'};
517 if (defined $imgfile) {
518 # *****
519 # What about support for rotate image (e.g. old ':r' notation)?
520 $self->process_image($self->{'xml_file_dir'}.$imgfile, $imgfile, $doc_obj, $self->{'current_section'});
521 }
522 my ($txtfile) = $_{'txtfile'};
523 if (defined($txtfile)&& $txtfile ne "") {
524 $self->process_text ($self->{'xml_file_dir'}.$txtfile, $txtfile, $doc_obj, $self->{'current_section'});
525 } else {
526 # A plugin inheriting from this might be able to derive text from the image
527 # (e.g., through GoogleVisionAPI), and so don't just assume there is no
528 # text for the image -- check its text length, and only set the dummy
529 # text if it is zero
530 $self->add_dummy_text_if_empty($doc_obj, $self->{'current_section'});
531 }
532 } elsif ($element eq "Metadata") {
533 $self->{'metadata_name'} = $_{'name'};
534 }
535}
536
537sub xml_end_tag {
538 my $self = shift(@_);
539 my ($expat, $element) = @_;
540
541 my $doc_obj = $self->{'doc_obj'};
542 if ($element eq "Page" || $element eq "PageGroup") {
543 # if Title hasn't been assigned, set PageNum as Title
544 if (!defined $doc_obj->get_metadata_element ($self->{'current_section'}, "Title") && defined $doc_obj->get_metadata_element ($self->{'current_section'}, "PageNum" )) {
545 $doc_obj->add_utf8_metadata ($self->{'current_section'}, "Title", $doc_obj->get_metadata_element ($self->{'current_section'}, "PageNum" ));
546 }
547 # move the current section back to the parent
548 $self->{'current_section'} = $doc_obj->get_parent_section($self->{'current_section'});
549 } elsif ($element eq "Metadata") {
550
551 # text read in by XML::Parser is in Perl's binary byte value
552 # form ... need to explicitly make it UTF-8
553 my $meta_name = decode("utf-8",$self->{'metadata_name'});
554 my $metadata_value = decode("utf-8",$self->{'metadata_value'});
555
556 if ($meta_name =~ /\./) {
557 $meta_name = "ex.$meta_name";
558 }
559
560 $doc_obj->add_utf8_metadata ($self->{'current_section'}, $meta_name, $metadata_value);
561 $self->{'metadata_name'} = "";
562 $self->{'metadata_value'} = "";
563
564 }
565 # otherwise we ignore the end tag
566}
567
568
569sub xml_text {
570 my $self = shift(@_);
571 my ($expat) = @_;
572
573 if ($self->{'element'} eq "Metadata" && $self->{'metadata_name'}) {
574 $self->{'metadata_value'} .= $_;
575 }
576}
577
578sub xml_doctype {
579}
580
581sub open_document {
582 my $self = shift(@_);
583
584 # create a new document
585 #$self->{'doc_obj'} = new doc ($self->{'filename'}, "indexed_doc", $self->{'file_rename_method'});
586 ## TODO is file filename_no_path??
587 #$self->set_initial_doc_fields($self->{'doc_obj'}, $self->{'filename'}, $self->{'processor'}, $self->{'metadata'});
588
589 # create a new document
590 $self->{'doc_obj'} = $self->init_new_doc_item($self->{'filename'}, $self->{'processor'}, $self->{'metadata'});
591
592 my ($dir, $file) = $self->{'filename'} =~ /^(.*?)([^\/\\]*)$/;
593 $self->{'xml_file_dir'} = $dir;
594 $self->{'num_pages'} = 0;
595 $self->{'has_internal_structure'} = 0;
596
597}
598
599sub close_document {
600 my $self = shift(@_);
601 my $doc_obj = $self->{'doc_obj'};
602
603 my $topsection = $doc_obj->get_top_section();
604
605 # add numpages metadata
606 $doc_obj->set_utf8_metadata_element ($topsection, 'NumPages', $self->{'num_pages'});
607
608 # set the document type
609 my $final_doc_type = "";
610 if ($self->{'documenttype'} eq "auto") {
611 if ($self->{'has_internal_structure'}) {
612 if ($self->{'gs_version'} eq "3") {
613 $final_doc_type = "pagedhierarchy";
614 }
615 else {
616 $final_doc_type = "hierarchy";
617 }
618 } else {
619 $final_doc_type = "paged";
620 }
621 } else {
622 # set to what doc type option was set to
623 $final_doc_type = $self->{'documenttype'};
624 }
625 $doc_obj->set_utf8_metadata_element ($topsection, "gsdlthistype", $final_doc_type);
626 ### capitalisation????
627# if ($self->{'documenttype'} eq 'paged') {
628 # set the gsdlthistype metadata to Paged - this ensures this document will
629 # be treated as a Paged doc, even if Titles are not numeric
630# $doc_obj->set_utf8_metadata_element ($topsection, "gsdlthistype", "Paged");
631# } else {
632# $doc_obj->set_utf8_metadata_element ($topsection, "gsdlthistype", "Hierarchy");
633# }
634
635 $doc_obj->set_utf8_metadata_element($topsection,"MaxImageWidth",$self->{'MaxImageWidth'});
636 $doc_obj->set_utf8_metadata_element($topsection,"MaxImageHeight",$self->{'MaxImageHeight'});
637 $self->{'MaxImageWidth'} = undef;
638 $self->{'MaxImageHeight'} = undef;
639}
640
641
642sub set_initial_doc_fields {
643 my $self = shift(@_);
644 my ($doc_obj, $filename_full_path, $processor, $metadata) = @_;
645
646 my $topsection = $doc_obj->get_top_section();
647
648 my $plugin_filename_encoding = $self->{'filename_encoding'};
649 my $filename_encoding = $self->deduce_filename_encoding($filename_full_path,$metadata,$plugin_filename_encoding);
650 $self->set_Source_metadata($doc_obj, $filename_full_path, $filename_encoding);
651
652 # if we want a header page, we need to add some text into the top section, otherwise this section will become invisible
653 if ($self->{'headerpage'}) {
654 $self->add_dummy_text($doc_obj, $topsection);
655 }
656}
657
658sub scan_xml_for_files_to_block
659{
660 my $self = shift (@_);
661 my ($filename_full_path, $dir, $block_hash) = @_;
662
663 open (ITEMFILE, $filename_full_path) || die "couldn't open $filename_full_path to work out which files to block\n";
664 my $line = "";
665 while (defined ($line = <ITEMFILE>)) {
666 next unless $line =~ /\w/;
667
668 if ($line =~ /imgfile=\"([^\"]+)\"/) {
669 $self->block_raw_filename($block_hash,&FileUtils::filenameConcatenate($dir,$1));
670 }
671 if ($line =~ /txtfile=\"([^\"]+)\"/) {
672 $self->block_raw_filename($block_hash,&FileUtils::filenameConcatenate($dir,$1));
673 }
674 }
675 close ITEMFILE;
676
677}
678
679sub scan_item_for_files_to_block
680{
681 my $self = shift (@_);
682 my ($filename_full_path, $dir, $block_hash) = @_;
683
684
685 open (ITEMFILE, $filename_full_path) || die "couldn't open $filename_full_path to work out which files to block\n";
686 my $line = "";
687 while (defined ($line = <ITEMFILE>)) {
688 next unless $line =~ /\w/;
689 chomp $line;
690 next if $line =~ /^#/; # ignore comment lines
691 next if ($line =~ /^<([^>]*)>\s*(.*?)\s*$/); # ignore metadata lines
692 # line should be like page:imagefilename:textfilename:r
693 $line =~ s/^\s+//; #remove space at the front
694 $line =~ s/\s+$//; #remove space at the end
695 my ($pagenum, $imgname, $txtname, $rotate) = split /:/, $line;
696
697 # find the image file if there is one
698 if (defined $imgname && $imgname ne "") {
699 $self->block_raw_filename($block_hash, &FileUtils::filenameConcatenate($dir,$imgname));
700 }
701 # find the text file if there is one
702 if (defined $txtname && $txtname ne "") {
703 $self->block_raw_filename($block_hash, &FileUtils::filenameConcatenate($dir,$txtname));
704 }
705 }
706 close ITEMFILE;
707
708}
709
710sub init_new_doc_item
711{
712 my $self = shift (@_);
713 my ($filename_full_path, $processor, $metadata) = @_;
714
715 my $doc_obj = new doc($filename_full_path, "indexed_doc", $self->{'file_rename_method'});
716 $self->set_initial_doc_fields($doc_obj, $filename_full_path, $processor, $metadata);
717
718 return $doc_obj;
719}
720
721sub read_and_process_itemtxt
722{
723 my $self = shift (@_);
724 my ($filename_full_path, $dir, $filename_no_path, $processor, $metadata, $doc_obj) = @_;
725
726 my $topsection = $doc_obj->get_top_section();
727
728 open (ITEMFILE, "<:encoding(UTF-8)", $filename_full_path) || die "couldn't open $filename_full_path\n";
729 my $line = "";
730 my $num = 0;
731 while (defined ($line = <ITEMFILE>)) {
732
733 next unless $line =~ /\w/;
734 chomp $line;
735 next if $line =~ /^#/; # ignore comment lines
736 if ($line =~ /^<([^>]*)>\s*(.*?)\s*$/) {
737 my $meta_name = $1;
738 my $meta_value = $2;
739 if ($meta_name =~ /\./) {
740 $meta_name = "ex.$meta_name";
741 }
742 $doc_obj->set_utf8_metadata_element ($topsection, $meta_name, $meta_value);
743 #$meta->{$1} = $2;
744 } else {
745 $num++;
746 # line should be like page:imagefilename:textfilename:r - the r is optional -> means rotate the image 180 deg
747 $line =~ s/^\s+//; #remove space at the front
748 $line =~ s/\s+$//; #remove space at the end
749 my ($pagenum, $imgname, $txtname, $rotate) = split /:/, $line;
750
751 # create a new section for each image file
752 my $cursection = $doc_obj->insert_section($doc_obj->get_end_child($topsection));
753 # the page number becomes the Title
754 $doc_obj->set_utf8_metadata_element($cursection, 'Title', $pagenum);
755
756 # process the image for this page if there is one
757 if (defined $imgname && $imgname ne "") {
758 my $result1 = $self->process_image($dir.$imgname, $imgname, $doc_obj, $cursection, $rotate);
759 if (!defined $result1)
760 {
761 print "PagedImagePlugin: couldn't process image \"$dir$imgname\" for item \"$filename_full_path\"\n";
762 }
763 }
764 # process the text file if one is there
765 if (defined $txtname && $txtname ne "") {
766 my $result2 = $self->process_text ($dir.$txtname, $txtname, $doc_obj, $cursection);
767
768 if (!defined $result2) {
769 print "PagedImagePlugin: couldn't process text file \"$dir.$txtname\" for item \"$filename_full_path\"\n";
770 $self->add_dummy_text_if_empty($doc_obj, $cursection);
771 }
772 } else {
773 # otherwise add in some dummy text
774 $self->add_dummy_text_if_empty($doc_obj, $cursection);
775 }
776 }
777 }
778
779 close ITEMFILE;
780
781 return $num;
782}
783
784
785sub process_item {
786 my $self = shift (@_);
787 my ($filename_full_path, $dir, $filename_no_path, $processor, $metadata) = @_;
788
789 # create a new document
790 #my $doc_obj = new doc ($filename_full_path, "indexed_doc", $self->{'file_rename_method'});
791 #$self->set_initial_doc_fields($doc_obj, $filename_full_path, $processor, $metadata);
792
793 # create a new document
794 my $doc_obj = $self->init_new_doc_item($filename_full_path, $processor, $metadata);
795
796 my $num_pages = $self->read_and_process_itemtxt($filename_full_path, $dir, $filename_no_path, $processor, $metadata, $doc_obj);
797
798 my $topsection = $doc_obj->get_top_section();
799
800 # simple item files are always paged unless user specified
801 if ($self->{'documenttype'} eq "auto") {
802 $doc_obj->set_utf8_metadata_element ($topsection, "gsdlthistype", "paged");
803 } else {
804 $doc_obj->set_utf8_metadata_element ($topsection, "gsdlthistype", $self->{'documenttype'});
805 }
806
807 # add numpages metadata
808 $doc_obj->set_utf8_metadata_element ($topsection, 'NumPages', "$num_pages");
809
810 $doc_obj->set_utf8_metadata_element($topsection,"MaxImageWidth",$self->{'MaxImageWidth'});
811 $doc_obj->set_utf8_metadata_element($topsection,"MaxImageHeight",$self->{'MaxImageHeight'});
812 $self->{'MaxImageWidth'} = undef;
813 $self->{'MaxImageHeight'} = undef;
814
815 return $doc_obj;
816}
817
818sub process_text {
819 my $self = shift (@_);
820 my ($filename_full_path, $file, $doc_obj, $cursection) = @_;
821
822 # check that the text file exists!!
823 if (!-f $filename_full_path) {
824 print "PagedImagePlugin: ERROR: File $filename_full_path does not exist, skipping\n";
825 return 0;
826 }
827
828 # remember that this text file was one of our source files, but only
829 # if we are not processing a tmp file
830 if (!$self->{'processing_tmp_files'} ) {
831 $doc_obj->associate_source_file($filename_full_path);
832 }
833 # Do encoding stuff
834 my ($language, $encoding) = $self->textcat_get_language_encoding ($filename_full_path);
835
836 my $text="";
837 &ReadTextFile::read_file($self, $filename_full_path, $encoding, $language, \$text); # already decoded as utf8
838 if (!length ($text)) {
839 # It's a bit unusual but not out of the question to have no text, so just give a warning
840 print "PagedImagePlugin: WARNING: $filename_full_path contains no text\n";
841 }
842
843 # we need to escape the escape character, or else mg will convert into
844 # eg literal newlines, instead of leaving the text as '\n'
845 $text =~ s/\\/\\\\/g; # macro language
846 $text =~ s/_/\\_/g; # macro language
847
848
849 if ($text =~ m/<html.*?>\s*<head.*?>.*<\/head>\s*<body.*?>(.*)<\/body>\s*<\/html>\s*$/is) {
850 # looks like HTML input
851 # no need to escape < and > or put in <pre> tags
852
853 $text = $1;
854
855 # add text to document object
856 $doc_obj->add_utf8_text($cursection, "$text");
857 }
858 else {
859 $text =~ s/</&lt;/g;
860 $text =~ s/>/&gt;/g;
861
862 # insert preformat tags and add text to document object
863 $doc_obj->add_utf8_text($cursection, "<pre>\n$text\n</pre>");
864 }
865
866
867 return 1;
868}
869
870
871sub clean_up_after_doc_obj_processing {
872 my $self = shift(@_);
873
874 $self->ImageConverter::clean_up_temporary_files();
875}
876
8771;
Note: See TracBrowser for help on using the repository browser.