File:  [LON-CAPA] / loncom / publisher / lonpublisher.pm
Revision 1.23: download - view: text, annotated - select for diffs
Tue Apr 3 11:26:02 2001 UTC (23 years, 2 months ago) by www
Branches: MAIN
CVS tags: HEAD
Handles other MIME types than XML-like

    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: 
   12: package Apache::lonpublisher;
   13: 
   14: use strict;
   15: use Apache::File;
   16: use File::Copy;
   17: use Apache::Constants qw(:common :http :methods);
   18: use HTML::TokeParser;
   19: use Apache::lonxml;
   20: use Apache::lonhomework;
   21: 
   22: my %addid;
   23: my %nokey;
   24: my %language;
   25: my %cprtag;
   26: 
   27: my %metadatafields;
   28: my %metadatakeys;
   29: 
   30: my $docroot;
   31: 
   32: # ----------------------------------------------- Evaluate string with metadata
   33: 
   34: sub metaeval {
   35:     my $metastring=shift;
   36:    
   37:         my $parser=HTML::TokeParser->new(\$metastring);
   38:         my $token;
   39:         while ($token=$parser->get_token) {
   40:            if ($token->[0] eq 'S') {
   41: 	      my $entry=$token->[1];
   42:               my $unikey=$entry;
   43:               if (defined($token->[2]->{'part'})) { 
   44:                  $unikey.='_'.$token->[2]->{'part'}; 
   45: 	      }
   46:               if (defined($token->[2]->{'name'})) { 
   47:                  $unikey.='_'.$token->[2]->{'name'}; 
   48: 	      }
   49:                map {
   50: 		  $metadatafields{$unikey.'.'.$_}=$token->[2]->{$_};
   51:                   if ($metadatakeys{$unikey}) {
   52: 		      $metadatakeys{$unikey}.=','.$_;
   53:                   } else {
   54:                       $metadatakeys{$unikey}=$_;
   55:                   }
   56:               } @{$token->[3]};
   57:               if ($metadatafields{$unikey}) {
   58: 		  my $newentry=$parser->get_text('/'.$entry);
   59:                   unless ($metadatafields{$unikey}=~/$newentry/) {
   60:                      $metadatafields{$unikey}.=', '.$newentry;
   61: 		  }
   62: 	      } else {
   63:                  $metadatafields{$unikey}=$parser->get_text('/'.$entry);
   64:               }
   65:           }
   66:        }
   67: }
   68: 
   69: # -------------------------------------------------------- Read a metadata file
   70: 
   71: sub metaread {
   72:     my ($logfile,$fn)=@_;
   73:     unless (-e $fn) {
   74: 	print $logfile 'No file '.$fn."\n";
   75:         return '<br><b>No file:</b> <tt>'.$fn.'</tt>';
   76:     }
   77:     print $logfile 'Processing '.$fn."\n";
   78:     my $metastring;
   79:     {
   80:      my $metafh=Apache::File->new($fn);
   81:      $metastring=join('',<$metafh>);
   82:     }
   83:     &metaeval($metastring);
   84:     return '<br><b>Processed file:</b> <tt>'.$fn.'</tt>';
   85: }
   86: 
   87: # --------------------------------------------------------- Various form fields
   88: 
   89: sub textfield {
   90:     my ($title,$name,$value)=@_;
   91:     return "\n<p><b>$title:</b><br>".
   92:            '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
   93: }
   94: 
   95: sub hiddenfield {
   96:     my ($name,$value)=@_;
   97:     return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
   98: }
   99: 
  100: sub selectbox {
  101:     my ($title,$name,$value,%options)=@_;
  102:     my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
  103:     map {
  104:         $selout.='<option value="'.$_.'"';
  105:         if ($_ eq $value) { $selout.=' selected'; }
  106:         $selout.='>'.$options{$_}.'</option>';
  107:     } sort keys %options;
  108:     return $selout.'</select>';
  109: }
  110: 
  111: # -------------------------------------------------------- Publication Step One
  112: 
  113: sub publish {
  114: 
  115:     my ($source,$target,$style)=@_;
  116:     my $logfile;
  117:     my $scrout='';
  118:     my $allmeta='';
  119:     my $content='';
  120: 
  121:     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
  122: 	return 
  123:          '<font color=red>No write permission to user directory, FAIL</font>';
  124:     }
  125:     print $logfile 
  126: "\n\n================= Publish ".localtime()." Phase One  ================\n";
  127: 
  128:     if (($style eq 'ssi') || ($style eq 'rat')) {
  129: # ------------------------------------------------------- This needs processing
  130: 
  131: # ----------------------------------------------------------------- Backup Copy
  132: 	my $copyfile=$source.'.save';
  133:         if (copy($source,$copyfile)) {
  134: 	    print $logfile "Copied original file to ".$copyfile."\n";
  135:         } else {
  136: 	    print $logfile "Unable to write backup ".$copyfile.':'.$!."\n";
  137:           return "<font color=red>Failed to write backup copy, $!,FAIL</font>";
  138:         }
  139: # ------------------------------------------------------------- IDs and indices
  140: 
  141:         my $maxindex=10;
  142:         my $maxid=10;
  143: 
  144:         my $needsfixup=0;
  145: 
  146:         {
  147:           my $org=Apache::File->new($source);
  148:           $content=join('',<$org>);
  149:         }
  150:         {
  151:           my $parser=HTML::TokeParser->new(\$content);
  152:           my $token;
  153:           while ($token=$parser->get_token) {
  154:               if ($token->[0] eq 'S') {
  155:                   my $counter;
  156: 		  if ($counter=$addid{$token->[1]}) {
  157: 		      if ($counter eq 'id') {
  158: 			  if (defined($token->[2]->{'id'})) {
  159:                              $maxid=
  160: 		       ($token->[2]->{'id'}>$maxid)?$token->[2]->{'id'}:$maxid;
  161: 			 } else {
  162:                              $needsfixup=1;
  163:                          }
  164:                       } else {
  165:  			  if (defined($token->[2]->{'index'})) {
  166:                              $maxindex=
  167: 	   ($token->[2]->{'index'}>$maxindex)?$token->[2]->{'index'}:$maxindex;
  168: 			  } else {
  169:                              $needsfixup=1;
  170: 			  }
  171: 		      }
  172: 		  }
  173:               }
  174:           }
  175:       }
  176:       if ($needsfixup) {
  177:           print $logfile "Needs ID and/or index fixup\n".
  178: 	        "Max ID   : $maxid (min 10)\n".
  179:                 "Max Index: $maxindex (min 10)\n";
  180: 
  181:           my $outstring='';
  182:           my $parser=HTML::TokeParser->new(\$content);
  183:           my $token;
  184:           while ($token=$parser->get_token) {
  185:               if ($token->[0] eq 'S') {
  186:                   my $counter;
  187: 		  if ($counter=$addid{$token->[1]}) {
  188: 		      if ($counter eq 'id') {
  189: 			  if (defined($token->[2]->{'id'})) {
  190: 			      $outstring.=$token->[4];
  191: 			  } else {
  192:                               $maxid++;
  193:                               my $thisid=' id="'.$maxid.'"';
  194: 			      my $fixup=$token->[4];
  195:                               $fixup=~s/(\<\w+)/$1$thisid/;
  196:                               $outstring.=$fixup;
  197:                               print $logfile 'ID: '.$fixup."\n";
  198:                           }
  199:                       } else {
  200:  			  if (defined($token->[2]->{'index'})) {
  201: 			      $outstring.=$token->[4];
  202: 			  } else {
  203:                               $maxindex++;
  204:                               my $thisindex=' index="'.$maxindex.'"';
  205: 			      my $fixup=$token->[4];
  206:                               $fixup=~s/(\<\w+)/$1$thisindex/;
  207:                               $outstring.=$fixup;
  208:                               print $logfile 'Index: '.$fixup."\n";
  209: 			  }
  210: 		      }
  211: 		  } else {
  212:                       $outstring.=$token->[4];
  213:                   }
  214:               } elsif ($token->[0] eq 'E') {
  215:                   $outstring.=$token->[2];
  216:               } else {
  217:                   $outstring.=$token->[1];
  218:               }
  219:           }
  220:         {
  221:           my $org;
  222:           unless ($org=Apache::File->new('>'.$source)) {
  223:              print $logfile "No write permit to $source\n";
  224:              return 
  225:               "<font color=red>No write permission to $source, FAIL</font>";
  226: 	  }
  227:           print $org $outstring;
  228:         }
  229: 	  $content=$outstring;
  230:           print $logfile "End of ID and/or index fixup\n".
  231: 	        "Max ID   : $maxid (min 10)\n".
  232:                 "Max Index: $maxindex (min 10)\n";
  233:       } else {
  234: 	  print $logfile "Does not need ID and/or index fixup\n";
  235:       }
  236: 
  237: # --------------------------------------------- Initial step done, now metadata
  238: 
  239: # ---------------------------------------- Storage for metadata keys and fields
  240: 
  241:      %metadatafields=();
  242:      %metadatakeys=();
  243:      
  244:      my %oldparmstores=();
  245: 
  246: # ------------------------------------------------ First, check out environment
  247:      unless (-e $source.'.meta') {
  248:         $metadatafields{'author'}=$ENV{'environment.firstname'}.' '.
  249: 	                          $ENV{'environment.middlename'}.' '.
  250: 		                  $ENV{'environment.lastname'}.' '.
  251: 		                  $ENV{'environment.generation'};
  252:         $metadatafields{'author'}=~s/\s+/ /g;
  253:         $metadatafields{'author'}=~s/\s+$//;
  254:         $metadatafields{'owner'}=$ENV{'user.name'}.'@'.$ENV{'user.domain'};
  255: 
  256: # ------------------------------------------------ Check out directory hierachy
  257: 
  258:         my $thisdisfn=$source;
  259:         $thisdisfn=~s/^\/home\/$ENV{'user.name'}\///;
  260: 
  261:         my @urlparts=split(/\//,$thisdisfn);
  262:         $#urlparts--;
  263: 
  264:         my $currentpath='/home/'.$ENV{'user.name'}.'/';
  265: 
  266:         map {
  267: 	    $currentpath.=$_.'/';
  268:             $scrout.=&metaread($logfile,$currentpath.'default.meta');
  269:         } @urlparts;
  270: 
  271: # ------------------- Clear out parameters and stores (there should not be any)
  272: 
  273:         map {
  274: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
  275: 		delete $metadatafields{$_};
  276:             }
  277:         } keys %metadatafields;
  278: 
  279:     } else {
  280: # ---------------------- Read previous metafile, remember parameters and stores
  281: 
  282:         $scrout.=&metaread($logfile,$source.'.meta');
  283: 
  284:         map {
  285: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
  286:                 $oldparmstores{$_}=1;
  287: 		delete $metadatafields{$_};
  288:             }
  289:         } keys %metadatafields;
  290:         
  291:     }
  292: 
  293: # -------------------------------------------------- Parse content for metadata
  294: 
  295:         $allmeta=Apache::lonxml::xmlparse('meta',$content);
  296:         &metaeval($allmeta);
  297: 
  298: # ---------------- Find and document discrepancies in the parameters and stores
  299: 
  300:         my $chparms='';
  301:         map {
  302: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
  303:                 unless ($_=~/\.\w+$/) { 
  304:                    unless ($oldparmstores{$_}) {
  305: 		      print $logfile 'New: '.$_."\n";
  306:                       $chparms.=$_.' ';
  307:                    }
  308: 	        }
  309:             }
  310:         } sort keys %metadatafields;
  311:         if ($chparms) {
  312: 	    $scrout.='<p><b>New parameters or stored values:</b> '.
  313:                      $chparms;
  314:         }
  315: 
  316:         my $chparms='';
  317:         map {
  318: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
  319:                 unless (($metadatafields{$_.'.name'}) || ($_=~/\.\w+$/)) {
  320: 		    print $logfile 'Obsolete: '.$_."\n";
  321:                     $chparms.=$_.' ';
  322:                 }
  323:             }
  324:         } sort keys %oldparmstores;
  325:         if ($chparms) {
  326: 	    $scrout.='<p><b>Obsolete parameters or stored values:</b> '.
  327:                      $chparms;
  328:         }
  329:     }
  330: # ------------------------------------------------------- Now have all metadata
  331: 
  332:         $scrout.=
  333:      '<form action="/adm/publish" method="post">'.
  334:           &hiddenfield('phase','two').
  335:           &hiddenfield('filename',$ENV{'form.filename'}).
  336: 	  &hiddenfield('allmeta',&Apache::lonnet::escape($allmeta)).
  337:           &textfield('Title','title',$metadatafields{'title'}).
  338:           &textfield('Author(s)','author',$metadatafields{'author'}).
  339: 	  &textfield('Subject','subject',$metadatafields{'subject'});
  340: 
  341: # --------------------------------------------------- Scan content for keywords
  342: 
  343: 	my $keywordout='<p><b>Keywords:</b><br><table border=2><tr>';
  344:         my $colcount=0;
  345:         
  346: 	{
  347: 	    my $textonly=$content;
  348:             $textonly=~s/\<script[^\<]+\<\/script\>//g;
  349:             $textonly=~s/\<m\>[^\<]+\<\/m\>//g;
  350:             $textonly=~s/\<[^\>]*\>//g;
  351:             $textonly=~tr/A-Z/a-z/;
  352:             $textonly=~s/[\$\&][a-z]\w*//g;
  353:             $textonly=~s/[^a-z\s]//g;
  354: 
  355:             my %keywords=();
  356:             map {
  357: 		unless ($nokey{$_}) {
  358:                    $keywords{$_}=1;
  359:                 } 
  360:             } ($textonly=~m/(\w+)/g);
  361: 
  362:             map {
  363: 		$keywords{$_}=1;
  364:             } split(/\W+/,$metadatafields{'keywords'});
  365: 
  366:             map {
  367:                 $keywordout.='<td><input type=checkbox name="key.'.$_.'"';
  368:                 if ($metadatafields{'keywords'}=~/$_/) { 
  369:                    $keywordout.=' checked'; 
  370:                 }
  371:                 $keywordout.='>'.$_.'</td>';
  372:                 if ($colcount>10) {
  373: 		    $keywordout.="</tr><tr>\n";
  374:                     $colcount=0;
  375:                 }
  376:                 $colcount++;
  377:             } sort keys %keywords;
  378:             $keywordout.='</tr></table>';
  379: 
  380:         }         
  381:         
  382: 	$scrout.=$keywordout;
  383: 
  384:         $scrout.=&textfield('Additional Keywords','addkey','');
  385: 
  386:         $scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
  387: 
  388:         $scrout.=
  389:              '<p><b>Abstract:</b><br><textarea cols=80 rows=5 name=abstract>'.
  390:               $metadatafields{'abstract'}.'</textarea>';
  391: 
  392: 	$source=~/\.(\w+)$/;
  393: 
  394: 	$scrout.=&hiddenfield('mime',$1);
  395: 
  396:         $scrout.=&selectbox('Language','language',
  397:                             $metadatafields{'language'},%language);
  398: 
  399:         unless ($metadatafields{'creationdate'}) {
  400: 	    $metadatafields{'creationdate'}=time;
  401:         }
  402:         $scrout.=&hiddenfield('creationdate',$metadatafields{'creationdate'});
  403: 
  404:         $scrout.=&hiddenfield('lastrevisiondate',time);
  405: 
  406: 			   
  407: 	$scrout.=&textfield('Publisher/Owner','owner',
  408:                             $metadatafields{'owner'});
  409: 
  410:         $scrout.=&selectbox('Copyright/Distribution','copyright',
  411:                             $metadatafields{'copyright'},%cprtag);
  412: 
  413:     return $scrout.
  414:       '<p><input type="submit" value="Finalize Publication"></form>';
  415: }
  416: 
  417: # -------------------------------------------------------- Publication Step Two
  418: 
  419: sub phasetwo {
  420: 
  421:     my ($source,$target,$style)=@_;
  422:     my $logfile;
  423:     my $scrout='';
  424: 
  425:     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
  426: 	return 
  427:          '<font color=red>No write permission to user directory, FAIL</font>';
  428:     }
  429:     print $logfile 
  430: "\n================= Publish ".localtime()." Phase Two  ================\n";
  431: 
  432:      %metadatafields=();
  433:      %metadatakeys=();
  434: 
  435:      &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
  436: 
  437:      $metadatafields{'title'}=$ENV{'form.title'};
  438:      $metadatafields{'author'}=$ENV{'form.author'};
  439:      $metadatafields{'subject'}=$ENV{'form.subject'};
  440:      $metadatafields{'notes'}=$ENV{'form.notes'};
  441:      $metadatafields{'abstract'}=$ENV{'form.abstract'};
  442:      $metadatafields{'mime'}=$ENV{'form.mime'};
  443:      $metadatafields{'language'}=$ENV{'form.language'};
  444:      $metadatafields{'creationdate'}=$ENV{'form.creationdate'};
  445:      $metadatafields{'lastrevisiondate'}=$ENV{'form.lastrevisiondate'};
  446:      $metadatafields{'owner'}=$ENV{'form.owner'};
  447:      $metadatafields{'copyright'}=$ENV{'form.copyright'};
  448: 
  449:      my $allkeywords=$ENV{'form.addkey'};
  450:      map {
  451:          if ($_=~/^form\.key\.(\w+)/) {
  452: 	     $allkeywords.=','.$1;
  453:          }
  454:      } keys %ENV;
  455:      $allkeywords=~s/\W+/\,/;
  456:      $allkeywords=~s/^\,//;
  457:      $metadatafields{'keywords'}=$allkeywords;
  458:  
  459:      {
  460:        print $logfile "\nWrite metadata file for ".$source;
  461:        my $mfh;
  462:        unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
  463: 	return 
  464:          '<font color=red>Could not write metadata, FAIL</font>';
  465:        }    
  466:        map {
  467: 	 unless ($_=~/\./) {
  468:            my $unikey=$_;
  469:            $unikey=~/^([A-Za-z]+)/;
  470:            my $tag=$1;
  471:            $tag=~tr/A-Z/a-z/;
  472:            print $mfh "\n\<$tag";
  473:            map {
  474:                my $value=$metadatafields{$unikey.'.'.$_};
  475:                $value=~s/\"/\'\'/g;
  476:                print $mfh ' '.$_.'="'.$value.'"';
  477:            } split(/\,/,$metadatakeys{$unikey});
  478: 	   print $mfh '>'.$metadatafields{$unikey}.'</'.$tag.'>';
  479:          }
  480:        } sort keys %metadatafields;
  481:        $scrout.='<p>Wrote Metadata';
  482:        print $logfile "\nWrote metadata";
  483:      }
  484: 
  485: # ----------------------------------------------------------- Copy old versions
  486:    
  487: if (-e $target) {
  488:     my $filename;
  489:     my $maxversion=0;
  490:     $target=~/(.*)\/([^\/]+)\.(\w+)$/;
  491:     my $srcf=$2;
  492:     my $srct=$3;
  493:     my $srcd=$1;
  494:     unless ($srcd=~/^\/home\/httpd\/html\/res/) {
  495: 	print $logfile "\nPANIC: Target dir is ".$srcd;
  496:         return "<font color=red>Invalid target directory, FAIL</font>";
  497:     }
  498:     opendir(DIR,$srcd);
  499:     while ($filename=readdir(DIR)) {
  500:        if ($filename=~/$srcf\.(\d+)\.$srct$/) {
  501: 	   $maxversion=($1>$maxversion)?$1:$maxversion;
  502:        }
  503:     }
  504:     closedir(DIR);
  505:     $maxversion++;
  506:     $scrout.='<p>Creating old version '.$maxversion;
  507:     print $logfile "\nCreating old version ".$maxversion;
  508: 
  509:     my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
  510: 
  511:         if (copy($target,$copyfile)) {
  512: 	    print $logfile "Copied old target to ".$copyfile."\n";
  513:             $scrout.='<p>Copied old target file';
  514:         } else {
  515: 	    print $logfile "Unable to write ".$copyfile.':'.$!."\n";
  516:            return "<font color=red>Failed to copy old target, $!, FAIL</font>";
  517:         }
  518: 
  519: # --------------------------------------------------------------- Copy Metadata
  520: 
  521: 	$copyfile=$copyfile.'.meta';
  522: 
  523:         if (copy($target.'.meta',$copyfile)) {
  524: 	    print $logfile "Copied old target metadata to ".$copyfile."\n";
  525:             $scrout.='<p>Copied old metadata';
  526:         } else {
  527: 	    print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
  528:             if (-e $target.'.meta') {
  529:                return 
  530:        "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
  531: 	    }
  532:         }
  533: 
  534: 
  535: } else {
  536:     $scrout.='<p>Initial version';
  537:     print $logfile "\nInitial version";
  538: }
  539: 
  540: # ---------------------------------------------------------------- Write Source
  541: 	my $copyfile=$target;
  542: 
  543:            my @parts=split(/\//,$copyfile);
  544:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
  545: 
  546:            my $count;
  547:            for ($count=5;$count<$#parts;$count++) {
  548:                $path.="/$parts[$count]";
  549:                if ((-e $path)!=1) {
  550:                    print $logfile "\nCreating directory ".$path;
  551:                    $scrout.='<p>Created directory '.$parts[$count];
  552: 		   mkdir($path,0777);
  553:                }
  554:            }
  555: 
  556:         if (copy($source,$copyfile)) {
  557: 	    print $logfile "Copied original source to ".$copyfile."\n";
  558:             $scrout.='<p>Copied source file';
  559:         } else {
  560: 	    print $logfile "Unable to write ".$copyfile.':'.$!."\n";
  561:             return "<font color=red>Failed to copy source, $!, FAIL</font>";
  562:         }
  563: 
  564: # --------------------------------------------------------------- Copy Metadata
  565: 
  566:         $copyfile=$copyfile.'.meta';
  567: 
  568:         if (copy($source.'.meta',$copyfile)) {
  569: 	    print $logfile "Copied original metadata to ".$copyfile."\n";
  570:             $scrout.='<p>Copied metadata';
  571:         } else {
  572: 	    print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
  573:             return 
  574:           "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
  575:         }
  576: 
  577: # --------------------------------------------------- Send update notifications
  578: 
  579: {
  580: 
  581:     my $filename;
  582:  
  583:     $target=~/(.*)\/([^\/]+)$/;
  584:     my $srcf=$2;
  585:     opendir(DIR,$1);
  586:     while ($filename=readdir(DIR)) {
  587:        if ($filename=~/$srcf\.(\w+)$/) {
  588: 	   my $subhost=$1;
  589:            if ($subhost ne 'meta') {
  590: 	       $scrout.='<p>Notifying host '.$subhost.':';
  591:                print $logfile "\nNotifying host '.$subhost.':'";
  592:                my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
  593:                $scrout.=$reply;
  594:                print $logfile $reply;              
  595:            }
  596:        }
  597:     }
  598:     closedir(DIR);
  599: 
  600: }
  601: 
  602: # ---------------------------------------- Send update notifications, meta only
  603: 
  604: {
  605: 
  606:     my $filename;
  607:  
  608:     $target=~/(.*)\/([^\/]+)$/;
  609:     my $srcf=$2.'.meta';
  610:     opendir(DIR,$1);
  611:     while ($filename=readdir(DIR)) {
  612:        if ($filename=~/$srcf\.(\w+)$/) {
  613: 	   my $subhost=$1;
  614:            if ($subhost ne 'meta') {
  615: 	       $scrout.=
  616:                 '<p>Notifying host for metadata only '.$subhost.':';
  617:                print $logfile 
  618:                 "\nNotifying host for metadata only '.$subhost.':'";
  619:                my $reply=&Apache::lonnet::critical(
  620:                                 'update:'.$target.'.meta',$subhost);
  621:                $scrout.=$reply;
  622:                print $logfile $reply;              
  623:            }
  624:        }
  625:     }
  626:     closedir(DIR);
  627: 
  628: }
  629: 
  630: # ------------------------------------------------ Provide link to new resource
  631: 
  632:     my $thisdistarget=$target;
  633:     $thisdistarget=~s/^$docroot//;
  634: 
  635:     my $thissrc=$source;
  636:     $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
  637: 
  638:     my $thissrcdir=$thissrc;
  639:     $thissrcdir=~s/\/[^\/]+$/\//;
  640: 
  641: 
  642:     return $scrout.
  643:       '<hr><a href="'.$thisdistarget.'"><font size=+2>View Target</font></a>'.
  644:       '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
  645:       '<p><a href="'.$thissrcdir.
  646:       '"><font size=+2>Back to Source Directory</font></a>';
  647: 
  648: }
  649: 
  650: # ================================================================ Main Handler
  651: 
  652: sub handler {
  653:   my $r=shift;
  654: 
  655:   if ($r->header_only) {
  656:      $r->content_type('text/html');
  657:      $r->send_http_header;
  658:      return OK;
  659:   }
  660: 
  661: # -------------------------------------------------------------- Check filename
  662: 
  663:   my $fn=$ENV{'form.filename'};
  664: 
  665:   unless ($fn) { 
  666:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  667:          ' trying to publish empty filename', $r->filename); 
  668:      return HTTP_NOT_FOUND;
  669:   } 
  670: 
  671:   unless ($ENV{'user.home'} eq $r->dir_config('lonHostID')) {
  672:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  673:          ' trying to publish file '.$ENV{'form.filename'}.
  674:          ' ('.$fn.') - not homeserver ('.$ENV{'user.home'}.')', 
  675:          $r->filename); 
  676:      return HTTP_NOT_ACCEPTABLE;
  677:   }
  678: 
  679:   $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
  680: 
  681:   my $targetdir='';
  682:   $docroot=$r->dir_config('lonDocRoot'); 
  683:   if ($1 ne $ENV{'user.name'}) {
  684:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  685:          ' trying to publish unowned file '.$ENV{'form.filename'}.
  686:          ' ('.$fn.')', 
  687:          $r->filename); 
  688:      return HTTP_NOT_ACCEPTABLE;
  689:   } else {
  690:       $targetdir=$docroot.'/res/'.$ENV{'user.domain'};
  691:   }
  692:                                  
  693:   
  694:   unless (-e $fn) { 
  695:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  696:          ' trying to publish non-existing file '.$ENV{'form.filename'}.
  697:          ' ('.$fn.')', 
  698:          $r->filename); 
  699:      return HTTP_NOT_FOUND;
  700:   } 
  701: 
  702: unless ($ENV{'form.phase'} eq 'two') {
  703: 
  704: # --------------------------------- File is there and owned, init lookup tables
  705: 
  706:   %addid=();
  707: 
  708:   {
  709:       my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
  710:       while (<$fh>=~/(\w+)\s+(\w+)/) {
  711:           $addid{$1}=$2;
  712:       }
  713:   }
  714: 
  715:   %nokey=();
  716: 
  717:   {
  718:      my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
  719:       map {
  720:           my $word=$_;
  721:           chomp($word);
  722:           $nokey{$word}=1;
  723:       } <$fh>;
  724:   }
  725: 
  726:   %language=();
  727: 
  728:   {
  729:      my $fh=Apache::File->new($r->dir_config('lonTabDir').'/language.tab');
  730:       map {
  731:           $_=~/(\w+)\s+([\w\s\-]+)/;
  732:           $language{$1}=$2;
  733:       } <$fh>;
  734:   }
  735: 
  736:   %cprtag=();
  737: 
  738:   {
  739:      my $fh=Apache::File->new($r->dir_config('lonIncludes').'/copyright.tab');
  740:       map {
  741:           $_=~/(\w+)\s+([\w\s\-]+)/;
  742:           $cprtag{$1}=$2;
  743:       } <$fh>;
  744:   }
  745: 
  746: }
  747: 
  748: # ----------------------------------------------------------- Start page output
  749: 
  750:   $r->content_type('text/html');
  751:   $r->send_http_header;
  752: 
  753:   $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
  754:   $r->print(
  755:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
  756:   my $thisfn=$fn;
  757:    
  758: # ------------------------------------------------------------- Individual file
  759:   {
  760:       $thisfn=~/\.(\w+)$/;
  761:       my $thistype=$1;
  762:       my $thisembstyle=&Apache::lonnet::fileembstyle($thistype);
  763: 
  764:       my $thistarget=$thisfn;
  765:       
  766:       $thistarget=~s/^\/home/$targetdir/;
  767:       $thistarget=~s/\/public\_html//;
  768: 
  769:       my $thisdistarget=$thistarget;
  770:       $thisdistarget=~s/^$docroot//;
  771: 
  772:       my $thisdisfn=$thisfn;
  773:       $thisdisfn=~s/^\/home\/$ENV{'user.name'}\/public_html\///;
  774: 
  775:       $r->print('<h2>Publishing '.
  776:         &Apache::lonnet::filedescription($thistype).' <tt>'.
  777:         $thisdisfn.'</tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
  778:   
  779: # ------------ We are publishing from $thisfn to $thistarget with $thisembstyle
  780: 
  781:        unless ($ENV{'form.phase'} eq 'two') {
  782:           $r->print('<hr>'.&publish($thisfn,$thistarget,$thisembstyle));
  783:        } else {
  784:           $r->print('<hr>'.&phasetwo($thisfn,$thistarget,$thisembstyle));      
  785:        }  
  786: 
  787:   }
  788:   $r->print('</body></html>');
  789: 
  790:   return OK;
  791: }
  792: 
  793: 1;
  794: __END__
  795: 
  796: 
  797: 
  798: 
  799: 
  800: 
  801: 

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