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