1: # The LearningOnline Network with CAPA
2: # Publication Handler
3: #
4: # $Id: lonpublisher.pm,v 1.74 2002/03/06 22:58:45 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::TokeParser;
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::TokeParser->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 publish {
221:
222: my ($source,$target,$style)=@_;
223: my $logfile;
224: my $scrout='';
225: my $allmeta='';
226: my $content='';
227: my %allow=();
228: undef %allow;
229:
230: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
231: return
232: '<font color=red>No write permission to user directory, FAIL</font>';
233: }
234: print $logfile
235: "\n\n================= Publish ".localtime()." Phase One ================\n";
236:
237: if (($style eq 'ssi') || ($style eq 'rat')) {
238: # ------------------------------------------------------- This needs processing
239:
240: # ----------------------------------------------------------------- Backup Copy
241: my $copyfile=$source.'.save';
242: if (copy($source,$copyfile)) {
243: print $logfile "Copied original file to ".$copyfile."\n";
244: } else {
245: print $logfile "Unable to write backup ".$copyfile.':'.$!."\n";
246: return "<font color=red>Failed to write backup copy, $!,FAIL</font>";
247: }
248: # ------------------------------------------------------------- IDs and indices
249:
250: my $maxindex=10;
251: my $maxid=10;
252:
253: my $needsfixup=0;
254:
255: {
256: my $org=Apache::File->new($source);
257: $content=join('',<$org>);
258: }
259: {
260: my $parser=HTML::TokeParser->new(\$content);
261: my $token;
262: while ($token=$parser->get_token) {
263: if ($token->[0] eq 'S') {
264: my $counter;
265: if ($counter=$addid{$token->[1]}) {
266: if ($counter eq 'id') {
267: if (defined($token->[2]->{'id'})) {
268: $maxid=
269: ($token->[2]->{'id'}>$maxid)?$token->[2]->{'id'}:$maxid;
270: } else {
271: $needsfixup=1;
272: }
273: } else {
274: if (defined($token->[2]->{'index'})) {
275: $maxindex=
276: ($token->[2]->{'index'}>$maxindex)?$token->[2]->{'index'}:$maxindex;
277: } else {
278: $needsfixup=1;
279: }
280: }
281: }
282: }
283: }
284: }
285: if ($needsfixup) {
286: print $logfile "Needs ID and/or index fixup\n".
287: "Max ID : $maxid (min 10)\n".
288: "Max Index: $maxindex (min 10)\n";
289: }
290: my $outstring='';
291: my $parser=HTML::TokeParser->new(\$content);
292: $parser->xml_mode(1);
293: my $token;
294: while ($token=$parser->get_token) {
295: if ($token->[0] eq 'S') {
296: my $counter;
297: my $tag=$token->[1];
298: my $lctag=lc($tag);
299: unless ($lctag eq 'allow') {
300: my %parms=%{$token->[2]};
301: $counter=$addid{$tag};
302: if (!$counter) { $counter=$addid{$lctag}; }
303: if ($counter) {
304: if ($counter eq 'id') {
305: unless (defined($parms{'id'})) {
306: $maxid++;
307: $parms{'id'}=$maxid;
308: print $logfile 'ID: '.$tag.':'.$maxid."\n";
309: }
310: } elsif ($counter eq 'index') {
311: unless (defined($parms{'index'})) {
312: $maxindex++;
313: $parms{'index'}=$maxindex;
314: print $logfile 'Index: '.$tag.':'.$maxindex."\n";
315: }
316: }
317: }
318:
319: foreach my $type ('src','href','background','bgimg') {
320: foreach my $key (keys(%parms)) {
321: if ($key =~ /^$type$/i) {
322: my $oldurl=$parms{$key};
323: my $newurl=&urlfixup($oldurl,$target);
324: if ($newurl ne $oldurl) {
325: $parms{$key}=$newurl;
326: print $logfile 'URL: '.$tag.':'.$oldurl.' - '.
327: $newurl."\n";
328: }
329: if (($newurl !~ /^javascript:/i) &&
330: ($newurl !~ /^mailto:/i) &&
331: ($newurl !~ /^http:/i) &&
332: ($newurl !~ /^\#/)) {
333: $allow{&absoluteurl($newurl,$target)}=1;
334: }
335: }
336: last;
337: }
338: }
339:
340: if ($lctag eq 'applet') {
341: my $codebase='';
342: if (defined($parms{'codebase'})) {
343: my $oldcodebase=$parms{'codebase'};
344: unless ($oldcodebase=~/\/$/) {
345: $oldcodebase.='/';
346: }
347: $codebase=&urlfixup($oldcodebase,$target);
348: $codebase=~s/\/$//;
349: if ($codebase ne $oldcodebase) {
350: $parms{'codebase'}=$codebase;
351: print $logfile 'URL codebase: '.$tag.':'.
352: $oldcodebase.' - '.
353: $codebase."\n";
354: }
355: $allow{&absoluteurl($codebase,$target).'/*'}=1;
356: } else {
357: foreach ('archive','code','object') {
358: if (defined($parms{$_})) {
359: my $oldurl=$parms{$_};
360: my $newurl=&urlfixup($oldurl,$target);
361: $newurl=~s/\/[^\/]+$/\/\*/;
362: print $logfile 'Allow: applet '.$_.':'.
363: $oldurl.' allows '.
364: $newurl."\n";
365: $allow{&absoluteurl($newurl,$target)}=1;
366: }
367: }
368: }
369: }
370:
371: my $newparmstring='';
372: my $endtag='';
373: foreach (keys %parms) {
374: if ($_ eq '/') {
375: $endtag=' /';
376: } else {
377: my $quote=($parms{$_}=~/\"/?"'":'"');
378: $newparmstring.=' '.$_.'='.$quote.$parms{$_}.$quote;
379: }
380: }
381: if (!$endtag) { if ($token->[4]=~m:/>$:) { $endtag=' /'; }; }
382: $outstring.='<'.$tag.$newparmstring.$endtag.'>';
383: } else {
384: $allow{$token->[2]->{'src'}}=1;
385: }
386: } elsif ($token->[0] eq 'E') {
387: if ($token->[2]) {
388: unless ($token->[1] eq 'allow') {
389: $outstring.='</'.$token->[1].'>';
390: }
391: }
392: } else {
393: $outstring.=$token->[1];
394: }
395: }
396: # ------------------------------------------------------------ Construct Allows
397:
398: $scrout.='<h3>Dependencies</h3>';
399: my $allowstr='';
400: foreach (sort(keys(%allow))) {
401: my $thisdep=$_;
402: if ($thisdep !~ /[^\s]/) { next; }
403: unless ($style eq 'rat') {
404: $allowstr.="\n".'<allow src="'.$thisdep.'" />';
405: }
406: $scrout.='<br>';
407: unless ($thisdep=~/\*/) {
408: $scrout.='<a href="'.$thisdep.'">';
409: }
410: $scrout.='<tt>'.$thisdep.'</tt>';
411: unless ($thisdep=~/\*/) {
412: $scrout.='</a>';
413: if (
414: &Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
415: $thisdep.'.meta') eq '-1') {
416: $scrout.=
417: ' - <font color=red>Currently not available</font>';
418: } else {
419: my %temphash=(&Apache::lonnet::declutter($target).'___'.
420: &Apache::lonnet::declutter($thisdep).'___usage'
421: => time);
422: $thisdep=~/^\/res\/(\w+)\/(\w+)\//;
423: if ((defined($1)) && (defined($2))) {
424: &Apache::lonnet::put('resevaldata',\%temphash,$1,$2);
425: }
426: }
427: }
428: }
429: $allowstr=~s/\n+/\n/g;
430: $outstring=~s/(\<\/[^\>]+\>\s*)$/$allowstr$1/s;
431:
432: # ------------------------------------------------------------- Write modified
433:
434: {
435: my $org;
436: unless ($org=Apache::File->new('>'.$source)) {
437: print $logfile "No write permit to $source\n";
438: return
439: "<font color=red>No write permission to $source, FAIL</font>";
440: }
441: print $org $outstring;
442: }
443: $content=$outstring;
444:
445: if ($needsfixup) {
446: print $logfile "End of ID and/or index fixup\n".
447: "Max ID : $maxid (min 10)\n".
448: "Max Index: $maxindex (min 10)\n";
449: } else {
450: print $logfile "Does not need ID and/or index fixup\n";
451: }
452: }
453: # --------------------------------------------- Initial step done, now metadata
454:
455: # ---------------------------------------- Storage for metadata keys and fields
456:
457: %metadatafields=();
458: %metadatakeys=();
459:
460: my %oldparmstores=();
461:
462: $scrout.='<h3>Metadata Information</h3>';
463:
464: # ------------------------------------------------ First, check out environment
465: unless (-e $source.'.meta') {
466: $metadatafields{'author'}=$ENV{'environment.firstname'}.' '.
467: $ENV{'environment.middlename'}.' '.
468: $ENV{'environment.lastname'}.' '.
469: $ENV{'environment.generation'};
470: $metadatafields{'author'}=~s/\s+/ /g;
471: $metadatafields{'author'}=~s/\s+$//;
472: $metadatafields{'owner'}=$cuname.'@'.$cudom;
473:
474: # ------------------------------------------------ Check out directory hierachy
475:
476: my $thisdisfn=$source;
477: $thisdisfn=~s/^\/home\/$cuname\///;
478:
479: my @urlparts=split(/\//,$thisdisfn);
480: $#urlparts--;
481:
482: my $currentpath='/home/'.$cuname.'/';
483:
484: foreach (@urlparts) {
485: $currentpath.=$_.'/';
486: $scrout.=&metaread($logfile,$currentpath.'default.meta');
487: }
488:
489: # ------------------- Clear out parameters and stores (there should not be any)
490:
491: foreach (keys %metadatafields) {
492: if (($_=~/^parameter/) || ($_=~/^stores/)) {
493: delete $metadatafields{$_};
494: }
495: }
496:
497: } else {
498: # ---------------------- Read previous metafile, remember parameters and stores
499:
500: $scrout.=&metaread($logfile,$source.'.meta');
501:
502: foreach (keys %metadatafields) {
503: if (($_=~/^parameter/) || ($_=~/^stores/)) {
504: $oldparmstores{$_}=1;
505: delete $metadatafields{$_};
506: }
507: }
508:
509: }
510:
511: # -------------------------------------------------- Parse content for metadata
512: if ($style eq 'ssi') {
513: my $oldenv=$ENV{'request.uri'};
514:
515: $ENV{'request.uri'}=$target;
516: $allmeta=Apache::lonxml::xmlparse('meta',$content);
517: $ENV{'request.uri'}=$oldenv;
518:
519: &metaeval($allmeta);
520: }
521: # ---------------- Find and document discrepancies in the parameters and stores
522:
523: my $chparms='';
524: foreach (sort keys %metadatafields) {
525: if (($_=~/^parameter/) || ($_=~/^stores/)) {
526: unless ($_=~/\.\w+$/) {
527: unless ($oldparmstores{$_}) {
528: print $logfile 'New: '.$_."\n";
529: $chparms.=$_.' ';
530: }
531: }
532: }
533: }
534: if ($chparms) {
535: $scrout.='<p><b>New parameters or stored values:</b> '.
536: $chparms;
537: }
538:
539: $chparms='';
540: foreach (sort keys %oldparmstores) {
541: if (($_=~/^parameter/) || ($_=~/^stores/)) {
542: unless (($metadatafields{$_.'.name'}) ||
543: ($metadatafields{$_.'.package'}) || ($_=~/\.\w+$/)) {
544: print $logfile 'Obsolete: '.$_."\n";
545: $chparms.=$_.' ';
546: }
547: }
548: }
549: if ($chparms) {
550: $scrout.='<p><b>Obsolete parameters or stored values:</b> '.
551: $chparms;
552: }
553:
554: # ------------------------------------------------------- Now have all metadata
555:
556: $scrout.=
557: '<form action="/adm/publish" method="post">'.
558: '<p><input type="submit" value="Finalize Publication" /></p>'.
559: &hiddenfield('phase','two').
560: &hiddenfield('filename',$ENV{'form.filename'}).
561: &hiddenfield('allmeta',&Apache::lonnet::escape($allmeta)).
562: &hiddenfield('dependencies',join(',',keys %allow)).
563: &textfield('Title','title',$metadatafields{'title'}).
564: &textfield('Author(s)','author',$metadatafields{'author'}).
565: &textfield('Subject','subject',$metadatafields{'subject'});
566:
567: # --------------------------------------------------- Scan content for keywords
568:
569: my $keywordout='<p><b>Keywords:</b><br><table border=2><tr>';
570: my $colcount=0;
571: my %keywords=();
572:
573: if (length($content)<500000) {
574: my $textonly=$content;
575: $textonly=~s/\<script[^\<]+\<\/script\>//g;
576: $textonly=~s/\<m\>[^\<]+\<\/m\>//g;
577: $textonly=~s/\<[^\>]*\>//g;
578: $textonly=~tr/A-Z/a-z/;
579: $textonly=~s/[\$\&][a-z]\w*//g;
580: $textonly=~s/[^a-z\s]//g;
581:
582: foreach ($textonly=~m/(\w+)/g) {
583: unless ($nokey{$_}) {
584: $keywords{$_}=1;
585: }
586: }
587: }
588:
589:
590: foreach (split(/\W+/,$metadatafields{'keywords'})) {
591: $keywords{$_}=1;
592: }
593:
594: foreach (sort keys %keywords) {
595: $keywordout.='<td><input type=checkbox name="key.'.$_.'"';
596: if ($metadatafields{'keywords'}) {
597: if ($metadatafields{'keywords'}=~/$_/) {
598: $keywordout.=' checked';
599: }
600: } elsif (&Apache::loncommon::keyword($_)) {
601: $keywordout.=' checked';
602: }
603: $keywordout.='>'.$_.'</td>';
604: if ($colcount>10) {
605: $keywordout.="</tr><tr>\n";
606: $colcount=0;
607: }
608: $colcount++;
609: }
610:
611: $keywordout.='</tr></table>';
612:
613: $scrout.=$keywordout;
614:
615: $scrout.=&textfield('Additional Keywords','addkey','');
616:
617: $scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
618:
619: $scrout.=
620: '<p><b>Abstract:</b><br><textarea cols=80 rows=5 name=abstract>'.
621: $metadatafields{'abstract'}.'</textarea>';
622:
623: $source=~/\.(\w+)$/;
624:
625: $scrout.=&hiddenfield('mime',$1);
626:
627: $scrout.=&selectbox('Language','language',
628: $metadatafields{'language'},
629: \&Apache::loncommon::languagedescription,
630: (&Apache::loncommon::languageids),
631: );
632:
633: unless ($metadatafields{'creationdate'}) {
634: $metadatafields{'creationdate'}=time;
635: }
636: $scrout.=&hiddenfield('creationdate',$metadatafields{'creationdate'});
637:
638: $scrout.=&hiddenfield('lastrevisiondate',time);
639:
640:
641: $scrout.=&textfield('Publisher/Owner','owner',
642: $metadatafields{'owner'});
643: # --------------------------------------------------- Correct copyright for rat
644: if ($style eq 'rat') {
645: if ($metadatafields{'copyright'} eq 'public') {
646: delete $metadatafields{'copyright'};
647: }
648: $scrout.=&selectbox('Copyright/Distribution','copyright',
649: $metadatafields{'copyright'},
650: \&Apache::loncommon::copyrightdescription,
651: (grep !/^public$/,(&Apache::loncommon::copyrightids)));
652: }
653: else {
654: $scrout.=&selectbox('Copyright/Distribution','copyright',
655: $metadatafields{'copyright'},
656: \&Apache::loncommon::copyrightdescription,
657: (&Apache::loncommon::copyrightids));
658: }
659: return $scrout.
660: '<p><input type="submit" value="Finalize Publication" /></p></form>';
661: }
662:
663: # -------------------------------------------------------- Publication Step Two
664:
665: sub phasetwo {
666:
667: my ($source,$target,$style,$distarget)=@_;
668: my $logfile;
669: my $scrout='';
670:
671: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
672: return
673: '<font color=red>No write permission to user directory, FAIL</font>';
674: }
675: print $logfile
676: "\n================= Publish ".localtime()." Phase Two ================\n";
677:
678: %metadatafields=();
679: %metadatakeys=();
680:
681: &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
682:
683: $metadatafields{'title'}=$ENV{'form.title'};
684: $metadatafields{'author'}=$ENV{'form.author'};
685: $metadatafields{'subject'}=$ENV{'form.subject'};
686: $metadatafields{'notes'}=$ENV{'form.notes'};
687: $metadatafields{'abstract'}=$ENV{'form.abstract'};
688: $metadatafields{'mime'}=$ENV{'form.mime'};
689: $metadatafields{'language'}=$ENV{'form.language'};
690: $metadatafields{'creationdate'}=$ENV{'form.creationdate'};
691: $metadatafields{'lastrevisiondate'}=$ENV{'form.lastrevisiondate'};
692: $metadatafields{'owner'}=$ENV{'form.owner'};
693: $metadatafields{'copyright'}=$ENV{'form.copyright'};
694: $metadatafields{'dependencies'}=$ENV{'form.dependencies'};
695:
696: my $allkeywords=$ENV{'form.addkey'};
697: foreach (keys %ENV) {
698: if ($_=~/^form\.key\.(\w+)/) {
699: $allkeywords.=','.$1;
700: }
701: }
702: $allkeywords=~s/\W+/\,/;
703: $allkeywords=~s/^\,//;
704: $metadatafields{'keywords'}=$allkeywords;
705:
706: {
707: print $logfile "\nWrite metadata file for ".$source;
708: my $mfh;
709: unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
710: return
711: '<font color=red>Could not write metadata, FAIL</font>';
712: }
713: foreach (sort keys %metadatafields) {
714: unless ($_=~/\./) {
715: my $unikey=$_;
716: $unikey=~/^([A-Za-z]+)/;
717: my $tag=$1;
718: $tag=~tr/A-Z/a-z/;
719: print $mfh "\n\<$tag";
720: foreach (split(/\,/,$metadatakeys{$unikey})) {
721: my $value=$metadatafields{$unikey.'.'.$_};
722: $value=~s/\"/\'\'/g;
723: print $mfh ' '.$_.'="'.$value.'"';
724: }
725: print $mfh '>'.$metadatafields{$unikey}.'</'.$tag.'>';
726: }
727: }
728: $scrout.='<p>Wrote Metadata';
729: print $logfile "\nWrote metadata";
730: }
731:
732: # -------------------------------- Synchronize entry with SQL metadata database
733: my $warning;
734:
735: unless ($metadatafields{'copyright'} eq 'priv') {
736:
737: my $dbh;
738: {
739: unless (
740: $dbh = DBI->connect("DBI:mysql:loncapa","www",
741: $Apache::lonnet::perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
742: ) {
743: $warning='<font color=red>WARNING: Cannot connect to '.
744: 'database!</font>';
745: }
746: else {
747: my %sqldatafields;
748: $sqldatafields{'url'}=$distarget;
749: my $sth=$dbh->prepare(
750: 'delete from metadata where url like binary'.
751: '"'.$sqldatafields{'url'}.'"');
752: $sth->execute();
753: foreach ('title','author','subject','keywords','notes','abstract',
754: 'mime','language','creationdate','lastrevisiondate','owner',
755: 'copyright') {
756: my $field=$metadatafields{$_}; $field=~s/\"/\'\'/g;
757: $sqldatafields{$_}=$field;
758: }
759:
760: $sth=$dbh->prepare('insert into metadata values ('.
761: '"'.delete($sqldatafields{'title'}).'"'.','.
762: '"'.delete($sqldatafields{'author'}).'"'.','.
763: '"'.delete($sqldatafields{'subject'}).'"'.','.
764: '"'.delete($sqldatafields{'url'}).'"'.','.
765: '"'.delete($sqldatafields{'keywords'}).'"'.','.
766: '"'.'current'.'"'.','.
767: '"'.delete($sqldatafields{'notes'}).'"'.','.
768: '"'.delete($sqldatafields{'abstract'}).'"'.','.
769: '"'.delete($sqldatafields{'mime'}).'"'.','.
770: '"'.delete($sqldatafields{'language'}).'"'.','.
771: '"'.
772: sqltime(delete($sqldatafields{'creationdate'}))
773: .'"'.','.
774: '"'.
775: sqltime(delete(
776: $sqldatafields{'lastrevisiondate'})).'"'.','.
777: '"'.delete($sqldatafields{'owner'}).'"'.','.
778: '"'.delete(
779: $sqldatafields{'copyright'}).'"'.')');
780: $sth->execute();
781: $dbh->disconnect;
782: $scrout.='<p>Synchronized SQL metadata database';
783: print $logfile "\nSynchronized SQL metadata database";
784: }
785: }
786:
787: } else {
788: $scrout.='<p>Private Publication - did not synchronize database';
789: print $logfile "\nPrivate: Did not synchronize data into ".
790: "SQL metadata database";
791: }
792: # ----------------------------------------------------------- Copy old versions
793:
794: if (-e $target) {
795: my $filename;
796: my $maxversion=0;
797: $target=~/(.*)\/([^\/]+)\.(\w+)$/;
798: my $srcf=$2;
799: my $srct=$3;
800: my $srcd=$1;
801: unless ($srcd=~/^\/home\/httpd\/html\/res/) {
802: print $logfile "\nPANIC: Target dir is ".$srcd;
803: return "<font color=red>Invalid target directory, FAIL</font>";
804: }
805: opendir(DIR,$srcd);
806: while ($filename=readdir(DIR)) {
807: if ($filename=~/$srcf\.(\d+)\.$srct$/) {
808: $maxversion=($1>$maxversion)?$1:$maxversion;
809: }
810: }
811: closedir(DIR);
812: $maxversion++;
813: $scrout.='<p>Creating old version '.$maxversion;
814: print $logfile "\nCreating old version ".$maxversion;
815:
816: my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
817:
818: if (copy($target,$copyfile)) {
819: print $logfile "Copied old target to ".$copyfile."\n";
820: $scrout.='<p>Copied old target file';
821: } else {
822: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
823: return "<font color=red>Failed to copy old target, $!, FAIL</font>";
824: }
825:
826: # --------------------------------------------------------------- Copy Metadata
827:
828: $copyfile=$copyfile.'.meta';
829:
830: if (copy($target.'.meta',$copyfile)) {
831: print $logfile "Copied old target metadata to ".$copyfile."\n";
832: $scrout.='<p>Copied old metadata';
833: } else {
834: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
835: if (-e $target.'.meta') {
836: return
837: "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
838: }
839: }
840:
841:
842: } else {
843: $scrout.='<p>Initial version';
844: print $logfile "\nInitial version";
845: }
846:
847: # ---------------------------------------------------------------- Write Source
848: my $copyfile=$target;
849:
850: my @parts=split(/\//,$copyfile);
851: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
852:
853: my $count;
854: for ($count=5;$count<$#parts;$count++) {
855: $path.="/$parts[$count]";
856: if ((-e $path)!=1) {
857: print $logfile "\nCreating directory ".$path;
858: $scrout.='<p>Created directory '.$parts[$count];
859: mkdir($path,0777);
860: }
861: }
862:
863: if (copy($source,$copyfile)) {
864: print $logfile "Copied original source to ".$copyfile."\n";
865: $scrout.='<p>Copied source file';
866: } else {
867: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
868: return "<font color=red>Failed to copy source, $!, FAIL</font>";
869: }
870:
871: # --------------------------------------------------------------- Copy Metadata
872:
873: $copyfile=$copyfile.'.meta';
874:
875: if (copy($source.'.meta',$copyfile)) {
876: print $logfile "Copied original metadata to ".$copyfile."\n";
877: $scrout.='<p>Copied metadata';
878: } else {
879: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
880: return
881: "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
882: }
883:
884: # --------------------------------------------------- Send update notifications
885:
886: {
887:
888: my $filename;
889:
890: $target=~/(.*)\/([^\/]+)$/;
891: my $srcf=$2;
892: opendir(DIR,$1);
893: while ($filename=readdir(DIR)) {
894: if ($filename=~/$srcf\.(\w+)$/) {
895: my $subhost=$1;
896: if ($subhost ne 'meta') {
897: $scrout.='<p>Notifying host '.$subhost.':';
898: print $logfile "\nNotifying host '.$subhost.':'";
899: my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
900: $scrout.=$reply;
901: print $logfile $reply;
902: }
903: }
904: }
905: closedir(DIR);
906:
907: }
908:
909: # ---------------------------------------- Send update notifications, meta only
910:
911: {
912:
913: my $filename;
914:
915: $target=~/(.*)\/([^\/]+)$/;
916: my $srcf=$2.'.meta';
917: opendir(DIR,$1);
918: while ($filename=readdir(DIR)) {
919: if ($filename=~/$srcf\.(\w+)$/) {
920: my $subhost=$1;
921: if ($subhost ne 'meta') {
922: $scrout.=
923: '<p>Notifying host for metadata only '.$subhost.':';
924: print $logfile
925: "\nNotifying host for metadata only '.$subhost.':'";
926: my $reply=&Apache::lonnet::critical(
927: 'update:'.$target.'.meta',$subhost);
928: $scrout.=$reply;
929: print $logfile $reply;
930: }
931: }
932: }
933: closedir(DIR);
934:
935: }
936:
937: # ------------------------------------------------ Provide link to new resource
938:
939: my $thisdistarget=$target;
940: $thisdistarget=~s/^$docroot//;
941:
942: my $thissrc=$source;
943: $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
944:
945: my $thissrcdir=$thissrc;
946: $thissrcdir=~s/\/[^\/]+$/\//;
947:
948:
949: return $warning.$scrout.
950: '<hr><a href="'.$thisdistarget.'"><font size=+2>View Target</font></a>'.
951: '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
952: '<p><a href="'.$thissrcdir.
953: '"><font size=+2>Back to Source Directory</font></a>';
954:
955: }
956:
957: # ================================================================ Main Handler
958:
959: sub handler {
960: my $r=shift;
961:
962: if ($r->header_only) {
963: $r->content_type('text/html');
964: $r->send_http_header;
965: return OK;
966: }
967:
968: # Get query string for limited number of parameters
969:
970: foreach (split(/&/,$ENV{'QUERY_STRING'})) {
971: my ($name, $value) = split(/=/,$_);
972: $value =~ tr/+/ /;
973: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
974: if ($name eq 'filename') {
975: unless ($ENV{'form.'.$name}) {
976: $ENV{'form.'.$name}=$value;
977: }
978: }
979: }
980:
981:
982: # -------------------------------------------------------------- Check filename
983:
984: my $fn=$ENV{'form.filename'};
985:
986:
987: unless ($fn) {
988: $r->log_reason($cuname.' at '.$cudom.
989: ' trying to publish empty filename', $r->filename);
990: return HTTP_NOT_FOUND;
991: }
992:
993: ($cuname,$cudom)=
994: &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
995: unless (($cuname) && ($cudom)) {
996: $r->log_reason($cuname.' at '.$cudom.
997: ' trying to publish file '.$ENV{'form.filename'}.
998: ' ('.$fn.') - not authorized',
999: $r->filename);
1000: return HTTP_NOT_ACCEPTABLE;
1001: }
1002:
1003: unless (&Apache::lonnet::homeserver($cuname,$cudom)
1004: eq $r->dir_config('lonHostID')) {
1005: $r->log_reason($cuname.' at '.$cudom.
1006: ' trying to publish file '.$ENV{'form.filename'}.
1007: ' ('.$fn.') - not homeserver ('.
1008: &Apache::lonnet::homeserver($cuname,$cudom).')',
1009: $r->filename);
1010: return HTTP_NOT_ACCEPTABLE;
1011: }
1012:
1013: $fn=~s/^http\:\/\/[^\/]+//;
1014: $fn=~s/^\/\~(\w+)/\/home\/$1\/public_html/;
1015:
1016: my $targetdir='';
1017: $docroot=$r->dir_config('lonDocRoot');
1018: if ($1 ne $cuname) {
1019: $r->log_reason($cuname.' at '.$cudom.
1020: ' trying to publish unowned file '.$ENV{'form.filename'}.
1021: ' ('.$fn.')',
1022: $r->filename);
1023: return HTTP_NOT_ACCEPTABLE;
1024: } else {
1025: $targetdir=$docroot.'/res/'.$cudom;
1026: }
1027:
1028:
1029: unless (-e $fn) {
1030: $r->log_reason($cuname.' at '.$cudom.
1031: ' trying to publish non-existing file '.$ENV{'form.filename'}.
1032: ' ('.$fn.')',
1033: $r->filename);
1034: return HTTP_NOT_FOUND;
1035: }
1036:
1037: unless ($ENV{'form.phase'} eq 'two') {
1038:
1039: # --------------------------------- File is there and owned, init lookup tables
1040:
1041: %addid=();
1042:
1043: {
1044: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
1045: while (<$fh>=~/(\w+)\s+(\w+)/) {
1046: $addid{$1}=$2;
1047: }
1048: }
1049:
1050: %nokey=();
1051:
1052: {
1053: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
1054: while (<$fh>) {
1055: my $word=$_;
1056: chomp($word);
1057: $nokey{$word}=1;
1058: }
1059: }
1060:
1061: }
1062:
1063: # ----------------------------------------------------------- Start page output
1064:
1065: $r->content_type('text/html');
1066: $r->send_http_header;
1067:
1068: $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
1069: $r->print(
1070: '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
1071: my $thisfn=$fn;
1072:
1073: # ------------------------------------------------------------- Individual file
1074: {
1075: $thisfn=~/\.(\w+)$/;
1076: my $thistype=$1;
1077: my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
1078:
1079: my $thistarget=$thisfn;
1080:
1081: $thistarget=~s/^\/home/$targetdir/;
1082: $thistarget=~s/\/public\_html//;
1083:
1084: my $thisdistarget=$thistarget;
1085: $thisdistarget=~s/^$docroot//;
1086:
1087: my $thisdisfn=$thisfn;
1088: $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
1089:
1090: $r->print('<h2>Publishing '.
1091: &Apache::loncommon::filedescription($thistype).' <tt>'.
1092: $thisdisfn.'</tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
1093:
1094: if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
1095: $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
1096: '</font></h3>');
1097: }
1098:
1099: if (&Apache::loncommon::fileembstyle($thistype) eq 'ssi') {
1100: $r->print('<br><a href="/adm/diff?filename=/~'.$cuname.'/'.
1101: $thisdisfn.
1102: '&versionone=priv" target=cat>Diffs with Current Version</a><p>');
1103: }
1104:
1105: # ------------ We are publishing from $thisfn to $thistarget with $thisembstyle
1106:
1107: unless ($ENV{'form.phase'} eq 'two') {
1108: $r->print(
1109: '<hr>'.&publish($thisfn,$thistarget,$thisembstyle));
1110: } else {
1111: $r->print(
1112: '<hr>'.&phasetwo($thisfn,$thistarget,$thisembstyle,$thisdistarget));
1113: }
1114:
1115: }
1116: $r->print('</body></html>');
1117:
1118: return OK;
1119: }
1120:
1121: 1;
1122: __END__
1123:
1124: =head1 NAME
1125:
1126: Apache::lonpublisher - Publication Handler
1127:
1128: =head1 SYNOPSIS
1129:
1130: Invoked by /etc/httpd/conf/srm.conf:
1131:
1132: <Location /adm/publish>
1133: PerlAccessHandler Apache::lonacc
1134: SetHandler perl-script
1135: PerlHandler Apache::lonpublisher
1136: ErrorDocument 403 /adm/login
1137: ErrorDocument 404 /adm/notfound.html
1138: ErrorDocument 406 /adm/unauthorized.html
1139: ErrorDocument 500 /adm/errorhandler
1140: </Location>
1141:
1142: =head1 INTRODUCTION
1143:
1144: This module publishes a file. This involves gathering metadata,
1145: versioning the file, copying file from construction space to
1146: publication space, and copying metadata from construction space
1147: to publication space.
1148:
1149: This is part of the LearningOnline Network with CAPA project
1150: described at http://www.lon-capa.org.
1151:
1152: =head1 HANDLER SUBROUTINE
1153:
1154: This routine is called by Apache and mod_perl.
1155:
1156: =over 4
1157:
1158: =item *
1159:
1160: Get query string for limited number of parameters
1161:
1162: =item *
1163:
1164: Check filename
1165:
1166: =item *
1167:
1168: File is there and owned, init lookup tables
1169:
1170: =item *
1171:
1172: Start page output
1173:
1174: =item *
1175:
1176: Individual file
1177:
1178: =item *
1179:
1180: publish from $thisfn to $thistarget with $thisembstyle
1181:
1182: =back
1183:
1184: =head1 OTHER SUBROUTINES
1185:
1186: =over 4
1187:
1188: =item *
1189:
1190: metaeval() : Evaluate string with metadata
1191:
1192: =item *
1193:
1194: metaread() : Read a metadata file
1195:
1196: =item *
1197:
1198: sqltime() : convert 'time' format into a datetime sql format
1199:
1200: =item *
1201:
1202: textfield() : form field
1203:
1204: =item *
1205:
1206: hiddenfield() : form field
1207:
1208: =item *
1209:
1210: selectbox() : form field
1211:
1212: =item *
1213:
1214: urlfixup() : fixup URL (Publication Step One)
1215:
1216: =item *
1217:
1218: publish() : publish (Publication Step One)
1219:
1220: =item *
1221:
1222: phasetwo() : render second interface showing status of publication steps
1223: (Publication Step Two)
1224:
1225: =back
1226:
1227: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>