File:  [LON-CAPA] / loncom / publisher / lonpublisher.pm
Revision 1.24: download - view: text, annotated - select for diffs
Mon Apr 16 20:02:50 2001 UTC (23 years, 1 month ago) by harris41
Branches: MAIN
CVS tags: HEAD
adding in sql metadata database updating feature -Scott

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

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