Annotation of loncom/publisher/lonpublisher.pm, revision 1.23

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

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