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