Index: /main/trunk/greenstone2/perllib/plugins/GreenstoneSQLPlugin.pm
===================================================================
--- /main/trunk/greenstone2/perllib/plugins/GreenstoneSQLPlugin.pm	(revision 32541)
+++ /main/trunk/greenstone2/perllib/plugins/GreenstoneSQLPlugin.pm	(revision 32542)
@@ -1,5 +1,5 @@
 ###########################################################################
 #
-# GreenstoneSQLPlugin.pm -- reads into doc_obj from SQL db and docsql-<OID>.xml
+# GreenstoneSQLPlugin.pm -- reads into doc_obj from SQL db and docsql.xml
 # Metadata and/or fulltext are stored in SQL db, the rest may be stored in
 # the docsql .xml files.
@@ -42,5 +42,5 @@
 # Ask about docsql naming convention adopted to identify OID. Better way?
 # collection names -> table names: it seems hyphens not allowed. Changed to underscores.
-# - Startup parameters
+# + Startup parameters (except removeold/build_mode)
 # - incremental building: where do we need to add code to delete rows from our sql table after
 # incrementally importing a coll with fewer docs (for instance)? What about deleted/modified meta?
@@ -63,4 +63,7 @@
 # won't delete it from archives but buildcol step will. Need to implement this with this database plugin or wherever the actual flow is
 
+# TODO: Add public instructions on using this plugin and its plugout: start with installing mysql binary, changing pwd, running the server (and the client against it for checking, basic cmds like create and drop). Then discuss db name, table names (per coll), db cols and col types, and how the plugout and plugin work.
+# Discuss the plugin/plugout parameters.
+
 sub BEGIN {
     @GreenstoneSQLPlugin::ISA = ('GreenstoneXMLPlugin');
@@ -69,9 +72,13 @@
 # This plugin must be in the document plugins pipeline IN PLACE OF GreenstoneXMLPlugin
 # So we won't have a process exp conflict here.
+# The structure of docsql.xml files is identical to doc.xml and the contents are similar except:
+#   - since metadata and/or fulltxt are stored in mysql db instead, just XML comments indicating
+#   this are left inside docsql.xml within the <Description> (for meta) and/or <Content> (for txt)
+#   - the root element Archive now has a docoid attribute: <Archive docoid="OID">
 sub get_default_process_exp {
     my $self = shift (@_);
 
-    #return q^(?i)docsql(-\d+)?\.xml$^;
-    return q^(?i)docsql(-.+)?\.xml$^;
+    return q^(?i)docsql(-\d+)?\.xml$^; # regex based on this method in GreenstoneXMLPlugin
+    #return q^(?i)docsql(-.+)?\.xml$^; # no longer storing the OID embedded in docsql .xml filename
 }
 
@@ -156,4 +163,28 @@
 }
 
+sub xml_start_tag {
+    my $self = shift(@_);
+    my ($expat, $element) = @_;
+
+    my $outhandle = $self->{'outhandle'};
+    
+    $self->{'element'} = $element;
+    if ($element eq "Archive") { # docsql.xml files contain a OID attribute on Archive element
+	# the element's attributes are in %_ as per ReadXMLFile::xml_start_tag() (while $_
+	# contains the tag)
+
+	# Don't access %_{'docoid'} directly: keep getting a warning message to 
+	# use $_{'docoid'} for scalar contexts, but %_ is the element's attr hashmap 
+	# whereas $_ has the tag info. So we don't want to do $_{'docoid'}.
+	my %attr_hash = %_; # right way, see OAIPlugin.pm
+	$self->{'doc_oid'} = $attr_hash{'docoid'};	
+	print $outhandle "Extracted OID from docsql.xml: ".$self->{'doc_oid'}."\n"
+	    if $self->{'verbosity'} > 1;
+
+    }
+    else { # let superclass GreenstoneXMLPlugin continue to process <Section> and <Metadata> elements
+	$self->SUPER::xml_start_tag(@_);
+    }
+}
 
 # TODO Q: Why are there 3 passes when we're only indexing at doc and section level (2 passes)?
@@ -169,5 +200,5 @@
     my $gs_sql = $self->{'gs_sql'};
 
-    my $oid = $self->{'doc_oid'}; # we stored current doc's OID during sub read() 
+    my $oid = $self->{'doc_oid'}; # we stored current doc's OID during sub xml_start_tag()
     print $outhandle "==== OID of document (meta|text) to be read in from DB: $oid\n"
 	if $self->{'verbosity'} > 1;
@@ -325,35 +356,2 @@
 }
 
-sub read {
-    my $self = shift (@_);  
-  
-    my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
-
-    # when running buildcol.pl, the filename should match "docsql-<OID>.xml"
-    # when running import.pl it will be the original document's filename
-    # we only want to read in from db when running buildcol.pl
-
-    # doc_obj doesn't exist yet and only exists during super::read(): a new doc (doc_obj)
-    # is created in super::open_document() and is made undef again on super::close_document().
-    # Further, can't read it in from doc.xml to work out which OID to query in sql db:
-    # even if we got access to doc_obj, if no meta stored in docsql.xml, then when
-    # doc_obj is read in from docsql.xml there will be no OID. So OID is docsql.xml filename
-    # contains OID in filename. Having extracted OID from the filename, store OID in plugin-self
-    if($file =~ m/docsql-(.+?)\.xml$/) {
-
-	# work out docoid from filename of form "docsql-<OID>.xml". $file can have a containing
-	# subfolder besides filename, e.g. "dir/docsql-<OID>.xml"
-	
-	# https://stackoverflow.com/questions/22836/how-do-i-perform-a-perl-substitution-on-a-string-while-keeping-the-original
-	(my $oid = $file) =~ s@^(.*?)docsql-(.+?)\.xml$@$2@;
-
-	$self->{'doc_oid'} = $oid;
-    }    
-
-    
-    # always read docsql.xml, as we then know doc structure, and assoc files are dealt with
-    # Plus we need to read docsql.xml if either meta or fulltxt went into there instead of to sql db
-    return $self->SUPER::read(@_); # will open_doc, close_doc then process doc_obj for indexing, then undef doc_obj
-
-    
-}
Index: /main/trunk/greenstone2/perllib/plugouts/GreenstoneSQLPlugout.pm
===================================================================
--- /main/trunk/greenstone2/perllib/plugouts/GreenstoneSQLPlugout.pm	(revision 32541)
+++ /main/trunk/greenstone2/perllib/plugouts/GreenstoneSQLPlugout.pm	(revision 32542)
@@ -52,8 +52,10 @@
 # It's fine: the die() stmts all take place before setting up the super class' begin
 
+# TODO Q: about build_mode: how to detect removeold
 # TODO: deal with -removeold and everything? Or type out instructions for user
 
 # TODO Q: what is "group" in GreenstoneXMLPlugout?
 # TODO Q: site_name only exists for GS3. What about GS2?
+
 
 my $process_mode_list =
@@ -216,14 +218,22 @@
 }
 
-# produce files called docsql-<OID>.xml instead of doc.xml
+# Produce files called docsql.xml instead of doc.xml
 sub get_doc_xml_filename {
     my $self = shift (@_);
     my ($doc_obj) = @_;
     
-    my $doc_id = $doc_obj->get_OID();
-    my $filename = $doc_id ? "docsql-".$doc_id.".xml" : "docsql.xml";
-    return $filename;
-}
-   
+    return "docsql.xml";
+}
+
+# overriding to store doc OID as attribute of top level element: <Archive docoid="oid">
+sub output_xml_header {
+    my $self = shift (@_);
+    my ($outhandle, $doc_oid) = @_;
+
+    print $outhandle '<?xml version="1.0" encoding="utf-8" standalone="no"?>' . "\n";
+    print $outhandle "<!DOCTYPE Archive SYSTEM \"http://greenstone.org/dtd/Archive/1.0/Archive.dtd\">\n";
+    print $outhandle "<Archive docoid=\"$doc_oid\">\n";
+}
+ 
 # TODO: check arc-inf.db for whether each entry is to be deleted/indexed/reindexed/been indexed
 sub saveas {
Index: /main/trunk/greenstone2/perllib/plugouts/GreenstoneXMLPlugout.pm
===================================================================
--- /main/trunk/greenstone2/perllib/plugouts/GreenstoneXMLPlugout.pm	(revision 32541)
+++ /main/trunk/greenstone2/perllib/plugouts/GreenstoneXMLPlugout.pm	(revision 32542)
@@ -190,5 +190,5 @@
     # only output the header if we have started a new doc
     if (!$self->is_group() || $self->{'new_doc_dir'}) {
-	$self->output_xml_header($outhandler);
+	$self->output_xml_header($outhandler, $doc_obj->get_OID());
     }
  
@@ -238,5 +238,5 @@
 sub output_xml_header {
     my $self = shift (@_);
-    my ($outhandle) = @_;
+    my ($outhandle, $doc_oid) = @_;
 
     print $outhandle '<?xml version="1.0" encoding="utf-8" standalone="no"?>' . "\n";
