1: # The LearningOnline Network with CAPA
2: # Publication Handler
3: #
4: # $Id: lonpublisher.pm,v 1.85 2002/07/26 19:35:20 albertel Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: #
29: # (TeX Content Handler
30: #
31: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
32: #
33: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
34: # 03/23 Guy Albertelli
35: # 03/24,03/29,04/03 Gerd Kortemeyer
36: # 04/16/2001 Scott Harrison
37: # 05/03,05/05,05/07 Gerd Kortemeyer
38: # 05/28/2001 Scott Harrison
39: # 06/23,08/07,08/11,8/13,8/17,8/18,8/24,9/26,10/16 Gerd Kortemeyer
40: # 12/04,12/05 Guy Albertelli
41: # 12/05 Gerd Kortemeyer
42: # 12/05 Guy Albertelli
43: # 12/06,12/07 Gerd Kortemeyer
44: # 12/15,12/16 Scott Harrison
45: # 12/25 Gerd Kortemeyer
46: # YEAR=2002
47: # 1/16,1/17 Scott Harrison
48: # 1/17 Gerd Kortemeyer
49: #
50: ###
51:
52: ###############################################################################
53: ## ##
54: ## ORGANIZATION OF THIS PERL MODULE ##
55: ## ##
56: ## 1. Modules used by this module ##
57: ## 2. Various subroutines ##
58: ## 3. Publication Step One ##
59: ## 4. Phase Two ##
60: ## 5. Main Handler ##
61: ## ##
62: ###############################################################################
63:
64: package Apache::lonpublisher;
65:
66: # ------------------------------------------------- modules used by this module
67: use strict;
68: use Apache::File;
69: use File::Copy;
70: use Apache::Constants qw(:common :http :methods);
71: use HTML::LCParser;
72: use Apache::lonxml;
73: use Apache::lonhomework;
74: use Apache::loncacc;
75: use DBI;
76: use Apache::lonnet();
77: use Apache::loncommon();
78:
79: my %addid;
80: my %nokey;
81:
82: my %metadatafields;
83: my %metadatakeys;
84:
85: my $docroot;
86:
87: my $cuname;
88: my $cudom;
89:
90: # ----------------------------------------------- Evaluate string with metadata
91: sub metaeval {
92: my $metastring=shift;
93:
94: my $parser=HTML::LCParser->new(\$metastring);
95: my $token;
96: while ($token=$parser->get_token) {
97: if ($token->[0] eq 'S') {
98: my $entry=$token->[1];
99: my $unikey=$entry;
100: if (defined($token->[2]->{'package'})) {
101: $unikey.='_package_'.$token->[2]->{'package'};
102: }
103: if (defined($token->[2]->{'part'})) {
104: $unikey.='_'.$token->[2]->{'part'};
105: }
106: if (defined($token->[2]->{'id'})) {
107: $unikey.='_'.$token->[2]->{'id'};
108: }
109: if (defined($token->[2]->{'name'})) {
110: $unikey.='_'.$token->[2]->{'name'};
111: }
112: foreach (@{$token->[3]}) {
113: $metadatafields{$unikey.'.'.$_}=$token->[2]->{$_};
114: if ($metadatakeys{$unikey}) {
115: $metadatakeys{$unikey}.=','.$_;
116: } else {
117: $metadatakeys{$unikey}=$_;
118: }
119: }
120: if ($metadatafields{$unikey}) {
121: my $newentry=$parser->get_text('/'.$entry);
122: unless (($metadatafields{$unikey}=~/$newentry/) ||
123: ($newentry eq '')) {
124: $metadatafields{$unikey}.=', '.$newentry;
125: }
126: } else {
127: $metadatafields{$unikey}=$parser->get_text('/'.$entry);
128: }
129: }
130: }
131: }
132:
133: # -------------------------------------------------------- Read a metadata file
134: sub metaread {
135: my ($logfile,$fn)=@_;
136: unless (-e $fn) {
137: print $logfile 'No file '.$fn."\n";
138: return '<br><b>No file:</b> <tt>'.$fn.'</tt>';
139: }
140: print $logfile 'Processing '.$fn."\n";
141: my $metastring;
142: {
143: my $metafh=Apache::File->new($fn);
144: $metastring=join('',<$metafh>);
145: }
146: &metaeval($metastring);
147: return '<br><b>Processed file:</b> <tt>'.$fn.'</tt>';
148: }
149:
150: # ---------------------------- convert 'time' format into a datetime sql format
151: sub sqltime {
152: my $timef=shift @_;
153: my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
154: localtime($timef);
155: $mon++; $year+=1900;
156: return "$year-$mon-$mday $hour:$min:$sec";
157: }
158:
159: # --------------------------------------------------------- Various form fields
160:
161: sub textfield {
162: my ($title,$name,$value)=@_;
163: return "\n<p><b>$title:</b><br>".
164: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
165: }
166:
167: sub hiddenfield {
168: my ($name,$value)=@_;
169: return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
170: }
171:
172: sub selectbox {
173: my ($title,$name,$value,$functionref,@idlist)=@_;
174: my $uctitle=uc($title);
175: my $selout="\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
176: "</b></font><br />".'<select name="'.$name.'">';
177: foreach (@idlist) {
178: $selout.='<option value=\''.$_.'\'';
179: if ($_ eq $value) {
180: $selout.=' selected>'.&{$functionref}($_).'</option>';
181: }
182: else {$selout.='>'.&{$functionref}($_).'</option>';}
183: }
184: return $selout.'</select>';
185: }
186:
187: # -------------------------------------------------------- Publication Step One
188:
189: sub urlfixup {
190: my ($url,$target)=@_;
191: unless ($url) { return ''; }
192: #javascript code needs no fixing
193: if ($url =~ /^javascript:/i) { return $url; }
194: if ($url =~ /^mailto:/i) { return $url; }
195: #internal document links need no fixing
196: if ($url =~ /^\#/) { return $url; }
197: my ($host)=($url=~/(?:http\:\/\/)*([^\/]+)/);
198: foreach (values %Apache::lonnet::hostname) {
199: if ($_ eq $host) {
200: $url=~s/^http\:\/\///;
201: $url=~s/^$host//;
202: }
203: }
204: if ($url=~/^http\:\/\//) { return $url; }
205: $url=~s/\~$cuname/res\/$cudom\/$cuname/;
206: return $url;
207: }
208:
209:
210: sub absoluteurl {
211: my ($url,$target)=@_;
212: unless ($url) { return ''; }
213: if ($target) {
214: $target=~s/\/[^\/]+$//;
215: $url=&Apache::lonnet::hreflocation($target,$url);
216: }
217: return $url;
218: }
219:
220: sub set_allow {
221: my ($allow,$logfile,$target,$tag,$oldurl)=@_;
222: my $newurl=&urlfixup($oldurl,$target);
223: my $return_url=$oldurl;
224: print $logfile 'GUYURL: '.$tag.':'.$oldurl.' - '.$newurl."\n";
225: if ($newurl ne $oldurl) {
226: $return_url=$newurl;
227: print $logfile 'URL: '.$tag.':'.$oldurl.' - '.$newurl."\n";
228: }
229: if (($newurl !~ /^javascript:/i) &&
230: ($newurl !~ /^mailto:/i) &&
231: ($newurl !~ /^http:/i) &&
232: ($newurl !~ /^\#/)) {
233: $$allow{&absoluteurl($newurl,$target)}=1;
234: }
235: return $return_url
236: }
237:
238: sub get_subscribed_hosts {
239: my ($target)=@_;
240: my @subscribed;
241: my $filename;
242: $target=~/(.*)\/([^\/]+)$/;
243: my $srcf=$2;
244: opendir(DIR,$1);
245: while ($filename=readdir(DIR)) {
246: if ($filename=~/$srcf\.(\w+)$/) {
247: my $subhost=$1;
248: if ($subhost ne 'meta' && $subhost ne 'subscription') {
249: push(@subscribed,$subhost);
250: }
251: }
252: }
253: closedir(DIR);
254: my $sh;
255: if ( $sh=Apache::File->new("$target.subscription") ) {
256: &Apache::lonnet::logthis("opened $target.subscription");
257: while (my $subline=<$sh>) {
258: &Apache::lonnet::logthis("Trying $subline");
259: if ($subline =~ /(^\w+):/) { push(@subscribed,$1); } else {
260: &Apache::lonnet::logthis("No Match for $subline");
261: }
262: }
263: } else {
264: &Apache::lonnet::logthis("Un able to open $target.subscription");
265: }
266: &Apache::lonnet::logthis("Got list of ".join(':',@subscribed));
267: return @subscribed;
268: }
269:
270: sub publish {
271:
272: my ($source,$target,$style)=@_;
273: my $logfile;
274: my $scrout='';
275: my $allmeta='';
276: my $content='';
277: my %allow=();
278: undef %allow;
279:
280: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
281: return
282: '<font color=red>No write permission to user directory, FAIL</font>';
283: }
284: print $logfile
285: "\n\n================= Publish ".localtime()." Phase One ================\n";
286:
287: if (($style eq 'ssi') || ($style eq 'rat')) {
288: # ------------------------------------------------------- This needs processing
289:
290: # ----------------------------------------------------------------- Backup Copy
291: my $copyfile=$source.'.save';
292: if (copy($source,$copyfile)) {
293: print $logfile "Copied original file to ".$copyfile."\n";
294: } else {
295: print $logfile "Unable to write backup ".$copyfile.':'.$!."\n";
296: return "<font color=red>Failed to write backup copy, $!,FAIL</font>";
297: }
298: # ------------------------------------------------------------- IDs and indices
299:
300: my $maxindex=10;
301: my $maxid=10;
302:
303: my $needsfixup=0;
304:
305: {
306: my $org=Apache::File->new($source);
307: $content=join('',<$org>);
308: }
309: {
310: my $parser=HTML::LCParser->new(\$content);
311: my $token;
312: while ($token=$parser->get_token) {
313: if ($token->[0] eq 'S') {
314: my $counter;
315: if ($counter=$addid{$token->[1]}) {
316: if ($counter eq 'id') {
317: if (defined($token->[2]->{'id'})) {
318: $maxid=
319: ($token->[2]->{'id'}>$maxid)?$token->[2]->{'id'}:$maxid;
320: } else {
321: $needsfixup=1;
322: }
323: } else {
324: if (defined($token->[2]->{'index'})) {
325: $maxindex=
326: ($token->[2]->{'index'}>$maxindex)?$token->[2]->{'index'}:$maxindex;
327: } else {
328: $needsfixup=1;
329: }
330: }
331: }
332: }
333: }
334: }
335: if ($needsfixup) {
336: print $logfile "Needs ID and/or index fixup\n".
337: "Max ID : $maxid (min 10)\n".
338: "Max Index: $maxindex (min 10)\n";
339: }
340: my $outstring='';
341: my $parser=HTML::LCParser->new(\$content);
342: $parser->xml_mode(1);
343: my $token;
344: while ($token=$parser->get_token) {
345: if ($token->[0] eq 'S') {
346: my $counter;
347: my $tag=$token->[1];
348: my $lctag=lc($tag);
349: unless ($lctag eq 'allow') {
350: my %parms=%{$token->[2]};
351: $counter=$addid{$tag};
352: if (!$counter) { $counter=$addid{$lctag}; }
353: if ($counter) {
354: if ($counter eq 'id') {
355: unless (defined($parms{'id'})) {
356: $maxid++;
357: $parms{'id'}=$maxid;
358: print $logfile 'ID: '.$tag.':'.$maxid."\n";
359: }
360: } elsif ($counter eq 'index') {
361: unless (defined($parms{'index'})) {
362: $maxindex++;
363: $parms{'index'}=$maxindex;
364: print $logfile 'Index: '.$tag.':'.$maxindex."\n";
365: }
366: }
367: }
368:
369: foreach my $type ('src','href','background','bgimg') {
370: foreach my $key (keys(%parms)) {
371: print $logfile "for $type, and $key\n";
372: if ($key =~ /^$type$/i) {
373: print $logfile "calling set_allow\n";
374: $parms{$key}=&set_allow(\%allow,$logfile,
375: $target,$tag,
376: $parms{$key});
377: }
378: }
379: }
380: # probably a <randomlabel> image type <label>
381: if ($lctag eq 'label' && defined($parms{'description'})) {
382: my $next_token=$parser->get_token();
383: if ($next_token->[0] eq 'T') {
384: $next_token->[1]=&set_allow(\%allow,$logfile,
385: $target,$tag,
386: $next_token->[1]);
387: }
388: $parser->unget_token($next_token);
389: }
390: if ($lctag eq 'applet') {
391: my $codebase='';
392: if (defined($parms{'codebase'})) {
393: my $oldcodebase=$parms{'codebase'};
394: unless ($oldcodebase=~/\/$/) {
395: $oldcodebase.='/';
396: }
397: $codebase=&urlfixup($oldcodebase,$target);
398: $codebase=~s/\/$//;
399: if ($codebase ne $oldcodebase) {
400: $parms{'codebase'}=$codebase;
401: print $logfile 'URL codebase: '.$tag.':'.
402: $oldcodebase.' - '.
403: $codebase."\n";
404: }
405: $allow{&absoluteurl($codebase,$target).'/*'}=1;
406: } else {
407: foreach ('archive','code','object') {
408: if (defined($parms{$_})) {
409: my $oldurl=$parms{$_};
410: my $newurl=&urlfixup($oldurl,$target);
411: $newurl=~s/\/[^\/]+$/\/\*/;
412: print $logfile 'Allow: applet '.$_.':'.
413: $oldurl.' allows '.
414: $newurl."\n";
415: $allow{&absoluteurl($newurl,$target)}=1;
416: }
417: }
418: }
419: }
420:
421: my $newparmstring='';
422: my $endtag='';
423: foreach (keys %parms) {
424: if ($_ eq '/') {
425: $endtag=' /';
426: } else {
427: my $quote=($parms{$_}=~/\"/?"'":'"');
428: $newparmstring.=' '.$_.'='.$quote.$parms{$_}.$quote;
429: }
430: }
431: if (!$endtag) { if ($token->[4]=~m:/>$:) { $endtag=' /'; }; }
432: $outstring.='<'.$tag.$newparmstring.$endtag.'>';
433: } else {
434: $allow{$token->[2]->{'src'}}=1;
435: }
436: } elsif ($token->[0] eq 'E') {
437: if ($token->[2]) {
438: unless ($token->[1] eq 'allow') {
439: $outstring.='</'.$token->[1].'>';
440: }
441: }
442: } else {
443: $outstring.=$token->[1];
444: }
445: }
446: # ------------------------------------------------------------ Construct Allows
447:
448: $scrout.='<h3>Dependencies</h3>';
449: my $allowstr='';
450: foreach (sort(keys(%allow))) {
451: my $thisdep=$_;
452: if ($thisdep !~ /[^\s]/) { next; }
453: unless ($style eq 'rat') {
454: $allowstr.="\n".'<allow src="'.$thisdep.'" />';
455: }
456: $scrout.='<br>';
457: unless ($thisdep=~/\*/) {
458: $scrout.='<a href="'.$thisdep.'">';
459: }
460: $scrout.='<tt>'.$thisdep.'</tt>';
461: unless ($thisdep=~/\*/) {
462: $scrout.='</a>';
463: if (
464: &Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
465: $thisdep.'.meta') eq '-1') {
466: $scrout.=
467: ' - <font color=red>Currently not available</font>';
468: } else {
469: my %temphash=(&Apache::lonnet::declutter($target).'___'.
470: &Apache::lonnet::declutter($thisdep).'___usage'
471: => time);
472: $thisdep=~/^\/res\/(\w+)\/(\w+)\//;
473: if ((defined($1)) && (defined($2))) {
474: &Apache::lonnet::put('resevaldata',\%temphash,$1,$2);
475: }
476: }
477: }
478: }
479: $outstring=~s/\n*(\<\/[^\>]+\>)\s*$/$allowstr\n$1\n/s;
480:
481: #Encode any High ASCII characters
482: $outstring=&HTML::Entities::encode($outstring,"\200-\377");
483: # ------------------------------------------------------------- Write modified
484:
485: {
486: my $org;
487: unless ($org=Apache::File->new('>'.$source)) {
488: print $logfile "No write permit to $source\n";
489: return
490: "<font color=red>No write permission to $source, FAIL</font>";
491: }
492: print $org $outstring;
493: }
494: $content=$outstring;
495:
496: if ($needsfixup) {
497: print $logfile "End of ID and/or index fixup\n".
498: "Max ID : $maxid (min 10)\n".
499: "Max Index: $maxindex (min 10)\n";
500: } else {
501: print $logfile "Does not need ID and/or index fixup\n";
502: }
503: }
504: # --------------------------------------------- Initial step done, now metadata
505:
506: # ---------------------------------------- Storage for metadata keys and fields
507:
508: %metadatafields=();
509: %metadatakeys=();
510:
511: my %oldparmstores=();
512:
513:
514: $scrout.='<h3>Metadata Information ' .
515: Apache::loncommon::help_open_topic("Metadata_Description")
516: . '</h3>';
517:
518: # ------------------------------------------------ First, check out environment
519: unless (-e $source.'.meta') {
520: $metadatafields{'author'}=$ENV{'environment.firstname'}.' '.
521: $ENV{'environment.middlename'}.' '.
522: $ENV{'environment.lastname'}.' '.
523: $ENV{'environment.generation'};
524: $metadatafields{'author'}=~s/\s+/ /g;
525: $metadatafields{'author'}=~s/\s+$//;
526: $metadatafields{'owner'}=$cuname.'@'.$cudom;
527:
528: # ------------------------------------------------ Check out directory hierachy
529:
530: my $thisdisfn=$source;
531: $thisdisfn=~s/^\/home\/$cuname\///;
532:
533: my @urlparts=split(/\//,$thisdisfn);
534: $#urlparts--;
535:
536: my $currentpath='/home/'.$cuname.'/';
537:
538: foreach (@urlparts) {
539: $currentpath.=$_.'/';
540: $scrout.=&metaread($logfile,$currentpath.'default.meta');
541: }
542:
543: # ------------------- Clear out parameters and stores (there should not be any)
544:
545: foreach (keys %metadatafields) {
546: if (($_=~/^parameter/) || ($_=~/^stores/)) {
547: delete $metadatafields{$_};
548: }
549: }
550:
551: } else {
552: # ---------------------- Read previous metafile, remember parameters and stores
553:
554: $scrout.=&metaread($logfile,$source.'.meta');
555:
556: foreach (keys %metadatafields) {
557: if (($_=~/^parameter/) || ($_=~/^stores/)) {
558: $oldparmstores{$_}=1;
559: delete $metadatafields{$_};
560: }
561: }
562:
563: }
564:
565: # -------------------------------------------------- Parse content for metadata
566: if ($style eq 'ssi') {
567: my $oldenv=$ENV{'request.uri'};
568:
569: $ENV{'request.uri'}=$target;
570: $allmeta=Apache::lonxml::xmlparse(undef,'meta',$content);
571: $ENV{'request.uri'}=$oldenv;
572:
573: &metaeval($allmeta);
574: }
575: # ---------------- Find and document discrepancies in the parameters and stores
576:
577: my $chparms='';
578: foreach (sort keys %metadatafields) {
579: if (($_=~/^parameter/) || ($_=~/^stores/)) {
580: unless ($_=~/\.\w+$/) {
581: unless ($oldparmstores{$_}) {
582: print $logfile 'New: '.$_."\n";
583: $chparms.=$_.' ';
584: }
585: }
586: }
587: }
588: if ($chparms) {
589: $scrout.='<p><b>New parameters or stored values:</b> '.
590: $chparms;
591: }
592:
593: $chparms='';
594: foreach (sort keys %oldparmstores) {
595: if (($_=~/^parameter/) || ($_=~/^stores/)) {
596: unless (($metadatafields{$_.'.name'}) ||
597: ($metadatafields{$_.'.package'}) || ($_=~/\.\w+$/)) {
598: print $logfile 'Obsolete: '.$_."\n";
599: $chparms.=$_.' ';
600: }
601: }
602: }
603: if ($chparms) {
604: $scrout.='<p><b>Obsolete parameters or stored values:</b> '.
605: $chparms;
606: }
607:
608: # ------------------------------------------------------- Now have all metadata
609:
610: $scrout.=
611: '<form name="pubform" action="/adm/publish" method="post">'.
612: '<p><input type="submit" value="Finalize Publication" /></p>'.
613: &hiddenfield('phase','two').
614: &hiddenfield('filename',$ENV{'form.filename'}).
615: &hiddenfield('allmeta',&Apache::lonnet::escape($allmeta)).
616: &hiddenfield('dependencies',join(',',keys %allow)).
617: &textfield('Title','title',$metadatafields{'title'}).
618: &textfield('Author(s)','author',$metadatafields{'author'}).
619: &textfield('Subject','subject',$metadatafields{'subject'});
620:
621: # --------------------------------------------------- Scan content for keywords
622:
623: my $keywords_help = Apache::loncommon::help_open_topic("Publishing_Keywords");
624: my $keywordout=<<"END";
625: <script>
626: function checkAll(field)
627: {
628: for (i = 0; i < field.length; i++)
629: field[i].checked = true ;
630: }
631:
632: function uncheckAll(field)
633: {
634: for (i = 0; i < field.length; i++)
635: field[i].checked = false ;
636: }
637: </script>
638: <p><b>Keywords: $keywords_help</b>
639: <input type="button" value="check all" onclick="javascript:checkAll(document.pubform.keywords)">
640: <input type="button" value="uncheck all" onclick="javascript:uncheckAll(document.pubform.keywords)">
641: <br />
642: END
643: $keywordout.='<table border=2><tr>';
644: my $colcount=0;
645: my %keywords=();
646:
647: if (length($content)<500000) {
648: my $textonly=$content;
649: $textonly=~s/\<script[^\<]+\<\/script\>//g;
650: $textonly=~s/\<m\>[^\<]+\<\/m\>//g;
651: $textonly=~s/\<[^\>]*\>//g;
652: $textonly=~tr/A-Z/a-z/;
653: $textonly=~s/[\$\&][a-z]\w*//g;
654: $textonly=~s/[^a-z\s]//g;
655:
656: foreach ($textonly=~m/(\w+)/g) {
657: unless ($nokey{$_}) {
658: $keywords{$_}=1;
659: }
660: }
661: }
662:
663:
664: foreach (split(/\W+/,$metadatafields{'keywords'})) {
665: $keywords{$_}=1;
666: }
667:
668: foreach (sort keys %keywords) {
669: $keywordout.='<td><input type=checkbox name="keywords" value="'.$_.'"';
670: if ($metadatafields{'keywords'}) {
671: if ($metadatafields{'keywords'}=~/$_/) {
672: $keywordout.=' checked';
673: }
674: } elsif (&Apache::loncommon::keyword($_)) {
675: $keywordout.=' checked';
676: }
677: $keywordout.='>'.$_.'</td>';
678: if ($colcount>10) {
679: $keywordout.="</tr><tr>\n";
680: $colcount=0;
681: }
682: $colcount++;
683: }
684:
685: $keywordout.='</tr></table>';
686:
687: $scrout.=$keywordout;
688:
689: $scrout.=&textfield('Additional Keywords','addkey','');
690:
691: $scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
692:
693: $scrout.=
694: '<p><b>Abstract:</b><br><textarea cols=80 rows=5 name=abstract>'.
695: $metadatafields{'abstract'}.'</textarea>';
696:
697: $source=~/\.(\w+)$/;
698:
699: $scrout.=&hiddenfield('mime',$1);
700:
701: $scrout.=&selectbox('Language','language',
702: $metadatafields{'language'},
703: \&Apache::loncommon::languagedescription,
704: (&Apache::loncommon::languageids),
705: );
706:
707: unless ($metadatafields{'creationdate'}) {
708: $metadatafields{'creationdate'}=time;
709: }
710: $scrout.=&hiddenfield('creationdate',$metadatafields{'creationdate'});
711:
712: $scrout.=&hiddenfield('lastrevisiondate',time);
713:
714:
715: $scrout.=&textfield('Publisher/Owner','owner',
716: $metadatafields{'owner'});
717: # --------------------------------------------------- Correct copyright for rat
718:
719: if ($style eq 'rat') {
720: if ($metadatafields{'copyright'} eq 'public') {
721: delete $metadatafields{'copyright'};
722: }
723: $scrout.=&selectbox('Copyright/Distribution','copyright',
724: $metadatafields{'copyright'},
725: \&Apache::loncommon::copyrightdescription,
726: (grep !/^public$/,(&Apache::loncommon::copyrightids)));
727: }
728: else {
729: $scrout.=&selectbox('Copyright/Distribution','copyright',
730: $metadatafields{'copyright'},
731: \&Apache::loncommon::copyrightdescription,
732: (&Apache::loncommon::copyrightids));
733: }
734:
735: my $copyright_help = Apache::loncommon::help_open_topic("Publishing_Copyright");
736: $scrout =~ s/DISTRIBUTION:/'DISTRIBUTION: ' . $copyright_help/ge;
737: return $scrout.
738: '<p><input type="submit" value="Finalize Publication" /></p></form>';
739: }
740:
741: # -------------------------------------------------------- Publication Step Two
742:
743: sub phasetwo {
744:
745: my ($source,$target,$style,$distarget)=@_;
746: my $logfile;
747: my $scrout='';
748: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
749: return
750: '<font color=red>No write permission to user directory, FAIL</font>';
751: }
752: print $logfile
753: "\n================= Publish ".localtime()." Phase Two ================\n";
754:
755: %metadatafields=();
756: %metadatakeys=();
757:
758: &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
759:
760: $metadatafields{'title'}=$ENV{'form.title'};
761: $metadatafields{'author'}=$ENV{'form.author'};
762: $metadatafields{'subject'}=$ENV{'form.subject'};
763: $metadatafields{'notes'}=$ENV{'form.notes'};
764: $metadatafields{'abstract'}=$ENV{'form.abstract'};
765: $metadatafields{'mime'}=$ENV{'form.mime'};
766: $metadatafields{'language'}=$ENV{'form.language'};
767: $metadatafields{'creationdate'}=$ENV{'form.creationdate'};
768: $metadatafields{'lastrevisiondate'}=$ENV{'form.lastrevisiondate'};
769: $metadatafields{'owner'}=$ENV{'form.owner'};
770: $metadatafields{'copyright'}=$ENV{'form.copyright'};
771: $metadatafields{'dependencies'}=$ENV{'form.dependencies'};
772:
773: my $allkeywords=$ENV{'form.addkey'};
774: if (exists($ENV{'form.keywords'}) && (ref($ENV{'form.keywords'}))) {
775: my @Keywords = @{$ENV{'form.keywords'}};
776: foreach (@Keywords) {
777: $allkeywords.=','.$_;
778: }
779: }
780: $allkeywords=~s/\W+/\,/;
781: $allkeywords=~s/^\,//;
782: $metadatafields{'keywords'}=$allkeywords;
783:
784: {
785: print $logfile "\nWrite metadata file for ".$source;
786: my $mfh;
787: unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
788: return
789: '<font color=red>Could not write metadata, FAIL</font>';
790: }
791: foreach (sort keys %metadatafields) {
792: unless ($_=~/\./) {
793: my $unikey=$_;
794: $unikey=~/^([A-Za-z]+)/;
795: my $tag=$1;
796: $tag=~tr/A-Z/a-z/;
797: print $mfh "\n\<$tag";
798: foreach (split(/\,/,$metadatakeys{$unikey})) {
799: my $value=$metadatafields{$unikey.'.'.$_};
800: $value=~s/\"/\'\'/g;
801: print $mfh ' '.$_.'="'.$value.'"';
802: }
803: print $mfh '>'.
804: &HTML::Entities::encode($metadatafields{$unikey})
805: .'</'.$tag.'>';
806: }
807: }
808: $scrout.='<p>Wrote Metadata';
809: print $logfile "\nWrote metadata";
810: }
811:
812: # -------------------------------- Synchronize entry with SQL metadata database
813: my $warning;
814:
815: unless ($metadatafields{'copyright'} eq 'priv') {
816:
817: my $dbh;
818: {
819: unless (
820: $dbh = DBI->connect("DBI:mysql:loncapa","www",
821: $Apache::lonnet::perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
822: ) {
823: $warning='<font color=red>WARNING: Cannot connect to '.
824: 'database!</font>';
825: }
826: else {
827: my %sqldatafields;
828: $sqldatafields{'url'}=$distarget;
829: my $sth=$dbh->prepare(
830: 'delete from metadata where url like binary'.
831: '"'.$sqldatafields{'url'}.'"');
832: $sth->execute();
833: foreach ('title','author','subject','keywords','notes','abstract',
834: 'mime','language','creationdate','lastrevisiondate','owner',
835: 'copyright') {
836: my $field=$metadatafields{$_}; $field=~s/\"/\'\'/g;
837: $sqldatafields{$_}=$field;
838: }
839:
840: $sth=$dbh->prepare('insert into metadata values ('.
841: '"'.delete($sqldatafields{'title'}).'"'.','.
842: '"'.delete($sqldatafields{'author'}).'"'.','.
843: '"'.delete($sqldatafields{'subject'}).'"'.','.
844: '"'.delete($sqldatafields{'url'}).'"'.','.
845: '"'.delete($sqldatafields{'keywords'}).'"'.','.
846: '"'.'current'.'"'.','.
847: '"'.delete($sqldatafields{'notes'}).'"'.','.
848: '"'.delete($sqldatafields{'abstract'}).'"'.','.
849: '"'.delete($sqldatafields{'mime'}).'"'.','.
850: '"'.delete($sqldatafields{'language'}).'"'.','.
851: '"'.
852: sqltime(delete($sqldatafields{'creationdate'}))
853: .'"'.','.
854: '"'.
855: sqltime(delete(
856: $sqldatafields{'lastrevisiondate'})).'"'.','.
857: '"'.delete($sqldatafields{'owner'}).'"'.','.
858: '"'.delete(
859: $sqldatafields{'copyright'}).'"'.')');
860: $sth->execute();
861: $dbh->disconnect;
862: $scrout.='<p>Synchronized SQL metadata database';
863: print $logfile "\nSynchronized SQL metadata database";
864: }
865: }
866:
867: } else {
868: $scrout.='<p>Private Publication - did not synchronize database';
869: print $logfile "\nPrivate: Did not synchronize data into ".
870: "SQL metadata database";
871: }
872: # ----------------------------------------------------------- Copy old versions
873:
874: if (-e $target) {
875: my $filename;
876: my $maxversion=0;
877: $target=~/(.*)\/([^\/]+)\.(\w+)$/;
878: my $srcf=$2;
879: my $srct=$3;
880: my $srcd=$1;
881: unless ($srcd=~/^\/home\/httpd\/html\/res/) {
882: print $logfile "\nPANIC: Target dir is ".$srcd;
883: return "<font color=red>Invalid target directory, FAIL</font>";
884: }
885: opendir(DIR,$srcd);
886: while ($filename=readdir(DIR)) {
887: if ($filename=~/$srcf\.(\d+)\.$srct$/) {
888: $maxversion=($1>$maxversion)?$1:$maxversion;
889: }
890: }
891: closedir(DIR);
892: $maxversion++;
893: $scrout.='<p>Creating old version '.$maxversion;
894: print $logfile "\nCreating old version ".$maxversion;
895:
896: my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
897:
898: if (copy($target,$copyfile)) {
899: print $logfile "Copied old target to ".$copyfile."\n";
900: $scrout.='<p>Copied old target file';
901: } else {
902: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
903: return "<font color=red>Failed to copy old target, $!, FAIL</font>";
904: }
905:
906: # --------------------------------------------------------------- Copy Metadata
907:
908: $copyfile=$copyfile.'.meta';
909:
910: if (copy($target.'.meta',$copyfile)) {
911: print $logfile "Copied old target metadata to ".$copyfile."\n";
912: $scrout.='<p>Copied old metadata';
913: } else {
914: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
915: if (-e $target.'.meta') {
916: return
917: "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
918: }
919: }
920:
921:
922: } else {
923: $scrout.='<p>Initial version';
924: print $logfile "\nInitial version";
925: }
926:
927: # ---------------------------------------------------------------- Write Source
928: my $copyfile=$target;
929:
930: my @parts=split(/\//,$copyfile);
931: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
932:
933: my $count;
934: for ($count=5;$count<$#parts;$count++) {
935: $path.="/$parts[$count]";
936: if ((-e $path)!=1) {
937: print $logfile "\nCreating directory ".$path;
938: $scrout.='<p>Created directory '.$parts[$count];
939: mkdir($path,0777);
940: }
941: }
942:
943: if (copy($source,$copyfile)) {
944: print $logfile "Copied original source to ".$copyfile."\n";
945: $scrout.='<p>Copied source file';
946: } else {
947: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
948: return "<font color=red>Failed to copy source, $!, FAIL</font>";
949: }
950:
951: # --------------------------------------------------------------- Copy Metadata
952:
953: $copyfile=$copyfile.'.meta';
954:
955: if (copy($source.'.meta',$copyfile)) {
956: print $logfile "Copied original metadata to ".$copyfile."\n";
957: $scrout.='<p>Copied metadata';
958: } else {
959: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
960: return
961: "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
962: }
963:
964: # --------------------------------------------------- Send update notifications
965:
966: my @subscribed=&get_subscribed_hosts($target);
967: foreach my $subhost (@subscribed) {
968: $scrout.='<p>Notifying host '.$subhost.':';
969: print $logfile "\nNotifying host ".$subhost.':';
970: my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
971: $scrout.=$reply;
972: print $logfile $reply;
973: }
974:
975: # ---------------------------------------- Send update notifications, meta only
976:
977: my @subscribedmeta=&get_subscribed_hosts("$target.meta");
978: foreach my $subhost (@subscribedmeta) {
979: $scrout.='<p>Notifying host for metadata only '.$subhost.':';
980: print $logfile "\nNotifying host for metadata only ".$subhost.':';
981: my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
982: $subhost);
983: $scrout.=$reply;
984: print $logfile $reply;
985: }
986:
987: # ------------------------------------------------ Provide link to new resource
988:
989: my $thisdistarget=$target;
990: $thisdistarget=~s/^$docroot//;
991:
992: my $thissrc=$source;
993: $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
994:
995: my $thissrcdir=$thissrc;
996: $thissrcdir=~s/\/[^\/]+$/\//;
997:
998:
999: return $warning.$scrout.
1000: '<hr><a href="'.$thisdistarget.'"><font size=+2>View Published Version</font></a>'.
1001: '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
1002: '<p><a href="'.$thissrcdir.
1003: '"><font size=+2>Back to Source Directory</font></a>';
1004:
1005: }
1006:
1007: # ================================================================ Main Handler
1008:
1009: sub handler {
1010: my $r=shift;
1011:
1012: if ($r->header_only) {
1013: $r->content_type('text/html');
1014: $r->send_http_header;
1015: return OK;
1016: }
1017:
1018: # Get query string for limited number of parameters
1019:
1020: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1021: ['filename']);
1022:
1023: # -------------------------------------------------------------- Check filename
1024:
1025: my $fn=$ENV{'form.filename'};
1026:
1027:
1028: unless ($fn) {
1029: $r->log_reason($cuname.' at '.$cudom.
1030: ' trying to publish empty filename', $r->filename);
1031: return HTTP_NOT_FOUND;
1032: }
1033:
1034: ($cuname,$cudom)=
1035: &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
1036: unless (($cuname) && ($cudom)) {
1037: $r->log_reason($cuname.' at '.$cudom.
1038: ' trying to publish file '.$ENV{'form.filename'}.
1039: ' ('.$fn.') - not authorized',
1040: $r->filename);
1041: return HTTP_NOT_ACCEPTABLE;
1042: }
1043:
1044: unless (&Apache::lonnet::homeserver($cuname,$cudom)
1045: eq $r->dir_config('lonHostID')) {
1046: $r->log_reason($cuname.' at '.$cudom.
1047: ' trying to publish file '.$ENV{'form.filename'}.
1048: ' ('.$fn.') - not homeserver ('.
1049: &Apache::lonnet::homeserver($cuname,$cudom).')',
1050: $r->filename);
1051: return HTTP_NOT_ACCEPTABLE;
1052: }
1053:
1054: $fn=~s/^http\:\/\/[^\/]+//;
1055: $fn=~s/^\/\~(\w+)/\/home\/$1\/public_html/;
1056:
1057: my $targetdir='';
1058: $docroot=$r->dir_config('lonDocRoot');
1059: if ($1 ne $cuname) {
1060: $r->log_reason($cuname.' at '.$cudom.
1061: ' trying to publish unowned file '.$ENV{'form.filename'}.
1062: ' ('.$fn.')',
1063: $r->filename);
1064: return HTTP_NOT_ACCEPTABLE;
1065: } else {
1066: $targetdir=$docroot.'/res/'.$cudom;
1067: }
1068:
1069:
1070: unless (-e $fn) {
1071: $r->log_reason($cuname.' at '.$cudom.
1072: ' trying to publish non-existing file '.$ENV{'form.filename'}.
1073: ' ('.$fn.')',
1074: $r->filename);
1075: return HTTP_NOT_FOUND;
1076: }
1077:
1078: unless ($ENV{'form.phase'} eq 'two') {
1079:
1080: # --------------------------------- File is there and owned, init lookup tables
1081:
1082: %addid=();
1083:
1084: {
1085: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
1086: while (<$fh>=~/(\w+)\s+(\w+)/) {
1087: $addid{$1}=$2;
1088: }
1089: }
1090:
1091: %nokey=();
1092:
1093: {
1094: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
1095: while (<$fh>) {
1096: my $word=$_;
1097: chomp($word);
1098: $nokey{$word}=1;
1099: }
1100: }
1101:
1102: }
1103:
1104: # ----------------------------------------------------------- Start page output
1105:
1106: $r->content_type('text/html');
1107: $r->send_http_header;
1108:
1109: $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
1110: $r->print(
1111: '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
1112: my $thisfn=$fn;
1113:
1114: # ------------------------------------------------------------- Individual file
1115: {
1116: $thisfn=~/\.(\w+)$/;
1117: my $thistype=$1;
1118: my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
1119:
1120: my $thistarget=$thisfn;
1121:
1122: $thistarget=~s/^\/home/$targetdir/;
1123: $thistarget=~s/\/public\_html//;
1124:
1125: my $thisdistarget=$thistarget;
1126: $thisdistarget=~s/^$docroot//;
1127:
1128: my $thisdisfn=$thisfn;
1129: $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
1130:
1131: $r->print('<h2>Publishing '.
1132: &Apache::loncommon::filedescription($thistype).' <tt>'.
1133: $thisdisfn.'</tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
1134:
1135: if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
1136: $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
1137: '</font></h3>');
1138: }
1139:
1140: if (&Apache::loncommon::fileembstyle($thistype) eq 'ssi') {
1141: $r->print('<br><a href="/adm/diff?filename=/~'.$cuname.'/'.
1142: $thisdisfn.
1143: '&versionone=priv" target=cat>Diffs with Current Version</a><p>');
1144: }
1145:
1146: # ------------ We are publishing from $thisfn to $thistarget with $thisembstyle
1147:
1148: unless ($ENV{'form.phase'} eq 'two') {
1149: $r->print(
1150: '<hr>'.&publish($thisfn,$thistarget,$thisembstyle));
1151: } else {
1152: $r->print(
1153: '<hr>'.&phasetwo($thisfn,$thistarget,$thisembstyle,$thisdistarget));
1154: }
1155:
1156: }
1157: $r->print('</body></html>');
1158:
1159: return OK;
1160: }
1161:
1162: 1;
1163: __END__
1164:
1165: =head1 NAME
1166:
1167: Apache::lonpublisher - Publication Handler
1168:
1169: =head1 SYNOPSIS
1170:
1171: Invoked by /etc/httpd/conf/srm.conf:
1172:
1173: <Location /adm/publish>
1174: PerlAccessHandler Apache::lonacc
1175: SetHandler perl-script
1176: PerlHandler Apache::lonpublisher
1177: ErrorDocument 403 /adm/login
1178: ErrorDocument 404 /adm/notfound.html
1179: ErrorDocument 406 /adm/unauthorized.html
1180: ErrorDocument 500 /adm/errorhandler
1181: </Location>
1182:
1183: =head1 INTRODUCTION
1184:
1185: This module publishes a file. This involves gathering metadata,
1186: versioning the file, copying file from construction space to
1187: publication space, and copying metadata from construction space
1188: to publication space.
1189:
1190: This is part of the LearningOnline Network with CAPA project
1191: described at http://www.lon-capa.org.
1192:
1193: =head1 HANDLER SUBROUTINE
1194:
1195: This routine is called by Apache and mod_perl.
1196:
1197: =over 4
1198:
1199: =item *
1200:
1201: Get query string for limited number of parameters
1202:
1203: =item *
1204:
1205: Check filename
1206:
1207: =item *
1208:
1209: File is there and owned, init lookup tables
1210:
1211: =item *
1212:
1213: Start page output
1214:
1215: =item *
1216:
1217: Individual file
1218:
1219: =item *
1220:
1221: publish from $thisfn to $thistarget with $thisembstyle
1222:
1223: =back
1224:
1225: =head1 OTHER SUBROUTINES
1226:
1227: =over 4
1228:
1229: =item *
1230:
1231: metaeval() : Evaluate string with metadata
1232:
1233: =item *
1234:
1235: metaread() : Read a metadata file
1236:
1237: =item *
1238:
1239: sqltime() : convert 'time' format into a datetime sql format
1240:
1241: =item *
1242:
1243: textfield() : form field
1244:
1245: =item *
1246:
1247: hiddenfield() : form field
1248:
1249: =item *
1250:
1251: selectbox() : form field
1252:
1253: =item *
1254:
1255: urlfixup() : fixup URL (Publication Step One)
1256:
1257: =item *
1258:
1259: publish() : publish (Publication Step One)
1260:
1261: =item *
1262:
1263: phasetwo() : render second interface showing status of publication steps
1264: (Publication Step Two)
1265:
1266: =back
1267:
1268: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>