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

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

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