File:  [LON-CAPA] / loncom / publisher / lonpublisher.pm
Revision 1.32: download - view: text, annotated - select for diffs
Tue Aug 7 12:07:39 2001 UTC (22 years, 10 months ago) by www
Branches: MAIN
CVS tags: HEAD
Towards making packages work

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

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