File:  [LON-CAPA] / loncom / publisher / lonpublisher.pm
Revision 1.88: download - view: text, annotated - select for diffs
Wed Aug 7 19:59:06 2002 UTC (21 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- removing some debugging code

    1: # The LearningOnline Network with CAPA
    2: # Publication Handler
    3: #
    4: # $Id: lonpublisher.pm,v 1.88 2002/08/07 19:59:06 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # 
   29: # (TeX Content Handler
   30: #
   31: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   32: #
   33: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
   34: # 03/23 Guy Albertelli
   35: # 03/24,03/29,04/03 Gerd Kortemeyer
   36: # 04/16/2001 Scott Harrison
   37: # 05/03,05/05,05/07 Gerd Kortemeyer
   38: # 05/28/2001 Scott Harrison
   39: # 06/23,08/07,08/11,8/13,8/17,8/18,8/24,9/26,10/16 Gerd Kortemeyer
   40: # 12/04,12/05 Guy Albertelli
   41: # 12/05 Gerd Kortemeyer
   42: # 12/05 Guy Albertelli
   43: # 12/06,12/07 Gerd Kortemeyer
   44: # 12/15,12/16 Scott Harrison
   45: # 12/25 Gerd Kortemeyer
   46: # YEAR=2002
   47: # 1/16,1/17 Scott Harrison
   48: # 1/17 Gerd Kortemeyer
   49: #
   50: ###
   51: 
   52: ###############################################################################
   53: ##                                                                           ##
   54: ## ORGANIZATION OF THIS PERL MODULE                                          ##
   55: ##                                                                           ##
   56: ## 1. Modules used by this module                                            ##
   57: ## 2. Various subroutines                                                    ##
   58: ## 3. Publication Step One                                                   ##
   59: ## 4. Phase Two                                                              ##
   60: ## 5. Main Handler                                                           ##
   61: ##                                                                           ##
   62: ###############################################################################
   63: 
   64: package Apache::lonpublisher;
   65: 
   66: # ------------------------------------------------- modules used by this module
   67: use strict;
   68: use Apache::File;
   69: use File::Copy;
   70: use Apache::Constants qw(:common :http :methods);
   71: use HTML::LCParser;
   72: use Apache::lonxml;
   73: use Apache::lonhomework;
   74: use Apache::loncacc;
   75: use DBI;
   76: use Apache::lonnet();
   77: use Apache::loncommon();
   78: 
   79: my %addid;
   80: my %nokey;
   81: 
   82: my %metadatafields;
   83: my %metadatakeys;
   84: 
   85: my $docroot;
   86: 
   87: my $cuname;
   88: my $cudom;
   89: 
   90: # ----------------------------------------------- Evaluate string with metadata
   91: sub metaeval {
   92:     my $metastring=shift;
   93:    
   94:         my $parser=HTML::LCParser->new(\$metastring);
   95:         my $token;
   96:         while ($token=$parser->get_token) {
   97:            if ($token->[0] eq 'S') {
   98: 	      my $entry=$token->[1];
   99:               my $unikey=$entry;
  100:               if (defined($token->[2]->{'package'})) { 
  101:                   $unikey.='_package_'.$token->[2]->{'package'};
  102:               } 
  103:               if (defined($token->[2]->{'part'})) { 
  104:                  $unikey.='_'.$token->[2]->{'part'}; 
  105: 	      }
  106:               if (defined($token->[2]->{'id'})) { 
  107:                   $unikey.='_'.$token->[2]->{'id'};
  108:               } 
  109:               if (defined($token->[2]->{'name'})) { 
  110:                  $unikey.='_'.$token->[2]->{'name'}; 
  111: 	      }
  112:               foreach (@{$token->[3]}) {
  113: 		  $metadatafields{$unikey.'.'.$_}=$token->[2]->{$_};
  114:                   if ($metadatakeys{$unikey}) {
  115: 		      $metadatakeys{$unikey}.=','.$_;
  116:                   } else {
  117:                       $metadatakeys{$unikey}=$_;
  118:                   }
  119:               }
  120:               if ($metadatafields{$unikey}) {
  121: 		  my $newentry=$parser->get_text('/'.$entry);
  122:                   unless (($metadatafields{$unikey}=~/$newentry/) ||
  123:                           ($newentry eq '')) {
  124:                      $metadatafields{$unikey}.=', '.$newentry;
  125: 		  }
  126: 	      } else {
  127:                  $metadatafields{$unikey}=$parser->get_text('/'.$entry);
  128:               }
  129:           }
  130:        }
  131: }
  132: 
  133: # -------------------------------------------------------- Read a metadata file
  134: sub metaread {
  135:     my ($logfile,$fn)=@_;
  136:     unless (-e $fn) {
  137: 	print $logfile 'No file '.$fn."\n";
  138:         return '<br><b>No file:</b> <tt>'.$fn.'</tt>';
  139:     }
  140:     print $logfile 'Processing '.$fn."\n";
  141:     my $metastring;
  142:     {
  143:      my $metafh=Apache::File->new($fn);
  144:      $metastring=join('',<$metafh>);
  145:     }
  146:     &metaeval($metastring);
  147:     return '<br><b>Processed file:</b> <tt>'.$fn.'</tt>';
  148: }
  149: 
  150: # ---------------------------- convert 'time' format into a datetime sql format
  151: sub sqltime {
  152:     my $timef=shift @_;
  153:     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  154: 	localtime($timef);
  155:     $mon++; $year+=1900;
  156:     return "$year-$mon-$mday $hour:$min:$sec";
  157: }
  158: 
  159: # --------------------------------------------------------- Various form fields
  160: 
  161: sub textfield {
  162:     my ($title,$name,$value)=@_;
  163:     return "\n<p><b>$title:</b><br>".
  164:            '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
  165: }
  166: 
  167: sub hiddenfield {
  168:     my ($name,$value)=@_;
  169:     return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
  170: }
  171: 
  172: sub selectbox {
  173:     my ($title,$name,$value,$functionref,@idlist)=@_;
  174:     my $uctitle=uc($title);
  175:     my $selout="\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
  176: 	"</b></font><br />".'<select name="'.$name.'">';
  177:     foreach (@idlist) {
  178:         $selout.='<option value=\''.$_.'\'';
  179:         if ($_ eq $value) {
  180: 	    $selout.=' selected>'.&{$functionref}($_).'</option>';
  181: 	}
  182:         else {$selout.='>'.&{$functionref}($_).'</option>';}
  183:     }
  184:     return $selout.'</select>';
  185: }
  186: 
  187: # -------------------------------------------------------- Publication Step One
  188: 
  189: sub urlfixup {
  190:     my ($url,$target)=@_;
  191:     unless ($url) { return ''; }
  192:     #javascript code needs no fixing
  193:     if ($url =~ /^javascript:/i) { return $url; }
  194:     if ($url =~ /^mailto:/i) { return $url; }
  195:     #internal document links need no fixing
  196:     if ($url =~ /^\#/) { return $url; } 
  197:     my ($host)=($url=~/(?:http\:\/\/)*([^\/]+)/);
  198:     foreach (values %Apache::lonnet::hostname) {
  199: 	if ($_ eq $host) {
  200: 	    $url=~s/^http\:\/\///;
  201:             $url=~s/^$host//;
  202:         }
  203:     }
  204:     if ($url=~/^http\:\/\//) { return $url; }
  205:     $url=~s/\~$cuname/res\/$cudom\/$cuname/;
  206:     return $url;
  207: }
  208: 
  209: 
  210: sub absoluteurl {
  211:     my ($url,$target)=@_;
  212:     unless ($url) { return ''; }
  213:     if ($target) {
  214: 	$target=~s/\/[^\/]+$//;
  215:        $url=&Apache::lonnet::hreflocation($target,$url);
  216:     }
  217:     return $url;
  218: }
  219: 
  220: sub set_allow {
  221:     my ($allow,$logfile,$target,$tag,$oldurl)=@_;
  222:     my $newurl=&urlfixup($oldurl,$target);
  223:     my $return_url=$oldurl;
  224:     print $logfile 'GUYURL: '.$tag.':'.$oldurl.' - '.$newurl."\n";
  225:     if ($newurl ne $oldurl) {
  226: 	$return_url=$newurl;
  227: 	print $logfile 'URL: '.$tag.':'.$oldurl.' - '.$newurl."\n";
  228:     }
  229:     if (($newurl !~ /^javascript:/i) &&
  230: 	($newurl !~ /^mailto:/i) &&
  231: 	($newurl !~ /^http:/i) &&
  232: 	($newurl !~ /^\#/)) {
  233: 	$$allow{&absoluteurl($newurl,$target)}=1;
  234:     }
  235:     return $return_url
  236: }
  237: 
  238: sub get_subscribed_hosts {
  239:     my ($target)=@_;
  240:     my @subscribed;
  241:     my $filename;
  242:     $target=~/(.*)\/([^\/]+)$/;
  243:     my $srcf=$2;
  244:     opendir(DIR,$1);
  245:     while ($filename=readdir(DIR)) {
  246: 	if ($filename=~/$srcf\.(\w+)$/) {
  247: 	    my $subhost=$1;
  248: 	    if ($subhost ne 'meta' && $subhost ne 'subscription') {
  249: 		push(@subscribed,$subhost);
  250: 	    }
  251: 	}
  252:     }
  253:     closedir(DIR);
  254:     my $sh;
  255:     if ( $sh=Apache::File->new("$target.subscription") ) {
  256: 	&Apache::lonnet::logthis("opened $target.subscription");
  257: 	while (my $subline=<$sh>) {
  258: 	    &Apache::lonnet::logthis("Trying $subline");
  259: 	    if ($subline =~ /(^\w+):/) { push(@subscribed,$1); } else {
  260: 		&Apache::lonnet::logthis("No Match for $subline");
  261: 	    }
  262: 	}
  263:     } else {
  264: 	&Apache::lonnet::logthis("Un able to open $target.subscription");
  265:     }
  266:     &Apache::lonnet::logthis("Got list of ".join(':',@subscribed));
  267:     return @subscribed;
  268: }
  269: 
  270: 
  271: sub get_max_ids_indices {
  272:     my ($content)=@_;
  273:     my $maxindex=10;
  274:     my $maxid=10;
  275:     my $needsfixup=0;
  276: 
  277:     my $parser=HTML::LCParser->new($content);
  278:     my $token;
  279:     while ($token=$parser->get_token) {
  280: 	if ($token->[0] eq 'S') {
  281: 	    my $counter;
  282: 	    if ($counter=$addid{$token->[1]}) {
  283: 		if ($counter eq 'id') {
  284: 		    if (defined($token->[2]->{'id'})) {
  285: 			$maxid=($token->[2]->{'id'}>$maxid)?$token->[2]->{'id'}:$maxid;
  286: 		    } else {
  287: 			$needsfixup=1;
  288: 		    }
  289: 		} else {
  290: 		    if (defined($token->[2]->{'index'})) {
  291: 			$maxindex=($token->[2]->{'index'}>$maxindex)?$token->[2]->{'index'}:$maxindex;
  292: 		    } else {
  293: 			$needsfixup=1;
  294: 		    }
  295: 		}
  296: 	    }
  297: 	}
  298:     }
  299:     return ($needsfixup,$maxid,$maxindex);
  300: }
  301: 
  302: sub get_all_text_unbalanced {
  303:     #there is a copy of this in lonxml.pm
  304:     my($tag,$pars)= @_;
  305:     my $token;
  306:     my $result='';
  307:     $tag='<'.$tag.'>';
  308:     while ($token = $$pars[-1]->get_token) {
  309: 	if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  310: 	    $result.=$token->[1];
  311: 	} elsif ($token->[0] eq 'PI') {
  312: 	    $result.=$token->[2];
  313: 	} elsif ($token->[0] eq 'S') {
  314: 	    $result.=$token->[4];
  315: 	} elsif ($token->[0] eq 'E')  {
  316: 	    $result.=$token->[2];
  317: 	}
  318: 	if ($result =~ /(.*)$tag(.*)/) {
  319: 	    #&Apache::lonnet::logthis('Got a winner with leftovers ::'.$2);
  320: 	    #&Apache::lonnet::logthis('Result is :'.$1);
  321: 	    $result=$1;
  322: 	    my $redo=$tag.$2;
  323: 	    push (@$pars,HTML::LCParser->new(\$redo));
  324: 	    $$pars[-1]->xml_mode('1');
  325: 	    last;
  326: 	}
  327:     }
  328:     return $result
  329: }
  330: 
  331: #Arguably this should all be done as a lonnet::ssi instead
  332: sub fix_ids_and_indices {
  333:     my ($logfile,$source,$target)=@_;
  334: 
  335:     my %allow;
  336:     my $content;
  337:     {
  338: 	my $org=Apache::File->new($source);
  339: 	$content=join('',<$org>);
  340:     }
  341: 
  342:     my ($needsfixup,$maxid,$maxindex)=&get_max_ids_indices(\$content);
  343: 
  344:     if ($needsfixup) {
  345: 	print $logfile "Needs ID and/or index fixup\n".
  346: 	    "Max ID   : $maxid (min 10)\n".
  347:                 "Max Index: $maxindex (min 10)\n";
  348:     }
  349:     my $outstring='';
  350:     my @parser;
  351:     $parser[0]=HTML::LCParser->new(\$content);
  352:     $parser[-1]->xml_mode(1);
  353:     my $token;
  354:     while (@parser) {
  355: 	while ($token=$parser[-1]->get_token) {
  356: 	    if ($token->[0] eq 'S') {
  357: 		my $counter;
  358: 		my $tag=$token->[1];
  359: 		my $lctag=lc($tag);
  360: 		if ($lctag eq 'allow') {
  361: 		    $allow{$token->[2]->{'src'}}=1;
  362: 		    next;
  363: 		}
  364: 		my %parms=%{$token->[2]};
  365: 		$counter=$addid{$tag};
  366: 		if (!$counter) { $counter=$addid{$lctag}; }
  367: 		if ($counter) {
  368: 		    if ($counter eq 'id') {
  369: 			unless (defined($parms{'id'})) {
  370: 			    $maxid++;
  371: 			    $parms{'id'}=$maxid;
  372: 			    print $logfile 'ID: '.$tag.':'.$maxid."\n";
  373: 			}
  374: 		    } elsif ($counter eq 'index') {
  375: 			unless (defined($parms{'index'})) {
  376: 			    $maxindex++;
  377: 			    $parms{'index'}=$maxindex;
  378: 			    print $logfile 'Index: '.$tag.':'.$maxindex."\n";
  379: 			}
  380: 		    }
  381: 		}
  382: 		foreach my $type ('src','href','background','bgimg') {
  383: 		    foreach my $key (keys(%parms)) {
  384: 			if ($key =~ /^$type$/i) {
  385: 			    $parms{$key}=&set_allow(\%allow,$logfile,
  386: 						    $target,$tag,
  387: 						    $parms{$key});
  388: 			}
  389: 		    }
  390: 		}
  391: 		# probably a <randomlabel> image type <label>
  392: 		if ($lctag eq 'label' && defined($parms{'description'})) {
  393: 		    my $next_token=$parser[-1]->get_token();
  394: 		    if ($next_token->[0] eq 'T') {
  395: 			$next_token->[1]=&set_allow(\%allow,$logfile,
  396: 						    $target,$tag,
  397: 						    $next_token->[1]);
  398: 		    }
  399: 		    $parser[-1]->unget_token($next_token);
  400: 		}
  401: 		if ($lctag eq 'applet') {
  402: 		    my $codebase='';
  403: 		    if (defined($parms{'codebase'})) {
  404: 			my $oldcodebase=$parms{'codebase'};
  405: 			unless ($oldcodebase=~/\/$/) {
  406: 			    $oldcodebase.='/';
  407: 			}
  408: 			$codebase=&urlfixup($oldcodebase,$target);
  409: 			$codebase=~s/\/$//;    
  410: 			if ($codebase ne $oldcodebase) {
  411: 			    $parms{'codebase'}=$codebase;
  412: 			    print $logfile 'URL codebase: '.$tag.':'.
  413: 				$oldcodebase.' - '.
  414: 				    $codebase."\n";
  415: 			}
  416: 			$allow{&absoluteurl($codebase,$target).'/*'}=1;
  417: 		    } else {
  418: 			foreach ('archive','code','object') {
  419: 			    if (defined($parms{$_})) {
  420: 				my $oldurl=$parms{$_};
  421: 				my $newurl=&urlfixup($oldurl,$target);
  422: 				$newurl=~s/\/[^\/]+$/\/\*/;
  423: 				print $logfile 'Allow: applet '.$_.':'.
  424: 				    $oldurl.' allows '.
  425: 					$newurl."\n";
  426: 				$allow{&absoluteurl($newurl,$target)}=1;
  427: 			    }
  428: 			}
  429: 		    }
  430: 		}
  431: 		my $newparmstring='';
  432: 		my $endtag='';
  433: 		foreach (keys %parms) {
  434: 		    if ($_ eq '/') {
  435: 			$endtag=' /';
  436: 		    } else { 
  437: 			my $quote=($parms{$_}=~/\"/?"'":'"');
  438: 			$newparmstring.=' '.$_.'='.$quote.$parms{$_}.$quote;
  439: 		    }
  440: 		}
  441: 		if (!$endtag) { if ($token->[4]=~m:/>$:) { $endtag=' /'; }; }
  442: 		$outstring.='<'.$tag.$newparmstring.$endtag.'>';
  443: 		if ($lctag eq 'm') {
  444: 		    $outstring.=&get_all_text_unbalanced('/m',\@parser);
  445: 		}
  446: 	    } elsif ($token->[0] eq 'E') {
  447: 		if ($token->[2]) {
  448: 		    unless ($token->[1] eq 'allow') {
  449: 			$outstring.='</'.$token->[1].'>';
  450: 		    }
  451: 		}
  452: 	    } else {
  453: 		$outstring.=$token->[1];
  454: 	    }
  455: 	}
  456: 	pop(@parser);
  457:     }
  458: 
  459:     if ($needsfixup) {
  460: 	print $logfile "End of ID and/or index fixup\n".
  461: 	    "Max ID   : $maxid (min 10)\n".
  462: 		"Max Index: $maxindex (min 10)\n";
  463:     } else {
  464: 	print $logfile "Does not need ID and/or index fixup\n";
  465:     }
  466: 
  467:     return ($outstring,%allow);
  468: }
  469: 
  470: sub publish {
  471: 
  472:     my ($source,$target,$style)=@_;
  473:     my $logfile;
  474:     my $scrout='';
  475:     my $allmeta='';
  476:     my $content='';
  477:     my %allow=();
  478: 
  479:     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
  480: 	return 
  481:          '<font color=red>No write permission to user directory, FAIL</font>';
  482:     }
  483:     print $logfile 
  484: "\n\n================= Publish ".localtime()." Phase One  ================\n";
  485: 
  486:     if (($style eq 'ssi') || ($style eq 'rat')) {
  487: # ------------------------------------------------------- This needs processing
  488: 
  489: # ----------------------------------------------------------------- Backup Copy
  490: 	my $copyfile=$source.'.save';
  491:         if (copy($source,$copyfile)) {
  492: 	    print $logfile "Copied original file to ".$copyfile."\n";
  493:         } else {
  494: 	    print $logfile "Unable to write backup ".$copyfile.':'.$!."\n";
  495:           return "<font color=red>Failed to write backup copy, $!,FAIL</font>";
  496:         }
  497: # ------------------------------------------------------------- IDs and indices
  498: 	
  499: 	my $outstring;
  500: 	($outstring,%allow)=&fix_ids_and_indices($logfile,$source,$target);
  501: # ------------------------------------------------------------ Construct Allows
  502:     
  503: 	$scrout.='<h3>Dependencies</h3>';
  504:         my $allowstr='';
  505:         foreach (sort(keys(%allow))) {
  506: 	   my $thisdep=$_;
  507: 	   if ($thisdep !~ /[^\s]/) { next; }
  508:            unless ($style eq 'rat') { 
  509:               $allowstr.="\n".'<allow src="'.$thisdep.'" />';
  510: 	   }
  511:            $scrout.='<br>';
  512:            unless ($thisdep=~/\*/) {
  513: 	       $scrout.='<a href="'.$thisdep.'">';
  514:            }
  515:            $scrout.='<tt>'.$thisdep.'</tt>';
  516:            unless ($thisdep=~/\*/) {
  517: 	       $scrout.='</a>';
  518:                if (
  519:        &Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
  520:                                             $thisdep.'.meta') eq '-1') {
  521: 		   $scrout.=
  522:                            ' - <font color=red>Currently not available</font>';
  523:                } else {
  524:                    my %temphash=(&Apache::lonnet::declutter($target).'___'.
  525:                              &Apache::lonnet::declutter($thisdep).'___usage'
  526:                                  => time);
  527:                    $thisdep=~/^\/res\/(\w+)\/(\w+)\//;
  528:                    if ((defined($1)) && (defined($2))) {
  529:                       &Apache::lonnet::put('resevaldata',\%temphash,$1,$2);
  530: 		   }
  531: 	       }
  532:            }
  533:         }
  534:         $outstring=~s/\n*(\<\/[^\>]+\>)\s*$/$allowstr\n$1\n/s;
  535: 
  536: 	#Encode any High ASCII characters
  537: 	$outstring=&HTML::Entities::encode($outstring,"\200-\377");
  538: # ------------------------------------------------------------- Write modified
  539: 
  540:         {
  541:           my $org;
  542:           unless ($org=Apache::File->new('>'.$source)) {
  543:              print $logfile "No write permit to $source\n";
  544:              return 
  545:               "<font color=red>No write permission to $source, FAIL</font>";
  546: 	  }
  547:           print $org $outstring;
  548:         }
  549: 	  $content=$outstring;
  550: 
  551:     }
  552: # --------------------------------------------- Initial step done, now metadata
  553: 
  554: # ---------------------------------------- Storage for metadata keys and fields
  555: 
  556:      %metadatafields=();
  557:      %metadatakeys=();
  558:      
  559:      my %oldparmstores=();
  560:      
  561:      
  562:      $scrout.='<h3>Metadata Information ' .
  563:        Apache::loncommon::help_open_topic("Metadata_Description")
  564:        . '</h3>';
  565: 
  566: # ------------------------------------------------ First, check out environment
  567:      unless (-e $source.'.meta') {
  568:         $metadatafields{'author'}=$ENV{'environment.firstname'}.' '.
  569: 	                          $ENV{'environment.middlename'}.' '.
  570: 		                  $ENV{'environment.lastname'}.' '.
  571: 		                  $ENV{'environment.generation'};
  572:         $metadatafields{'author'}=~s/\s+/ /g;
  573:         $metadatafields{'author'}=~s/\s+$//;
  574:         $metadatafields{'owner'}=$cuname.'@'.$cudom;
  575: 
  576: # ------------------------------------------------ Check out directory hierachy
  577: 
  578:         my $thisdisfn=$source;
  579:         $thisdisfn=~s/^\/home\/$cuname\///;
  580: 
  581:         my @urlparts=split(/\//,$thisdisfn);
  582:         $#urlparts--;
  583: 
  584:         my $currentpath='/home/'.$cuname.'/';
  585: 
  586:         foreach (@urlparts) {
  587: 	    $currentpath.=$_.'/';
  588:             $scrout.=&metaread($logfile,$currentpath.'default.meta');
  589:         }
  590: 
  591: # ------------------- Clear out parameters and stores (there should not be any)
  592: 
  593:         foreach (keys %metadatafields) {
  594: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
  595: 		delete $metadatafields{$_};
  596:             }
  597:         }
  598: 
  599:     } else {
  600: # ---------------------- Read previous metafile, remember parameters and stores
  601: 
  602:         $scrout.=&metaread($logfile,$source.'.meta');
  603: 
  604:         foreach (keys %metadatafields) {
  605: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
  606:                 $oldparmstores{$_}=1;
  607: 		delete $metadatafields{$_};
  608:             }
  609:         }
  610:         
  611:     }
  612: 
  613: # -------------------------------------------------- Parse content for metadata
  614:     if ($style eq 'ssi') {
  615:         my $oldenv=$ENV{'request.uri'};
  616: 
  617:         $ENV{'request.uri'}=$target;
  618:         $allmeta=Apache::lonxml::xmlparse(undef,'meta',$content);
  619:         $ENV{'request.uri'}=$oldenv;
  620: 
  621:         &metaeval($allmeta);
  622:     }
  623: # ---------------- Find and document discrepancies in the parameters and stores
  624: 
  625:         my $chparms='';
  626:         foreach (sort keys %metadatafields) {
  627: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
  628:                 unless ($_=~/\.\w+$/) { 
  629:                    unless ($oldparmstores{$_}) {
  630: 		      print $logfile 'New: '.$_."\n";
  631:                       $chparms.=$_.' ';
  632:                    }
  633: 	        }
  634:             }
  635:         }
  636:         if ($chparms) {
  637: 	    $scrout.='<p><b>New parameters or stored values:</b> '.
  638:                      $chparms;
  639:         }
  640: 
  641:         $chparms='';
  642:         foreach (sort keys %oldparmstores) {
  643: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
  644:                 unless (($metadatafields{$_.'.name'}) ||
  645:                         ($metadatafields{$_.'.package'}) || ($_=~/\.\w+$/)) {
  646: 		    print $logfile 'Obsolete: '.$_."\n";
  647:                     $chparms.=$_.' ';
  648:                 }
  649:             }
  650:         }
  651:         if ($chparms) {
  652: 	    $scrout.='<p><b>Obsolete parameters or stored values:</b> '.
  653:                      $chparms;
  654:         }
  655: 
  656: # ------------------------------------------------------- Now have all metadata
  657: 
  658:         $scrout.=
  659:      '<form name="pubform" action="/adm/publish" method="post">'.
  660:        '<p><input type="submit" value="Finalize Publication" /></p>'.
  661:           &hiddenfield('phase','two').
  662:           &hiddenfield('filename',$ENV{'form.filename'}).
  663: 	  &hiddenfield('allmeta',&Apache::lonnet::escape($allmeta)).
  664:           &hiddenfield('dependencies',join(',',keys %allow)).
  665:           &textfield('Title','title',$metadatafields{'title'}).
  666:           &textfield('Author(s)','author',$metadatafields{'author'}).
  667: 	  &textfield('Subject','subject',$metadatafields{'subject'});
  668: 
  669: # --------------------------------------------------- Scan content for keywords
  670: 
  671:         my $keywords_help = Apache::loncommon::help_open_topic("Publishing_Keywords");
  672: 	my $keywordout=<<"END";
  673: <script>
  674: function checkAll(field)
  675: {
  676:     for (i = 0; i < field.length; i++)
  677:         field[i].checked = true ;
  678: }
  679: 
  680: function uncheckAll(field)
  681: {
  682:     for (i = 0; i < field.length; i++)
  683:         field[i].checked = false ;
  684: }
  685: </script>
  686: <p><b>Keywords: $keywords_help</b> 
  687: <input type="button" value="check all" onclick="javascript:checkAll(document.pubform.keywords)"> 
  688: <input type="button" value="uncheck all" onclick="javascript:uncheckAll(document.pubform.keywords)"> 
  689: <br />
  690: END
  691:         $keywordout.='<table border=2><tr>';
  692:         my $colcount=0;
  693:         my %keywords=();
  694:         
  695: 	if (length($content)<500000) {
  696: 	    my $textonly=$content;
  697:             $textonly=~s/\<script[^\<]+\<\/script\>//g;
  698:             $textonly=~s/\<m\>[^\<]+\<\/m\>//g;
  699:             $textonly=~s/\<[^\>]*\>//g;
  700:             $textonly=~tr/A-Z/a-z/;
  701:             $textonly=~s/[\$\&][a-z]\w*//g;
  702:             $textonly=~s/[^a-z\s]//g;
  703: 
  704:             foreach ($textonly=~m/(\w+)/g) {
  705: 		unless ($nokey{$_}) {
  706:                    $keywords{$_}=1;
  707:                 } 
  708:             }
  709:         }
  710: 
  711:             
  712:             foreach (split(/\W+/,$metadatafields{'keywords'})) {
  713: 		$keywords{$_}=1;
  714:             }
  715: 
  716:             foreach (sort keys %keywords) {
  717:                 $keywordout.='<td><input type=checkbox name="keywords" value="'.$_.'"';
  718:                 if ($metadatafields{'keywords'}) {
  719:                    if ($metadatafields{'keywords'}=~/$_/) { 
  720:                       $keywordout.=' checked'; 
  721:                    }
  722: 	        } elsif (&Apache::loncommon::keyword($_)) {
  723: 	            $keywordout.=' checked';
  724:                 } 
  725:                 $keywordout.='>'.$_.'</td>';
  726:                 if ($colcount>10) {
  727: 		    $keywordout.="</tr><tr>\n";
  728:                     $colcount=0;
  729:                 }
  730:                 $colcount++;
  731:             }
  732:         
  733: 	$keywordout.='</tr></table>';
  734: 
  735:         $scrout.=$keywordout;
  736: 
  737:         $scrout.=&textfield('Additional Keywords','addkey','');
  738: 
  739:         $scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
  740: 
  741:         $scrout.=
  742:              '<p><b>Abstract:</b><br><textarea cols=80 rows=5 name=abstract>'.
  743:               $metadatafields{'abstract'}.'</textarea>';
  744: 
  745: 	$source=~/\.(\w+)$/;
  746: 
  747: 	$scrout.=&hiddenfield('mime',$1);
  748: 
  749:         $scrout.=&selectbox('Language','language',
  750:                             $metadatafields{'language'},
  751: 			    \&Apache::loncommon::languagedescription,
  752: 			    (&Apache::loncommon::languageids),
  753: 			     );
  754: 
  755:         unless ($metadatafields{'creationdate'}) {
  756: 	    $metadatafields{'creationdate'}=time;
  757:         }
  758:         $scrout.=&hiddenfield('creationdate',$metadatafields{'creationdate'});
  759: 
  760:         $scrout.=&hiddenfield('lastrevisiondate',time);
  761: 
  762: 			   
  763: 	$scrout.=&textfield('Publisher/Owner','owner',
  764:                             $metadatafields{'owner'});
  765: # --------------------------------------------------- Correct copyright for rat        
  766: 
  767:     if ($style eq 'rat') {
  768: 	if ($metadatafields{'copyright'} eq 'public') { 
  769: 	    delete $metadatafields{'copyright'};
  770: 	}
  771:         $scrout.=&selectbox('Copyright/Distribution','copyright',
  772:                             $metadatafields{'copyright'},
  773: 			    \&Apache::loncommon::copyrightdescription,
  774: 		     (grep !/^public$/,(&Apache::loncommon::copyrightids)));
  775:     }
  776:     else {
  777:         $scrout.=&selectbox('Copyright/Distribution','copyright',
  778:                             $metadatafields{'copyright'},
  779: 			    \&Apache::loncommon::copyrightdescription,
  780: 			     (&Apache::loncommon::copyrightids));
  781:     }
  782: 
  783:     my $copyright_help = Apache::loncommon::help_open_topic("Publishing_Copyright");
  784:     $scrout =~ s/DISTRIBUTION:/'DISTRIBUTION: ' . $copyright_help/ge;
  785:     return $scrout.
  786:       '<p><input type="submit" value="Finalize Publication" /></p></form>';
  787: }
  788: 
  789: # -------------------------------------------------------- Publication Step Two
  790: 
  791: sub phasetwo {
  792: 
  793:     my ($source,$target,$style,$distarget)=@_;
  794:     my $logfile;
  795:     my $scrout='';
  796:     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
  797: 	return 
  798:          '<font color=red>No write permission to user directory, FAIL</font>';
  799:     }
  800:     print $logfile 
  801: "\n================= Publish ".localtime()." Phase Two  ================\n";
  802: 
  803:      %metadatafields=();
  804:      %metadatakeys=();
  805: 
  806:      &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
  807: 
  808:      $metadatafields{'title'}=$ENV{'form.title'};
  809:      $metadatafields{'author'}=$ENV{'form.author'};
  810:      $metadatafields{'subject'}=$ENV{'form.subject'};
  811:      $metadatafields{'notes'}=$ENV{'form.notes'};
  812:      $metadatafields{'abstract'}=$ENV{'form.abstract'};
  813:      $metadatafields{'mime'}=$ENV{'form.mime'};
  814:      $metadatafields{'language'}=$ENV{'form.language'};
  815:      $metadatafields{'creationdate'}=$ENV{'form.creationdate'};
  816:      $metadatafields{'lastrevisiondate'}=$ENV{'form.lastrevisiondate'};
  817:      $metadatafields{'owner'}=$ENV{'form.owner'};
  818:      $metadatafields{'copyright'}=$ENV{'form.copyright'};
  819:      $metadatafields{'dependencies'}=$ENV{'form.dependencies'};
  820: 
  821:      my $allkeywords=$ENV{'form.addkey'};
  822:      if (exists($ENV{'form.keywords'}) && (ref($ENV{'form.keywords'}))) {
  823:          my @Keywords = @{$ENV{'form.keywords'}};
  824:          foreach (@Keywords) {
  825:              $allkeywords.=','.$_;
  826:          }
  827:      }
  828:      $allkeywords=~s/\W+/\,/;
  829:      $allkeywords=~s/^\,//;
  830:      $metadatafields{'keywords'}=$allkeywords;
  831:  
  832:      {
  833:        print $logfile "\nWrite metadata file for ".$source;
  834:        my $mfh;
  835:        unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
  836: 	return 
  837:          '<font color=red>Could not write metadata, FAIL</font>';
  838:        }
  839:        foreach (sort keys %metadatafields) {
  840: 	 unless ($_=~/\./) {
  841:            my $unikey=$_;
  842:            $unikey=~/^([A-Za-z]+)/;
  843:            my $tag=$1;
  844:            $tag=~tr/A-Z/a-z/;
  845:            print $mfh "\n\<$tag";
  846:            foreach (split(/\,/,$metadatakeys{$unikey})) {
  847:                my $value=$metadatafields{$unikey.'.'.$_};
  848:                $value=~s/\"/\'\'/g;
  849:                print $mfh ' '.$_.'="'.$value.'"';
  850:            }
  851: 	   print $mfh '>'.
  852: 	     &HTML::Entities::encode($metadatafields{$unikey})
  853: 	       .'</'.$tag.'>';
  854:          }
  855:        }
  856:        $scrout.='<p>Wrote Metadata';
  857:        print $logfile "\nWrote metadata";
  858:      }
  859: 
  860: # -------------------------------- Synchronize entry with SQL metadata database
  861:   my $warning;
  862: 
  863:   unless ($metadatafields{'copyright'} eq 'priv') {
  864: 
  865:     my $dbh;
  866:     {
  867: 	unless (
  868: 		$dbh = DBI->connect("DBI:mysql:loncapa","www",
  869:     $Apache::lonnet::perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
  870: 		) { 
  871: 	    $warning='<font color=red>WARNING: Cannot connect to '.
  872: 		'database!</font>';
  873: 	}
  874: 	else {
  875: 	    my %sqldatafields;
  876: 	    $sqldatafields{'url'}=$distarget;
  877: 	    my $sth=$dbh->prepare(
  878: 				  'delete from metadata where url like binary'.
  879: 				  '"'.$sqldatafields{'url'}.'"');
  880: 	    $sth->execute();
  881: 	    foreach ('title','author','subject','keywords','notes','abstract',
  882: 	     'mime','language','creationdate','lastrevisiondate','owner',
  883: 	     'copyright') {
  884: 		my $field=$metadatafields{$_}; $field=~s/\"/\'\'/g; 
  885: 		$sqldatafields{$_}=$field;
  886: 	    }
  887: 	    
  888: 	    $sth=$dbh->prepare('insert into metadata values ('.
  889: 			       '"'.delete($sqldatafields{'title'}).'"'.','.
  890: 			       '"'.delete($sqldatafields{'author'}).'"'.','.
  891: 			       '"'.delete($sqldatafields{'subject'}).'"'.','.
  892: 			       '"'.delete($sqldatafields{'url'}).'"'.','.
  893: 			       '"'.delete($sqldatafields{'keywords'}).'"'.','.
  894: 			       '"'.'current'.'"'.','.
  895: 			       '"'.delete($sqldatafields{'notes'}).'"'.','.
  896: 			       '"'.delete($sqldatafields{'abstract'}).'"'.','.
  897: 			       '"'.delete($sqldatafields{'mime'}).'"'.','.
  898: 			       '"'.delete($sqldatafields{'language'}).'"'.','.
  899: 			       '"'.
  900: 			       sqltime(delete($sqldatafields{'creationdate'}))
  901: 			       .'"'.','.
  902: 			       '"'.
  903: 			       sqltime(delete(
  904: 			       $sqldatafields{'lastrevisiondate'})).'"'.','.
  905: 			       '"'.delete($sqldatafields{'owner'}).'"'.','.
  906: 			       '"'.delete(
  907: 			       $sqldatafields{'copyright'}).'"'.')');
  908: 	    $sth->execute();
  909: 	    $dbh->disconnect;
  910: 	    $scrout.='<p>Synchronized SQL metadata database';
  911: 	    print $logfile "\nSynchronized SQL metadata database";
  912: 	}
  913:     }
  914: 
  915: } else {
  916:     $scrout.='<p>Private Publication - did not synchronize database';
  917:     print $logfile "\nPrivate: Did not synchronize data into ".
  918: 	"SQL metadata database";
  919: }
  920: # ----------------------------------------------------------- Copy old versions
  921:    
  922: if (-e $target) {
  923:     my $filename;
  924:     my $maxversion=0;
  925:     $target=~/(.*)\/([^\/]+)\.(\w+)$/;
  926:     my $srcf=$2;
  927:     my $srct=$3;
  928:     my $srcd=$1;
  929:     unless ($srcd=~/^\/home\/httpd\/html\/res/) {
  930: 	print $logfile "\nPANIC: Target dir is ".$srcd;
  931:         return "<font color=red>Invalid target directory, FAIL</font>";
  932:     }
  933:     opendir(DIR,$srcd);
  934:     while ($filename=readdir(DIR)) {
  935:        if ($filename=~/$srcf\.(\d+)\.$srct$/) {
  936: 	   $maxversion=($1>$maxversion)?$1:$maxversion;
  937:        }
  938:     }
  939:     closedir(DIR);
  940:     $maxversion++;
  941:     $scrout.='<p>Creating old version '.$maxversion;
  942:     print $logfile "\nCreating old version ".$maxversion;
  943: 
  944:     my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
  945: 
  946:         if (copy($target,$copyfile)) {
  947: 	    print $logfile "Copied old target to ".$copyfile."\n";
  948:             $scrout.='<p>Copied old target file';
  949:         } else {
  950: 	    print $logfile "Unable to write ".$copyfile.':'.$!."\n";
  951:            return "<font color=red>Failed to copy old target, $!, FAIL</font>";
  952:         }
  953: 
  954: # --------------------------------------------------------------- Copy Metadata
  955: 
  956: 	$copyfile=$copyfile.'.meta';
  957: 
  958:         if (copy($target.'.meta',$copyfile)) {
  959: 	    print $logfile "Copied old target metadata to ".$copyfile."\n";
  960:             $scrout.='<p>Copied old metadata';
  961:         } else {
  962: 	    print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
  963:             if (-e $target.'.meta') {
  964:                return 
  965:        "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
  966: 	    }
  967:         }
  968: 
  969: 
  970: } else {
  971:     $scrout.='<p>Initial version';
  972:     print $logfile "\nInitial version";
  973: }
  974: 
  975: # ---------------------------------------------------------------- Write Source
  976: 	my $copyfile=$target;
  977: 
  978:            my @parts=split(/\//,$copyfile);
  979:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
  980: 
  981:            my $count;
  982:            for ($count=5;$count<$#parts;$count++) {
  983:                $path.="/$parts[$count]";
  984:                if ((-e $path)!=1) {
  985:                    print $logfile "\nCreating directory ".$path;
  986:                    $scrout.='<p>Created directory '.$parts[$count];
  987: 		   mkdir($path,0777);
  988:                }
  989:            }
  990: 
  991:         if (copy($source,$copyfile)) {
  992: 	    print $logfile "Copied original source to ".$copyfile."\n";
  993:             $scrout.='<p>Copied source file';
  994:         } else {
  995: 	    print $logfile "Unable to write ".$copyfile.':'.$!."\n";
  996:             return "<font color=red>Failed to copy source, $!, FAIL</font>";
  997:         }
  998: 
  999: # --------------------------------------------------------------- Copy Metadata
 1000: 
 1001:         $copyfile=$copyfile.'.meta';
 1002: 
 1003:         if (copy($source.'.meta',$copyfile)) {
 1004: 	    print $logfile "Copied original metadata to ".$copyfile."\n";
 1005:             $scrout.='<p>Copied metadata';
 1006:         } else {
 1007: 	    print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 1008:             return 
 1009:           "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
 1010:         }
 1011: 
 1012: # --------------------------------------------------- Send update notifications
 1013: 
 1014:     my @subscribed=&get_subscribed_hosts($target);
 1015:     foreach my $subhost (@subscribed) {
 1016: 	$scrout.='<p>Notifying host '.$subhost.':';
 1017: 	print $logfile "\nNotifying host ".$subhost.':';
 1018: 	my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 1019: 	$scrout.=$reply;
 1020: 	print $logfile $reply;
 1021:     }
 1022: 
 1023: # ---------------------------------------- Send update notifications, meta only
 1024: 
 1025:     my @subscribedmeta=&get_subscribed_hosts("$target.meta");
 1026:     foreach my $subhost (@subscribedmeta) {
 1027: 	$scrout.='<p>Notifying host for metadata only '.$subhost.':';
 1028: 	print $logfile "\nNotifying host for metadata only ".$subhost.':';
 1029: 	my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 1030: 					    $subhost);
 1031: 	$scrout.=$reply;
 1032: 	print $logfile $reply;
 1033:     }
 1034: 
 1035: # ------------------------------------------------ Provide link to new resource
 1036: 
 1037:     my $thisdistarget=$target;
 1038:     $thisdistarget=~s/^$docroot//;
 1039: 
 1040:     my $thissrc=$source;
 1041:     $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
 1042: 
 1043:     my $thissrcdir=$thissrc;
 1044:     $thissrcdir=~s/\/[^\/]+$/\//;
 1045: 
 1046: 
 1047:     return $warning.$scrout.
 1048:       '<hr><a href="'.$thisdistarget.'"><font size=+2>View Published Version</font></a>'.
 1049:       '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
 1050:       '<p><a href="'.$thissrcdir.
 1051:       '"><font size=+2>Back to Source Directory</font></a>';
 1052: 
 1053: }
 1054: 
 1055: # ================================================================ Main Handler
 1056: 
 1057: sub handler {
 1058:   my $r=shift;
 1059: 
 1060:   if ($r->header_only) {
 1061:      $r->content_type('text/html');
 1062:      $r->send_http_header;
 1063:      return OK;
 1064:   }
 1065: 
 1066: # Get query string for limited number of parameters
 1067: 
 1068:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1069:                                             ['filename']);
 1070: 
 1071: # -------------------------------------------------------------- Check filename
 1072: 
 1073:   my $fn=$ENV{'form.filename'};
 1074: 
 1075:   
 1076:   unless ($fn) { 
 1077:      $r->log_reason($cuname.' at '.$cudom.
 1078:          ' trying to publish empty filename', $r->filename); 
 1079:      return HTTP_NOT_FOUND;
 1080:   } 
 1081: 
 1082:   ($cuname,$cudom)=
 1083:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
 1084:   unless (($cuname) && ($cudom)) {
 1085:      $r->log_reason($cuname.' at '.$cudom.
 1086:          ' trying to publish file '.$ENV{'form.filename'}.
 1087:          ' ('.$fn.') - not authorized', 
 1088:          $r->filename); 
 1089:      return HTTP_NOT_ACCEPTABLE;
 1090:   }
 1091: 
 1092:   unless (&Apache::lonnet::homeserver($cuname,$cudom) 
 1093:           eq $r->dir_config('lonHostID')) {
 1094:      $r->log_reason($cuname.' at '.$cudom.
 1095:          ' trying to publish file '.$ENV{'form.filename'}.
 1096:          ' ('.$fn.') - not homeserver ('.
 1097:          &Apache::lonnet::homeserver($cuname,$cudom).')', 
 1098:          $r->filename); 
 1099:      return HTTP_NOT_ACCEPTABLE;
 1100:   }
 1101: 
 1102:   $fn=~s/^http\:\/\/[^\/]+//;
 1103:   $fn=~s/^\/\~(\w+)/\/home\/$1\/public_html/;
 1104: 
 1105:   my $targetdir='';
 1106:   $docroot=$r->dir_config('lonDocRoot'); 
 1107:   if ($1 ne $cuname) {
 1108:      $r->log_reason($cuname.' at '.$cudom.
 1109:          ' trying to publish unowned file '.$ENV{'form.filename'}.
 1110:          ' ('.$fn.')', 
 1111:          $r->filename); 
 1112:      return HTTP_NOT_ACCEPTABLE;
 1113:   } else {
 1114:       $targetdir=$docroot.'/res/'.$cudom;
 1115:   }
 1116:                                  
 1117:   
 1118:   unless (-e $fn) { 
 1119:      $r->log_reason($cuname.' at '.$cudom.
 1120:          ' trying to publish non-existing file '.$ENV{'form.filename'}.
 1121:          ' ('.$fn.')', 
 1122:          $r->filename); 
 1123:      return HTTP_NOT_FOUND;
 1124:   } 
 1125: 
 1126: unless ($ENV{'form.phase'} eq 'two') {
 1127: 
 1128: # --------------------------------- File is there and owned, init lookup tables
 1129: 
 1130:   %addid=();
 1131: 
 1132:   {
 1133:       my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
 1134:       while (<$fh>=~/(\w+)\s+(\w+)/) {
 1135:           $addid{$1}=$2;
 1136:       }
 1137:   }
 1138: 
 1139:   %nokey=();
 1140: 
 1141:   {
 1142:      my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
 1143:       while (<$fh>) {
 1144:           my $word=$_;
 1145:           chomp($word);
 1146:           $nokey{$word}=1;
 1147:       }
 1148:   }
 1149: 
 1150: }
 1151: 
 1152: # ----------------------------------------------------------- Start page output
 1153: 
 1154:   $r->content_type('text/html');
 1155:   $r->send_http_header;
 1156: 
 1157:   $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
 1158:   $r->print(
 1159:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
 1160:   my $thisfn=$fn;
 1161:    
 1162: # ------------------------------------------------------------- Individual file
 1163:   {
 1164:       $thisfn=~/\.(\w+)$/;
 1165:       my $thistype=$1;
 1166:       my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
 1167: 
 1168:       my $thistarget=$thisfn;
 1169:       
 1170:       $thistarget=~s/^\/home/$targetdir/;
 1171:       $thistarget=~s/\/public\_html//;
 1172: 
 1173:       my $thisdistarget=$thistarget;
 1174:       $thisdistarget=~s/^$docroot//;
 1175: 
 1176:       my $thisdisfn=$thisfn;
 1177:       $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
 1178: 
 1179:       $r->print('<h2>Publishing '.
 1180:         &Apache::loncommon::filedescription($thistype).' <tt>'.
 1181:         $thisdisfn.'</tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
 1182:    
 1183:        if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
 1184:           $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
 1185:                '</font></h3>');
 1186:       }
 1187: 
 1188:       if (&Apache::loncommon::fileembstyle($thistype) eq 'ssi') {
 1189:           $r->print('<br><a href="/adm/diff?filename=/~'.$cuname.'/'.
 1190:                     $thisdisfn.
 1191:   	  '&versionone=priv" target=cat>Diffs with Current Version</a><p>');
 1192:       }
 1193:   
 1194: # ------------ We are publishing from $thisfn to $thistarget with $thisembstyle
 1195: 
 1196:        unless ($ENV{'form.phase'} eq 'two') {
 1197:          $r->print(
 1198:           '<hr>'.&publish($thisfn,$thistarget,$thisembstyle));
 1199:        } else {
 1200:          $r->print(
 1201:           '<hr>'.&phasetwo($thisfn,$thistarget,$thisembstyle,$thisdistarget)); 
 1202:        }  
 1203: 
 1204:   }
 1205:   $r->print('</body></html>');
 1206: 
 1207:   return OK;
 1208: }
 1209: 
 1210: 1;
 1211: __END__
 1212: 
 1213: =head1 NAME
 1214: 
 1215: Apache::lonpublisher - Publication Handler
 1216: 
 1217: =head1 SYNOPSIS
 1218: 
 1219: Invoked by /etc/httpd/conf/srm.conf:
 1220: 
 1221:  <Location /adm/publish>
 1222:  PerlAccessHandler       Apache::lonacc
 1223:  SetHandler perl-script
 1224:  PerlHandler Apache::lonpublisher
 1225:  ErrorDocument     403 /adm/login
 1226:  ErrorDocument     404 /adm/notfound.html
 1227:  ErrorDocument     406 /adm/unauthorized.html
 1228:  ErrorDocument	  500 /adm/errorhandler
 1229:  </Location>
 1230: 
 1231: =head1 INTRODUCTION
 1232: 
 1233: This module publishes a file.  This involves gathering metadata,
 1234: versioning the file, copying file from construction space to
 1235: publication space, and copying metadata from construction space
 1236: to publication space.
 1237: 
 1238: This is part of the LearningOnline Network with CAPA project
 1239: described at http://www.lon-capa.org.
 1240: 
 1241: =head1 HANDLER SUBROUTINE
 1242: 
 1243: This routine is called by Apache and mod_perl.
 1244: 
 1245: =over 4
 1246: 
 1247: =item *
 1248: 
 1249: Get query string for limited number of parameters
 1250: 
 1251: =item *
 1252: 
 1253: Check filename
 1254: 
 1255: =item *
 1256: 
 1257: File is there and owned, init lookup tables
 1258: 
 1259: =item *
 1260: 
 1261: Start page output
 1262: 
 1263: =item *
 1264: 
 1265: Individual file
 1266: 
 1267: =item *
 1268: 
 1269: publish from $thisfn to $thistarget with $thisembstyle
 1270: 
 1271: =back
 1272: 
 1273: =head1 OTHER SUBROUTINES
 1274: 
 1275: =over 4
 1276: 
 1277: =item *
 1278: 
 1279: metaeval() : Evaluate string with metadata
 1280: 
 1281: =item *
 1282: 
 1283: metaread() : Read a metadata file
 1284: 
 1285: =item *
 1286: 
 1287: sqltime() : convert 'time' format into a datetime sql format
 1288: 
 1289: =item *
 1290: 
 1291: textfield() : form field
 1292: 
 1293: =item *
 1294: 
 1295: hiddenfield() : form field
 1296: 
 1297: =item *
 1298: 
 1299: selectbox() : form field
 1300: 
 1301: =item *
 1302: 
 1303: urlfixup() : fixup URL (Publication Step One)
 1304: 
 1305: =item *
 1306: 
 1307: publish() : publish (Publication Step One)
 1308: 
 1309: =item *
 1310: 
 1311: phasetwo() : render second interface showing status of publication steps
 1312: (Publication Step Two)
 1313: 
 1314: =back
 1315: 
 1316: =cut

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