File:  [LON-CAPA] / loncom / publisher / lonpublisher.pm
Revision 1.33: download - view: text, annotated - select for diffs
Tue Aug 7 21:27:06 2001 UTC (22 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
Gets rid of spurious "obsolete" message for packages

    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'}) ||
  344:                         ($metadatafields{$_.'.package'}) || ($_=~/\.\w+$/)) {
  345: 		    print $logfile 'Obsolete: '.$_."\n";
  346:                     $chparms.=$_.' ';
  347:                 }
  348:             }
  349:         } sort keys %oldparmstores;
  350:         if ($chparms) {
  351: 	    $scrout.='<p><b>Obsolete parameters or stored values:</b> '.
  352:                      $chparms;
  353:         }
  354:     }
  355: # ------------------------------------------------------- Now have all metadata
  356: 
  357:         $scrout.=
  358:      '<form action="/adm/publish" method="post">'.
  359:           &hiddenfield('phase','two').
  360:           &hiddenfield('filename',$ENV{'form.filename'}).
  361: 	  &hiddenfield('allmeta',&Apache::lonnet::escape($allmeta)).
  362:           &textfield('Title','title',$metadatafields{'title'}).
  363:           &textfield('Author(s)','author',$metadatafields{'author'}).
  364: 	  &textfield('Subject','subject',$metadatafields{'subject'});
  365: 
  366: # --------------------------------------------------- Scan content for keywords
  367: 
  368: 	my $keywordout='<p><b>Keywords:</b><br><table border=2><tr>';
  369:         my $colcount=0;
  370:         
  371: 	{
  372: 	    my $textonly=$content;
  373:             $textonly=~s/\<script[^\<]+\<\/script\>//g;
  374:             $textonly=~s/\<m\>[^\<]+\<\/m\>//g;
  375:             $textonly=~s/\<[^\>]*\>//g;
  376:             $textonly=~tr/A-Z/a-z/;
  377:             $textonly=~s/[\$\&][a-z]\w*//g;
  378:             $textonly=~s/[^a-z\s]//g;
  379: 
  380:             my %keywords=();
  381:             map {
  382: 		unless ($nokey{$_}) {
  383:                    $keywords{$_}=1;
  384:                 } 
  385:             } ($textonly=~m/(\w+)/g);
  386: 
  387:             map {
  388: 		$keywords{$_}=1;
  389:             } split(/\W+/,$metadatafields{'keywords'});
  390: 
  391:             map {
  392:                 $keywordout.='<td><input type=checkbox name="key.'.$_.'"';
  393:                 if ($metadatafields{'keywords'}=~/$_/) { 
  394:                    $keywordout.=' checked'; 
  395:                 }
  396:                 $keywordout.='>'.$_.'</td>';
  397:                 if ($colcount>10) {
  398: 		    $keywordout.="</tr><tr>\n";
  399:                     $colcount=0;
  400:                 }
  401:                 $colcount++;
  402:             } sort keys %keywords;
  403:             $keywordout.='</tr></table>';
  404: 
  405:         }         
  406:         
  407: 	$scrout.=$keywordout;
  408: 
  409:         $scrout.=&textfield('Additional Keywords','addkey','');
  410: 
  411:         $scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
  412: 
  413:         $scrout.=
  414:              '<p><b>Abstract:</b><br><textarea cols=80 rows=5 name=abstract>'.
  415:               $metadatafields{'abstract'}.'</textarea>';
  416: 
  417: 	$source=~/\.(\w+)$/;
  418: 
  419: 	$scrout.=&hiddenfield('mime',$1);
  420: 
  421:         $scrout.=&selectbox('Language','language',
  422:                             $metadatafields{'language'},%language);
  423: 
  424:         unless ($metadatafields{'creationdate'}) {
  425: 	    $metadatafields{'creationdate'}=time;
  426:         }
  427:         $scrout.=&hiddenfield('creationdate',$metadatafields{'creationdate'});
  428: 
  429:         $scrout.=&hiddenfield('lastrevisiondate',time);
  430: 
  431: 			   
  432: 	$scrout.=&textfield('Publisher/Owner','owner',
  433:                             $metadatafields{'owner'});
  434: 
  435:         $scrout.=&selectbox('Copyright/Distribution','copyright',
  436:                             $metadatafields{'copyright'},%cprtag);
  437: 
  438:     return $scrout.
  439:       '<p><input type="submit" value="Finalize Publication"></form>';
  440: }
  441: 
  442: # -------------------------------------------------------- Publication Step Two
  443: 
  444: sub phasetwo {
  445: 
  446:     my ($source,$target,$style,$distarget)=@_;
  447:     my $logfile;
  448:     my $scrout='';
  449: 
  450:     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
  451: 	return 
  452:          '<font color=red>No write permission to user directory, FAIL</font>';
  453:     }
  454:     print $logfile 
  455: "\n================= Publish ".localtime()." Phase Two  ================\n";
  456: 
  457:      %metadatafields=();
  458:      %metadatakeys=();
  459: 
  460:      &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
  461: 
  462:      $metadatafields{'title'}=$ENV{'form.title'};
  463:      $metadatafields{'author'}=$ENV{'form.author'};
  464:      $metadatafields{'subject'}=$ENV{'form.subject'};
  465:      $metadatafields{'notes'}=$ENV{'form.notes'};
  466:      $metadatafields{'abstract'}=$ENV{'form.abstract'};
  467:      $metadatafields{'mime'}=$ENV{'form.mime'};
  468:      $metadatafields{'language'}=$ENV{'form.language'};
  469:      $metadatafields{'creationdate'}=$ENV{'form.creationdate'};
  470:      $metadatafields{'lastrevisiondate'}=$ENV{'form.lastrevisiondate'};
  471:      $metadatafields{'owner'}=$ENV{'form.owner'};
  472:      $metadatafields{'copyright'}=$ENV{'form.copyright'};
  473: 
  474:      my $allkeywords=$ENV{'form.addkey'};
  475:      map {
  476:          if ($_=~/^form\.key\.(\w+)/) {
  477: 	     $allkeywords.=','.$1;
  478:          }
  479:      } keys %ENV;
  480:      $allkeywords=~s/\W+/\,/;
  481:      $allkeywords=~s/^\,//;
  482:      $metadatafields{'keywords'}=$allkeywords;
  483:  
  484:      {
  485:        print $logfile "\nWrite metadata file for ".$source;
  486:        my $mfh;
  487:        unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
  488: 	return 
  489:          '<font color=red>Could not write metadata, FAIL</font>';
  490:        }    
  491:        map {
  492: 	 unless ($_=~/\./) {
  493:            my $unikey=$_;
  494:            $unikey=~/^([A-Za-z]+)/;
  495:            my $tag=$1;
  496:            $tag=~tr/A-Z/a-z/;
  497:            print $mfh "\n\<$tag";
  498:            map {
  499:                my $value=$metadatafields{$unikey.'.'.$_};
  500:                $value=~s/\"/\'\'/g;
  501:                print $mfh ' '.$_.'="'.$value.'"';
  502:            } split(/\,/,$metadatakeys{$unikey});
  503: 	   print $mfh '>'.$metadatafields{$unikey}.'</'.$tag.'>';
  504:          }
  505:        } sort keys %metadatafields;
  506:        $scrout.='<p>Wrote Metadata';
  507:        print $logfile "\nWrote metadata";
  508:      }
  509: 
  510: # -------------------------------- Synchronize entry with SQL metadata database
  511:     my %perlvar;
  512:     open (CONFIG,"/etc/httpd/conf/access.conf") || die "Can't read access.conf";
  513:     my $configline;
  514:     while ($configline=<CONFIG>) {
  515: 	if ($configline =~ /PerlSetVar/) {
  516: 	    my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
  517: 	    chomp($varvalue);
  518: 	    $perlvar{$varname}=$varvalue;
  519: 	}
  520:     }
  521:     close(CONFIG);
  522: 
  523:     my $warning;
  524:     my $dbh;
  525:     {
  526: 	unless (
  527: 		$dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
  528: 		) { 
  529: 	    $warning='<font color=red>WARNING: Cannot connect to '.
  530: 		'database!</font>';
  531: 	}
  532: 	else {
  533: 	    my %sqldatafields;
  534: 	    $sqldatafields{'url'}=$distarget;
  535: 	    my $sth=$dbh->prepare(
  536: 				  'delete from metadata where url like binary'.
  537: 				  '"'.$sqldatafields{'url'}.'"');
  538: 	    $sth->execute();
  539: 	    map {my $field=$metadatafields{$_}; $field=~s/\"/\'\'/g; 
  540: 		 $sqldatafields{$_}=$field;}
  541: 	    ('title','author','subject','keywords','notes','abstract',
  542: 	     'mime','language','creationdate','lastrevisiondate','owner',
  543: 	     'copyright');
  544: 	    
  545: 	    $sth=$dbh->prepare('insert into metadata values ('.
  546: 			       '"'.delete($sqldatafields{'title'}).'"'.','.
  547: 			       '"'.delete($sqldatafields{'author'}).'"'.','.
  548: 			       '"'.delete($sqldatafields{'subject'}).'"'.','.
  549: 			       '"'.delete($sqldatafields{'url'}).'"'.','.
  550: 			       '"'.delete($sqldatafields{'keywords'}).'"'.','.
  551: 			       '"'.'current'.'"'.','.
  552: 			       '"'.delete($sqldatafields{'notes'}).'"'.','.
  553: 			       '"'.delete($sqldatafields{'abstract'}).'"'.','.
  554: 			       '"'.delete($sqldatafields{'mime'}).'"'.','.
  555: 			       '"'.delete($sqldatafields{'language'}).'"'.','.
  556: 			       '"'.
  557: 			       sqltime(delete($sqldatafields{'creationdate'}))
  558: 			       .'"'.','.
  559: 			       '"'.
  560: 			       sqltime(delete(
  561: 			       $sqldatafields{'lastrevisiondate'})).'"'.','.
  562: 			       '"'.delete($sqldatafields{'owner'}).'"'.','.
  563: 			       '"'.delete(
  564: 			       $sqldatafields{'copyright'}).'"'.')');
  565: 	    $sth->execute();
  566: 	    $dbh->disconnect;
  567: 	    $scrout.='<p>Synchronized SQL metadata database';
  568: 	    print $logfile "\nSynchronized SQL metadata database";
  569: 	}
  570:     }
  571: 
  572: 
  573: # ----------------------------------------------------------- Copy old versions
  574:    
  575: if (-e $target) {
  576:     my $filename;
  577:     my $maxversion=0;
  578:     $target=~/(.*)\/([^\/]+)\.(\w+)$/;
  579:     my $srcf=$2;
  580:     my $srct=$3;
  581:     my $srcd=$1;
  582:     unless ($srcd=~/^\/home\/httpd\/html\/res/) {
  583: 	print $logfile "\nPANIC: Target dir is ".$srcd;
  584:         return "<font color=red>Invalid target directory, FAIL</font>";
  585:     }
  586:     opendir(DIR,$srcd);
  587:     while ($filename=readdir(DIR)) {
  588:        if ($filename=~/$srcf\.(\d+)\.$srct$/) {
  589: 	   $maxversion=($1>$maxversion)?$1:$maxversion;
  590:        }
  591:     }
  592:     closedir(DIR);
  593:     $maxversion++;
  594:     $scrout.='<p>Creating old version '.$maxversion;
  595:     print $logfile "\nCreating old version ".$maxversion;
  596: 
  597:     my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
  598: 
  599:         if (copy($target,$copyfile)) {
  600: 	    print $logfile "Copied old target to ".$copyfile."\n";
  601:             $scrout.='<p>Copied old target file';
  602:         } else {
  603: 	    print $logfile "Unable to write ".$copyfile.':'.$!."\n";
  604:            return "<font color=red>Failed to copy old target, $!, FAIL</font>";
  605:         }
  606: 
  607: # --------------------------------------------------------------- Copy Metadata
  608: 
  609: 	$copyfile=$copyfile.'.meta';
  610: 
  611:         if (copy($target.'.meta',$copyfile)) {
  612: 	    print $logfile "Copied old target metadata to ".$copyfile."\n";
  613:             $scrout.='<p>Copied old metadata';
  614:         } else {
  615: 	    print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
  616:             if (-e $target.'.meta') {
  617:                return 
  618:        "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
  619: 	    }
  620:         }
  621: 
  622: 
  623: } else {
  624:     $scrout.='<p>Initial version';
  625:     print $logfile "\nInitial version";
  626: }
  627: 
  628: # ---------------------------------------------------------------- Write Source
  629: 	my $copyfile=$target;
  630: 
  631:            my @parts=split(/\//,$copyfile);
  632:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
  633: 
  634:            my $count;
  635:            for ($count=5;$count<$#parts;$count++) {
  636:                $path.="/$parts[$count]";
  637:                if ((-e $path)!=1) {
  638:                    print $logfile "\nCreating directory ".$path;
  639:                    $scrout.='<p>Created directory '.$parts[$count];
  640: 		   mkdir($path,0777);
  641:                }
  642:            }
  643: 
  644:         if (copy($source,$copyfile)) {
  645: 	    print $logfile "Copied original source to ".$copyfile."\n";
  646:             $scrout.='<p>Copied source file';
  647:         } else {
  648: 	    print $logfile "Unable to write ".$copyfile.':'.$!."\n";
  649:             return "<font color=red>Failed to copy source, $!, FAIL</font>";
  650:         }
  651: 
  652: # --------------------------------------------------------------- Copy Metadata
  653: 
  654:         $copyfile=$copyfile.'.meta';
  655: 
  656:         if (copy($source.'.meta',$copyfile)) {
  657: 	    print $logfile "Copied original metadata to ".$copyfile."\n";
  658:             $scrout.='<p>Copied metadata';
  659:         } else {
  660: 	    print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
  661:             return 
  662:           "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
  663:         }
  664: 
  665: # --------------------------------------------------- Send update notifications
  666: 
  667: {
  668: 
  669:     my $filename;
  670:  
  671:     $target=~/(.*)\/([^\/]+)$/;
  672:     my $srcf=$2;
  673:     opendir(DIR,$1);
  674:     while ($filename=readdir(DIR)) {
  675:        if ($filename=~/$srcf\.(\w+)$/) {
  676: 	   my $subhost=$1;
  677:            if ($subhost ne 'meta') {
  678: 	       $scrout.='<p>Notifying host '.$subhost.':';
  679:                print $logfile "\nNotifying host '.$subhost.':'";
  680:                my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
  681:                $scrout.=$reply;
  682:                print $logfile $reply;              
  683:            }
  684:        }
  685:     }
  686:     closedir(DIR);
  687: 
  688: }
  689: 
  690: # ---------------------------------------- Send update notifications, meta only
  691: 
  692: {
  693: 
  694:     my $filename;
  695:  
  696:     $target=~/(.*)\/([^\/]+)$/;
  697:     my $srcf=$2.'.meta';
  698:     opendir(DIR,$1);
  699:     while ($filename=readdir(DIR)) {
  700:        if ($filename=~/$srcf\.(\w+)$/) {
  701: 	   my $subhost=$1;
  702:            if ($subhost ne 'meta') {
  703: 	       $scrout.=
  704:                 '<p>Notifying host for metadata only '.$subhost.':';
  705:                print $logfile 
  706:                 "\nNotifying host for metadata only '.$subhost.':'";
  707:                my $reply=&Apache::lonnet::critical(
  708:                                 'update:'.$target.'.meta',$subhost);
  709:                $scrout.=$reply;
  710:                print $logfile $reply;              
  711:            }
  712:        }
  713:     }
  714:     closedir(DIR);
  715: 
  716: }
  717: 
  718: # ------------------------------------------------ Provide link to new resource
  719: 
  720:     my $thisdistarget=$target;
  721:     $thisdistarget=~s/^$docroot//;
  722: 
  723:     my $thissrc=$source;
  724:     $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
  725: 
  726:     my $thissrcdir=$thissrc;
  727:     $thissrcdir=~s/\/[^\/]+$/\//;
  728: 
  729: 
  730:     return $warning.$scrout.
  731:       '<hr><a href="'.$thisdistarget.'"><font size=+2>View Target</font></a>'.
  732:       '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
  733:       '<p><a href="'.$thissrcdir.
  734:       '"><font size=+2>Back to Source Directory</font></a>';
  735: 
  736: }
  737: 
  738: # ================================================================ Main Handler
  739: 
  740: sub handler {
  741:   my $r=shift;
  742: 
  743:   if ($r->header_only) {
  744:      $r->content_type('text/html');
  745:      $r->send_http_header;
  746:      return OK;
  747:   }
  748: 
  749: # -------------------------------------------------------------- Check filename
  750: 
  751:   my $fn=$ENV{'form.filename'};
  752: 
  753:   
  754:   unless ($fn) { 
  755:      $r->log_reason($cuname.' at '.$cudom.
  756:          ' trying to publish empty filename', $r->filename); 
  757:      return HTTP_NOT_FOUND;
  758:   } 
  759: 
  760:   ($cuname,$cudom)=
  761:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
  762:   unless (($cuname) && ($cudom)) {
  763:      $r->log_reason($cuname.' at '.$cudom.
  764:          ' trying to publish file '.$ENV{'form.filename'}.
  765:          ' ('.$fn.') - not authorized', 
  766:          $r->filename); 
  767:      return HTTP_NOT_ACCEPTABLE;
  768:   }
  769: 
  770:   unless (&Apache::lonnet::homeserver($cuname,$cudom) 
  771:           eq $r->dir_config('lonHostID')) {
  772:      $r->log_reason($cuname.' at '.$cudom.
  773:          ' trying to publish file '.$ENV{'form.filename'}.
  774:          ' ('.$fn.') - not homeserver ('.
  775:          &Apache::lonnet::homeserver($cuname,$cudom).')', 
  776:          $r->filename); 
  777:      return HTTP_NOT_ACCEPTABLE;
  778:   }
  779: 
  780:   $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
  781: 
  782:   my $targetdir='';
  783:   $docroot=$r->dir_config('lonDocRoot'); 
  784:   if ($1 ne $cuname) {
  785:      $r->log_reason($cuname.' at '.$cudom.
  786:          ' trying to publish unowned file '.$ENV{'form.filename'}.
  787:          ' ('.$fn.')', 
  788:          $r->filename); 
  789:      return HTTP_NOT_ACCEPTABLE;
  790:   } else {
  791:       $targetdir=$docroot.'/res/'.$cudom;
  792:   }
  793:                                  
  794:   
  795:   unless (-e $fn) { 
  796:      $r->log_reason($cuname.' at '.$cudom.
  797:          ' trying to publish non-existing file '.$ENV{'form.filename'}.
  798:          ' ('.$fn.')', 
  799:          $r->filename); 
  800:      return HTTP_NOT_FOUND;
  801:   } 
  802: 
  803: unless ($ENV{'form.phase'} eq 'two') {
  804: 
  805: # --------------------------------- File is there and owned, init lookup tables
  806: 
  807:   %addid=();
  808: 
  809:   {
  810:       my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
  811:       while (<$fh>=~/(\w+)\s+(\w+)/) {
  812:           $addid{$1}=$2;
  813:       }
  814:   }
  815: 
  816:   %nokey=();
  817: 
  818:   {
  819:      my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
  820:       map {
  821:           my $word=$_;
  822:           chomp($word);
  823:           $nokey{$word}=1;
  824:       } <$fh>;
  825:   }
  826: 
  827:   %language=();
  828: 
  829:   {
  830:      my $fh=Apache::File->new($r->dir_config('lonTabDir').'/language.tab');
  831:       map {
  832:           $_=~/(\w+)\s+([\w\s\-]+)/;
  833:           $language{$1}=$2;
  834:       } <$fh>;
  835:   }
  836: 
  837:   %cprtag=();
  838: 
  839:   {
  840:      my $fh=Apache::File->new($r->dir_config('lonIncludes').'/copyright.tab');
  841:       map {
  842:           $_=~/(\w+)\s+([\w\s\-]+)/;
  843:           $cprtag{$1}=$2;
  844:       } <$fh>;
  845:   }
  846: 
  847: }
  848: 
  849: # ----------------------------------------------------------- Start page output
  850: 
  851:   $r->content_type('text/html');
  852:   $r->send_http_header;
  853: 
  854:   $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
  855:   $r->print(
  856:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
  857:   my $thisfn=$fn;
  858:    
  859: # ------------------------------------------------------------- Individual file
  860:   {
  861:       $thisfn=~/\.(\w+)$/;
  862:       my $thistype=$1;
  863:       my $thisembstyle=&Apache::lonnet::fileembstyle($thistype);
  864: 
  865:       my $thistarget=$thisfn;
  866:       
  867:       $thistarget=~s/^\/home/$targetdir/;
  868:       $thistarget=~s/\/public\_html//;
  869: 
  870:       my $thisdistarget=$thistarget;
  871:       $thisdistarget=~s/^$docroot//;
  872: 
  873:       my $thisdisfn=$thisfn;
  874:       $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
  875: 
  876:       $r->print('<h2>Publishing '.
  877:         &Apache::lonnet::filedescription($thistype).' <tt>'.
  878:         $thisdisfn.'</tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
  879:    
  880:        if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
  881:           $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
  882:                '</font></h3>');
  883:       }
  884: 
  885:       if (&Apache::lonnet::fileembstyle($thistype) eq 'ssi') {
  886:           $r->print('<br><a href="/adm/diff?filename=/~'.$cuname.'/'.
  887:                     $thisdisfn.
  888:   	  '&versionone=priv" target=cat>Diffs with Current Version</a><p>');
  889:       }
  890:   
  891: # ------------ We are publishing from $thisfn to $thistarget with $thisembstyle
  892: 
  893:        unless ($ENV{'form.phase'} eq 'two') {
  894:          $r->print(
  895:           '<hr>'.&publish($thisfn,$thistarget,$thisembstyle));
  896:        } else {
  897:          $r->print(
  898:           '<hr>'.&phasetwo($thisfn,$thistarget,$thisembstyle,$thisdistarget)); 
  899:        }  
  900: 
  901:   }
  902:   $r->print('</body></html>');
  903: 
  904:   return OK;
  905: }
  906: 
  907: 1;
  908: __END__
  909: 
  910: 
  911: 
  912: 
  913: 
  914: 
  915: 

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