Diff for /loncom/publisher/lonpublisher.pm between versions 1.97 and 1.101

version 1.97, 2002/09/18 15:43:06 version 1.101, 2002/10/08 13:24:36
Line 309  sub sqltime { Line 309  sub sqltime {
 #########################################  #########################################
 #########################################  #########################################
   
   sub coursedependencies {
       my $url=&Apache::lonnet::declutter(shift);
       $url=~s/\.meta$//;
       my ($adomain,$aauthor)=($url=~/^(\w+)\/(\w+)\//);
       my $regexp=$url;
       $regexp=~s/(\W)/\\$1/g;
       $regexp='___'.$regexp.'___course';
       my %evaldata=&Apache::lonnet::dump('nohist_resevaldata',$adomain,
          $aauthor,$regexp);
       my %courses=();
       foreach (keys %evaldata) {
    if ($_=~/^([a-zA-Z0-9]+_[a-zA-Z0-9]+)___.+___course$/) {
       $courses{$1}=1;
           }
       }
       return %courses;
   }
   #########################################
   #########################################
   
   
 =pod  =pod
   
 =item Form-field-generating subroutines.  =item Form-field-generating subroutines.
Line 469  sub get_subscribed_hosts { Line 490  sub get_subscribed_hosts {
     while ($filename=readdir(DIR)) {      while ($filename=readdir(DIR)) {
  if ($filename=~/$srcf\.(\w+)$/) {   if ($filename=~/$srcf\.(\w+)$/) {
     my $subhost=$1;      my $subhost=$1;
     if ($subhost ne 'meta' && $subhost ne 'subscription') {      if (($subhost ne 'meta' && $subhost ne 'subscription') &&
                   ($subhost ne $Apache::lonnet::perlvar{'lonHostID'})) {
  push(@subscribed,$subhost);   push(@subscribed,$subhost);
     }      }
  }   }
Line 480  sub get_subscribed_hosts { Line 502  sub get_subscribed_hosts {
  &Apache::lonnet::logthis("opened $target.subscription");   &Apache::lonnet::logthis("opened $target.subscription");
  while (my $subline=<$sh>) {   while (my $subline=<$sh>) {
     &Apache::lonnet::logthis("Trying $subline");      &Apache::lonnet::logthis("Trying $subline");
     if ($subline =~ /(^\w+):/) { push(@subscribed,$1); } else {      if ($subline =~ /(^\w+):/) { 
                   if ($1 ne $Apache::lonnet::perlvar{'lonHostID'}) { 
                      push(@subscribed,$1);
           }
               } else {
  &Apache::lonnet::logthis("No Match for $subline");   &Apache::lonnet::logthis("No Match for $subline");
     }      }
  }   }
     } else {      } else {
  &Apache::lonnet::logthis("Unable to open $target.subscription");   &Apache::lonnet::logthis("Unable to open $target.subscription");
     }      }
     &Apache::lonnet::logthis("Got list of ".join(':',@subscribed));  
     return @subscribed;      return @subscribed;
 }  }
   
Line 1191  Returns: Line 1216  Returns:
 =item Scalar string  =item Scalar string
   
 String contains status (errors and warnings) and information associated with  String contains status (errors and warnings) and information associated with
 the server's attempts at publication.  the server's attempts at publication.     
   
 =cut  =cut
   
   #'stupid emacs
 #########################################  #########################################
 #########################################  #########################################
 sub phasetwo {  sub phasetwo {
   
     my ($source,$target,$style,$distarget,$batch)=@_;      my ($r,$source,$target,$style,$distarget,$batch)=@_;
     my $logfile;      my $logfile;
     my $scrout='';  
     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {      unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
  return    return 
          '<font color=red>No write permission to user directory, FAIL</font>';              '<font color=red>No write permission to user directory, FAIL</font>';
     }      }
     print $logfile       print $logfile 
 "\n================= Publish ".localtime()." Phase Two  ================\n";          "\n================= Publish ".localtime()." Phase Two  ================\n";
       
      %metadatafields=();      %metadatafields=();
      %metadatakeys=();      %metadatakeys=();
       
      &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));      &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
       
      $metadatafields{'title'}=$ENV{'form.title'};      $metadatafields{'title'}=$ENV{'form.title'};
      $metadatafields{'author'}=$ENV{'form.author'};      $metadatafields{'author'}=$ENV{'form.author'};
      $metadatafields{'subject'}=$ENV{'form.subject'};      $metadatafields{'subject'}=$ENV{'form.subject'};
      $metadatafields{'notes'}=$ENV{'form.notes'};      $metadatafields{'notes'}=$ENV{'form.notes'};
      $metadatafields{'abstract'}=$ENV{'form.abstract'};      $metadatafields{'abstract'}=$ENV{'form.abstract'};
      $metadatafields{'mime'}=$ENV{'form.mime'};      $metadatafields{'mime'}=$ENV{'form.mime'};
      $metadatafields{'language'}=$ENV{'form.language'};      $metadatafields{'language'}=$ENV{'form.language'};
      $metadatafields{'creationdate'}=      $metadatafields{'creationdate'}=
          &sqltime($ENV{'form.creationdate'});          &sqltime($ENV{'form.creationdate'});
      $metadatafields{'lastrevisiondate'}=      $metadatafields{'lastrevisiondate'}=
          &sqltime($ENV{'form.lastrevisiondate'});          &sqltime($ENV{'form.lastrevisiondate'});
      $metadatafields{'owner'}=$ENV{'form.owner'};      $metadatafields{'owner'}=$ENV{'form.owner'};
      $metadatafields{'copyright'}=$ENV{'form.copyright'};      $metadatafields{'copyright'}=$ENV{'form.copyright'};
      $metadatafields{'dependencies'}=$ENV{'form.dependencies'};      $metadatafields{'dependencies'}=$ENV{'form.dependencies'};
       
      my $allkeywords=$ENV{'form.addkey'};      my $allkeywords=$ENV{'form.addkey'};
      if (exists($ENV{'form.keywords'})) {      if (exists($ENV{'form.keywords'})) {
          if (ref($ENV{'form.keywords'})) {          if (ref($ENV{'form.keywords'})) {
              $allkeywords .= ','.join(',',@{$ENV{'form.keywords'}});              $allkeywords .= ','.join(',',@{$ENV{'form.keywords'}});
          } else {          } else {
              $allkeywords .= ','.$ENV{'form.keywords'};              $allkeywords .= ','.$ENV{'form.keywords'};
          }          }
      }      }
      $allkeywords=~s/\W+/\,/;      $allkeywords=~s/\W+/\,/;
      $allkeywords=~s/^\,//;      $allkeywords=~s/^\,//;
      $metadatafields{'keywords'}=$allkeywords;      $metadatafields{'keywords'}=$allkeywords;
        
      {      {
        print $logfile "\nWrite metadata file for ".$source;          print $logfile "\nWrite metadata file for ".$source;
        my $mfh;          my $mfh;
        unless ($mfh=Apache::File->new('>'.$source.'.meta')) {          unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
  return               return 
          '<font color=red>Could not write metadata, FAIL</font>';                  '<font color=red>Could not write metadata, FAIL</font>';
        }          }
        foreach (sort keys %metadatafields) {          foreach (sort keys %metadatafields) {
  unless ($_=~/\./) {              unless ($_=~/\./) {
            my $unikey=$_;                  my $unikey=$_;
            $unikey=~/^([A-Za-z]+)/;                  $unikey=~/^([A-Za-z]+)/;
            my $tag=$1;                  my $tag=$1;
            $tag=~tr/A-Z/a-z/;                  $tag=~tr/A-Z/a-z/;
            print $mfh "\n\<$tag";                  print $mfh "\n\<$tag";
            foreach (split(/\,/,$metadatakeys{$unikey})) {                  foreach (split(/\,/,$metadatakeys{$unikey})) {
                my $value=$metadatafields{$unikey.'.'.$_};                      my $value=$metadatafields{$unikey.'.'.$_};
                $value=~s/\"/\'\'/g;                      $value=~s/\"/\'\'/g;
                print $mfh ' '.$_.'="'.$value.'"';                      print $mfh ' '.$_.'="'.$value.'"';
            }                  }
    print $mfh '>'.                  print $mfh '>'.
      &HTML::Entities::encode($metadatafields{$unikey})                      &HTML::Entities::encode($metadatafields{$unikey})
        .'</'.$tag.'>';                          .'</'.$tag.'>';
          }              }
        }          }
        $scrout.='<p>Wrote Metadata';          $r->print('<p>Wrote Metadata');
        print $logfile "\nWrote metadata";          print $logfile "\nWrote metadata";
      }      }
       
 # -------------------------------- Synchronize entry with SQL metadata database  # -------------------------------- Synchronize entry with SQL metadata database
     my $warning;  
     $metadatafields{'url'} = $distarget;      $metadatafields{'url'} = $distarget;
     $metadatafields{'version'} = 'current';      $metadatafields{'version'} = 'current';
     unless ($metadatafields{'copyright'} eq 'priv') {      unless ($metadatafields{'copyright'} eq 'priv') {
         my ($error,$success) = &store_metadata(\%metadatafields);          my ($error,$success) = &store_metadata(\%metadatafields);
         if ($success) {          if ($success) {
             $scrout.='<p>Synchronized SQL metadata database';              $r->print('<p>Synchronized SQL metadata database');
             print $logfile "\nSynchronized SQL metadata database";              print $logfile "\nSynchronized SQL metadata database";
         } else {          } else {
             $warning.=$error;              $r->print($error);
             print $logfile "\n".$error;              print $logfile "\n".$error;
         }          }
     } else {      } else {
         $scrout.='<p>Private Publication - did not synchronize database';          $r->print('<p>Private Publication - did not synchronize database');
         print $logfile "\nPrivate: Did not synchronize data into ".          print $logfile "\nPrivate: Did not synchronize data into ".
             "SQL metadata database";              "SQL metadata database";
     }      }
 # ----------------------------------------------------------- Copy old versions  # ----------------------------------------------------------- Copy old versions
         
 if (-e $target) {      if (-e $target) {
     my $filename;          my $filename;
     my $maxversion=0;          my $maxversion=0;
     $target=~/(.*)\/([^\/]+)\.(\w+)$/;          $target=~/(.*)\/([^\/]+)\.(\w+)$/;
     my $srcf=$2;          my $srcf=$2;
     my $srct=$3;          my $srct=$3;
     my $srcd=$1;          my $srcd=$1;
     unless ($srcd=~/^\/home\/httpd\/html\/res/) {          unless ($srcd=~/^\/home\/httpd\/html\/res/) {
  print $logfile "\nPANIC: Target dir is ".$srcd;              print $logfile "\nPANIC: Target dir is ".$srcd;
         return "<font color=red>Invalid target directory, FAIL</font>";              return "<font color=red>Invalid target directory, FAIL</font>";
     }          }
     opendir(DIR,$srcd);          opendir(DIR,$srcd);
     while ($filename=readdir(DIR)) {          while ($filename=readdir(DIR)) {
        if ($filename=~/$srcf\.(\d+)\.$srct$/) {              if (-l $srcd.'/'.$filename) {
    $maxversion=($1>$maxversion)?$1:$maxversion;                  unlink($srcd.'/'.$filename);
        }                  unlink($srcd.'/'.$filename.'.meta');
     }              } else {
     closedir(DIR);                  if ($filename=~/$srcf\.(\d+)\.$srct$/) {
     $maxversion++;                      $maxversion=($1>$maxversion)?$1:$maxversion;
     $scrout.='<p>Creating old version '.$maxversion;                  }
     print $logfile "\nCreating old version ".$maxversion;              }
           }
     my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;          closedir(DIR);
           $maxversion++;
           $r->print('<p>Creating old version '.$maxversion);
           print $logfile "\nCreating old version ".$maxversion;
           
           my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
           
         if (copy($target,$copyfile)) {          if (copy($target,$copyfile)) {
     print $logfile "Copied old target to ".$copyfile."\n";      print $logfile "Copied old target to ".$copyfile."\n";
             $scrout.='<p>Copied old target file';              $r->print('<p>Copied old target file');
         } else {          } else {
     print $logfile "Unable to write ".$copyfile.':'.$!."\n";      print $logfile "Unable to write ".$copyfile.':'.$!."\n";
            return "<font color=red>Failed to copy old target, $!, FAIL</font>";              return "<font color=red>Failed to copy old target, $!, FAIL</font>";
         }          }
           
 # --------------------------------------------------------------- Copy Metadata  # --------------------------------------------------------------- Copy Metadata
   
  $copyfile=$copyfile.'.meta';   $copyfile=$copyfile.'.meta';
           
         if (copy($target.'.meta',$copyfile)) {          if (copy($target.'.meta',$copyfile)) {
     print $logfile "Copied old target metadata to ".$copyfile."\n";      print $logfile "Copied old target metadata to ".$copyfile."\n";
             $scrout.='<p>Copied old metadata';              $r->print('<p>Copied old metadata')
         } else {          } else {
     print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";      print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
             if (-e $target.'.meta') {              if (-e $target.'.meta') {
                return                   return 
        "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";                      "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
     }      }
         }          }
           
           
 } else {      } else {
     $scrout.='<p>Initial version';          $r->print('<p>Initial version');
     print $logfile "\nInitial version";          print $logfile "\nInitial version";
 }      }
   
 # ---------------------------------------------------------------- Write Source  # ---------------------------------------------------------------- Write Source
  my $copyfile=$target;      my $copyfile=$target;
       
            my @parts=split(/\//,$copyfile);      my @parts=split(/\//,$copyfile);
            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";      my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
       
            my $count;      my $count;
            for ($count=5;$count<$#parts;$count++) {      for ($count=5;$count<$#parts;$count++) {
                $path.="/$parts[$count]";          $path.="/$parts[$count]";
                if ((-e $path)!=1) {          if ((-e $path)!=1) {
                    print $logfile "\nCreating directory ".$path;              print $logfile "\nCreating directory ".$path;
                    $scrout.='<p>Created directory '.$parts[$count];              $r->print('<p>Created directory '.$parts[$count]);
    mkdir($path,0777);              mkdir($path,0777);
                }  
            }  
   
         if (copy($source,$copyfile)) {  
     print $logfile "\nCopied original source to ".$copyfile."\n";  
             $scrout.='<p>Copied source file';  
         } else {  
     print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";  
             return "<font color=red>Failed to copy source, $!, FAIL</font>";  
         }          }
       }
       
       if (copy($source,$copyfile)) {
           print $logfile "\nCopied original source to ".$copyfile."\n";
           $r->print('<p>Copied source file');
       } else {
           print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
           return "<font color=red>Failed to copy source, $!, FAIL</font>";
       }
       
 # --------------------------------------------------------------- Copy Metadata  # --------------------------------------------------------------- Copy Metadata
   
         $copyfile=$copyfile.'.meta';      $copyfile=$copyfile.'.meta';
       
         if (copy($source.'.meta',$copyfile)) {      if (copy($source.'.meta',$copyfile)) {
     print $logfile "\nCopied original metadata to ".$copyfile."\n";          print $logfile "\nCopied original metadata to ".$copyfile."\n";
             $scrout.='<p>Copied metadata';          $r->print('<p>Copied metadata');
         } else {      } else {
     print $logfile "\nUnable to write metadata ".$copyfile.':'.$!."\n";          print $logfile "\nUnable to write metadata ".$copyfile.':'.$!."\n";
             return           return 
           "<font color=red>Failed to write metadata copy, $!, FAIL</font>";              "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
         }      }
       $r->rflush;
 # --------------------------------------------------- Send update notifications  # --------------------------------------------------- Send update notifications
   
     my @subscribed=&get_subscribed_hosts($target);      my @subscribed=&get_subscribed_hosts($target);
     foreach my $subhost (@subscribed) {      foreach my $subhost (@subscribed) {
  $scrout.='<p>Notifying host '.$subhost.':';   $r->print('<p>Notifying host '.$subhost.':');$r->rflush;
  print $logfile "\nNotifying host ".$subhost.':';   print $logfile "\nNotifying host ".$subhost.':';
  my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);   my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
  $scrout.=$reply;   $r->print($reply.'<br />');$r->rflush;
  print $logfile $reply;   print $logfile $reply;
     }      }
       
 # ---------------------------------------- Send update notifications, meta only  # ---------------------------------------- Send update notifications, meta only
   
     my @subscribedmeta=&get_subscribed_hosts("$target.meta");      my @subscribedmeta=&get_subscribed_hosts("$target.meta");
     foreach my $subhost (@subscribedmeta) {      foreach my $subhost (@subscribedmeta) {
  $scrout.='<p>Notifying host for metadata only '.$subhost.':';   $r->print('<p>Notifying host for metadata only '.$subhost.':');$r->rflush;
  print $logfile "\nNotifying host for metadata only ".$subhost.':';   print $logfile "\nNotifying host for metadata only ".$subhost.':';
  my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',   my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
     $subhost);      $subhost);
  $scrout.=$reply;   $r->print($reply.'<br />');$r->rflush;
    print $logfile $reply;
       }
       
   # --------------------------------------------------- Notify subscribed courses
       my %courses=&coursedependencies($target);
       my $now=time;
       foreach (keys %courses) {
    $r->print('<p>Notifying course '.$_.':');$r->rflush;
    print $logfile "\nNotifying host ".$_.':';
           my ($cdom,$cname)=split(/\_/,$_);
    my $reply=&Apache::lonnet::cput
                     ('versionupdate',{$target => $now},$cdom,$cname);
    $r->print($reply.'<br />');$r->rflush;
  print $logfile $reply;   print $logfile $reply;
     }      }
   
 # ------------------------------------------------ Provide link to new resource  # ------------------------------------------------ Provide link to new resource
   unless ($batch) {      unless ($batch) {
     my $thisdistarget=$target;          my $thisdistarget=$target;
     $thisdistarget=~s/^$docroot//;          $thisdistarget=~s/^$docroot//;
           
     my $thissrc=$source;          my $thissrc=$source;
     $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;          $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
           
     my $thissrcdir=$thissrc;          my $thissrcdir=$thissrc;
     $thissrcdir=~s/\/[^\/]+$/\//;          $thissrcdir=~s/\/[^\/]+$/\//;
           
           
     return $warning.$scrout.          $r->print(
       '<hr><a href="'.$thisdistarget.'"><font size="+2">'.             '<hr><a href="'.$thisdistarget.'"><font size="+2">'.
       'View Published Version</font></a>'.             'View Published Version</font></a>'.
       '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.             '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
       '<p><a href="'.$thissrcdir.             '<p><a href="'.$thissrcdir.
       '"><font size="+2">Back to Source Directory</font></a>';                     '"><font size="+2">Back to Source Directory</font></a>');
   } else {      }
     return $warning.$scrout;  
   }  
 }  }
   
 #########################################  #########################################
Line 1456  sub batchpublish { Line 1496  sub batchpublish {
 # phase two takes  # phase two takes
 # my ($source,$target,$style,$distarget,batch)=@_;  # my ($source,$target,$style,$distarget,batch)=@_;
 # $ENV{'form.allmeta'},$ENV{'form.title'},$ENV{'form.author'},...  # $ENV{'form.allmeta'},$ENV{'form.title'},$ENV{'form.author'},...
     $r->print(      $r->print('<p>');
 '<p>'.&phasetwo($srcfile,$targetfile,$thisembstyle,$thisdistarget,1).'</p>');      &phasetwo($r,$srcfile,$targetfile,$thisembstyle,$thisdistarget,1);
       $r->print('</p>');
     return '';      return '';
 }  }
   
Line 1466  sub batchpublish { Line 1507  sub batchpublish {
 sub publishdirectory {  sub publishdirectory {
     my ($r,$fn,$thisdisfn)=@_;      my ($r,$fn,$thisdisfn)=@_;
     my $resdir=      my $resdir=
     $Apache::lonnet::perlvar{'lonDocRoot'}.'/res/'.$cudom.'/'.$cuname.      $Apache::lonnet::perlvar{'lonDocRoot'}.'/res/'.$cudom.'/'.$cuname.'/'.
       $thisdisfn;        $thisdisfn;
       $r->print('<h1>Directory <tt>'.$thisdisfn.'/</tt></h1>'.        $r->print('<h1>Directory <tt>'.$thisdisfn.'</tt></h1>'.
                 'Target: <tt>'.$resdir.'</tt><br />');                  'Target: <tt>'.$resdir.'</tt><br />');
   
       my $dirptr=16384; # Mask indicating a directory in stat.cmode.        my $dirptr=16384; # Mask indicating a directory in stat.cmode.
Line 1656  unless ($ENV{'form.phase'} eq 'two') { Line 1697  unless ($ENV{'form.phase'} eq 'two') {
   
   $r->print('<html><head><title>LON-CAPA Publishing</title></head>');    $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
   $r->print(&Apache::loncommon::bodytag('Resource Publication'));    $r->print(&Apache::loncommon::bodytag('Resource Publication'));
   
   
   my $thisfn=$fn;    my $thisfn=$fn;
   
   my $thistarget=$thisfn;    my $thistarget=$thisfn;
Line 1701  unless ($ENV{'form.phase'} eq 'two') { Line 1744  unless ($ENV{'form.phase'} eq 'two') {
          $r->print(           $r->print(
           '<hr />'.&publish($thisfn,$thistarget,$thisembstyle));            '<hr />'.&publish($thisfn,$thistarget,$thisembstyle));
        } else {         } else {
          $r->print(             $r->print('<hr />');
           '<hr />'.&phasetwo($thisfn,$thistarget,             &phasetwo($r,$thisfn,$thistarget,$thisembstyle,$thisdistarget); 
      $thisembstyle,$thisdistarget));   
        }           }  
   
   }    }

Removed from v.1.97  
changed lines
  Added in v.1.101


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>