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