Annotation of loncom/publisher/lonpublisher.pm, revision 1.35
1.1 www 1: # The LearningOnline Network with CAPA
2: # Publication Handler
3: #
4: # (TeX Content Handler
5: #
6: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
7: #
1.15 www 8: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
1.20 www 9: # 03/23 Guy Albertelli
1.23 www 10: # 03/24,03/29,04/03 Gerd Kortemeyer
1.24 harris41 11: # 04/16/2001 Scott Harrison
1.27 www 12: # 05/03,05/05,05/07 Gerd Kortemeyer
1.30 harris41 13: # 05/28/2001 Scott Harrison
1.34 www 14: # 06/23,08/07,08/11 Gerd Kortemeyer
1.1 www 15:
16: package Apache::lonpublisher;
17:
18: use strict;
19: use Apache::File;
1.13 www 20: use File::Copy;
1.2 www 21: use Apache::Constants qw(:common :http :methods);
22: use HTML::TokeParser;
1.4 www 23: use Apache::lonxml;
1.17 albertel 24: use Apache::lonhomework;
1.27 www 25: use Apache::loncacc;
1.24 harris41 26: use DBI;
1.2 www 27:
1.3 www 28: my %addid;
1.5 www 29: my %nokey;
1.9 www 30: my %language;
1.10 www 31: my %cprtag;
32:
1.7 www 33: my %metadatafields;
34: my %metadatakeys;
35:
1.12 www 36: my $docroot;
37:
1.27 www 38: my $cuname;
39: my $cudom;
40:
1.12 www 41: # ----------------------------------------------- Evaluate string with metadata
42:
1.7 www 43: sub metaeval {
44: my $metastring=shift;
45:
46: my $parser=HTML::TokeParser->new(\$metastring);
47: my $token;
48: while ($token=$parser->get_token) {
49: if ($token->[0] eq 'S') {
50: my $entry=$token->[1];
51: my $unikey=$entry;
1.32 www 52: if (defined($token->[2]->{'package'})) {
53: $unikey.='_package_'.$token->[2]->{'package'};
54: }
1.7 www 55: if (defined($token->[2]->{'part'})) {
56: $unikey.='_'.$token->[2]->{'part'};
57: }
1.32 www 58: if (defined($token->[2]->{'id'})) {
59: $unikey.='_'.$token->[2]->{'id'};
60: }
1.7 www 61: if (defined($token->[2]->{'name'})) {
62: $unikey.='_'.$token->[2]->{'name'};
63: }
64: map {
65: $metadatafields{$unikey.'.'.$_}=$token->[2]->{$_};
66: if ($metadatakeys{$unikey}) {
67: $metadatakeys{$unikey}.=','.$_;
68: } else {
69: $metadatakeys{$unikey}=$_;
70: }
71: } @{$token->[3]};
72: if ($metadatafields{$unikey}) {
1.8 www 73: my $newentry=$parser->get_text('/'.$entry);
74: unless ($metadatafields{$unikey}=~/$newentry/) {
75: $metadatafields{$unikey}.=', '.$newentry;
76: }
1.7 www 77: } else {
78: $metadatafields{$unikey}=$parser->get_text('/'.$entry);
79: }
80: }
81: }
82: }
83:
1.12 www 84: # -------------------------------------------------------- Read a metadata file
85:
1.7 www 86: sub metaread {
87: my ($logfile,$fn)=@_;
88: unless (-e $fn) {
89: print $logfile 'No file '.$fn."\n";
90: return '<br><b>No file:</b> <tt>'.$fn.'</tt>';
91: }
92: print $logfile 'Processing '.$fn."\n";
93: my $metastring;
94: {
95: my $metafh=Apache::File->new($fn);
96: $metastring=join('',<$metafh>);
97: }
98: &metaeval($metastring);
99: return '<br><b>Processed file:</b> <tt>'.$fn.'</tt>';
100: }
101:
1.25 harris41 102: # ---------------------------- convert 'time' format into a datetime sql format
103: sub sqltime {
104: my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
105: localtime(@_[0]);
106: $mon++; $year+=1900;
107: return "$year-$mon-$mday $hour:$min:$sec";
108: }
109:
1.12 www 110: # --------------------------------------------------------- Various form fields
111:
1.8 www 112: sub textfield {
1.10 www 113: my ($title,$name,$value)=@_;
1.8 www 114: return "\n<p><b>$title:</b><br>".
1.11 www 115: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
116: }
117:
118: sub hiddenfield {
119: my ($name,$value)=@_;
120: return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
1.8 www 121: }
122:
1.9 www 123: sub selectbox {
1.10 www 124: my ($title,$name,$value,%options)=@_;
125: my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
126: map {
127: $selout.='<option value="'.$_.'"';
128: if ($_ eq $value) { $selout.=' selected'; }
129: $selout.='>'.$options{$_}.'</option>';
130: } sort keys %options;
131: return $selout.'</select>';
1.9 www 132: }
133:
1.12 www 134: # -------------------------------------------------------- Publication Step One
135:
1.34 www 136: sub makeallowed {
137: }
138:
139: sub urlfixup {
1.35 ! www 140: my ($url,$target)=@_;
! 141: my ($host)=($url=~/(?:http\:\/\/)*([^\/]+)/);
! 142: map {
! 143: if ($_ eq $host) {
! 144: $url=~s/^http\:\/\///;
! 145: $url=~s/^$host//;
! 146: }
! 147: } values %Apache::lonnet::hostname;
! 148: $url=~s/\~$cuname/res\/$cudom\/$cuname/;
! 149: if ($target) {
! 150: $target=~s/\/[^\/]+$//;
! 151: $url=&Apache::lonnet::hreflocation($target,$url);
! 152: }
! 153: return $url;
1.34 www 154: }
155:
1.2 www 156: sub publish {
1.4 www 157:
1.2 www 158: my ($source,$target,$style)=@_;
159: my $logfile;
1.4 www 160: my $scrout='';
1.23 www 161: my $allmeta='';
162: my $content='';
1.4 www 163:
1.2 www 164: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
1.7 www 165: return
166: '<font color=red>No write permission to user directory, FAIL</font>';
1.2 www 167: }
168: print $logfile
1.11 www 169: "\n\n================= Publish ".localtime()." Phase One ================\n";
1.2 www 170:
1.3 www 171: if (($style eq 'ssi') || ($style eq 'rat')) {
172: # ------------------------------------------------------- This needs processing
1.4 www 173:
174: # ----------------------------------------------------------------- Backup Copy
1.3 www 175: my $copyfile=$source.'.save';
1.13 www 176: if (copy($source,$copyfile)) {
1.3 www 177: print $logfile "Copied original file to ".$copyfile."\n";
178: } else {
1.13 www 179: print $logfile "Unable to write backup ".$copyfile.':'.$!."\n";
180: return "<font color=red>Failed to write backup copy, $!,FAIL</font>";
1.3 www 181: }
1.4 www 182: # ------------------------------------------------------------- IDs and indices
183:
184: my $maxindex=10;
185: my $maxid=10;
1.23 www 186:
1.4 www 187: my $needsfixup=0;
188:
189: {
190: my $org=Apache::File->new($source);
191: $content=join('',<$org>);
192: }
193: {
194: my $parser=HTML::TokeParser->new(\$content);
195: my $token;
196: while ($token=$parser->get_token) {
197: if ($token->[0] eq 'S') {
198: my $counter;
199: if ($counter=$addid{$token->[1]}) {
200: if ($counter eq 'id') {
201: if (defined($token->[2]->{'id'})) {
202: $maxid=
203: ($token->[2]->{'id'}>$maxid)?$token->[2]->{'id'}:$maxid;
204: } else {
205: $needsfixup=1;
206: }
207: } else {
208: if (defined($token->[2]->{'index'})) {
209: $maxindex=
210: ($token->[2]->{'index'}>$maxindex)?$token->[2]->{'index'}:$maxindex;
211: } else {
212: $needsfixup=1;
213: }
214: }
215: }
216: }
217: }
218: }
219: if ($needsfixup) {
220: print $logfile "Needs ID and/or index fixup\n".
221: "Max ID : $maxid (min 10)\n".
222: "Max Index: $maxindex (min 10)\n";
1.34 www 223: }
1.4 www 224: my $outstring='';
225: my $parser=HTML::TokeParser->new(\$content);
226: my $token;
227: while ($token=$parser->get_token) {
228: if ($token->[0] eq 'S') {
1.34 www 229: my $counter;
230: my $tag=$token->[1];
231: unless ($tag eq 'allow') {
232: my %parms=%{$token->[2]};
233: if ($counter=$addid{$tag}) {
1.4 www 234: if ($counter eq 'id') {
1.34 www 235: unless (defined($parms{'id'})) {
1.4 www 236: $maxid++;
1.34 www 237: $parms{'id'}=$maxid;
238: print $logfile 'ID: '.$tag.':'.$maxid."\n";
1.4 www 239: }
1.34 www 240: } elsif ($counter eq 'index') {
241: unless (defined($parms{'index'})) {
1.4 www 242: $maxindex++;
1.34 www 243: $parms{'index'}=$maxindex;
244: print $logfile 'Index: '.$tag.':'.$maxindex."\n";
1.4 www 245: }
246: }
1.34 www 247: }
248:
249: map {
250: if (defined($parms{$_})) {
251: my $oldurl=$parms{$_};
1.35 ! www 252: my $newurl=&urlfixup($oldurl,$target);
1.34 www 253: if ($newurl ne $oldurl) {
254: $parms{$_}=$newurl;
255: print $logfile 'URL: '.$tag.':'.$oldurl.' - '.
256: $newurl."\n";
257: }
258: &makeallowed($newurl);
259: }
260: } ('src','href','codebase');
261:
262: my $newparmstring='';
263: my $endtag='';
264: map {
265: if ($_ eq '/') {
266: $endtag=' /';
267: } else {
268: my $quote=($parms{$_}=~/\"/?"'":'"');
269: $newparmstring.=' '.$_.'='.$quote.$parms{$_}.$quote;
270: }
271: } keys %parms;
272:
273: $outstring.='<'.$tag.$newparmstring.$endtag.'>';
274: }
1.4 www 275: } elsif ($token->[0] eq 'E') {
1.34 www 276: unless ($token->[1] eq 'allow') {
277: $outstring.=$token->[2];
278: }
1.4 www 279: } else {
280: $outstring.=$token->[1];
281: }
282: }
283: {
284: my $org;
285: unless ($org=Apache::File->new('>'.$source)) {
286: print $logfile "No write permit to $source\n";
1.7 www 287: return
288: "<font color=red>No write permission to $source, FAIL</font>";
1.4 www 289: }
290: print $org $outstring;
291: }
292: $content=$outstring;
1.34 www 293:
294: if ($needsfixup) {
1.4 www 295: print $logfile "End of ID and/or index fixup\n".
296: "Max ID : $maxid (min 10)\n".
297: "Max Index: $maxindex (min 10)\n";
298: } else {
299: print $logfile "Does not need ID and/or index fixup\n";
300: }
1.7 www 301:
302: # --------------------------------------------- Initial step done, now metadata
303:
304: # ---------------------------------------- Storage for metadata keys and fields
305:
1.8 www 306: %metadatafields=();
307: %metadatakeys=();
308:
309: my %oldparmstores=();
1.7 www 310:
311: # ------------------------------------------------ First, check out environment
1.8 www 312: unless (-e $source.'.meta') {
1.7 www 313: $metadatafields{'author'}=$ENV{'environment.firstname'}.' '.
314: $ENV{'environment.middlename'}.' '.
315: $ENV{'environment.lastname'}.' '.
316: $ENV{'environment.generation'};
1.8 www 317: $metadatafields{'author'}=~s/\s+/ /g;
318: $metadatafields{'author'}=~s/\s+$//;
1.27 www 319: $metadatafields{'owner'}=$cuname.'@'.$cudom;
1.7 www 320:
321: # ------------------------------------------------ Check out directory hierachy
322:
323: my $thisdisfn=$source;
1.27 www 324: $thisdisfn=~s/^\/home\/$cuname\///;
1.7 www 325:
326: my @urlparts=split(/\//,$thisdisfn);
327: $#urlparts--;
328:
1.27 www 329: my $currentpath='/home/'.$cuname.'/';
1.7 www 330:
331: map {
332: $currentpath.=$_.'/';
333: $scrout.=&metaread($logfile,$currentpath.'default.meta');
334: } @urlparts;
335:
336: # ------------------- Clear out parameters and stores (there should not be any)
337:
338: map {
339: if (($_=~/^parameter/) || ($_=~/^stores/)) {
340: delete $metadatafields{$_};
341: }
342: } keys %metadatafields;
343:
1.8 www 344: } else {
1.7 www 345: # ---------------------- Read previous metafile, remember parameters and stores
346:
347: $scrout.=&metaread($logfile,$source.'.meta');
348:
349: map {
350: if (($_=~/^parameter/) || ($_=~/^stores/)) {
351: $oldparmstores{$_}=1;
352: delete $metadatafields{$_};
353: }
354: } keys %metadatafields;
355:
1.8 www 356: }
1.7 www 357:
1.4 www 358: # -------------------------------------------------- Parse content for metadata
359:
1.23 www 360: $allmeta=Apache::lonxml::xmlparse('meta',$content);
1.32 www 361:
1.19 albertel 362: &metaeval($allmeta);
1.7 www 363:
364: # ---------------- Find and document discrepancies in the parameters and stores
365:
366: my $chparms='';
367: map {
368: if (($_=~/^parameter/) || ($_=~/^stores/)) {
369: unless ($_=~/\.\w+$/) {
370: unless ($oldparmstores{$_}) {
371: print $logfile 'New: '.$_."\n";
372: $chparms.=$_.' ';
373: }
374: }
375: }
376: } sort keys %metadatafields;
377: if ($chparms) {
378: $scrout.='<p><b>New parameters or stored values:</b> '.
379: $chparms;
380: }
381:
382: my $chparms='';
383: map {
384: if (($_=~/^parameter/) || ($_=~/^stores/)) {
1.33 www 385: unless (($metadatafields{$_.'.name'}) ||
386: ($metadatafields{$_.'.package'}) || ($_=~/\.\w+$/)) {
1.7 www 387: print $logfile 'Obsolete: '.$_."\n";
388: $chparms.=$_.' ';
389: }
390: }
391: } sort keys %oldparmstores;
392: if ($chparms) {
393: $scrout.='<p><b>Obsolete parameters or stored values:</b> '.
394: $chparms;
395: }
1.23 www 396: }
1.8 www 397: # ------------------------------------------------------- Now have all metadata
1.5 www 398:
1.8 www 399: $scrout.=
400: '<form action="/adm/publish" method="post">'.
1.11 www 401: &hiddenfield('phase','two').
402: &hiddenfield('filename',$ENV{'form.filename'}).
403: &hiddenfield('allmeta',&Apache::lonnet::escape($allmeta)).
1.10 www 404: &textfield('Title','title',$metadatafields{'title'}).
405: &textfield('Author(s)','author',$metadatafields{'author'}).
406: &textfield('Subject','subject',$metadatafields{'subject'});
1.5 www 407:
408: # --------------------------------------------------- Scan content for keywords
1.7 www 409:
1.8 www 410: my $keywordout='<p><b>Keywords:</b><br><table border=2><tr>';
1.7 www 411: my $colcount=0;
412:
1.5 www 413: {
414: my $textonly=$content;
415: $textonly=~s/\<script[^\<]+\<\/script\>//g;
416: $textonly=~s/\<m\>[^\<]+\<\/m\>//g;
417: $textonly=~s/\<[^\>]*\>//g;
418: $textonly=~tr/A-Z/a-z/;
419: $textonly=~s/[\$\&][a-z]\w*//g;
420: $textonly=~s/[^a-z\s]//g;
421:
422: my %keywords=();
423: map {
424: unless ($nokey{$_}) {
425: $keywords{$_}=1;
426: }
427: } ($textonly=~m/(\w+)/g);
428:
1.12 www 429: map {
430: $keywords{$_}=1;
431: } split(/\W+/,$metadatafields{'keywords'});
1.5 www 432:
1.7 www 433: map {
1.12 www 434: $keywordout.='<td><input type=checkbox name="key.'.$_.'"';
1.8 www 435: if ($metadatafields{'keywords'}=~/$_/) {
436: $keywordout.=' checked';
437: }
438: $keywordout.='>'.$_.'</td>';
1.7 www 439: if ($colcount>10) {
440: $keywordout.="</tr><tr>\n";
441: $colcount=0;
442: }
443: $colcount++;
444: } sort keys %keywords;
445: $keywordout.='</tr></table>';
1.5 www 446:
447: }
1.4 www 448:
1.7 www 449: $scrout.=$keywordout;
1.9 www 450:
1.12 www 451: $scrout.=&textfield('Additional Keywords','addkey','');
452:
1.10 www 453: $scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
1.9 www 454:
455: $scrout.=
456: '<p><b>Abstract:</b><br><textarea cols=80 rows=5 name=abstract>'.
457: $metadatafields{'abstract'}.'</textarea>';
458:
1.11 www 459: $source=~/\.(\w+)$/;
460:
461: $scrout.=&hiddenfield('mime',$1);
462:
1.10 www 463: $scrout.=&selectbox('Language','language',
464: $metadatafields{'language'},%language);
1.11 www 465:
466: unless ($metadatafields{'creationdate'}) {
467: $metadatafields{'creationdate'}=time;
468: }
469: $scrout.=&hiddenfield('creationdate',$metadatafields{'creationdate'});
470:
471: $scrout.=&hiddenfield('lastrevisiondate',time);
472:
1.9 www 473:
1.10 www 474: $scrout.=&textfield('Publisher/Owner','owner',
475: $metadatafields{'owner'});
476:
477: $scrout.=&selectbox('Copyright/Distribution','copyright',
478: $metadatafields{'copyright'},%cprtag);
1.9 www 479:
1.8 www 480: return $scrout.
481: '<p><input type="submit" value="Finalize Publication"></form>';
1.2 www 482: }
1.1 www 483:
1.12 www 484: # -------------------------------------------------------- Publication Step Two
485:
1.11 www 486: sub phasetwo {
487:
1.24 harris41 488: my ($source,$target,$style,$distarget)=@_;
1.11 www 489: my $logfile;
490: my $scrout='';
491:
492: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
493: return
494: '<font color=red>No write permission to user directory, FAIL</font>';
495: }
496: print $logfile
497: "\n================= Publish ".localtime()." Phase Two ================\n";
498:
499: %metadatafields=();
500: %metadatakeys=();
501:
502: &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
503:
504: $metadatafields{'title'}=$ENV{'form.title'};
505: $metadatafields{'author'}=$ENV{'form.author'};
506: $metadatafields{'subject'}=$ENV{'form.subject'};
507: $metadatafields{'notes'}=$ENV{'form.notes'};
508: $metadatafields{'abstract'}=$ENV{'form.abstract'};
509: $metadatafields{'mime'}=$ENV{'form.mime'};
510: $metadatafields{'language'}=$ENV{'form.language'};
511: $metadatafields{'creationdate'}=$ENV{'form.creationdate'};
512: $metadatafields{'lastrevisiondate'}=$ENV{'form.lastrevisiondate'};
513: $metadatafields{'owner'}=$ENV{'form.owner'};
514: $metadatafields{'copyright'}=$ENV{'form.copyright'};
1.12 www 515:
516: my $allkeywords=$ENV{'form.addkey'};
1.11 www 517: map {
1.12 www 518: if ($_=~/^form\.key\.(\w+)/) {
519: $allkeywords.=','.$1;
520: }
521: } keys %ENV;
522: $allkeywords=~s/\W+/\,/;
523: $allkeywords=~s/^\,//;
524: $metadatafields{'keywords'}=$allkeywords;
525:
526: {
527: print $logfile "\nWrite metadata file for ".$source;
528: my $mfh;
529: unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
530: return
531: '<font color=red>Could not write metadata, FAIL</font>';
532: }
533: map {
534: unless ($_=~/\./) {
535: my $unikey=$_;
536: $unikey=~/^([A-Za-z]+)/;
537: my $tag=$1;
538: $tag=~tr/A-Z/a-z/;
539: print $mfh "\n\<$tag";
540: map {
541: my $value=$metadatafields{$unikey.'.'.$_};
542: $value=~s/\"/\'\'/g;
543: print $mfh ' '.$_.'="'.$value.'"';
544: } split(/\,/,$metadatakeys{$unikey});
545: print $mfh '>'.$metadatafields{$unikey}.'</'.$tag.'>';
546: }
547: } sort keys %metadatafields;
548: $scrout.='<p>Wrote Metadata';
549: print $logfile "\nWrote metadata";
550: }
551:
1.24 harris41 552: # -------------------------------- Synchronize entry with SQL metadata database
1.25 harris41 553: my %perlvar;
554: open (CONFIG,"/etc/httpd/conf/access.conf") || die "Can't read access.conf";
555: my $configline;
556: while ($configline=<CONFIG>) {
557: if ($configline =~ /PerlSetVar/) {
558: my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
559: chomp($varvalue);
560: $perlvar{$varname}=$varvalue;
561: }
562: }
563: close(CONFIG);
564:
1.29 harris41 565: my $warning;
1.24 harris41 566: my $dbh;
567: {
568: unless (
569: $dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
570: ) {
1.29 harris41 571: $warning='<font color=red>WARNING: Cannot connect to '.
572: 'database!</font>';
573: }
574: else {
575: my %sqldatafields;
576: $sqldatafields{'url'}=$distarget;
577: my $sth=$dbh->prepare(
578: 'delete from metadata where url like binary'.
579: '"'.$sqldatafields{'url'}.'"');
580: $sth->execute();
581: map {my $field=$metadatafields{$_}; $field=~s/\"/\'\'/g;
582: $sqldatafields{$_}=$field;}
583: ('title','author','subject','keywords','notes','abstract',
584: 'mime','language','creationdate','lastrevisiondate','owner',
585: 'copyright');
586:
587: $sth=$dbh->prepare('insert into metadata values ('.
588: '"'.delete($sqldatafields{'title'}).'"'.','.
589: '"'.delete($sqldatafields{'author'}).'"'.','.
590: '"'.delete($sqldatafields{'subject'}).'"'.','.
591: '"'.delete($sqldatafields{'url'}).'"'.','.
592: '"'.delete($sqldatafields{'keywords'}).'"'.','.
593: '"'.'current'.'"'.','.
594: '"'.delete($sqldatafields{'notes'}).'"'.','.
595: '"'.delete($sqldatafields{'abstract'}).'"'.','.
596: '"'.delete($sqldatafields{'mime'}).'"'.','.
597: '"'.delete($sqldatafields{'language'}).'"'.','.
598: '"'.
599: sqltime(delete($sqldatafields{'creationdate'}))
600: .'"'.','.
601: '"'.
602: sqltime(delete(
603: $sqldatafields{'lastrevisiondate'})).'"'.','.
604: '"'.delete($sqldatafields{'owner'}).'"'.','.
605: '"'.delete(
606: $sqldatafields{'copyright'}).'"'.')');
607: $sth->execute();
608: $dbh->disconnect;
609: $scrout.='<p>Synchronized SQL metadata database';
610: print $logfile "\nSynchronized SQL metadata database";
1.24 harris41 611: }
612: }
613:
614:
1.12 www 615: # ----------------------------------------------------------- Copy old versions
616:
617: if (-e $target) {
618: my $filename;
619: my $maxversion=0;
620: $target=~/(.*)\/([^\/]+)\.(\w+)$/;
621: my $srcf=$2;
622: my $srct=$3;
623: my $srcd=$1;
624: unless ($srcd=~/^\/home\/httpd\/html\/res/) {
625: print $logfile "\nPANIC: Target dir is ".$srcd;
626: return "<font color=red>Invalid target directory, FAIL</font>";
627: }
628: opendir(DIR,$srcd);
629: while ($filename=readdir(DIR)) {
630: if ($filename=~/$srcf\.(\d+)\.$srct$/) {
631: $maxversion=($1>$maxversion)?$1:$maxversion;
632: }
633: }
634: closedir(DIR);
635: $maxversion++;
636: $scrout.='<p>Creating old version '.$maxversion;
637: print $logfile "\nCreating old version ".$maxversion;
638:
639: my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
640:
1.13 www 641: if (copy($target,$copyfile)) {
1.12 www 642: print $logfile "Copied old target to ".$copyfile."\n";
643: $scrout.='<p>Copied old target file';
644: } else {
1.13 www 645: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
646: return "<font color=red>Failed to copy old target, $!, FAIL</font>";
1.12 www 647: }
648:
649: # --------------------------------------------------------------- Copy Metadata
650:
651: $copyfile=$copyfile.'.meta';
1.13 www 652:
653: if (copy($target.'.meta',$copyfile)) {
1.14 www 654: print $logfile "Copied old target metadata to ".$copyfile."\n";
1.12 www 655: $scrout.='<p>Copied old metadata';
656: } else {
1.13 www 657: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1.14 www 658: if (-e $target.'.meta') {
659: return
1.13 www 660: "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
1.14 www 661: }
1.12 www 662: }
1.11 www 663:
664:
1.12 www 665: } else {
666: $scrout.='<p>Initial version';
667: print $logfile "\nInitial version";
668: }
669:
670: # ---------------------------------------------------------------- Write Source
671: my $copyfile=$target;
672:
673: my @parts=split(/\//,$copyfile);
674: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
675:
676: my $count;
677: for ($count=5;$count<$#parts;$count++) {
678: $path.="/$parts[$count]";
679: if ((-e $path)!=1) {
680: print $logfile "\nCreating directory ".$path;
681: $scrout.='<p>Created directory '.$parts[$count];
682: mkdir($path,0777);
683: }
684: }
685:
1.13 www 686: if (copy($source,$copyfile)) {
1.12 www 687: print $logfile "Copied original source to ".$copyfile."\n";
688: $scrout.='<p>Copied source file';
689: } else {
1.13 www 690: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
691: return "<font color=red>Failed to copy source, $!, FAIL</font>";
1.12 www 692: }
693:
694: # --------------------------------------------------------------- Copy Metadata
695:
1.13 www 696: $copyfile=$copyfile.'.meta';
697:
698: if (copy($source.'.meta',$copyfile)) {
1.12 www 699: print $logfile "Copied original metadata to ".$copyfile."\n";
700: $scrout.='<p>Copied metadata';
701: } else {
1.13 www 702: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1.12 www 703: return
1.13 www 704: "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
1.12 www 705: }
706:
707: # --------------------------------------------------- Send update notifications
708:
709: {
710:
711: my $filename;
712:
713: $target=~/(.*)\/([^\/]+)$/;
714: my $srcf=$2;
715: opendir(DIR,$1);
716: while ($filename=readdir(DIR)) {
717: if ($filename=~/$srcf\.(\w+)$/) {
718: my $subhost=$1;
719: if ($subhost ne 'meta') {
720: $scrout.='<p>Notifying host '.$subhost.':';
721: print $logfile "\nNotifying host '.$subhost.':'";
722: my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
1.20 www 723: $scrout.=$reply;
724: print $logfile $reply;
725: }
726: }
727: }
728: closedir(DIR);
729:
730: }
731:
732: # ---------------------------------------- Send update notifications, meta only
733:
734: {
735:
736: my $filename;
737:
738: $target=~/(.*)\/([^\/]+)$/;
739: my $srcf=$2.'.meta';
740: opendir(DIR,$1);
741: while ($filename=readdir(DIR)) {
742: if ($filename=~/$srcf\.(\w+)$/) {
743: my $subhost=$1;
744: if ($subhost ne 'meta') {
745: $scrout.=
746: '<p>Notifying host for metadata only '.$subhost.':';
747: print $logfile
748: "\nNotifying host for metadata only '.$subhost.':'";
749: my $reply=&Apache::lonnet::critical(
750: 'update:'.$target.'.meta',$subhost);
1.12 www 751: $scrout.=$reply;
752: print $logfile $reply;
753: }
754: }
755: }
756: closedir(DIR);
757:
758: }
759:
760: # ------------------------------------------------ Provide link to new resource
761:
762: my $thisdistarget=$target;
763: $thisdistarget=~s/^$docroot//;
764:
1.22 www 765: my $thissrc=$source;
766: $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
767:
768: my $thissrcdir=$thissrc;
769: $thissrcdir=~s/\/[^\/]+$/\//;
770:
771:
1.29 harris41 772: return $warning.$scrout.
1.22 www 773: '<hr><a href="'.$thisdistarget.'"><font size=+2>View Target</font></a>'.
774: '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
775: '<p><a href="'.$thissrcdir.
776: '"><font size=+2>Back to Source Directory</font></a>';
777:
1.11 www 778: }
779:
1.1 www 780: # ================================================================ Main Handler
781:
782: sub handler {
783: my $r=shift;
1.2 www 784:
785: if ($r->header_only) {
786: $r->content_type('text/html');
787: $r->send_http_header;
788: return OK;
789: }
790:
791: # -------------------------------------------------------------- Check filename
792:
793: my $fn=$ENV{'form.filename'};
794:
1.27 www 795:
1.2 www 796: unless ($fn) {
1.27 www 797: $r->log_reason($cuname.' at '.$cudom.
1.2 www 798: ' trying to publish empty filename', $r->filename);
799: return HTTP_NOT_FOUND;
800: }
1.4 www 801:
1.31 www 802: ($cuname,$cudom)=
803: &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
804: unless (($cuname) && ($cudom)) {
1.27 www 805: $r->log_reason($cuname.' at '.$cudom.
1.4 www 806: ' trying to publish file '.$ENV{'form.filename'}.
1.27 www 807: ' ('.$fn.') - not authorized',
808: $r->filename);
809: return HTTP_NOT_ACCEPTABLE;
810: }
811:
812: unless (&Apache::lonnet::homeserver($cuname,$cudom)
813: eq $r->dir_config('lonHostID')) {
814: $r->log_reason($cuname.' at '.$cudom.
815: ' trying to publish file '.$ENV{'form.filename'}.
816: ' ('.$fn.') - not homeserver ('.
817: &Apache::lonnet::homeserver($cuname,$cudom).')',
1.4 www 818: $r->filename);
819: return HTTP_NOT_ACCEPTABLE;
820: }
1.2 www 821:
822: $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
823:
824: my $targetdir='';
1.12 www 825: $docroot=$r->dir_config('lonDocRoot');
1.27 www 826: if ($1 ne $cuname) {
827: $r->log_reason($cuname.' at '.$cudom.
1.2 www 828: ' trying to publish unowned file '.$ENV{'form.filename'}.
829: ' ('.$fn.')',
830: $r->filename);
831: return HTTP_NOT_ACCEPTABLE;
832: } else {
1.27 www 833: $targetdir=$docroot.'/res/'.$cudom;
1.2 www 834: }
835:
836:
837: unless (-e $fn) {
1.27 www 838: $r->log_reason($cuname.' at '.$cudom.
1.2 www 839: ' trying to publish non-existing file '.$ENV{'form.filename'}.
840: ' ('.$fn.')',
841: $r->filename);
842: return HTTP_NOT_FOUND;
843: }
844:
1.11 www 845: unless ($ENV{'form.phase'} eq 'two') {
846:
1.2 www 847: # --------------------------------- File is there and owned, init lookup tables
848:
1.3 www 849: %addid=();
850:
851: {
852: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
853: while (<$fh>=~/(\w+)\s+(\w+)/) {
854: $addid{$1}=$2;
855: }
1.5 www 856: }
857:
858: %nokey=();
859:
860: {
861: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
862: map {
863: my $word=$_;
864: chomp($word);
865: $nokey{$word}=1;
1.9 www 866: } <$fh>;
867: }
868:
869: %language=();
870:
871: {
872: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/language.tab');
873: map {
1.10 www 874: $_=~/(\w+)\s+([\w\s\-]+)/;
1.9 www 875: $language{$1}=$2;
1.10 www 876: } <$fh>;
877: }
878:
879: %cprtag=();
880:
881: {
882: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/copyright.tab');
883: map {
884: $_=~/(\w+)\s+([\w\s\-]+)/;
885: $cprtag{$1}=$2;
1.5 www 886: } <$fh>;
1.3 www 887: }
1.11 www 888:
889: }
890:
1.2 www 891: # ----------------------------------------------------------- Start page output
892:
1.1 www 893: $r->content_type('text/html');
894: $r->send_http_header;
895:
896: $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
1.15 www 897: $r->print(
898: '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
1.2 www 899: my $thisfn=$fn;
900:
901: # ------------------------------------------------------------- Individual file
902: {
903: $thisfn=~/\.(\w+)$/;
904: my $thistype=$1;
905: my $thisembstyle=&Apache::lonnet::fileembstyle($thistype);
906:
907: my $thistarget=$thisfn;
908:
909: $thistarget=~s/^\/home/$targetdir/;
910: $thistarget=~s/\/public\_html//;
911:
912: my $thisdistarget=$thistarget;
913: $thisdistarget=~s/^$docroot//;
914:
915: my $thisdisfn=$thisfn;
1.27 www 916: $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
1.2 www 917:
918: $r->print('<h2>Publishing '.
919: &Apache::lonnet::filedescription($thistype).' <tt>'.
920: $thisdisfn.'</tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
1.27 www 921:
922: if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
923: $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
924: '</font></h3>');
925: }
1.26 www 926:
927: if (&Apache::lonnet::fileembstyle($thistype) eq 'ssi') {
1.28 www 928: $r->print('<br><a href="/adm/diff?filename=/~'.$cuname.'/'.
929: $thisdisfn.
1.26 www 930: '&versionone=priv" target=cat>Diffs with Current Version</a><p>');
931: }
1.11 www 932:
1.2 www 933: # ------------ We are publishing from $thisfn to $thistarget with $thisembstyle
934:
1.11 www 935: unless ($ENV{'form.phase'} eq 'two') {
1.27 www 936: $r->print(
937: '<hr>'.&publish($thisfn,$thistarget,$thisembstyle));
1.11 www 938: } else {
1.27 www 939: $r->print(
940: '<hr>'.&phasetwo($thisfn,$thistarget,$thisembstyle,$thisdistarget));
1.11 www 941: }
1.2 www 942:
1.11 www 943: }
1.1 www 944: $r->print('</body></html>');
1.15 www 945:
1.1 www 946: return OK;
947: }
948:
949: 1;
950: __END__
951:
952:
953:
954:
955:
956:
957:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>