Index: trunk/gsdl/perllib/plugins/ConvertToPlug.pm
===================================================================
--- trunk/gsdl/perllib/plugins/ConvertToPlug.pm	(revision 10277)
+++ trunk/gsdl/perllib/plugins/ConvertToPlug.pm	(revision 10278)
@@ -1,6 +1,5 @@
 ###########################################################################
 #
-# ConvertToPlug.pm -- plugin that inherits from HTML or TEXT Plug, depending
-#                     on plugin argument convert_to
+# ConvertToPlug.pm -- plugin that inherits from BasPlug
 #
 # A component of the Greenstone digital library software
@@ -26,12 +25,10 @@
 ###########################################################################
 
-# The plugin is inherited by such plugins as WordPlug and PDFPlug.
-# It facilitates the conversion of these document types to either HTML
-# or TEXT by setting up variable that instruct ConvertToBasPlug
-# how to work.
-
-# It works by dynamically inheriting HTMLPlug or TEXTPlug based on
-# the plugin argument 'convert_to'.  If the argument is not present,
-# the default is to inherit HTMLPlug.
+# This plugin is inherited by such plugins as WordPlug, PPTPlug, PSPlug, RTFPlug 
+# and PDFPlug. It facilitates the conversion of these document types to either 
+# HTML, Text or auto (allow user to choose which format to convert to).
+# It works by dynamically inheriting BasPlug and base on the plugin type in
+# secondary_plugins to devide which format to 'convert_to'. If the argument is 
+# not present, the default is to inherit auto.
 
 
@@ -39,22 +36,30 @@
 
 use BasPlug;
+use ghtml;
 use HTMLPlug;
 use TEXTPlug;
-use ghtml;
-
-use strict;
-no strict 'refs'; # allow filehandles to be variables and viceversa
+use PagedImgPlug;
+
+#use strict;
+#no strict 'refs'; # allow filehandles to be variables and viceversa
 
 sub BEGIN {
-    @ConvertToPlug::ISA = ('HTMLPlug');
-#    @ISA = ('HTMLPlug', 'TEXTPlug');
-#    @ISA = ('BasPlug'); #, 'HTMLPlug', 'TEXTPlug');
+    @ISA = ('BasPlug');
 }
 
 my $convert_to_list =
-    [ {	'name' => "html",
+    [ {	'name' => "auto",
+	'desc' => "{ConvertToPlug.convert_to.auto}" },
+      {	'name' => "html",
 	'desc' => "{ConvertToPlug.convert_to.html}" },
       {	'name' => "text",
-	'desc' => "{ConvertToPlug.convert_to.text}" } ];
+	'desc' => "{ConvertToPlug.convert_to.text}" },
+      { 'name' => "pagedimg-jpg",
+	'desc' => "{ConvertToPlug.convert_to.pagedimg-jpg"},
+      { 'name' => "pagedimg-gif",
+	'desc' => "{ConvertToPlug.convert_to.pagedimg-gif"},
+      { 'name' => "pagedimg-png",
+	'desc' => "{ConvertToPlug.convert_to.pagedimg-png"}, 
+      ];
 
 my $arguments =
@@ -95,5 +100,7 @@
 	if($inputargs->[$intCounter] eq "-convert_to")
 	{
-	    if($inputargs->[$intCounter+1] eq "text" || $inputargs->[$intCounter+1] eq "html")
+	    #if($inputargs->[$intCounter+1] eq "auto" || $inputargs->[$intCounter+1] =~ /pagedimg.*/i || $inputargs->[$intCounter+1] eq "text" || $inputargs->[$intCounter+1] eq "html")
+	    # if the setting is "auto" then refer to html for now
+	    if($inputargs->[$intCounter+1] =~ /pagedimg.*/i || $inputargs->[$intCounter+1] eq "text" || $inputargs->[$intCounter+1] eq "html")
 	    {
 		return $inputargs->[$intCounter+1];
@@ -103,4 +110,27 @@
     }
     return "html";
+}
+
+sub load_secondary_plugins
+{
+    my $self = shift (@_);
+    my ($class,$plugin_options) = @_;
+
+    my @convert_to_list = split(",",$self->{'convert_to'});
+    
+    $secondary_plugins = {};
+
+    foreach my $convert_to (@convert_to_list) {
+	# load in "convert_to" plugin package
+	my $plugin_class = $convert_to."Plug";
+	my $plugin_package = $plugin_class.".pm";
+	require $plugin_package;
+
+	# call its constructor with extra options that we've worked out!
+	my $arglist = $plugin_options->{$plugin_class};
+	my $secondary_plugin = new $plugin_class([], \@$arglist);
+	$secondary_plugins->{$plugin_class} = $secondary_plugin;
+    }
+    $self->{'secondary_plugins'} = $secondary_plugins;
 }
 
@@ -127,13 +157,33 @@
 	$self->{'convert_to'} = "TEXT";
 	$self->{'convert_to_ext'} = "txt";
-    }
-    else
-    {
+	my $text_options = [];
+	push(@$text_options,"-metadata_fields","Title,GENERATOR");
+	$secondary_plugin_options->{'TextPlug'} = $text_options;
+    }
+    elsif ($strConvertTo =~ /pagedimg.*/i){
+	$self = (defined $hashArgOptLists)? new PagedImgPlug($pluginlist,$inputargs,$hashArgOptLists): new PagedImgPlug($pluginlist,$inputargs);
+	$self->{'convert_to'} = "PagedImg";
+	#$self->{'convert_to'} = $strConvertTo;
+	my $convert_to_ext = $strConvertTo;
+	$convert_to_ext =~ s/.*\-(.*)/$1/i;
+	if ($convert_to_ext eq "gif"){
+	    $self->{'convert_to_ext'} = "gif";
+	} elsif ($convert_to_ext eq "jpg"){
+	    $self->{'convert_to_ext'} = "jpg";
+	} elsif ($convert_to_ext eq "png") {
+	    $self->{'convert_to_ext'} = "png";
+	}
+   	my $pagedimg_options = [];
+	push(@$pagedimg_options,"-metadata_fields","Title,GENERATOR");
+	$secondary_plugin_options->{'PagedImgPlug'} = $pagedimg_options;
+    } else {
+	# HTML or auto
 	$self = (defined $hashArgOptLists)? new HTMLPlug($pluginlist,$inputargs,$hashArgOptLists): new HTMLPlug($pluginlist,$inputargs);
 	$self->{'convert_to'} = "HTML";
 	$self->{'convert_to_ext'} = "html";
-
-	$self->{'rename_assoc_files'} = 1;
-	$self->{'metadata_fields'} .= ",GENERATOR";
+	my $html_options = [];
+	push(@$html_options,"-rename_assoc_files","1");
+	push(@$html_options,"-metadata_fields","Title,GENERATOR");
+	$secondary_plugin_options->{'HTMLPlug'} = $html_options;
     }
 
@@ -141,17 +191,39 @@
 }
 
-# we don't need to block anything, so override the one for HTMLPlug
-# files are converted in a temp dir and extra files not passed down the
-# plugin list
-sub get_default_block_exp {
-    my $self = shift (@_);
-
-    return "";
-}
-
-# Go straight to BasPlug and avoid the special case implemented by HTMLPlug
-sub store_block_files {
-    return BasPlug::store_block_files(@_);
-}
+
+sub init {
+    my $self = shift (@_);
+    my ($verbosity, $outhandle, $failhandle) = @_;
+
+    $self->SUPER::init($verbosity,$outhandle,$failhandle);
+
+    my $secondary_plugins =  $self->{'secondary_plugins'};
+
+    foreach my $plug_name (keys %$secondary_plugins) {
+	my $plugin = $secondary_plugins->{$plug_name};
+	$plugin->init($verbosity,$outhandle,$failhandle);
+    }
+}
+
+sub deinit {
+    # called only once, after all plugin passes have been done
+
+    my ($self) = @_;
+
+    my $secondary_plugins =  $self->{'secondary_plugins'};
+
+    foreach my $plug_name (keys %$secondary_plugins) {
+	my $plugin = $secondary_plugins->{$plug_name};
+	$plugin->deinit();
+    }
+}
+
+
+sub convert_post_process
+{
+    # by default do no post processing
+    return;
+}
+
 
 # Run conversion utility on the input file.  
@@ -171,4 +243,5 @@
     my $convert_to = $self->{'convert_to'};
     my $failhandle = $self->{'failhandle'};
+    my $convert_to_ext = $self->{'convert_to_ext'};
 
     # softlink to collection tmp dir
@@ -198,5 +271,11 @@
     # Execute the conversion command and get the type of the result,
     # making sure the converter gives us the appropriate output type
-    my $output_type = lc($convert_to);
+    my $output_type="";
+    if ($convert_to =~ m/PagedImg/i) {
+	$output_type = lc($convert_to)."-".lc($convert_to_ext);
+    } else {
+	$output_type = lc($convert_to);
+    }
+
     my $cmd = "perl -S gsConvert.pl -verbose $verbosity ";
     if (defined $self->{'convert_options'}) {
@@ -211,5 +290,5 @@
 
     # remove symbolic link to original file
-    &util::rm($tmp_filename);
+    #&util::rm($tmp_filename);
 
     # Check STDERR here
@@ -233,5 +312,4 @@
     # store the *actual* output type and return the output filename
     # it's possible we requested conversion to html, but only to text succeeded
-
     $self->{'convert_to_ext'} = $output_type;
     if ($output_type =~ /html/i) {
@@ -239,9 +317,14 @@
     } elsif ($output_type =~ /te?xt/i) {
 	$self->{'converted_to'} = "TEXT";
-    }
+    } elsif ($output_type =~ /item/i){
+	$self->{'converted_to'} = "PagedImg";
+    }
+    
     my $output_filename = $tmp_filename;
-
-    $output_filename =~ s/$suffix$/.$output_type/;
-
+    if ($output_type =~ /item/i) {
+	$output_filename = $tmp_dirname . "\\$tailname\\" . $tailname . ".$output_type";
+    } else {
+	$output_filename =~ s/$suffix$/.$output_type/;
+    }
     return $output_filename;
 }
@@ -249,5 +332,4 @@
 
 # Remove collection specific tmp directory and all its contents.
-
 sub cleanup_tmp_area {
     my $self = shift (@_);
@@ -255,14 +337,11 @@
     my $tmp_dirname 
 	= &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "tmp");
-    &util::rm_r($tmp_dirname);
+    #&util::rm_r($tmp_dirname);
     &util::mk_dir($tmp_dirname);
 }
-
-
-
 
 # Override BasPlug read
 # We don't want to get language encoding stuff until after we've converted
-# our file to either TEXT or HTML.
+# our file to either TEXT or HTML or PagedImage.
 sub read {
     my $self = shift (@_);
@@ -273,70 +352,57 @@
 
     my $outhandle = $self->{'outhandle'};
-    
-    my $filename = $file;
-    $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
-
-    if ($self->associate_with($file,$filename,$metadata)) {
-	# a form of smart block
-	$self->{'num_blocked'} ++;
-	return 0; # blocked
-    }
-
-    if ($self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/) {
-	$self->{'num_blocked'} ++;
-	return 0;
-    }
-    if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
-        return undef;
-    }
-    $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
-
-    # read in file ($text will be in utf8)
-    my $text = "";
+
+    my ($block_status,$filename) = $self->read_block(@_);
+    return $block_status if ((!defined $block_status) || ($block_status==0));
+    $file = $self->read_tidy_file($file);
 
     my $output_ext = $self->{'convert_to_ext'};
-    
     my $conv_filename = $self->tmp_area_convert_file($output_ext, $filename);
-
     if ("$conv_filename" eq "") {return 0;} # allows continue on errors
     if (! -e "$conv_filename") {return 0;} # allows continue on errors
     $self->{'conv_filename'} = $conv_filename;
-
-    # Do encoding stuff
-    my ($language, $encoding) = $self->textcat_get_language_encoding ($conv_filename);
-
-    &BasPlug::read_file($self, $conv_filename, $encoding, $language, \$text);
-    if (!length ($text)) {
-	my $plugin_name = ref ($self);
-        print $outhandle "$plugin_name: ERROR: $file contains no text\n" if $self->{'verbosity'};
-        return 0;
-    }
-
-    # if we converted to HTML, convert &eacute; and etc to utf-8.
-    # this should really happen before language_extraction, but that means
-    # modifying a file on disk...
-    $text =~ s/&([^;]+);/&ghtml::getcharequiv($1,0)/ge;
-
-    # create a new document
-    #my $doc_obj = new doc ($conv_filename, "indexed_doc");
-    # now we use the original filename here
-    my $doc_obj = new doc($filename, "indexed_doc");
-    $doc_obj->set_converted_filename($conv_filename);
-    $doc_obj->set_OIDtype ($processor->{'OIDtype'});
-    $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Language", $language);
-    $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Encoding", $encoding);
+    
+    $self->convert_post_process($conv_filename);
+    
+    my $secondary_plugins =  $self->{'secondary_plugins'};
+    my $num_secondary_plugins = scalar(keys %$secondary_plugins);
+    if ($num_secondary_plugins == 0) {
+	print $outhandle "Warning: No secondary plugin to use in conversion.  Skipping $file\n";
+	return 0; # effectively block it
+    }
+
+    my @plugin_names = keys %$secondary_plugins;
+    my $plugin_name = shift @plugin_names;
+	
+    if ($num_secondary_plugins > 1) {
+	print $outhandle "Warning: Multiple secondary plugins not supported yet!  Choosing $plugin_name\n.";
+    }
+    
+    my $secondary_plugin = $secondary_plugins->{$plugin_name};
+    
+    # note: metadata is not carried on to the next level
+    my ($rv,$doc_obj) 
+	= $secondary_plugin->read_into_doc_obj ($pluginfo,"", $conv_filename, 
+						$metadata, $processor, $maxdocs, $total_count,
+						$gli);
+
+    if ((!defined $rv) || ($rv<1)) {
+	# wasn't processed
+	return $rv;
+    }
+    
+    # Override previous gsdlsourcefilename set by secondary plugin
+    my $collect_file = &util::filename_within_collection($filename);
+    my $collect_conv_file = &util::filename_within_collection($conv_filename);
+    $doc_obj->set_source_filename ($collect_file); 
+    $doc_obj->set_converted_filename($collect_conv_file);
+
     my ($filemeta) = $file =~ /([^\\\/]+)$/;
-    $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Source", &ghtml::dmsafe($filemeta));
-    if ($self->{'cover_image'}) {
-	$self->associate_cover_image($doc_obj, $filename);
-    }
-    $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
-    $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "FileSize", (-s $filename));
-
-    # include any metadata passed in from previous plugins 
-    # note that this metadata is associated with the top level section
-    $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
+    $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "Source", &ghtml::dmsafe($filemeta));
+    $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
+    $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "FileSize", (-s $filename));
+    
     # do plugin specific processing of doc_obj
-    unless (defined ($self->process(\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli))) {
+    unless (defined ($self->process(undef, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli))) {
 	print STDERR "<ProcessingError n='$file'>\n" if ($gli);
 	return -1;
@@ -348,5 +414,5 @@
     # process the document
     $processor->process($doc_obj);
-    $self->cleanup_tmp_area();
+##    $self->cleanup_tmp_area();
 
     $self->{'num_processed'} ++;
@@ -356,30 +422,9 @@
 
 
-# do plugin specific processing of doc_obj for HTML type
+# do plugin specific processing of doc_obj for doc_ext type
 sub process_type {
     my $self = shift (@_);
-    my ($doc_ext, $textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
-    
-    my $conv_filename = $self->{'conv_filename'};
-    my $tmp_dirname = File::Basename::dirname($conv_filename);
-    my $tmp_tailname = File::Basename::basename($conv_filename);
-    
-    my $converted_to = $self->{'converted_to'};
-    my $ret_val;    
-
-    if ($converted_to eq "TEXT")
-    {
-
-	$ret_val = &TEXTPlug::process($self, $textref, $pluginfo,
-				      $tmp_dirname, $tmp_tailname,
-				      $metadata, $doc_obj);
-    }
-    else
-    {
-	$ret_val = &HTMLPlug::process($self, $textref, $pluginfo,
-				      $tmp_dirname, $tmp_tailname,
-				      $metadata, $doc_obj);
-    }
-
+    my ($doc_ext, $base_dir, $file, $doc_obj) = @_;
+    
     # associate original file with doc object
     my $cursection = $doc_obj->get_top_section();
@@ -413,6 +458,13 @@
     $doc_obj->add_utf8_metadata ($cursection, "/srclink", "</a>"); 
 
-    return $ret_val;
+    return 1;
 }
 
 1;
+
+
+
+
+
+
+
