Annotation of loncom/publisher/lonpublisher.pm, revision 1.52
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.51 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.49 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.50 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.52 ! albertel 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.50 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.50 www 503: $colcount++;
1.7 www 504: } sort keys %keywords;
1.49 www 505:
1.51 www 506: } else {
507: $keywordout.='<td>File too long for keyword analysis</td>';
1.49 www 508: }
1.50 www 509:
1.51 www 510: $keywordout.='</tr></table>';
511:
512: $scrout.=$keywordout;
1.9 www 513:
1.12 www 514: $scrout.=&textfield('Additional Keywords','addkey','');
515:
1.10 www 516: $scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
1.9 www 517:
518: $scrout.=
519: '<p><b>Abstract:</b><br><textarea cols=80 rows=5 name=abstract>'.
520: $metadatafields{'abstract'}.'</textarea>';
521:
1.11 www 522: $source=~/\.(\w+)$/;
523:
524: $scrout.=&hiddenfield('mime',$1);
525:
1.10 www 526: $scrout.=&selectbox('Language','language',
527: $metadatafields{'language'},%language);
1.11 www 528:
529: unless ($metadatafields{'creationdate'}) {
530: $metadatafields{'creationdate'}=time;
531: }
532: $scrout.=&hiddenfield('creationdate',$metadatafields{'creationdate'});
533:
534: $scrout.=&hiddenfield('lastrevisiondate',time);
535:
1.9 www 536:
1.10 www 537: $scrout.=&textfield('Publisher/Owner','owner',
538: $metadatafields{'owner'});
1.45 www 539: # --------------------------------------------------- Correct copyright for rat
540: if ($style eq 'rat') {
541: if ($metadatafields{'copyright'} eq 'public') {
542: delete $metadatafields{'copyright'};
543: }
544: delete $cprtag{'public'};
545: }
1.10 www 546:
547: $scrout.=&selectbox('Copyright/Distribution','copyright',
548: $metadatafields{'copyright'},%cprtag);
1.9 www 549:
1.8 www 550: return $scrout.
551: '<p><input type="submit" value="Finalize Publication"></form>';
1.2 www 552: }
1.1 www 553:
1.12 www 554: # -------------------------------------------------------- Publication Step Two
555:
1.11 www 556: sub phasetwo {
557:
1.24 harris41 558: my ($source,$target,$style,$distarget)=@_;
1.11 www 559: my $logfile;
560: my $scrout='';
561:
562: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
563: return
564: '<font color=red>No write permission to user directory, FAIL</font>';
565: }
566: print $logfile
567: "\n================= Publish ".localtime()." Phase Two ================\n";
568:
569: %metadatafields=();
570: %metadatakeys=();
571:
572: &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
573:
574: $metadatafields{'title'}=$ENV{'form.title'};
575: $metadatafields{'author'}=$ENV{'form.author'};
576: $metadatafields{'subject'}=$ENV{'form.subject'};
577: $metadatafields{'notes'}=$ENV{'form.notes'};
578: $metadatafields{'abstract'}=$ENV{'form.abstract'};
579: $metadatafields{'mime'}=$ENV{'form.mime'};
580: $metadatafields{'language'}=$ENV{'form.language'};
581: $metadatafields{'creationdate'}=$ENV{'form.creationdate'};
582: $metadatafields{'lastrevisiondate'}=$ENV{'form.lastrevisiondate'};
583: $metadatafields{'owner'}=$ENV{'form.owner'};
584: $metadatafields{'copyright'}=$ENV{'form.copyright'};
1.12 www 585:
586: my $allkeywords=$ENV{'form.addkey'};
1.11 www 587: map {
1.12 www 588: if ($_=~/^form\.key\.(\w+)/) {
589: $allkeywords.=','.$1;
590: }
591: } keys %ENV;
592: $allkeywords=~s/\W+/\,/;
593: $allkeywords=~s/^\,//;
594: $metadatafields{'keywords'}=$allkeywords;
595:
596: {
597: print $logfile "\nWrite metadata file for ".$source;
598: my $mfh;
599: unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
600: return
601: '<font color=red>Could not write metadata, FAIL</font>';
602: }
603: map {
604: unless ($_=~/\./) {
605: my $unikey=$_;
606: $unikey=~/^([A-Za-z]+)/;
607: my $tag=$1;
608: $tag=~tr/A-Z/a-z/;
609: print $mfh "\n\<$tag";
610: map {
611: my $value=$metadatafields{$unikey.'.'.$_};
612: $value=~s/\"/\'\'/g;
613: print $mfh ' '.$_.'="'.$value.'"';
614: } split(/\,/,$metadatakeys{$unikey});
615: print $mfh '>'.$metadatafields{$unikey}.'</'.$tag.'>';
616: }
617: } sort keys %metadatafields;
618: $scrout.='<p>Wrote Metadata';
619: print $logfile "\nWrote metadata";
620: }
621:
1.24 harris41 622: # -------------------------------- Synchronize entry with SQL metadata database
1.25 harris41 623: my %perlvar;
624: open (CONFIG,"/etc/httpd/conf/access.conf") || die "Can't read access.conf";
625: my $configline;
626: while ($configline=<CONFIG>) {
627: if ($configline =~ /PerlSetVar/) {
628: my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
629: chomp($varvalue);
630: $perlvar{$varname}=$varvalue;
631: }
632: }
633: close(CONFIG);
634:
1.29 harris41 635: my $warning;
1.24 harris41 636: my $dbh;
637: {
638: unless (
639: $dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
640: ) {
1.29 harris41 641: $warning='<font color=red>WARNING: Cannot connect to '.
642: 'database!</font>';
643: }
644: else {
645: my %sqldatafields;
646: $sqldatafields{'url'}=$distarget;
647: my $sth=$dbh->prepare(
648: 'delete from metadata where url like binary'.
649: '"'.$sqldatafields{'url'}.'"');
650: $sth->execute();
651: map {my $field=$metadatafields{$_}; $field=~s/\"/\'\'/g;
652: $sqldatafields{$_}=$field;}
653: ('title','author','subject','keywords','notes','abstract',
654: 'mime','language','creationdate','lastrevisiondate','owner',
655: 'copyright');
656:
657: $sth=$dbh->prepare('insert into metadata values ('.
658: '"'.delete($sqldatafields{'title'}).'"'.','.
659: '"'.delete($sqldatafields{'author'}).'"'.','.
660: '"'.delete($sqldatafields{'subject'}).'"'.','.
661: '"'.delete($sqldatafields{'url'}).'"'.','.
662: '"'.delete($sqldatafields{'keywords'}).'"'.','.
663: '"'.'current'.'"'.','.
664: '"'.delete($sqldatafields{'notes'}).'"'.','.
665: '"'.delete($sqldatafields{'abstract'}).'"'.','.
666: '"'.delete($sqldatafields{'mime'}).'"'.','.
667: '"'.delete($sqldatafields{'language'}).'"'.','.
668: '"'.
669: sqltime(delete($sqldatafields{'creationdate'}))
670: .'"'.','.
671: '"'.
672: sqltime(delete(
673: $sqldatafields{'lastrevisiondate'})).'"'.','.
674: '"'.delete($sqldatafields{'owner'}).'"'.','.
675: '"'.delete(
676: $sqldatafields{'copyright'}).'"'.')');
677: $sth->execute();
678: $dbh->disconnect;
679: $scrout.='<p>Synchronized SQL metadata database';
680: print $logfile "\nSynchronized SQL metadata database";
1.24 harris41 681: }
682: }
683:
684:
1.12 www 685: # ----------------------------------------------------------- Copy old versions
686:
687: if (-e $target) {
688: my $filename;
689: my $maxversion=0;
690: $target=~/(.*)\/([^\/]+)\.(\w+)$/;
691: my $srcf=$2;
692: my $srct=$3;
693: my $srcd=$1;
694: unless ($srcd=~/^\/home\/httpd\/html\/res/) {
695: print $logfile "\nPANIC: Target dir is ".$srcd;
696: return "<font color=red>Invalid target directory, FAIL</font>";
697: }
698: opendir(DIR,$srcd);
699: while ($filename=readdir(DIR)) {
700: if ($filename=~/$srcf\.(\d+)\.$srct$/) {
701: $maxversion=($1>$maxversion)?$1:$maxversion;
702: }
703: }
704: closedir(DIR);
705: $maxversion++;
706: $scrout.='<p>Creating old version '.$maxversion;
707: print $logfile "\nCreating old version ".$maxversion;
708:
709: my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
710:
1.13 www 711: if (copy($target,$copyfile)) {
1.12 www 712: print $logfile "Copied old target to ".$copyfile."\n";
713: $scrout.='<p>Copied old target file';
714: } else {
1.13 www 715: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
716: return "<font color=red>Failed to copy old target, $!, FAIL</font>";
1.12 www 717: }
718:
719: # --------------------------------------------------------------- Copy Metadata
720:
721: $copyfile=$copyfile.'.meta';
1.13 www 722:
723: if (copy($target.'.meta',$copyfile)) {
1.14 www 724: print $logfile "Copied old target metadata to ".$copyfile."\n";
1.12 www 725: $scrout.='<p>Copied old metadata';
726: } else {
1.13 www 727: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1.14 www 728: if (-e $target.'.meta') {
729: return
1.13 www 730: "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
1.14 www 731: }
1.12 www 732: }
1.11 www 733:
734:
1.12 www 735: } else {
736: $scrout.='<p>Initial version';
737: print $logfile "\nInitial version";
738: }
739:
740: # ---------------------------------------------------------------- Write Source
741: my $copyfile=$target;
742:
743: my @parts=split(/\//,$copyfile);
744: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
745:
746: my $count;
747: for ($count=5;$count<$#parts;$count++) {
748: $path.="/$parts[$count]";
749: if ((-e $path)!=1) {
750: print $logfile "\nCreating directory ".$path;
751: $scrout.='<p>Created directory '.$parts[$count];
752: mkdir($path,0777);
753: }
754: }
755:
1.13 www 756: if (copy($source,$copyfile)) {
1.12 www 757: print $logfile "Copied original source to ".$copyfile."\n";
758: $scrout.='<p>Copied source file';
759: } else {
1.13 www 760: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
761: return "<font color=red>Failed to copy source, $!, FAIL</font>";
1.12 www 762: }
763:
764: # --------------------------------------------------------------- Copy Metadata
765:
1.13 www 766: $copyfile=$copyfile.'.meta';
767:
768: if (copy($source.'.meta',$copyfile)) {
1.12 www 769: print $logfile "Copied original metadata to ".$copyfile."\n";
770: $scrout.='<p>Copied metadata';
771: } else {
1.13 www 772: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1.12 www 773: return
1.13 www 774: "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
1.12 www 775: }
776:
777: # --------------------------------------------------- Send update notifications
778:
779: {
780:
781: my $filename;
782:
783: $target=~/(.*)\/([^\/]+)$/;
784: my $srcf=$2;
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.='<p>Notifying host '.$subhost.':';
791: print $logfile "\nNotifying host '.$subhost.':'";
792: my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
1.20 www 793: $scrout.=$reply;
794: print $logfile $reply;
795: }
796: }
797: }
798: closedir(DIR);
799:
800: }
801:
802: # ---------------------------------------- Send update notifications, meta only
803:
804: {
805:
806: my $filename;
807:
808: $target=~/(.*)\/([^\/]+)$/;
809: my $srcf=$2.'.meta';
810: opendir(DIR,$1);
811: while ($filename=readdir(DIR)) {
812: if ($filename=~/$srcf\.(\w+)$/) {
813: my $subhost=$1;
814: if ($subhost ne 'meta') {
815: $scrout.=
816: '<p>Notifying host for metadata only '.$subhost.':';
817: print $logfile
818: "\nNotifying host for metadata only '.$subhost.':'";
819: my $reply=&Apache::lonnet::critical(
820: 'update:'.$target.'.meta',$subhost);
1.12 www 821: $scrout.=$reply;
822: print $logfile $reply;
823: }
824: }
825: }
826: closedir(DIR);
827:
828: }
829:
830: # ------------------------------------------------ Provide link to new resource
831:
832: my $thisdistarget=$target;
833: $thisdistarget=~s/^$docroot//;
834:
1.22 www 835: my $thissrc=$source;
836: $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
837:
838: my $thissrcdir=$thissrc;
839: $thissrcdir=~s/\/[^\/]+$/\//;
840:
841:
1.29 harris41 842: return $warning.$scrout.
1.22 www 843: '<hr><a href="'.$thisdistarget.'"><font size=+2>View Target</font></a>'.
844: '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
845: '<p><a href="'.$thissrcdir.
846: '"><font size=+2>Back to Source Directory</font></a>';
847:
1.11 www 848: }
849:
1.1 www 850: # ================================================================ Main Handler
851:
852: sub handler {
853: my $r=shift;
1.2 www 854:
855: if ($r->header_only) {
856: $r->content_type('text/html');
857: $r->send_http_header;
858: return OK;
859: }
860:
1.43 www 861: # Get query string for limited number of parameters
862:
863: map {
864: my ($name, $value) = split(/=/,$_);
865: $value =~ tr/+/ /;
866: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
867: if ($name eq 'filename') {
868: unless ($ENV{'form.'.$name}) {
869: $ENV{'form.'.$name}=$value;
870: }
871: }
872: } (split(/&/,$ENV{'QUERY_STRING'}));
873:
874:
1.2 www 875: # -------------------------------------------------------------- Check filename
876:
877: my $fn=$ENV{'form.filename'};
878:
1.27 www 879:
1.2 www 880: unless ($fn) {
1.27 www 881: $r->log_reason($cuname.' at '.$cudom.
1.2 www 882: ' trying to publish empty filename', $r->filename);
883: return HTTP_NOT_FOUND;
884: }
1.4 www 885:
1.31 www 886: ($cuname,$cudom)=
887: &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
888: unless (($cuname) && ($cudom)) {
1.27 www 889: $r->log_reason($cuname.' at '.$cudom.
1.4 www 890: ' trying to publish file '.$ENV{'form.filename'}.
1.27 www 891: ' ('.$fn.') - not authorized',
892: $r->filename);
893: return HTTP_NOT_ACCEPTABLE;
894: }
895:
896: unless (&Apache::lonnet::homeserver($cuname,$cudom)
897: eq $r->dir_config('lonHostID')) {
898: $r->log_reason($cuname.' at '.$cudom.
899: ' trying to publish file '.$ENV{'form.filename'}.
900: ' ('.$fn.') - not homeserver ('.
901: &Apache::lonnet::homeserver($cuname,$cudom).')',
1.4 www 902: $r->filename);
903: return HTTP_NOT_ACCEPTABLE;
904: }
1.2 www 905:
1.43 www 906: $fn=~s/^http\:\/\/[^\/]+//;
907: $fn=~s/^\/\~(\w+)/\/home\/$1\/public_html/;
1.2 www 908:
909: my $targetdir='';
1.12 www 910: $docroot=$r->dir_config('lonDocRoot');
1.27 www 911: if ($1 ne $cuname) {
912: $r->log_reason($cuname.' at '.$cudom.
1.2 www 913: ' trying to publish unowned file '.$ENV{'form.filename'}.
914: ' ('.$fn.')',
915: $r->filename);
916: return HTTP_NOT_ACCEPTABLE;
917: } else {
1.27 www 918: $targetdir=$docroot.'/res/'.$cudom;
1.2 www 919: }
920:
921:
922: unless (-e $fn) {
1.27 www 923: $r->log_reason($cuname.' at '.$cudom.
1.2 www 924: ' trying to publish non-existing file '.$ENV{'form.filename'}.
925: ' ('.$fn.')',
926: $r->filename);
927: return HTTP_NOT_FOUND;
928: }
929:
1.11 www 930: unless ($ENV{'form.phase'} eq 'two') {
931:
1.2 www 932: # --------------------------------- File is there and owned, init lookup tables
933:
1.3 www 934: %addid=();
935:
936: {
937: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
938: while (<$fh>=~/(\w+)\s+(\w+)/) {
939: $addid{$1}=$2;
940: }
1.5 www 941: }
942:
943: %nokey=();
944:
945: {
946: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
947: map {
948: my $word=$_;
949: chomp($word);
950: $nokey{$word}=1;
1.9 www 951: } <$fh>;
952: }
953:
954: %language=();
955:
956: {
957: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/language.tab');
958: map {
1.10 www 959: $_=~/(\w+)\s+([\w\s\-]+)/;
1.9 www 960: $language{$1}=$2;
1.10 www 961: } <$fh>;
962: }
963:
964: %cprtag=();
965:
966: {
967: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/copyright.tab');
968: map {
969: $_=~/(\w+)\s+([\w\s\-]+)/;
970: $cprtag{$1}=$2;
1.5 www 971: } <$fh>;
1.3 www 972: }
1.11 www 973:
974: }
975:
1.2 www 976: # ----------------------------------------------------------- Start page output
977:
1.1 www 978: $r->content_type('text/html');
979: $r->send_http_header;
980:
981: $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
1.15 www 982: $r->print(
983: '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
1.2 www 984: my $thisfn=$fn;
985:
986: # ------------------------------------------------------------- Individual file
987: {
988: $thisfn=~/\.(\w+)$/;
989: my $thistype=$1;
990: my $thisembstyle=&Apache::lonnet::fileembstyle($thistype);
991:
992: my $thistarget=$thisfn;
993:
994: $thistarget=~s/^\/home/$targetdir/;
995: $thistarget=~s/\/public\_html//;
996:
997: my $thisdistarget=$thistarget;
998: $thisdistarget=~s/^$docroot//;
999:
1000: my $thisdisfn=$thisfn;
1.27 www 1001: $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
1.2 www 1002:
1003: $r->print('<h2>Publishing '.
1004: &Apache::lonnet::filedescription($thistype).' <tt>'.
1005: $thisdisfn.'</tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
1.27 www 1006:
1007: if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
1008: $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
1009: '</font></h3>');
1010: }
1.26 www 1011:
1012: if (&Apache::lonnet::fileembstyle($thistype) eq 'ssi') {
1.28 www 1013: $r->print('<br><a href="/adm/diff?filename=/~'.$cuname.'/'.
1014: $thisdisfn.
1.26 www 1015: '&versionone=priv" target=cat>Diffs with Current Version</a><p>');
1016: }
1.11 www 1017:
1.2 www 1018: # ------------ We are publishing from $thisfn to $thistarget with $thisembstyle
1019:
1.11 www 1020: unless ($ENV{'form.phase'} eq 'two') {
1.27 www 1021: $r->print(
1022: '<hr>'.&publish($thisfn,$thistarget,$thisembstyle));
1.11 www 1023: } else {
1.27 www 1024: $r->print(
1025: '<hr>'.&phasetwo($thisfn,$thistarget,$thisembstyle,$thisdistarget));
1.11 www 1026: }
1.2 www 1027:
1.11 www 1028: }
1.1 www 1029: $r->print('</body></html>');
1.15 www 1030:
1.1 www 1031: return OK;
1032: }
1033:
1034: 1;
1035: __END__
1036:
1037:
1038:
1039:
1040:
1041:
1042:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>