File:  [LON-CAPA] / loncom / publisher / lonpublisher.pm
Revision 1.46: download - view: text, annotated - select for diffs
Wed Oct 3 11:04:57 2001 UTC (22 years, 8 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
making a less memory intensive parsing of words

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

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