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