Diff for /loncom/publisher/lonpublisher.pm between versions 1.81 and 1.87

version 1.81, 2002/05/17 22:08:01 version 1.87, 2002/08/07 19:50:22
Line 235  sub set_allow { Line 235  sub set_allow {
     return $return_url      return $return_url
 }  }
   
   sub get_subscribed_hosts {
       my ($target)=@_;
       my @subscribed;
       my $filename;
       $target=~/(.*)\/([^\/]+)$/;
       my $srcf=$2;
       opendir(DIR,$1);
       while ($filename=readdir(DIR)) {
    if ($filename=~/$srcf\.(\w+)$/) {
       my $subhost=$1;
       if ($subhost ne 'meta' && $subhost ne 'subscription') {
    push(@subscribed,$subhost);
       }
    }
       }
       closedir(DIR);
       my $sh;
       if ( $sh=Apache::File->new("$target.subscription") ) {
    &Apache::lonnet::logthis("opened $target.subscription");
    while (my $subline=<$sh>) {
       &Apache::lonnet::logthis("Trying $subline");
       if ($subline =~ /(^\w+):/) { push(@subscribed,$1); } else {
    &Apache::lonnet::logthis("No Match for $subline");
       }
    }
       } else {
    &Apache::lonnet::logthis("Un able to open $target.subscription");
       }
       &Apache::lonnet::logthis("Got list of ".join(':',@subscribed));
       return @subscribed;
   }
   
   
   sub get_max_ids_indices {
       my ($content)=@_;
       my $maxindex=10;
       my $maxid=10;
       my $needsfixup=0;
   
       my $parser=HTML::LCParser->new($content);
       my $token;
       while ($token=$parser->get_token) {
    if ($token->[0] eq 'S') {
       my $counter;
       if ($counter=$addid{$token->[1]}) {
    if ($counter eq 'id') {
       if (defined($token->[2]->{'id'})) {
    $maxid=($token->[2]->{'id'}>$maxid)?$token->[2]->{'id'}:$maxid;
       } else {
    $needsfixup=1;
       }
    } else {
       if (defined($token->[2]->{'index'})) {
    $maxindex=($token->[2]->{'index'}>$maxindex)?$token->[2]->{'index'}:$maxindex;
       } else {
    $needsfixup=1;
       }
    }
       }
    }
       }
       return ($needsfixup,$maxid,$maxindex);
   }
   
   sub get_all_text_unbalanced {
       #there is a copy of this in lonxml.pm
       my($tag,$pars)= @_;
       my $token;
       my $result='';
       $tag='<'.$tag.'>';
       while ($token = $$pars[-1]->get_token) {
    if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
       $result.=$token->[1];
    } elsif ($token->[0] eq 'PI') {
       $result.=$token->[2];
    } elsif ($token->[0] eq 'S') {
       $result.=$token->[4];
    } elsif ($token->[0] eq 'E')  {
       $result.=$token->[2];
    }
    if ($result =~ /(.*)$tag(.*)/) {
       &Apache::lonnet::logthis('Got a winner with leftovers ::'.$2);
       &Apache::lonnet::logthis('Result is :'.$1);
       $result=$1;
       my $redo=$tag.$2;
       push (@$pars,HTML::LCParser->new(\$redo));
       $$pars[-1]->xml_mode('1');
       last;
    }
       }
       return $result
   }
   
   #Arguably this should all be done as a lonnet::ssi instead
   sub fix_ids_and_indices {
       my ($logfile,$source,$target)=@_;
   
       my %allow;
       my $content;
       {
    my $org=Apache::File->new($source);
    $content=join('',<$org>);
       }
   
       my ($needsfixup,$maxid,$maxindex)=&get_max_ids_indices(\$content);
   
       if ($needsfixup) {
    print $logfile "Needs ID and/or index fixup\n".
       "Max ID   : $maxid (min 10)\n".
                   "Max Index: $maxindex (min 10)\n";
       }
       my $outstring='';
       my @parser;
       $parser[0]=HTML::LCParser->new(\$content);
       $parser[-1]->xml_mode(1);
       my $token;
       while (@parser) {
    while ($token=$parser[-1]->get_token) {
       if ($token->[0] eq 'S') {
    my $counter;
    my $tag=$token->[1];
    my $lctag=lc($tag);
    if ($lctag eq 'allow') {
       $allow{$token->[2]->{'src'}}=1;
       next;
    }
    my %parms=%{$token->[2]};
    $counter=$addid{$tag};
    if (!$counter) { $counter=$addid{$lctag}; }
    if ($counter) {
       if ($counter eq 'id') {
    unless (defined($parms{'id'})) {
       $maxid++;
       $parms{'id'}=$maxid;
       print $logfile 'ID: '.$tag.':'.$maxid."\n";
    }
       } elsif ($counter eq 'index') {
    unless (defined($parms{'index'})) {
       $maxindex++;
       $parms{'index'}=$maxindex;
       print $logfile 'Index: '.$tag.':'.$maxindex."\n";
    }
       }
    }
    foreach my $type ('src','href','background','bgimg') {
       foreach my $key (keys(%parms)) {
    print $logfile "for $type, and $key\n";
    if ($key =~ /^$type$/i) {
       print $logfile "calling set_allow\n";
       $parms{$key}=&set_allow(\%allow,$logfile,
       $target,$tag,
       $parms{$key});
    }
       }
    }
    # probably a <randomlabel> image type <label>
    if ($lctag eq 'label' && defined($parms{'description'})) {
       my $next_token=$parser[-1]->get_token();
       if ($next_token->[0] eq 'T') {
    $next_token->[1]=&set_allow(\%allow,$logfile,
       $target,$tag,
       $next_token->[1]);
       }
       $parser[-1]->unget_token($next_token);
    }
    if ($lctag eq 'applet') {
       my $codebase='';
       if (defined($parms{'codebase'})) {
    my $oldcodebase=$parms{'codebase'};
    unless ($oldcodebase=~/\/$/) {
       $oldcodebase.='/';
    }
    $codebase=&urlfixup($oldcodebase,$target);
    $codebase=~s/\/$//;    
    if ($codebase ne $oldcodebase) {
       $parms{'codebase'}=$codebase;
       print $logfile 'URL codebase: '.$tag.':'.
    $oldcodebase.' - '.
       $codebase."\n";
    }
    $allow{&absoluteurl($codebase,$target).'/*'}=1;
       } else {
    foreach ('archive','code','object') {
       if (defined($parms{$_})) {
    my $oldurl=$parms{$_};
    my $newurl=&urlfixup($oldurl,$target);
    $newurl=~s/\/[^\/]+$/\/\*/;
    print $logfile 'Allow: applet '.$_.':'.
       $oldurl.' allows '.
    $newurl."\n";
    $allow{&absoluteurl($newurl,$target)}=1;
       }
    }
       }
    }
    my $newparmstring='';
    my $endtag='';
    foreach (keys %parms) {
       if ($_ eq '/') {
    $endtag=' /';
       } else { 
    my $quote=($parms{$_}=~/\"/?"'":'"');
    $newparmstring.=' '.$_.'='.$quote.$parms{$_}.$quote;
       }
    }
    if (!$endtag) { if ($token->[4]=~m:/>$:) { $endtag=' /'; }; }
    $outstring.='<'.$tag.$newparmstring.$endtag.'>';
    if ($lctag eq 'm') {
       $outstring.=&get_all_text_unbalanced('/m',\@parser);
    }
       } elsif ($token->[0] eq 'E') {
    if ($token->[2]) {
       unless ($token->[1] eq 'allow') {
    $outstring.='</'.$token->[1].'>';
       }
    }
       } else {
    $outstring.=$token->[1];
       }
    }
    pop(@parser);
       }
   
       if ($needsfixup) {
    print $logfile "End of ID and/or index fixup\n".
       "Max ID   : $maxid (min 10)\n".
    "Max Index: $maxindex (min 10)\n";
       } else {
    print $logfile "Does not need ID and/or index fixup\n";
       }
   
       return ($outstring,%allow);
   }
   
 sub publish {  sub publish {
   
     my ($source,$target,$style)=@_;      my ($source,$target,$style)=@_;
Line 243  sub publish { Line 477  sub publish {
     my $allmeta='';      my $allmeta='';
     my $content='';      my $content='';
     my %allow=();      my %allow=();
     undef %allow;  
   
     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {      unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
  return    return 
Line 264  sub publish { Line 497  sub publish {
           return "<font color=red>Failed to write backup copy, $!,FAIL</font>";            return "<font color=red>Failed to write backup copy, $!,FAIL</font>";
         }          }
 # ------------------------------------------------------------- IDs and indices  # ------------------------------------------------------------- IDs and indices
   
         my $maxindex=10;   my $outstring;
         my $maxid=10;   ($outstring,%allow)=&fix_ids_and_indices($logfile,$source,$target);
   
         my $needsfixup=0;  
   
         {  
           my $org=Apache::File->new($source);  
           $content=join('',<$org>);  
         }  
         {  
           my $parser=HTML::LCParser->new(\$content);  
           my $token;  
           while ($token=$parser->get_token) {  
               if ($token->[0] eq 'S') {  
                   my $counter;  
   if ($counter=$addid{$token->[1]}) {  
       if ($counter eq 'id') {  
   if (defined($token->[2]->{'id'})) {  
                              $maxid=  
        ($token->[2]->{'id'}>$maxid)?$token->[2]->{'id'}:$maxid;  
  } else {  
                              $needsfixup=1;  
                          }  
                       } else {  
    if (defined($token->[2]->{'index'})) {  
                              $maxindex=  
    ($token->[2]->{'index'}>$maxindex)?$token->[2]->{'index'}:$maxindex;  
   } else {  
                              $needsfixup=1;  
   }  
       }  
   }  
               }  
           }  
       }  
       if ($needsfixup) {  
           print $logfile "Needs ID and/or index fixup\n".  
         "Max ID   : $maxid (min 10)\n".  
                 "Max Index: $maxindex (min 10)\n";  
       }  
           my $outstring='';  
           my $parser=HTML::LCParser->new(\$content);  
           $parser->xml_mode(1);  
           my $token;  
           while ($token=$parser->get_token) {  
               if ($token->[0] eq 'S') {  
                 my $counter;  
                 my $tag=$token->[1];  
                 my $lctag=lc($tag);  
                 unless ($lctag eq 'allow') {    
                   my %parms=%{$token->[2]};  
                   $counter=$addid{$tag};  
                   if (!$counter) { $counter=$addid{$lctag}; }  
                   if ($counter) {  
       if ($counter eq 'id') {  
   unless (defined($parms{'id'})) {  
                               $maxid++;  
                               $parms{'id'}=$maxid;  
                               print $logfile 'ID: '.$tag.':'.$maxid."\n";  
                           }  
                       } elsif ($counter eq 'index') {  
    unless (defined($parms{'index'})) {  
                               $maxindex++;  
                               $parms{'index'}=$maxindex;  
                               print $logfile 'Index: '.$tag.':'.$maxindex."\n";  
   }  
       }  
   }  
   
                   foreach my $type ('src','href','background','bgimg') {  
       foreach my $key (keys(%parms)) {  
   print $logfile "for $type, and $key\n";  
   if ($key =~ /^$type$/i) {  
       print $logfile "calling set_allow\n";  
       $parms{$key}=&set_allow(\%allow,$logfile,  
       $target,$tag,  
       $parms{$key});  
   }  
       }  
                   }  
   # probably a <randomlabel> image type <label>  
   if ($lctag eq 'label' && defined($parms{'description'})) {  
       my $next_token=$parser->get_token();  
       if ($next_token->[0] eq 'T') {  
   $next_token->[1]=&set_allow(\%allow,$logfile,  
       $target,$tag,  
       $next_token->[1]);  
       }  
       $parser->unget_token($next_token);  
   }  
                   if ($lctag eq 'applet') {  
       my $codebase='';  
                       if (defined($parms{'codebase'})) {  
          my $oldcodebase=$parms{'codebase'};  
                          unless ($oldcodebase=~/\/$/) {  
                             $oldcodebase.='/';  
                          }  
                          $codebase=&urlfixup($oldcodebase,$target);  
                          $codebase=~s/\/$//;      
                          if ($codebase ne $oldcodebase) {  
      $parms{'codebase'}=$codebase;  
                              print $logfile 'URL codebase: '.$tag.':'.  
                                   $oldcodebase.' - '.  
   $codebase."\n";  
  }  
                          $allow{&absoluteurl($codebase,$target).'/*'}=1;  
       } else {  
                         foreach ('archive','code','object') {  
                           if (defined($parms{$_})) {  
       my $oldurl=$parms{$_};  
                               my $newurl=&urlfixup($oldurl,$target);  
       $newurl=~s/\/[^\/]+$/\/\*/;  
                                   print $logfile 'Allow: applet '.$_.':'.  
                                   $oldurl.' allows '.  
   $newurl."\n";  
                               $allow{&absoluteurl($newurl,$target)}=1;  
                           }  
                         }  
                       }  
                   }  
   
                   my $newparmstring='';  
                   my $endtag='';  
                   foreach (keys %parms) {  
                     if ($_ eq '/') {  
                       $endtag=' /';  
                     } else {   
                       my $quote=($parms{$_}=~/\"/?"'":'"');  
                       $newparmstring.=' '.$_.'='.$quote.$parms{$_}.$quote;  
     }  
                   }  
   if (!$endtag) { if ($token->[4]=~m:/>$:) { $endtag=' /'; }; }  
   $outstring.='<'.$tag.$newparmstring.$endtag.'>';  
          } else {  
    $allow{$token->[2]->{'src'}}=1;  
  }  
               } elsif ($token->[0] eq 'E') {  
  if ($token->[2]) {  
                   unless ($token->[1] eq 'allow') {  
                      $outstring.='</'.$token->[1].'>';  
   }  
  }  
               } else {  
                   $outstring.=$token->[1];  
               }  
           }  
 # ------------------------------------------------------------ Construct Allows  # ------------------------------------------------------------ Construct Allows
           
  $scrout.='<h3>Dependencies</h3>';   $scrout.='<h3>Dependencies</h3>';
Line 444  sub publish { Line 533  sub publish {
        }         }
            }             }
         }          }
         $allowstr=~s/\n+/\n/g;          $outstring=~s/\n*(\<\/[^\>]+\>)\s*$/$allowstr\n$1\n/s;
         $outstring=~s/(\<\/[^\>]+\>\s*)$/$allowstr$1/s;  
   
  #Encode any High ASCII characters   #Encode any High ASCII characters
  $outstring=&HTML::Entities::encode($outstring,"\200-\377");   $outstring=&HTML::Entities::encode($outstring,"\200-\377");
Line 462  sub publish { Line 550  sub publish {
         }          }
   $content=$outstring;    $content=$outstring;
   
       if ($needsfixup) {  
           print $logfile "End of ID and/or index fixup\n".  
         "Max ID   : $maxid (min 10)\n".  
                 "Max Index: $maxindex (min 10)\n";  
       } else {  
   print $logfile "Does not need ID and/or index fixup\n";  
       }  
     }      }
 # --------------------------------------------- Initial step done, now metadata  # --------------------------------------------- Initial step done, now metadata
   
Line 479  sub publish { Line 560  sub publish {
             
      my %oldparmstores=();       my %oldparmstores=();
             
      $scrout.='<h3>Metadata Information</h3>';       
        $scrout.='<h3>Metadata Information ' .
          Apache::loncommon::help_open_topic("Metadata_Description")
          . '</h3>';
   
 # ------------------------------------------------ First, check out environment  # ------------------------------------------------ First, check out environment
      unless (-e $source.'.meta') {       unless (-e $source.'.meta') {
Line 533  sub publish { Line 617  sub publish {
         my $oldenv=$ENV{'request.uri'};          my $oldenv=$ENV{'request.uri'};
   
         $ENV{'request.uri'}=$target;          $ENV{'request.uri'}=$target;
         $allmeta=Apache::lonxml::xmlparse('meta',$content);          $allmeta=Apache::lonxml::xmlparse(undef,'meta',$content);
         $ENV{'request.uri'}=$oldenv;          $ENV{'request.uri'}=$oldenv;
   
         &metaeval($allmeta);          &metaeval($allmeta);
Line 586  sub publish { Line 670  sub publish {
   
 # --------------------------------------------------- Scan content for keywords  # --------------------------------------------------- Scan content for keywords
   
           my $keywords_help = Apache::loncommon::help_open_topic("Publishing_Keywords");
  my $keywordout=<<"END";   my $keywordout=<<"END";
 <script>  <script>
 function checkAll(field)  function checkAll(field)
Line 600  function uncheckAll(field) Line 685  function uncheckAll(field)
         field[i].checked = false ;          field[i].checked = false ;
 }  }
 </script>  </script>
 <p><b>Keywords:</b>   <p><b>Keywords: $keywords_help</b> 
 <input type="button" value="check all" onclick="javascript:checkAll(document.pubform.keywords)">   <input type="button" value="check all" onclick="javascript:checkAll(document.pubform.keywords)"> 
 <input type="button" value="uncheck all" onclick="javascript:uncheckAll(document.pubform.keywords)">   <input type="button" value="uncheck all" onclick="javascript:uncheckAll(document.pubform.keywords)"> 
 <br />  <br />
Line 680  END Line 765  END
  $scrout.=&textfield('Publisher/Owner','owner',   $scrout.=&textfield('Publisher/Owner','owner',
                             $metadatafields{'owner'});                              $metadatafields{'owner'});
 # --------------------------------------------------- Correct copyright for rat          # --------------------------------------------------- Correct copyright for rat        
   
     if ($style eq 'rat') {      if ($style eq 'rat') {
  if ($metadatafields{'copyright'} eq 'public') {    if ($metadatafields{'copyright'} eq 'public') { 
     delete $metadatafields{'copyright'};      delete $metadatafields{'copyright'};
Line 695  END Line 781  END
     \&Apache::loncommon::copyrightdescription,      \&Apache::loncommon::copyrightdescription,
      (&Apache::loncommon::copyrightids));       (&Apache::loncommon::copyrightids));
     }      }
   
       my $copyright_help = Apache::loncommon::help_open_topic("Publishing_Copyright");
       $scrout =~ s/DISTRIBUTION:/'DISTRIBUTION: ' . $copyright_help/ge;
     return $scrout.      return $scrout.
       '<p><input type="submit" value="Finalize Publication" /></p></form>';        '<p><input type="submit" value="Finalize Publication" /></p></form>';
 }  }
Line 924  if (-e $target) { Line 1013  if (-e $target) {
   
 # --------------------------------------------------- Send update notifications  # --------------------------------------------------- Send update notifications
   
 {      my @subscribed=&get_subscribed_hosts($target);
       foreach my $subhost (@subscribed) {
     my $filename;   $scrout.='<p>Notifying host '.$subhost.':';
     print $logfile "\nNotifying host ".$subhost.':';
     $target=~/(.*)\/([^\/]+)$/;   my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
     my $srcf=$2;   $scrout.=$reply;
     opendir(DIR,$1);   print $logfile $reply;
     while ($filename=readdir(DIR)) {  
        if ($filename=~/$srcf\.(\w+)$/) {  
    my $subhost=$1;  
            if ($subhost ne 'meta') {  
        $scrout.='<p>Notifying host '.$subhost.':';  
                print $logfile "\nNotifying host '.$subhost.':'";  
                my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);  
                $scrout.=$reply;  
                print $logfile $reply;                
            }  
        }  
     }      }
     closedir(DIR);  
   
 }  
   
 # ---------------------------------------- Send update notifications, meta only  # ---------------------------------------- Send update notifications, meta only
   
 {      my @subscribedmeta=&get_subscribed_hosts("$target.meta");
       foreach my $subhost (@subscribedmeta) {
     my $filename;   $scrout.='<p>Notifying host for metadata only '.$subhost.':';
     print $logfile "\nNotifying host for metadata only ".$subhost.':';
     $target=~/(.*)\/([^\/]+)$/;   my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
     my $srcf=$2.'.meta';      $subhost);
     opendir(DIR,$1);   $scrout.=$reply;
     while ($filename=readdir(DIR)) {   print $logfile $reply;
        if ($filename=~/$srcf\.(\w+)$/) {  
    my $subhost=$1;  
            if ($subhost ne 'meta') {  
        $scrout.=  
                 '<p>Notifying host for metadata only '.$subhost.':';  
                print $logfile   
                 "\nNotifying host for metadata only '.$subhost.':'";  
                my $reply=&Apache::lonnet::critical(  
                                 'update:'.$target.'.meta',$subhost);  
                $scrout.=$reply;  
                print $logfile $reply;                
            }  
        }  
     }      }
     closedir(DIR);  
   
 }  
   
 # ------------------------------------------------ Provide link to new resource  # ------------------------------------------------ Provide link to new resource
   

Removed from v.1.81  
changed lines
  Added in v.1.87


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