File:  [LON-CAPA] / loncom / publisher / lonpublisher.pm
Revision 1.62: download - view: text, annotated - select for diffs
Thu Dec 6 18:56:14 2001 UTC (22 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Now also keeps track of resource usage in RAT docs

    1: # The LearningOnline Network with CAPA
    2: # Publication Handler
    3: #
    4: # $Id: lonpublisher.pm,v 1.62 2001/12/06 18:56:14 www Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # 
   29: # (TeX Content Handler
   30: #
   31: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   32: #
   33: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
   34: # 03/23 Guy Albertelli
   35: # 03/24,03/29,04/03 Gerd Kortemeyer
   36: # 04/16/2001 Scott Harrison
   37: # 05/03,05/05,05/07 Gerd Kortemeyer
   38: # 05/28/2001 Scott Harrison
   39: # 06/23,08/07,08/11,8/13,8/17,8/18,8/24,9/26,10/16 Gerd Kortemeyer
   40: # 12/04,12/05 Guy Albertelli
   41: # 12/05 Gerd Kortemeyer
   42: # 12/05 Guy Albertelli
   43: # 12/06 Gerd Kortemeyer
   44: 
   45: package Apache::lonpublisher;
   46: 
   47: use strict;
   48: use Apache::File;
   49: use File::Copy;
   50: use Apache::Constants qw(:common :http :methods);
   51: use HTML::TokeParser;
   52: use Apache::lonxml;
   53: use Apache::lonhomework;
   54: use Apache::loncacc;
   55: use DBI;
   56: 
   57: my %addid;
   58: my %nokey;
   59: my %language;
   60: my %cprtag;
   61: 
   62: my %metadatafields;
   63: my %metadatakeys;
   64: 
   65: my $docroot;
   66: 
   67: my $cuname;
   68: my $cudom;
   69: 
   70: # ----------------------------------------------- Evaluate string with metadata
   71: 
   72: sub metaeval {
   73:     my $metastring=shift;
   74:    
   75:         my $parser=HTML::TokeParser->new(\$metastring);
   76:         my $token;
   77:         while ($token=$parser->get_token) {
   78:            if ($token->[0] eq 'S') {
   79: 	      my $entry=$token->[1];
   80:               my $unikey=$entry;
   81:               if (defined($token->[2]->{'package'})) { 
   82:                   $unikey.='_package_'.$token->[2]->{'package'};
   83:               } 
   84:               if (defined($token->[2]->{'part'})) { 
   85:                  $unikey.='_'.$token->[2]->{'part'}; 
   86: 	      }
   87:               if (defined($token->[2]->{'id'})) { 
   88:                   $unikey.='_'.$token->[2]->{'id'};
   89:               } 
   90:               if (defined($token->[2]->{'name'})) { 
   91:                  $unikey.='_'.$token->[2]->{'name'}; 
   92: 	      }
   93:                map {
   94: 		  $metadatafields{$unikey.'.'.$_}=$token->[2]->{$_};
   95:                   if ($metadatakeys{$unikey}) {
   96: 		      $metadatakeys{$unikey}.=','.$_;
   97:                   } else {
   98:                       $metadatakeys{$unikey}=$_;
   99:                   }
  100:               } @{$token->[3]};
  101:               if ($metadatafields{$unikey}) {
  102: 		  my $newentry=$parser->get_text('/'.$entry);
  103:                   unless (($metadatafields{$unikey}=~/$newentry/) ||
  104:                           ($newentry eq '')) {
  105:                      $metadatafields{$unikey}.=', '.$newentry;
  106: 		  }
  107: 	      } else {
  108:                  $metadatafields{$unikey}=$parser->get_text('/'.$entry);
  109:               }
  110:           }
  111:        }
  112: }
  113: 
  114: # -------------------------------------------------------- Read a metadata file
  115: 
  116: sub metaread {
  117:     my ($logfile,$fn)=@_;
  118:     unless (-e $fn) {
  119: 	print $logfile 'No file '.$fn."\n";
  120:         return '<br><b>No file:</b> <tt>'.$fn.'</tt>';
  121:     }
  122:     print $logfile 'Processing '.$fn."\n";
  123:     my $metastring;
  124:     {
  125:      my $metafh=Apache::File->new($fn);
  126:      $metastring=join('',<$metafh>);
  127:     }
  128:     &metaeval($metastring);
  129:     return '<br><b>Processed file:</b> <tt>'.$fn.'</tt>';
  130: }
  131: 
  132: # ---------------------------- convert 'time' format into a datetime sql format
  133: sub sqltime {
  134:     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  135: 	localtime(@_[0]);
  136:     $mon++; $year+=1900;
  137:     return "$year-$mon-$mday $hour:$min:$sec";
  138: }
  139: 
  140: # --------------------------------------------------------- Various form fields
  141: 
  142: sub textfield {
  143:     my ($title,$name,$value)=@_;
  144:     return "\n<p><b>$title:</b><br>".
  145:            '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
  146: }
  147: 
  148: sub hiddenfield {
  149:     my ($name,$value)=@_;
  150:     return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
  151: }
  152: 
  153: sub selectbox {
  154:     my ($title,$name,$value,%options)=@_;
  155:     my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
  156:     map {
  157:         $selout.='<option value="'.$_.'"';
  158:         if ($_ eq $value) { $selout.=' selected'; }
  159:         $selout.='>'.$options{$_}.'</option>';
  160:     } sort keys %options;
  161:     return $selout.'</select>';
  162: }
  163: 
  164: # -------------------------------------------------------- Publication Step One
  165: 
  166: sub urlfixup {
  167:     my ($url,$target)=@_;
  168:     unless ($url) { return ''; }
  169:     my ($host)=($url=~/(?:http\:\/\/)*([^\/]+)/);
  170:     map {
  171: 	if ($_ eq $host) {
  172: 	    $url=~s/^http\:\/\///;
  173:             $url=~s/^$host//;
  174:         }
  175:     } values %Apache::lonnet::hostname;
  176:     if ($url=~/^http\:\/\//) { return $url; }
  177:     $url=~s/\~$cuname/res\/$cudom\/$cuname/;
  178:     if ($target) {
  179: 	$target=~s/\/[^\/]+$//;
  180:        $url=&Apache::lonnet::hreflocation($target,$url);
  181:     }
  182:     return $url;
  183: }
  184: 
  185: sub publish {
  186: 
  187:     my ($source,$target,$style)=@_;
  188:     my $logfile;
  189:     my $scrout='';
  190:     my $allmeta='';
  191:     my $content='';
  192:     my %allow=();
  193:     undef %allow;
  194: 
  195:     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
  196: 	return 
  197:          '<font color=red>No write permission to user directory, FAIL</font>';
  198:     }
  199:     print $logfile 
  200: "\n\n================= Publish ".localtime()." Phase One  ================\n";
  201: 
  202:     if (($style eq 'ssi') || ($style eq 'rat')) {
  203: # ------------------------------------------------------- This needs processing
  204: 
  205: # ----------------------------------------------------------------- Backup Copy
  206: 	my $copyfile=$source.'.save';
  207:         if (copy($source,$copyfile)) {
  208: 	    print $logfile "Copied original file to ".$copyfile."\n";
  209:         } else {
  210: 	    print $logfile "Unable to write backup ".$copyfile.':'.$!."\n";
  211:           return "<font color=red>Failed to write backup copy, $!,FAIL</font>";
  212:         }
  213: # ------------------------------------------------------------- IDs and indices
  214: 
  215:         my $maxindex=10;
  216:         my $maxid=10;
  217: 
  218:         my $needsfixup=0;
  219: 
  220:         {
  221:           my $org=Apache::File->new($source);
  222:           $content=join('',<$org>);
  223:         }
  224:         {
  225:           my $parser=HTML::TokeParser->new(\$content);
  226:           my $token;
  227:           while ($token=$parser->get_token) {
  228:               if ($token->[0] eq 'S') {
  229:                   my $counter;
  230: 		  if ($counter=$addid{$token->[1]}) {
  231: 		      if ($counter eq 'id') {
  232: 			  if (defined($token->[2]->{'id'})) {
  233:                              $maxid=
  234: 		       ($token->[2]->{'id'}>$maxid)?$token->[2]->{'id'}:$maxid;
  235: 			 } else {
  236:                              $needsfixup=1;
  237:                          }
  238:                       } else {
  239:  			  if (defined($token->[2]->{'index'})) {
  240:                              $maxindex=
  241: 	   ($token->[2]->{'index'}>$maxindex)?$token->[2]->{'index'}:$maxindex;
  242: 			  } else {
  243:                              $needsfixup=1;
  244: 			  }
  245: 		      }
  246: 		  }
  247:               }
  248:           }
  249:       }
  250:       if ($needsfixup) {
  251:           print $logfile "Needs ID and/or index fixup\n".
  252: 	        "Max ID   : $maxid (min 10)\n".
  253:                 "Max Index: $maxindex (min 10)\n";
  254:       }
  255:           my $outstring='';
  256:           my $parser=HTML::TokeParser->new(\$content);
  257:           $parser->xml_mode(1);
  258:           my $token;
  259:           while ($token=$parser->get_token) {
  260:               if ($token->[0] eq 'S') {
  261:                 my $counter;
  262:                 my $tag=$token->[1];
  263:                 my $lctag=lc($tag);
  264:                 unless ($lctag eq 'allow') {  
  265:                   my %parms=%{$token->[2]};
  266:                   $counter=$addid{$tag};
  267:                   if (!$counter) { $counter=$addid{$lctag}; }
  268:                   if ($counter) {
  269: 		      if ($counter eq 'id') {
  270: 			  unless (defined($parms{'id'})) {
  271:                               $maxid++;
  272:                               $parms{'id'}=$maxid;
  273:                               print $logfile 'ID: '.$tag.':'.$maxid."\n";
  274:                           }
  275:                       } elsif ($counter eq 'index') {
  276:  			  unless (defined($parms{'index'})) {
  277:                               $maxindex++;
  278:                               $parms{'index'}=$maxindex;
  279:                               print $logfile 'Index: '.$tag.':'.$maxindex."\n";
  280: 			  }
  281: 		      }
  282: 		  } 
  283:                   
  284:                   map {
  285:                       if (defined($parms{$_})) {
  286: 			  my $oldurl=$parms{$_};
  287:                           my $newurl=&urlfixup($oldurl,$target);
  288:                           if ($newurl ne $oldurl) {
  289: 			      $parms{$_}=$newurl;
  290:                               print $logfile 'URL: '.$tag.':'.$oldurl.' - '.
  291: 				  $newurl."\n";
  292: 			  }
  293:                           $allow{$newurl}=1;
  294:                       }
  295:                   } ('src','href','background');
  296: 
  297:                   if ($lctag eq 'applet') {
  298: 		      my $codebase='';
  299:                       if (defined($parms{'codebase'})) {
  300: 		         my $oldcodebase=$parms{'codebase'};
  301:                          unless ($oldcodebase=~/\/$/) {
  302:                             $oldcodebase.='/';
  303:                          }
  304:                          $codebase=&urlfixup($oldcodebase,$target);
  305:                          $codebase=~s/\/$//;    
  306:                          if ($codebase ne $oldcodebase) {
  307: 			     $parms{'codebase'}=$codebase;
  308:                              print $logfile 'URL codebase: '.$tag.':'.
  309:                                   $oldcodebase.' - '.
  310: 				  $codebase."\n";
  311: 			 }
  312:                          $allow{$codebase.'/*'}=1;
  313: 		      } else {
  314:                         map {
  315:                           if (defined($parms{$_})) {
  316: 			      my $oldurl=$parms{$_};
  317:                               my $newurl=&urlfixup($oldurl,$target);
  318: 			      $newurl=~s/\/[^\/]+$/\/\*/;
  319:                                   print $logfile 'Allow: applet '.$_.':'.
  320:                                   $oldurl.' allows '.
  321: 				  $newurl."\n";
  322:                               $allow{$newurl}=1;
  323:                           }
  324:                         } ('archive','code','object');
  325:                       }
  326:                   }
  327: 
  328:                   my $newparmstring='';
  329:                   my $endtag='';
  330:                   map {
  331:                     if ($_ eq '/') {
  332:                       $endtag=' /';
  333:                     } else { 
  334:                       my $quote=($parms{$_}=~/\"/?"'":'"');
  335:                       $newparmstring.=' '.$_.'='.$quote.$parms{$_}.$quote;
  336: 		    }
  337:                   } keys %parms;
  338: 		  if (!$endtag) { if ($token->[4]=~m:/>$:) { $endtag=' /'; }; }
  339: 		  $outstring.='<'.$tag.$newparmstring.$endtag.'>';
  340: 	         } else {
  341: 		   $allow{$token->[2]->{'src'}}=1;
  342: 		 }
  343:               } elsif ($token->[0] eq 'E') {
  344: 		if ($token->[2]) {
  345:                   unless ($token->[1] eq 'allow') {
  346:                      $outstring.='</'.$token->[1].'>';
  347: 		  }
  348: 		}
  349:               } else {
  350:                   $outstring.=$token->[1];
  351:               }
  352:           }
  353: # ------------------------------------------------------------ Construct Allows
  354:     
  355: 	$scrout.='<h3>Dependencies</h3>';
  356:         my $allowstr='';
  357:         map {
  358: 	   my $thisdep=$_;
  359:            unless ($style eq 'rat') { 
  360:               $allowstr.="\n".'<allow src="'.$thisdep.'" />';
  361: 	   }
  362:            $scrout.='<br>';
  363:            unless ($thisdep=~/\*/) {
  364: 	       $scrout.='<a href="'.$thisdep.'">';
  365:            }
  366:            $scrout.='<tt>'.$thisdep.'</tt>';
  367:            unless ($thisdep=~/\*/) {
  368: 	       $scrout.='</a>';
  369:                if (
  370:        &Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
  371:                                             $thisdep.'.meta') eq '-1') {
  372: 		   $scrout.=
  373:                            ' - <font color=red>Currently not available</font>';
  374:                } else {
  375:                    my %temphash=(&Apache::lonnet::declutter($target).'___'.
  376:                              &Apache::lonnet::declutter($thisdep).'___usage'
  377:                                  => time);
  378:                    $thisdep=~/^\/res\/(\w+)\/(\w+)\//;
  379:                    if ((defined($1)) && (defined($2))) {
  380:                       &Apache::lonnet::put('resevaldata',\%temphash,$1,$2);
  381: 		   }
  382: 	       }
  383:            }
  384:         } keys %allow;
  385:         $outstring=~s/(\<\/[^\>]+\>\s*)$/$allowstr$1/s;
  386: 
  387: # ------------------------------------------------------------- Write modified
  388: 
  389:         {
  390:           my $org;
  391:           unless ($org=Apache::File->new('>'.$source)) {
  392:              print $logfile "No write permit to $source\n";
  393:              return 
  394:               "<font color=red>No write permission to $source, FAIL</font>";
  395: 	  }
  396:           print $org $outstring;
  397:         }
  398: 	  $content=$outstring;
  399: 
  400:       if ($needsfixup) {
  401:           print $logfile "End of ID and/or index fixup\n".
  402: 	        "Max ID   : $maxid (min 10)\n".
  403:                 "Max Index: $maxindex (min 10)\n";
  404:       } else {
  405: 	  print $logfile "Does not need ID and/or index fixup\n";
  406:       }
  407:     }
  408: # --------------------------------------------- Initial step done, now metadata
  409: 
  410: # ---------------------------------------- Storage for metadata keys and fields
  411: 
  412:      %metadatafields=();
  413:      %metadatakeys=();
  414:      
  415:      my %oldparmstores=();
  416:      
  417:      $scrout.='<h3>Metadata Information</h3>';
  418: 
  419: # ------------------------------------------------ First, check out environment
  420:      unless (-e $source.'.meta') {
  421:         $metadatafields{'author'}=$ENV{'environment.firstname'}.' '.
  422: 	                          $ENV{'environment.middlename'}.' '.
  423: 		                  $ENV{'environment.lastname'}.' '.
  424: 		                  $ENV{'environment.generation'};
  425:         $metadatafields{'author'}=~s/\s+/ /g;
  426:         $metadatafields{'author'}=~s/\s+$//;
  427:         $metadatafields{'owner'}=$cuname.'@'.$cudom;
  428: 
  429: # ------------------------------------------------ Check out directory hierachy
  430: 
  431:         my $thisdisfn=$source;
  432:         $thisdisfn=~s/^\/home\/$cuname\///;
  433: 
  434:         my @urlparts=split(/\//,$thisdisfn);
  435:         $#urlparts--;
  436: 
  437:         my $currentpath='/home/'.$cuname.'/';
  438: 
  439:         map {
  440: 	    $currentpath.=$_.'/';
  441:             $scrout.=&metaread($logfile,$currentpath.'default.meta');
  442:         } @urlparts;
  443: 
  444: # ------------------- Clear out parameters and stores (there should not be any)
  445: 
  446:         map {
  447: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
  448: 		delete $metadatafields{$_};
  449:             }
  450:         } keys %metadatafields;
  451: 
  452:     } else {
  453: # ---------------------- Read previous metafile, remember parameters and stores
  454: 
  455:         $scrout.=&metaread($logfile,$source.'.meta');
  456: 
  457:         map {
  458: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
  459:                 $oldparmstores{$_}=1;
  460: 		delete $metadatafields{$_};
  461:             }
  462:         } keys %metadatafields;
  463:         
  464:     }
  465: 
  466: # -------------------------------------------------- Parse content for metadata
  467:     if ($style eq 'ssi') {
  468:         my $oldenv=$ENV{'request.uri'};
  469: 
  470:         $ENV{'request.uri'}=$target;
  471:         $allmeta=Apache::lonxml::xmlparse('meta',$content);
  472:         $ENV{'request.uri'}=$oldenv;
  473: 
  474:         &metaeval($allmeta);
  475:     }
  476: # ---------------- Find and document discrepancies in the parameters and stores
  477: 
  478:         my $chparms='';
  479:         map {
  480: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
  481:                 unless ($_=~/\.\w+$/) { 
  482:                    unless ($oldparmstores{$_}) {
  483: 		      print $logfile 'New: '.$_."\n";
  484:                       $chparms.=$_.' ';
  485:                    }
  486: 	        }
  487:             }
  488:         } sort keys %metadatafields;
  489:         if ($chparms) {
  490: 	    $scrout.='<p><b>New parameters or stored values:</b> '.
  491:                      $chparms;
  492:         }
  493: 
  494:         my $chparms='';
  495:         map {
  496: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
  497:                 unless (($metadatafields{$_.'.name'}) ||
  498:                         ($metadatafields{$_.'.package'}) || ($_=~/\.\w+$/)) {
  499: 		    print $logfile 'Obsolete: '.$_."\n";
  500:                     $chparms.=$_.' ';
  501:                 }
  502:             }
  503:         } sort keys %oldparmstores;
  504:         if ($chparms) {
  505: 	    $scrout.='<p><b>Obsolete parameters or stored values:</b> '.
  506:                      $chparms;
  507:         }
  508: 
  509: # ------------------------------------------------------- Now have all metadata
  510: 
  511:         $scrout.=
  512:      '<form action="/adm/publish" method="post">'.
  513:           &hiddenfield('phase','two').
  514:           &hiddenfield('filename',$ENV{'form.filename'}).
  515: 	  &hiddenfield('allmeta',&Apache::lonnet::escape($allmeta)).
  516:           &hiddenfield('dependencies',join(',',keys %allow)).
  517:           &textfield('Title','title',$metadatafields{'title'}).
  518:           &textfield('Author(s)','author',$metadatafields{'author'}).
  519: 	  &textfield('Subject','subject',$metadatafields{'subject'});
  520: 
  521: # --------------------------------------------------- Scan content for keywords
  522: 
  523: 	my $keywordout='<p><b>Keywords:</b><br><table border=2><tr>';
  524:         my $colcount=0;
  525:         
  526: 	if (length($content)<500000) {
  527: 	    my $textonly=$content;
  528:             $textonly=~s/\<script[^\<]+\<\/script\>//g;
  529:             $textonly=~s/\<m\>[^\<]+\<\/m\>//g;
  530:             $textonly=~s/\<[^\>]*\>//g;
  531:             $textonly=~tr/A-Z/a-z/;
  532:             $textonly=~s/[\$\&][a-z]\w*//g;
  533:             $textonly=~s/[^a-z\s]//g;
  534: 
  535:             my %keywords=();
  536:             map {
  537: 		unless ($nokey{$_}) {
  538:                    $keywords{$_}=1;
  539:                 } 
  540:             } ($textonly=~m/(\w+)/g);
  541: 
  542:             map {
  543: 		$keywords{$_}=1;
  544:             } split(/\W+/,$metadatafields{'keywords'});
  545: 
  546:             map {
  547:                 $keywordout.='<td><input type=checkbox name="key.'.$_.'"';
  548:                 if ($metadatafields{'keywords'}=~/$_/) { 
  549:                    $keywordout.=' checked'; 
  550:                 }
  551:                 $keywordout.='>'.$_.'</td>';
  552:                 if ($colcount>10) {
  553: 		    $keywordout.="</tr><tr>\n";
  554:                     $colcount=0;
  555:                 }
  556:                 $colcount++;
  557:             } sort keys %keywords;
  558: 
  559:         } else {
  560: 	    $keywordout.='<td>File too long for keyword analysis</td>';
  561:         }         
  562:         
  563: 	$keywordout.='</tr></table>';
  564: 
  565:         $scrout.=$keywordout;
  566: 
  567:         $scrout.=&textfield('Additional Keywords','addkey','');
  568: 
  569:         $scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
  570: 
  571:         $scrout.=
  572:              '<p><b>Abstract:</b><br><textarea cols=80 rows=5 name=abstract>'.
  573:               $metadatafields{'abstract'}.'</textarea>';
  574: 
  575: 	$source=~/\.(\w+)$/;
  576: 
  577: 	$scrout.=&hiddenfield('mime',$1);
  578: 
  579:         $scrout.=&selectbox('Language','language',
  580:                             $metadatafields{'language'},%language);
  581: 
  582:         unless ($metadatafields{'creationdate'}) {
  583: 	    $metadatafields{'creationdate'}=time;
  584:         }
  585:         $scrout.=&hiddenfield('creationdate',$metadatafields{'creationdate'});
  586: 
  587:         $scrout.=&hiddenfield('lastrevisiondate',time);
  588: 
  589: 			   
  590: 	$scrout.=&textfield('Publisher/Owner','owner',
  591:                             $metadatafields{'owner'});
  592: # --------------------------------------------------- Correct copyright for rat        
  593:     if ($style eq 'rat') {
  594:        if ($metadatafields{'copyright'} eq 'public') { 
  595:           delete $metadatafields{'copyright'};
  596:        }
  597:        delete $cprtag{'public'};
  598:    }
  599: 
  600:         $scrout.=&selectbox('Copyright/Distribution','copyright',
  601:                             $metadatafields{'copyright'},%cprtag);
  602: 
  603:     return $scrout.
  604:       '<p><input type="submit" value="Finalize Publication"></form>';
  605: }
  606: 
  607: # -------------------------------------------------------- Publication Step Two
  608: 
  609: sub phasetwo {
  610: 
  611:     my ($source,$target,$style,$distarget)=@_;
  612:     my $logfile;
  613:     my $scrout='';
  614: 
  615:     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
  616: 	return 
  617:          '<font color=red>No write permission to user directory, FAIL</font>';
  618:     }
  619:     print $logfile 
  620: "\n================= Publish ".localtime()." Phase Two  ================\n";
  621: 
  622:      %metadatafields=();
  623:      %metadatakeys=();
  624: 
  625:      &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
  626: 
  627:      $metadatafields{'title'}=$ENV{'form.title'};
  628:      $metadatafields{'author'}=$ENV{'form.author'};
  629:      $metadatafields{'subject'}=$ENV{'form.subject'};
  630:      $metadatafields{'notes'}=$ENV{'form.notes'};
  631:      $metadatafields{'abstract'}=$ENV{'form.abstract'};
  632:      $metadatafields{'mime'}=$ENV{'form.mime'};
  633:      $metadatafields{'language'}=$ENV{'form.language'};
  634:      $metadatafields{'creationdate'}=$ENV{'form.creationdate'};
  635:      $metadatafields{'lastrevisiondate'}=$ENV{'form.lastrevisiondate'};
  636:      $metadatafields{'owner'}=$ENV{'form.owner'};
  637:      $metadatafields{'copyright'}=$ENV{'form.copyright'};
  638:      $metadatafields{'dependencies'}=$ENV{'form.dependencies'};
  639: 
  640:      my $allkeywords=$ENV{'form.addkey'};
  641:      map {
  642:          if ($_=~/^form\.key\.(\w+)/) {
  643: 	     $allkeywords.=','.$1;
  644:          }
  645:      } keys %ENV;
  646:      $allkeywords=~s/\W+/\,/;
  647:      $allkeywords=~s/^\,//;
  648:      $metadatafields{'keywords'}=$allkeywords;
  649:  
  650:      {
  651:        print $logfile "\nWrite metadata file for ".$source;
  652:        my $mfh;
  653:        unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
  654: 	return 
  655:          '<font color=red>Could not write metadata, FAIL</font>';
  656:        }    
  657:        map {
  658: 	 unless ($_=~/\./) {
  659:            my $unikey=$_;
  660:            $unikey=~/^([A-Za-z]+)/;
  661:            my $tag=$1;
  662:            $tag=~tr/A-Z/a-z/;
  663:            print $mfh "\n\<$tag";
  664:            map {
  665:                my $value=$metadatafields{$unikey.'.'.$_};
  666:                $value=~s/\"/\'\'/g;
  667:                print $mfh ' '.$_.'="'.$value.'"';
  668:            } split(/\,/,$metadatakeys{$unikey});
  669: 	   print $mfh '>'.$metadatafields{$unikey}.'</'.$tag.'>';
  670:          }
  671:        } sort keys %metadatafields;
  672:        $scrout.='<p>Wrote Metadata';
  673:        print $logfile "\nWrote metadata";
  674:      }
  675: 
  676: # -------------------------------- Synchronize entry with SQL metadata database
  677:     my %perlvar;
  678:     open (CONFIG,"/etc/httpd/conf/access.conf") || die "Can't read access.conf";
  679:     my $configline;
  680:     while ($configline=<CONFIG>) {
  681: 	if ($configline =~ /PerlSetVar/) {
  682: 	    my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
  683: 	    chomp($varvalue);
  684: 	    $perlvar{$varname}=$varvalue;
  685: 	}
  686:     }
  687:     close(CONFIG);
  688: 
  689:     my $warning;
  690:     my $dbh;
  691:     {
  692: 	unless (
  693: 		$dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
  694: 		) { 
  695: 	    $warning='<font color=red>WARNING: Cannot connect to '.
  696: 		'database!</font>';
  697: 	}
  698: 	else {
  699: 	    my %sqldatafields;
  700: 	    $sqldatafields{'url'}=$distarget;
  701: 	    my $sth=$dbh->prepare(
  702: 				  'delete from metadata where url like binary'.
  703: 				  '"'.$sqldatafields{'url'}.'"');
  704: 	    $sth->execute();
  705: 	    map {my $field=$metadatafields{$_}; $field=~s/\"/\'\'/g; 
  706: 		 $sqldatafields{$_}=$field;}
  707: 	    ('title','author','subject','keywords','notes','abstract',
  708: 	     'mime','language','creationdate','lastrevisiondate','owner',
  709: 	     'copyright');
  710: 	    
  711: 	    $sth=$dbh->prepare('insert into metadata values ('.
  712: 			       '"'.delete($sqldatafields{'title'}).'"'.','.
  713: 			       '"'.delete($sqldatafields{'author'}).'"'.','.
  714: 			       '"'.delete($sqldatafields{'subject'}).'"'.','.
  715: 			       '"'.delete($sqldatafields{'url'}).'"'.','.
  716: 			       '"'.delete($sqldatafields{'keywords'}).'"'.','.
  717: 			       '"'.'current'.'"'.','.
  718: 			       '"'.delete($sqldatafields{'notes'}).'"'.','.
  719: 			       '"'.delete($sqldatafields{'abstract'}).'"'.','.
  720: 			       '"'.delete($sqldatafields{'mime'}).'"'.','.
  721: 			       '"'.delete($sqldatafields{'language'}).'"'.','.
  722: 			       '"'.
  723: 			       sqltime(delete($sqldatafields{'creationdate'}))
  724: 			       .'"'.','.
  725: 			       '"'.
  726: 			       sqltime(delete(
  727: 			       $sqldatafields{'lastrevisiondate'})).'"'.','.
  728: 			       '"'.delete($sqldatafields{'owner'}).'"'.','.
  729: 			       '"'.delete(
  730: 			       $sqldatafields{'copyright'}).'"'.')');
  731: 	    $sth->execute();
  732: 	    $dbh->disconnect;
  733: 	    $scrout.='<p>Synchronized SQL metadata database';
  734: 	    print $logfile "\nSynchronized SQL metadata database";
  735: 	}
  736:     }
  737: 
  738: 
  739: # ----------------------------------------------------------- Copy old versions
  740:    
  741: if (-e $target) {
  742:     my $filename;
  743:     my $maxversion=0;
  744:     $target=~/(.*)\/([^\/]+)\.(\w+)$/;
  745:     my $srcf=$2;
  746:     my $srct=$3;
  747:     my $srcd=$1;
  748:     unless ($srcd=~/^\/home\/httpd\/html\/res/) {
  749: 	print $logfile "\nPANIC: Target dir is ".$srcd;
  750:         return "<font color=red>Invalid target directory, FAIL</font>";
  751:     }
  752:     opendir(DIR,$srcd);
  753:     while ($filename=readdir(DIR)) {
  754:        if ($filename=~/$srcf\.(\d+)\.$srct$/) {
  755: 	   $maxversion=($1>$maxversion)?$1:$maxversion;
  756:        }
  757:     }
  758:     closedir(DIR);
  759:     $maxversion++;
  760:     $scrout.='<p>Creating old version '.$maxversion;
  761:     print $logfile "\nCreating old version ".$maxversion;
  762: 
  763:     my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
  764: 
  765:         if (copy($target,$copyfile)) {
  766: 	    print $logfile "Copied old target to ".$copyfile."\n";
  767:             $scrout.='<p>Copied old target file';
  768:         } else {
  769: 	    print $logfile "Unable to write ".$copyfile.':'.$!."\n";
  770:            return "<font color=red>Failed to copy old target, $!, FAIL</font>";
  771:         }
  772: 
  773: # --------------------------------------------------------------- Copy Metadata
  774: 
  775: 	$copyfile=$copyfile.'.meta';
  776: 
  777:         if (copy($target.'.meta',$copyfile)) {
  778: 	    print $logfile "Copied old target metadata to ".$copyfile."\n";
  779:             $scrout.='<p>Copied old metadata';
  780:         } else {
  781: 	    print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
  782:             if (-e $target.'.meta') {
  783:                return 
  784:        "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
  785: 	    }
  786:         }
  787: 
  788: 
  789: } else {
  790:     $scrout.='<p>Initial version';
  791:     print $logfile "\nInitial version";
  792: }
  793: 
  794: # ---------------------------------------------------------------- Write Source
  795: 	my $copyfile=$target;
  796: 
  797:            my @parts=split(/\//,$copyfile);
  798:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
  799: 
  800:            my $count;
  801:            for ($count=5;$count<$#parts;$count++) {
  802:                $path.="/$parts[$count]";
  803:                if ((-e $path)!=1) {
  804:                    print $logfile "\nCreating directory ".$path;
  805:                    $scrout.='<p>Created directory '.$parts[$count];
  806: 		   mkdir($path,0777);
  807:                }
  808:            }
  809: 
  810:         if (copy($source,$copyfile)) {
  811: 	    print $logfile "Copied original source to ".$copyfile."\n";
  812:             $scrout.='<p>Copied source file';
  813:         } else {
  814: 	    print $logfile "Unable to write ".$copyfile.':'.$!."\n";
  815:             return "<font color=red>Failed to copy source, $!, FAIL</font>";
  816:         }
  817: 
  818: # --------------------------------------------------------------- Copy Metadata
  819: 
  820:         $copyfile=$copyfile.'.meta';
  821: 
  822:         if (copy($source.'.meta',$copyfile)) {
  823: 	    print $logfile "Copied original metadata to ".$copyfile."\n";
  824:             $scrout.='<p>Copied metadata';
  825:         } else {
  826: 	    print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
  827:             return 
  828:           "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
  829:         }
  830: 
  831: # --------------------------------------------------- Send update notifications
  832: 
  833: {
  834: 
  835:     my $filename;
  836:  
  837:     $target=~/(.*)\/([^\/]+)$/;
  838:     my $srcf=$2;
  839:     opendir(DIR,$1);
  840:     while ($filename=readdir(DIR)) {
  841:        if ($filename=~/$srcf\.(\w+)$/) {
  842: 	   my $subhost=$1;
  843:            if ($subhost ne 'meta') {
  844: 	       $scrout.='<p>Notifying host '.$subhost.':';
  845:                print $logfile "\nNotifying host '.$subhost.':'";
  846:                my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
  847:                $scrout.=$reply;
  848:                print $logfile $reply;              
  849:            }
  850:        }
  851:     }
  852:     closedir(DIR);
  853: 
  854: }
  855: 
  856: # ---------------------------------------- Send update notifications, meta only
  857: 
  858: {
  859: 
  860:     my $filename;
  861:  
  862:     $target=~/(.*)\/([^\/]+)$/;
  863:     my $srcf=$2.'.meta';
  864:     opendir(DIR,$1);
  865:     while ($filename=readdir(DIR)) {
  866:        if ($filename=~/$srcf\.(\w+)$/) {
  867: 	   my $subhost=$1;
  868:            if ($subhost ne 'meta') {
  869: 	       $scrout.=
  870:                 '<p>Notifying host for metadata only '.$subhost.':';
  871:                print $logfile 
  872:                 "\nNotifying host for metadata only '.$subhost.':'";
  873:                my $reply=&Apache::lonnet::critical(
  874:                                 'update:'.$target.'.meta',$subhost);
  875:                $scrout.=$reply;
  876:                print $logfile $reply;              
  877:            }
  878:        }
  879:     }
  880:     closedir(DIR);
  881: 
  882: }
  883: 
  884: # ------------------------------------------------ Provide link to new resource
  885: 
  886:     my $thisdistarget=$target;
  887:     $thisdistarget=~s/^$docroot//;
  888: 
  889:     my $thissrc=$source;
  890:     $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
  891: 
  892:     my $thissrcdir=$thissrc;
  893:     $thissrcdir=~s/\/[^\/]+$/\//;
  894: 
  895: 
  896:     return $warning.$scrout.
  897:       '<hr><a href="'.$thisdistarget.'"><font size=+2>View Target</font></a>'.
  898:       '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
  899:       '<p><a href="'.$thissrcdir.
  900:       '"><font size=+2>Back to Source Directory</font></a>';
  901: 
  902: }
  903: 
  904: # ================================================================ Main Handler
  905: 
  906: sub handler {
  907:   my $r=shift;
  908: 
  909:   if ($r->header_only) {
  910:      $r->content_type('text/html');
  911:      $r->send_http_header;
  912:      return OK;
  913:   }
  914: 
  915: # Get query string for limited number of parameters
  916: 
  917:     map {
  918:        my ($name, $value) = split(/=/,$_);
  919:        $value =~ tr/+/ /;
  920:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  921:        if ($name eq 'filename') {
  922:            unless ($ENV{'form.'.$name}) {
  923:               $ENV{'form.'.$name}=$value;
  924: 	   }
  925:        }
  926:     } (split(/&/,$ENV{'QUERY_STRING'}));
  927: 
  928: 
  929: # -------------------------------------------------------------- Check filename
  930: 
  931:   my $fn=$ENV{'form.filename'};
  932: 
  933:   
  934:   unless ($fn) { 
  935:      $r->log_reason($cuname.' at '.$cudom.
  936:          ' trying to publish empty filename', $r->filename); 
  937:      return HTTP_NOT_FOUND;
  938:   } 
  939: 
  940:   ($cuname,$cudom)=
  941:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
  942:   unless (($cuname) && ($cudom)) {
  943:      $r->log_reason($cuname.' at '.$cudom.
  944:          ' trying to publish file '.$ENV{'form.filename'}.
  945:          ' ('.$fn.') - not authorized', 
  946:          $r->filename); 
  947:      return HTTP_NOT_ACCEPTABLE;
  948:   }
  949: 
  950:   unless (&Apache::lonnet::homeserver($cuname,$cudom) 
  951:           eq $r->dir_config('lonHostID')) {
  952:      $r->log_reason($cuname.' at '.$cudom.
  953:          ' trying to publish file '.$ENV{'form.filename'}.
  954:          ' ('.$fn.') - not homeserver ('.
  955:          &Apache::lonnet::homeserver($cuname,$cudom).')', 
  956:          $r->filename); 
  957:      return HTTP_NOT_ACCEPTABLE;
  958:   }
  959: 
  960:   $fn=~s/^http\:\/\/[^\/]+//;
  961:   $fn=~s/^\/\~(\w+)/\/home\/$1\/public_html/;
  962: 
  963:   my $targetdir='';
  964:   $docroot=$r->dir_config('lonDocRoot'); 
  965:   if ($1 ne $cuname) {
  966:      $r->log_reason($cuname.' at '.$cudom.
  967:          ' trying to publish unowned file '.$ENV{'form.filename'}.
  968:          ' ('.$fn.')', 
  969:          $r->filename); 
  970:      return HTTP_NOT_ACCEPTABLE;
  971:   } else {
  972:       $targetdir=$docroot.'/res/'.$cudom;
  973:   }
  974:                                  
  975:   
  976:   unless (-e $fn) { 
  977:      $r->log_reason($cuname.' at '.$cudom.
  978:          ' trying to publish non-existing file '.$ENV{'form.filename'}.
  979:          ' ('.$fn.')', 
  980:          $r->filename); 
  981:      return HTTP_NOT_FOUND;
  982:   } 
  983: 
  984: unless ($ENV{'form.phase'} eq 'two') {
  985: 
  986: # --------------------------------- File is there and owned, init lookup tables
  987: 
  988:   %addid=();
  989: 
  990:   {
  991:       my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
  992:       while (<$fh>=~/(\w+)\s+(\w+)/) {
  993:           $addid{$1}=$2;
  994:       }
  995:   }
  996: 
  997:   %nokey=();
  998: 
  999:   {
 1000:      my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
 1001:       map {
 1002:           my $word=$_;
 1003:           chomp($word);
 1004:           $nokey{$word}=1;
 1005:       } <$fh>;
 1006:   }
 1007: 
 1008:   %language=();
 1009: 
 1010:   {
 1011:      my $fh=Apache::File->new($r->dir_config('lonTabDir').'/language.tab');
 1012:       map {
 1013:           $_=~/(\w+)\s+([\w\s\-]+)/;
 1014:           $language{$1}=$2;
 1015:       } <$fh>;
 1016:   }
 1017: 
 1018:   %cprtag=();
 1019: 
 1020:   {
 1021:      my $fh=Apache::File->new($r->dir_config('lonIncludes').'/copyright.tab');
 1022:       map {
 1023:           $_=~/(\w+)\s+([\w\s\-]+)/;
 1024:           $cprtag{$1}=$2;
 1025:       } <$fh>;
 1026:   }
 1027: 
 1028: }
 1029: 
 1030: # ----------------------------------------------------------- Start page output
 1031: 
 1032:   $r->content_type('text/html');
 1033:   $r->send_http_header;
 1034: 
 1035:   $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
 1036:   $r->print(
 1037:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
 1038:   my $thisfn=$fn;
 1039:    
 1040: # ------------------------------------------------------------- Individual file
 1041:   {
 1042:       $thisfn=~/\.(\w+)$/;
 1043:       my $thistype=$1;
 1044:       my $thisembstyle=&Apache::lonnet::fileembstyle($thistype);
 1045: 
 1046:       my $thistarget=$thisfn;
 1047:       
 1048:       $thistarget=~s/^\/home/$targetdir/;
 1049:       $thistarget=~s/\/public\_html//;
 1050: 
 1051:       my $thisdistarget=$thistarget;
 1052:       $thisdistarget=~s/^$docroot//;
 1053: 
 1054:       my $thisdisfn=$thisfn;
 1055:       $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
 1056: 
 1057:       $r->print('<h2>Publishing '.
 1058:         &Apache::lonnet::filedescription($thistype).' <tt>'.
 1059:         $thisdisfn.'</tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
 1060:    
 1061:        if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
 1062:           $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
 1063:                '</font></h3>');
 1064:       }
 1065: 
 1066:       if (&Apache::lonnet::fileembstyle($thistype) eq 'ssi') {
 1067:           $r->print('<br><a href="/adm/diff?filename=/~'.$cuname.'/'.
 1068:                     $thisdisfn.
 1069:   	  '&versionone=priv" target=cat>Diffs with Current Version</a><p>');
 1070:       }
 1071:   
 1072: # ------------ We are publishing from $thisfn to $thistarget with $thisembstyle
 1073: 
 1074:        unless ($ENV{'form.phase'} eq 'two') {
 1075:          $r->print(
 1076:           '<hr>'.&publish($thisfn,$thistarget,$thisembstyle));
 1077:        } else {
 1078:          $r->print(
 1079:           '<hr>'.&phasetwo($thisfn,$thistarget,$thisembstyle,$thisdistarget)); 
 1080:        }  
 1081: 
 1082:   }
 1083:   $r->print('</body></html>');
 1084: 
 1085:   return OK;
 1086: }
 1087: 
 1088: 1;
 1089: __END__
 1090: 
 1091: 
 1092: 
 1093: 
 1094: 
 1095: 
 1096: 

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