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