Index: /main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm
===================================================================
--- /main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm	(revision 23399)
+++ /main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm	(revision 23400)
@@ -58,6 +58,6 @@
 
     "set-archives-metadata" => { 'compulsory-args' => [ "d", "metaname", "metavalue" ],
-			         'optional-args'   => [ "metapos" ]
-
+			         'optional-args'   => [ "metapos", "metamode" ]
+					  # metamode can be "accumulate", "override",
 				},
 
@@ -99,5 +99,6 @@
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
     my $gsdlhome  = $self->{'gsdlhome'};
-
+	my $infodbtype = $self->{'infodbtype'};
+	
     # Note: Not sure why get_live_metadata doesn't need the authentication check
 
@@ -123,5 +124,5 @@
     $collect_tail =~ s/^.*[\/|\\]//;
     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
-    my $infodb_file_path = &dbutil::get_infodb_file_path("gdbm", "live-$collect_tail", $index_text_directory);
+    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
     
     # Obtain the content of the key
@@ -176,4 +177,5 @@
     my $metaname  = $self->{'metaname'};
     my $metapos   = $self->{'metapos'};
+    my $infodbtype = $self->{'infodbtype'};
 
     # To people who know $collect_tail please add some comments
@@ -182,10 +184,9 @@
     $collect_tail =~ s/^.*[\/\\]//;
     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
-    my $infodb_file_path = &dbutil::get_infodb_file_path("gdbm", $collect_tail, $index_text_directory);
+    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
 
     # Read the docid entry
-    my $doc_rec_string = &dbutil::read_infodb_entry("gdbm", $infodb_file_path, $docid);
-    my $doc_rec = &dbutil::convert_infodb_string_to_hash($doc_rec_string);
-
+    my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
+  
     # Basically loop through and unescape_html the values
     foreach my $k (keys %$doc_rec) {
@@ -216,5 +217,6 @@
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
     my $gsdlhome  = $self->{'gsdlhome'};
-
+    my $infodbtype = $self->{'infodbtype'};
+  
     # don't user authenticate for now
     if ($baseaction::authentication_enabled) {
@@ -235,4 +237,5 @@
     }
     my $metavalue = $self->{'metavalue'};
+  
 
     # Generate the dbkey    
@@ -245,5 +248,5 @@
     $collect_tail =~ s/^.*[\/\\]//;
     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
-    my $infodb_file_path = &dbutil::get_infodb_file_path("gdbm", "live-$collect_tail", $index_text_directory);
+    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
 
     # Set the new value
@@ -279,4 +282,6 @@
     my $gsdlhome  = $self->{'gsdlhome'};
 
+	
+	
     # don't user authenticate for now
     if ($baseaction::authentication_enabled) {
@@ -296,5 +301,6 @@
     my $metapos   = $self->{'metapos'};
     my $metavalue = $self->{'metavalue'};
-
+	my $infodbtype = $self->{'infodbtype'};
+	
     # To people who know $collect_tail please add some comments
     # Obtain path to the database
@@ -302,64 +308,35 @@
     $collect_tail =~ s/^.*[\/\\]//;
     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
-    my $infodb_file_path = &dbutil::get_infodb_file_path("gdbm", $collect_tail, $index_text_directory);
-
+    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
+	
     # Read the docid entry
-    my $doc_rec_string = &dbutil::read_infodb_entry("gdbm", $infodb_file_path, $docid);
-    my $doc_rec = &dbutil::convert_infodb_string_to_hash($doc_rec_string);
-    foreach my $k (keys %$doc_rec) {
-	my @escaped_v = ();
-	foreach my $v (@{$doc_rec->{$k}}) {
-	    if ($k eq "contains") {
-		# protect quotes in ".2;".3 etc
-		$v =~ s/\"/\\\"/g;
-		push(@escaped_v, $v);
-	    }
-	    else {
-		my $ev = &ghtml::unescape_html($v);
-		$ev =~ s/\"/\\\"/g;
-		push(@escaped_v, $ev);
-	    }
-	}
-	$doc_rec->{$k} = \@escaped_v;
-    }
-    ## print STDERR "**** metavalue = $metavalue\n";
-
-    # Protect the quotes
-    $metavalue =~ s/\"/\\\"/g;
-
+    my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
+	
     # Set the metadata value
     if (defined $metapos) {
-	$doc_rec->{$metaname}->[$metapos] = $metavalue;
+		$doc_rec->{$metaname}->[$metapos] = $metavalue;
     }
     else {
-	$doc_rec->{$metaname} = [ $metavalue ];
-    }
-    ## print STDERR "**** metavalue = $metavalue\n";
-
-    # Generate the record string
-    my $serialized_doc_rec = &dbutil::convert_infodb_hash_to_string($doc_rec);
-    ## print STDERR "**** ser dr\n$serialized_doc_rec\n\n\n";
-
-    # Store it into GDBM
-    my $cmd = "gdbmset \"$infodb_file_path\" \"$docid\" \"$serialized_doc_rec\"";
-    my $status = system($cmd);
+		$doc_rec->{$metaname} = [ $metavalue ];
+    }
+ 
+	my $status = &dbutil::set_infodb_entry($infodbtype, $infodb_file_path,$docid,$doc_rec);
     if ($status != 0) {
         # Catch error if gdbmget failed
-	my $mess = "Failed to set metadata key: $docid\n";
-	
-	$mess .= "PATH: $ENV{'PATH'}\n";
-	$mess .= "cmd = $cmd\n";
-	$mess .= "Exit status: $status\n";
-	$mess .= "System Error Message: $!\n";
-
-	$gsdl_cgi->generate_error($mess);
+		my $mess = "Failed to set metadata key: $docid\n";
+	
+		$mess .= "PATH: $ENV{'PATH'}\n";
+		$mess .= "Exit status: $status\n";
+		$mess .= "System Error Message: $!\n";
+
+		$gsdl_cgi->generate_error($mess);
     }
     else {
-	my $mess = "set-document-metadata successful: Key[$docid]\n";
-	$mess .= "  $metaname";
-	$mess .= "->[$metapos]" if (defined $metapos);
-	$mess .= " = $metavalue";
-
-	$gsdl_cgi->generate_ok_message($mess);
+		my $mess = "set-document-metadata successful: Key[$docid]\n";
+		$mess .= "  $metaname";
+		$mess .= "->[$metapos]" if (defined $metapos);
+		$mess .= " = $metavalue";
+
+		$gsdl_cgi->generate_ok_message($mess);
     }
     
@@ -460,5 +437,5 @@
 {
     my $self = shift @_;
-    my ($gsdl_cgi, $doc_xml_filename, $metaname, $metavalue, $metapos) = @_;
+    my ($gsdl_cgi, $doc_xml_filename, $metaname, $metavalue, $metapos, $metamode) = @_;
 
     # use XML::Rules to add it in (read in and out again)
@@ -472,5 +449,7 @@
     my $options = { 'metaname'  => $metaname,
 		    'metapos'   => $metapos,
-		    'metavalue' => $metavalue };
+		    'metavalue' => $metavalue,
+			'metamode'  => $metamode };
+			
     $self->edit_xml_file($gsdl_cgi,$doc_xml_filename,\@rules,$options);
 }
@@ -485,5 +464,6 @@
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
     my $gsdlhome  = $self->{'gsdlhome'};
-
+    my $infodbtype = $self->{'infodbtype'};
+	
     # don't user authenticate for now
     if ($baseaction::authentication_enabled) {
@@ -503,11 +483,18 @@
     my $metaname   = $self->{'metaname'};
     my $metavalue  = $self->{'metavalue'};
+	
     my $metapos    = $self->{'metapos'};
     $metapos = 0 if (!defined $metapos);
+
+	my $metamode   = $self->{'metamode'};
+    if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
+	# make "accumulate" the default (less destructive, as won't actually 
+	# delete any existing values)
+	$metamode = "accumulate";
+    }
     
     # Obtain the doc.xml path for the specified docID
-    my $arcinfo_doc_filename = &dbutil::get_infodb_file_path("gdbm", "archiveinf-doc", $archive_dir);
-    my $doc_rec_string = &dbutil::read_infodb_entry("gdbm", $arcinfo_doc_filename, $docid);
-    my $doc_rec = &dbutil::convert_infodb_string_to_hash($doc_rec_string);    
+    my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
+    my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
     my $doc_xml_file = $doc_rec->{'doc-file'}->[0];
     
@@ -521,8 +508,17 @@
     # Running import.pl -groupsize will cause this to have multiple sections in one doc.xml
     $self->edit_doc_xml($gsdl_cgi,$doc_xml_filename,
-			$metaname,$metavalue,$metapos);
-
+			$metaname,$metavalue,$metapos,$metamode);
+
+	my $mess = "set-archives-metadata successful: Key[$docid]\n";
+	$mess .= "  $metaname";
+	$mess .= "->[$metapos]" if (defined $metapos);
+	$mess .= " = $metavalue";
+    $mess .= " ($metamode)\n";
+	
+	$gsdl_cgi->generate_ok_message($mess);
+		
     # Release the lock once it is done
     $self->unlock_collection($username, $collect);
+	
 }
 
@@ -682,5 +678,6 @@
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
     my $gsdlhome  = $self->{'gsdlhome'};
-
+    my $infodbtype = $self->{'infodbtype'};
+	
     # don't user authenticate for now
     if ($baseaction::authentication_enabled) {
@@ -719,7 +716,6 @@
     my $import_filename = undef;
     if (defined $docid) {
-	my $arcinfo_doc_filename = &dbutil::get_infodb_file_path("gdbm", "archiveinf-doc", $archive_dir);
-	my $doc_rec_string = &dbutil::read_infodb_entry("gdbm", $arcinfo_doc_filename, $docid);
-	my $doc_rec = &dbutil::convert_infodb_string_to_hash($doc_rec_string);
+	my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
+	my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
 
 	# This now stores the full pathname
@@ -759,5 +755,6 @@
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
     my $gsdlhome  = $self->{'gsdlhome'};
-
+    my $infodbtype = $self->{'infodbtype'};
+	
     if ($baseaction::authentication_enabled) {
 	# Ensure the user is allowed to edit this collection
@@ -786,5 +783,5 @@
     $collect_tail =~ s/^.*[\/\\]//;
     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
-    my $infodb_file_path = &dbutil::get_infodb_file_path("gdbm", "live-$collect_tail", $index_text_directory);
+    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
 
     # Remove the key
@@ -817,5 +814,6 @@
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
     my $gsdlhome  = $self->{'gsdlhome'};
-
+    my $infodbtype = $self->{'infodbtype'};
+	
     # don't user authenticate for now
     if ($baseaction::authentication_enabled) {
@@ -843,9 +841,8 @@
     $collect_tail =~ s/^.*[\/\\]//;
     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
-    my $infodb_file_path = &dbutil::get_infodb_file_path("gdbm", $collect_tail, $index_text_directory);
+    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
 
     # Read the docid entry
-    my $doc_rec_string = &dbutil::read_infodb_entry("gdbm", $infodb_file_path, $docid);
-    my $doc_rec = &dbutil::convert_infodb_string_to_hash($doc_rec_string);
+    my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
 
     # Basically loop through and unescape_html the values
@@ -923,5 +920,6 @@
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
     my $gsdlhome  = $self->{'gsdlhome'};
-
+    my $infodbtype = $self->{'infodbtype'};
+	
     # If the import metadata and gdbm database have been updated, we need to insert some notification to warn user that the the text they see at the moment is not indexed and require a rebuild.
     my $rebuild_pending_macro = "_rebuildpendingmessage_";
@@ -963,7 +961,6 @@
     # If the doc oid is not specified, we assume the metadata.xml is next to the specified "f"
     my $metadata_xml_file;
-    my $arcinfo_doc_filename = &dbutil::get_infodb_file_path("gdbm", "archiveinf-doc", $archive_dir);
-    my $archive_doc_rec_string = &dbutil::read_infodb_entry("gdbm", $arcinfo_doc_filename, $docid);
-    my $archive_doc_rec = &dbutil::convert_infodb_string_to_hash($archive_doc_rec_string);
+    my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
+    my $archive_doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
     
     # This now stores the full pathname
@@ -1014,9 +1011,9 @@
     $collect_tail =~ s/^.*[\/\\]//;
     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
-    my $infodb_file_path = &dbutil::get_infodb_file_path("gdbm", $collect_tail, $index_text_directory);
+    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
 
     # Read the docid entry
-    my $doc_rec_string = &dbutil::read_infodb_entry("gdbm", $infodb_file_path, $docid);
-    my $doc_rec = &dbutil::convert_infodb_string_to_hash($doc_rec_string);
+    my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
+   
     foreach my $k (keys %$doc_rec) {
 	my @escaped_v = ();
