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