1: # The LearningOnline Network with CAPA
2: # Publication Handler
3: #
4: # $Id: lonpublisher.pm,v 1.101 2002/10/08 13:24:36 www Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: #
29: # (TeX Content Handler
30: #
31: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
32: #
33: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
34: # 03/23 Guy Albertelli
35: # 03/24,03/29,04/03 Gerd Kortemeyer
36: # 04/16/2001 Scott Harrison
37: # 05/03,05/05,05/07 Gerd Kortemeyer
38: # 05/28/2001 Scott Harrison
39: # 06/23,08/07,08/11,8/13,8/17,8/18,8/24,9/26,10/16 Gerd Kortemeyer
40: # 12/04,12/05 Guy Albertelli
41: # 12/05 Gerd Kortemeyer
42: # 12/05 Guy Albertelli
43: # 12/06,12/07 Gerd Kortemeyer
44: # 12/15,12/16 Scott Harrison
45: # 12/25 Gerd Kortemeyer
46: # YEAR=2002
47: # 1/16,1/17 Scott Harrison
48: # 1/17 Gerd Kortemeyer
49: #
50: ###
51:
52: ###############################################################################
53: ## ##
54: ## ORGANIZATION OF THIS PERL MODULE ##
55: ## ##
56: ## 1. Modules used by this module ##
57: ## 2. Various subroutines ##
58: ## 3. Publication Step One ##
59: ## 4. Phase Two ##
60: ## 5. Main Handler ##
61: ## ##
62: ###############################################################################
63:
64:
65: ######################################################################
66: ######################################################################
67:
68: =pod
69:
70: =head1 NAME
71:
72: lonpublisher - LON-CAPA publishing handler
73:
74: =head1 SYNOPSIS
75:
76: B<lonpublisher> is used by B<mod_perl> inside B<Apache>. This is the
77: invocation by F<loncapa_apache.conf>:
78:
79: <Location /adm/publish>
80: PerlAccessHandler Apache::lonacc
81: SetHandler perl-script
82: PerlHandler Apache::lonpublisher
83: ErrorDocument 403 /adm/login
84: ErrorDocument 404 /adm/notfound.html
85: ErrorDocument 406 /adm/unauthorized.html
86: ErrorDocument 500 /adm/errorhandler
87: </Location>
88:
89: =head1 DESCRIPTION
90:
91: B<lonpublisher> takes the proper steps to add resources to the LON-CAPA
92: digital library. This includes updating the metadata table in the
93: LON-CAPA database.
94:
95: B<lonpublisher> is many things to many people.
96:
97: This module publishes a file. This involves gathering metadata,
98: versioning the file, copying file from construction space to
99: publication space, and copying metadata from construction space
100: to publication space.
101:
102: =head2 SUBROUTINES
103:
104: Many of the undocumented subroutines implement various magical
105: parsing shortcuts.
106:
107: =over 4
108:
109: =cut
110:
111: ######################################################################
112: ######################################################################
113:
114:
115: package Apache::lonpublisher;
116:
117: # ------------------------------------------------- modules used by this module
118: use strict;
119: use Apache::File;
120: use File::Copy;
121: use Apache::Constants qw(:common :http :methods);
122: use HTML::LCParser;
123: use Apache::lonxml;
124: use Apache::lonhomework;
125: use Apache::loncacc;
126: use DBI;
127: use Apache::lonnet();
128: use Apache::loncommon();
129: use Apache::lonmysql;
130:
131: my %addid;
132: my %nokey;
133:
134: my %metadatafields;
135: my %metadatakeys;
136:
137: my $docroot;
138:
139: my $cuname;
140: my $cudom;
141:
142: #########################################
143: #########################################
144:
145: =pod
146:
147: =item B<metaeval>
148:
149: Evaluates a string that contains metadata. This subroutine
150: stores values inside I<%metadatafields> and I<%metadatakeys>.
151: The hash key is a I<$unikey> corresponding to a unique id
152: that is descriptive of the parser location inside the XML tree.
153:
154: Parameters:
155:
156: =over 4
157:
158: =item I<$metastring>
159:
160: A string that contains metadata.
161:
162: =back
163:
164: Returns:
165:
166: nothing
167:
168: =cut
169:
170: #########################################
171: #########################################
172: sub metaeval {
173: my $metastring=shift;
174:
175: my $parser=HTML::LCParser->new(\$metastring);
176: my $token;
177: while ($token=$parser->get_token) {
178: if ($token->[0] eq 'S') {
179: my $entry=$token->[1];
180: my $unikey=$entry;
181: if (defined($token->[2]->{'package'})) {
182: $unikey.='_package_'.$token->[2]->{'package'};
183: }
184: if (defined($token->[2]->{'part'})) {
185: $unikey.='_'.$token->[2]->{'part'};
186: }
187: if (defined($token->[2]->{'id'})) {
188: $unikey.='_'.$token->[2]->{'id'};
189: }
190: if (defined($token->[2]->{'name'})) {
191: $unikey.='_'.$token->[2]->{'name'};
192: }
193: foreach (@{$token->[3]}) {
194: $metadatafields{$unikey.'.'.$_}=$token->[2]->{$_};
195: if ($metadatakeys{$unikey}) {
196: $metadatakeys{$unikey}.=','.$_;
197: } else {
198: $metadatakeys{$unikey}=$_;
199: }
200: }
201: if ($metadatafields{$unikey}) {
202: my $newentry=$parser->get_text('/'.$entry);
203: unless (($metadatafields{$unikey}=~/$newentry/) ||
204: ($newentry eq '')) {
205: $metadatafields{$unikey}.=', '.$newentry;
206: }
207: } else {
208: $metadatafields{$unikey}=$parser->get_text('/'.$entry);
209: }
210: }
211: }
212: }
213:
214: #########################################
215: #########################################
216:
217: =pod
218:
219: =item B<metaread>
220:
221: Read a metadata file
222:
223: Parameters:
224:
225: =over
226:
227: =item I<$logfile>
228:
229: File output stream to output errors and warnings to.
230:
231: =item I<$fn>
232:
233: File name (including path).
234:
235: =back
236:
237: Returns:
238:
239: =over 4
240:
241: =item Scalar string (if successful)
242:
243: XHTML text that indicates successful reading of the metadata.
244:
245: =back
246:
247: =cut
248:
249: #########################################
250: #########################################
251: sub metaread {
252: my ($logfile,$fn)=@_;
253: unless (-e $fn) {
254: print($logfile 'No file '.$fn."\n");
255: return '<br><b>No file:</b> <tt>'.$fn.'</tt>';
256: }
257: print($logfile 'Processing '.$fn."\n");
258: my $metastring;
259: {
260: my $metafh=Apache::File->new($fn);
261: $metastring=join('',<$metafh>);
262: }
263: &metaeval($metastring);
264: return '<br><b>Processed file:</b> <tt>'.$fn.'</tt>';
265: }
266:
267: #########################################
268: #########################################
269:
270: =pod
271:
272: =item B<sqltime>
273:
274: Convert 'time' format into a datetime sql format
275:
276: Parameters:
277:
278: =over 4
279:
280: =item I<$timef>
281:
282: Seconds since 00:00:00 UTC, January 1, 1970.
283:
284: =back
285:
286: Returns:
287:
288: =over 4
289:
290: =item Scalar string
291:
292: MySQL-compatible datetime string.
293:
294: =back
295:
296: =cut
297:
298: #########################################
299: #########################################
300: sub sqltime {
301: my $timef=shift @_;
302: my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
303: localtime($timef);
304: $mon++; $year+=1900;
305: return "$year-$mon-$mday $hour:$min:$sec";
306: }
307:
308:
309: #########################################
310: #########################################
311:
312: sub coursedependencies {
313: my $url=&Apache::lonnet::declutter(shift);
314: $url=~s/\.meta$//;
315: my ($adomain,$aauthor)=($url=~/^(\w+)\/(\w+)\//);
316: my $regexp=$url;
317: $regexp=~s/(\W)/\\$1/g;
318: $regexp='___'.$regexp.'___course';
319: my %evaldata=&Apache::lonnet::dump('nohist_resevaldata',$adomain,
320: $aauthor,$regexp);
321: my %courses=();
322: foreach (keys %evaldata) {
323: if ($_=~/^([a-zA-Z0-9]+_[a-zA-Z0-9]+)___.+___course$/) {
324: $courses{$1}=1;
325: }
326: }
327: return %courses;
328: }
329: #########################################
330: #########################################
331:
332:
333: =pod
334:
335: =item Form-field-generating subroutines.
336:
337: For input parameters, these subroutines take in values
338: such as I<$name>, I<$value> and other form field metadata.
339: The output (scalar string that is returned) is an XHTML
340: string which presents the form field (foreseeably inside
341: <form></form> tags).
342:
343: =over 4
344:
345: =item B<textfield>
346:
347: =item B<hiddenfield>
348:
349: =item B<selectbox>
350:
351: =back
352:
353: =cut
354:
355: #########################################
356: #########################################
357: sub textfield {
358: my ($title,$name,$value)=@_;
359: return "\n<p><b>$title:</b><br>".
360: '<input type="text" name="'.$name.'" size=80 value="'.$value.'" />';
361: }
362:
363: sub hiddenfield {
364: my ($name,$value)=@_;
365: return "\n".'<input type="hidden" name="'.$name.'" value="'.$value.'" />';
366: }
367:
368: sub selectbox {
369: my ($title,$name,$value,$functionref,@idlist)=@_;
370: my $uctitle=uc($title);
371: my $selout="\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
372: "</b></font><br />".'<select name="'.$name.'">';
373: foreach (@idlist) {
374: $selout.='<option value=\''.$_.'\'';
375: if ($_ eq $value) {
376: $selout.=' selected>'.&{$functionref}($_).'</option>';
377: }
378: else {$selout.='>'.&{$functionref}($_).'</option>';}
379: }
380: return $selout.'</select>';
381: }
382:
383: #########################################
384: #########################################
385:
386: =pod
387:
388: =item B<urlfixup>
389:
390: Fix up a url? First step of publication
391:
392: =cut
393:
394: #########################################
395: #########################################
396: sub urlfixup {
397: my ($url,$target)=@_;
398: unless ($url) { return ''; }
399: #javascript code needs no fixing
400: if ($url =~ /^javascript:/i) { return $url; }
401: if ($url =~ /^mailto:/i) { return $url; }
402: #internal document links need no fixing
403: if ($url =~ /^\#/) { return $url; }
404: my ($host)=($url=~/(?:http\:\/\/)*([^\/]+)/);
405: foreach (values %Apache::lonnet::hostname) {
406: if ($_ eq $host) {
407: $url=~s/^http\:\/\///;
408: $url=~s/^$host//;
409: }
410: }
411: if ($url=~/^http\:\/\//) { return $url; }
412: $url=~s/\~$cuname/res\/$cudom\/$cuname/;
413: return $url;
414: }
415:
416: #########################################
417: #########################################
418:
419: =pod
420:
421: =item B<absoluteurl>
422:
423: Currently undocumented.
424:
425: =cut
426:
427: #########################################
428: #########################################
429: sub absoluteurl {
430: my ($url,$target)=@_;
431: unless ($url) { return ''; }
432: if ($target) {
433: $target=~s/\/[^\/]+$//;
434: $url=&Apache::lonnet::hreflocation($target,$url);
435: }
436: return $url;
437: }
438:
439: #########################################
440: #########################################
441:
442: =pod
443:
444: =item B<set_allow>
445:
446: Currently undocumented
447:
448: =cut
449:
450: #########################################
451: #########################################
452: sub set_allow {
453: my ($allow,$logfile,$target,$tag,$oldurl)=@_;
454: my $newurl=&urlfixup($oldurl,$target);
455: my $return_url=$oldurl;
456: print $logfile 'GUYURL: '.$tag.':'.$oldurl.' - '.$newurl."\n";
457: if ($newurl ne $oldurl) {
458: $return_url=$newurl;
459: print $logfile 'URL: '.$tag.':'.$oldurl.' - '.$newurl."\n";
460: }
461: if (($newurl !~ /^javascript:/i) &&
462: ($newurl !~ /^mailto:/i) &&
463: ($newurl !~ /^http:/i) &&
464: ($newurl !~ /^\#/)) {
465: $$allow{&absoluteurl($newurl,$target)}=1;
466: }
467: return $return_url
468: }
469:
470: #########################################
471: #########################################
472:
473: =pod
474:
475: =item B<get_subscribed_hosts>
476:
477: Currently undocumented
478:
479: =cut
480:
481: #########################################
482: #########################################
483: sub get_subscribed_hosts {
484: my ($target)=@_;
485: my @subscribed;
486: my $filename;
487: $target=~/(.*)\/([^\/]+)$/;
488: my $srcf=$2;
489: opendir(DIR,$1);
490: while ($filename=readdir(DIR)) {
491: if ($filename=~/$srcf\.(\w+)$/) {
492: my $subhost=$1;
493: if (($subhost ne 'meta' && $subhost ne 'subscription') &&
494: ($subhost ne $Apache::lonnet::perlvar{'lonHostID'})) {
495: push(@subscribed,$subhost);
496: }
497: }
498: }
499: closedir(DIR);
500: my $sh;
501: if ( $sh=Apache::File->new("$target.subscription") ) {
502: &Apache::lonnet::logthis("opened $target.subscription");
503: while (my $subline=<$sh>) {
504: &Apache::lonnet::logthis("Trying $subline");
505: if ($subline =~ /(^\w+):/) {
506: if ($1 ne $Apache::lonnet::perlvar{'lonHostID'}) {
507: push(@subscribed,$1);
508: }
509: } else {
510: &Apache::lonnet::logthis("No Match for $subline");
511: }
512: }
513: } else {
514: &Apache::lonnet::logthis("Unable to open $target.subscription");
515: }
516: return @subscribed;
517: }
518:
519:
520: #########################################
521: #########################################
522:
523: =pod
524:
525: =item B<get_max_ids_indices>
526:
527: Currently undocumented
528:
529: =cut
530:
531: #########################################
532: #########################################
533: sub get_max_ids_indices {
534: my ($content)=@_;
535: my $maxindex=10;
536: my $maxid=10;
537: my $needsfixup=0;
538:
539: my $parser=HTML::LCParser->new($content);
540: my $token;
541: while ($token=$parser->get_token) {
542: if ($token->[0] eq 'S') {
543: my $counter;
544: if ($counter=$addid{$token->[1]}) {
545: if ($counter eq 'id') {
546: if (defined($token->[2]->{'id'})) {
547: $maxid=($token->[2]->{'id'}>$maxid)?$token->[2]->{'id'}:$maxid;
548: } else {
549: $needsfixup=1;
550: }
551: } else {
552: if (defined($token->[2]->{'index'})) {
553: $maxindex=($token->[2]->{'index'}>$maxindex)?$token->[2]->{'index'}:$maxindex;
554: } else {
555: $needsfixup=1;
556: }
557: }
558: }
559: }
560: }
561: return ($needsfixup,$maxid,$maxindex);
562: }
563:
564: #########################################
565: #########################################
566:
567: =pod
568:
569: =item B<get_all_text_unbalanced>
570:
571: Currently undocumented
572:
573: =cut
574:
575: #########################################
576: #########################################
577: sub get_all_text_unbalanced {
578: #there is a copy of this in lonxml.pm
579: my($tag,$pars)= @_;
580: my $token;
581: my $result='';
582: $tag='<'.$tag.'>';
583: while ($token = $$pars[-1]->get_token) {
584: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
585: $result.=$token->[1];
586: } elsif ($token->[0] eq 'PI') {
587: $result.=$token->[2];
588: } elsif ($token->[0] eq 'S') {
589: $result.=$token->[4];
590: } elsif ($token->[0] eq 'E') {
591: $result.=$token->[2];
592: }
593: if ($result =~ /(.*)$tag(.*)/) {
594: #&Apache::lonnet::logthis('Got a winner with leftovers ::'.$2);
595: #&Apache::lonnet::logthis('Result is :'.$1);
596: $result=$1;
597: my $redo=$tag.$2;
598: push (@$pars,HTML::LCParser->new(\$redo));
599: $$pars[-1]->xml_mode('1');
600: last;
601: }
602: }
603: return $result
604: }
605:
606: #########################################
607: #########################################
608:
609: =pod
610:
611: =item B<fix_ids_and_indices>
612:
613: Currently undocumented
614:
615: =cut
616:
617: #########################################
618: #########################################
619: #Arguably this should all be done as a lonnet::ssi instead
620: sub fix_ids_and_indices {
621: my ($logfile,$source,$target)=@_;
622:
623: my %allow;
624: my $content;
625: {
626: my $org=Apache::File->new($source);
627: $content=join('',<$org>);
628: }
629:
630: my ($needsfixup,$maxid,$maxindex)=&get_max_ids_indices(\$content);
631:
632: if ($needsfixup) {
633: print $logfile "Needs ID and/or index fixup\n".
634: "Max ID : $maxid (min 10)\n".
635: "Max Index: $maxindex (min 10)\n";
636: }
637: my $outstring='';
638: my @parser;
639: $parser[0]=HTML::LCParser->new(\$content);
640: $parser[-1]->xml_mode(1);
641: my $token;
642: while (@parser) {
643: while ($token=$parser[-1]->get_token) {
644: if ($token->[0] eq 'S') {
645: my $counter;
646: my $tag=$token->[1];
647: my $lctag=lc($tag);
648: if ($lctag eq 'allow') {
649: $allow{$token->[2]->{'src'}}=1;
650: next;
651: }
652: my %parms=%{$token->[2]};
653: $counter=$addid{$tag};
654: if (!$counter) { $counter=$addid{$lctag}; }
655: if ($counter) {
656: if ($counter eq 'id') {
657: unless (defined($parms{'id'})) {
658: $maxid++;
659: $parms{'id'}=$maxid;
660: print $logfile 'ID: '.$tag.':'.$maxid."\n";
661: }
662: } elsif ($counter eq 'index') {
663: unless (defined($parms{'index'})) {
664: $maxindex++;
665: $parms{'index'}=$maxindex;
666: print $logfile 'Index: '.$tag.':'.$maxindex."\n";
667: }
668: }
669: }
670: foreach my $type ('src','href','background','bgimg') {
671: foreach my $key (keys(%parms)) {
672: if ($key =~ /^$type$/i) {
673: $parms{$key}=&set_allow(\%allow,$logfile,
674: $target,$tag,
675: $parms{$key});
676: }
677: }
678: }
679: # probably a <randomlabel> image type <label>
680: if ($lctag eq 'label' && defined($parms{'description'})) {
681: my $next_token=$parser[-1]->get_token();
682: if ($next_token->[0] eq 'T') {
683: $next_token->[1]=&set_allow(\%allow,$logfile,
684: $target,$tag,
685: $next_token->[1]);
686: }
687: $parser[-1]->unget_token($next_token);
688: }
689: if ($lctag eq 'applet') {
690: my $codebase='';
691: if (defined($parms{'codebase'})) {
692: my $oldcodebase=$parms{'codebase'};
693: unless ($oldcodebase=~/\/$/) {
694: $oldcodebase.='/';
695: }
696: $codebase=&urlfixup($oldcodebase,$target);
697: $codebase=~s/\/$//;
698: if ($codebase ne $oldcodebase) {
699: $parms{'codebase'}=$codebase;
700: print $logfile 'URL codebase: '.$tag.':'.
701: $oldcodebase.' - '.
702: $codebase."\n";
703: }
704: $allow{&absoluteurl($codebase,$target).'/*'}=1;
705: } else {
706: foreach ('archive','code','object') {
707: if (defined($parms{$_})) {
708: my $oldurl=$parms{$_};
709: my $newurl=&urlfixup($oldurl,$target);
710: $newurl=~s/\/[^\/]+$/\/\*/;
711: print $logfile 'Allow: applet '.$_.':'.
712: $oldurl.' allows '.
713: $newurl."\n";
714: $allow{&absoluteurl($newurl,$target)}=1;
715: }
716: }
717: }
718: }
719: my $newparmstring='';
720: my $endtag='';
721: foreach (keys %parms) {
722: if ($_ eq '/') {
723: $endtag=' /';
724: } else {
725: my $quote=($parms{$_}=~/\"/?"'":'"');
726: $newparmstring.=' '.$_.'='.$quote.$parms{$_}.$quote;
727: }
728: }
729: if (!$endtag) { if ($token->[4]=~m:/>$:) { $endtag=' /'; }; }
730: $outstring.='<'.$tag.$newparmstring.$endtag.'>';
731: if ($lctag eq 'm') {
732: $outstring.=&get_all_text_unbalanced('/m',\@parser);
733: }
734: } elsif ($token->[0] eq 'E') {
735: if ($token->[2]) {
736: unless ($token->[1] eq 'allow') {
737: $outstring.='</'.$token->[1].'>';
738: }
739: }
740: } else {
741: $outstring.=$token->[1];
742: }
743: }
744: pop(@parser);
745: }
746:
747: if ($needsfixup) {
748: print $logfile "End of ID and/or index fixup\n".
749: "Max ID : $maxid (min 10)\n".
750: "Max Index: $maxindex (min 10)\n";
751: } else {
752: print $logfile "Does not need ID and/or index fixup\n";
753: }
754:
755: return ($outstring,%allow);
756: }
757:
758: #########################################
759: #########################################
760:
761: =pod
762:
763: =item B<store_metadata>
764:
765: Store the metadata in the metadata table in the loncapa database.
766: Uses lonmysql to access the database.
767:
768: Inputs: \%metadata
769:
770: Returns: (error,status). error is undef on success, status is undef on error.
771:
772: =cut
773:
774: #########################################
775: #########################################
776: sub store_metadata {
777: my %metadata = %{shift()};
778: my $error;
779: # Determine if the table exists
780: my $status = &Apache::lonmysql::check_table('metadata');
781: if (! defined($status)) {
782: $error='<font color="red">WARNING: Cannot connect to '.
783: 'database!</font>';
784: &Apache::lonnet::logthis($error);
785: return ($error,undef);
786: }
787: if ($status == 0) {
788: # It would be nice to actually create the table....
789: $error ='<font color="red">WARNING: The metadata table does not '.
790: 'exist in the LON-CAPA database.</font>';
791: &Apache::lonnet::logthis($error);
792: return ($error,undef);
793: }
794: # Remove old value from table
795: $status = &Apache::lonmysql::remove_from_table
796: ('metadata','url',$metadata{'url'});
797: if (! defined($status)) {
798: $error = '<font color="red">Error when removing old values from '.
799: 'metadata table in LON-CAPA database.</font>';
800: &Apache::lonnet::logthis($error);
801: return ($error,undef);
802: }
803: # Store data in table.
804: $status = &Apache::lonmysql::store_row('metadata',\%metadata);
805: if (! defined($status)) {
806: $error='<font color="red">Error occured storing new values in '.
807: 'metadata table in LON-CAPA database</font>';
808: &Apache::lonnet::logthis($error);
809: return ($error,undef);
810: }
811: return (undef,$status);
812: }
813:
814: #########################################
815: #########################################
816:
817: =pod
818:
819: =item B<publish>
820:
821: This is the workhorse function of this module. This subroutine generates
822: backup copies, performs any automatic processing (prior to publication,
823: especially for rat and ssi files),
824:
825: I<Additional documentation needed.>
826:
827: =cut
828:
829: #########################################
830: #########################################
831: sub publish {
832:
833: my ($source,$target,$style,$batch)=@_;
834: my $logfile;
835: my $scrout='';
836: my $allmeta='';
837: my $content='';
838: my %allow=();
839:
840: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
841: return
842: '<font color=red>No write permission to user directory, FAIL</font>';
843: }
844: print $logfile
845: "\n\n================= Publish ".localtime()." Phase One ================\n";
846:
847: if (($style eq 'ssi') || ($style eq 'rat')) {
848: # ------------------------------------------------------- This needs processing
849:
850: # ----------------------------------------------------------------- Backup Copy
851: my $copyfile=$source.'.save';
852: if (copy($source,$copyfile)) {
853: print $logfile "Copied original file to ".$copyfile."\n";
854: } else {
855: print $logfile "Unable to write backup ".$copyfile.':'.$!."\n";
856: return "<font color=red>Failed to write backup copy, $!,FAIL</font>";
857: }
858: # ------------------------------------------------------------- IDs and indices
859:
860: my $outstring;
861: ($outstring,%allow)=&fix_ids_and_indices($logfile,$source,$target);
862: # ------------------------------------------------------------ Construct Allows
863:
864: $scrout.='<h3>Dependencies</h3>';
865: my $allowstr='';
866: foreach (sort(keys(%allow))) {
867: my $thisdep=$_;
868: if ($thisdep !~ /[^\s]/) { next; }
869: unless ($style eq 'rat') {
870: $allowstr.="\n".'<allow src="'.$thisdep.'" />';
871: }
872: $scrout.='<br>';
873: unless ($thisdep=~/\*/) {
874: $scrout.='<a href="'.$thisdep.'">';
875: }
876: $scrout.='<tt>'.$thisdep.'</tt>';
877: unless ($thisdep=~/\*/) {
878: $scrout.='</a>';
879: if (
880: &Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
881: $thisdep.'.meta') eq '-1') {
882: $scrout.= ' - <font color="red">Currently not available'.
883: '</font>';
884: } else {
885: my %temphash=(&Apache::lonnet::declutter($target).'___'.
886: &Apache::lonnet::declutter($thisdep).'___usage'
887: => time);
888: $thisdep=~/^\/res\/(\w+)\/(\w+)\//;
889: if ((defined($1)) && (defined($2))) {
890: &Apache::lonnet::put('nohist_resevaldata',\%temphash,
891: $1,$2);
892: }
893: }
894: }
895: }
896: $outstring=~s/\n*(\<\/[^\>]+\>)\s*$/$allowstr\n$1\n/s;
897:
898: #Encode any High ASCII characters
899: $outstring=&HTML::Entities::encode($outstring,"\200-\377");
900: # ------------------------------------------------------------- Write modified.
901:
902: {
903: my $org;
904: unless ($org=Apache::File->new('>'.$source)) {
905: print $logfile "No write permit to $source\n";
906: return
907: '<font color="red">No write permission to '.$source.
908: ', FAIL</font>';
909: }
910: print($org $outstring);
911: }
912: $content=$outstring;
913:
914: }
915: # -------------------------------------------- Initial step done, now metadata.
916:
917: # --------------------------------------- Storage for metadata keys and fields.
918:
919: %metadatafields=();
920: %metadatakeys=();
921:
922: my %oldparmstores=();
923:
924: unless ($batch) {
925: $scrout.='<h3>Metadata Information ' .
926: Apache::loncommon::help_open_topic("Metadata_Description")
927: . '</h3>';
928: }
929:
930: # ------------------------------------------------ First, check out environment
931: unless (-e $source.'.meta') {
932: $metadatafields{'author'}=$ENV{'environment.firstname'}.' '.
933: $ENV{'environment.middlename'}.' '.
934: $ENV{'environment.lastname'}.' '.
935: $ENV{'environment.generation'};
936: $metadatafields{'author'}=~s/\s+/ /g;
937: $metadatafields{'author'}=~s/\s+$//;
938: $metadatafields{'owner'}=$cuname.'@'.$cudom;
939:
940: # ------------------------------------------------ Check out directory hierachy
941:
942: my $thisdisfn=$source;
943: $thisdisfn=~s/^\/home\/$cuname\///;
944:
945: my @urlparts=split(/\//,$thisdisfn);
946: $#urlparts--;
947:
948: my $currentpath='/home/'.$cuname.'/';
949:
950: foreach (@urlparts) {
951: $currentpath.=$_.'/';
952: $scrout.=&metaread($logfile,$currentpath.'default.meta');
953: }
954:
955: # ------------------- Clear out parameters and stores (there should not be any)
956:
957: foreach (keys %metadatafields) {
958: if (($_=~/^parameter/) || ($_=~/^stores/)) {
959: delete $metadatafields{$_};
960: }
961: }
962:
963: } else {
964: # ---------------------- Read previous metafile, remember parameters and stores
965:
966: $scrout.=&metaread($logfile,$source.'.meta');
967:
968: foreach (keys %metadatafields) {
969: if (($_=~/^parameter/) || ($_=~/^stores/)) {
970: $oldparmstores{$_}=1;
971: delete $metadatafields{$_};
972: }
973: }
974:
975: }
976:
977: # -------------------------------------------------- Parse content for metadata
978: if ($style eq 'ssi') {
979: my $oldenv=$ENV{'request.uri'};
980:
981: $ENV{'request.uri'}=$target;
982: $allmeta=Apache::lonxml::xmlparse(undef,'meta',$content);
983: $ENV{'request.uri'}=$oldenv;
984:
985: &metaeval($allmeta);
986: }
987: # ---------------- Find and document discrepancies in the parameters and stores
988:
989: my $chparms='';
990: foreach (sort keys %metadatafields) {
991: if (($_=~/^parameter/) || ($_=~/^stores/)) {
992: unless ($_=~/\.\w+$/) {
993: unless ($oldparmstores{$_}) {
994: print $logfile 'New: '.$_."\n";
995: $chparms.=$_.' ';
996: }
997: }
998: }
999: }
1000: if ($chparms) {
1001: $scrout.='<p><b>New parameters or stored values:</b> '.
1002: $chparms;
1003: }
1004:
1005: $chparms='';
1006: foreach (sort keys %oldparmstores) {
1007: if (($_=~/^parameter/) || ($_=~/^stores/)) {
1008: unless (($metadatafields{$_.'.name'}) ||
1009: ($metadatafields{$_.'.package'}) || ($_=~/\.\w+$/)) {
1010: print $logfile 'Obsolete: '.$_."\n";
1011: $chparms.=$_.' ';
1012: }
1013: }
1014: }
1015: if ($chparms) {
1016: $scrout.='<p><b>Obsolete parameters or stored values:</b> '.
1017: $chparms;
1018: }
1019:
1020: # ------------------------------------------------------- Now have all metadata
1021:
1022: my %keywords=();
1023:
1024: if (length($content)<500000) {
1025: my $textonly=$content;
1026: $textonly=~s/\<script[^\<]+\<\/script\>//g;
1027: $textonly=~s/\<m\>[^\<]+\<\/m\>//g;
1028: $textonly=~s/\<[^\>]*\>//g;
1029: $textonly=~tr/A-Z/a-z/;
1030: $textonly=~s/[\$\&][a-z]\w*//g;
1031: $textonly=~s/[^a-z\s]//g;
1032:
1033: foreach ($textonly=~m/(\w+)/g) {
1034: unless ($nokey{$_}) {
1035: $keywords{$_}=1;
1036: }
1037: }
1038: }
1039:
1040:
1041: foreach (split(/\W+/,$metadatafields{'keywords'})) {
1042: $keywords{$_}=1;
1043: }
1044: # --------------------------------------------------- Now we also have keywords
1045: # =============================================================================
1046: # INTERACTIVE MODE
1047: #
1048: unless ($batch) {
1049: $scrout.=
1050: '<form name="pubform" action="/adm/publish" method="post">'.
1051: '<p><input type="submit" value="Finalize Publication" /></p>'.
1052: &hiddenfield('phase','two').
1053: &hiddenfield('filename',$ENV{'form.filename'}).
1054: &hiddenfield('allmeta',&Apache::lonnet::escape($allmeta)).
1055: &hiddenfield('dependencies',join(',',keys %allow)).
1056: &textfield('Title','title',$metadatafields{'title'}).
1057: &textfield('Author(s)','author',$metadatafields{'author'}).
1058: &textfield('Subject','subject',$metadatafields{'subject'});
1059:
1060: # --------------------------------------------------- Scan content for keywords
1061:
1062: my $keywords_help = Apache::loncommon::help_open_topic("Publishing_Keywords");
1063: my $keywordout=<<"END";
1064: <script>
1065: function checkAll(field)
1066: {
1067: for (i = 0; i < field.length; i++)
1068: field[i].checked = true ;
1069: }
1070:
1071: function uncheckAll(field)
1072: {
1073: for (i = 0; i < field.length; i++)
1074: field[i].checked = false ;
1075: }
1076: </script>
1077: <p><b>Keywords: $keywords_help</b>
1078: <input type="button" value="check all" onclick="javascript:checkAll(document.pubform.keywords)">
1079: <input type="button" value="uncheck all" onclick="javascript:uncheckAll(document.pubform.keywords)">
1080: <br />
1081: END
1082: $keywordout.='<table border=2><tr>';
1083: my $colcount=0;
1084:
1085: foreach (sort keys %keywords) {
1086: $keywordout.='<td><input type=checkbox name="keywords" value="'.$_.'"';
1087: if ($metadatafields{'keywords'}) {
1088: if ($metadatafields{'keywords'}=~/$_/) {
1089: $keywordout.=' checked';
1090: }
1091: } elsif (&Apache::loncommon::keyword($_)) {
1092: $keywordout.=' checked';
1093: }
1094: $keywordout.='>'.$_.'</td>';
1095: if ($colcount>10) {
1096: $keywordout.="</tr><tr>\n";
1097: $colcount=0;
1098: }
1099: $colcount++;
1100: }
1101:
1102: $keywordout.='</tr></table>';
1103:
1104: $scrout.=$keywordout;
1105:
1106: $scrout.=&textfield('Additional Keywords','addkey','');
1107:
1108: $scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
1109:
1110: $scrout.=
1111: '<p><b>Abstract:</b><br><textarea cols=80 rows=5 name=abstract>'.
1112: $metadatafields{'abstract'}.'</textarea>';
1113:
1114: $source=~/\.(\w+)$/;
1115:
1116: $scrout.=&hiddenfield('mime',$1);
1117:
1118: $scrout.=&selectbox('Language','language',
1119: $metadatafields{'language'},
1120: \&Apache::loncommon::languagedescription,
1121: (&Apache::loncommon::languageids),
1122: );
1123:
1124: unless ($metadatafields{'creationdate'}) {
1125: $metadatafields{'creationdate'}=time;
1126: }
1127: $scrout.=&hiddenfield('creationdate',$metadatafields{'creationdate'});
1128:
1129: $scrout.=&hiddenfield('lastrevisiondate',time);
1130:
1131:
1132: $scrout.=&textfield('Publisher/Owner','owner',
1133: $metadatafields{'owner'});
1134:
1135: # -------------------------------------------------- Correct copyright for rat.
1136: if ($style eq 'rat') {
1137: if ($metadatafields{'copyright'} eq 'public') {
1138: delete $metadatafields{'copyright'};
1139: }
1140: $scrout.=&selectbox('Copyright/Distribution','copyright',
1141: $metadatafields{'copyright'},
1142: \&Apache::loncommon::copyrightdescription,
1143: (grep !/^public$/,(&Apache::loncommon::copyrightids)));
1144: }
1145: else {
1146: $scrout.=&selectbox('Copyright/Distribution','copyright',
1147: $metadatafields{'copyright'},
1148: \&Apache::loncommon::copyrightdescription,
1149: (&Apache::loncommon::copyrightids));
1150: }
1151:
1152: my $copyright_help =
1153: Apache::loncommon::help_open_topic('Publishing_Copyright');
1154: $scrout =~ s/DISTRIBUTION:/'DISTRIBUTION: ' . $copyright_help/ge;
1155: return $scrout.
1156: '<p><input type="submit" value="Finalize Publication" /></p></form>';
1157: # =============================================================================
1158: # BATCH MODE
1159: #
1160: } else {
1161: # Transfer metadata directly to environment for stage 2
1162: foreach (keys %metadatafields) {
1163: $ENV{'form.'.$_}=$metadatafields{$_};
1164: }
1165: $ENV{'form.addkey'}='';
1166: $ENV{'form.keywords'}='';
1167: foreach (keys %keywords) {
1168: if ($metadatafields{'keywords'}) {
1169: if ($metadatafields{'keywords'}=~/$_/) {
1170: $ENV{'form.keywords'}.=$_.',';
1171: }
1172: } elsif (&Apache::loncommon::keyword($_)) {
1173: $ENV{'form.keywords'}.=$_.',';
1174: }
1175: }
1176: $ENV{'form.keywords'}=~s/\,$//;
1177: unless ($ENV{'form.creationdate'}) { $ENV{'form.creationdate'}=time; }
1178: $ENV{'form.lastrevisiondate'}=time;
1179: if ((($style eq 'rat') && ($ENV{'form.copyright'} eq 'public')) ||
1180: (!$ENV{'form.copyright'})) {
1181: $ENV{'form.copyright'}='default';
1182: }
1183: $ENV{'form.allmeta'}=&Apache::lonnet::escape($allmeta);
1184: return $scrout;
1185: }
1186: }
1187:
1188: #########################################
1189: #########################################
1190:
1191: =pod
1192:
1193: =item B<phasetwo>
1194:
1195: Render second interface showing status of publication steps.
1196: This is publication step two.
1197:
1198: Parameters:
1199:
1200: =over 4
1201:
1202: =item I<$source>
1203:
1204: =item I<$target>
1205:
1206: =item I<$style>
1207:
1208: =item I<$distarget>
1209:
1210: =back
1211:
1212: Returns:
1213:
1214: =over 4
1215:
1216: =item Scalar string
1217:
1218: String contains status (errors and warnings) and information associated with
1219: the server's attempts at publication.
1220:
1221: =cut
1222:
1223: #'stupid emacs
1224: #########################################
1225: #########################################
1226: sub phasetwo {
1227:
1228: my ($r,$source,$target,$style,$distarget,$batch)=@_;
1229: my $logfile;
1230: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
1231: return
1232: '<font color=red>No write permission to user directory, FAIL</font>';
1233: }
1234: print $logfile
1235: "\n================= Publish ".localtime()." Phase Two ================\n";
1236:
1237: %metadatafields=();
1238: %metadatakeys=();
1239:
1240: &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
1241:
1242: $metadatafields{'title'}=$ENV{'form.title'};
1243: $metadatafields{'author'}=$ENV{'form.author'};
1244: $metadatafields{'subject'}=$ENV{'form.subject'};
1245: $metadatafields{'notes'}=$ENV{'form.notes'};
1246: $metadatafields{'abstract'}=$ENV{'form.abstract'};
1247: $metadatafields{'mime'}=$ENV{'form.mime'};
1248: $metadatafields{'language'}=$ENV{'form.language'};
1249: $metadatafields{'creationdate'}=
1250: &sqltime($ENV{'form.creationdate'});
1251: $metadatafields{'lastrevisiondate'}=
1252: &sqltime($ENV{'form.lastrevisiondate'});
1253: $metadatafields{'owner'}=$ENV{'form.owner'};
1254: $metadatafields{'copyright'}=$ENV{'form.copyright'};
1255: $metadatafields{'dependencies'}=$ENV{'form.dependencies'};
1256:
1257: my $allkeywords=$ENV{'form.addkey'};
1258: if (exists($ENV{'form.keywords'})) {
1259: if (ref($ENV{'form.keywords'})) {
1260: $allkeywords .= ','.join(',',@{$ENV{'form.keywords'}});
1261: } else {
1262: $allkeywords .= ','.$ENV{'form.keywords'};
1263: }
1264: }
1265: $allkeywords=~s/\W+/\,/;
1266: $allkeywords=~s/^\,//;
1267: $metadatafields{'keywords'}=$allkeywords;
1268:
1269: {
1270: print $logfile "\nWrite metadata file for ".$source;
1271: my $mfh;
1272: unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
1273: return
1274: '<font color=red>Could not write metadata, FAIL</font>';
1275: }
1276: foreach (sort keys %metadatafields) {
1277: unless ($_=~/\./) {
1278: my $unikey=$_;
1279: $unikey=~/^([A-Za-z]+)/;
1280: my $tag=$1;
1281: $tag=~tr/A-Z/a-z/;
1282: print $mfh "\n\<$tag";
1283: foreach (split(/\,/,$metadatakeys{$unikey})) {
1284: my $value=$metadatafields{$unikey.'.'.$_};
1285: $value=~s/\"/\'\'/g;
1286: print $mfh ' '.$_.'="'.$value.'"';
1287: }
1288: print $mfh '>'.
1289: &HTML::Entities::encode($metadatafields{$unikey})
1290: .'</'.$tag.'>';
1291: }
1292: }
1293: $r->print('<p>Wrote Metadata');
1294: print $logfile "\nWrote metadata";
1295: }
1296:
1297: # -------------------------------- Synchronize entry with SQL metadata database
1298:
1299: $metadatafields{'url'} = $distarget;
1300: $metadatafields{'version'} = 'current';
1301: unless ($metadatafields{'copyright'} eq 'priv') {
1302: my ($error,$success) = &store_metadata(\%metadatafields);
1303: if ($success) {
1304: $r->print('<p>Synchronized SQL metadata database');
1305: print $logfile "\nSynchronized SQL metadata database";
1306: } else {
1307: $r->print($error);
1308: print $logfile "\n".$error;
1309: }
1310: } else {
1311: $r->print('<p>Private Publication - did not synchronize database');
1312: print $logfile "\nPrivate: Did not synchronize data into ".
1313: "SQL metadata database";
1314: }
1315: # ----------------------------------------------------------- Copy old versions
1316:
1317: if (-e $target) {
1318: my $filename;
1319: my $maxversion=0;
1320: $target=~/(.*)\/([^\/]+)\.(\w+)$/;
1321: my $srcf=$2;
1322: my $srct=$3;
1323: my $srcd=$1;
1324: unless ($srcd=~/^\/home\/httpd\/html\/res/) {
1325: print $logfile "\nPANIC: Target dir is ".$srcd;
1326: return "<font color=red>Invalid target directory, FAIL</font>";
1327: }
1328: opendir(DIR,$srcd);
1329: while ($filename=readdir(DIR)) {
1330: if (-l $srcd.'/'.$filename) {
1331: unlink($srcd.'/'.$filename);
1332: unlink($srcd.'/'.$filename.'.meta');
1333: } else {
1334: if ($filename=~/$srcf\.(\d+)\.$srct$/) {
1335: $maxversion=($1>$maxversion)?$1:$maxversion;
1336: }
1337: }
1338: }
1339: closedir(DIR);
1340: $maxversion++;
1341: $r->print('<p>Creating old version '.$maxversion);
1342: print $logfile "\nCreating old version ".$maxversion;
1343:
1344: my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
1345:
1346: if (copy($target,$copyfile)) {
1347: print $logfile "Copied old target to ".$copyfile."\n";
1348: $r->print('<p>Copied old target file');
1349: } else {
1350: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
1351: return "<font color=red>Failed to copy old target, $!, FAIL</font>";
1352: }
1353:
1354: # --------------------------------------------------------------- Copy Metadata
1355:
1356: $copyfile=$copyfile.'.meta';
1357:
1358: if (copy($target.'.meta',$copyfile)) {
1359: print $logfile "Copied old target metadata to ".$copyfile."\n";
1360: $r->print('<p>Copied old metadata')
1361: } else {
1362: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1363: if (-e $target.'.meta') {
1364: return
1365: "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
1366: }
1367: }
1368:
1369:
1370: } else {
1371: $r->print('<p>Initial version');
1372: print $logfile "\nInitial version";
1373: }
1374:
1375: # ---------------------------------------------------------------- Write Source
1376: my $copyfile=$target;
1377:
1378: my @parts=split(/\//,$copyfile);
1379: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1380:
1381: my $count;
1382: for ($count=5;$count<$#parts;$count++) {
1383: $path.="/$parts[$count]";
1384: if ((-e $path)!=1) {
1385: print $logfile "\nCreating directory ".$path;
1386: $r->print('<p>Created directory '.$parts[$count]);
1387: mkdir($path,0777);
1388: }
1389: }
1390:
1391: if (copy($source,$copyfile)) {
1392: print $logfile "\nCopied original source to ".$copyfile."\n";
1393: $r->print('<p>Copied source file');
1394: } else {
1395: print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
1396: return "<font color=red>Failed to copy source, $!, FAIL</font>";
1397: }
1398:
1399: # --------------------------------------------------------------- Copy Metadata
1400:
1401: $copyfile=$copyfile.'.meta';
1402:
1403: if (copy($source.'.meta',$copyfile)) {
1404: print $logfile "\nCopied original metadata to ".$copyfile."\n";
1405: $r->print('<p>Copied metadata');
1406: } else {
1407: print $logfile "\nUnable to write metadata ".$copyfile.':'.$!."\n";
1408: return
1409: "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
1410: }
1411: $r->rflush;
1412: # --------------------------------------------------- Send update notifications
1413:
1414: my @subscribed=&get_subscribed_hosts($target);
1415: foreach my $subhost (@subscribed) {
1416: $r->print('<p>Notifying host '.$subhost.':');$r->rflush;
1417: print $logfile "\nNotifying host ".$subhost.':';
1418: my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
1419: $r->print($reply.'<br />');$r->rflush;
1420: print $logfile $reply;
1421: }
1422:
1423: # ---------------------------------------- Send update notifications, meta only
1424:
1425: my @subscribedmeta=&get_subscribed_hosts("$target.meta");
1426: foreach my $subhost (@subscribedmeta) {
1427: $r->print('<p>Notifying host for metadata only '.$subhost.':');$r->rflush;
1428: print $logfile "\nNotifying host for metadata only ".$subhost.':';
1429: my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
1430: $subhost);
1431: $r->print($reply.'<br />');$r->rflush;
1432: print $logfile $reply;
1433: }
1434:
1435: # --------------------------------------------------- Notify subscribed courses
1436: my %courses=&coursedependencies($target);
1437: my $now=time;
1438: foreach (keys %courses) {
1439: $r->print('<p>Notifying course '.$_.':');$r->rflush;
1440: print $logfile "\nNotifying host ".$_.':';
1441: my ($cdom,$cname)=split(/\_/,$_);
1442: my $reply=&Apache::lonnet::cput
1443: ('versionupdate',{$target => $now},$cdom,$cname);
1444: $r->print($reply.'<br />');$r->rflush;
1445: print $logfile $reply;
1446: }
1447: # ------------------------------------------------ Provide link to new resource
1448: unless ($batch) {
1449: my $thisdistarget=$target;
1450: $thisdistarget=~s/^$docroot//;
1451:
1452: my $thissrc=$source;
1453: $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
1454:
1455: my $thissrcdir=$thissrc;
1456: $thissrcdir=~s/\/[^\/]+$/\//;
1457:
1458:
1459: $r->print(
1460: '<hr><a href="'.$thisdistarget.'"><font size="+2">'.
1461: 'View Published Version</font></a>'.
1462: '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
1463: '<p><a href="'.$thissrcdir.
1464: '"><font size="+2">Back to Source Directory</font></a>');
1465: }
1466: }
1467:
1468: #########################################
1469:
1470: sub batchpublish {
1471: my ($r,$srcfile,$targetfile)=@_;
1472: my $thisdisfn=$srcfile;
1473: $thisdisfn=~s/\/home\/korte\/public_html\///;
1474: $srcfile=~s/\/+/\//g;
1475:
1476: my $docroot=$r->dir_config('lonDocRoot');
1477: my $thisdistarget=$targetfile;
1478: $thisdistarget=~s/^$docroot//;
1479:
1480:
1481: undef %metadatafields;
1482: undef %metadatakeys;
1483: %metadatafields=();
1484: %metadatakeys=();
1485: $srcfile=~/\.(\w+)$/;
1486: my $thistype=$1;
1487:
1488:
1489: my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
1490:
1491: $r->print('<h2>Publishing <tt>'.$thisdisfn.'</tt></h2>');
1492:
1493: # phase one takes
1494: # my ($source,$target,$style,$batch)=@_;
1495: $r->print('<p>'.&publish($srcfile,$targetfile,$thisembstyle,1).'</p>');
1496: # phase two takes
1497: # my ($source,$target,$style,$distarget,batch)=@_;
1498: # $ENV{'form.allmeta'},$ENV{'form.title'},$ENV{'form.author'},...
1499: $r->print('<p>');
1500: &phasetwo($r,$srcfile,$targetfile,$thisembstyle,$thisdistarget,1);
1501: $r->print('</p>');
1502: return '';
1503: }
1504:
1505: #########################################
1506:
1507: sub publishdirectory {
1508: my ($r,$fn,$thisdisfn)=@_;
1509: my $resdir=
1510: $Apache::lonnet::perlvar{'lonDocRoot'}.'/res/'.$cudom.'/'.$cuname.'/'.
1511: $thisdisfn;
1512: $r->print('<h1>Directory <tt>'.$thisdisfn.'</tt></h1>'.
1513: 'Target: <tt>'.$resdir.'</tt><br />');
1514:
1515: my $dirptr=16384; # Mask indicating a directory in stat.cmode.
1516:
1517: opendir(DIR,$fn);
1518: my @files=sort(readdir(DIR));
1519: foreach my $filename (@files) {
1520: my ($cdev,$cino,$cmode,$cnlink,
1521: $cuid,$cgid,$crdev,$csize,
1522: $catime,$cmtime,$cctime,
1523: $cblksize,$cblocks)=stat($fn.'/'.$filename);
1524:
1525: my $extension='';
1526: if ($filename=~/\.(\w+)$/) { $extension=$1; }
1527: if ($cmode&$dirptr) {
1528: if (($filename!~/^\./) && ($ENV{'form.pubrec'})) {
1529: &publishdirectory($r,$fn.'/'.$filename,$thisdisfn.'/'.$filename);
1530: }
1531: } elsif ((&Apache::loncommon::fileembstyle($extension) ne 'hdn') &&
1532: ($filename!~/^[\#\.]/) && ($filename!~/\~$/)) {
1533: # find out publication status and/or exiting metadata
1534: my $publishthis=0;
1535: if (-e $resdir.'/'.$filename) {
1536: my ($rdev,$rino,$rmode,$rnlink,
1537: $ruid,$rgid,$rrdev,$rsize,
1538: $ratime,$rmtime,$rctime,
1539: $rblksize,$rblocks)=stat($resdir.'/'.$filename);
1540: if ($rmtime<$cmtime) {
1541: # previously published, modified now
1542: $publishthis=1;
1543: }
1544: } else {
1545: # never published
1546: $publishthis=1;
1547: }
1548: if ($publishthis) {
1549: &batchpublish($r,$fn.'/'.$filename,$resdir.'/'.$filename);
1550: } else {
1551: $r->print('<br />Skipping '.$filename.'<br />');
1552: }
1553: $r->rflush();
1554: }
1555: }
1556: closedir(DIR);
1557: }
1558: #########################################
1559:
1560: =pod
1561:
1562: =item B<handler>
1563:
1564: A basic outline of the handler subroutine follows.
1565:
1566: =over 4
1567:
1568: =item *
1569:
1570: Get query string for limited number of parameters.
1571:
1572: =item *
1573:
1574: Check filename.
1575:
1576: =item *
1577:
1578: File is there and owned, init lookup tables.
1579:
1580: =item *
1581:
1582: Start page output.
1583:
1584: =item *
1585:
1586: Evaluate individual file, and then output information.
1587:
1588: =item *
1589:
1590: Publishing from $thisfn to $thistarget with $thisembstyle.
1591:
1592: =back
1593:
1594: =cut
1595:
1596: #########################################
1597: #########################################
1598: sub handler {
1599: my $r=shift;
1600:
1601: if ($r->header_only) {
1602: $r->content_type('text/html');
1603: $r->send_http_header;
1604: return OK;
1605: }
1606:
1607: # Get query string for limited number of parameters
1608:
1609: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1610: ['filename']);
1611:
1612: # -------------------------------------------------------------- Check filename
1613:
1614: my $fn=$ENV{'form.filename'};
1615:
1616:
1617: unless ($fn) {
1618: $r->log_reason($cuname.' at '.$cudom.
1619: ' trying to publish empty filename', $r->filename);
1620: return HTTP_NOT_FOUND;
1621: }
1622:
1623: ($cuname,$cudom)=
1624: &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
1625: unless (($cuname) && ($cudom)) {
1626: $r->log_reason($cuname.' at '.$cudom.
1627: ' trying to publish file '.$ENV{'form.filename'}.
1628: ' ('.$fn.') - not authorized',
1629: $r->filename);
1630: return HTTP_NOT_ACCEPTABLE;
1631: }
1632:
1633: unless (&Apache::lonnet::homeserver($cuname,$cudom)
1634: eq $r->dir_config('lonHostID')) {
1635: $r->log_reason($cuname.' at '.$cudom.
1636: ' trying to publish file '.$ENV{'form.filename'}.
1637: ' ('.$fn.') - not homeserver ('.
1638: &Apache::lonnet::homeserver($cuname,$cudom).')',
1639: $r->filename);
1640: return HTTP_NOT_ACCEPTABLE;
1641: }
1642:
1643: $fn=~s/^http\:\/\/[^\/]+//;
1644: $fn=~s/^\/\~(\w+)/\/home\/$1\/public_html/;
1645:
1646: my $targetdir='';
1647: $docroot=$r->dir_config('lonDocRoot');
1648: if ($1 ne $cuname) {
1649: $r->log_reason($cuname.' at '.$cudom.
1650: ' trying to publish unowned file '.$ENV{'form.filename'}.
1651: ' ('.$fn.')',
1652: $r->filename);
1653: return HTTP_NOT_ACCEPTABLE;
1654: } else {
1655: $targetdir=$docroot.'/res/'.$cudom;
1656: }
1657:
1658:
1659: unless (-e $fn) {
1660: $r->log_reason($cuname.' at '.$cudom.
1661: ' trying to publish non-existing file '.$ENV{'form.filename'}.
1662: ' ('.$fn.')',
1663: $r->filename);
1664: return HTTP_NOT_FOUND;
1665: }
1666:
1667: unless ($ENV{'form.phase'} eq 'two') {
1668:
1669: # -------------------------------- File is there and owned, init lookup tables.
1670:
1671: %addid=();
1672:
1673: {
1674: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
1675: while (<$fh>=~/(\w+)\s+(\w+)/) {
1676: $addid{$1}=$2;
1677: }
1678: }
1679:
1680: %nokey=();
1681:
1682: {
1683: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
1684: while (<$fh>) {
1685: my $word=$_;
1686: chomp($word);
1687: $nokey{$word}=1;
1688: }
1689: }
1690:
1691: }
1692:
1693: # ---------------------------------------------------------- Start page output.
1694:
1695: $r->content_type('text/html');
1696: $r->send_http_header;
1697:
1698: $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
1699: $r->print(&Apache::loncommon::bodytag('Resource Publication'));
1700:
1701:
1702: my $thisfn=$fn;
1703:
1704: my $thistarget=$thisfn;
1705:
1706: $thistarget=~s/^\/home/$targetdir/;
1707: $thistarget=~s/\/public\_html//;
1708:
1709: my $thisdistarget=$thistarget;
1710: $thisdistarget=~s/^$docroot//;
1711:
1712: my $thisdisfn=$thisfn;
1713: $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
1714:
1715: if ($fn=~/\/$/) {
1716: # -------------------------------------------------------- This is a directory
1717: &publishdirectory($r,$fn,$thisdisfn);
1718:
1719: } else {
1720: # ---------------------- Evaluate individual file, and then output information.
1721: $thisfn=~/\.(\w+)$/;
1722: my $thistype=$1;
1723: my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
1724:
1725: $r->print('<h2>Publishing '.
1726: &Apache::loncommon::filedescription($thistype).' <tt>'.
1727: '<a href="/~'.$cuname.'/'.$thisdisfn.'" target="cat">'.$thisdisfn.
1728: '</a></tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
1729:
1730: if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
1731: $r->print('<h3><font color="red">Co-Author: '.$cuname.' at '.$cudom.
1732: '</font></h3>');
1733: }
1734:
1735: if (&Apache::loncommon::fileembstyle($thistype) eq 'ssi') {
1736: $r->print('<br /><a href="/adm/diff?filename=/~'.$cuname.'/'.
1737: $thisdisfn.
1738: '&versionone=priv" target="cat">Diffs with Current Version</a><p>');
1739: }
1740:
1741: # ------------------ Publishing from $thisfn to $thistarget with $thisembstyle.
1742:
1743: unless ($ENV{'form.phase'} eq 'two') {
1744: $r->print(
1745: '<hr />'.&publish($thisfn,$thistarget,$thisembstyle));
1746: } else {
1747: $r->print('<hr />');
1748: &phasetwo($r,$thisfn,$thistarget,$thisembstyle,$thisdistarget);
1749: }
1750:
1751: }
1752: $r->print('</body></html>');
1753:
1754: return OK;
1755: }
1756:
1757: 1;
1758: __END__
1759:
1760: =pod
1761:
1762: =back
1763:
1764: =cut
1765:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>