Annotation of loncom/publisher/lonpublisher.pm, revision 1.48
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.48 ! www 14: # 06/23,08/07,08/11,8/13,8/17,8/18,8/24,9/26,10/16 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'})) {
1.48 ! www 59: $unikey.='.'.$token->[2]->{'id'};
1.32 www 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.48 ! 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.44 www 262: } ('src','href','background');
1.38 www 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.44 www 320: $scrout.='<h3>Dependencies</h3>';
1.36 www 321: my $allowstr="\n";
322: map {
323: $allowstr.='<allow src="'.$_.'" />'."\n";
1.44 www 324: $scrout.='<br>';
325: unless ($_=~/\*/) {
326: $scrout.='<a href="'.$_.'">';
327: }
328: $scrout.='<tt>'.$_.'</tt>';
329: unless ($_=~/\*/) {
330: $scrout.='</a>';
331: }
1.36 www 332: } keys %allow;
333: $outstring=~s/(\<\/[^\>]+\>\s*)$/$allowstr$1/s;
1.37 www 334: }
335: # ------------------------------------------------------------- Write modified
336:
1.4 www 337: {
338: my $org;
339: unless ($org=Apache::File->new('>'.$source)) {
340: print $logfile "No write permit to $source\n";
1.7 www 341: return
342: "<font color=red>No write permission to $source, FAIL</font>";
1.4 www 343: }
344: print $org $outstring;
345: }
346: $content=$outstring;
1.34 www 347:
348: if ($needsfixup) {
1.4 www 349: print $logfile "End of ID and/or index fixup\n".
350: "Max ID : $maxid (min 10)\n".
351: "Max Index: $maxindex (min 10)\n";
352: } else {
353: print $logfile "Does not need ID and/or index fixup\n";
354: }
1.37 www 355: }
1.7 www 356: # --------------------------------------------- Initial step done, now metadata
357:
358: # ---------------------------------------- Storage for metadata keys and fields
359:
1.8 www 360: %metadatafields=();
361: %metadatakeys=();
362:
363: my %oldparmstores=();
1.44 www 364:
365: $scrout.='<h3>Metadata Information</h3>';
1.7 www 366:
367: # ------------------------------------------------ First, check out environment
1.8 www 368: unless (-e $source.'.meta') {
1.7 www 369: $metadatafields{'author'}=$ENV{'environment.firstname'}.' '.
370: $ENV{'environment.middlename'}.' '.
371: $ENV{'environment.lastname'}.' '.
372: $ENV{'environment.generation'};
1.8 www 373: $metadatafields{'author'}=~s/\s+/ /g;
374: $metadatafields{'author'}=~s/\s+$//;
1.27 www 375: $metadatafields{'owner'}=$cuname.'@'.$cudom;
1.7 www 376:
377: # ------------------------------------------------ Check out directory hierachy
378:
379: my $thisdisfn=$source;
1.27 www 380: $thisdisfn=~s/^\/home\/$cuname\///;
1.7 www 381:
382: my @urlparts=split(/\//,$thisdisfn);
383: $#urlparts--;
384:
1.27 www 385: my $currentpath='/home/'.$cuname.'/';
1.7 www 386:
387: map {
388: $currentpath.=$_.'/';
389: $scrout.=&metaread($logfile,$currentpath.'default.meta');
390: } @urlparts;
391:
392: # ------------------- Clear out parameters and stores (there should not be any)
393:
394: map {
395: if (($_=~/^parameter/) || ($_=~/^stores/)) {
396: delete $metadatafields{$_};
397: }
398: } keys %metadatafields;
399:
1.8 www 400: } else {
1.7 www 401: # ---------------------- Read previous metafile, remember parameters and stores
402:
403: $scrout.=&metaread($logfile,$source.'.meta');
404:
405: map {
406: if (($_=~/^parameter/) || ($_=~/^stores/)) {
407: $oldparmstores{$_}=1;
408: delete $metadatafields{$_};
409: }
410: } keys %metadatafields;
411:
1.8 www 412: }
1.7 www 413:
1.4 www 414: # -------------------------------------------------- Parse content for metadata
1.37 www 415: if ($style eq 'ssi') {
1.42 www 416: my $oldenv=$ENV{'request.uri'};
417:
418: $ENV{'request.uri'}=$target;
1.23 www 419: $allmeta=Apache::lonxml::xmlparse('meta',$content);
1.42 www 420: $ENV{'request.uri'}=$oldenv;
1.32 www 421:
1.19 albertel 422: &metaeval($allmeta);
1.37 www 423: }
1.7 www 424: # ---------------- Find and document discrepancies in the parameters and stores
425:
426: my $chparms='';
427: map {
428: if (($_=~/^parameter/) || ($_=~/^stores/)) {
429: unless ($_=~/\.\w+$/) {
430: unless ($oldparmstores{$_}) {
431: print $logfile 'New: '.$_."\n";
432: $chparms.=$_.' ';
433: }
434: }
435: }
436: } sort keys %metadatafields;
437: if ($chparms) {
438: $scrout.='<p><b>New parameters or stored values:</b> '.
439: $chparms;
440: }
441:
442: my $chparms='';
443: map {
444: if (($_=~/^parameter/) || ($_=~/^stores/)) {
1.33 www 445: unless (($metadatafields{$_.'.name'}) ||
446: ($metadatafields{$_.'.package'}) || ($_=~/\.\w+$/)) {
1.7 www 447: print $logfile 'Obsolete: '.$_."\n";
448: $chparms.=$_.' ';
449: }
450: }
451: } sort keys %oldparmstores;
452: if ($chparms) {
453: $scrout.='<p><b>Obsolete parameters or stored values:</b> '.
454: $chparms;
455: }
1.37 www 456:
1.8 www 457: # ------------------------------------------------------- Now have all metadata
1.5 www 458:
1.8 www 459: $scrout.=
460: '<form action="/adm/publish" method="post">'.
1.11 www 461: &hiddenfield('phase','two').
462: &hiddenfield('filename',$ENV{'form.filename'}).
463: &hiddenfield('allmeta',&Apache::lonnet::escape($allmeta)).
1.10 www 464: &textfield('Title','title',$metadatafields{'title'}).
465: &textfield('Author(s)','author',$metadatafields{'author'}).
466: &textfield('Subject','subject',$metadatafields{'subject'});
1.5 www 467:
468: # --------------------------------------------------- Scan content for keywords
1.7 www 469:
1.8 www 470: my $keywordout='<p><b>Keywords:</b><br><table border=2><tr>';
1.7 www 471: my $colcount=0;
472:
1.48 ! www 473: if (length($content)<500000) {
1.5 www 474: my $textonly=$content;
475: $textonly=~s/\<script[^\<]+\<\/script\>//g;
476: $textonly=~s/\<m\>[^\<]+\<\/m\>//g;
477: $textonly=~s/\<[^\>]*\>//g;
478: $textonly=~tr/A-Z/a-z/;
479: $textonly=~s/[\$\&][a-z]\w*//g;
480: $textonly=~s/[^a-z\s]//g;
481:
482: my %keywords=();
1.48 ! www 483: map {
! 484: unless ($nokey{$_}) {
! 485: $keywords{$_}=1;
! 486: }
! 487: } ($textonly=~m/(\w+)/g);
1.5 www 488:
1.12 www 489: map {
490: $keywords{$_}=1;
491: } split(/\W+/,$metadatafields{'keywords'});
1.5 www 492:
1.7 www 493: map {
1.12 www 494: $keywordout.='<td><input type=checkbox name="key.'.$_.'"';
1.8 www 495: if ($metadatafields{'keywords'}=~/$_/) {
496: $keywordout.=' checked';
497: }
498: $keywordout.='>'.$_.'</td>';
1.7 www 499: if ($colcount>10) {
500: $keywordout.="</tr><tr>\n";
501: $colcount=0;
502: }
1.48 ! www 503: $colcount++;
1.7 www 504: } sort keys %keywords;
1.48 ! www 505: } else {
! 506: $keywordout.='<td>File too large, scan omitted</td>';
! 507: }
! 508:
! 509: $keywordout.='</tr></table>';
! 510:
1.7 www 511: $scrout.=$keywordout;
1.9 www 512:
1.12 www 513: $scrout.=&textfield('Additional Keywords','addkey','');
514:
1.10 www 515: $scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
1.9 www 516:
517: $scrout.=
518: '<p><b>Abstract:</b><br><textarea cols=80 rows=5 name=abstract>'.
519: $metadatafields{'abstract'}.'</textarea>';
520:
1.11 www 521: $source=~/\.(\w+)$/;
522:
523: $scrout.=&hiddenfield('mime',$1);
524:
1.10 www 525: $scrout.=&selectbox('Language','language',
526: $metadatafields{'language'},%language);
1.11 www 527:
528: unless ($metadatafields{'creationdate'}) {
529: $metadatafields{'creationdate'}=time;
530: }
531: $scrout.=&hiddenfield('creationdate',$metadatafields{'creationdate'});
532:
533: $scrout.=&hiddenfield('lastrevisiondate',time);
534:
1.9 www 535:
1.10 www 536: $scrout.=&textfield('Publisher/Owner','owner',
537: $metadatafields{'owner'});
1.45 www 538: # --------------------------------------------------- Correct copyright for rat
539: if ($style eq 'rat') {
540: if ($metadatafields{'copyright'} eq 'public') {
541: delete $metadatafields{'copyright'};
542: }
543: delete $cprtag{'public'};
544: }
1.10 www 545:
546: $scrout.=&selectbox('Copyright/Distribution','copyright',
547: $metadatafields{'copyright'},%cprtag);
1.9 www 548:
1.8 www 549: return $scrout.
550: '<p><input type="submit" value="Finalize Publication"></form>';
1.2 www 551: }
1.1 www 552:
1.12 www 553: # -------------------------------------------------------- Publication Step Two
554:
1.11 www 555: sub phasetwo {
556:
1.24 harris41 557: my ($source,$target,$style,$distarget)=@_;
1.11 www 558: my $logfile;
559: my $scrout='';
560:
561: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
562: return
563: '<font color=red>No write permission to user directory, FAIL</font>';
564: }
565: print $logfile
566: "\n================= Publish ".localtime()." Phase Two ================\n";
567:
568: %metadatafields=();
569: %metadatakeys=();
570:
571: &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
572:
573: $metadatafields{'title'}=$ENV{'form.title'};
574: $metadatafields{'author'}=$ENV{'form.author'};
575: $metadatafields{'subject'}=$ENV{'form.subject'};
576: $metadatafields{'notes'}=$ENV{'form.notes'};
577: $metadatafields{'abstract'}=$ENV{'form.abstract'};
578: $metadatafields{'mime'}=$ENV{'form.mime'};
579: $metadatafields{'language'}=$ENV{'form.language'};
580: $metadatafields{'creationdate'}=$ENV{'form.creationdate'};
581: $metadatafields{'lastrevisiondate'}=$ENV{'form.lastrevisiondate'};
582: $metadatafields{'owner'}=$ENV{'form.owner'};
583: $metadatafields{'copyright'}=$ENV{'form.copyright'};
1.12 www 584:
585: my $allkeywords=$ENV{'form.addkey'};
1.11 www 586: map {
1.12 www 587: if ($_=~/^form\.key\.(\w+)/) {
588: $allkeywords.=','.$1;
589: }
590: } keys %ENV;
591: $allkeywords=~s/\W+/\,/;
592: $allkeywords=~s/^\,//;
593: $metadatafields{'keywords'}=$allkeywords;
594:
595: {
596: print $logfile "\nWrite metadata file for ".$source;
597: my $mfh;
598: unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
599: return
600: '<font color=red>Could not write metadata, FAIL</font>';
601: }
602: map {
603: unless ($_=~/\./) {
604: my $unikey=$_;
605: $unikey=~/^([A-Za-z]+)/;
606: my $tag=$1;
607: $tag=~tr/A-Z/a-z/;
608: print $mfh "\n\<$tag";
609: map {
610: my $value=$metadatafields{$unikey.'.'.$_};
611: $value=~s/\"/\'\'/g;
612: print $mfh ' '.$_.'="'.$value.'"';
613: } split(/\,/,$metadatakeys{$unikey});
614: print $mfh '>'.$metadatafields{$unikey}.'</'.$tag.'>';
615: }
616: } sort keys %metadatafields;
617: $scrout.='<p>Wrote Metadata';
618: print $logfile "\nWrote metadata";
619: }
620:
1.24 harris41 621: # -------------------------------- Synchronize entry with SQL metadata database
1.25 harris41 622: my %perlvar;
623: open (CONFIG,"/etc/httpd/conf/access.conf") || die "Can't read access.conf";
624: my $configline;
625: while ($configline=<CONFIG>) {
626: if ($configline =~ /PerlSetVar/) {
627: my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
628: chomp($varvalue);
629: $perlvar{$varname}=$varvalue;
630: }
631: }
632: close(CONFIG);
633:
1.29 harris41 634: my $warning;
1.24 harris41 635: my $dbh;
636: {
637: unless (
638: $dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
639: ) {
1.29 harris41 640: $warning='<font color=red>WARNING: Cannot connect to '.
641: 'database!</font>';
642: }
643: else {
644: my %sqldatafields;
645: $sqldatafields{'url'}=$distarget;
646: my $sth=$dbh->prepare(
647: 'delete from metadata where url like binary'.
648: '"'.$sqldatafields{'url'}.'"');
649: $sth->execute();
650: map {my $field=$metadatafields{$_}; $field=~s/\"/\'\'/g;
651: $sqldatafields{$_}=$field;}
652: ('title','author','subject','keywords','notes','abstract',
653: 'mime','language','creationdate','lastrevisiondate','owner',
654: 'copyright');
655:
656: $sth=$dbh->prepare('insert into metadata values ('.
657: '"'.delete($sqldatafields{'title'}).'"'.','.
658: '"'.delete($sqldatafields{'author'}).'"'.','.
659: '"'.delete($sqldatafields{'subject'}).'"'.','.
660: '"'.delete($sqldatafields{'url'}).'"'.','.
661: '"'.delete($sqldatafields{'keywords'}).'"'.','.
662: '"'.'current'.'"'.','.
663: '"'.delete($sqldatafields{'notes'}).'"'.','.
664: '"'.delete($sqldatafields{'abstract'}).'"'.','.
665: '"'.delete($sqldatafields{'mime'}).'"'.','.
666: '"'.delete($sqldatafields{'language'}).'"'.','.
667: '"'.
668: sqltime(delete($sqldatafields{'creationdate'}))
669: .'"'.','.
670: '"'.
671: sqltime(delete(
672: $sqldatafields{'lastrevisiondate'})).'"'.','.
673: '"'.delete($sqldatafields{'owner'}).'"'.','.
674: '"'.delete(
675: $sqldatafields{'copyright'}).'"'.')');
676: $sth->execute();
677: $dbh->disconnect;
678: $scrout.='<p>Synchronized SQL metadata database';
679: print $logfile "\nSynchronized SQL metadata database";
1.24 harris41 680: }
681: }
682:
683:
1.12 www 684: # ----------------------------------------------------------- Copy old versions
685:
686: if (-e $target) {
687: my $filename;
688: my $maxversion=0;
689: $target=~/(.*)\/([^\/]+)\.(\w+)$/;
690: my $srcf=$2;
691: my $srct=$3;
692: my $srcd=$1;
693: unless ($srcd=~/^\/home\/httpd\/html\/res/) {
694: print $logfile "\nPANIC: Target dir is ".$srcd;
695: return "<font color=red>Invalid target directory, FAIL</font>";
696: }
697: opendir(DIR,$srcd);
698: while ($filename=readdir(DIR)) {
699: if ($filename=~/$srcf\.(\d+)\.$srct$/) {
700: $maxversion=($1>$maxversion)?$1:$maxversion;
701: }
702: }
703: closedir(DIR);
704: $maxversion++;
705: $scrout.='<p>Creating old version '.$maxversion;
706: print $logfile "\nCreating old version ".$maxversion;
707:
708: my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
709:
1.13 www 710: if (copy($target,$copyfile)) {
1.12 www 711: print $logfile "Copied old target to ".$copyfile."\n";
712: $scrout.='<p>Copied old target file';
713: } else {
1.13 www 714: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
715: return "<font color=red>Failed to copy old target, $!, FAIL</font>";
1.12 www 716: }
717:
718: # --------------------------------------------------------------- Copy Metadata
719:
720: $copyfile=$copyfile.'.meta';
1.13 www 721:
722: if (copy($target.'.meta',$copyfile)) {
1.14 www 723: print $logfile "Copied old target metadata to ".$copyfile."\n";
1.12 www 724: $scrout.='<p>Copied old metadata';
725: } else {
1.13 www 726: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1.14 www 727: if (-e $target.'.meta') {
728: return
1.13 www 729: "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
1.14 www 730: }
1.12 www 731: }
1.11 www 732:
733:
1.12 www 734: } else {
735: $scrout.='<p>Initial version';
736: print $logfile "\nInitial version";
737: }
738:
739: # ---------------------------------------------------------------- Write Source
740: my $copyfile=$target;
741:
742: my @parts=split(/\//,$copyfile);
743: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
744:
745: my $count;
746: for ($count=5;$count<$#parts;$count++) {
747: $path.="/$parts[$count]";
748: if ((-e $path)!=1) {
749: print $logfile "\nCreating directory ".$path;
750: $scrout.='<p>Created directory '.$parts[$count];
751: mkdir($path,0777);
752: }
753: }
754:
1.13 www 755: if (copy($source,$copyfile)) {
1.12 www 756: print $logfile "Copied original source to ".$copyfile."\n";
757: $scrout.='<p>Copied source file';
758: } else {
1.13 www 759: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
760: return "<font color=red>Failed to copy source, $!, FAIL</font>";
1.12 www 761: }
762:
763: # --------------------------------------------------------------- Copy Metadata
764:
1.13 www 765: $copyfile=$copyfile.'.meta';
766:
767: if (copy($source.'.meta',$copyfile)) {
1.12 www 768: print $logfile "Copied original metadata to ".$copyfile."\n";
769: $scrout.='<p>Copied metadata';
770: } else {
1.13 www 771: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1.12 www 772: return
1.13 www 773: "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
1.12 www 774: }
775:
776: # --------------------------------------------------- Send update notifications
777:
778: {
779:
780: my $filename;
781:
782: $target=~/(.*)\/([^\/]+)$/;
783: my $srcf=$2;
784: opendir(DIR,$1);
785: while ($filename=readdir(DIR)) {
786: if ($filename=~/$srcf\.(\w+)$/) {
787: my $subhost=$1;
788: if ($subhost ne 'meta') {
789: $scrout.='<p>Notifying host '.$subhost.':';
790: print $logfile "\nNotifying host '.$subhost.':'";
791: my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
1.20 www 792: $scrout.=$reply;
793: print $logfile $reply;
794: }
795: }
796: }
797: closedir(DIR);
798:
799: }
800:
801: # ---------------------------------------- Send update notifications, meta only
802:
803: {
804:
805: my $filename;
806:
807: $target=~/(.*)\/([^\/]+)$/;
808: my $srcf=$2.'.meta';
809: opendir(DIR,$1);
810: while ($filename=readdir(DIR)) {
811: if ($filename=~/$srcf\.(\w+)$/) {
812: my $subhost=$1;
813: if ($subhost ne 'meta') {
814: $scrout.=
815: '<p>Notifying host for metadata only '.$subhost.':';
816: print $logfile
817: "\nNotifying host for metadata only '.$subhost.':'";
818: my $reply=&Apache::lonnet::critical(
819: 'update:'.$target.'.meta',$subhost);
1.12 www 820: $scrout.=$reply;
821: print $logfile $reply;
822: }
823: }
824: }
825: closedir(DIR);
826:
827: }
828:
829: # ------------------------------------------------ Provide link to new resource
830:
831: my $thisdistarget=$target;
832: $thisdistarget=~s/^$docroot//;
833:
1.22 www 834: my $thissrc=$source;
835: $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
836:
837: my $thissrcdir=$thissrc;
838: $thissrcdir=~s/\/[^\/]+$/\//;
839:
840:
1.29 harris41 841: return $warning.$scrout.
1.22 www 842: '<hr><a href="'.$thisdistarget.'"><font size=+2>View Target</font></a>'.
843: '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
844: '<p><a href="'.$thissrcdir.
845: '"><font size=+2>Back to Source Directory</font></a>';
846:
1.11 www 847: }
848:
1.1 www 849: # ================================================================ Main Handler
850:
851: sub handler {
852: my $r=shift;
1.2 www 853:
854: if ($r->header_only) {
855: $r->content_type('text/html');
856: $r->send_http_header;
857: return OK;
858: }
859:
1.43 www 860: # Get query string for limited number of parameters
861:
862: map {
863: my ($name, $value) = split(/=/,$_);
864: $value =~ tr/+/ /;
865: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
866: if ($name eq 'filename') {
867: unless ($ENV{'form.'.$name}) {
868: $ENV{'form.'.$name}=$value;
869: }
870: }
871: } (split(/&/,$ENV{'QUERY_STRING'}));
872:
873:
1.2 www 874: # -------------------------------------------------------------- Check filename
875:
876: my $fn=$ENV{'form.filename'};
877:
1.27 www 878:
1.2 www 879: unless ($fn) {
1.27 www 880: $r->log_reason($cuname.' at '.$cudom.
1.2 www 881: ' trying to publish empty filename', $r->filename);
882: return HTTP_NOT_FOUND;
883: }
1.4 www 884:
1.31 www 885: ($cuname,$cudom)=
886: &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
887: unless (($cuname) && ($cudom)) {
1.27 www 888: $r->log_reason($cuname.' at '.$cudom.
1.4 www 889: ' trying to publish file '.$ENV{'form.filename'}.
1.27 www 890: ' ('.$fn.') - not authorized',
891: $r->filename);
892: return HTTP_NOT_ACCEPTABLE;
893: }
894:
895: unless (&Apache::lonnet::homeserver($cuname,$cudom)
896: eq $r->dir_config('lonHostID')) {
897: $r->log_reason($cuname.' at '.$cudom.
898: ' trying to publish file '.$ENV{'form.filename'}.
899: ' ('.$fn.') - not homeserver ('.
900: &Apache::lonnet::homeserver($cuname,$cudom).')',
1.4 www 901: $r->filename);
902: return HTTP_NOT_ACCEPTABLE;
903: }
1.2 www 904:
1.43 www 905: $fn=~s/^http\:\/\/[^\/]+//;
906: $fn=~s/^\/\~(\w+)/\/home\/$1\/public_html/;
1.2 www 907:
908: my $targetdir='';
1.12 www 909: $docroot=$r->dir_config('lonDocRoot');
1.27 www 910: if ($1 ne $cuname) {
911: $r->log_reason($cuname.' at '.$cudom.
1.2 www 912: ' trying to publish unowned file '.$ENV{'form.filename'}.
913: ' ('.$fn.')',
914: $r->filename);
915: return HTTP_NOT_ACCEPTABLE;
916: } else {
1.27 www 917: $targetdir=$docroot.'/res/'.$cudom;
1.2 www 918: }
919:
920:
921: unless (-e $fn) {
1.27 www 922: $r->log_reason($cuname.' at '.$cudom.
1.2 www 923: ' trying to publish non-existing file '.$ENV{'form.filename'}.
924: ' ('.$fn.')',
925: $r->filename);
926: return HTTP_NOT_FOUND;
927: }
928:
1.11 www 929: unless ($ENV{'form.phase'} eq 'two') {
930:
1.2 www 931: # --------------------------------- File is there and owned, init lookup tables
932:
1.3 www 933: %addid=();
934:
935: {
936: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
937: while (<$fh>=~/(\w+)\s+(\w+)/) {
938: $addid{$1}=$2;
939: }
1.5 www 940: }
941:
942: %nokey=();
943:
944: {
945: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
946: map {
947: my $word=$_;
948: chomp($word);
949: $nokey{$word}=1;
1.9 www 950: } <$fh>;
951: }
952:
953: %language=();
954:
955: {
956: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/language.tab');
957: map {
1.10 www 958: $_=~/(\w+)\s+([\w\s\-]+)/;
1.9 www 959: $language{$1}=$2;
1.10 www 960: } <$fh>;
961: }
962:
963: %cprtag=();
964:
965: {
966: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/copyright.tab');
967: map {
968: $_=~/(\w+)\s+([\w\s\-]+)/;
969: $cprtag{$1}=$2;
1.5 www 970: } <$fh>;
1.3 www 971: }
1.11 www 972:
973: }
974:
1.2 www 975: # ----------------------------------------------------------- Start page output
976:
1.1 www 977: $r->content_type('text/html');
978: $r->send_http_header;
979:
980: $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
1.15 www 981: $r->print(
982: '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
1.2 www 983: my $thisfn=$fn;
984:
985: # ------------------------------------------------------------- Individual file
986: {
987: $thisfn=~/\.(\w+)$/;
988: my $thistype=$1;
989: my $thisembstyle=&Apache::lonnet::fileembstyle($thistype);
990:
991: my $thistarget=$thisfn;
992:
993: $thistarget=~s/^\/home/$targetdir/;
994: $thistarget=~s/\/public\_html//;
995:
996: my $thisdistarget=$thistarget;
997: $thisdistarget=~s/^$docroot//;
998:
999: my $thisdisfn=$thisfn;
1.27 www 1000: $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
1.2 www 1001:
1002: $r->print('<h2>Publishing '.
1003: &Apache::lonnet::filedescription($thistype).' <tt>'.
1004: $thisdisfn.'</tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
1.27 www 1005:
1006: if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
1007: $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
1008: '</font></h3>');
1009: }
1.26 www 1010:
1011: if (&Apache::lonnet::fileembstyle($thistype) eq 'ssi') {
1.28 www 1012: $r->print('<br><a href="/adm/diff?filename=/~'.$cuname.'/'.
1013: $thisdisfn.
1.26 www 1014: '&versionone=priv" target=cat>Diffs with Current Version</a><p>');
1015: }
1.11 www 1016:
1.2 www 1017: # ------------ We are publishing from $thisfn to $thistarget with $thisembstyle
1018:
1.11 www 1019: unless ($ENV{'form.phase'} eq 'two') {
1.27 www 1020: $r->print(
1021: '<hr>'.&publish($thisfn,$thistarget,$thisembstyle));
1.11 www 1022: } else {
1.27 www 1023: $r->print(
1024: '<hr>'.&phasetwo($thisfn,$thistarget,$thisembstyle,$thisdistarget));
1.11 www 1025: }
1.2 www 1026:
1.11 www 1027: }
1.1 www 1028: $r->print('</body></html>');
1.15 www 1029:
1.1 www 1030: return OK;
1031: }
1032:
1033: 1;
1034: __END__
1035:
1036:
1037:
1038:
1039:
1040:
1041:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>