File:  [LON-CAPA] / loncom / publisher / lonpublisher.pm
Revision 1.31: download - view: text, annotated - select for diffs
Sat Jun 23 18:26:40 2001 UTC (22 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
More actions in construction space

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

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