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