File:  [LON-CAPA] / loncom / publisher / lonpublisher.pm
Revision 1.67: download - view: text, annotated - select for diffs
Tue Dec 25 21:58:54 2001 UTC (22 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Fix keywords and auto-identify

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

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