Index: main/trunk/greenstone2/perllib/gsmysql.pm
===================================================================
--- main/trunk/greenstone2/perllib/gsmysql.pm	(revision 32639)
+++ main/trunk/greenstone2/perllib/gsmysql.pm	(revision 32640)
@@ -208,5 +208,5 @@
     $params_map->{'db_encoding'} = $self->{'db_encoding'};
     $params_map->{'verbosity'} = $self->{'verbosity'};
-    
+	
     $self->{'db_handle'} = &_get_connection_instance($params_map); # getting singleton (class method)
     if($self->{'db_handle'}) {
@@ -240,5 +240,5 @@
     
     # For proper utf8 support in MySQL, encoding should be 'utf8mb4' as 'utf8' is insufficient
-    my $db_enc = "utf8mb4" if $params_map->{'db_encoding'} eq "utf8";
+    my $db_enc = ($params_map->{'db_encoding'} eq "utf8") ? "utf8mb4" : $params_map->{'db_encoding'};
 
     # Params for connecting to MySQL
@@ -287,6 +287,7 @@
 			       RaiseError => 0, # off by default, but being explicit
 			       AutoCommit => $autocommit,
-			       mysql_enable_utf8mb4 => 1 # tells MySQL to use (4 byte) UTF-8 for
-				   # communication and tells DBD::mysql to use it to decode the data, 
+			       #mysql_enable_utf8mb4 => 1 # Unrecognised in Windows' Strawberry Perl 5.22. On Ubuntu perl 5.22
+				   # if mysql_enable_utf8mb4 was set to 1 *during connect()*, this tells MySQL to use (4 byte) UTF-8 for
+				   # communication AND tells DBD::mysql to use it to decode the data, 
 				   # see https://stackoverflow.com/questions/46727362/perl-mysql-utf8mb4-issue-possible-bug 
 			   });
@@ -322,7 +323,12 @@
     # (see https://www.perlmonks.org/?node_id=259456)
     
-    #my $stmt = "set NAMES '" . $db_enc . "'";
-    #$dbh->do($stmt) || warn("Unable to set charset encoding at db server level to: " . $db_enc . "\n"); # tells MySQL to use UTF-8 for communication
-    #$dbh->{mysql_enable_utf8mb4} = 1; # tells DBD::mysql to decode the data
+	# For Strawberry Perl 5.22, need to the following 2 steps here to take care of encoding issues
+	# The 2 steps are: set names utf8mb4; AND setting mysql_enable_utf8mb4 = 1
+	# Doing this in 2 steps here also works on Ubuntu perl 5.22,
+	# but that also supported setting mysql_enable_utf8mb4 to 1 during connect()
+	# to do both these steps in one go
+    my $stmt = "set NAMES '" . $db_enc . "'";
+    $dbh->do($stmt) || warn("Unable to set charset encoding at db server level to: " . $db_enc . "\n"); # tells MySQL to use UTF-8 for communication
+    $dbh->{mysql_enable_utf8mb4} = 1; # tells DBD::mysql to decode the data
     
     # if we're here, then connection succeeded, store handle
Index: main/trunk/greenstone2/perllib/plugins/GreenstoneSQLPlugin.pm
===================================================================
--- main/trunk/greenstone2/perllib/plugins/GreenstoneSQLPlugin.pm	(revision 32639)
+++ main/trunk/greenstone2/perllib/plugins/GreenstoneSQLPlugin.pm	(revision 32640)
@@ -37,5 +37,6 @@
 use gsmysql;
 
-
+#use unicode;
+#use Encode;
 
 ########################################################################################
@@ -370,5 +371,4 @@
     # browse the rebuilt collection by files/titles. So unless you set the OID here, the deleted
     # doc oids will still be listed in the index.
-    $self->{'doc_obj'}->set_OID($oid);
 	
     print STDERR "   GreenstoneSQLPlugin processing doc $oid (reading into docobj from SQL db)\n"
@@ -385,4 +385,19 @@
 	    my ($primary_key, $did, $sid, $metaname, $metaval) = @$row;
 	    
+		# don't allow duplicate toplevel OID, as can happen when a doc has been renamed and requires reindexing
+		# TODO: any other meta that should not be duplicated, but can have been changed between rebuilds so that we need to call set_meta instead of add_meta?
+		# e.g. FileSize, SourceFile. But Language can be multiple, so gs meta is not guaranteed to be unique either. Whereas of dc metadata we know
+		# that some if not all should allow multiple entires for the same meta name, e.g. dc.Author/dc.Creator
+		if($sid =~ m@^root@ && $metaname eq "Identifier") {
+			# doc_obj's toplevel Identifier metadata is a special case:
+			# it should have only one value, so instead of add_meta() that will allow old Identifier meta to linger
+			# Need to do set_meta(). We then break out of the loop, to prevent duplicates (older values from DB) to be inserted for Identifier into doc_obj
+			# Handles the case where a doc was renamed and rebuilding triggers re-indexing case: old identifier is now overwritten with new one
+			$self->{'doc_obj'}->set_OID($oid); # calls doc_obj->set_metadata_element(top_section, Identifier, $oid). Sets OID if one doesn't exist.
+			next; # ensures Identifier set only once, and ensure Identifier is set to current docOID for the doc, a.o.t. allowing it to be set to any expired docOID from before a doc got renamed.			
+		}
+		
+		# process all other metadata the normal way:
+		
 	    # get rid of the artificial "root" introduced in section id when saving to sql db
 	    $sid =~ s@^root@@;
@@ -395,8 +410,15 @@
 	    $doc_obj->add_utf8_metadata($sid, $metaname, $metaval);
 	}
+	
+	# UTF8 debugging, e.g. if we have a with macron in dc.Title
+	##binmode(STDERR, ":utf8"); # not necessary when calling utf8decomp to print wide characters as unicode codept, avoiding 'wide character' warnings.
+	#my $dcTitle = $doc_obj->get_metadata_element($doc_obj->get_top_section(), "dc.Title");	
+	#print STDERR "###### dc.Title: ".&unicode::utf8decomp($dcTitle)."\n";
+	#print STDERR "###### DECODED dc.Title: ".&unicode::utf8decomp(&Encode::decode_utf8($dcTitle))."\n"; # decoding was needed for perl 5.18
+	
 	print $outhandle "----------FIN READING DOC's META FROM SQL DB------------\n"
 	    if $self->{'verbosity'} > 2;
     }
-    
+	
     if($proc_mode eq "all" || $proc_mode eq "text_only") {
 	# read in fulltxt for the collection (i.e. select * from <col>_fulltxt table
@@ -429,5 +451,5 @@
     
     # done reading into docobj from SQL db
-    
+	
     # don't forget to clean up on close() in superclass
     # It will get the doc_obj indexed then make it undef
Index: main/trunk/greenstone2/perllib/plugouts/GreenstoneSQLPlugout.pm
===================================================================
--- main/trunk/greenstone2/perllib/plugouts/GreenstoneSQLPlugout.pm	(revision 32639)
+++ main/trunk/greenstone2/perllib/plugouts/GreenstoneSQLPlugout.pm	(revision 32640)
@@ -38,4 +38,5 @@
 use DBI; # the central package for this plugout
 
+#use unicode;
 
 # This plugout does not output the metadata and/or fulltxt xml to a file,
@@ -304,4 +305,7 @@
     if($proc_mode eq "all" || $proc_mode eq "meta_only" ) {
 	
+	##binmode(STDERR, ":utf8"); # shouldn't be necessary as we call &unicode::utf8decomp() to avoid wide-character warnings by printing wide chars as unicode codepoints
+	#print STDERR "###### dc.Title: ".&unicode::utf8decomp($doc_obj->get_metadata_element($doc_obj->get_top_section(), "dc.Title"))."\n";
+	
 	foreach my $data (@{$section_ptr->{'metadata'}}) {
 	    my $meta_name = $data->[0];
@@ -310,5 +314,5 @@
 	    # going into it, unlike with doc(sql).xml
 	    my $meta_value = $data->[1];
-	    
+		
 	    # Write out the current section's meta to collection db's METADATA table	    
 	    
