[LON-CAPA-cvs] cvs: loncom /publisher lonpublisher.pm

matthew lon-capa-cvs@mail.lon-capa.org
Fri, 09 Aug 2002 17:57:48 -0000


matthew		Fri Aug  9 13:57:48 2002 EDT

  Modified files:              
    /loncom/publisher	lonpublisher.pm 
  Log:
  Bug 561 - nonexistant metadata table produces a user-visible error now.
  Changed to use lonmysql to access the MySQL database and do the insertion/
  deletion of elements.
  Removed some old POD documentation.
  
  
Index: loncom/publisher/lonpublisher.pm
diff -u loncom/publisher/lonpublisher.pm:1.88 loncom/publisher/lonpublisher.pm:1.89
--- loncom/publisher/lonpublisher.pm:1.88	Wed Aug  7 15:59:06 2002
+++ loncom/publisher/lonpublisher.pm	Fri Aug  9 13:57:48 2002
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Publication Handler
 #
-# $Id: lonpublisher.pm,v 1.88 2002/08/07 19:59:06 albertel Exp $
+# $Id: lonpublisher.pm,v 1.89 2002/08/09 17:57:48 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -75,6 +75,7 @@
 use DBI;
 use Apache::lonnet();
 use Apache::loncommon();
+use Apache::lonmysql;
 
 my %addid;
 my %nokey;
@@ -467,6 +468,62 @@
     return ($outstring,%allow);
 }
 
+#########################################
+#########################################
+
+=pod
+
+=item store_metadata
+
+Store the metadata in the metadata table in the loncapa database.
+Uses lonmysql to access the database.
+
+Inputs: \%metadata
+
+Returns: (error,status).  error is undef on success, status is undef on error.
+
+=cut
+
+#########################################
+#########################################
+sub store_metadata {
+    my %metadata = %{shift()};
+    my $error;
+    # Determine if the table exists
+    my $status = &Apache::lonmysql::check_table('metadata');
+    if (! defined($status)) {
+        $error='<font color="red">WARNING: Cannot connect to '.
+            'database!</font>';
+        &Apache::lonnet::logthis($error);
+        return ($error,undef);
+    }
+    if ($status == 0) {
+        # It would be nice to actually create the table....
+        $error ='<font color="red">WARNING: The metadata table does not '.
+            'exist in the LON-CAPA database.</font>';
+        &Apache::lonnet::logthis($error);
+        return ($error,undef);
+    }
+    # Remove old value from table
+    $status = &Apache::lonmysql::remove_from_table
+        ('metadata','url',$metadata{'url'});
+    if (! defined($status)) {
+        $error = '<font color="red">Error when removing old values from '.
+            'metadata table in LON-CAPA database.</font>';
+        &Apache::lonnet::logthis($error);
+        return ($error,undef);
+    }
+    # Store data in table.
+    $status = &Apache::lonmysql::store_row('metadata',\%metadata);
+    if (! defined($status)) {
+        $error='<font color="red">Error occured storing new values in '.
+            'metadata table in LON-CAPA database</font>';
+        &Apache::lonnet::logthis($error);
+        return ($error,undef);
+    }
+    return (undef,$status);
+}
+
 sub publish {
 
     my ($source,$target,$style)=@_;
@@ -858,65 +915,23 @@
      }
 
 # -------------------------------- Synchronize entry with SQL metadata database
-  my $warning;
-
-  unless ($metadatafields{'copyright'} eq 'priv') {
-
-    my $dbh;
-    {
-	unless (
-		$dbh = DBI->connect("DBI:mysql:loncapa","www",
-    $Apache::lonnet::perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
-		) { 
-	    $warning='<font color=red>WARNING: Cannot connect to '.
-		'database!</font>';
-	}
-	else {
-	    my %sqldatafields;
-	    $sqldatafields{'url'}=$distarget;
-	    my $sth=$dbh->prepare(
-				  'delete from metadata where url like binary'.
-				  '"'.$sqldatafields{'url'}.'"');
-	    $sth->execute();
-	    foreach ('title','author','subject','keywords','notes','abstract',
-	     'mime','language','creationdate','lastrevisiondate','owner',
-	     'copyright') {
-		my $field=$metadatafields{$_}; $field=~s/\"/\'\'/g; 
-		$sqldatafields{$_}=$field;
-	    }
-	    
-	    $sth=$dbh->prepare('insert into metadata values ('.
-			       '"'.delete($sqldatafields{'title'}).'"'.','.
-			       '"'.delete($sqldatafields{'author'}).'"'.','.
-			       '"'.delete($sqldatafields{'subject'}).'"'.','.
-			       '"'.delete($sqldatafields{'url'}).'"'.','.
-			       '"'.delete($sqldatafields{'keywords'}).'"'.','.
-			       '"'.'current'.'"'.','.
-			       '"'.delete($sqldatafields{'notes'}).'"'.','.
-			       '"'.delete($sqldatafields{'abstract'}).'"'.','.
-			       '"'.delete($sqldatafields{'mime'}).'"'.','.
-			       '"'.delete($sqldatafields{'language'}).'"'.','.
-			       '"'.
-			       sqltime(delete($sqldatafields{'creationdate'}))
-			       .'"'.','.
-			       '"'.
-			       sqltime(delete(
-			       $sqldatafields{'lastrevisiondate'})).'"'.','.
-			       '"'.delete($sqldatafields{'owner'}).'"'.','.
-			       '"'.delete(
-			       $sqldatafields{'copyright'}).'"'.')');
-	    $sth->execute();
-	    $dbh->disconnect;
-	    $scrout.='<p>Synchronized SQL metadata database';
-	    print $logfile "\nSynchronized SQL metadata database";
-	}
+    my $warning;
+    $metadatafields{'url'} = $distarget;
+    $metadatafields{'version'} = 'current';
+    unless ($metadatafields{'copyright'} eq 'priv') {
+        my ($error,$success) = &store_metadata(\%metadatafields);
+        if (! $success) {
+            $scrout.='<p>Synchronized SQL metadata database';
+            print $logfile "\nSynchronized SQL metadata database";
+        } else {
+            $warning.=$error;
+            print $logfile "\n".$error;
+        }
+    } else {
+        $scrout.='<p>Private Publication - did not synchronize database';
+        print $logfile "\nPrivate: Did not synchronize data into ".
+            "SQL metadata database";
     }
-
-} else {
-    $scrout.='<p>Private Publication - did not synchronize database';
-    print $logfile "\nPrivate: Did not synchronize data into ".
-	"SQL metadata database";
-}
 # ----------------------------------------------------------- Copy old versions
    
 if (-e $target) {
@@ -1210,107 +1225,9 @@
 1;
 __END__
 
-=head1 NAME
-
-Apache::lonpublisher - Publication Handler
-
-=head1 SYNOPSIS
-
-Invoked by /etc/httpd/conf/srm.conf:
-
- <Location /adm/publish>
- PerlAccessHandler       Apache::lonacc
- SetHandler perl-script
- PerlHandler Apache::lonpublisher
- ErrorDocument     403 /adm/login
- ErrorDocument     404 /adm/notfound.html
- ErrorDocument     406 /adm/unauthorized.html
- ErrorDocument	  500 /adm/errorhandler
- </Location>
-
-=head1 INTRODUCTION
-
-This module publishes a file.  This involves gathering metadata,
-versioning the file, copying file from construction space to
-publication space, and copying metadata from construction space
-to publication space.
-
-This is part of the LearningOnline Network with CAPA project
-described at http://www.lon-capa.org.
-
-=head1 HANDLER SUBROUTINE
-
-This routine is called by Apache and mod_perl.
-
-=over 4
-
-=item *
-
-Get query string for limited number of parameters
-
-=item *
-
-Check filename
-
-=item *
-
-File is there and owned, init lookup tables
-
-=item *
-
-Start page output
-
-=item *
-
-Individual file
-
-=item *
-
-publish from $thisfn to $thistarget with $thisembstyle
-
-=back
-
-=head1 OTHER SUBROUTINES
-
-=over 4
-
-=item *
-
-metaeval() : Evaluate string with metadata
-
-=item *
-
-metaread() : Read a metadata file
-
-=item *
-
-sqltime() : convert 'time' format into a datetime sql format
-
-=item *
-
-textfield() : form field
-
-=item *
-
-hiddenfield() : form field
-
-=item *
-
-selectbox() : form field
-
-=item *
-
-urlfixup() : fixup URL (Publication Step One)
-
-=item *
-
-publish() : publish (Publication Step One)
-
-=item *
-
-phasetwo() : render second interface showing status of publication steps
-(Publication Step Two)
+=pod
 
 =back
 
 =cut
+