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

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.33    ! www       343:                 unless (($metadatafields{$_.'.name'}) ||
        !           344:                         ($metadatafields{$_.'.package'}) || ($_=~/\.\w+$/)) {
1.7       www       345: 		    print $logfile 'Obsolete: '.$_."\n";
                    346:                     $chparms.=$_.' ';
                    347:                 }
                    348:             }
                    349:         } sort keys %oldparmstores;
                    350:         if ($chparms) {
                    351: 	    $scrout.='<p><b>Obsolete parameters or stored values:</b> '.
                    352:                      $chparms;
                    353:         }
1.23      www       354:     }
1.8       www       355: # ------------------------------------------------------- Now have all metadata
1.5       www       356: 
1.8       www       357:         $scrout.=
                    358:      '<form action="/adm/publish" method="post">'.
1.11      www       359:           &hiddenfield('phase','two').
                    360:           &hiddenfield('filename',$ENV{'form.filename'}).
                    361: 	  &hiddenfield('allmeta',&Apache::lonnet::escape($allmeta)).
1.10      www       362:           &textfield('Title','title',$metadatafields{'title'}).
                    363:           &textfield('Author(s)','author',$metadatafields{'author'}).
                    364: 	  &textfield('Subject','subject',$metadatafields{'subject'});
1.5       www       365: 
                    366: # --------------------------------------------------- Scan content for keywords
1.7       www       367: 
1.8       www       368: 	my $keywordout='<p><b>Keywords:</b><br><table border=2><tr>';
1.7       www       369:         my $colcount=0;
                    370:         
1.5       www       371: 	{
                    372: 	    my $textonly=$content;
                    373:             $textonly=~s/\<script[^\<]+\<\/script\>//g;
                    374:             $textonly=~s/\<m\>[^\<]+\<\/m\>//g;
                    375:             $textonly=~s/\<[^\>]*\>//g;
                    376:             $textonly=~tr/A-Z/a-z/;
                    377:             $textonly=~s/[\$\&][a-z]\w*//g;
                    378:             $textonly=~s/[^a-z\s]//g;
                    379: 
                    380:             my %keywords=();
                    381:             map {
                    382: 		unless ($nokey{$_}) {
                    383:                    $keywords{$_}=1;
                    384:                 } 
                    385:             } ($textonly=~m/(\w+)/g);
                    386: 
1.12      www       387:             map {
                    388: 		$keywords{$_}=1;
                    389:             } split(/\W+/,$metadatafields{'keywords'});
1.5       www       390: 
1.7       www       391:             map {
1.12      www       392:                 $keywordout.='<td><input type=checkbox name="key.'.$_.'"';
1.8       www       393:                 if ($metadatafields{'keywords'}=~/$_/) { 
                    394:                    $keywordout.=' checked'; 
                    395:                 }
                    396:                 $keywordout.='>'.$_.'</td>';
1.7       www       397:                 if ($colcount>10) {
                    398: 		    $keywordout.="</tr><tr>\n";
                    399:                     $colcount=0;
                    400:                 }
                    401:                 $colcount++;
                    402:             } sort keys %keywords;
                    403:             $keywordout.='</tr></table>';
1.5       www       404: 
                    405:         }         
1.4       www       406:         
1.7       www       407: 	$scrout.=$keywordout;
1.9       www       408: 
1.12      www       409:         $scrout.=&textfield('Additional Keywords','addkey','');
                    410: 
1.10      www       411:         $scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
1.9       www       412: 
                    413:         $scrout.=
                    414:              '<p><b>Abstract:</b><br><textarea cols=80 rows=5 name=abstract>'.
                    415:               $metadatafields{'abstract'}.'</textarea>';
                    416: 
1.11      www       417: 	$source=~/\.(\w+)$/;
                    418: 
                    419: 	$scrout.=&hiddenfield('mime',$1);
                    420: 
1.10      www       421:         $scrout.=&selectbox('Language','language',
                    422:                             $metadatafields{'language'},%language);
1.11      www       423: 
                    424:         unless ($metadatafields{'creationdate'}) {
                    425: 	    $metadatafields{'creationdate'}=time;
                    426:         }
                    427:         $scrout.=&hiddenfield('creationdate',$metadatafields{'creationdate'});
                    428: 
                    429:         $scrout.=&hiddenfield('lastrevisiondate',time);
                    430: 
1.9       www       431: 			   
1.10      www       432: 	$scrout.=&textfield('Publisher/Owner','owner',
                    433:                             $metadatafields{'owner'});
                    434: 
                    435:         $scrout.=&selectbox('Copyright/Distribution','copyright',
                    436:                             $metadatafields{'copyright'},%cprtag);
1.9       www       437: 
1.8       www       438:     return $scrout.
                    439:       '<p><input type="submit" value="Finalize Publication"></form>';
1.2       www       440: }
1.1       www       441: 
1.12      www       442: # -------------------------------------------------------- Publication Step Two
                    443: 
1.11      www       444: sub phasetwo {
                    445: 
1.24      harris41  446:     my ($source,$target,$style,$distarget)=@_;
1.11      www       447:     my $logfile;
                    448:     my $scrout='';
                    449: 
                    450:     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
                    451: 	return 
                    452:          '<font color=red>No write permission to user directory, FAIL</font>';
                    453:     }
                    454:     print $logfile 
                    455: "\n================= Publish ".localtime()." Phase Two  ================\n";
                    456: 
                    457:      %metadatafields=();
                    458:      %metadatakeys=();
                    459: 
                    460:      &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
                    461: 
                    462:      $metadatafields{'title'}=$ENV{'form.title'};
                    463:      $metadatafields{'author'}=$ENV{'form.author'};
                    464:      $metadatafields{'subject'}=$ENV{'form.subject'};
                    465:      $metadatafields{'notes'}=$ENV{'form.notes'};
                    466:      $metadatafields{'abstract'}=$ENV{'form.abstract'};
                    467:      $metadatafields{'mime'}=$ENV{'form.mime'};
                    468:      $metadatafields{'language'}=$ENV{'form.language'};
                    469:      $metadatafields{'creationdate'}=$ENV{'form.creationdate'};
                    470:      $metadatafields{'lastrevisiondate'}=$ENV{'form.lastrevisiondate'};
                    471:      $metadatafields{'owner'}=$ENV{'form.owner'};
                    472:      $metadatafields{'copyright'}=$ENV{'form.copyright'};
1.12      www       473: 
                    474:      my $allkeywords=$ENV{'form.addkey'};
1.11      www       475:      map {
1.12      www       476:          if ($_=~/^form\.key\.(\w+)/) {
                    477: 	     $allkeywords.=','.$1;
                    478:          }
                    479:      } keys %ENV;
                    480:      $allkeywords=~s/\W+/\,/;
                    481:      $allkeywords=~s/^\,//;
                    482:      $metadatafields{'keywords'}=$allkeywords;
                    483:  
                    484:      {
                    485:        print $logfile "\nWrite metadata file for ".$source;
                    486:        my $mfh;
                    487:        unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
                    488: 	return 
                    489:          '<font color=red>Could not write metadata, FAIL</font>';
                    490:        }    
                    491:        map {
                    492: 	 unless ($_=~/\./) {
                    493:            my $unikey=$_;
                    494:            $unikey=~/^([A-Za-z]+)/;
                    495:            my $tag=$1;
                    496:            $tag=~tr/A-Z/a-z/;
                    497:            print $mfh "\n\<$tag";
                    498:            map {
                    499:                my $value=$metadatafields{$unikey.'.'.$_};
                    500:                $value=~s/\"/\'\'/g;
                    501:                print $mfh ' '.$_.'="'.$value.'"';
                    502:            } split(/\,/,$metadatakeys{$unikey});
                    503: 	   print $mfh '>'.$metadatafields{$unikey}.'</'.$tag.'>';
                    504:          }
                    505:        } sort keys %metadatafields;
                    506:        $scrout.='<p>Wrote Metadata';
                    507:        print $logfile "\nWrote metadata";
                    508:      }
                    509: 
1.24      harris41  510: # -------------------------------- Synchronize entry with SQL metadata database
1.25      harris41  511:     my %perlvar;
                    512:     open (CONFIG,"/etc/httpd/conf/access.conf") || die "Can't read access.conf";
                    513:     my $configline;
                    514:     while ($configline=<CONFIG>) {
                    515: 	if ($configline =~ /PerlSetVar/) {
                    516: 	    my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
                    517: 	    chomp($varvalue);
                    518: 	    $perlvar{$varname}=$varvalue;
                    519: 	}
                    520:     }
                    521:     close(CONFIG);
                    522: 
1.29      harris41  523:     my $warning;
1.24      harris41  524:     my $dbh;
                    525:     {
                    526: 	unless (
                    527: 		$dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
                    528: 		) { 
1.29      harris41  529: 	    $warning='<font color=red>WARNING: Cannot connect to '.
                    530: 		'database!</font>';
                    531: 	}
                    532: 	else {
                    533: 	    my %sqldatafields;
                    534: 	    $sqldatafields{'url'}=$distarget;
                    535: 	    my $sth=$dbh->prepare(
                    536: 				  'delete from metadata where url like binary'.
                    537: 				  '"'.$sqldatafields{'url'}.'"');
                    538: 	    $sth->execute();
                    539: 	    map {my $field=$metadatafields{$_}; $field=~s/\"/\'\'/g; 
                    540: 		 $sqldatafields{$_}=$field;}
                    541: 	    ('title','author','subject','keywords','notes','abstract',
                    542: 	     'mime','language','creationdate','lastrevisiondate','owner',
                    543: 	     'copyright');
                    544: 	    
                    545: 	    $sth=$dbh->prepare('insert into metadata values ('.
                    546: 			       '"'.delete($sqldatafields{'title'}).'"'.','.
                    547: 			       '"'.delete($sqldatafields{'author'}).'"'.','.
                    548: 			       '"'.delete($sqldatafields{'subject'}).'"'.','.
                    549: 			       '"'.delete($sqldatafields{'url'}).'"'.','.
                    550: 			       '"'.delete($sqldatafields{'keywords'}).'"'.','.
                    551: 			       '"'.'current'.'"'.','.
                    552: 			       '"'.delete($sqldatafields{'notes'}).'"'.','.
                    553: 			       '"'.delete($sqldatafields{'abstract'}).'"'.','.
                    554: 			       '"'.delete($sqldatafields{'mime'}).'"'.','.
                    555: 			       '"'.delete($sqldatafields{'language'}).'"'.','.
                    556: 			       '"'.
                    557: 			       sqltime(delete($sqldatafields{'creationdate'}))
                    558: 			       .'"'.','.
                    559: 			       '"'.
                    560: 			       sqltime(delete(
                    561: 			       $sqldatafields{'lastrevisiondate'})).'"'.','.
                    562: 			       '"'.delete($sqldatafields{'owner'}).'"'.','.
                    563: 			       '"'.delete(
                    564: 			       $sqldatafields{'copyright'}).'"'.')');
                    565: 	    $sth->execute();
                    566: 	    $dbh->disconnect;
                    567: 	    $scrout.='<p>Synchronized SQL metadata database';
                    568: 	    print $logfile "\nSynchronized SQL metadata database";
1.24      harris41  569: 	}
                    570:     }
                    571: 
                    572: 
1.12      www       573: # ----------------------------------------------------------- Copy old versions
                    574:    
                    575: if (-e $target) {
                    576:     my $filename;
                    577:     my $maxversion=0;
                    578:     $target=~/(.*)\/([^\/]+)\.(\w+)$/;
                    579:     my $srcf=$2;
                    580:     my $srct=$3;
                    581:     my $srcd=$1;
                    582:     unless ($srcd=~/^\/home\/httpd\/html\/res/) {
                    583: 	print $logfile "\nPANIC: Target dir is ".$srcd;
                    584:         return "<font color=red>Invalid target directory, FAIL</font>";
                    585:     }
                    586:     opendir(DIR,$srcd);
                    587:     while ($filename=readdir(DIR)) {
                    588:        if ($filename=~/$srcf\.(\d+)\.$srct$/) {
                    589: 	   $maxversion=($1>$maxversion)?$1:$maxversion;
                    590:        }
                    591:     }
                    592:     closedir(DIR);
                    593:     $maxversion++;
                    594:     $scrout.='<p>Creating old version '.$maxversion;
                    595:     print $logfile "\nCreating old version ".$maxversion;
                    596: 
                    597:     my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
                    598: 
1.13      www       599:         if (copy($target,$copyfile)) {
1.12      www       600: 	    print $logfile "Copied old target to ".$copyfile."\n";
                    601:             $scrout.='<p>Copied old target file';
                    602:         } else {
1.13      www       603: 	    print $logfile "Unable to write ".$copyfile.':'.$!."\n";
                    604:            return "<font color=red>Failed to copy old target, $!, FAIL</font>";
1.12      www       605:         }
                    606: 
                    607: # --------------------------------------------------------------- Copy Metadata
                    608: 
                    609: 	$copyfile=$copyfile.'.meta';
1.13      www       610: 
                    611:         if (copy($target.'.meta',$copyfile)) {
1.14      www       612: 	    print $logfile "Copied old target metadata to ".$copyfile."\n";
1.12      www       613:             $scrout.='<p>Copied old metadata';
                    614:         } else {
1.13      www       615: 	    print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1.14      www       616:             if (-e $target.'.meta') {
                    617:                return 
1.13      www       618:        "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
1.14      www       619: 	    }
1.12      www       620:         }
1.11      www       621: 
                    622: 
1.12      www       623: } else {
                    624:     $scrout.='<p>Initial version';
                    625:     print $logfile "\nInitial version";
                    626: }
                    627: 
                    628: # ---------------------------------------------------------------- Write Source
                    629: 	my $copyfile=$target;
                    630: 
                    631:            my @parts=split(/\//,$copyfile);
                    632:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
                    633: 
                    634:            my $count;
                    635:            for ($count=5;$count<$#parts;$count++) {
                    636:                $path.="/$parts[$count]";
                    637:                if ((-e $path)!=1) {
                    638:                    print $logfile "\nCreating directory ".$path;
                    639:                    $scrout.='<p>Created directory '.$parts[$count];
                    640: 		   mkdir($path,0777);
                    641:                }
                    642:            }
                    643: 
1.13      www       644:         if (copy($source,$copyfile)) {
1.12      www       645: 	    print $logfile "Copied original source to ".$copyfile."\n";
                    646:             $scrout.='<p>Copied source file';
                    647:         } else {
1.13      www       648: 	    print $logfile "Unable to write ".$copyfile.':'.$!."\n";
                    649:             return "<font color=red>Failed to copy source, $!, FAIL</font>";
1.12      www       650:         }
                    651: 
                    652: # --------------------------------------------------------------- Copy Metadata
                    653: 
1.13      www       654:         $copyfile=$copyfile.'.meta';
                    655: 
                    656:         if (copy($source.'.meta',$copyfile)) {
1.12      www       657: 	    print $logfile "Copied original metadata to ".$copyfile."\n";
                    658:             $scrout.='<p>Copied metadata';
                    659:         } else {
1.13      www       660: 	    print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1.12      www       661:             return 
1.13      www       662:           "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
1.12      www       663:         }
                    664: 
                    665: # --------------------------------------------------- Send update notifications
                    666: 
                    667: {
                    668: 
                    669:     my $filename;
                    670:  
                    671:     $target=~/(.*)\/([^\/]+)$/;
                    672:     my $srcf=$2;
                    673:     opendir(DIR,$1);
                    674:     while ($filename=readdir(DIR)) {
                    675:        if ($filename=~/$srcf\.(\w+)$/) {
                    676: 	   my $subhost=$1;
                    677:            if ($subhost ne 'meta') {
                    678: 	       $scrout.='<p>Notifying host '.$subhost.':';
                    679:                print $logfile "\nNotifying host '.$subhost.':'";
                    680:                my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
1.20      www       681:                $scrout.=$reply;
                    682:                print $logfile $reply;              
                    683:            }
                    684:        }
                    685:     }
                    686:     closedir(DIR);
                    687: 
                    688: }
                    689: 
                    690: # ---------------------------------------- Send update notifications, meta only
                    691: 
                    692: {
                    693: 
                    694:     my $filename;
                    695:  
                    696:     $target=~/(.*)\/([^\/]+)$/;
                    697:     my $srcf=$2.'.meta';
                    698:     opendir(DIR,$1);
                    699:     while ($filename=readdir(DIR)) {
                    700:        if ($filename=~/$srcf\.(\w+)$/) {
                    701: 	   my $subhost=$1;
                    702:            if ($subhost ne 'meta') {
                    703: 	       $scrout.=
                    704:                 '<p>Notifying host for metadata only '.$subhost.':';
                    705:                print $logfile 
                    706:                 "\nNotifying host for metadata only '.$subhost.':'";
                    707:                my $reply=&Apache::lonnet::critical(
                    708:                                 'update:'.$target.'.meta',$subhost);
1.12      www       709:                $scrout.=$reply;
                    710:                print $logfile $reply;              
                    711:            }
                    712:        }
                    713:     }
                    714:     closedir(DIR);
                    715: 
                    716: }
                    717: 
                    718: # ------------------------------------------------ Provide link to new resource
                    719: 
                    720:     my $thisdistarget=$target;
                    721:     $thisdistarget=~s/^$docroot//;
                    722: 
1.22      www       723:     my $thissrc=$source;
                    724:     $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
                    725: 
                    726:     my $thissrcdir=$thissrc;
                    727:     $thissrcdir=~s/\/[^\/]+$/\//;
                    728: 
                    729: 
1.29      harris41  730:     return $warning.$scrout.
1.22      www       731:       '<hr><a href="'.$thisdistarget.'"><font size=+2>View Target</font></a>'.
                    732:       '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
                    733:       '<p><a href="'.$thissrcdir.
                    734:       '"><font size=+2>Back to Source Directory</font></a>';
                    735: 
1.11      www       736: }
                    737: 
1.1       www       738: # ================================================================ Main Handler
                    739: 
                    740: sub handler {
                    741:   my $r=shift;
1.2       www       742: 
                    743:   if ($r->header_only) {
                    744:      $r->content_type('text/html');
                    745:      $r->send_http_header;
                    746:      return OK;
                    747:   }
                    748: 
                    749: # -------------------------------------------------------------- Check filename
                    750: 
                    751:   my $fn=$ENV{'form.filename'};
                    752: 
1.27      www       753:   
1.2       www       754:   unless ($fn) { 
1.27      www       755:      $r->log_reason($cuname.' at '.$cudom.
1.2       www       756:          ' trying to publish empty filename', $r->filename); 
                    757:      return HTTP_NOT_FOUND;
                    758:   } 
1.4       www       759: 
1.31      www       760:   ($cuname,$cudom)=
                    761:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
                    762:   unless (($cuname) && ($cudom)) {
1.27      www       763:      $r->log_reason($cuname.' at '.$cudom.
1.4       www       764:          ' trying to publish file '.$ENV{'form.filename'}.
1.27      www       765:          ' ('.$fn.') - not authorized', 
                    766:          $r->filename); 
                    767:      return HTTP_NOT_ACCEPTABLE;
                    768:   }
                    769: 
                    770:   unless (&Apache::lonnet::homeserver($cuname,$cudom) 
                    771:           eq $r->dir_config('lonHostID')) {
                    772:      $r->log_reason($cuname.' at '.$cudom.
                    773:          ' trying to publish file '.$ENV{'form.filename'}.
                    774:          ' ('.$fn.') - not homeserver ('.
                    775:          &Apache::lonnet::homeserver($cuname,$cudom).')', 
1.4       www       776:          $r->filename); 
                    777:      return HTTP_NOT_ACCEPTABLE;
                    778:   }
1.2       www       779: 
                    780:   $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
                    781: 
                    782:   my $targetdir='';
1.12      www       783:   $docroot=$r->dir_config('lonDocRoot'); 
1.27      www       784:   if ($1 ne $cuname) {
                    785:      $r->log_reason($cuname.' at '.$cudom.
1.2       www       786:          ' trying to publish unowned file '.$ENV{'form.filename'}.
                    787:          ' ('.$fn.')', 
                    788:          $r->filename); 
                    789:      return HTTP_NOT_ACCEPTABLE;
                    790:   } else {
1.27      www       791:       $targetdir=$docroot.'/res/'.$cudom;
1.2       www       792:   }
                    793:                                  
                    794:   
                    795:   unless (-e $fn) { 
1.27      www       796:      $r->log_reason($cuname.' at '.$cudom.
1.2       www       797:          ' trying to publish non-existing file '.$ENV{'form.filename'}.
                    798:          ' ('.$fn.')', 
                    799:          $r->filename); 
                    800:      return HTTP_NOT_FOUND;
                    801:   } 
                    802: 
1.11      www       803: unless ($ENV{'form.phase'} eq 'two') {
                    804: 
1.2       www       805: # --------------------------------- File is there and owned, init lookup tables
                    806: 
1.3       www       807:   %addid=();
                    808: 
                    809:   {
                    810:       my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
                    811:       while (<$fh>=~/(\w+)\s+(\w+)/) {
                    812:           $addid{$1}=$2;
                    813:       }
1.5       www       814:   }
                    815: 
                    816:   %nokey=();
                    817: 
                    818:   {
                    819:      my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
                    820:       map {
                    821:           my $word=$_;
                    822:           chomp($word);
                    823:           $nokey{$word}=1;
1.9       www       824:       } <$fh>;
                    825:   }
                    826: 
                    827:   %language=();
                    828: 
                    829:   {
                    830:      my $fh=Apache::File->new($r->dir_config('lonTabDir').'/language.tab');
                    831:       map {
1.10      www       832:           $_=~/(\w+)\s+([\w\s\-]+)/;
1.9       www       833:           $language{$1}=$2;
1.10      www       834:       } <$fh>;
                    835:   }
                    836: 
                    837:   %cprtag=();
                    838: 
                    839:   {
                    840:      my $fh=Apache::File->new($r->dir_config('lonIncludes').'/copyright.tab');
                    841:       map {
                    842:           $_=~/(\w+)\s+([\w\s\-]+)/;
                    843:           $cprtag{$1}=$2;
1.5       www       844:       } <$fh>;
1.3       www       845:   }
1.11      www       846: 
                    847: }
                    848: 
1.2       www       849: # ----------------------------------------------------------- Start page output
                    850: 
1.1       www       851:   $r->content_type('text/html');
                    852:   $r->send_http_header;
                    853: 
                    854:   $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
1.15      www       855:   $r->print(
                    856:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
1.2       www       857:   my $thisfn=$fn;
                    858:    
                    859: # ------------------------------------------------------------- Individual file
                    860:   {
                    861:       $thisfn=~/\.(\w+)$/;
                    862:       my $thistype=$1;
                    863:       my $thisembstyle=&Apache::lonnet::fileembstyle($thistype);
                    864: 
                    865:       my $thistarget=$thisfn;
                    866:       
                    867:       $thistarget=~s/^\/home/$targetdir/;
                    868:       $thistarget=~s/\/public\_html//;
                    869: 
                    870:       my $thisdistarget=$thistarget;
                    871:       $thisdistarget=~s/^$docroot//;
                    872: 
                    873:       my $thisdisfn=$thisfn;
1.27      www       874:       $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
1.2       www       875: 
                    876:       $r->print('<h2>Publishing '.
                    877:         &Apache::lonnet::filedescription($thistype).' <tt>'.
                    878:         $thisdisfn.'</tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
1.27      www       879:    
                    880:        if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
                    881:           $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
                    882:                '</font></h3>');
                    883:       }
1.26      www       884: 
                    885:       if (&Apache::lonnet::fileembstyle($thistype) eq 'ssi') {
1.28      www       886:           $r->print('<br><a href="/adm/diff?filename=/~'.$cuname.'/'.
                    887:                     $thisdisfn.
1.26      www       888:   	  '&versionone=priv" target=cat>Diffs with Current Version</a><p>');
                    889:       }
1.11      www       890:   
1.2       www       891: # ------------ We are publishing from $thisfn to $thistarget with $thisembstyle
                    892: 
1.11      www       893:        unless ($ENV{'form.phase'} eq 'two') {
1.27      www       894:          $r->print(
                    895:           '<hr>'.&publish($thisfn,$thistarget,$thisembstyle));
1.11      www       896:        } else {
1.27      www       897:          $r->print(
                    898:           '<hr>'.&phasetwo($thisfn,$thistarget,$thisembstyle,$thisdistarget)); 
1.11      www       899:        }  
1.2       www       900: 
1.11      www       901:   }
1.1       www       902:   $r->print('</body></html>');
1.15      www       903: 
1.1       www       904:   return OK;
                    905: }
                    906: 
                    907: 1;
                    908: __END__
                    909: 
                    910: 
                    911: 
                    912: 
                    913: 
                    914: 
                    915: 

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