File:  [LON-CAPA] / loncom / publisher / lonpublisher.pm
Revision 1.35: download - view: text, annotated - select for diffs
Sat Aug 11 18:51:40 2001 UTC (22 years, 10 months ago) by www
Branches: MAIN
CVS tags: HEAD
Does URL fixup.

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

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