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