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