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