1: # The LearningOnline Network with CAPA
2: # Publication Handler
3: #
4: # $Id: lonpublisher.pm,v 1.65 2001/12/15 18:15:27 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 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 ynchronized SQL metadata database";
761: }
762: # ----------------------------------------------------------- Copy old versions
763:
764: if (-e $target) {
765: my $filename;
766: my $maxversion=0;
767: $target=~/(.*)\/([^\/]+)\.(\w+)$/;
768: my $srcf=$2;
769: my $srct=$3;
770: my $srcd=$1;
771: unless ($srcd=~/^\/home\/httpd\/html\/res/) {
772: print $logfile "\nPANIC: Target dir is ".$srcd;
773: return "<font color=red>Invalid target directory, FAIL</font>";
774: }
775: opendir(DIR,$srcd);
776: while ($filename=readdir(DIR)) {
777: if ($filename=~/$srcf\.(\d+)\.$srct$/) {
778: $maxversion=($1>$maxversion)?$1:$maxversion;
779: }
780: }
781: closedir(DIR);
782: $maxversion++;
783: $scrout.='<p>Creating old version '.$maxversion;
784: print $logfile "\nCreating old version ".$maxversion;
785:
786: my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
787:
788: if (copy($target,$copyfile)) {
789: print $logfile "Copied old target to ".$copyfile."\n";
790: $scrout.='<p>Copied old target file';
791: } else {
792: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
793: return "<font color=red>Failed to copy old target, $!, FAIL</font>";
794: }
795:
796: # --------------------------------------------------------------- Copy Metadata
797:
798: $copyfile=$copyfile.'.meta';
799:
800: if (copy($target.'.meta',$copyfile)) {
801: print $logfile "Copied old target metadata to ".$copyfile."\n";
802: $scrout.='<p>Copied old metadata';
803: } else {
804: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
805: if (-e $target.'.meta') {
806: return
807: "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
808: }
809: }
810:
811:
812: } else {
813: $scrout.='<p>Initial version';
814: print $logfile "\nInitial version";
815: }
816:
817: # ---------------------------------------------------------------- Write Source
818: my $copyfile=$target;
819:
820: my @parts=split(/\//,$copyfile);
821: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
822:
823: my $count;
824: for ($count=5;$count<$#parts;$count++) {
825: $path.="/$parts[$count]";
826: if ((-e $path)!=1) {
827: print $logfile "\nCreating directory ".$path;
828: $scrout.='<p>Created directory '.$parts[$count];
829: mkdir($path,0777);
830: }
831: }
832:
833: if (copy($source,$copyfile)) {
834: print $logfile "Copied original source to ".$copyfile."\n";
835: $scrout.='<p>Copied source file';
836: } else {
837: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
838: return "<font color=red>Failed to copy source, $!, FAIL</font>";
839: }
840:
841: # --------------------------------------------------------------- Copy Metadata
842:
843: $copyfile=$copyfile.'.meta';
844:
845: if (copy($source.'.meta',$copyfile)) {
846: print $logfile "Copied original metadata to ".$copyfile."\n";
847: $scrout.='<p>Copied metadata';
848: } else {
849: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
850: return
851: "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
852: }
853:
854: # --------------------------------------------------- Send update notifications
855:
856: {
857:
858: my $filename;
859:
860: $target=~/(.*)\/([^\/]+)$/;
861: my $srcf=$2;
862: opendir(DIR,$1);
863: while ($filename=readdir(DIR)) {
864: if ($filename=~/$srcf\.(\w+)$/) {
865: my $subhost=$1;
866: if ($subhost ne 'meta') {
867: $scrout.='<p>Notifying host '.$subhost.':';
868: print $logfile "\nNotifying host '.$subhost.':'";
869: my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
870: $scrout.=$reply;
871: print $logfile $reply;
872: }
873: }
874: }
875: closedir(DIR);
876:
877: }
878:
879: # ---------------------------------------- Send update notifications, meta only
880:
881: {
882:
883: my $filename;
884:
885: $target=~/(.*)\/([^\/]+)$/;
886: my $srcf=$2.'.meta';
887: opendir(DIR,$1);
888: while ($filename=readdir(DIR)) {
889: if ($filename=~/$srcf\.(\w+)$/) {
890: my $subhost=$1;
891: if ($subhost ne 'meta') {
892: $scrout.=
893: '<p>Notifying host for metadata only '.$subhost.':';
894: print $logfile
895: "\nNotifying host for metadata only '.$subhost.':'";
896: my $reply=&Apache::lonnet::critical(
897: 'update:'.$target.'.meta',$subhost);
898: $scrout.=$reply;
899: print $logfile $reply;
900: }
901: }
902: }
903: closedir(DIR);
904:
905: }
906:
907: # ------------------------------------------------ Provide link to new resource
908:
909: my $thisdistarget=$target;
910: $thisdistarget=~s/^$docroot//;
911:
912: my $thissrc=$source;
913: $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
914:
915: my $thissrcdir=$thissrc;
916: $thissrcdir=~s/\/[^\/]+$/\//;
917:
918:
919: return $warning.$scrout.
920: '<hr><a href="'.$thisdistarget.'"><font size=+2>View Target</font></a>'.
921: '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
922: '<p><a href="'.$thissrcdir.
923: '"><font size=+2>Back to Source Directory</font></a>';
924:
925: }
926:
927: # ================================================================ Main Handler
928:
929: sub handler {
930: my $r=shift;
931:
932: if ($r->header_only) {
933: $r->content_type('text/html');
934: $r->send_http_header;
935: return OK;
936: }
937:
938: # Get query string for limited number of parameters
939:
940: foreach (split(/&/,$ENV{'QUERY_STRING'})) {
941: my ($name, $value) = split(/=/,$_);
942: $value =~ tr/+/ /;
943: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
944: if ($name eq 'filename') {
945: unless ($ENV{'form.'.$name}) {
946: $ENV{'form.'.$name}=$value;
947: }
948: }
949: }
950:
951:
952: # -------------------------------------------------------------- Check filename
953:
954: my $fn=$ENV{'form.filename'};
955:
956:
957: unless ($fn) {
958: $r->log_reason($cuname.' at '.$cudom.
959: ' trying to publish empty filename', $r->filename);
960: return HTTP_NOT_FOUND;
961: }
962:
963: ($cuname,$cudom)=
964: &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
965: unless (($cuname) && ($cudom)) {
966: $r->log_reason($cuname.' at '.$cudom.
967: ' trying to publish file '.$ENV{'form.filename'}.
968: ' ('.$fn.') - not authorized',
969: $r->filename);
970: return HTTP_NOT_ACCEPTABLE;
971: }
972:
973: unless (&Apache::lonnet::homeserver($cuname,$cudom)
974: eq $r->dir_config('lonHostID')) {
975: $r->log_reason($cuname.' at '.$cudom.
976: ' trying to publish file '.$ENV{'form.filename'}.
977: ' ('.$fn.') - not homeserver ('.
978: &Apache::lonnet::homeserver($cuname,$cudom).')',
979: $r->filename);
980: return HTTP_NOT_ACCEPTABLE;
981: }
982:
983: $fn=~s/^http\:\/\/[^\/]+//;
984: $fn=~s/^\/\~(\w+)/\/home\/$1\/public_html/;
985:
986: my $targetdir='';
987: $docroot=$r->dir_config('lonDocRoot');
988: if ($1 ne $cuname) {
989: $r->log_reason($cuname.' at '.$cudom.
990: ' trying to publish unowned file '.$ENV{'form.filename'}.
991: ' ('.$fn.')',
992: $r->filename);
993: return HTTP_NOT_ACCEPTABLE;
994: } else {
995: $targetdir=$docroot.'/res/'.$cudom;
996: }
997:
998:
999: unless (-e $fn) {
1000: $r->log_reason($cuname.' at '.$cudom.
1001: ' trying to publish non-existing file '.$ENV{'form.filename'}.
1002: ' ('.$fn.')',
1003: $r->filename);
1004: return HTTP_NOT_FOUND;
1005: }
1006:
1007: unless ($ENV{'form.phase'} eq 'two') {
1008:
1009: # --------------------------------- File is there and owned, init lookup tables
1010:
1011: %addid=();
1012:
1013: {
1014: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
1015: while (<$fh>=~/(\w+)\s+(\w+)/) {
1016: $addid{$1}=$2;
1017: }
1018: }
1019:
1020: %nokey=();
1021:
1022: {
1023: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
1024: while (<$fh>) {
1025: my $word=$_;
1026: chomp($word);
1027: $nokey{$word}=1;
1028: }
1029: }
1030:
1031: }
1032:
1033: # ----------------------------------------------------------- Start page output
1034:
1035: $r->content_type('text/html');
1036: $r->send_http_header;
1037:
1038: $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
1039: $r->print(
1040: '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
1041: my $thisfn=$fn;
1042:
1043: # ------------------------------------------------------------- Individual file
1044: {
1045: $thisfn=~/\.(\w+)$/;
1046: my $thistype=$1;
1047: my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
1048:
1049: my $thistarget=$thisfn;
1050:
1051: $thistarget=~s/^\/home/$targetdir/;
1052: $thistarget=~s/\/public\_html//;
1053:
1054: my $thisdistarget=$thistarget;
1055: $thisdistarget=~s/^$docroot//;
1056:
1057: my $thisdisfn=$thisfn;
1058: $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
1059:
1060: $r->print('<h2>Publishing '.
1061: &Apache::lonnet::filedescription($thistype).' <tt>'.
1062: $thisdisfn.'</tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
1063:
1064: if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
1065: $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
1066: '</font></h3>');
1067: }
1068:
1069: if (&Apache::loncommon::fileembstyle($thistype) eq 'ssi') {
1070: $r->print('<br><a href="/adm/diff?filename=/~'.$cuname.'/'.
1071: $thisdisfn.
1072: '&versionone=priv" target=cat>Diffs with Current Version</a><p>');
1073: }
1074:
1075: # ------------ We are publishing from $thisfn to $thistarget with $thisembstyle
1076:
1077: unless ($ENV{'form.phase'} eq 'two') {
1078: $r->print(
1079: '<hr>'.&publish($thisfn,$thistarget,$thisembstyle));
1080: } else {
1081: $r->print(
1082: '<hr>'.&phasetwo($thisfn,$thistarget,$thisembstyle,$thisdistarget));
1083: }
1084:
1085: }
1086: $r->print('</body></html>');
1087:
1088: return OK;
1089: }
1090:
1091: 1;
1092: __END__
1093:
1094:
1095:
1096:
1097:
1098:
1099:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>